ggml-sycl.cpp 686 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869
  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_SOFT_MAX_BLOCK_SIZE 1024
  2911. #define SYCL_ALIBI_BLOCK_SIZE 32
  2912. #define SYCL_DIAG_MASK_INF_BLOCK_SIZE 32
  2913. #define SYCL_QUANTIZE_BLOCK_SIZE 256
  2914. #define SYCL_DEQUANTIZE_BLOCK_SIZE 256
  2915. #define SYCL_GET_ROWS_BLOCK_SIZE 256
  2916. #define SYCL_UPSCALE_BLOCK_SIZE 256
  2917. #define SYCL_CONCAT_BLOCK_SIZE 256
  2918. #define SYCL_PAD_BLOCK_SIZE 256
  2919. #define SYCL_ACC_BLOCK_SIZE 256
  2920. #define SYCL_IM2COL_BLOCK_SIZE 256
  2921. #define SYCL_POOL2D_BLOCK_SIZE 256
  2922. // dmmv = dequantize_mul_mat_vec
  2923. #ifndef GGML_SYCL_DMMV_X
  2924. #define GGML_SYCL_DMMV_X 32
  2925. #endif
  2926. #ifndef GGML_SYCL_MMV_Y
  2927. #define GGML_SYCL_MMV_Y 1
  2928. #endif
  2929. #ifndef K_QUANTS_PER_ITERATION
  2930. #define K_QUANTS_PER_ITERATION 2
  2931. #else
  2932. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  2933. #endif
  2934. #ifndef GGML_SYCL_PEER_MAX_BATCH_SIZE
  2935. #define GGML_SYCL_PEER_MAX_BATCH_SIZE 128
  2936. #endif // GGML_SYCL_PEER_MAX_BATCH_SIZE
  2937. #define MUL_MAT_SRC1_COL_STRIDE 128
  2938. #define MAX_STREAMS 8
  2939. static dpct::queue_ptr g_syclStreams[GGML_SYCL_MAX_DEVICES][MAX_STREAMS] = {{0}};
  2940. struct ggml_tensor_extra_gpu {
  2941. void * data_device[GGML_SYCL_MAX_DEVICES]; // 1 pointer for each device for split tensors
  2942. dpct::event_ptr
  2943. events[GGML_SYCL_MAX_DEVICES]
  2944. [MAX_STREAMS]; // events for synchronizing multiple GPUs
  2945. };
  2946. class sycl_gpu_mgr {
  2947. public:
  2948. std::vector<int> gpus;
  2949. std::vector<sycl::device> devices;
  2950. sycl::queue *first_queue;
  2951. sycl::context co_ctx;
  2952. int max_compute_units = 0;
  2953. int work_group_size = 0;
  2954. std::string gpus_list = "";
  2955. /*
  2956. Use all GPUs with same top max compute units
  2957. */
  2958. sycl_gpu_mgr() {
  2959. detect_sycl_gpu_list_with_max_cu();
  2960. get_allow_gpus();
  2961. create_context_with_gpus();
  2962. }
  2963. /*
  2964. Only use the assigned GPU
  2965. */
  2966. sycl_gpu_mgr(int main_gpu_id) {
  2967. sycl::device device = dpct::dev_mgr::instance().get_device(main_gpu_id);
  2968. dpct::device_info prop;
  2969. dpct::get_device_info(prop, device);
  2970. gpus.push_back(main_gpu_id);
  2971. devices.push_back(device);
  2972. work_group_size = prop.get_max_work_group_size();
  2973. max_compute_units = prop.get_max_compute_units();
  2974. get_allow_gpus();
  2975. create_context_with_gpus();
  2976. }
  2977. void create_context_with_gpus() {
  2978. sycl::context ctx = sycl::context(devices);
  2979. assert(gpus.size() > 0);
  2980. first_queue = dpct::get_current_device().create_queue(ctx, devices[0]);
  2981. co_ctx = first_queue->get_context();
  2982. }
  2983. sycl::context &get_co_ctx() { return co_ctx; }
  2984. void get_allow_gpus() {
  2985. gpus_list = "";
  2986. for (size_t i = 0; i < gpus.size(); ++i) {
  2987. gpus_list += std::to_string(gpus[i]);
  2988. gpus_list += ",";
  2989. }
  2990. if (gpus_list.length() > 1) {
  2991. gpus_list.pop_back();
  2992. }
  2993. }
  2994. bool is_allowed_gpu(int device_id) {
  2995. return std::find(gpus.begin(), gpus.end(), device_id) != gpus.end();
  2996. }
  2997. void detect_sycl_gpu_list_with_max_cu() try {
  2998. int device_count = dpct::dev_mgr::instance().device_count();
  2999. for (int id = 0; id < device_count; id++) {
  3000. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  3001. if (!device.is_gpu())
  3002. continue;
  3003. dpct::device_info prop;
  3004. dpct::get_device_info(prop, device);
  3005. if (max_compute_units < prop.get_max_compute_units())
  3006. max_compute_units = prop.get_max_compute_units();
  3007. }
  3008. for (int id = 0; id < device_count; id++) {
  3009. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  3010. if (!device.is_gpu())
  3011. continue;
  3012. dpct::device_info prop;
  3013. dpct::get_device_info(prop, device);
  3014. if (max_compute_units == prop.get_max_compute_units() &&
  3015. is_ext_oneapi_device(device)) {
  3016. gpus.push_back(id);
  3017. devices.push_back(device);
  3018. work_group_size = prop.get_max_work_group_size();
  3019. }
  3020. }
  3021. return;
  3022. } catch (sycl::exception const &exc) {
  3023. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3024. << ", line:" << __LINE__ << std::endl;
  3025. std::exit(1);
  3026. }
  3027. int get_gpu_count() { return (int)gpus.size(); }
  3028. int get_index(int id) {
  3029. for (int i = 0; i < (int)gpus.size(); i++) {
  3030. if (gpus[i] == id)
  3031. return i;
  3032. }
  3033. printf("miss to get device index by id=%d\n", id);
  3034. GGML_ASSERT(false);
  3035. }
  3036. int get_next_index(int id) {
  3037. int cur_index = get_index(id);
  3038. for (int i = cur_index + 1; i < (int)gpus.size(); i++) {
  3039. if (gpus[i] == id)
  3040. return i;
  3041. }
  3042. GGML_ASSERT(false);
  3043. }
  3044. bool is_ext_oneapi_device(const sycl::device &dev) {
  3045. sycl::backend dev_backend = dev.get_backend();
  3046. if (dev_backend == sycl::backend::ext_oneapi_level_zero ||
  3047. dev_backend == sycl::backend::ext_oneapi_cuda ||
  3048. dev_backend == sycl::backend::ext_oneapi_hip)
  3049. return true;
  3050. return false;
  3051. }
  3052. };
  3053. static sycl_gpu_mgr *g_sycl_gpu_mgr = NULL;
  3054. static int g_device_count = -1;
  3055. static int g_all_sycl_device_count = -1;
  3056. static int g_main_device = -1;
  3057. static int g_main_device_id = -1;
  3058. static bool g_ggml_backend_sycl_buffer_type_initialized = false;
  3059. static std::array<float, GGML_SYCL_MAX_DEVICES> g_default_tensor_split = {};
  3060. static float g_tensor_split[GGML_SYCL_MAX_DEVICES] = {0};
  3061. static ggml_sycl_backend_gpu_mode g_ggml_sycl_backend_gpu_mode = SYCL_UNSET_GPU_MODE;
  3062. struct sycl_device_capabilities {
  3063. int cc; // compute capability
  3064. bool vmm; // virtual memory support
  3065. size_t vmm_granularity; // granularity of virtual memory
  3066. int device_id;
  3067. };
  3068. static sycl_device_capabilities g_device_caps[GGML_SYCL_MAX_DEVICES] = { {0, false, 0, -1} };
  3069. struct sycl_device_id2index {
  3070. int index;
  3071. };
  3072. static void * g_scratch_buffer = nullptr;
  3073. static size_t g_scratch_size = 0; // disabled by default
  3074. static size_t g_scratch_offset = 0;
  3075. static dpct::queue_ptr g_sycl_handles[GGML_SYCL_MAX_DEVICES] = {nullptr};
  3076. int get_main_device(){
  3077. return g_main_device;
  3078. }
  3079. [[noreturn]]
  3080. static void bad_arch(const sycl::stream &stream_ct1) {
  3081. stream_ct1 << "ERROR: ggml-sycl was compiled without support for the "
  3082. "current GPU architecture.\n";
  3083. // __trap();
  3084. std::exit(1);
  3085. (void) bad_arch; // suppress unused function warning
  3086. }
  3087. /*
  3088. device_index: device index from 0 to n (continue numbers).
  3089. It is used for device select/set in SYCL backend internal data structure.
  3090. */
  3091. void check_allow_gpu_index(const int device_index) {
  3092. if (device_index >= g_device_count) {
  3093. char error_buf[256];
  3094. snprintf(error_buf, sizeof(error_buf),
  3095. "%s error: device_index:%d is out of range: [0-%d]", __func__,
  3096. device_index, g_device_count - 1);
  3097. fprintf(stderr, "%s\n", error_buf);
  3098. assert(false);
  3099. }
  3100. }
  3101. /*
  3102. device_id: device ID is shown by ggml_backend_sycl_print_sycl_devices().
  3103. It is only used to set current working device.
  3104. */
  3105. void check_allow_gpu_id(const int device_id) {
  3106. if (!g_sycl_gpu_mgr->is_allowed_gpu(device_id)) {
  3107. char error_buf[256];
  3108. snprintf(error_buf, sizeof(error_buf),
  3109. "error: cannot set device=%d, which is not allowed. Please "
  3110. "set GPU ID in: [%s]",
  3111. device_id, g_sycl_gpu_mgr->gpus_list.c_str());
  3112. fprintf(stderr, "%s\n", error_buf);
  3113. throw std::invalid_argument(error_buf);
  3114. }
  3115. }
  3116. int get_current_device_id() {
  3117. return dpct::dev_mgr::instance().current_device_id();
  3118. }
  3119. inline dpct::err0 ggml_sycl_set_device(const int device) try {
  3120. int device_id = g_sycl_gpu_mgr->gpus[device];
  3121. check_allow_gpu_id(device_id);
  3122. int current_device_id;
  3123. SYCL_CHECK(CHECK_TRY_ERROR(current_device_id = get_current_device_id()));
  3124. // GGML_SYCL_DEBUG("ggml_sycl_set_device device_id=%d,
  3125. // current_device_id=%d\n", device, current_device);
  3126. if (device_id == current_device_id) {
  3127. return 0;
  3128. }
  3129. return CHECK_TRY_ERROR(dpct::select_device(device_id));
  3130. } catch (sycl::exception const &exc) {
  3131. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3132. << ", line:" << __LINE__ << std::endl;
  3133. crash();
  3134. std::exit(1);
  3135. }
  3136. void log_ggml_var_device(const char*name, float *src, size_t total_elements, bool src_on_device){
  3137. if(!g_ggml_sycl_debug) return;
  3138. if(!src){
  3139. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3140. return;
  3141. }
  3142. char filename[1024];
  3143. sprintf(filename, "%s.txt", name);
  3144. printf("GGML Tensor:%s save to %s\n", name, filename);
  3145. size_t total_size = total_elements*sizeof(float);
  3146. float *local_buf = NULL;
  3147. if(src_on_device) {
  3148. local_buf = (float *) ggml_sycl_host_malloc(total_size);
  3149. ggml_sycl_set_device(g_main_device);
  3150. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3151. main_stream->memcpy(local_buf, src, total_size).wait();
  3152. }
  3153. else {
  3154. local_buf = (float *)src;
  3155. }
  3156. std::ofstream logfile;
  3157. logfile.open(filename);
  3158. for(size_t i=0; i<total_elements; i++){
  3159. logfile << local_buf[i] <<" ";
  3160. if((i+1)%20 ==0) logfile <<std::endl;
  3161. }
  3162. logfile <<std::endl;
  3163. logfile.close();
  3164. if(src_on_device) ggml_sycl_host_free(local_buf);
  3165. }
  3166. void log_ggml_var_device_fp16(const char*name, sycl::half *src, size_t total_elements, bool src_on_device){
  3167. if(!g_ggml_sycl_debug) return;
  3168. if(!src){
  3169. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3170. return;
  3171. }
  3172. char filename[1024];
  3173. sprintf(filename, "%s.txt", name);
  3174. printf("GGML Tensor:%s save to %s\n", name, filename);
  3175. size_t total_size = total_elements*sizeof(sycl::half);
  3176. sycl::half *local_buf = NULL;
  3177. if(src_on_device) {
  3178. local_buf = (sycl::half *) ggml_sycl_host_malloc(total_size);
  3179. ggml_sycl_set_device(g_main_device);
  3180. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3181. main_stream->memcpy(local_buf, src, total_size).wait();
  3182. }
  3183. else {
  3184. local_buf = (sycl::half *)src;
  3185. }
  3186. std::ofstream logfile;
  3187. logfile.open(filename);
  3188. for(size_t i=0; i<total_elements; i++){
  3189. logfile << local_buf[i] <<" ";
  3190. if((i+1)%20 ==0) logfile <<std::endl;
  3191. }
  3192. logfile <<std::endl;
  3193. logfile.close();
  3194. if(src_on_device) ggml_sycl_host_free(local_buf);
  3195. }
  3196. //todo: debug for crash in some case
  3197. void print_ggml_tensor(const char*name, struct ggml_tensor *src){
  3198. if(!g_ggml_sycl_debug) return;
  3199. if(!src){
  3200. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3201. return;
  3202. }
  3203. size_t total_elements = ggml_nelements(src);
  3204. const bool src_on_device = src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  3205. float *src_data =NULL;
  3206. if(src_on_device) {
  3207. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  3208. src_data = (float*)src_extra->data_device[g_main_device];
  3209. }
  3210. else {
  3211. src_data = (float *)src->data;
  3212. }
  3213. log_ggml_var_device(name, src_data, total_elements, src_on_device);
  3214. }
  3215. static int log_file_name_idx=0;
  3216. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt) {
  3217. stop_cnt = 4;
  3218. if(log_file_name_idx>=stop_cnt) return;
  3219. char filename[1280];
  3220. sprintf(filename, "%s_%07d", name, log_file_name_idx);
  3221. log_file_name_idx++;
  3222. print_ggml_tensor(filename, src);
  3223. }
  3224. static __dpct_inline__ float warp_reduce_sum(float x,
  3225. const sycl::nd_item<3> &item_ct1) {
  3226. #pragma unroll
  3227. for (int mask = 16; mask > 0; mask >>= 1) {
  3228. /*
  3229. DPCT1096:98: The right-most dimension of the work-group used in the SYCL
  3230. kernel that calls this function may be less than "32". The function
  3231. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3232. CPU device. Modify the size of the work-group to ensure that the value
  3233. of the right-most dimension is a multiple of "32".
  3234. */
  3235. x += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), x, mask);
  3236. }
  3237. return x;
  3238. }
  3239. static __dpct_inline__ sycl::float2
  3240. warp_reduce_sum(sycl::float2 a, const sycl::nd_item<3> &item_ct1) {
  3241. #pragma unroll
  3242. for (int mask = 16; mask > 0; mask >>= 1) {
  3243. a.x() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.x(),
  3244. mask);
  3245. a.y() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.y(),
  3246. mask);
  3247. }
  3248. return a;
  3249. }
  3250. static __dpct_inline__ float warp_reduce_max(float x,
  3251. const sycl::nd_item<3> &item_ct1) {
  3252. #pragma unroll
  3253. for (int mask = 16; mask > 0; mask >>= 1) {
  3254. /*
  3255. DPCT1096:97: The right-most dimension of the work-group used in the SYCL
  3256. kernel that calls this function may be less than "32". The function
  3257. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3258. CPU device. Modify the size of the work-group to ensure that the value
  3259. of the right-most dimension is a multiple of "32".
  3260. */
  3261. x = sycl::fmax(x, dpct::permute_sub_group_by_xor(
  3262. item_ct1.get_sub_group(), x, mask));
  3263. }
  3264. return x;
  3265. }
  3266. static __dpct_inline__ float op_repeat(const float a, const float b) {
  3267. return b;
  3268. GGML_UNUSED(a);
  3269. }
  3270. static __dpct_inline__ float op_add(const float a, const float b) {
  3271. return a + b;
  3272. }
  3273. static __dpct_inline__ float op_mul(const float a, const float b) {
  3274. return a * b;
  3275. }
  3276. static __dpct_inline__ float op_div(const float a, const float b) {
  3277. return a / b;
  3278. }
  3279. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3280. static void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3281. int ne0, int ne1, int ne2, int ne3,
  3282. int ne10, int ne11, int ne12, int ne13,
  3283. /*int s0, */ int s1, int s2, int s3,
  3284. /*int s10,*/ int s11, int s12, int s13,
  3285. const sycl::nd_item<3> &item_ct1) {
  3286. const int i0s = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3287. item_ct1.get_local_id(2);
  3288. const int i1 = (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  3289. item_ct1.get_local_id(1));
  3290. const int i2 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3291. item_ct1.get_local_id(0)) /
  3292. ne3;
  3293. const int i3 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3294. item_ct1.get_local_id(0)) %
  3295. ne3;
  3296. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3297. return;
  3298. }
  3299. const int i11 = i1 % ne11;
  3300. const int i12 = i2 % ne12;
  3301. const int i13 = i3 % ne13;
  3302. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3303. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3304. const size_t i_dst = i_src0;
  3305. const src0_t * src0_row = src0 + i_src0;
  3306. const src1_t * src1_row = src1 + i_src1;
  3307. dst_t * dst_row = dst + i_dst;
  3308. for (int i0 = i0s; i0 < ne0;
  3309. i0 += item_ct1.get_local_range(2) * item_ct1.get_group_range(2)) {
  3310. const int i10 = i0 % ne10;
  3311. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3312. }
  3313. }
  3314. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3315. static void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3316. int ne0, int ne1, int ne2, int ne3,
  3317. int ne10, int ne11, int ne12, int ne13,
  3318. /*int s0, */ int s1, int s2, int s3,
  3319. /*int s10,*/ int s11, int s12, int s13,
  3320. const sycl::nd_item<3> &item_ct1) {
  3321. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3322. item_ct1.get_local_id(2);
  3323. const int i3 = i/(ne2*ne1*ne0);
  3324. const int i2 = (i/(ne1*ne0)) % ne2;
  3325. const int i1 = (i/ne0) % ne1;
  3326. const int i0 = i % ne0;
  3327. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3328. return;
  3329. }
  3330. const int i11 = i1 % ne11;
  3331. const int i12 = i2 % ne12;
  3332. const int i13 = i3 % ne13;
  3333. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3334. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3335. const size_t i_dst = i_src0;
  3336. const src0_t * src0_row = src0 + i_src0;
  3337. const src1_t * src1_row = src1 + i_src1;
  3338. dst_t * dst_row = dst + i_dst;
  3339. const int i10 = i0 % ne10;
  3340. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3341. }
  3342. static void acc_f32(const float * x, const float * y, float * dst, const int ne,
  3343. const int ne10, const int ne11, const int ne12,
  3344. const int nb1, const int nb2, int offset, const sycl::nd_item<3> &item_ct1) {
  3345. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3346. item_ct1.get_local_id(2);
  3347. if (i >= ne) {
  3348. return;
  3349. }
  3350. int src1_idx = i - offset;
  3351. int oz = src1_idx / nb2;
  3352. int oy = (src1_idx - (oz * nb2)) / nb1;
  3353. int ox = src1_idx % nb1;
  3354. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  3355. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  3356. } else {
  3357. dst[i] = x[i];
  3358. }
  3359. }
  3360. static void gelu_f32(const float * x, float * dst, const int k,
  3361. const sycl::nd_item<3> &item_ct1) {
  3362. const float GELU_COEF_A = 0.044715f;
  3363. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  3364. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3365. item_ct1.get_local_id(2);
  3366. if (i >= k) {
  3367. return;
  3368. }
  3369. float xi = x[i];
  3370. dst[i] = 0.5f * xi *
  3371. (1.0f +
  3372. sycl::tanh(SQRT_2_OVER_PI * xi * (1.0f + GELU_COEF_A * xi * xi)));
  3373. }
  3374. static void silu_f32(const float * x, float * dst, const int k,
  3375. const sycl::nd_item<3> &item_ct1) {
  3376. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3377. item_ct1.get_local_id(2);
  3378. if (i >= k) {
  3379. return;
  3380. }
  3381. dst[i] = x[i] / (1.0f + sycl::native::exp(-x[i]));
  3382. }
  3383. static void gelu_quick_f32(const float *x, float *dst, int k,
  3384. const sycl::nd_item<3> &item_ct1) {
  3385. const float GELU_QUICK_COEF = -1.702f;
  3386. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3387. item_ct1.get_local_id(2);
  3388. if (i >= k) {
  3389. return;
  3390. }
  3391. dst[i] = x[i] * (1.0f / (1.0f + sycl::native::exp(GELU_QUICK_COEF * x[i])));
  3392. }
  3393. static void tanh_f32(const float *x, float *dst, int k,
  3394. const sycl::nd_item<3> &item_ct1) {
  3395. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3396. item_ct1.get_local_id(2);
  3397. if (i >= k) {
  3398. return;
  3399. }
  3400. dst[i] = sycl::tanh((float)(x[i]));
  3401. }
  3402. static void relu_f32(const float * x, float * dst, const int k,
  3403. const sycl::nd_item<3> &item_ct1) {
  3404. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3405. item_ct1.get_local_id(2);
  3406. if (i >= k) {
  3407. return;
  3408. }
  3409. dst[i] = sycl::fmax((float)(x[i]), (float)0);
  3410. }
  3411. static void hardsigmoid_f32(const float * x, float * dst, const int k,
  3412. const sycl::nd_item<3> &item_ct1) {
  3413. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3414. item_ct1.get_local_id(2);
  3415. if (i >= k) {
  3416. return;
  3417. }
  3418. dst[i] = sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3419. }
  3420. static void hardswish_f32(const float * x, float * dst, const int k,
  3421. const sycl::nd_item<3> &item_ct1) {
  3422. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3423. item_ct1.get_local_id(2);
  3424. if (i >= k) {
  3425. return;
  3426. }
  3427. dst[i] = x[i] * sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3428. }
  3429. static void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope,
  3430. const sycl::nd_item<3> &item_ct1) {
  3431. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3432. item_ct1.get_local_id(2);
  3433. if (i >= k) {
  3434. return;
  3435. }
  3436. dst[i] = sycl::fmax((float)(x[i]), (float)0) +
  3437. sycl::fmin((float)(x[i]), 0.0f) * negative_slope;
  3438. }
  3439. static void sqr_f32(const float * x, float * dst, const int k,
  3440. const sycl::nd_item<3> &item_ct1) {
  3441. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3442. item_ct1.get_local_id(2);
  3443. if (i >= k) {
  3444. return;
  3445. }
  3446. dst[i] = x[i] * x[i];
  3447. }
  3448. static void norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3449. const sycl::nd_item<3> &item_ct1, sycl::float2 *s_sum, int block_size) {
  3450. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3451. item_ct1.get_local_id(1);
  3452. const int tid = item_ct1.get_local_id(2);
  3453. sycl::float2 mean_var = sycl::float2(0.f, 0.f);
  3454. for (int col = tid; col < ncols; col += block_size) {
  3455. const float xi = x[row*ncols + col];
  3456. mean_var.x() += xi;
  3457. mean_var.y() += xi * xi;
  3458. }
  3459. // sum up partial sums
  3460. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3461. if (block_size > WARP_SIZE) {
  3462. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3463. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3464. if (lane_id == 0) {
  3465. s_sum[warp_id] = mean_var;
  3466. }
  3467. /*
  3468. DPCT1118:0: SYCL group functions and algorithms must be encountered in
  3469. converged control flow. You may need to adjust the code.
  3470. */
  3471. item_ct1.barrier(sycl::access::fence_space::local_space);
  3472. mean_var = s_sum[lane_id];
  3473. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3474. }
  3475. const float mean = mean_var.x() / ncols;
  3476. const float var = mean_var.y() / ncols - mean * mean;
  3477. const float inv_std = sycl::rsqrt(var + eps);
  3478. for (int col = tid; col < ncols; col += block_size) {
  3479. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  3480. }
  3481. }
  3482. static void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02,
  3483. const sycl::nd_item<3> &item_ct1) {
  3484. int nidx = item_ct1.get_local_id(2) +
  3485. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3486. if (nidx >= ne0) {
  3487. return;
  3488. }
  3489. // operation
  3490. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3491. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3492. if (item_ct1.get_group(0) < ne02) { // src0
  3493. int offset_src =
  3494. nidx + item_ct1.get_group(1) * ne0 +
  3495. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3496. dst[offset_dst] = x[offset_src];
  3497. } else {
  3498. int offset_src =
  3499. nidx + item_ct1.get_group(1) * ne0 +
  3500. (item_ct1.get_group(0) - ne02) * ne0 * item_ct1.get_group_range(1);
  3501. dst[offset_dst] = y[offset_src];
  3502. }
  3503. }
  3504. static void upscale_f32(const float *x, float *dst, const int ne00, const int nb02, const int scale_factor,
  3505. const sycl::nd_item<3> &item_ct1) {
  3506. int ne0 = ne00 * scale_factor;
  3507. int nidx = item_ct1.get_local_id(2) +
  3508. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3509. if (nidx >= ne0) {
  3510. return;
  3511. }
  3512. // operation
  3513. int i00 = nidx / scale_factor;
  3514. int i01 = item_ct1.get_group(1) / scale_factor;
  3515. int offset_src = i00 + i01 * ne00 + item_ct1.get_group(0) * nb02;
  3516. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3517. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3518. dst[offset_dst] = x[offset_src];
  3519. }
  3520. static void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02,
  3521. const sycl::nd_item<3> &item_ct1) {
  3522. int nidx = item_ct1.get_local_id(2) +
  3523. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3524. if (nidx >= ne0) {
  3525. return;
  3526. }
  3527. // operation
  3528. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3529. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3530. if (nidx < ne00 && item_ct1.get_group(1) < ne01 &&
  3531. item_ct1.get_group(0) < ne02) {
  3532. int offset_src = nidx + item_ct1.get_group(1) * ne00 +
  3533. item_ct1.get_group(0) * ne00 * ne01;
  3534. dst[offset_dst] = x[offset_src];
  3535. } else {
  3536. dst[offset_dst] = 0.0f;
  3537. }
  3538. }
  3539. static void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps,
  3540. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3541. int start = item_ct1.get_group(2) * group_size;
  3542. int end = start + group_size;
  3543. start += item_ct1.get_local_id(2);
  3544. if (end >= ne_elements) {
  3545. end = ne_elements;
  3546. }
  3547. float tmp = 0.0f; // partial sum for thread in warp
  3548. for (int j = start; j < end; j += block_size) {
  3549. tmp += x[j];
  3550. }
  3551. tmp = warp_reduce_sum(tmp, item_ct1);
  3552. if (block_size > WARP_SIZE) {
  3553. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3554. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3555. if (lane_id == 0) {
  3556. s_sum[warp_id] = tmp;
  3557. }
  3558. /*
  3559. DPCT1118:1: SYCL group functions and algorithms must be encountered in
  3560. converged control flow. You may need to adjust the code.
  3561. */
  3562. /*
  3563. DPCT1065:54: Consider replacing sycl::nd_item::barrier() with
  3564. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3565. better performance if there is no access to global memory.
  3566. */
  3567. item_ct1.barrier();
  3568. tmp = s_sum[lane_id];
  3569. tmp = warp_reduce_sum(tmp, item_ct1);
  3570. }
  3571. float mean = tmp / group_size;
  3572. tmp = 0.0f;
  3573. for (int j = start; j < end; j += block_size) {
  3574. float xi = x[j] - mean;
  3575. dst[j] = xi;
  3576. tmp += xi * xi;
  3577. }
  3578. tmp = warp_reduce_sum(tmp, item_ct1);
  3579. if (block_size > WARP_SIZE) {
  3580. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3581. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3582. if (lane_id == 0) {
  3583. s_sum[warp_id] = tmp;
  3584. }
  3585. /*
  3586. DPCT1118:2: SYCL group functions and algorithms must be encountered in
  3587. converged control flow. You may need to adjust the code.
  3588. */
  3589. /*
  3590. DPCT1065:55: Consider replacing sycl::nd_item::barrier() with
  3591. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3592. better performance if there is no access to global memory.
  3593. */
  3594. item_ct1.barrier();
  3595. tmp = s_sum[lane_id];
  3596. tmp = warp_reduce_sum(tmp, item_ct1);
  3597. }
  3598. float variance = tmp / group_size;
  3599. float scale = sycl::rsqrt(variance + eps);
  3600. for (int j = start; j < end; j += block_size) {
  3601. dst[j] *= scale;
  3602. }
  3603. }
  3604. static void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3605. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3606. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3607. item_ct1.get_local_id(1);
  3608. const int tid = item_ct1.get_local_id(2);
  3609. float tmp = 0.0f; // partial sum for thread in warp
  3610. for (int col = tid; col < ncols; col += block_size) {
  3611. const float xi = x[row*ncols + col];
  3612. tmp += xi * xi;
  3613. }
  3614. // sum up partial sums
  3615. tmp = warp_reduce_sum(tmp, item_ct1);
  3616. if (block_size > WARP_SIZE) {
  3617. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3618. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3619. if (lane_id == 0) {
  3620. s_sum[warp_id] = tmp;
  3621. }
  3622. /*
  3623. DPCT1118:3: SYCL group functions and algorithms must be encountered in
  3624. converged control flow. You may need to adjust the code.
  3625. */
  3626. item_ct1.barrier(sycl::access::fence_space::local_space);
  3627. tmp = s_sum[lane_id];
  3628. tmp = warp_reduce_sum(tmp, item_ct1);
  3629. }
  3630. const float mean = tmp / ncols;
  3631. const float scale = sycl::rsqrt(mean + eps);
  3632. for (int col = tid; col < ncols; col += block_size) {
  3633. dst[row*ncols + col] = scale * x[row*ncols + col];
  3634. }
  3635. }
  3636. static __dpct_inline__ void dequantize_q4_0(const void *vx, const int ib,
  3637. const int iqs, dfloat2 &v) {
  3638. const block_q4_0 * x = (const block_q4_0 *) vx;
  3639. const dfloat d = x[ib].d;
  3640. const int vui = x[ib].qs[iqs];
  3641. v.x() = vui & 0xF;
  3642. v.y() = vui >> 4;
  3643. #ifdef GGML_SYCL_F16
  3644. // v = v - {8.0f, 8.0f};
  3645. // v = v * {d, d};
  3646. v.s0() = (v.s0() - 8.0f) * d;
  3647. v.s1() = (v.s1() - 8.0f) * d;
  3648. #else
  3649. v.x() = (v.x() - 8.0f) * d;
  3650. v.y() = (v.y() - 8.0f) * d;
  3651. #endif // GGML_SYCL_F16
  3652. }
  3653. static __dpct_inline__ void dequantize_q4_1(const void *vx, const int ib,
  3654. const int iqs, dfloat2 &v) {
  3655. const block_q4_1 * x = (const block_q4_1 *) vx;
  3656. const dfloat d = x[ib].dm[0];
  3657. const dfloat m = x[ib].dm[1];
  3658. const int vui = x[ib].qs[iqs];
  3659. v.x() = vui & 0xF;
  3660. v.y() = vui >> 4;
  3661. #ifdef GGML_SYCL_F16
  3662. // v = v * {d, d};
  3663. // v = v + {m, m};
  3664. v.s0() = (v.s0() * d) + m;
  3665. v.s1() = (v.s1() * d) + m;
  3666. #else
  3667. v.x() = (v.x() * d) + m;
  3668. v.y() = (v.y() * d) + m;
  3669. #endif // GGML_SYCL_F16
  3670. }
  3671. static __dpct_inline__ void dequantize_q5_0(const void *vx, const int ib,
  3672. const int iqs, dfloat2 &v) {
  3673. const block_q5_0 * x = (const block_q5_0 *) vx;
  3674. const dfloat d = x[ib].d;
  3675. uint32_t qh;
  3676. memcpy(&qh, x[ib].qh, sizeof(qh));
  3677. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3678. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3679. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3680. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3681. #ifdef GGML_SYCL_F16
  3682. // v = v - {16.0f, 16.0f};
  3683. // v = v * {d, d};
  3684. v.s0() = (v.s0() - 16.0f) * d;
  3685. v.s1() = (v.s1() - 16.0f) * d;
  3686. #else
  3687. v.x() = (v.x() - 16.0f) * d;
  3688. v.y() = (v.y() - 16.0f) * d;
  3689. #endif // GGML_SYCL_F16
  3690. }
  3691. static __dpct_inline__ void dequantize_q5_1(const void *vx, const int ib,
  3692. const int iqs, dfloat2 &v) {
  3693. const block_q5_1 * x = (const block_q5_1 *) vx;
  3694. const dfloat d = x[ib].dm[0];
  3695. const dfloat m = x[ib].dm[1];
  3696. uint32_t qh;
  3697. memcpy(&qh, x[ib].qh, sizeof(qh));
  3698. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3699. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3700. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3701. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3702. #ifdef GGML_SYCL_F16
  3703. // v = v * {d, d};
  3704. // v = v + {m, m};
  3705. v.s0() = (v.s0() * d) + m;
  3706. v.s1() = (v.s1() * d) + m;
  3707. #else
  3708. v.x() = (v.x() * d) + m;
  3709. v.y() = (v.y() * d) + m;
  3710. #endif // GGML_SYCL_F16
  3711. }
  3712. static __dpct_inline__ void dequantize_q8_0(const void *vx, const int ib,
  3713. const int iqs, dfloat2 &v) {
  3714. const block_q8_0 * x = (const block_q8_0 *) vx;
  3715. const dfloat d = x[ib].d;
  3716. v.x() = x[ib].qs[iqs + 0];
  3717. v.y() = x[ib].qs[iqs + 1];
  3718. #ifdef GGML_SYCL_F16
  3719. // v = v * {d, d};
  3720. v.s0() *= d;
  3721. v.s1() *= d;
  3722. #else
  3723. v.x() *= d;
  3724. v.y() *= d;
  3725. #endif // GGML_SYCL_F16
  3726. }
  3727. template<typename dst_t>
  3728. static void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32,
  3729. const sycl::nd_item<3> &item_ct1) {
  3730. const int i = item_ct1.get_group(2);
  3731. // assume 32 threads
  3732. const int tid = item_ct1.get_local_id(2);
  3733. const int il = tid/8;
  3734. const int ir = tid%8;
  3735. const int ib = 8*i + ir;
  3736. if (ib >= nb32) {
  3737. return;
  3738. }
  3739. dst_t * y = yy + 256*i + 32*ir + 4*il;
  3740. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  3741. const float d = sycl::vec<sycl::half, 1>(x->d)
  3742. .convert<float, sycl::rounding_mode::automatic>()[0];
  3743. const float dm = -8*d;
  3744. const uint8_t * q = x->qs + 4*il;
  3745. for (int l = 0; l < 4; ++l) {
  3746. y[l+ 0] = d * (q[l] & 0xF) + dm;
  3747. y[l+16] = d * (q[l] >> 4) + dm;
  3748. }
  3749. }
  3750. template<typename dst_t>
  3751. static void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32,
  3752. const sycl::nd_item<3> &item_ct1) {
  3753. const int i = item_ct1.get_group(2);
  3754. // assume 32 threads
  3755. const int tid = item_ct1.get_local_id(2);
  3756. const int il = tid/8;
  3757. const int ir = tid%8;
  3758. const int ib = 8*i + ir;
  3759. if (ib >= nb32) {
  3760. return;
  3761. }
  3762. dst_t * y = yy + 256*i + 32*ir + 4*il;
  3763. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  3764. const sycl::float2 d =
  3765. x->dm.convert<float, sycl::rounding_mode::automatic>();
  3766. const uint8_t * q = x->qs + 4*il;
  3767. for (int l = 0; l < 4; ++l) {
  3768. y[l + 0] = d.x() * (q[l] & 0xF) + d.y();
  3769. y[l + 16] = d.x() * (q[l] >> 4) + d.y();
  3770. }
  3771. }
  3772. //================================== k-quants
  3773. template<typename dst_t>
  3774. static void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3775. const sycl::nd_item<3> &item_ct1) {
  3776. const int i = item_ct1.get_group(2);
  3777. const block_q2_K * x = (const block_q2_K *) vx;
  3778. const int tid = item_ct1.get_local_id(2);
  3779. #if QK_K == 256
  3780. const int n = tid/32;
  3781. const int l = tid - 32*n;
  3782. const int is = 8*n + l/16;
  3783. const uint8_t q = x[i].qs[32*n + l];
  3784. dst_t * y = yy + i*QK_K + 128*n;
  3785. float dall = x[i].dm[0];
  3786. float dmin = x[i].dm[1];
  3787. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3788. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3789. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  3790. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  3791. #else
  3792. const int is = tid/16; // 0 or 1
  3793. const int il = tid%16; // 0...15
  3794. const uint8_t q = x[i].qs[il] >> (2*is);
  3795. dst_t * y = yy + i*QK_K + 16*is + il;
  3796. float dall = x[i].dm[0];
  3797. float dmin = x[i].dm[1];
  3798. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3799. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3800. #endif
  3801. }
  3802. template<typename dst_t>
  3803. static void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3804. const sycl::nd_item<3> &item_ct1) {
  3805. const int i = item_ct1.get_group(2);
  3806. const block_q3_K * x = (const block_q3_K *) vx;
  3807. #if QK_K == 256
  3808. const int r = item_ct1.get_local_id(2) / 4;
  3809. const int tid = r/2;
  3810. const int is0 = r%2;
  3811. const int l0 = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4);
  3812. const int n = tid / 4;
  3813. const int j = tid - 4*n;
  3814. uint8_t m = 1 << (4*n + j);
  3815. int is = 8*n + 2*j + is0;
  3816. int shift = 2*j;
  3817. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  3818. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  3819. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  3820. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  3821. float d_all = x[i].d;
  3822. float dl = d_all * (us - 32);
  3823. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  3824. const uint8_t * q = x[i].qs + 32*n;
  3825. const uint8_t * hm = x[i].hmask;
  3826. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  3827. #else
  3828. const int tid = item_ct1.get_local_id(2);
  3829. const int is = tid/16; // 0 or 1
  3830. const int il = tid%16; // 0...15
  3831. const int im = il/8; // 0...1
  3832. const int in = il%8; // 0...7
  3833. dst_t * y = yy + i*QK_K + 16*is + il;
  3834. const uint8_t q = x[i].qs[il] >> (2*is);
  3835. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  3836. const float d = (float)x[i].d;
  3837. if (is == 0) {
  3838. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  3839. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  3840. } else {
  3841. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  3842. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  3843. }
  3844. #endif
  3845. }
  3846. #if QK_K == 256
  3847. static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  3848. if (j < 4) {
  3849. d = q[j] & 63; m = q[j + 4] & 63;
  3850. } else {
  3851. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  3852. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  3853. }
  3854. }
  3855. #endif
  3856. template<typename dst_t>
  3857. static void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3858. const sycl::nd_item<3> &item_ct1) {
  3859. const block_q4_K * x = (const block_q4_K *) vx;
  3860. const int i = item_ct1.get_group(2);
  3861. #if QK_K == 256
  3862. // assume 32 threads
  3863. const int tid = item_ct1.get_local_id(2);
  3864. const int il = tid/8;
  3865. const int ir = tid%8;
  3866. const int is = 2*il;
  3867. const int n = 4;
  3868. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  3869. const float dall = x[i].dm[0];
  3870. const float dmin = x[i].dm[1];
  3871. const uint8_t * q = x[i].qs + 32*il + n*ir;
  3872. uint8_t sc, m;
  3873. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3874. const float d1 = dall * sc; const float m1 = dmin * m;
  3875. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3876. const float d2 = dall * sc; const float m2 = dmin * m;
  3877. for (int l = 0; l < n; ++l) {
  3878. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  3879. y[l +32] = d2 * (q[l] >> 4) - m2;
  3880. }
  3881. #else
  3882. const int tid = item_ct1.get_local_id(2);
  3883. const uint8_t * q = x[i].qs;
  3884. dst_t * y = yy + i*QK_K;
  3885. const float d = (float)x[i].dm[0];
  3886. const float m = (float)x[i].dm[1];
  3887. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  3888. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  3889. #endif
  3890. }
  3891. template<typename dst_t>
  3892. static void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3893. const sycl::nd_item<3> &item_ct1) {
  3894. const block_q5_K * x = (const block_q5_K *) vx;
  3895. const int i = item_ct1.get_group(2);
  3896. #if QK_K == 256
  3897. // assume 64 threads - this is very slightly better than the one below
  3898. const int tid = item_ct1.get_local_id(2);
  3899. const int il = tid/16; // il is in 0...3
  3900. const int ir = tid%16; // ir is in 0...15
  3901. const int is = 2*il; // is is in 0...6
  3902. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  3903. const float dall = x[i].dm[0];
  3904. const float dmin = x[i].dm[1];
  3905. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  3906. const uint8_t * qh = x[i].qh + 2*ir;
  3907. uint8_t sc, m;
  3908. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3909. const float d1 = dall * sc; const float m1 = dmin * m;
  3910. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3911. const float d2 = dall * sc; const float m2 = dmin * m;
  3912. uint8_t hm = 1 << (2*il);
  3913. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  3914. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  3915. hm <<= 1;
  3916. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  3917. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  3918. #else
  3919. const int tid = item_ct1.get_local_id(2);
  3920. const uint8_t q = x[i].qs[tid];
  3921. const int im = tid/8; // 0...3
  3922. const int in = tid%8; // 0...7
  3923. const int is = tid/16; // 0 or 1
  3924. const uint8_t h = x[i].qh[in] >> im;
  3925. const float d = x[i].d;
  3926. dst_t * y = yy + i*QK_K + tid;
  3927. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  3928. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  3929. #endif
  3930. }
  3931. template<typename dst_t>
  3932. static void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3933. const sycl::nd_item<3> &item_ct1) {
  3934. const block_q6_K * x = (const block_q6_K *) vx;
  3935. const int i = item_ct1.get_group(2);
  3936. #if QK_K == 256
  3937. // assume 64 threads - this is very slightly better than the one below
  3938. const int tid = item_ct1.get_local_id(2);
  3939. const int ip = tid/32; // ip is 0 or 1
  3940. const int il = tid - 32*ip; // 0...32
  3941. const int is = 8*ip + il/16;
  3942. dst_t * y = yy + i*QK_K + 128*ip + il;
  3943. const float d = x[i].d;
  3944. const uint8_t * ql = x[i].ql + 64*ip + il;
  3945. const uint8_t qh = x[i].qh[32*ip + il];
  3946. const int8_t * sc = x[i].scales + is;
  3947. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3948. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  3949. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3950. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  3951. #else
  3952. // assume 32 threads
  3953. const int tid = item_ct1.get_local_id(2);
  3954. const int ip = tid/16; // 0 or 1
  3955. const int il = tid - 16*ip; // 0...15
  3956. dst_t * y = yy + i*QK_K + 16*ip + il;
  3957. const float d = x[i].d;
  3958. const uint8_t ql = x[i].ql[16*ip + il];
  3959. const uint8_t qh = x[i].qh[il] >> (2*ip);
  3960. const int8_t * sc = x[i].scales;
  3961. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3962. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3963. #endif
  3964. }
  3965. template<typename dst_t>
  3966. static void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3967. const sycl::nd_item<3> &item_ct1,
  3968. const uint64_t *iq2xxs_grid_ptr,
  3969. const uint8_t *ksigns_iq2xs_ptr,
  3970. const uint8_t *kmask_iq2xs_ptr) {
  3971. const int i = item_ct1.get_group(2);
  3972. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  3973. const int tid = item_ct1.get_local_id(2);
  3974. #if QK_K == 256
  3975. const int il = tid/8; // 0...3
  3976. const int ib = tid%8; // 0...7
  3977. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  3978. const uint16_t * q2 = x[i].qs + 4*ib;
  3979. const uint8_t * aux8 = (const uint8_t *)q2;
  3980. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid_ptr + aux8[il]);
  3981. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3982. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  3983. const uint8_t signs = ksigns_iq2xs_ptr[(aux32 >> 7*il) & 127];
  3984. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs_ptr[j] ? -1.f : 1.f);
  3985. #else
  3986. assert(false);
  3987. #endif
  3988. }
  3989. template<typename dst_t>
  3990. static void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3991. const sycl::nd_item<3> &item_ct1,
  3992. const uint64_t *iq2xs_grid,
  3993. const uint8_t *ksigns_iq2xs,
  3994. const uint8_t *kmask_iq2xs) {
  3995. const int i = item_ct1.get_group(2);
  3996. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  3997. const int tid = item_ct1.get_local_id(2);
  3998. #if QK_K == 256
  3999. const int il = tid/8; // 0...3
  4000. const int ib = tid%8; // 0...7
  4001. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4002. const uint16_t * q2 = x[i].qs + 4*ib;
  4003. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  4004. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  4005. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  4006. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  4007. #else
  4008. assert(false);
  4009. #endif
  4010. }
  4011. template <typename dst_t>
  4012. __dpct_inline__ static void
  4013. dequantize_block_iq2_s(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4014. const sycl::nd_item<3> &item_ct1) {
  4015. const int i = item_ct1.get_group(2);
  4016. const block_iq2_s * x = (const block_iq2_s *) vx;
  4017. const int tid = item_ct1.get_local_id(2);
  4018. #if QK_K == 256
  4019. const int il = tid/8; // 0...3
  4020. const int ib = tid%8; // 0...7
  4021. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4022. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (x[i].qs[4*ib+il] | ((x[i].qh[ib] << (8-2*il)) & 0x300)));
  4023. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  4024. const uint8_t signs = x[i].qs[QK_K/8+4*ib+il];
  4025. #pragma unroll
  4026. for (int j = 0; j < 8; ++j)
  4027. y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  4028. #else
  4029. assert(false);
  4030. #endif
  4031. }
  4032. template<typename dst_t>
  4033. static void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4034. const sycl::nd_item<3> &item_ct1,
  4035. const uint32_t *iq3xxs_grid,
  4036. const uint8_t *ksigns_iq2xs,
  4037. const uint8_t *kmask_iq2xs) {
  4038. const int i = item_ct1.get_group(2);
  4039. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  4040. const int tid = item_ct1.get_local_id(2);
  4041. #if QK_K == 256
  4042. const int il = tid/8; // 0...3
  4043. const int ib = tid%8; // 0...7
  4044. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4045. const uint8_t * q3 = x[i].qs + 8*ib;
  4046. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  4047. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  4048. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  4049. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4050. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  4051. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  4052. for (int j = 0; j < 4; ++j) {
  4053. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4054. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4055. }
  4056. #else
  4057. assert(false);
  4058. #endif
  4059. }
  4060. template <typename dst_t>
  4061. __dpct_inline__ static void
  4062. dequantize_block_iq3_s(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4063. const sycl::nd_item<3> &item_ct1,
  4064. const uint8_t *kmask_iq2xs, const uint32_t *iq3s_grid) {
  4065. const int i = item_ct1.get_group(2);
  4066. const block_iq3_s * x = (const block_iq3_s *) vx;
  4067. const int tid = item_ct1.get_local_id(2);
  4068. #if QK_K == 256
  4069. const int il = tid/8; // 0...3
  4070. const int ib = tid%8; // 0...7
  4071. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4072. const uint8_t * qs = x[i].qs + 8*ib;
  4073. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*il+0] | ((x[i].qh[ib] << (8-2*il)) & 256)));
  4074. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*il+1] | ((x[i].qh[ib] << (7-2*il)) & 256)));
  4075. const float d = (float)x[i].d * (1 + 2*((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf));
  4076. const uint8_t signs = x[i].signs[4*ib + il];
  4077. #pragma unroll
  4078. for (int j = 0; j < 4; ++j) {
  4079. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4080. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4081. }
  4082. #else
  4083. assert(false);
  4084. #endif
  4085. }
  4086. template <typename dst_t>
  4087. __dpct_inline__ static void
  4088. dequantize_block_iq1_s(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4089. const sycl::nd_item<3> &item_ct1,
  4090. const uint32_t *iq1s_grid_gpu) {
  4091. const int i = item_ct1.get_group(2);
  4092. const block_iq1_s * x = (const block_iq1_s *) vx;
  4093. const int tid = item_ct1.get_local_id(2);
  4094. #if QK_K == 256
  4095. const int il = tid/8; // 0...3
  4096. const int ib = tid%8; // 0...7
  4097. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4098. const float delta = x[i].qh[ib] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA;
  4099. const float d = (float)x[i].d * (2*((x[i].qh[ib] >> 12) & 7) + 1);
  4100. uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
  4101. grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[ib] >> 3*il) & 7) << 8)];
  4102. grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
  4103. grid32[0] &= 0x0f0f0f0f;
  4104. #pragma unroll
  4105. for (int j = 0; j < 8; ++j) {
  4106. y[j] = d * (q[j] + delta);
  4107. }
  4108. #else
  4109. assert(false);
  4110. #endif
  4111. }
  4112. template <typename dst_t>
  4113. __dpct_inline__ static void
  4114. dequantize_block_iq1_m(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4115. const sycl::nd_item<3> &item_ct1,
  4116. const uint32_t *iq1s_grid_gpu) {
  4117. const int i = item_ct1.get_group(2);
  4118. const block_iq1_m * x = (const block_iq1_m *) vx;
  4119. const int tid = item_ct1.get_local_id(2);
  4120. #if QK_K == 256
  4121. const int il = tid/8; // 0...3
  4122. const int ib = tid%8; // 0...7
  4123. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4124. const uint16_t * sc = (const uint16_t *)x[i].scales;
  4125. iq1m_scale_t scale;
  4126. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  4127. const int ib16 = 2*ib + il/2; // sc[ib16/4] >> 3*(ib16%4) -> sc[ib/2] >> 3*((2*ib+il/2)%4);
  4128. const float d = (float)scale.f16 * (2*((sc[ib16/4] >> 3*(ib16%4)) & 0x7) + 1);
  4129. const float delta = x[i].qh[2*ib+il/2] & (0x08 << 4*(il%2)) ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA;
  4130. uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
  4131. grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[2*ib+il/2] >> 4*(il%2)) & 7) << 8)];
  4132. grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
  4133. grid32[0] &= 0x0f0f0f0f;
  4134. #pragma unroll
  4135. for (int j = 0; j < 8; ++j) {
  4136. y[j] = d * (q[j] + delta);
  4137. }
  4138. #else
  4139. assert(false);
  4140. #endif
  4141. }
  4142. template <typename dst_t>
  4143. __dpct_inline__ static void
  4144. dequantize_block_iq4_nl(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4145. const sycl::nd_item<3> &item_ct1) {
  4146. const int i = item_ct1.get_group(2);
  4147. const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
  4148. const int tid = item_ct1.get_local_id(2);
  4149. const int il = tid/8; // 0...3
  4150. const int ib = tid%8; // 0...7
  4151. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  4152. const uint8_t * q4 = x[ib].qs + 4*il;
  4153. const float d = (float)x[ib].d;
  4154. #pragma unroll
  4155. for (int j = 0; j < 4; ++j) {
  4156. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  4157. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  4158. }
  4159. }
  4160. template <typename dst_t>
  4161. __dpct_inline__ static void
  4162. dequantize_block_iq4_xs(const void *__restrict__ vx, dst_t *__restrict__ yy,
  4163. const sycl::nd_item<3> &item_ct1) {
  4164. const int i = item_ct1.get_group(2);
  4165. const block_iq4_xs * x = (const block_iq4_xs *)vx;
  4166. const int tid = item_ct1.get_local_id(2);
  4167. const int il = tid/8; // 0...3
  4168. const int ib = tid%8; // 0...7
  4169. dst_t * y = yy + i*QK_K + 32*ib + 4*il;
  4170. const uint8_t * q4 = x[i].qs + 16*ib + 4*il;
  4171. 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);
  4172. #pragma unroll
  4173. for (int j = 0; j < 4; ++j) {
  4174. y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
  4175. y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
  4176. }
  4177. }
  4178. /*
  4179. DPCT1110:4: The total declared local variable size in device function
  4180. dequantize_mul_mat_vec_q2_k exceeds 128 bytes and may cause high register
  4181. pressure. Consult with your hardware vendor to find the total register size
  4182. available and adjust the code, or use smaller sub-group size to avoid high
  4183. register pressure.
  4184. */
  4185. static void dequantize_mul_mat_vec_q2_k(const void *__restrict__ vx,
  4186. const float *__restrict__ yy,
  4187. float *__restrict__ dst,
  4188. const int ncols, int nrows,
  4189. const sycl::nd_item<3> &item_ct1) {
  4190. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4191. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4192. item_ct1.get_local_id(1);
  4193. if (row > nrows) return;
  4194. const int num_blocks_per_row = ncols / QK_K;
  4195. const int ib0 = row*num_blocks_per_row;
  4196. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  4197. float tmp = 0; // partial sum for thread in warp
  4198. #if QK_K == 256
  4199. const int tid =
  4200. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  4201. const int ix =
  4202. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4203. const int step = 16/K_QUANTS_PER_ITERATION;
  4204. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4205. const int in = tid - step*im; // 0...15 or 0...7
  4206. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  4207. const int q_offset = 32*im + l0;
  4208. const int s_offset = 8*im;
  4209. const int y_offset = 128*im + l0;
  4210. uint32_t aux[4];
  4211. const uint8_t * d = (const uint8_t *)aux;
  4212. const uint8_t * m = (const uint8_t *)(aux + 2);
  4213. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4214. const float * y = yy + i * QK_K + y_offset;
  4215. const uint8_t * q = x[i].qs + q_offset;
  4216. const float dall = x[i].dm[0];
  4217. const float dmin = x[i].dm[1];
  4218. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  4219. aux[0] = a[0] & 0x0f0f0f0f;
  4220. aux[1] = a[1] & 0x0f0f0f0f;
  4221. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  4222. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  4223. float sum1 = 0, sum2 = 0;
  4224. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4225. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  4226. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  4227. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  4228. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  4229. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  4230. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  4231. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  4232. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  4233. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  4234. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  4235. }
  4236. tmp += dall * sum1 - dmin * sum2;
  4237. }
  4238. #else
  4239. const int tid = item_ct1.get_local_id(2) /
  4240. (2 * K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  4241. const int ix = item_ct1.get_local_id(2) %
  4242. (2 * K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  4243. const int offset = tid * K_QUANTS_PER_ITERATION;
  4244. uint32_t uaux[2];
  4245. const uint8_t * d = (const uint8_t *)uaux;
  4246. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4247. const float * y = yy + i * QK_K + offset;
  4248. const uint8_t * q = x[i].qs + offset;
  4249. const uint32_t * s = (const uint32_t *)x[i].scales;
  4250. uaux[0] = s[0] & 0x0f0f0f0f;
  4251. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  4252. const sycl::float2 dall =
  4253. x[i].dm.convert<float, sycl::rounding_mode::automatic>();
  4254. float sum1 = 0, sum2 = 0;
  4255. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4256. const uint8_t ql = q[l];
  4257. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  4258. + y[l+16] * d[1] * ((ql >> 2) & 3)
  4259. + y[l+32] * d[2] * ((ql >> 4) & 3)
  4260. + y[l+48] * d[3] * ((ql >> 6) & 3);
  4261. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  4262. }
  4263. tmp += dall.x() * sum1 - dall.y() * sum2;
  4264. }
  4265. #endif
  4266. // sum up partial sums and write back result
  4267. #pragma unroll
  4268. for (int mask = 16; mask > 0; mask >>= 1) {
  4269. tmp +=
  4270. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4271. }
  4272. if (item_ct1.get_local_id(2) == 0) {
  4273. dst[row] = tmp;
  4274. }
  4275. }
  4276. /*
  4277. DPCT1110:5: The total declared local variable size in device function
  4278. dequantize_mul_mat_vec_q3_k exceeds 128 bytes and may cause high register
  4279. pressure. Consult with your hardware vendor to find the total register size
  4280. available and adjust the code, or use smaller sub-group size to avoid high
  4281. register pressure.
  4282. */
  4283. static void dequantize_mul_mat_vec_q3_k(const void *__restrict__ vx,
  4284. const float *__restrict__ yy,
  4285. float *__restrict__ dst,
  4286. const int ncols, int nrows,
  4287. const sycl::nd_item<3> &item_ct1) {
  4288. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4289. item_ct1.get_local_id(1);
  4290. if (row > nrows) return;
  4291. const int num_blocks_per_row = ncols / QK_K;
  4292. const int ib0 = row*num_blocks_per_row;
  4293. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  4294. float tmp = 0; // partial sum for thread in warp
  4295. #if QK_K == 256
  4296. const uint16_t kmask1 = 0x0303;
  4297. const uint16_t kmask2 = 0x0f0f;
  4298. const int tid =
  4299. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4300. const int ix =
  4301. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4302. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  4303. const int step = 16/K_QUANTS_PER_ITERATION;
  4304. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4305. const int in = tid - step*im; // 0....15 or 0...7
  4306. const uint8_t m = 1 << (4*im);
  4307. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  4308. const int q_offset = 32*im + l0;
  4309. const int y_offset = 128*im + l0;
  4310. uint16_t utmp[4];
  4311. const int8_t * s = (const int8_t *)utmp;
  4312. const uint16_t s_shift = 4*im;
  4313. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4314. const float * y = yy + i * QK_K + y_offset;
  4315. const uint8_t * q = x[i].qs + q_offset;
  4316. const uint8_t * h = x[i].hmask + l0;
  4317. const uint16_t * a = (const uint16_t *)x[i].scales;
  4318. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  4319. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  4320. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  4321. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  4322. const float d = x[i].d;
  4323. float sum = 0;
  4324. for (int l = 0; l < n; ++l) {
  4325. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  4326. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  4327. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  4328. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  4329. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  4330. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  4331. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  4332. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  4333. }
  4334. tmp += d * sum;
  4335. }
  4336. #else
  4337. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  4338. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  4339. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  4340. const int in = offset/8; // 0 or 1
  4341. const int im = offset%8; // 0...7
  4342. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4343. const float * y = yy + i * QK_K + offset;
  4344. const uint8_t * q = x[i].qs + offset;
  4345. const uint8_t * s = x[i].scales;
  4346. const float dall = (float)x[i].d;
  4347. float sum = 0;
  4348. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4349. const uint8_t hl = x[i].hmask[im+l] >> in;
  4350. const uint8_t ql = q[l];
  4351. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  4352. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  4353. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  4354. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  4355. }
  4356. tmp += sum;
  4357. }
  4358. #endif
  4359. // sum up partial sums and write back result
  4360. #pragma unroll
  4361. for (int mask = 16; mask > 0; mask >>= 1) {
  4362. tmp +=
  4363. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4364. }
  4365. if (item_ct1.get_local_id(2) == 0) {
  4366. dst[row] = tmp;
  4367. }
  4368. }
  4369. /*
  4370. DPCT1110:6: The total declared local variable size in device function
  4371. dequantize_mul_mat_vec_q4_k exceeds 128 bytes and may cause high register
  4372. pressure. Consult with your hardware vendor to find the total register size
  4373. available and adjust the code, or use smaller sub-group size to avoid high
  4374. register pressure.
  4375. */
  4376. static void dequantize_mul_mat_vec_q4_k(const void *__restrict__ vx,
  4377. const float *__restrict__ yy,
  4378. float *__restrict__ dst,
  4379. const int ncols, int nrows,
  4380. const sycl::nd_item<3> &item_ct1) {
  4381. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4382. item_ct1.get_local_id(1);
  4383. if (row > nrows) return;
  4384. const int num_blocks_per_row = ncols / QK_K;
  4385. const int ib0 = row*num_blocks_per_row;
  4386. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  4387. #if QK_K == 256
  4388. const uint16_t kmask1 = 0x3f3f;
  4389. const uint16_t kmask2 = 0x0f0f;
  4390. const uint16_t kmask3 = 0xc0c0;
  4391. const int tid =
  4392. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4393. const int ix =
  4394. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4395. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  4396. const int il = tid/step; // 0...3
  4397. const int ir = tid - step*il; // 0...7 or 0...3
  4398. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  4399. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4400. const int in = il%2;
  4401. const int l0 = n*(2*ir + in);
  4402. const int q_offset = 32*im + l0;
  4403. const int y_offset = 64*im + l0;
  4404. uint16_t aux[4];
  4405. const uint8_t * sc = (const uint8_t *)aux;
  4406. #if K_QUANTS_PER_ITERATION == 2
  4407. uint32_t q32[4];
  4408. const uint8_t * q4 = (const uint8_t *)q32;
  4409. #else
  4410. uint16_t q16[4];
  4411. const uint8_t * q4 = (const uint8_t *)q16;
  4412. #endif
  4413. float tmp = 0; // partial sum for thread in warp
  4414. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4415. const float * y1 = yy + i*QK_K + y_offset;
  4416. const float * y2 = y1 + 128;
  4417. const float dall = x[i].dm[0];
  4418. const float dmin = x[i].dm[1];
  4419. const uint16_t * a = (const uint16_t *)x[i].scales;
  4420. aux[0] = a[im+0] & kmask1;
  4421. aux[1] = a[im+2] & kmask1;
  4422. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4423. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4424. #if K_QUANTS_PER_ITERATION == 2
  4425. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  4426. const uint32_t * q2 = q1 + 16;
  4427. q32[0] = q1[0] & 0x0f0f0f0f;
  4428. q32[1] = q1[0] & 0xf0f0f0f0;
  4429. q32[2] = q2[0] & 0x0f0f0f0f;
  4430. q32[3] = q2[0] & 0xf0f0f0f0;
  4431. sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
  4432. float smin = 0;
  4433. for (int l = 0; l < 4; ++l) {
  4434. s.x() += y1[l] * q4[l + 0]; s.y() += y1[l + 32] * q4[l + 4];
  4435. s.z() += y2[l] * q4[l + 8]; s.w() += y2[l + 32] * q4[l + 12];
  4436. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4437. }
  4438. tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f / 16.f +
  4439. s.z() * sc[4] + s.w() * sc[5] * 1.f / 16.f) -
  4440. dmin * smin;
  4441. #else
  4442. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  4443. const uint16_t * q2 = q1 + 32;
  4444. q16[0] = q1[0] & 0x0f0f;
  4445. q16[1] = q1[0] & 0xf0f0;
  4446. q16[2] = q2[0] & 0x0f0f;
  4447. q16[3] = q2[0] & 0xf0f0;
  4448. float4 s = {0.f, 0.f, 0.f, 0.f};
  4449. float smin = 0;
  4450. for (int l = 0; l < 2; ++l) {
  4451. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  4452. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  4453. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4454. }
  4455. 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;
  4456. #endif
  4457. }
  4458. #else
  4459. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15
  4460. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION);
  4461. const int step = tid * K_QUANTS_PER_ITERATION;
  4462. uint16_t aux16[2];
  4463. const uint8_t * s = (const uint8_t *)aux16;
  4464. float tmp = 0;
  4465. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4466. const uint8_t * q = x[i].qs + step;
  4467. const float * y = yy + i*QK_K + step;
  4468. const uint16_t * a = (const uint16_t *)x[i].scales;
  4469. aux16[0] = a[0] & 0x0f0f;
  4470. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4471. const float d = (float)x[i].dm[0];
  4472. const float m = (float)x[i].dm[1];
  4473. float sum = 0.f;
  4474. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4475. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  4476. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  4477. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  4478. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  4479. }
  4480. tmp += sum;
  4481. }
  4482. #endif
  4483. // sum up partial sums and write back result
  4484. #pragma unroll
  4485. for (int mask = 16; mask > 0; mask >>= 1) {
  4486. tmp +=
  4487. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4488. }
  4489. if (tid == 0) {
  4490. dst[row] = tmp;
  4491. }
  4492. }
  4493. /*
  4494. DPCT1110:7: The total declared local variable size in device function
  4495. dequantize_mul_mat_vec_q5_k exceeds 128 bytes and may cause high register
  4496. pressure. Consult with your hardware vendor to find the total register size
  4497. available and adjust the code, or use smaller sub-group size to avoid high
  4498. register pressure.
  4499. */
  4500. static void dequantize_mul_mat_vec_q5_k(const void *__restrict__ vx,
  4501. const float *__restrict__ yy,
  4502. float *__restrict__ dst,
  4503. const int ncols,
  4504. const sycl::nd_item<3> &item_ct1) {
  4505. const int row = item_ct1.get_group(2);
  4506. const int num_blocks_per_row = ncols / QK_K;
  4507. const int ib0 = row*num_blocks_per_row;
  4508. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  4509. float tmp = 0; // partial sum for thread in warp
  4510. #if QK_K == 256
  4511. const uint16_t kmask1 = 0x3f3f;
  4512. const uint16_t kmask2 = 0x0f0f;
  4513. const uint16_t kmask3 = 0xc0c0;
  4514. const int tid = item_ct1.get_local_id(2) / 2; // 0...15
  4515. const int ix = item_ct1.get_local_id(2) % 2;
  4516. const int il = tid/4; // 0...3
  4517. const int ir = tid - 4*il;// 0...3
  4518. const int n = 2;
  4519. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4520. const int in = il%2;
  4521. const int l0 = n*(2*ir + in);
  4522. const int q_offset = 32*im + l0;
  4523. const int y_offset = 64*im + l0;
  4524. const uint8_t hm1 = 1 << (2*im);
  4525. const uint8_t hm2 = hm1 << 4;
  4526. uint16_t aux[4];
  4527. const uint8_t * sc = (const uint8_t *)aux;
  4528. uint16_t q16[8];
  4529. const uint8_t * q4 = (const uint8_t *)q16;
  4530. for (int i = ix; i < num_blocks_per_row; i += 2) {
  4531. const uint8_t * ql1 = x[i].qs + q_offset;
  4532. const uint8_t * qh = x[i].qh + l0;
  4533. const float * y1 = yy + i*QK_K + y_offset;
  4534. const float * y2 = y1 + 128;
  4535. const float dall = x[i].dm[0];
  4536. const float dmin = x[i].dm[1];
  4537. const uint16_t * a = (const uint16_t *)x[i].scales;
  4538. aux[0] = a[im+0] & kmask1;
  4539. aux[1] = a[im+2] & kmask1;
  4540. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4541. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4542. sycl::float4 sum = {0.f, 0.f, 0.f, 0.f};
  4543. float smin = 0;
  4544. const uint16_t * q1 = (const uint16_t *)ql1;
  4545. const uint16_t * q2 = q1 + 32;
  4546. q16[0] = q1[0] & 0x0f0f;
  4547. q16[1] = q1[8] & 0x0f0f;
  4548. q16[2] = (q1[0] >> 4) & 0x0f0f;
  4549. q16[3] = (q1[8] >> 4) & 0x0f0f;
  4550. q16[4] = q2[0] & 0x0f0f;
  4551. q16[5] = q2[8] & 0x0f0f;
  4552. q16[6] = (q2[0] >> 4) & 0x0f0f;
  4553. q16[7] = (q2[8] >> 4) & 0x0f0f;
  4554. for (int l = 0; l < n; ++l) {
  4555. sum.x() +=
  4556. y1[l + 0] * (q4[l + 0] + (qh[l + 0] & (hm1 << 0) ? 16 : 0)) +
  4557. y1[l + 16] * (q4[l + 2] + (qh[l + 16] & (hm1 << 0) ? 16 : 0));
  4558. sum.y() +=
  4559. y1[l + 32] * (q4[l + 4] + (qh[l + 0] & (hm1 << 1) ? 16 : 0)) +
  4560. y1[l + 48] * (q4[l + 6] + (qh[l + 16] & (hm1 << 1) ? 16 : 0));
  4561. sum.z() +=
  4562. y2[l + 0] * (q4[l + 8] + (qh[l + 0] & (hm2 << 0) ? 16 : 0)) +
  4563. y2[l + 16] * (q4[l + 10] + (qh[l + 16] & (hm2 << 0) ? 16 : 0));
  4564. sum.w() +=
  4565. y2[l + 32] * (q4[l + 12] + (qh[l + 0] & (hm2 << 1) ? 16 : 0)) +
  4566. y2[l + 48] * (q4[l + 14] + (qh[l + 16] & (hm2 << 1) ? 16 : 0));
  4567. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  4568. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  4569. }
  4570. tmp += dall * (sum.x() * sc[0] + sum.y() * sc[1] + sum.z() * sc[4] +
  4571. sum.w() * sc[5]) -
  4572. dmin * smin;
  4573. }
  4574. #else
  4575. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15
  4576. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION);
  4577. const int step = tid * K_QUANTS_PER_ITERATION;
  4578. const int im = step/8;
  4579. const int in = step%8;
  4580. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4581. const uint8_t * q = x[i].qs + step;
  4582. const int8_t * s = x[i].scales;
  4583. const float * y = yy + i*QK_K + step;
  4584. const float d = x[i].d;
  4585. float sum = 0.f;
  4586. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4587. const uint8_t h = x[i].qh[in+j] >> im;
  4588. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  4589. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  4590. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  4591. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  4592. }
  4593. tmp += sum;
  4594. }
  4595. #endif
  4596. // sum up partial sums and write back result
  4597. #pragma unroll
  4598. for (int mask = 16; mask > 0; mask >>= 1) {
  4599. tmp +=
  4600. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4601. }
  4602. if (item_ct1.get_local_id(2) == 0) {
  4603. dst[row] = tmp;
  4604. }
  4605. }
  4606. static void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows,
  4607. const sycl::nd_item<3> &item_ct1) {
  4608. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4609. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4610. item_ct1.get_local_id(1);
  4611. if (row > nrows) return;
  4612. const int num_blocks_per_row = ncols / QK_K;
  4613. const int ib0 = row*num_blocks_per_row;
  4614. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  4615. #if QK_K == 256
  4616. const int tid =
  4617. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4618. const int ix =
  4619. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0, 1
  4620. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  4621. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4622. const int in = tid - step*im; // 0...15 or 0...7
  4623. #if K_QUANTS_PER_ITERATION == 1
  4624. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  4625. const int is = 0;
  4626. #else
  4627. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  4628. const int is = in / 4;
  4629. #endif
  4630. const int ql_offset = 64*im + l0;
  4631. const int qh_offset = 32*im + l0;
  4632. const int s_offset = 8*im + is;
  4633. const int y_offset = 128*im + l0;
  4634. float tmp = 0; // partial sum for thread in warp
  4635. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4636. const float * y = yy + i * QK_K + y_offset;
  4637. const uint8_t * ql = x[i].ql + ql_offset;
  4638. const uint8_t * qh = x[i].qh + qh_offset;
  4639. const int8_t * s = x[i].scales + s_offset;
  4640. const float d = x[i].d;
  4641. #if K_QUANTS_PER_ITERATION == 1
  4642. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  4643. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  4644. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  4645. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  4646. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  4647. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  4648. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  4649. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  4650. tmp += sum;
  4651. #else
  4652. float sum = 0;
  4653. for (int l = 0; l < 4; ++l) {
  4654. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  4655. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  4656. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  4657. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  4658. }
  4659. tmp += sum;
  4660. #endif
  4661. }
  4662. #else
  4663. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...7
  4664. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION); // 0...3
  4665. const int step = tid * K_QUANTS_PER_ITERATION;
  4666. float tmp = 0; // partial sum for thread in warp
  4667. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4668. const float * y = yy + i * QK_K + step;
  4669. const uint8_t * ql = x[i].ql + step;
  4670. const uint8_t * qh = x[i].qh + step;
  4671. const int8_t * s = x[i].scales;
  4672. const float d = x[i+0].d;
  4673. float sum = 0;
  4674. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4675. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  4676. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  4677. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  4678. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  4679. }
  4680. tmp += sum;
  4681. }
  4682. #endif
  4683. // sum up partial sums and write back result
  4684. #pragma unroll
  4685. for (int mask = 16; mask > 0; mask >>= 1) {
  4686. tmp +=
  4687. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4688. }
  4689. if (tid == 0) {
  4690. dst[row] = tmp;
  4691. }
  4692. }
  4693. static void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4694. const sycl::half *x = (const sycl::half *)vx;
  4695. // automatic half -> float type cast if dfloat == float
  4696. v.x() = x[ib + iqs + 0];
  4697. v.y() = x[ib + iqs + 1];
  4698. }
  4699. static void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4700. const float * x = (const float *) vx;
  4701. // automatic half -> float type cast if dfloat == float
  4702. v.x() = x[ib + iqs + 0];
  4703. v.y() = x[ib + iqs + 1];
  4704. }
  4705. static void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded,
  4706. const sycl::nd_item<3> &item_ct1) {
  4707. const int ix = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4708. item_ct1.get_local_id(2);
  4709. if (ix >= kx_padded) {
  4710. return;
  4711. }
  4712. const int iy = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4713. item_ct1.get_local_id(1);
  4714. const int i_padded = iy*kx_padded + ix;
  4715. block_q8_1 * y = (block_q8_1 *) vy;
  4716. const int ib = i_padded / QK8_1; // block index
  4717. const int iqs = i_padded % QK8_1; // quant index
  4718. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  4719. float amax = sycl::fabs((float)xi);
  4720. float sum = xi;
  4721. #pragma unroll
  4722. for (int mask = 16; mask > 0; mask >>= 1) {
  4723. amax = sycl::fmax(amax, dpct::permute_sub_group_by_xor(
  4724. item_ct1.get_sub_group(), amax, mask));
  4725. sum +=
  4726. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), sum, mask);
  4727. }
  4728. const float d = amax / 127;
  4729. const int8_t q = amax == 0.0f ? 0 : sycl::round(xi / d);
  4730. y[ib].qs[iqs] = q;
  4731. if (iqs > 0) {
  4732. return;
  4733. }
  4734. reinterpret_cast<sycl::half &>(y[ib].ds.x()) = d;
  4735. reinterpret_cast<sycl::half &>(y[ib].ds.y()) = sum;
  4736. }
  4737. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4738. static void k_get_rows(
  4739. const void * src0, const int32_t * src1, dst_t * dst,
  4740. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4741. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4742. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4743. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4744. size_t s10, size_t s11, size_t s12,
  4745. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4746. const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4747. item_ct1.get_local_id(2)) *
  4748. 2;
  4749. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4750. item_ct1.get_local_id(1);
  4751. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4752. item_ct1.get_local_id(0)) /
  4753. ne12;
  4754. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4755. item_ct1.get_local_id(0)) %
  4756. ne12;
  4757. if (i00 >= ne00) {
  4758. return;
  4759. }
  4760. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4761. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4762. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  4763. const int ib = i00/qk; // block index
  4764. const int iqs = (i00%qk)/qr; // quant index
  4765. const int iybs = i00 - i00%qk; // dst block start index
  4766. const int y_offset = qr == 1 ? 1 : qk/2;
  4767. // dequantize
  4768. dfloat2 v;
  4769. dequantize_kernel(src0_row, ib, iqs, v);
  4770. dst_row[iybs + iqs + 0] = v.x();
  4771. dst_row[iybs + iqs + y_offset] = v.y();
  4772. }
  4773. template<typename src0_t, typename dst_t>
  4774. static void k_get_rows_float(
  4775. const src0_t * src0, const int32_t * src1, dst_t * dst,
  4776. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4777. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4778. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4779. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4780. size_t s10, size_t s11, size_t s12,
  4781. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4782. const int i00 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4783. item_ct1.get_local_id(2);
  4784. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4785. item_ct1.get_local_id(1);
  4786. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4787. item_ct1.get_local_id(0)) /
  4788. ne12;
  4789. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4790. item_ct1.get_local_id(0)) %
  4791. ne12;
  4792. if (i00 >= ne00) {
  4793. return;
  4794. }
  4795. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4796. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4797. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  4798. dst_row[i00] = src0_row[i00];
  4799. }
  4800. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4801. static void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4802. const sycl::nd_item<3> &item_ct1) {
  4803. const int i = 2 * (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4804. item_ct1.get_local_id(2));
  4805. if (i >= k) {
  4806. return;
  4807. }
  4808. const int ib = i/qk; // block index
  4809. const int iqs = (i%qk)/qr; // quant index
  4810. const int iybs = i - i%qk; // y block start index
  4811. const int y_offset = qr == 1 ? 1 : qk/2;
  4812. // dequantize
  4813. dfloat2 v;
  4814. dequantize_kernel(vx, ib, iqs, v);
  4815. y[iybs + iqs + 0] = v.x();
  4816. y[iybs + iqs + y_offset] = v.y();
  4817. }
  4818. template <typename src_t, typename dst_t>
  4819. static void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4820. const sycl::nd_item<3> &item_ct1) {
  4821. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4822. item_ct1.get_local_id(2);
  4823. if (i >= k) {
  4824. return;
  4825. }
  4826. const src_t * x = (src_t *) vx;
  4827. y[i] = x[i];
  4828. }
  4829. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  4830. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  4831. #define VDR_Q4_0_Q8_1_MMVQ 2
  4832. #define VDR_Q4_0_Q8_1_MMQ 4
  4833. template <int vdr>
  4834. static __dpct_inline__ float vec_dot_q4_0_q8_1_impl(const int *v, const int *u,
  4835. const float &d4,
  4836. const sycl::half2 &ds8) {
  4837. int sumi = 0;
  4838. #pragma unroll
  4839. for (int i = 0; i < vdr; ++i) {
  4840. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4841. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4842. // SIMD dot product of quantized values
  4843. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4844. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4845. }
  4846. const sycl::float2 ds8f =
  4847. ds8.convert<float, sycl::rounding_mode::automatic>();
  4848. // second part effectively subtracts 8 from each quant value
  4849. return d4 * (sumi * ds8f.x() - (8 * vdr / QI4_0) * ds8f.y());
  4850. }
  4851. #define VDR_Q4_1_Q8_1_MMVQ 2
  4852. #define VDR_Q4_1_Q8_1_MMQ 4
  4853. template <int vdr>
  4854. static __dpct_inline__ float vec_dot_q4_1_q8_1_impl(const int *v, const int *u,
  4855. const sycl::half2 &dm4,
  4856. const sycl::half2 &ds8) {
  4857. int sumi = 0;
  4858. #pragma unroll
  4859. for (int i = 0; i < vdr; ++i) {
  4860. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4861. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4862. // SIMD dot product of quantized values
  4863. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4864. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4865. }
  4866. #ifdef GGML_SYCL_F16
  4867. const sycl::float2 tmp =
  4868. (dm4 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4869. const float d4d8 = tmp.x();
  4870. const float m4s8 = tmp.y();
  4871. #else
  4872. const sycl::float2 dm4f =
  4873. dm4.convert<float, sycl::rounding_mode::automatic>();
  4874. const sycl::float2 ds8f =
  4875. ds8.convert<float, sycl::rounding_mode::automatic>();
  4876. const float d4d8 = dm4f.x() * ds8f.x();
  4877. const float m4s8 = dm4f.y() * ds8f.y();
  4878. #endif // GGML_SYCL_F16
  4879. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  4880. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  4881. }
  4882. #define VDR_Q5_0_Q8_1_MMVQ 2
  4883. #define VDR_Q5_0_Q8_1_MMQ 4
  4884. template <int vdr>
  4885. static __dpct_inline__ float
  4886. vec_dot_q5_0_q8_1_impl(const int *vl, const int *vh, const int *u,
  4887. const float &d5, const sycl::half2 &ds8) {
  4888. int sumi = 0;
  4889. #pragma unroll
  4890. for (int i = 0; i < vdr; ++i) {
  4891. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4892. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4893. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4894. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4895. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4896. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4897. sumi); // SIMD dot product of quantized values
  4898. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4899. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4900. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4901. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4902. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4903. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4904. sumi); // SIMD dot product of quantized values
  4905. }
  4906. const sycl::float2 ds8f =
  4907. ds8.convert<float, sycl::rounding_mode::automatic>();
  4908. // second part effectively subtracts 16 from each quant value
  4909. return d5 * (sumi * ds8f.x() - (16 * vdr / QI5_0) * ds8f.y());
  4910. }
  4911. #define VDR_Q5_1_Q8_1_MMVQ 2
  4912. #define VDR_Q5_1_Q8_1_MMQ 4
  4913. template <int vdr>
  4914. static __dpct_inline__ float
  4915. vec_dot_q5_1_q8_1_impl(const int *vl, const int *vh, const int *u,
  4916. const sycl::half2 &dm5, const sycl::half2 &ds8) {
  4917. int sumi = 0;
  4918. #pragma unroll
  4919. for (int i = 0; i < vdr; ++i) {
  4920. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4921. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4922. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4923. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4924. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4925. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4926. sumi); // SIMD dot product of quantized values
  4927. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4928. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4929. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4930. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4931. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4932. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4933. sumi); // SIMD dot product of quantized values
  4934. }
  4935. #ifdef GGML_SYCL_F16
  4936. const sycl::float2 tmp =
  4937. (dm5 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4938. const float d5d8 = tmp.x();
  4939. const float m5s8 = tmp.y();
  4940. #else
  4941. const sycl::float2 dm5f =
  4942. dm5.convert<float, sycl::rounding_mode::automatic>();
  4943. const sycl::float2 ds8f =
  4944. ds8.convert<float, sycl::rounding_mode::automatic>();
  4945. const float d5d8 = dm5f.x() * ds8f.x();
  4946. const float m5s8 = dm5f.y() * ds8f.y();
  4947. #endif // GGML_SYCL_F16
  4948. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  4949. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  4950. }
  4951. #define VDR_Q8_0_Q8_1_MMVQ 2
  4952. #define VDR_Q8_0_Q8_1_MMQ 8
  4953. template <int vdr>
  4954. static __dpct_inline__ float vec_dot_q8_0_q8_1_impl(const int *v, const int *u,
  4955. const float &d8_0,
  4956. const float &d8_1) {
  4957. int sumi = 0;
  4958. #pragma unroll
  4959. for (int i = 0; i < vdr; ++i) {
  4960. // SIMD dot product of quantized values
  4961. sumi = dpct::dp4a(v[i], u[i], sumi);
  4962. }
  4963. return d8_0*d8_1 * sumi;
  4964. }
  4965. template <int vdr>
  4966. static __dpct_inline__ float vec_dot_q8_1_q8_1_impl(const int *v, const int *u,
  4967. const sycl::half2 &dm8,
  4968. const sycl::half2 &ds8) {
  4969. int sumi = 0;
  4970. #pragma unroll
  4971. for (int i = 0; i < vdr; ++i) {
  4972. // SIMD dot product of quantized values
  4973. sumi = dpct::dp4a(v[i], u[i], sumi);
  4974. }
  4975. #ifdef GGML_SYCL_F16
  4976. const sycl::float2 tmp =
  4977. (dm8 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4978. const float d8d8 = tmp.x();
  4979. const float m8s8 = tmp.y();
  4980. #else
  4981. const sycl::float2 dm8f =
  4982. dm8.convert<float, sycl::rounding_mode::automatic>();
  4983. const sycl::float2 ds8f =
  4984. ds8.convert<float, sycl::rounding_mode::automatic>();
  4985. const float d8d8 = dm8f.x() * ds8f.x();
  4986. const float m8s8 = dm8f.y() * ds8f.y();
  4987. #endif // GGML_SYCL_F16
  4988. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  4989. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  4990. }
  4991. #define VDR_Q2_K_Q8_1_MMVQ 1
  4992. #define VDR_Q2_K_Q8_1_MMQ 2
  4993. // contiguous v/x values
  4994. static __dpct_inline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  4995. const int &v, const int *__restrict__ u, const uint8_t *__restrict__ scales,
  4996. const sycl::half2 &dm2, const float *__restrict__ d8) {
  4997. float sumf_d = 0.0f;
  4998. float sumf_m = 0.0f;
  4999. #pragma unroll
  5000. for (int i = 0; i < QR2_K; ++i) {
  5001. const int sc = scales[2*i];
  5002. const int vi = (v >> (2*i)) & 0x03030303;
  5003. sumf_d +=
  5004. d8[i] * (dpct::dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  5005. // fill int with 4x m
  5006. int m = sc >> 4;
  5007. m |= m << 8;
  5008. m |= m << 16;
  5009. sumf_m += d8[i] *
  5010. dpct::dp4a(
  5011. m, u[i],
  5012. 0); // multiply constant q2_K part with sum of q8_1 values
  5013. }
  5014. const sycl::float2 dm2f =
  5015. dm2.convert<float, sycl::rounding_mode::automatic>();
  5016. return dm2f.x() * sumf_d - dm2f.y() * sumf_m;
  5017. }
  5018. // contiguous u/y values
  5019. static __dpct_inline__ float
  5020. vec_dot_q2_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  5021. const uint8_t *__restrict__ scales,
  5022. const sycl::half2 &dm2, const float &d8) {
  5023. int sumi_d = 0;
  5024. int sumi_m = 0;
  5025. #pragma unroll
  5026. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  5027. int sumi_d_sc = 0;
  5028. const int sc = scales[i0 / (QI8_1/2)];
  5029. // fill int with 4x m
  5030. int m = sc >> 4;
  5031. m |= m << 8;
  5032. m |= m << 16;
  5033. #pragma unroll
  5034. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  5035. sumi_d_sc = dpct::dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  5036. sumi_m = dpct::dp4a(m, u[i],
  5037. sumi_m); // multiply sum of q8_1 values with m
  5038. }
  5039. sumi_d += sumi_d_sc * (sc & 0xF);
  5040. }
  5041. const sycl::float2 dm2f =
  5042. dm2.convert<float, sycl::rounding_mode::automatic>();
  5043. return d8 * (dm2f.x() * sumi_d - dm2f.y() * sumi_m);
  5044. }
  5045. #define VDR_Q3_K_Q8_1_MMVQ 1
  5046. #define VDR_Q3_K_Q8_1_MMQ 2
  5047. // contiguous v/x values
  5048. static __dpct_inline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  5049. const int &vl, const int &vh, const int *__restrict__ u,
  5050. const uint8_t *__restrict__ scales, const int &scale_offset,
  5051. const float &d3, const float *__restrict__ d8) {
  5052. float sumf = 0.0f;
  5053. #pragma unroll
  5054. for (int i = 0; i < QR3_K; ++i) {
  5055. const int isc = scale_offset + 2*i;
  5056. const int isc_low = isc % (QK_K/32);
  5057. const int sc_shift_low = 4 * (isc / (QK_K/32));
  5058. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  5059. const int isc_high = isc % (QK_K/64);
  5060. const int sc_shift_high = 2 * (isc / (QK_K/64));
  5061. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  5062. const int sc = (sc_low | sc_high) - 32;
  5063. const int vil = (vl >> (2*i)) & 0x03030303;
  5064. const int vih = ((vh >> i) << 2) & 0x04040404;
  5065. const int vi =
  5066. dpct::vectorized_binary<sycl::char4>(vil, vih, dpct::sub_sat());
  5067. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  5068. }
  5069. return d3 * sumf;
  5070. }
  5071. // contiguous u/y values
  5072. static __dpct_inline__ float
  5073. vec_dot_q3_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  5074. const int8_t *__restrict__ scales, const float &d3,
  5075. const float &d8) {
  5076. int sumi = 0;
  5077. #pragma unroll
  5078. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  5079. int sumi_sc = 0;
  5080. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  5081. sumi_sc = dpct::dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  5082. }
  5083. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  5084. }
  5085. return d3*d8 * sumi;
  5086. }
  5087. #define VDR_Q4_K_Q8_1_MMVQ 2
  5088. #define VDR_Q4_K_Q8_1_MMQ 8
  5089. // contiguous v/x values
  5090. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  5091. const int *__restrict__ v, const int *__restrict__ u,
  5092. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5093. const sycl::half2 &dm4, const float *__restrict__ d8) {
  5094. float sumf_d = 0.0f;
  5095. float sumf_m = 0.0f;
  5096. #pragma unroll
  5097. for (int i = 0; i < QR4_K; ++i) {
  5098. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  5099. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  5100. const int dot1 =
  5101. dpct::dp4a(v1i, u[2 * i + 1],
  5102. dpct::dp4a(v0i, u[2 * i + 0], 0)); // SIMD dot product
  5103. const int dot2 =
  5104. dpct::dp4a(0x01010101, u[2 * i + 1],
  5105. dpct::dp4a(0x01010101, u[2 * i + 0], 0)); // sum of u
  5106. sumf_d += d8[i] * (dot1 * sc[i]);
  5107. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  5108. }
  5109. const sycl::float2 dm4f =
  5110. dm4.convert<float, sycl::rounding_mode::automatic>();
  5111. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5112. }
  5113. // contiguous u/y values
  5114. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_mmq(
  5115. const int *__restrict__ v, const int *__restrict__ u,
  5116. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5117. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  5118. float sumf_d = 0.0f;
  5119. float sumf_m = 0.0f;
  5120. #pragma unroll
  5121. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  5122. int sumi_d = 0;
  5123. #pragma unroll
  5124. for (int j = 0; j < QI8_1; ++j) {
  5125. sumi_d = dpct::dp4a((v[j] >> (4 * i)) & 0x0F0F0F0F,
  5126. u[i * QI8_1 + j], sumi_d); // SIMD dot product
  5127. }
  5128. const sycl::float2 ds8f =
  5129. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  5130. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  5131. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  5132. }
  5133. const sycl::float2 dm4f =
  5134. dm4.convert<float, sycl::rounding_mode::automatic>();
  5135. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5136. }
  5137. #define VDR_Q5_K_Q8_1_MMVQ 2
  5138. #define VDR_Q5_K_Q8_1_MMQ 8
  5139. // contiguous v/x values
  5140. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  5141. const int *__restrict__ vl, const int *__restrict__ vh,
  5142. const int *__restrict__ u, const uint8_t *__restrict__ sc,
  5143. const uint8_t *__restrict__ m, const sycl::half2 &dm5,
  5144. const float *__restrict__ d8) {
  5145. float sumf_d = 0.0f;
  5146. float sumf_m = 0.0f;
  5147. #pragma unroll
  5148. for (int i = 0; i < QR5_K; ++i) {
  5149. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  5150. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  5151. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  5152. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  5153. const int v0i = vl0i | vh0i;
  5154. const int v1i = vl1i | vh1i;
  5155. const int dot1 =
  5156. dpct::dp4a(v0i, u[2 * i + 0],
  5157. dpct::dp4a(v1i, u[2 * i + 1], 0)); // SIMD dot product
  5158. const int dot2 =
  5159. dpct::dp4a(0x01010101, u[2 * i + 0],
  5160. dpct::dp4a(0x01010101, u[2 * i + 1], 0)); // sum of u
  5161. sumf_d += d8[i] * (dot1 * sc[i]);
  5162. sumf_m += d8[i] * (dot2 * m[i]);
  5163. }
  5164. const sycl::float2 dm5f =
  5165. dm5.convert<float, sycl::rounding_mode::automatic>();
  5166. return dm5f.x() * sumf_d - dm5f.y() * sumf_m;
  5167. }
  5168. // contiguous u/y values
  5169. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_mmq(
  5170. const int *__restrict__ v, const int *__restrict__ u,
  5171. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5172. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  5173. float sumf_d = 0.0f;
  5174. float sumf_m = 0.0f;
  5175. #pragma unroll
  5176. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  5177. int sumi_d = 0;
  5178. #pragma unroll
  5179. for (int j = 0; j < QI8_1; ++j) {
  5180. sumi_d = dpct::dp4a(v[i * QI8_1 + j], u[i * QI8_1 + j],
  5181. sumi_d); // SIMD dot product
  5182. }
  5183. const sycl::float2 ds8f =
  5184. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  5185. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  5186. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  5187. }
  5188. const sycl::float2 dm4f =
  5189. dm4.convert<float, sycl::rounding_mode::automatic>();
  5190. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5191. }
  5192. #define VDR_Q6_K_Q8_1_MMVQ 1
  5193. #define VDR_Q6_K_Q8_1_MMQ 8
  5194. // contiguous v/x values
  5195. static __dpct_inline__ float
  5196. vec_dot_q6_K_q8_1_impl_mmvq(const int &vl, const int &vh,
  5197. const int *__restrict__ u,
  5198. const int8_t *__restrict__ scales, const float &d,
  5199. const float *__restrict__ d8) {
  5200. float sumf = 0.0f;
  5201. #pragma unroll
  5202. for (int i = 0; i < QR6_K; ++i) {
  5203. const int sc = scales[4*i];
  5204. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  5205. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  5206. const int vi = dpct::vectorized_binary<sycl::char4>(
  5207. (vil | vih), 0x20202020, dpct::sub_sat()); // vi = (vil | vih) - 32
  5208. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  5209. }
  5210. return d*sumf;
  5211. }
  5212. // contiguous u/y values
  5213. static __dpct_inline__ float
  5214. vec_dot_q6_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  5215. const int8_t *__restrict__ sc, const float &d6,
  5216. const float *__restrict__ d8) {
  5217. float sumf_d = 0.0f;
  5218. #pragma unroll
  5219. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  5220. sycl::int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  5221. #pragma unroll
  5222. for (int i = i0; i < i0 + 2; ++i) {
  5223. sumi_d.x() = dpct::dp4a(v[2 * i + 0], u[2 * i + 0],
  5224. sumi_d.x()); // SIMD dot product
  5225. sumi_d.x() = dpct::dp4a(v[2 * i + 1], u[2 * i + 1],
  5226. sumi_d.x()); // SIMD dot product
  5227. sumi_d.y() = dpct::dp4a(v[2 * i + 4], u[2 * i + 4],
  5228. sumi_d.y()); // SIMD dot product
  5229. sumi_d.y() = dpct::dp4a(v[2 * i + 5], u[2 * i + 5],
  5230. sumi_d.y()); // SIMD dot product
  5231. }
  5232. sumf_d += d8[i0 / 4] *
  5233. (sc[i0 / 2 + 0] * sumi_d.x() + sc[i0 / 2 + 1] * sumi_d.y());
  5234. }
  5235. return d6 * sumf_d;
  5236. }
  5237. static __dpct_inline__ float
  5238. vec_dot_q4_0_q8_1(const void *__restrict__ vbq,
  5239. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5240. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  5241. int v[VDR_Q4_0_Q8_1_MMVQ];
  5242. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  5243. #pragma unroll
  5244. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  5245. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  5246. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5247. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  5248. }
  5249. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  5250. }
  5251. template <int mmq_y>
  5252. static __dpct_inline__ void
  5253. allocate_tiles_q4_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5254. int *tile_x_qs_q4_0, float *tile_x_d_q4_0) {
  5255. (void)x_qh; (void)x_sc;
  5256. *x_ql = tile_x_qs_q4_0;
  5257. *x_dm = (sycl::half2 *)tile_x_d_q4_0;
  5258. }
  5259. template <int mmq_y, int nwarps, bool need_check>
  5260. static __dpct_inline__ void
  5261. load_tiles_q4_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5262. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5263. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5264. const int &k, const int &blocks_per_row) {
  5265. (void)x_qh; (void)x_sc;
  5266. GGML_SYCL_ASSUME(i_offset >= 0);
  5267. GGML_SYCL_ASSUME(i_offset < nwarps);
  5268. GGML_SYCL_ASSUME(k >= 0);
  5269. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5270. const int kbx = k / QI4_0;
  5271. const int kqsx = k % QI4_0;
  5272. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  5273. float * x_dmf = (float *) x_dm;
  5274. #pragma unroll
  5275. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5276. int i = i0 + i_offset;
  5277. if (need_check) {
  5278. i = sycl::min(i, i_max);
  5279. }
  5280. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5281. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5282. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  5283. }
  5284. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  5285. const int kbxd = k % blocks_per_tile_x_row;
  5286. #pragma unroll
  5287. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  5288. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  5289. if (need_check) {
  5290. i = sycl::min(i, i_max);
  5291. }
  5292. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5293. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  5294. }
  5295. }
  5296. static __dpct_inline__ float vec_dot_q4_0_q8_1_mul_mat(
  5297. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5298. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5299. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5300. const int &i, const int &j, const int &k) {
  5301. (void)x_qh; (void)x_sc;
  5302. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5303. const float * x_dmf = (const float *) x_dm;
  5304. int u[2*VDR_Q4_0_Q8_1_MMQ];
  5305. #pragma unroll
  5306. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  5307. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5308. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  5309. }
  5310. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  5311. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  5312. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5313. }
  5314. static __dpct_inline__ float
  5315. vec_dot_q4_1_q8_1(const void *__restrict__ vbq,
  5316. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5317. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  5318. int v[VDR_Q4_1_Q8_1_MMVQ];
  5319. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  5320. #pragma unroll
  5321. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  5322. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  5323. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5324. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  5325. }
  5326. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  5327. }
  5328. template <int mmq_y>
  5329. static __dpct_inline__ void
  5330. allocate_tiles_q4_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5331. int *tile_x_qs_q4_1, sycl::half2 *tile_x_dm_q4_1) {
  5332. (void)x_qh; (void)x_sc;
  5333. *x_ql = tile_x_qs_q4_1;
  5334. *x_dm = tile_x_dm_q4_1;
  5335. }
  5336. template <int mmq_y, int nwarps, bool need_check>
  5337. static __dpct_inline__ void
  5338. load_tiles_q4_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5339. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5340. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5341. const int &k, const int &blocks_per_row) {
  5342. (void)x_qh; (void)x_sc;
  5343. GGML_SYCL_ASSUME(i_offset >= 0);
  5344. GGML_SYCL_ASSUME(i_offset < nwarps);
  5345. GGML_SYCL_ASSUME(k >= 0);
  5346. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5347. const int kbx = k / QI4_1;
  5348. const int kqsx = k % QI4_1;
  5349. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  5350. #pragma unroll
  5351. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5352. int i = i0 + i_offset;
  5353. if (need_check) {
  5354. i = sycl::min(i, i_max);
  5355. }
  5356. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5357. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5358. }
  5359. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  5360. const int kbxd = k % blocks_per_tile_x_row;
  5361. #pragma unroll
  5362. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  5363. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  5364. if (need_check) {
  5365. i = sycl::min(i, i_max);
  5366. }
  5367. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5368. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  5369. }
  5370. }
  5371. static __dpct_inline__ float vec_dot_q4_1_q8_1_mul_mat(
  5372. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5373. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5374. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5375. const int &i, const int &j, const int &k) {
  5376. (void)x_qh; (void)x_sc;
  5377. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5378. int u[2*VDR_Q4_1_Q8_1_MMQ];
  5379. #pragma unroll
  5380. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  5381. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5382. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  5383. }
  5384. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  5385. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  5386. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5387. }
  5388. static __dpct_inline__ float
  5389. vec_dot_q5_0_q8_1(const void *__restrict__ vbq,
  5390. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5391. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  5392. int vl[VDR_Q5_0_Q8_1_MMVQ];
  5393. int vh[VDR_Q5_0_Q8_1_MMVQ];
  5394. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  5395. #pragma unroll
  5396. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  5397. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  5398. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  5399. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5400. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  5401. }
  5402. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  5403. }
  5404. template <int mmq_y>
  5405. static __dpct_inline__ void
  5406. allocate_tiles_q5_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5407. int *tile_x_ql_q5_0, float *tile_x_d_q5_0) {
  5408. (void)x_qh; (void)x_sc;
  5409. *x_ql = tile_x_ql_q5_0;
  5410. *x_dm = (sycl::half2 *)tile_x_d_q5_0;
  5411. }
  5412. template <int mmq_y, int nwarps, bool need_check>
  5413. static __dpct_inline__ void
  5414. load_tiles_q5_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5415. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5416. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5417. const int &k, const int &blocks_per_row) {
  5418. (void)x_qh; (void)x_sc;
  5419. GGML_SYCL_ASSUME(i_offset >= 0);
  5420. GGML_SYCL_ASSUME(i_offset < nwarps);
  5421. GGML_SYCL_ASSUME(k >= 0);
  5422. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5423. const int kbx = k / QI5_0;
  5424. const int kqsx = k % QI5_0;
  5425. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  5426. #pragma unroll
  5427. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5428. int i = i0 + i_offset;
  5429. if (need_check) {
  5430. i = sycl::min(i, i_max);
  5431. }
  5432. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5433. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  5434. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  5435. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5436. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5437. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5438. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5439. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5440. qs0 = dpct::vectorized_binary<sycl::char4>(
  5441. qs0, 0x10101010, dpct::sub_sat()); // subtract 16
  5442. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5443. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5444. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5445. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5446. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5447. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5448. qs1 = dpct::vectorized_binary<sycl::char4>(
  5449. qs1, 0x10101010, dpct::sub_sat()); // subtract 16
  5450. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5451. }
  5452. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  5453. const int kbxd = k % blocks_per_tile_x_row;
  5454. float * x_dmf = (float *) x_dm;
  5455. #pragma unroll
  5456. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  5457. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  5458. if (need_check) {
  5459. i = sycl::min(i, i_max);
  5460. }
  5461. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5462. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  5463. }
  5464. }
  5465. static __dpct_inline__ float vec_dot_q5_0_q8_1_mul_mat(
  5466. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5467. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5468. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5469. const int &i, const int &j, const int &k) {
  5470. (void)x_qh; (void)x_sc;
  5471. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5472. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  5473. const float * x_dmf = (const float *) x_dm;
  5474. const float * y_df = (const float *) y_ds;
  5475. int u[2*VDR_Q5_0_Q8_1_MMQ];
  5476. #pragma unroll
  5477. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  5478. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5479. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  5480. }
  5481. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  5482. (&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)]);
  5483. }
  5484. static __dpct_inline__ float
  5485. vec_dot_q5_1_q8_1(const void *__restrict__ vbq,
  5486. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5487. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  5488. int vl[VDR_Q5_1_Q8_1_MMVQ];
  5489. int vh[VDR_Q5_1_Q8_1_MMVQ];
  5490. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  5491. #pragma unroll
  5492. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  5493. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  5494. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  5495. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5496. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  5497. }
  5498. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  5499. }
  5500. template <int mmq_y>
  5501. static __dpct_inline__ void
  5502. allocate_tiles_q5_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5503. int *tile_x_ql_q5_1, sycl::half2 *tile_x_dm_q5_1) {
  5504. (void)x_qh; (void)x_sc;
  5505. *x_ql = tile_x_ql_q5_1;
  5506. *x_dm = tile_x_dm_q5_1;
  5507. }
  5508. template <int mmq_y, int nwarps, bool need_check>
  5509. static __dpct_inline__ void
  5510. load_tiles_q5_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5511. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5512. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5513. const int &k, const int &blocks_per_row) {
  5514. (void)x_qh; (void)x_sc;
  5515. GGML_SYCL_ASSUME(i_offset >= 0);
  5516. GGML_SYCL_ASSUME(i_offset < nwarps);
  5517. GGML_SYCL_ASSUME(k >= 0);
  5518. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5519. const int kbx = k / QI5_1;
  5520. const int kqsx = k % QI5_1;
  5521. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  5522. #pragma unroll
  5523. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5524. int i = i0 + i_offset;
  5525. if (need_check) {
  5526. i = sycl::min(i, i_max);
  5527. }
  5528. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5529. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5530. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  5531. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5532. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5533. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5534. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5535. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5536. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5537. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5538. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5539. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5540. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5541. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5542. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5543. }
  5544. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  5545. const int kbxd = k % blocks_per_tile_x_row;
  5546. #pragma unroll
  5547. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  5548. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  5549. if (need_check) {
  5550. i = sycl::min(i, i_max);
  5551. }
  5552. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5553. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  5554. }
  5555. }
  5556. static __dpct_inline__ float vec_dot_q5_1_q8_1_mul_mat(
  5557. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5558. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5559. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5560. const int &i, const int &j, const int &k) {
  5561. (void)x_qh; (void)x_sc;
  5562. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5563. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  5564. int u[2*VDR_Q5_1_Q8_1_MMQ];
  5565. #pragma unroll
  5566. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  5567. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5568. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  5569. }
  5570. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  5571. (&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)]);
  5572. }
  5573. static __dpct_inline__ float
  5574. vec_dot_q8_0_q8_1(const void *__restrict__ vbq,
  5575. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5576. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  5577. int v[VDR_Q8_0_Q8_1_MMVQ];
  5578. int u[VDR_Q8_0_Q8_1_MMVQ];
  5579. #pragma unroll
  5580. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  5581. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  5582. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5583. }
  5584. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d,
  5585. bq8_1->ds[0]);
  5586. }
  5587. template <int mmq_y>
  5588. static __dpct_inline__ void
  5589. allocate_tiles_q8_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5590. int *tile_x_qs_q8_0, float *tile_x_d_q8_0) {
  5591. (void)x_qh; (void)x_sc;
  5592. *x_ql = tile_x_qs_q8_0;
  5593. *x_dm = (sycl::half2 *)tile_x_d_q8_0;
  5594. }
  5595. template <int mmq_y, int nwarps, bool need_check>
  5596. static __dpct_inline__ void
  5597. load_tiles_q8_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5598. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5599. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5600. const int &k, const int &blocks_per_row) {
  5601. (void)x_qh; (void)x_sc;
  5602. GGML_SYCL_ASSUME(i_offset >= 0);
  5603. GGML_SYCL_ASSUME(i_offset < nwarps);
  5604. GGML_SYCL_ASSUME(k >= 0);
  5605. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5606. const int kbx = k / QI8_0;
  5607. const int kqsx = k % QI8_0;
  5608. float * x_dmf = (float *) x_dm;
  5609. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  5610. #pragma unroll
  5611. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5612. int i = i0 + i_offset;
  5613. if (need_check) {
  5614. i = sycl::min(i, i_max);
  5615. }
  5616. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5617. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  5618. }
  5619. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  5620. const int kbxd = k % blocks_per_tile_x_row;
  5621. #pragma unroll
  5622. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  5623. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  5624. if (need_check) {
  5625. i = sycl::min(i, i_max);
  5626. }
  5627. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5628. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  5629. }
  5630. }
  5631. static __dpct_inline__ float vec_dot_q8_0_q8_1_mul_mat(
  5632. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5633. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5634. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5635. const int &i, const int &j, const int &k) {
  5636. (void)x_qh; (void)x_sc;
  5637. const float * x_dmf = (const float *) x_dm;
  5638. const float * y_df = (const float *) y_ds;
  5639. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  5640. (&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],
  5641. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  5642. }
  5643. static __dpct_inline__ float
  5644. vec_dot_q2_K_q8_1(const void *__restrict__ vbq,
  5645. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5646. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  5647. const int bq8_offset = QR2_K * (iqs / QI8_1);
  5648. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5649. const uint8_t * scales = bq2_K->scales + scale_offset;
  5650. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  5651. int u[QR2_K];
  5652. float d8[QR2_K];
  5653. #pragma unroll
  5654. for (int i = 0; i < QR2_K; ++ i) {
  5655. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5656. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5657. }
  5658. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  5659. }
  5660. template <int mmq_y>
  5661. static __dpct_inline__ void
  5662. allocate_tiles_q2_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5663. int *tile_x_ql_q2_K, sycl::half2 *tile_x_dm_q2_K,
  5664. int *tile_x_sc_q2_K) {
  5665. (void)x_qh;
  5666. *x_ql = tile_x_ql_q2_K;
  5667. *x_dm = tile_x_dm_q2_K;
  5668. *x_sc = tile_x_sc_q2_K;
  5669. }
  5670. template <int mmq_y, int nwarps, bool need_check>
  5671. static __dpct_inline__ void
  5672. load_tiles_q2_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5673. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5674. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5675. const int &k, const int &blocks_per_row) {
  5676. (void)x_qh;
  5677. GGML_SYCL_ASSUME(i_offset >= 0);
  5678. GGML_SYCL_ASSUME(i_offset < nwarps);
  5679. GGML_SYCL_ASSUME(k >= 0);
  5680. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5681. const int kbx = k / QI2_K;
  5682. const int kqsx = k % QI2_K;
  5683. const block_q2_K * bx0 = (const block_q2_K *) vx;
  5684. #pragma unroll
  5685. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5686. int i = i0 + i_offset;
  5687. if (need_check) {
  5688. i = sycl::min(i, i_max);
  5689. }
  5690. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  5691. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5692. }
  5693. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  5694. const int kbxd = k % blocks_per_tile_x_row;
  5695. #pragma unroll
  5696. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  5697. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  5698. if (need_check) {
  5699. i = sycl::min(i, i_max);
  5700. }
  5701. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5702. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  5703. }
  5704. #pragma unroll
  5705. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5706. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5707. if (need_check) {
  5708. i = sycl::min(i, i_max);
  5709. }
  5710. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  5711. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  5712. }
  5713. }
  5714. static __dpct_inline__ float vec_dot_q2_K_q8_1_mul_mat(
  5715. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5716. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5717. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5718. const int &i, const int &j, const int &k) {
  5719. (void)x_qh;
  5720. const int kbx = k / QI2_K;
  5721. const int ky = (k % QI2_K) * QR2_K;
  5722. const float * y_df = (const float *) y_ds;
  5723. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  5724. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  5725. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  5726. #pragma unroll
  5727. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  5728. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5729. }
  5730. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  5731. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  5732. 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]);
  5733. }
  5734. static __dpct_inline__ float
  5735. vec_dot_q3_K_q8_1(const void *__restrict__ vbq,
  5736. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5737. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  5738. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  5739. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5740. const float d = bq3_K->d;
  5741. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  5742. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5743. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  5744. int u[QR3_K];
  5745. float d8[QR3_K];
  5746. #pragma unroll
  5747. for (int i = 0; i < QR3_K; ++i) {
  5748. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5749. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5750. }
  5751. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  5752. }
  5753. template <int mmq_y>
  5754. static __dpct_inline__ void
  5755. allocate_tiles_q3_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5756. int *tile_x_ql_q3_K, sycl::half2 *tile_x_dm_q3_K,
  5757. int *tile_x_qh_q3_K, int *tile_x_sc_q3_K) {
  5758. *x_ql = tile_x_ql_q3_K;
  5759. *x_dm = tile_x_dm_q3_K;
  5760. *x_qh = tile_x_qh_q3_K;
  5761. *x_sc = tile_x_sc_q3_K;
  5762. }
  5763. template <int mmq_y, int nwarps, bool need_check>
  5764. static __dpct_inline__ void
  5765. load_tiles_q3_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5766. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5767. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5768. const int &k, const int &blocks_per_row) {
  5769. GGML_SYCL_ASSUME(i_offset >= 0);
  5770. GGML_SYCL_ASSUME(i_offset < nwarps);
  5771. GGML_SYCL_ASSUME(k >= 0);
  5772. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5773. const int kbx = k / QI3_K;
  5774. const int kqsx = k % QI3_K;
  5775. const block_q3_K * bx0 = (const block_q3_K *) vx;
  5776. #pragma unroll
  5777. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5778. int i = i0 + i_offset;
  5779. if (need_check) {
  5780. i = sycl::min(i, i_max);
  5781. }
  5782. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  5783. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5784. }
  5785. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  5786. const int kbxd = k % blocks_per_tile_x_row;
  5787. float * x_dmf = (float *) x_dm;
  5788. #pragma unroll
  5789. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  5790. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  5791. if (need_check) {
  5792. i = sycl::min(i, i_max);
  5793. }
  5794. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5795. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  5796. }
  5797. #pragma unroll
  5798. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  5799. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  5800. if (need_check) {
  5801. i = sycl::min(i, i_max);
  5802. }
  5803. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  5804. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5805. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  5806. }
  5807. #pragma unroll
  5808. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5809. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5810. if (need_check) {
  5811. i = sycl::min(i, i_max);
  5812. }
  5813. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  5814. const int ksc = k % (QI3_K/4);
  5815. const int ksc_low = ksc % (QI3_K/8);
  5816. const int shift_low = 4 * (ksc / (QI3_K/8));
  5817. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  5818. const int ksc_high = QI3_K/8;
  5819. const int shift_high = 2 * ksc;
  5820. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  5821. const int sc = dpct::vectorized_binary<sycl::char4>(
  5822. sc_low | sc_high, 0x20202020, dpct::sub_sat());
  5823. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  5824. }
  5825. }
  5826. static __dpct_inline__ float vec_dot_q3_K_q8_1_mul_mat(
  5827. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5828. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5829. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5830. const int &i, const int &j, const int &k) {
  5831. const int kbx = k / QI3_K;
  5832. const int ky = (k % QI3_K) * QR3_K;
  5833. const float * x_dmf = (const float *) x_dm;
  5834. const float * y_df = (const float *) y_ds;
  5835. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  5836. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  5837. #pragma unroll
  5838. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  5839. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  5840. const int shift = 2 * ((ky % 32) / 8);
  5841. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5842. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  5843. const int vlh = (vh << 2) & 0x04040404;
  5844. v[l] = dpct::vectorized_binary<sycl::char4>(vll, vlh, dpct::sub_sat());
  5845. }
  5846. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  5847. 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]);
  5848. }
  5849. static __dpct_inline__ float
  5850. vec_dot_q4_K_q8_1(const void *__restrict__ vbq,
  5851. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5852. #ifndef GGML_QKK_64
  5853. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5854. int v[2];
  5855. int u[2*QR4_K];
  5856. float d8[QR4_K];
  5857. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  5858. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  5859. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  5860. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  5861. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  5862. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  5863. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5864. v[0] = q4[0];
  5865. v[1] = q4[4];
  5866. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  5867. uint16_t aux[2];
  5868. const int j = bq8_offset/2;
  5869. if (j < 2) {
  5870. aux[0] = scales[j+0] & 0x3f3f;
  5871. aux[1] = scales[j+2] & 0x3f3f;
  5872. } else {
  5873. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5874. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5875. }
  5876. const uint8_t * sc = (const uint8_t *)aux;
  5877. const uint8_t * m = sc + 2;
  5878. for (int i = 0; i < QR4_K; ++i) {
  5879. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5880. d8[i] = bq8i->ds[0];
  5881. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5882. u[2*i+0] = q8[0];
  5883. u[2*i+1] = q8[4];
  5884. }
  5885. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  5886. #else
  5887. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  5888. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5889. float sumf_d = 0.0f;
  5890. float sumf_m = 0.0f;
  5891. uint16_t aux16[2];
  5892. const uint8_t * s = (const uint8_t *)aux16;
  5893. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  5894. aux16[0] = a[0] & 0x0f0f;
  5895. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5896. const float dall = bq4_K->dm[0];
  5897. const float dmin = bq4_K->dm[1];
  5898. const float d8_1 = bq8_1[0].ds[0];
  5899. const float d8_2 = bq8_1[1].ds[1];
  5900. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  5901. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  5902. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  5903. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  5904. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  5905. const int v1 = q4[0];
  5906. const int v2 = q4[4];
  5907. const int dot1 = dpct::dp4a(ui2, v2 & 0x0f0f0f0f, dpct::dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  5908. const int dot2 = dpct::dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, dpct::dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  5909. const int dot3 = dpct::dp4a(0x01010101, ui2, dpct::dp4a(0x01010101, ui1, 0));
  5910. const int dot4 = dpct::dp4a(0x01010101, ui4, dpct::dp4a(0x01010101, ui3, 0));
  5911. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  5912. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  5913. return dall * sumf_d - dmin * sumf_m;
  5914. #else
  5915. bad_arch();
  5916. #endif // __SYCL_ARCH__ >= VER_4VEC
  5917. #endif
  5918. }
  5919. template <int mmq_y>
  5920. static __dpct_inline__ void
  5921. allocate_tiles_q4_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5922. int *tile_x_ql_q4_K, sycl::half2 *tile_x_dm_q4_K,
  5923. int *tile_x_sc_q4_K) {
  5924. (void)x_qh;
  5925. *x_ql = tile_x_ql_q4_K;
  5926. *x_dm = tile_x_dm_q4_K;
  5927. *x_sc = tile_x_sc_q4_K;
  5928. }
  5929. template <int mmq_y, int nwarps, bool need_check>
  5930. static __dpct_inline__ void
  5931. load_tiles_q4_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5932. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5933. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5934. const int &k, const int &blocks_per_row) {
  5935. (void)x_qh;
  5936. GGML_SYCL_ASSUME(i_offset >= 0);
  5937. GGML_SYCL_ASSUME(i_offset < nwarps);
  5938. GGML_SYCL_ASSUME(k >= 0);
  5939. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5940. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  5941. const int kqsx = k % QI4_K; // == k if QK_K == 256
  5942. const block_q4_K * bx0 = (const block_q4_K *) vx;
  5943. #pragma unroll
  5944. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5945. int i = i0 + i_offset;
  5946. if (need_check) {
  5947. i = sycl::min(i, i_max);
  5948. }
  5949. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  5950. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5951. }
  5952. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  5953. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5954. #pragma unroll
  5955. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  5956. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  5957. if (need_check) {
  5958. i = sycl::min(i, i_max);
  5959. }
  5960. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5961. #if QK_K == 256
  5962. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  5963. #else
  5964. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  5965. #endif
  5966. }
  5967. #pragma unroll
  5968. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5969. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5970. if (need_check) {
  5971. i = sycl::min(i, i_max);
  5972. }
  5973. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  5974. const int * scales = (const int *) bxi->scales;
  5975. const int ksc = k % (WARP_SIZE/8);
  5976. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5977. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5978. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5979. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5980. }
  5981. }
  5982. static __dpct_inline__ float vec_dot_q4_K_q8_1_mul_mat(
  5983. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5984. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5985. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5986. const int &i, const int &j, const int &k) {
  5987. (void)x_qh;
  5988. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  5989. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  5990. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  5991. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  5992. }
  5993. static __dpct_inline__ float
  5994. vec_dot_q5_K_q8_1(const void *__restrict__ vbq,
  5995. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5996. #ifndef GGML_QKK_64
  5997. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  5998. int vl[2];
  5999. int vh[2];
  6000. int u[2*QR5_K];
  6001. float d8[QR5_K];
  6002. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  6003. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  6004. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  6005. vl[0] = ql[0];
  6006. vl[1] = ql[4];
  6007. vh[0] = qh[0] >> bq8_offset;
  6008. vh[1] = qh[4] >> bq8_offset;
  6009. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  6010. uint16_t aux[2];
  6011. const int j = bq8_offset/2;
  6012. if (j < 2) {
  6013. aux[0] = scales[j+0] & 0x3f3f;
  6014. aux[1] = scales[j+2] & 0x3f3f;
  6015. } else {
  6016. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  6017. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  6018. }
  6019. const uint8_t * sc = (const uint8_t *)aux;
  6020. const uint8_t * m = sc + 2;
  6021. #pragma unroll
  6022. for (int i = 0; i < QR5_K; ++i) {
  6023. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  6024. d8[i] = bq8i->ds[0];
  6025. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  6026. u[2*i+0] = q8[0];
  6027. u[2*i+1] = q8[4];
  6028. }
  6029. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  6030. #else
  6031. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  6032. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  6033. const int8_t * s = bq5_K->scales;
  6034. const float d = bq5_K->d;
  6035. const float d8_1 = bq8_1[0].ds[0];
  6036. const float d8_2 = bq8_1[1].ds[1];
  6037. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  6038. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  6039. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  6040. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  6041. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  6042. const int vl1 = ql[0];
  6043. const int vl2 = ql[4];
  6044. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  6045. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  6046. const int in = step%8; // 0, 4, 0, 4
  6047. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  6048. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  6049. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  6050. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  6051. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  6052. const float sumf_d = d8_1 * (dpct::dp4a(ui1, v1, 0) * s[0] + dpct::dp4a(ui2, v2, 0) * s[1])
  6053. + d8_2 * (dpct::dp4a(ui3, v3, 0) * s[2] + dpct::dp4a(ui4, v4, 0) * s[3]);
  6054. return d * sumf_d;
  6055. #else
  6056. bad_arch();
  6057. #endif // __SYCL_ARCH__ >= VER_4VEC
  6058. #endif
  6059. }
  6060. template <int mmq_y>
  6061. static __dpct_inline__ void
  6062. allocate_tiles_q5_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  6063. int *tile_x_ql_q5_K, sycl::half2 *tile_x_dm_q5_K,
  6064. int *tile_x_sc_q5_K) {
  6065. (void)x_qh;
  6066. *x_ql = tile_x_ql_q5_K;
  6067. *x_dm = tile_x_dm_q5_K;
  6068. *x_sc = tile_x_sc_q5_K;
  6069. }
  6070. template <int mmq_y, int nwarps, bool need_check>
  6071. static __dpct_inline__ void
  6072. load_tiles_q5_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  6073. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6074. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6075. const int &k, const int &blocks_per_row) {
  6076. (void)x_qh;
  6077. GGML_SYCL_ASSUME(i_offset >= 0);
  6078. GGML_SYCL_ASSUME(i_offset < nwarps);
  6079. GGML_SYCL_ASSUME(k >= 0);
  6080. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6081. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  6082. const int kqsx = k % QI5_K; // == k if QK_K == 256
  6083. const block_q5_K * bx0 = (const block_q5_K *) vx;
  6084. #pragma unroll
  6085. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6086. int i = i0 + i_offset;
  6087. if (need_check) {
  6088. i = sycl::min(i, i_max);
  6089. }
  6090. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  6091. const int ky = QR5_K*kqsx;
  6092. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  6093. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  6094. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  6095. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  6096. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  6097. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  6098. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  6099. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  6100. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  6101. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  6102. }
  6103. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  6104. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  6105. #pragma unroll
  6106. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  6107. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  6108. if (need_check) {
  6109. i = sycl::min(i, i_max);
  6110. }
  6111. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  6112. #if QK_K == 256
  6113. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  6114. #endif
  6115. }
  6116. #pragma unroll
  6117. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  6118. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  6119. if (need_check) {
  6120. i = sycl::min(i, i_max);
  6121. }
  6122. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  6123. const int * scales = (const int *) bxi->scales;
  6124. const int ksc = k % (WARP_SIZE/8);
  6125. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  6126. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  6127. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  6128. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  6129. }
  6130. }
  6131. static __dpct_inline__ float vec_dot_q5_K_q8_1_mul_mat(
  6132. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6133. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6134. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6135. const int &i, const int &j, const int &k) {
  6136. (void)x_qh;
  6137. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  6138. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  6139. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  6140. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  6141. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  6142. }
  6143. static __dpct_inline__ float
  6144. vec_dot_q6_K_q8_1(const void *__restrict__ vbq,
  6145. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6146. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  6147. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  6148. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  6149. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  6150. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  6151. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  6152. const int8_t * scales = bq6_K->scales + scale_offset;
  6153. int u[QR6_K];
  6154. float d8[QR6_K];
  6155. #pragma unroll
  6156. for (int i = 0; i < QR6_K; ++i) {
  6157. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  6158. d8[i] = bq8_1[bq8_offset + 2 * i].ds[0];
  6159. }
  6160. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  6161. }
  6162. template <int mmq_y>
  6163. static __dpct_inline__ void
  6164. allocate_tiles_q6_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  6165. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_sc) {
  6166. (void)x_qh;
  6167. *x_ql = tile_x_ql;
  6168. *x_dm = tile_x_dm;
  6169. *x_sc = tile_x_sc;
  6170. }
  6171. template <int mmq_y, int nwarps, bool need_check>
  6172. static __dpct_inline__ void
  6173. load_tiles_q6_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  6174. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6175. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6176. const int &k, const int &blocks_per_row) {
  6177. (void)x_qh;
  6178. GGML_SYCL_ASSUME(i_offset >= 0);
  6179. GGML_SYCL_ASSUME(i_offset < nwarps);
  6180. GGML_SYCL_ASSUME(k >= 0);
  6181. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6182. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  6183. const int kqsx = k % QI6_K; // == k if QK_K == 256
  6184. const block_q6_K * bx0 = (const block_q6_K *) vx;
  6185. #pragma unroll
  6186. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6187. int i = i0 + i_offset;
  6188. if (need_check) {
  6189. i = sycl::min(i, i_max);
  6190. }
  6191. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  6192. const int ky = QR6_K*kqsx;
  6193. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  6194. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  6195. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  6196. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  6197. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  6198. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  6199. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  6200. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  6201. x_ql[i * (2 * WARP_SIZE + 1) + kq0] =
  6202. dpct::vectorized_binary<sycl::char4>(ql0 | qh0, 0x20202020,
  6203. dpct::sub_sat());
  6204. x_ql[i * (2 * WARP_SIZE + 1) + kq1] =
  6205. dpct::vectorized_binary<sycl::char4>(ql1 | qh1, 0x20202020,
  6206. dpct::sub_sat());
  6207. }
  6208. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  6209. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  6210. float * x_dmf = (float *) x_dm;
  6211. #pragma unroll
  6212. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  6213. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  6214. if (need_check) {
  6215. i = sycl::min(i, i_max);
  6216. }
  6217. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  6218. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  6219. }
  6220. #pragma unroll
  6221. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  6222. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  6223. if (need_check) {
  6224. i = sycl::min(i, i_max);
  6225. }
  6226. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  6227. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  6228. }
  6229. }
  6230. static __dpct_inline__ float vec_dot_q6_K_q8_1_mul_mat(
  6231. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6232. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6233. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6234. const int &i, const int &j, const int &k) {
  6235. (void)x_qh;
  6236. const float * x_dmf = (const float *) x_dm;
  6237. const float * y_df = (const float *) y_ds;
  6238. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  6239. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  6240. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  6241. 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]);
  6242. }
  6243. static __dpct_inline__ float
  6244. vec_dot_iq2_xxs_q8_1(const void *__restrict__ vbq,
  6245. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6246. const uint64_t *iq2xxs_grid, const uint8_t *ksigns_iq2xs,
  6247. const uint8_t *kmask_iq2xs) {
  6248. #if QK_K == 256
  6249. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  6250. #if QR2_XXS == 8
  6251. const int ib32 = iqs;
  6252. const uint16_t * q2 = bq2->qs + 4*ib32;
  6253. const uint8_t * aux8 = (const uint8_t *)q2;
  6254. const int8_t * q8 = bq8_1[ib32].qs;
  6255. uint32_t aux32 = q2[2] | (q2[3] << 16);
  6256. int sumi = 0;
  6257. for (int l = 0; l < 4; ++l) {
  6258. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6259. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  6260. for (int j = 0; j < 8; ++j) {
  6261. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6262. }
  6263. q8 += 8;
  6264. aux32 >>= 7;
  6265. }
  6266. const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.25f;
  6267. return d * sumi;
  6268. #else
  6269. // iqs is 0...15
  6270. const int ib32 = iqs/2;
  6271. const int il = iqs%2;
  6272. const uint16_t * q2 = bq2->qs + 4*ib32;
  6273. const uint8_t * aux8 = (const uint8_t *)q2;
  6274. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  6275. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  6276. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  6277. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * bq8_1[ib32].ds[0] * 0.25f;
  6278. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  6279. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  6280. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  6281. int sumi1 = 0, sumi2 = 0;
  6282. for (int j = 0; j < 8; ++j) {
  6283. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  6284. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  6285. }
  6286. return d * (sumi1 + sumi2);
  6287. #endif
  6288. #else
  6289. assert(false);
  6290. return 0.f;
  6291. #endif
  6292. }
  6293. static __dpct_inline__ float
  6294. vec_dot_iq2_xs_q8_1(const void *__restrict__ vbq,
  6295. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6296. const uint64_t *iq2xs_grid, const uint64_t *ksigns64) {
  6297. #if DPCT_COMPATIBILITY_TEMP >= \
  6298. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6299. #if QK_K == 256
  6300. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  6301. const int ib32 = iqs;
  6302. const uint16_t * q2 = bq2->qs + 4*ib32;
  6303. const int8_t * q8 = bq8_1[ib32].qs;
  6304. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  6305. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  6306. int sumi1 = 0;
  6307. for (int l = 0; l < 2; ++l) {
  6308. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  6309. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  6310. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6311. grid[0] ^ signs[0], signs[0], std::minus<>());
  6312. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6313. grid[1] ^ signs[1], signs[1], std::minus<>());
  6314. sumi1 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  6315. sumi1 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  6316. q8 += 8;
  6317. }
  6318. int sumi2 = 0;
  6319. for (int l = 2; l < 4; ++l) {
  6320. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  6321. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  6322. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6323. grid[0] ^ signs[0], signs[0], std::minus<>());
  6324. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6325. grid[1] ^ signs[1], signs[1], std::minus<>());
  6326. sumi2 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  6327. sumi2 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  6328. q8 += 8;
  6329. }
  6330. const float d = (float)bq2->d * bq8_1[ib32].ds[0] * 0.25f;
  6331. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  6332. #else
  6333. assert(false);
  6334. return 0.f;
  6335. #endif
  6336. #else
  6337. assert(false);
  6338. return 0.f;
  6339. #endif
  6340. }
  6341. static __dpct_inline__ float
  6342. vec_dot_iq2_s_q8_1(const void *__restrict__ vbq,
  6343. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6344. #if QK_K == 256
  6345. const block_iq2_s * bq2 = (const block_iq2_s *) vbq;
  6346. const int ib32 = iqs;
  6347. const int8_t * q8 = bq8_1[ib32].qs;
  6348. const uint8_t * signs = bq2->qs + QK_K/8 + 4*ib32;
  6349. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  6350. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  6351. int sumi1 = 0;
  6352. for (int l = 0; l < 2; ++l) {
  6353. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  6354. const uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6355. ((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201,
  6356. std::equal_to<>());
  6357. const uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6358. ((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201,
  6359. std::equal_to<>());
  6360. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6361. grid[0] ^ signs0, signs0, std::minus<>());
  6362. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6363. grid[1] ^ signs1, signs1, std::minus<>());
  6364. sumi1 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  6365. sumi1 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  6366. q8 += 8;
  6367. }
  6368. int sumi2 = 0;
  6369. for (int l = 2; l < 4; ++l) {
  6370. const uint32_t * grid = (const uint32_t *)(iq2s_grid + (bq2->qs[4*ib32+l] | ((bq2->qh[ib32] << (8-2*l)) & 0x300)));
  6371. const uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6372. ((signs[l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201,
  6373. std::equal_to<>());
  6374. const uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6375. ((signs[l] >> 4) * 0x01010101) & 0x08040201, 0x08040201,
  6376. std::equal_to<>());
  6377. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6378. grid[0] ^ signs0, signs0, std::minus<>());
  6379. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6380. grid[1] ^ signs1, signs1, std::minus<>());
  6381. sumi2 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  6382. sumi2 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  6383. q8 += 8;
  6384. }
  6385. const float d = (float)bq2->d * bq8_1[ib32].ds[0] * 0.25f;
  6386. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  6387. #else
  6388. assert(false);
  6389. #endif
  6390. }
  6391. static __dpct_inline__ float
  6392. vec_dot_iq3_xxs_q8_1(const void *__restrict__ vbq,
  6393. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6394. const uint32_t *iq3xxs_grid, const uint64_t *ksigns64) {
  6395. #if DPCT_COMPATIBILITY_TEMP >= \
  6396. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6397. #if QK_K == 256
  6398. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  6399. const int ib32 = iqs;
  6400. const uint8_t * q3 = bq2->qs + 8*ib32;
  6401. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  6402. const int8_t * q8 = bq8_1[ib32].qs;
  6403. uint32_t aux32 = gas[0] | (gas[1] << 16);
  6404. int sumi = 0;
  6405. for (int l = 0; l < 4; ++l) {
  6406. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  6407. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  6408. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  6409. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6410. grid1[0] ^ signs[0], signs[0], std::minus<>());
  6411. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6412. grid2[0] ^ signs[1], signs[1], std::minus<>());
  6413. sumi = dpct::dp4a(grid_l, *((int *)q8 + 0), sumi);
  6414. sumi = dpct::dp4a(grid_h, *((int *)q8 + 1), sumi);
  6415. q8 += 8;
  6416. aux32 >>= 7;
  6417. }
  6418. const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.5f;
  6419. return d * sumi;
  6420. #else
  6421. assert(false);
  6422. return 0.f;
  6423. #endif
  6424. #else
  6425. assert(false);
  6426. return 0.f;
  6427. #endif
  6428. }
  6429. static __dpct_inline__ float
  6430. vec_dot_iq3_s_q8_1(const void *__restrict__ vbq,
  6431. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6432. const uint32_t *iq3s_grid) {
  6433. #if QK_K == 256
  6434. const block_iq3_s * bq2 = (const block_iq3_s *) vbq;
  6435. const int ib32 = iqs;
  6436. const uint8_t * qs = bq2->qs + 8*ib32;
  6437. const int8_t * q8 = bq8_1[ib32].qs;
  6438. int sumi = 0;
  6439. for (int l = 0; l < 4; ++l) {
  6440. const uint32_t * grid1 = iq3s_grid + (qs[2*l+0] | ((bq2->qh[ib32] << (8 - 2*l)) & 256));
  6441. const uint32_t * grid2 = iq3s_grid + (qs[2*l+1] | ((bq2->qh[ib32] << (7 - 2*l)) & 256));
  6442. uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6443. ((bq2->signs[4 * ib32 + l] & 0xf) * 0x01010101) & 0x08040201,
  6444. 0x08040201, std::equal_to<>());
  6445. uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6446. ((bq2->signs[4 * ib32 + l] >> 4) * 0x01010101) & 0x08040201,
  6447. 0x08040201, std::equal_to<>());
  6448. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6449. grid1[0] ^ signs0, signs0, std::minus<>());
  6450. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6451. grid2[0] ^ signs1, signs1, std::minus<>());
  6452. sumi = dpct::dp4a(grid_l, *((int *)q8 + 0), sumi);
  6453. sumi = dpct::dp4a(grid_h, *((int *)q8 + 1), sumi);
  6454. q8 += 8;
  6455. }
  6456. const float d =
  6457. (float)bq2->d *
  6458. (1 + 2 * ((bq2->scales[ib32 / 2] >> 4 * (ib32 % 2)) & 0xf)) *
  6459. bq8_1[ib32].ds[0];
  6460. return d * sumi;
  6461. #else
  6462. assert(false);
  6463. #endif
  6464. }
  6465. static __dpct_inline__ float
  6466. vec_dot_iq1_s_q8_1(const void *__restrict__ vbq,
  6467. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6468. const uint32_t *iq1s_grid_gpu) {
  6469. #if QK_K == 256
  6470. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  6471. const int ib32 = iqs;
  6472. int sumi = 0;
  6473. const int * q8 = (const int *)bq8_1[ib32].qs;
  6474. for (int l = 0; l < 4; ++l) {
  6475. const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[ib32] >> 3*l) & 7) << 8)));
  6476. int grid0 = grid[0] & 0x0f0f0f0f;
  6477. int grid1 = (grid[0] >> 4) & 0x0f0f0f0f;
  6478. sumi = dpct::dp4a(q8[2 * l + 1], grid1,
  6479. dpct::dp4a(q8[2 * l + 0], grid0, sumi));
  6480. }
  6481. const float delta = bq1->qh[ib32] & 0x8000 ? -1-IQ1S_DELTA : -1+IQ1S_DELTA;
  6482. const float d1q = (float)bq1->d * (2*((bq1->qh[ib32] >> 12) & 7) + 1);
  6483. const float d = d1q * bq8_1[ib32].ds[0];
  6484. const float m = d1q * bq8_1[ib32].ds[1];
  6485. return d * sumi + m * delta;
  6486. #else
  6487. assert(false);
  6488. #endif
  6489. }
  6490. static __dpct_inline__ float
  6491. vec_dot_iq1_m_q8_1(const void *__restrict__ vbq,
  6492. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6493. #if QK_K == 256
  6494. const block_iq1_m * bq1 = (const block_iq1_m *) vbq;
  6495. const int ib32 = iqs;
  6496. int sumi[2] = {0, 0};
  6497. float sumf[2] = {0.f, 0.f};
  6498. const int * q8 = (const int *)bq8_1[ib32].qs;
  6499. for (int l = 0; l < 4; ++l) {
  6500. const int * grid = (const int *)(iq1s_grid_gpu + (bq1->qs[4*ib32+l] | (((bq1->qh[2*ib32+l/2] >> 4*(l%2)) & 7) << 8)));
  6501. int grid0 = grid[0] & 0x0f0f0f0f;
  6502. int grid1 = (grid[0] >> 4) & 0x0f0f0f0f;
  6503. sumi[l / 2] = dpct::dp4a(q8[2 * l + 1], grid1,
  6504. dpct::dp4a(q8[2 * l + 0], grid0, sumi[l / 2]));
  6505. const float delta = (bq1->qh[2*ib32+l/2] >> 4*(l%2)) & 0x08 ? -1-IQ1M_DELTA : -1+IQ1M_DELTA;
  6506. const int sumy = dpct::dp4a(q8[2 * l + 1], 0x01010101,
  6507. dpct::dp4a(q8[2 * l + 0], 0x01010101, 0));
  6508. sumf[l/2] += delta*sumy;
  6509. }
  6510. iq1m_scale_t scale;
  6511. const uint16_t * sc = (const uint16_t *)bq1->scales;
  6512. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  6513. const float d = (float)scale.f16 * bq8_1[ib32].ds[0];
  6514. 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));
  6515. #else
  6516. assert(false);
  6517. #endif
  6518. }
  6519. static __dpct_inline__ void get_int_from_table_16(const uint32_t &q4,
  6520. const uint8_t *values,
  6521. int &val1, int &val2) {
  6522. uint32_t aux32; const uint8_t * q8 = (const uint8_t *)&aux32;
  6523. aux32 = q4 & 0x0f0f0f0f;
  6524. uint16_t v1 = values[q8[0]] | (values[q8[1]] << 8);
  6525. uint16_t v2 = values[q8[2]] | (values[q8[3]] << 8);
  6526. val1 = v1 | (v2 << 16);
  6527. aux32 = (q4 >> 4) & 0x0f0f0f0f;
  6528. v1 = values[q8[0]] | (values[q8[1]] << 8);
  6529. v2 = values[q8[2]] | (values[q8[3]] << 8);
  6530. val2 = v1 | (v2 << 16);
  6531. }
  6532. static __dpct_inline__ float
  6533. vec_dot_iq4_nl_q8_1(const void *__restrict__ vbq,
  6534. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6535. const block_iq4_nl * bq = (const block_iq4_nl *) vbq;
  6536. const uint16_t * q4 = (const uint16_t *)bq->qs + 2*iqs;
  6537. const int32_t * q8 = (const int32_t *)bq8_1->qs + iqs;
  6538. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  6539. int v1, v2;
  6540. int sumi1 = 0, sumi2 = 0;
  6541. for (int l = 0; l < VDR_Q4_0_Q8_1_MMVQ; ++l) {
  6542. const uint32_t aux = q4[2*l] | (q4[2*l+1] << 16);
  6543. get_int_from_table_16(aux, values, v1, v2);
  6544. sumi1 = dpct::dp4a(v1, q8[l + 0], sumi1);
  6545. sumi2 = dpct::dp4a(v2, q8[l + 4], sumi2);
  6546. }
  6547. const float d = (float)bq->d * bq8_1->ds[0];
  6548. return d * (sumi1 + sumi2);
  6549. }
  6550. static __dpct_inline__ float
  6551. vec_dot_iq4_xs_q8_1(const void *__restrict__ vbq,
  6552. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6553. #if QK_K == 256
  6554. const block_iq4_xs * bq4 = (const block_iq4_xs *) vbq;
  6555. const uint8_t * values = (const uint8_t *)kvalues_iq4nl;
  6556. // iqs is 0...7
  6557. const int ib32 = iqs;
  6558. const int32_t * q8 = (const int *)bq8_1[ib32].qs;
  6559. const uint32_t * q4 = (const uint32_t *)bq4->qs + 4*ib32;
  6560. const int8_t ls = ((bq4->scales_l[ib32/2] >> 4*(ib32%2)) & 0xf) | (((bq4->scales_h >> 2*ib32) & 3) << 4);
  6561. const float d = (float)bq4->d * (ls - 32) * bq8_1[ib32].ds[0];
  6562. int v1, v2;
  6563. int sumi1 = 0, sumi2 = 0;
  6564. for (int j = 0; j < 4; ++j) {
  6565. get_int_from_table_16(q4[j], values, v1, v2);
  6566. sumi1 = dpct::dp4a(v1, q8[j + 0], sumi1);
  6567. sumi2 = dpct::dp4a(v2, q8[j + 4], sumi2);
  6568. }
  6569. return d * (sumi1 + sumi2);
  6570. #else
  6571. assert(false);
  6572. #endif
  6573. }
  6574. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x,
  6575. int mmq_y, int nwarps, load_tiles_sycl_t load_tiles, int vdr,
  6576. vec_dot_q_mul_mat_sycl_t vec_dot>
  6577. /*
  6578. DPCT1110:8: The total declared local variable size in device function mul_mat_q
  6579. exceeds 128 bytes and may cause high register pressure. Consult with your
  6580. hardware vendor to find the total register size available and adjust the code,
  6581. or use smaller sub-group size to avoid high register pressure.
  6582. */
  6583. static __dpct_inline__ void
  6584. mul_mat_q(const void *__restrict__ vx, const void *__restrict__ vy,
  6585. float *__restrict__ dst, const int ncols_x, const int nrows_x,
  6586. const int ncols_y, const int nrows_y, const int nrows_dst,
  6587. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_qh,
  6588. int *tile_x_sc, const sycl::nd_item<3> &item_ct1, int *tile_y_qs,
  6589. sycl::half2 *tile_y_ds) {
  6590. const block_q_t * x = (const block_q_t *) vx;
  6591. const block_q8_1 * y = (const block_q8_1 *) vy;
  6592. const int blocks_per_row_x = ncols_x / qk;
  6593. const int blocks_per_col_y = nrows_y / QK8_1;
  6594. const int blocks_per_warp = WARP_SIZE / qi;
  6595. const int & ncols_dst = ncols_y;
  6596. const int row_dst_0 = item_ct1.get_group(2) * mmq_y;
  6597. const int & row_x_0 = row_dst_0;
  6598. const int col_dst_0 = item_ct1.get_group(1) * mmq_x;
  6599. const int & col_y_0 = col_dst_0;
  6600. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  6601. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  6602. load_tiles(x + row_x_0 * blocks_per_row_x + ib0, tile_x_ql, tile_x_dm,
  6603. tile_x_qh, tile_x_sc, item_ct1.get_local_id(1),
  6604. nrows_x - row_x_0 - 1, item_ct1.get_local_id(2),
  6605. blocks_per_row_x);
  6606. #pragma unroll
  6607. for (int ir = 0; ir < qr; ++ir) {
  6608. const int kqs = ir * WARP_SIZE + item_ct1.get_local_id(2);
  6609. const int kbxd = kqs / QI8_1;
  6610. #pragma unroll
  6611. for (int i = 0; i < mmq_x; i += nwarps) {
  6612. const int col_y_eff = dpct::min(
  6613. (unsigned int)(col_y_0 + item_ct1.get_local_id(1) + i),
  6614. ncols_y - 1); // to prevent out-of-bounds memory accesses
  6615. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  6616. const int index_y = (item_ct1.get_local_id(1) + i) * WARP_SIZE +
  6617. kqs % WARP_SIZE;
  6618. tile_y_qs[index_y] = get_int_from_int8_aligned(
  6619. by0->qs, item_ct1.get_local_id(2) % QI8_1);
  6620. }
  6621. #pragma unroll
  6622. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  6623. const int ids =
  6624. (ids0 + item_ct1.get_local_id(1) * QI8_1 +
  6625. item_ct1.get_local_id(2) / (WARP_SIZE / QI8_1)) %
  6626. mmq_x;
  6627. const int kby = item_ct1.get_local_id(2) % (WARP_SIZE / QI8_1);
  6628. const int col_y_eff = sycl::min(col_y_0 + ids, ncols_y - 1);
  6629. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  6630. const sycl::half2 *dsi_src =
  6631. &y[col_y_eff * blocks_per_col_y + ib0 * (qk / QK8_1) +
  6632. ir * (WARP_SIZE / QI8_1) + kby]
  6633. .ds;
  6634. sycl::half2 *dsi_dst =
  6635. &tile_y_ds[ids * (WARP_SIZE / QI8_1) + kby];
  6636. if (need_sum) {
  6637. *dsi_dst = *dsi_src;
  6638. } else {
  6639. float * dfi_dst = (float *) dsi_dst;
  6640. *dfi_dst = (*dsi_src)[0];
  6641. }
  6642. }
  6643. /*
  6644. DPCT1118:9: SYCL group functions and algorithms must be encountered
  6645. in converged control flow. You may need to adjust the code.
  6646. */
  6647. /*
  6648. DPCT1065:56: Consider replacing sycl::nd_item::barrier() with
  6649. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6650. better performance if there is no access to global memory.
  6651. */
  6652. item_ct1.barrier();
  6653. // #pragma unroll // unrolling this loop causes too much register pressure
  6654. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  6655. #pragma unroll
  6656. for (int j = 0; j < mmq_x; j += nwarps) {
  6657. #pragma unroll
  6658. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  6659. sum[i / WARP_SIZE][j / nwarps] += vec_dot(
  6660. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  6661. tile_y_qs, tile_y_ds, item_ct1.get_local_id(2) + i,
  6662. item_ct1.get_local_id(1) + j, k);
  6663. }
  6664. }
  6665. }
  6666. /*
  6667. DPCT1118:10: SYCL group functions and algorithms must be encountered
  6668. in converged control flow. You may need to adjust the code.
  6669. */
  6670. /*
  6671. DPCT1065:57: Consider replacing sycl::nd_item::barrier() with
  6672. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6673. better performance if there is no access to global memory.
  6674. */
  6675. item_ct1.barrier();
  6676. }
  6677. }
  6678. #pragma unroll
  6679. for (int j = 0; j < mmq_x; j += nwarps) {
  6680. const int col_dst = col_dst_0 + j + item_ct1.get_local_id(1);
  6681. if (col_dst >= ncols_dst) {
  6682. return;
  6683. }
  6684. #pragma unroll
  6685. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  6686. const int row_dst = row_dst_0 + item_ct1.get_local_id(2) + i;
  6687. if (row_dst >= nrows_dst) {
  6688. continue;
  6689. }
  6690. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  6691. }
  6692. }
  6693. }
  6694. #define MMQ_X_Q4_0_RDNA2 64
  6695. #define MMQ_Y_Q4_0_RDNA2 128
  6696. #define NWARPS_Q4_0_RDNA2 8
  6697. #define MMQ_X_Q4_0_RDNA1 64
  6698. #define MMQ_Y_Q4_0_RDNA1 64
  6699. #define NWARPS_Q4_0_RDNA1 8
  6700. #if defined(SYCL_USE_XMX)
  6701. #define MMQ_X_Q4_0_AMPERE 4
  6702. #define MMQ_Y_Q4_0_AMPERE 32
  6703. #define NWARPS_Q4_0_AMPERE 4
  6704. #else
  6705. #define MMQ_X_Q4_0_AMPERE 64
  6706. #define MMQ_Y_Q4_0_AMPERE 128
  6707. #define NWARPS_Q4_0_AMPERE 4
  6708. #endif
  6709. #define MMQ_X_Q4_0_PASCAL 64
  6710. #define MMQ_Y_Q4_0_PASCAL 64
  6711. #define NWARPS_Q4_0_PASCAL 8
  6712. template <bool need_check> static void
  6713. mul_mat_q4_0(
  6714. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6715. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6716. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_0, float *tile_x_d_q4_0,
  6717. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6718. int * tile_x_ql = nullptr;
  6719. sycl::half2 *tile_x_dm = nullptr;
  6720. int * tile_x_qh = nullptr;
  6721. int * tile_x_sc = nullptr;
  6722. //sycl_todo: change according to hardware
  6723. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  6724. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  6725. const int nwarps = NWARPS_Q4_0_AMPERE;
  6726. allocate_tiles_q4_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6727. tile_x_qs_q4_0, tile_x_d_q4_0);
  6728. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps,
  6729. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ,
  6730. vec_dot_q4_0_q8_1_mul_mat>(
  6731. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6732. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6733. }
  6734. #define MMQ_X_Q4_1_RDNA2 64
  6735. #define MMQ_Y_Q4_1_RDNA2 128
  6736. #define NWARPS_Q4_1_RDNA2 8
  6737. #define MMQ_X_Q4_1_RDNA1 64
  6738. #define MMQ_Y_Q4_1_RDNA1 64
  6739. #define NWARPS_Q4_1_RDNA1 8
  6740. #if defined(SYCL_USE_XMX)
  6741. #define MMQ_X_Q4_1_AMPERE 4
  6742. #define MMQ_Y_Q4_1_AMPERE 32
  6743. #define NWARPS_Q4_1_AMPERE 4
  6744. #else
  6745. #define MMQ_X_Q4_1_AMPERE 64
  6746. #define MMQ_Y_Q4_1_AMPERE 128
  6747. #define NWARPS_Q4_1_AMPERE 4
  6748. #endif
  6749. #define MMQ_X_Q4_1_PASCAL 64
  6750. #define MMQ_Y_Q4_1_PASCAL 64
  6751. #define NWARPS_Q4_1_PASCAL 8
  6752. template <bool need_check> static void
  6753. mul_mat_q4_1(
  6754. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6755. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6756. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_1,
  6757. sycl::half2 *tile_x_dm_q4_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6758. int * tile_x_ql = nullptr;
  6759. sycl::half2 *tile_x_dm = nullptr;
  6760. int * tile_x_qh = nullptr;
  6761. int * tile_x_sc = nullptr;
  6762. //sycl_todo: change according to hardware
  6763. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  6764. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  6765. const int nwarps = NWARPS_Q4_1_AMPERE;
  6766. allocate_tiles_q4_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6767. tile_x_qs_q4_1, tile_x_dm_q4_1);
  6768. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps,
  6769. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ,
  6770. vec_dot_q4_1_q8_1_mul_mat>(
  6771. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6772. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6773. }
  6774. #define MMQ_X_Q5_0_RDNA2 64
  6775. #define MMQ_Y_Q5_0_RDNA2 128
  6776. #define NWARPS_Q5_0_RDNA2 8
  6777. #define MMQ_X_Q5_0_RDNA1 64
  6778. #define MMQ_Y_Q5_0_RDNA1 64
  6779. #define NWARPS_Q5_0_RDNA1 8
  6780. #if defined(SYCL_USE_XMX)
  6781. #define MMQ_X_Q5_0_AMPERE 4
  6782. #define MMQ_Y_Q5_0_AMPERE 32
  6783. #define NWARPS_Q5_0_AMPERE 4
  6784. #else
  6785. #define MMQ_X_Q5_0_AMPERE 128
  6786. #define MMQ_Y_Q5_0_AMPERE 64
  6787. #define NWARPS_Q5_0_AMPERE 4
  6788. #endif
  6789. #define MMQ_X_Q5_0_PASCAL 64
  6790. #define MMQ_Y_Q5_0_PASCAL 64
  6791. #define NWARPS_Q5_0_PASCAL 8
  6792. template <bool need_check> static void
  6793. mul_mat_q5_0(
  6794. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6795. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6796. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_0, float *tile_x_d_q5_0,
  6797. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6798. int * tile_x_ql = nullptr;
  6799. sycl::half2 *tile_x_dm = nullptr;
  6800. int * tile_x_qh = nullptr;
  6801. int * tile_x_sc = nullptr;
  6802. //sycl_todo: change according to hardware
  6803. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  6804. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  6805. const int nwarps = NWARPS_Q5_0_AMPERE;
  6806. allocate_tiles_q5_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6807. tile_x_ql_q5_0, tile_x_d_q5_0);
  6808. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps,
  6809. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ,
  6810. vec_dot_q5_0_q8_1_mul_mat>(
  6811. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6812. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6813. }
  6814. #define MMQ_X_Q5_1_RDNA2 64
  6815. #define MMQ_Y_Q5_1_RDNA2 128
  6816. #define NWARPS_Q5_1_RDNA2 8
  6817. #define MMQ_X_Q5_1_RDNA1 64
  6818. #define MMQ_Y_Q5_1_RDNA1 64
  6819. #define NWARPS_Q5_1_RDNA1 8
  6820. #if defined(SYCL_USE_XMX)
  6821. #define MMQ_X_Q5_1_AMPERE 4
  6822. #define MMQ_Y_Q5_1_AMPERE 32
  6823. #define NWARPS_Q5_1_AMPERE 4
  6824. #else
  6825. #define MMQ_X_Q5_1_AMPERE 128
  6826. #define MMQ_Y_Q5_1_AMPERE 64
  6827. #define NWARPS_Q5_1_AMPERE 4
  6828. #endif
  6829. #define MMQ_X_Q5_1_PASCAL 64
  6830. #define MMQ_Y_Q5_1_PASCAL 64
  6831. #define NWARPS_Q5_1_PASCAL 8
  6832. template <bool need_check> static void
  6833. mul_mat_q5_1(
  6834. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6835. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6836. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_1,
  6837. sycl::half2 *tile_x_dm_q5_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6838. int * tile_x_ql = nullptr;
  6839. sycl::half2 *tile_x_dm = nullptr;
  6840. int * tile_x_qh = nullptr;
  6841. int * tile_x_sc = nullptr;
  6842. //sycl_todo: change according to hardware
  6843. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  6844. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  6845. const int nwarps = NWARPS_Q5_1_AMPERE;
  6846. allocate_tiles_q5_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6847. tile_x_ql_q5_1, tile_x_dm_q5_1);
  6848. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps,
  6849. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ,
  6850. vec_dot_q5_1_q8_1_mul_mat>(
  6851. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6852. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6853. }
  6854. #define MMQ_X_Q8_0_RDNA2 64
  6855. #define MMQ_Y_Q8_0_RDNA2 128
  6856. #define NWARPS_Q8_0_RDNA2 8
  6857. #define MMQ_X_Q8_0_RDNA1 64
  6858. #define MMQ_Y_Q8_0_RDNA1 64
  6859. #define NWARPS_Q8_0_RDNA1 8
  6860. #if defined(SYCL_USE_XMX)
  6861. #define MMQ_X_Q8_0_AMPERE 4
  6862. #define MMQ_Y_Q8_0_AMPERE 32
  6863. #define NWARPS_Q8_0_AMPERE 4
  6864. #else
  6865. #define MMQ_X_Q8_0_AMPERE 128
  6866. #define MMQ_Y_Q8_0_AMPERE 64
  6867. #define NWARPS_Q8_0_AMPERE 4
  6868. #endif
  6869. #define MMQ_X_Q8_0_PASCAL 64
  6870. #define MMQ_Y_Q8_0_PASCAL 64
  6871. #define NWARPS_Q8_0_PASCAL 8
  6872. template <bool need_check> static void
  6873. mul_mat_q8_0(
  6874. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6875. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6876. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q8_0, float *tile_x_d_q8_0,
  6877. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6878. int * tile_x_ql = nullptr;
  6879. sycl::half2 *tile_x_dm = nullptr;
  6880. int * tile_x_qh = nullptr;
  6881. int * tile_x_sc = nullptr;
  6882. //sycl_todo: change according to hardware
  6883. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  6884. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  6885. const int nwarps = NWARPS_Q8_0_AMPERE;
  6886. allocate_tiles_q8_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6887. tile_x_qs_q8_0, tile_x_d_q8_0);
  6888. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps,
  6889. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ,
  6890. vec_dot_q8_0_q8_1_mul_mat>(
  6891. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6892. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6893. }
  6894. #define MMQ_X_Q2_K_RDNA2 64
  6895. #define MMQ_Y_Q2_K_RDNA2 128
  6896. #define NWARPS_Q2_K_RDNA2 8
  6897. #define MMQ_X_Q2_K_RDNA1 128
  6898. #define MMQ_Y_Q2_K_RDNA1 32
  6899. #define NWARPS_Q2_K_RDNA1 8
  6900. #if defined(SYCL_USE_XMX)
  6901. #define MMQ_X_Q2_K_AMPERE 4
  6902. #define MMQ_Y_Q2_K_AMPERE 32
  6903. #define NWARPS_Q2_K_AMPERE 4
  6904. #else
  6905. #define MMQ_X_Q2_K_AMPERE 64
  6906. #define MMQ_Y_Q2_K_AMPERE 128
  6907. #define NWARPS_Q2_K_AMPERE 4
  6908. #endif
  6909. #define MMQ_X_Q2_K_PASCAL 64
  6910. #define MMQ_Y_Q2_K_PASCAL 64
  6911. #define NWARPS_Q2_K_PASCAL 8
  6912. template <bool need_check> static void
  6913. mul_mat_q2_K(
  6914. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6915. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6916. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q2_K,
  6917. sycl::half2 *tile_x_dm_q2_K, int *tile_x_sc_q2_K, int *tile_y_qs,
  6918. sycl::half2 *tile_y_ds) {
  6919. int * tile_x_ql = nullptr;
  6920. sycl::half2 *tile_x_dm = nullptr;
  6921. int * tile_x_qh = nullptr;
  6922. int * tile_x_sc = nullptr;
  6923. //sycl_todo: change according to hardware
  6924. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  6925. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  6926. const int nwarps = NWARPS_Q2_K_AMPERE;
  6927. allocate_tiles_q2_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6928. tile_x_ql_q2_K, tile_x_dm_q2_K, tile_x_sc_q2_K);
  6929. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps,
  6930. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ,
  6931. vec_dot_q2_K_q8_1_mul_mat>(
  6932. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6933. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6934. }
  6935. #define MMQ_X_Q3_K_RDNA2 128
  6936. #define MMQ_Y_Q3_K_RDNA2 64
  6937. #define NWARPS_Q3_K_RDNA2 8
  6938. #define MMQ_X_Q3_K_RDNA1 32
  6939. #define MMQ_Y_Q3_K_RDNA1 128
  6940. #define NWARPS_Q3_K_RDNA1 8
  6941. #if defined(SYCL_USE_XMX)
  6942. #define MMQ_X_Q3_K_AMPERE 4
  6943. #define MMQ_Y_Q3_K_AMPERE 32
  6944. #define NWARPS_Q3_K_AMPERE 4
  6945. #else
  6946. #define MMQ_X_Q3_K_AMPERE 128
  6947. #define MMQ_Y_Q3_K_AMPERE 128
  6948. #define NWARPS_Q3_K_AMPERE 4
  6949. #endif
  6950. #define MMQ_X_Q3_K_PASCAL 64
  6951. #define MMQ_Y_Q3_K_PASCAL 64
  6952. #define NWARPS_Q3_K_PASCAL 8
  6953. template <bool need_check> static void
  6954. mul_mat_q3_K(
  6955. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6956. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6957. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q3_K,
  6958. sycl::half2 *tile_x_dm_q3_K, int *tile_x_qh_q3_K, int *tile_x_sc_q3_K,
  6959. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6960. int * tile_x_ql = nullptr;
  6961. sycl::half2 *tile_x_dm = nullptr;
  6962. int * tile_x_qh = nullptr;
  6963. int * tile_x_sc = nullptr;
  6964. //sycl_todo: change according to hardware
  6965. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  6966. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  6967. const int nwarps = NWARPS_Q3_K_AMPERE;
  6968. allocate_tiles_q3_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6969. tile_x_ql_q3_K, tile_x_dm_q3_K, tile_x_qh_q3_K,
  6970. tile_x_sc_q3_K);
  6971. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps,
  6972. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ,
  6973. vec_dot_q3_K_q8_1_mul_mat>(
  6974. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6975. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6976. }
  6977. #define MMQ_X_Q4_K_RDNA2 64
  6978. #define MMQ_Y_Q4_K_RDNA2 128
  6979. #define NWARPS_Q4_K_RDNA2 8
  6980. #define MMQ_X_Q4_K_RDNA1 32
  6981. #define MMQ_Y_Q4_K_RDNA1 64
  6982. #define NWARPS_Q4_K_RDNA1 8
  6983. #if defined(SYCL_USE_XMX)
  6984. #define MMQ_X_Q4_K_AMPERE 4
  6985. #define MMQ_Y_Q4_K_AMPERE 32
  6986. #define NWARPS_Q4_K_AMPERE 4
  6987. #else
  6988. #define MMQ_X_Q4_K_AMPERE 64
  6989. #define MMQ_Y_Q4_K_AMPERE 128
  6990. #define NWARPS_Q4_K_AMPERE 4
  6991. #endif
  6992. #define MMQ_X_Q4_K_PASCAL 64
  6993. #define MMQ_Y_Q4_K_PASCAL 64
  6994. #define NWARPS_Q4_K_PASCAL 8
  6995. template <bool need_check> static void
  6996. mul_mat_q4_K(
  6997. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6998. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6999. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q4_K,
  7000. sycl::half2 *tile_x_dm_q4_K, int *tile_x_sc_q4_K, int *tile_y_qs,
  7001. sycl::half2 *tile_y_ds) {
  7002. int * tile_x_ql = nullptr;
  7003. sycl::half2 *tile_x_dm = nullptr;
  7004. int * tile_x_qh = nullptr;
  7005. int * tile_x_sc = nullptr;
  7006. //sycl_todo: change according to hardware
  7007. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  7008. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  7009. const int nwarps = NWARPS_Q4_K_AMPERE;
  7010. allocate_tiles_q4_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7011. tile_x_ql_q4_K, tile_x_dm_q4_K, tile_x_sc_q4_K);
  7012. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps,
  7013. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ,
  7014. vec_dot_q4_K_q8_1_mul_mat>(
  7015. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7016. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7017. }
  7018. #define MMQ_X_Q5_K_RDNA2 64
  7019. #define MMQ_Y_Q5_K_RDNA2 128
  7020. #define NWARPS_Q5_K_RDNA2 8
  7021. #define MMQ_X_Q5_K_RDNA1 32
  7022. #define MMQ_Y_Q5_K_RDNA1 64
  7023. #define NWARPS_Q5_K_RDNA1 8
  7024. #if defined(SYCL_USE_XMX)
  7025. #define MMQ_X_Q5_K_AMPERE 4
  7026. #define MMQ_Y_Q5_K_AMPERE 32
  7027. #define NWARPS_Q5_K_AMPERE 4
  7028. #else
  7029. #define MMQ_X_Q5_K_AMPERE 64
  7030. #define MMQ_Y_Q5_K_AMPERE 128
  7031. #define NWARPS_Q5_K_AMPERE 4
  7032. #endif
  7033. #define MMQ_X_Q5_K_PASCAL 64
  7034. #define MMQ_Y_Q5_K_PASCAL 64
  7035. #define NWARPS_Q5_K_PASCAL 8
  7036. template <bool need_check> static void
  7037. mul_mat_q5_K(
  7038. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7039. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7040. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_K,
  7041. sycl::half2 *tile_x_dm_q5_K, int *tile_x_sc_q5_K, int *tile_y_qs,
  7042. sycl::half2 *tile_y_ds) {
  7043. int * tile_x_ql = nullptr;
  7044. sycl::half2 *tile_x_dm = nullptr;
  7045. int * tile_x_qh = nullptr;
  7046. int * tile_x_sc = nullptr;
  7047. //sycl_todo: change according to hardware
  7048. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  7049. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  7050. const int nwarps = NWARPS_Q5_K_AMPERE;
  7051. allocate_tiles_q5_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7052. tile_x_ql_q5_K, tile_x_dm_q5_K, tile_x_sc_q5_K);
  7053. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps,
  7054. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ,
  7055. vec_dot_q5_K_q8_1_mul_mat>(
  7056. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7057. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7058. }
  7059. #define MMQ_X_Q6_K_RDNA2 64
  7060. #define MMQ_Y_Q6_K_RDNA2 128
  7061. #define NWARPS_Q6_K_RDNA2 8
  7062. #define MMQ_X_Q6_K_RDNA1 32
  7063. #define MMQ_Y_Q6_K_RDNA1 64
  7064. #define NWARPS_Q6_K_RDNA1 8
  7065. #if defined(SYCL_USE_XMX)
  7066. #define MMQ_X_Q6_K_AMPERE 4
  7067. #define MMQ_Y_Q6_K_AMPERE 32
  7068. #define NWARPS_Q6_K_AMPERE 4
  7069. #else
  7070. #define MMQ_X_Q6_K_AMPERE 64
  7071. #define MMQ_Y_Q6_K_AMPERE 64
  7072. #define NWARPS_Q6_K_AMPERE 4
  7073. #endif
  7074. #define MMQ_X_Q6_K_PASCAL 64
  7075. #define MMQ_Y_Q6_K_PASCAL 64
  7076. #define NWARPS_Q6_K_PASCAL 8
  7077. template <bool need_check> static void
  7078. mul_mat_q6_K(
  7079. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7080. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7081. const sycl::nd_item<3> &item_ct1, int *tile_x_ql, sycl::half2 *tile_x_dm,
  7082. int *tile_x_sc, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  7083. // int * tile_x_ql = nullptr;
  7084. // sycl::half2 *tile_x_dm = nullptr;
  7085. int * tile_x_qh = nullptr;
  7086. // int * tile_x_sc = nullptr;
  7087. //sycl_todo: change according to hardware
  7088. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  7089. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  7090. const int nwarps = NWARPS_Q6_K_AMPERE;
  7091. allocate_tiles_q6_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7092. tile_x_ql, tile_x_dm, tile_x_sc);
  7093. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps,
  7094. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ,
  7095. vec_dot_q6_K_q8_1_mul_mat>(
  7096. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7097. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7098. }
  7099. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl>
  7100. static void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  7101. const sycl::nd_item<3> &item_ct1) {
  7102. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7103. item_ct1.get_local_id(1);
  7104. if (row >= nrows) {
  7105. return;
  7106. }
  7107. const int blocks_per_row = ncols / qk;
  7108. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7109. // partial sum for each thread
  7110. float tmp = 0.0f;
  7111. const block_q_t * x = (const block_q_t *) vx;
  7112. const block_q8_1 * y = (const block_q8_1 *) vy;
  7113. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7114. i += blocks_per_warp) {
  7115. const int ibx = row*blocks_per_row + i; // x block index
  7116. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7117. const int iqs =
  7118. vdr *
  7119. (item_ct1.get_local_id(2) %
  7120. (qi / vdr)); // x block quant index when casting the quants to int
  7121. tmp += vec_dot_q_sycl(&x[ibx], &y[iby], iqs);
  7122. }
  7123. // sum up partial sums and write back result
  7124. #pragma unroll
  7125. for (int mask = 16; mask > 0; mask >>= 1) {
  7126. tmp +=
  7127. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7128. }
  7129. if (item_ct1.get_local_id(2) == 0) {
  7130. dst[row] = tmp;
  7131. }
  7132. }
  7133. template <int qk, int qi, typename block_q_t, int vdr>
  7134. static void mul_mat_vec_q_iq2_xxs_q8_1(const void *__restrict__ vx,
  7135. const void *__restrict__ vy,
  7136. float *__restrict__ dst, const int ncols,
  7137. const int nrows,
  7138. const sycl::nd_item<3> &item_ct1) {
  7139. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7140. item_ct1.get_local_id(1);
  7141. if (row >= nrows) {
  7142. return;
  7143. }
  7144. const int blocks_per_row = ncols / qk;
  7145. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7146. // partial sum for each thread
  7147. float tmp = 0.0f;
  7148. const block_q_t * x = (const block_q_t *) vx;
  7149. const block_q8_1 * y = (const block_q8_1 *) vy;
  7150. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7151. i += blocks_per_warp) {
  7152. const int ibx = row*blocks_per_row + i; // x block index
  7153. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7154. const int iqs =
  7155. vdr *
  7156. (item_ct1.get_local_id(2) %
  7157. (qi / vdr)); // x block quant index when casting the quants to int
  7158. tmp += vec_dot_iq2_xxs_q8_1(&x[ibx], &y[iby], iqs, iq2xxs_grid, ksigns_iq2xs, kmask_iq2xs);
  7159. }
  7160. // sum up partial sums and write back result
  7161. #pragma unroll
  7162. for (int mask = 16; mask > 0; mask >>= 1) {
  7163. tmp +=
  7164. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7165. }
  7166. if (item_ct1.get_local_id(2) == 0) {
  7167. dst[row] = tmp;
  7168. }
  7169. }
  7170. template <int qk, int qi, typename block_q_t, int vdr>
  7171. static void mul_mat_vec_q_iq2_xs_q8_1(const void *__restrict__ vx,
  7172. const void *__restrict__ vy,
  7173. float *__restrict__ dst, const int ncols,
  7174. const int nrows,
  7175. const sycl::nd_item<3> &item_ct1) {
  7176. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7177. item_ct1.get_local_id(1);
  7178. if (row >= nrows) {
  7179. return;
  7180. }
  7181. const int blocks_per_row = ncols / qk;
  7182. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7183. // partial sum for each thread
  7184. float tmp = 0.0f;
  7185. const block_q_t * x = (const block_q_t *) vx;
  7186. const block_q8_1 * y = (const block_q8_1 *) vy;
  7187. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7188. i += blocks_per_warp) {
  7189. const int ibx = row*blocks_per_row + i; // x block index
  7190. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7191. const int iqs =
  7192. vdr *
  7193. (item_ct1.get_local_id(2) %
  7194. (qi / vdr)); // x block quant index when casting the quants to int
  7195. tmp += vec_dot_iq2_xs_q8_1(&x[ibx], &y[iby], iqs, iq2xs_grid, ksigns64);
  7196. }
  7197. // sum up partial sums and write back result
  7198. #pragma unroll
  7199. for (int mask = 16; mask > 0; mask >>= 1) {
  7200. tmp +=
  7201. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7202. }
  7203. if (item_ct1.get_local_id(2) == 0) {
  7204. dst[row] = tmp;
  7205. }
  7206. }
  7207. template <int qk, int qi, typename block_q_t, int vdr>
  7208. static void mul_mat_vec_q_iq2_s_q8_1(const void *__restrict__ vx,
  7209. const void *__restrict__ vy,
  7210. float *__restrict__ dst, const int ncols,
  7211. const int nrows,
  7212. const sycl::nd_item<3> &item_ct1) {
  7213. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7214. item_ct1.get_local_id(1);
  7215. if (row >= nrows) {
  7216. return;
  7217. }
  7218. const int blocks_per_row = ncols / qk;
  7219. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7220. // partial sum for each thread
  7221. float tmp = 0.0f;
  7222. const block_q_t * x = (const block_q_t *) vx;
  7223. const block_q8_1 * y = (const block_q8_1 *) vy;
  7224. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7225. i += blocks_per_warp) {
  7226. const int ibx = row*blocks_per_row + i; // x block index
  7227. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7228. const int iqs =
  7229. vdr *
  7230. (item_ct1.get_local_id(2) %
  7231. (qi / vdr)); // x block quant index when casting the quants to int
  7232. tmp += vec_dot_iq2_s_q8_1(&x[ibx], &y[iby], iqs);
  7233. }
  7234. // sum up partial sums and write back result
  7235. #pragma unroll
  7236. for (int mask = 16; mask > 0; mask >>= 1) {
  7237. tmp +=
  7238. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7239. }
  7240. if (item_ct1.get_local_id(2) == 0) {
  7241. dst[row] = tmp;
  7242. }
  7243. }
  7244. template <int qk, int qi, typename block_q_t, int vdr>
  7245. static void mul_mat_vec_q_iq3_xxs_q8_1(const void *__restrict__ vx,
  7246. const void *__restrict__ vy,
  7247. float *__restrict__ dst, const int ncols,
  7248. const int nrows,
  7249. const sycl::nd_item<3> &item_ct1) {
  7250. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7251. item_ct1.get_local_id(1);
  7252. if (row >= nrows) {
  7253. return;
  7254. }
  7255. const int blocks_per_row = ncols / qk;
  7256. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7257. // partial sum for each thread
  7258. float tmp = 0.0f;
  7259. const block_q_t * x = (const block_q_t *) vx;
  7260. const block_q8_1 * y = (const block_q8_1 *) vy;
  7261. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7262. i += blocks_per_warp) {
  7263. const int ibx = row*blocks_per_row + i; // x block index
  7264. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7265. const int iqs =
  7266. vdr *
  7267. (item_ct1.get_local_id(2) %
  7268. (qi / vdr)); // x block quant index when casting the quants to int
  7269. tmp += vec_dot_iq3_xxs_q8_1(&x[ibx], &y[iby], iqs, iq3xxs_grid, ksigns64);
  7270. }
  7271. // sum up partial sums and write back result
  7272. #pragma unroll
  7273. for (int mask = 16; mask > 0; mask >>= 1) {
  7274. tmp +=
  7275. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7276. }
  7277. if (item_ct1.get_local_id(2) == 0) {
  7278. dst[row] = tmp;
  7279. }
  7280. }
  7281. template <int qk, int qi, typename block_q_t, int vdr>
  7282. static void mul_mat_vec_q_iq3_s_q8_1(const void *__restrict__ vx,
  7283. const void *__restrict__ vy,
  7284. float *__restrict__ dst, const int ncols,
  7285. const int nrows,
  7286. const sycl::nd_item<3> &item_ct1) {
  7287. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7288. item_ct1.get_local_id(1);
  7289. if (row >= nrows) {
  7290. return;
  7291. }
  7292. const int blocks_per_row = ncols / qk;
  7293. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7294. // partial sum for each thread
  7295. float tmp = 0.0f;
  7296. const block_q_t * x = (const block_q_t *) vx;
  7297. const block_q8_1 * y = (const block_q8_1 *) vy;
  7298. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7299. i += blocks_per_warp) {
  7300. const int ibx = row*blocks_per_row + i; // x block index
  7301. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7302. const int iqs =
  7303. vdr *
  7304. (item_ct1.get_local_id(2) %
  7305. (qi / vdr)); // x block quant index when casting the quants to int
  7306. tmp += vec_dot_iq3_s_q8_1(&x[ibx], &y[iby], iqs, iq3s_grid);
  7307. }
  7308. // sum up partial sums and write back result
  7309. #pragma unroll
  7310. for (int mask = 16; mask > 0; mask >>= 1) {
  7311. tmp +=
  7312. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7313. }
  7314. if (item_ct1.get_local_id(2) == 0) {
  7315. dst[row] = tmp;
  7316. }
  7317. }
  7318. template <int qk, int qi, typename block_q_t, int vdr>
  7319. static void mul_mat_vec_q_iq1_s_q8_1(const void *__restrict__ vx,
  7320. const void *__restrict__ vy,
  7321. float *__restrict__ dst, const int ncols,
  7322. const int nrows,
  7323. const sycl::nd_item<3> &item_ct1) {
  7324. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7325. item_ct1.get_local_id(1);
  7326. if (row >= nrows) {
  7327. return;
  7328. }
  7329. const int blocks_per_row = ncols / qk;
  7330. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7331. // partial sum for each thread
  7332. float tmp = 0.0f;
  7333. const block_q_t * x = (const block_q_t *) vx;
  7334. const block_q8_1 * y = (const block_q8_1 *) vy;
  7335. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7336. i += blocks_per_warp) {
  7337. const int ibx = row*blocks_per_row + i; // x block index
  7338. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7339. const int iqs =
  7340. vdr *
  7341. (item_ct1.get_local_id(2) %
  7342. (qi / vdr)); // x block quant index when casting the quants to int
  7343. tmp += vec_dot_iq1_s_q8_1(&x[ibx], &y[iby], iqs, iq1s_grid_gpu);
  7344. }
  7345. // sum up partial sums and write back result
  7346. #pragma unroll
  7347. for (int mask = 16; mask > 0; mask >>= 1) {
  7348. tmp +=
  7349. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7350. }
  7351. if (item_ct1.get_local_id(2) == 0) {
  7352. dst[row] = tmp;
  7353. }
  7354. }
  7355. template <int qk, int qi, typename block_q_t, int vdr>
  7356. static void mul_mat_vec_q_iq1_m_q8_1(const void *__restrict__ vx,
  7357. const void *__restrict__ vy,
  7358. float *__restrict__ dst, const int ncols,
  7359. const int nrows,
  7360. const sycl::nd_item<3> &item_ct1) {
  7361. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7362. item_ct1.get_local_id(1);
  7363. if (row >= nrows) {
  7364. return;
  7365. }
  7366. const int blocks_per_row = ncols / qk;
  7367. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7368. // partial sum for each thread
  7369. float tmp = 0.0f;
  7370. const block_q_t * x = (const block_q_t *) vx;
  7371. const block_q8_1 * y = (const block_q8_1 *) vy;
  7372. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7373. i += blocks_per_warp) {
  7374. const int ibx = row*blocks_per_row + i; // x block index
  7375. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7376. const int iqs =
  7377. vdr *
  7378. (item_ct1.get_local_id(2) %
  7379. (qi / vdr)); // x block quant index when casting the quants to int
  7380. tmp += vec_dot_iq1_m_q8_1(&x[ibx], &y[iby], iqs);
  7381. }
  7382. // sum up partial sums and write back result
  7383. #pragma unroll
  7384. for (int mask = 16; mask > 0; mask >>= 1) {
  7385. tmp +=
  7386. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7387. }
  7388. if (item_ct1.get_local_id(2) == 0) {
  7389. dst[row] = tmp;
  7390. }
  7391. }
  7392. template <int qk, int qi, typename block_q_t, int vdr>
  7393. static void mul_mat_vec_q_iq4_nl_q8_1(const void *__restrict__ vx,
  7394. const void *__restrict__ vy,
  7395. float *__restrict__ dst, const int ncols,
  7396. const int nrows,
  7397. const sycl::nd_item<3> &item_ct1) {
  7398. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7399. item_ct1.get_local_id(1);
  7400. if (row >= nrows) {
  7401. return;
  7402. }
  7403. const int blocks_per_row = ncols / qk;
  7404. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7405. // partial sum for each thread
  7406. float tmp = 0.0f;
  7407. const block_q_t * x = (const block_q_t *) vx;
  7408. const block_q8_1 * y = (const block_q8_1 *) vy;
  7409. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7410. i += blocks_per_warp) {
  7411. const int ibx = row*blocks_per_row + i; // x block index
  7412. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7413. const int iqs =
  7414. vdr *
  7415. (item_ct1.get_local_id(2) %
  7416. (qi / vdr)); // x block quant index when casting the quants to int
  7417. tmp += vec_dot_iq4_nl_q8_1(&x[ibx], &y[iby], iqs);
  7418. }
  7419. // sum up partial sums and write back result
  7420. #pragma unroll
  7421. for (int mask = 16; mask > 0; mask >>= 1) {
  7422. tmp +=
  7423. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7424. }
  7425. if (item_ct1.get_local_id(2) == 0) {
  7426. dst[row] = tmp;
  7427. }
  7428. }
  7429. template <int qk, int qi, typename block_q_t, int vdr>
  7430. static void mul_mat_vec_q_iq4_xs_q8_1(const void *__restrict__ vx,
  7431. const void *__restrict__ vy,
  7432. float *__restrict__ dst, const int ncols,
  7433. const int nrows,
  7434. const sycl::nd_item<3> &item_ct1) {
  7435. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7436. item_ct1.get_local_id(1);
  7437. if (row >= nrows) {
  7438. return;
  7439. }
  7440. const int blocks_per_row = ncols / qk;
  7441. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7442. // partial sum for each thread
  7443. float tmp = 0.0f;
  7444. const block_q_t * x = (const block_q_t *) vx;
  7445. const block_q8_1 * y = (const block_q8_1 *) vy;
  7446. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7447. i += blocks_per_warp) {
  7448. const int ibx = row*blocks_per_row + i; // x block index
  7449. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7450. const int iqs =
  7451. vdr *
  7452. (item_ct1.get_local_id(2) %
  7453. (qi / vdr)); // x block quant index when casting the quants to int
  7454. tmp += vec_dot_iq4_xs_q8_1(&x[ibx], &y[iby], iqs);
  7455. }
  7456. // sum up partial sums and write back result
  7457. #pragma unroll
  7458. for (int mask = 16; mask > 0; mask >>= 1) {
  7459. tmp +=
  7460. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7461. }
  7462. if (item_ct1.get_local_id(2) == 0) {
  7463. dst[row] = tmp;
  7464. }
  7465. }
  7466. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  7467. static void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows,
  7468. const sycl::nd_item<3> &item_ct1) {
  7469. // qk = quantized weights per x block
  7470. // qr = number of quantized weights per data value in x block
  7471. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7472. item_ct1.get_local_id(1);
  7473. if (row >= nrows) {
  7474. return;
  7475. }
  7476. const int tid = item_ct1.get_local_id(2);
  7477. const int iter_stride = 2*GGML_SYCL_DMMV_X;
  7478. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  7479. const int y_offset = qr == 1 ? 1 : qk/2;
  7480. // partial sum for each thread
  7481. #ifdef GGML_SYCL_F16
  7482. sycl::half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  7483. #else
  7484. float tmp = 0.0f;
  7485. #endif // GGML_SYCL_F16
  7486. for (int i = 0; i < ncols; i += iter_stride) {
  7487. const int col = i + vals_per_iter*tid;
  7488. const int ib = (row*ncols + col)/qk; // x block index
  7489. const int iqs = (col%qk)/qr; // x quant index
  7490. const int iybs = col - col%qk; // y block start index
  7491. // processing >2 values per i iter is faster for fast GPUs
  7492. #pragma unroll
  7493. for (int j = 0; j < vals_per_iter; j += 2) {
  7494. // process 2 vals per j iter
  7495. // dequantize
  7496. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  7497. dfloat2 v;
  7498. dequantize_kernel(vx, ib, iqs + j/qr, v);
  7499. // matrix multiplication
  7500. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  7501. #ifdef GGML_SYCL_F16
  7502. dfloat2 t1{y[iybs + iqs + j / qr + 0],
  7503. y[iybs + iqs + j / qr + y_offset]};
  7504. tmp += v * t1;
  7505. #else
  7506. tmp += v.x() * y[iybs + iqs + j / qr + 0];
  7507. tmp += v.y() * y[iybs + iqs + j / qr + y_offset];
  7508. #endif // GGML_SYCL_F16
  7509. }
  7510. }
  7511. // sum up partial sums and write back result
  7512. #pragma unroll
  7513. for (int mask = 16; mask > 0; mask >>= 1) {
  7514. tmp +=
  7515. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7516. }
  7517. if (tid == 0) {
  7518. #ifdef GGML_SYCL_F16
  7519. dst[row] = tmp.x() + tmp.y();
  7520. #else
  7521. dst[row] = tmp;
  7522. #endif // GGML_SYCL_F16
  7523. }
  7524. }
  7525. static void mul_mat_p021_f16_f32(
  7526. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  7527. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y,
  7528. const sycl::nd_item<3> &item_ct1) {
  7529. const sycl::half *x = (const sycl::half *)vx;
  7530. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7531. item_ct1.get_local_id(1);
  7532. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  7533. item_ct1.get_local_id(0);
  7534. const int channel_x = channel / (nchannels_y / nchannels_x);
  7535. const int nrows_y = ncols_x;
  7536. const int nrows_dst = nrows_x;
  7537. const int row_dst = row_x;
  7538. float tmp = 0.0f;
  7539. for (int col_x0 = 0; col_x0 < ncols_x;
  7540. col_x0 += item_ct1.get_local_range(2)) {
  7541. const int col_x = col_x0 + item_ct1.get_local_id(2);
  7542. if (col_x >= ncols_x) {
  7543. break;
  7544. }
  7545. // x is transposed and permuted
  7546. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  7547. const float xi =
  7548. sycl::vec<sycl::half, 1>(x[ix])
  7549. .convert<float, sycl::rounding_mode::automatic>()[0];
  7550. const int row_y = col_x;
  7551. // y is not transposed but permuted
  7552. const int iy = channel*nrows_y + row_y;
  7553. tmp += xi * y[iy];
  7554. }
  7555. // dst is not transposed and not permuted
  7556. const int idst = channel*nrows_dst + row_dst;
  7557. // sum up partial sums and write back result
  7558. #pragma unroll
  7559. for (int mask = 16; mask > 0; mask >>= 1) {
  7560. tmp +=
  7561. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7562. }
  7563. if (item_ct1.get_local_id(2) == 0) {
  7564. dst[idst] = tmp;
  7565. }
  7566. }
  7567. static void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  7568. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  7569. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor,
  7570. const sycl::nd_item<3> &item_ct1) {
  7571. const sycl::half *x = (const sycl::half *)vx;
  7572. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7573. item_ct1.get_local_id(1);
  7574. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  7575. item_ct1.get_local_id(0);
  7576. const int channel_x = channel / channel_x_divisor;
  7577. const int nrows_y = ncols_x;
  7578. const int nrows_dst = nrows_x;
  7579. const int row_dst = row_x;
  7580. const int idst = channel*nrows_dst + row_dst;
  7581. float tmp = 0.0f;
  7582. for (int col_x0 = 0; col_x0 < ncols_x;
  7583. col_x0 += item_ct1.get_local_range(2)) {
  7584. const int col_x = col_x0 + item_ct1.get_local_id(2);
  7585. if (col_x >= ncols_x) {
  7586. break;
  7587. }
  7588. const int row_y = col_x;
  7589. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  7590. const int iy = channel*nrows_y + row_y;
  7591. const float xi =
  7592. sycl::vec<sycl::half, 1>(x[ix])
  7593. .convert<float, sycl::rounding_mode::automatic>()[0];
  7594. tmp += xi * y[iy];
  7595. }
  7596. // sum up partial sums and write back result
  7597. #pragma unroll
  7598. for (int mask = 16; mask > 0; mask >>= 1) {
  7599. tmp +=
  7600. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7601. }
  7602. if (item_ct1.get_local_id(2) == 0) {
  7603. dst[idst] = tmp;
  7604. }
  7605. }
  7606. static void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  7607. const float * xi = (const float *) cxi;
  7608. float * dsti = (float *) cdsti;
  7609. *dsti = *xi;
  7610. }
  7611. static void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  7612. const float * xi = (const float *) cxi;
  7613. sycl::half *dsti = (sycl::half *)cdsti;
  7614. *dsti = sycl::vec<float, 1>(*xi)
  7615. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7616. }
  7617. static void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  7618. const sycl::half *xi = (const sycl::half *)cxi;
  7619. sycl::half *dsti = (sycl::half *)cdsti;
  7620. *dsti = *xi;
  7621. }
  7622. static void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  7623. const sycl::half *xi = (const sycl::half *)cxi;
  7624. float * dsti = (float *) cdsti;
  7625. *dsti = *xi;
  7626. }
  7627. static void cpy_1_i16_i16(const char * cxi, char * cdsti) {
  7628. const int16_t *xi = (const int16_t *)cxi;
  7629. int16_t *dsti = (int16_t *)cdsti;
  7630. *dsti = *xi;
  7631. }
  7632. static void cpy_1_i32_i32(const char * cxi, char * cdsti) {
  7633. const int32_t *xi = (const int32_t *)cxi;
  7634. int32_t *dsti = (int32_t *)cdsti;
  7635. *dsti = *xi;
  7636. }
  7637. template <cpy_kernel_t cpy_1>
  7638. static void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  7639. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7640. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  7641. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  7642. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7643. item_ct1.get_local_id(2);
  7644. if (i >= ne) {
  7645. return;
  7646. }
  7647. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  7648. // then combine those indices with the corresponding byte offsets to get the total offsets
  7649. const int i03 = i/(ne00 * ne01 * ne02);
  7650. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  7651. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  7652. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  7653. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  7654. const int i13 = i/(ne10 * ne11 * ne12);
  7655. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  7656. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  7657. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  7658. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  7659. cpy_1(cx + x_offset, cdst + dst_offset);
  7660. }
  7661. static void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  7662. const float * xi = (const float *) cxi;
  7663. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  7664. float amax = 0.0f; // absolute max
  7665. for (int j = 0; j < QK8_0; j++) {
  7666. const float v = xi[j];
  7667. amax = sycl::fmax(amax, sycl::fabs((float)v));
  7668. }
  7669. const float d = amax / ((1 << 7) - 1);
  7670. const float id = d ? 1.0f/d : 0.0f;
  7671. dsti->d = d;
  7672. for (int j = 0; j < QK8_0; ++j) {
  7673. const float x0 = xi[j]*id;
  7674. dsti->qs[j] = sycl::round((float)x0);
  7675. }
  7676. }
  7677. static void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  7678. const float * xi = (const float *) cxi;
  7679. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  7680. float amax = 0.0f;
  7681. float vmax = 0.0f;
  7682. for (int j = 0; j < QK4_0; ++j) {
  7683. const float v = xi[j];
  7684. if (amax < sycl::fabs((float)v)) {
  7685. amax = sycl::fabs((float)v);
  7686. vmax = v;
  7687. }
  7688. }
  7689. const float d = vmax / -8;
  7690. const float id = d ? 1.0f/d : 0.0f;
  7691. dsti->d = d;
  7692. for (int j = 0; j < QK4_0/2; ++j) {
  7693. const float x0 = xi[0 + j]*id;
  7694. const float x1 = xi[QK4_0/2 + j]*id;
  7695. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 8.5f));
  7696. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 8.5f));
  7697. dsti->qs[j] = xi0;
  7698. dsti->qs[j] |= xi1 << 4;
  7699. }
  7700. }
  7701. static void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  7702. const float * xi = (const float *) cxi;
  7703. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  7704. float vmin = FLT_MAX;
  7705. float vmax = -FLT_MAX;
  7706. for (int j = 0; j < QK4_1; ++j) {
  7707. const float v = xi[j];
  7708. if (v < vmin) vmin = v;
  7709. if (v > vmax) vmax = v;
  7710. }
  7711. const float d = (vmax - vmin) / ((1 << 4) - 1);
  7712. const float id = d ? 1.0f/d : 0.0f;
  7713. dsti->dm.x() = d;
  7714. dsti->dm.y() = vmin;
  7715. for (int j = 0; j < QK4_1/2; ++j) {
  7716. const float x0 = (xi[0 + j] - vmin)*id;
  7717. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  7718. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 0.5f));
  7719. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 0.5f));
  7720. dsti->qs[j] = xi0;
  7721. dsti->qs[j] |= xi1 << 4;
  7722. }
  7723. }
  7724. template <cpy_kernel_t cpy_blck, int qk>
  7725. static void cpy_f32_q(const char * cx, char * cdst, const int ne,
  7726. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7727. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  7728. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  7729. const int i = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7730. item_ct1.get_local_id(2)) *
  7731. qk;
  7732. if (i >= ne) {
  7733. return;
  7734. }
  7735. const int i03 = i/(ne00 * ne01 * ne02);
  7736. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  7737. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  7738. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  7739. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  7740. const int i13 = i/(ne10 * ne11 * ne12);
  7741. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  7742. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  7743. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  7744. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  7745. cpy_blck(cx + x_offset, cdst + dst_offset);
  7746. }
  7747. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  7748. const float y = (i0 / 2 - low) / sycl::max(0.001f, high - low);
  7749. return 1.0f - sycl::min(1.0f, sycl::max(0.0f, y));
  7750. }
  7751. struct rope_corr_dims {
  7752. float v[4];
  7753. };
  7754. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  7755. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  7756. static void rope_yarn(
  7757. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  7758. float * cos_theta, float * sin_theta
  7759. ) {
  7760. // Get n-d rotational scaling corrected for extrapolation
  7761. float theta_interp = freq_scale * theta_extrap;
  7762. float theta = theta_interp;
  7763. if (ext_factor != 0.0f) {
  7764. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  7765. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  7766. // Get n-d magnitude scaling corrected for interpolation
  7767. mscale *= 1.0f + 0.1f * sycl::log(1.0f / freq_scale);
  7768. }
  7769. *cos_theta = sycl::cos(theta) * mscale;
  7770. *sin_theta = sycl::sin(theta) * mscale;
  7771. }
  7772. // rope == RoPE == rotary positional embedding
  7773. template<typename T, bool has_pos>
  7774. static void rope(
  7775. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  7776. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  7777. ,
  7778. const sycl::nd_item<3> &item_ct1) {
  7779. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7780. item_ct1.get_local_id(1));
  7781. if (col >= ncols) {
  7782. return;
  7783. }
  7784. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7785. item_ct1.get_local_id(2);
  7786. const int i = row*ncols + col;
  7787. const int i2 = row/p_delta_rows;
  7788. const int p = has_pos ? pos[i2] : 0;
  7789. const float theta_base = p * dpct::pow(freq_base, -float(col) / ncols);
  7790. float cos_theta, sin_theta;
  7791. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  7792. const float x0 = x[i + 0];
  7793. const float x1 = x[i + 1];
  7794. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7795. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  7796. }
  7797. template<typename T, bool has_pos>
  7798. static void rope_neox(
  7799. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  7800. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  7801. ,
  7802. const sycl::nd_item<3> &item_ct1) {
  7803. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7804. item_ct1.get_local_id(1));
  7805. if (col >= ncols) {
  7806. return;
  7807. }
  7808. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7809. item_ct1.get_local_id(2);
  7810. const int ib = col / n_dims;
  7811. const int ic = col % n_dims;
  7812. if (ib > 0) {
  7813. const int i = row*ncols + ib*n_dims + ic;
  7814. dst[i + 0] = x[i + 0];
  7815. dst[i + 1] = x[i + 1];
  7816. return;
  7817. }
  7818. const int i = row*ncols + ib*n_dims + ic/2;
  7819. const int i2 = row/p_delta_rows;
  7820. float cur_rot = inv_ndims * ic - ib;
  7821. const int p = has_pos ? pos[i2] : 0;
  7822. const float theta_base =
  7823. p * freq_scale * dpct::pow(theta_scale, col / 2.0f);
  7824. float cos_theta, sin_theta;
  7825. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  7826. const float x0 = x[i + 0];
  7827. const float x1 = x[i + n_dims/2];
  7828. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7829. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  7830. }
  7831. static void rope_glm_f32(
  7832. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  7833. int n_ctx
  7834. , const sycl::nd_item<3> &item_ct1) {
  7835. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7836. item_ct1.get_local_id(2);
  7837. const int half_n_dims = ncols/4;
  7838. if (col >= half_n_dims) {
  7839. return;
  7840. }
  7841. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7842. item_ct1.get_local_id(1);
  7843. const int i = row*ncols + col;
  7844. const int i2 = row/p_delta_rows;
  7845. const float col_theta_scale = dpct::pow(freq_base, -2.0f * col / ncols);
  7846. // FIXME: this is likely wrong
  7847. const int p = pos != nullptr ? pos[i2] : 0;
  7848. const float theta = sycl::min(p, n_ctx - 2) * freq_scale * col_theta_scale;
  7849. const float sin_theta = sycl::sin((float)theta);
  7850. const float cos_theta = sycl::cos((float)theta);
  7851. const float x0 = x[i + 0];
  7852. const float x1 = x[i + half_n_dims];
  7853. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7854. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  7855. const float block_theta =
  7856. ((float)sycl::max(p - n_ctx - 2, 0)) * col_theta_scale;
  7857. const float sin_block_theta = sycl::sin((float)block_theta);
  7858. const float cos_block_theta = sycl::cos((float)block_theta);
  7859. const float x2 = x[i + half_n_dims * 2];
  7860. const float x3 = x[i + half_n_dims * 3];
  7861. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  7862. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  7863. }
  7864. static void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  7865. const int n_heads_log2_floor, const float m0, const float m1,
  7866. const sycl::nd_item<3> &item_ct1) {
  7867. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7868. item_ct1.get_local_id(2);
  7869. if (col >= ncols) {
  7870. return;
  7871. }
  7872. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7873. item_ct1.get_local_id(1);
  7874. const int i = row*ncols + col;
  7875. const int k = row/k_rows;
  7876. float m_k;
  7877. if (k < n_heads_log2_floor) {
  7878. m_k = dpct::pow(m0, k + 1);
  7879. } else {
  7880. m_k = dpct::pow(m1, 2 * (k - n_heads_log2_floor) + 1);
  7881. }
  7882. dst[i] = col * m_k + x[i];
  7883. }
  7884. static void k_sum_rows_f32(const float * x, float * dst, const int ncols,
  7885. const sycl::nd_item<3> &item_ct1) {
  7886. const int row = item_ct1.get_group(1);
  7887. const int col = item_ct1.get_local_id(2);
  7888. float sum = 0.0f;
  7889. for (int i = col; i < ncols; i += item_ct1.get_local_range(2)) {
  7890. sum += x[row * ncols + i];
  7891. }
  7892. sum = warp_reduce_sum(sum, item_ct1);
  7893. if (col == 0) {
  7894. dst[row] = sum;
  7895. }
  7896. }
  7897. template<typename T>
  7898. static inline void ggml_sycl_swap(T & a, T & b) {
  7899. T tmp = a;
  7900. a = b;
  7901. b = tmp;
  7902. }
  7903. template <ggml_sort_order order>
  7904. __dpct_inline__ static void
  7905. k_argsort_f32_i32(const float *x, int *dst, const int ncols, int ncols_pad,
  7906. const sycl::nd_item<3> &item_ct1, uint8_t *dpct_local) {
  7907. // bitonic sort
  7908. int col = item_ct1.get_local_id(2);
  7909. int row = item_ct1.get_group(1);
  7910. if (col >= ncols_pad) {
  7911. return;
  7912. }
  7913. const float * x_row = x + row * ncols;
  7914. auto dst_row = (int *)dpct_local;
  7915. // initialize indices
  7916. dst_row[col] = col;
  7917. item_ct1.barrier(sycl::access::fence_space::local_space);
  7918. for (int k = 2; k <= ncols_pad; k *= 2) {
  7919. for (int j = k / 2; j > 0; j /= 2) {
  7920. int ixj = col ^ j;
  7921. if (ixj > col) {
  7922. if ((col & k) == 0) {
  7923. if (dst_row[col] >= ncols ||
  7924. (dst_row[ixj] < ncols && (order == GGML_SORT_ORDER_ASC ?
  7925. x_row[dst_row[col]] > x_row[dst_row[ixj]] :
  7926. x_row[dst_row[col]] < x_row[dst_row[ixj]]))
  7927. ) {
  7928. ggml_sycl_swap(dst_row[col], dst_row[ixj]);
  7929. }
  7930. } else {
  7931. if (dst_row[ixj] >= ncols ||
  7932. (dst_row[col] < ncols && (order == GGML_SORT_ORDER_ASC ?
  7933. x_row[dst_row[col]] < x_row[dst_row[ixj]] :
  7934. x_row[dst_row[col]] > x_row[dst_row[ixj]]))
  7935. ) {
  7936. ggml_sycl_swap(dst_row[col], dst_row[ixj]);
  7937. }
  7938. }
  7939. }
  7940. /*
  7941. DPCT1118:1: SYCL group functions and algorithms must be encountered
  7942. in converged control flow. You may need to adjust the code.
  7943. */
  7944. item_ct1.barrier(sycl::access::fence_space::local_space);
  7945. }
  7946. }
  7947. // copy the result to dst without the padding
  7948. if (col < ncols) {
  7949. dst[row * ncols + col] = dst_row[col];
  7950. }
  7951. }
  7952. static void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past,
  7953. const sycl::nd_item<3> &item_ct1) {
  7954. const int col = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7955. item_ct1.get_local_id(1);
  7956. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7957. item_ct1.get_local_id(2);
  7958. if (col >= ncols) {
  7959. return;
  7960. }
  7961. const int i = row*ncols + col;
  7962. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  7963. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  7964. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  7965. }
  7966. template <bool vals_smem, int ncols_template, int block_size_template>
  7967. static void soft_max_f32(const float * x, const float * mask, const float *pos, float * dst, const int ncols_par,
  7968. const int nrows_y, const float scale, const float max_bias, const float m0,
  7969. const float m1, uint32_t n_head_log2, const sycl::nd_item<3> &item_ct1, float *buf) {
  7970. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  7971. const int tid = item_ct1.get_local_id(2);
  7972. const int rowx = item_ct1.get_group(2);
  7973. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  7974. const int block_size = block_size_template == 0 ? item_ct1.get_local_range(2) : block_size_template;
  7975. const int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  7976. const int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  7977. float slope = 0.0f;
  7978. // ALiBi
  7979. if (max_bias > 0.0f) {
  7980. const uint32_t h = rowx/nrows_y; // head index
  7981. const float base = h < n_head_log2 ? m0 : m1;
  7982. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  7983. slope = sycl::pow(base, float(exp));
  7984. }
  7985. float * vals = vals_smem ? buf + WARP_SIZE : dst + rowx*ncols;
  7986. float max_val = -INFINITY;
  7987. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7988. const int col = col0 + tid;
  7989. if (ncols_template == 0 && col >= ncols) {
  7990. break;
  7991. }
  7992. const int ix = rowx*ncols + col;
  7993. const int iy = rowy*ncols + col;
  7994. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
  7995. vals[col] = val;
  7996. max_val = sycl::max(max_val, val);
  7997. }
  7998. // find the max value in the block
  7999. max_val = warp_reduce_max(max_val, item_ct1);
  8000. if (block_size > WARP_SIZE) {
  8001. if (warp_id == 0) {
  8002. buf[lane_id] = -INFINITY;
  8003. }
  8004. item_ct1.barrier(sycl::access::fence_space::local_space);
  8005. if (lane_id == 0) {
  8006. buf[warp_id] = max_val;
  8007. }
  8008. item_ct1.barrier(sycl::access::fence_space::local_space);
  8009. max_val = buf[lane_id];
  8010. max_val = warp_reduce_max(max_val, item_ct1);
  8011. }
  8012. float tmp = 0.f;
  8013. #pragma unroll
  8014. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  8015. const int col = col0 + tid;
  8016. if (ncols_template == 0 && col >= ncols) {
  8017. break;
  8018. }
  8019. const float val = sycl::native::exp(vals[col] - max_val);
  8020. tmp += val;
  8021. vals[col] = val;
  8022. }
  8023. // find the sum of exps in the block
  8024. tmp = warp_reduce_sum(tmp, item_ct1);
  8025. if (block_size > WARP_SIZE) {
  8026. if (warp_id == 0) {
  8027. buf[lane_id] = 0.f;
  8028. }
  8029. item_ct1.barrier(sycl::access::fence_space::local_space);
  8030. if (lane_id == 0) {
  8031. buf[warp_id] = tmp;
  8032. }
  8033. item_ct1.barrier(sycl::access::fence_space::local_space);
  8034. tmp = buf[lane_id];
  8035. tmp = warp_reduce_sum(tmp, item_ct1);
  8036. }
  8037. const float inv_sum = 1.f / tmp;
  8038. #pragma unroll
  8039. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  8040. const int col = col0 + tid;
  8041. if (ncols_template == 0 && col >= ncols) {
  8042. return;
  8043. }
  8044. const int idst = rowx*ncols + col;
  8045. dst[idst] = vals[col] * inv_sum;
  8046. }
  8047. }
  8048. static void scale_f32(const float * x, float * dst, const float scale, const int k,
  8049. const sycl::nd_item<3> &item_ct1) {
  8050. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  8051. item_ct1.get_local_id(2);
  8052. if (i >= k) {
  8053. return;
  8054. }
  8055. dst[i] = scale * x[i];
  8056. }
  8057. static void clamp_f32(const float * x, float * dst, const float min, const float max, const int k,
  8058. const sycl::nd_item<3> &item_ct1) {
  8059. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  8060. item_ct1.get_local_id(2);
  8061. if (i >= k) {
  8062. return;
  8063. }
  8064. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  8065. }
  8066. template <typename T>
  8067. static void im2col_kernel(const float *x, T *dst, int offset_delta,
  8068. int IW, int IH, int OW, int KW, int KH,
  8069. int pelements, int CHW, int s0, int s1, int p0,
  8070. int p1, int d0, int d1,
  8071. const sycl::nd_item<3> &item_ct1) {
  8072. const int i = item_ct1.get_local_id(2) +
  8073. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  8074. if (i >= pelements) {
  8075. return;
  8076. }
  8077. const int ksize = OW * (KH > 1 ? KW : 1);
  8078. const int kx = i / ksize;
  8079. const int kd = kx * ksize;
  8080. const int ky = (i - kd) / OW;
  8081. const int ix = i % OW;
  8082. const int64_t iiw = ix * s0 + kx * d0 - p0;
  8083. const int64_t iih = item_ct1.get_group(1) * s1 + ky * d1 - p1;
  8084. const int64_t offset_dst =
  8085. (item_ct1.get_group(1) * OW + ix) * CHW +
  8086. (item_ct1.get_group(0) * (KW * KH) + ky * KW + kx);
  8087. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  8088. dst[offset_dst] =
  8089. sycl::vec<float, 1>(0.0f)
  8090. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  8091. } else {
  8092. const int64_t offset_src = item_ct1.get_group(0) * offset_delta;
  8093. dst[offset_dst] =
  8094. sycl::vec<float, 1>(x[offset_src + iih * IW + iiw])
  8095. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  8096. }
  8097. }
  8098. template <typename Ti, typename To>
  8099. static void pool2d_nchw_kernel(
  8100. const int ih, const int iw, const int oh, const int ow,
  8101. const int kh, const int kw, const int sh, const int sw,
  8102. const int ph, const int pw, const int parallel_elements,
  8103. const Ti* src, To* dst, const enum ggml_op_pool op,
  8104. const sycl::nd_item<3> &item_ct1) {
  8105. int idx = item_ct1.get_local_id(2) +
  8106. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  8107. if (idx >= parallel_elements) {
  8108. return;
  8109. }
  8110. const int I_HW = ih * iw;
  8111. const int O_HW = oh * ow;
  8112. const int nc = idx / O_HW;
  8113. const int cur_oh = idx % O_HW / ow;
  8114. const int cur_ow = idx % O_HW % ow;
  8115. const Ti* i_ptr = src + nc * I_HW;
  8116. To* o_ptr = dst + nc * O_HW;
  8117. const int start_h = cur_oh * sh - ph;
  8118. const int bh = sycl::max(0, start_h);
  8119. const int eh = sycl::min(ih, start_h + kh);
  8120. const int start_w = cur_ow * sw - pw;
  8121. const int bw = sycl::max(0, start_w);
  8122. const int ew = sycl::min(iw, start_w + kw);
  8123. To res = 0;
  8124. switch (op) {
  8125. case GGML_OP_POOL_AVG: res = 0; break;
  8126. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  8127. }
  8128. for (int i = bh; i < eh; i += 1) {
  8129. for (int j = bw; j < ew; j += 1) {
  8130. #if DPCT_COMPATIBILITY_TEMP >= 350
  8131. /*
  8132. DPCT1098:106: The '*' expression is used instead of the __ldg
  8133. call. These two expressions do not provide the exact same
  8134. functionality. Check the generated code for potential precision
  8135. and/or performance issues.
  8136. */
  8137. Ti cur = *(i_ptr + i * iw + j);
  8138. #else
  8139. Ti cur = i_ptr[i * iw + j];
  8140. #endif
  8141. switch (op) {
  8142. case GGML_OP_POOL_AVG: res += (cur / (kh * kw)); break;
  8143. case GGML_OP_POOL_MAX: res = sycl::max(res, (To)cur); break;
  8144. }
  8145. }
  8146. }
  8147. o_ptr[cur_oh * ow + cur_ow] = res;
  8148. }
  8149. template <int qk, int qr, dequantize_kernel_t dq>
  8150. static void get_rows_sycl(const ggml_tensor *src0, const ggml_tensor *src1,
  8151. ggml_tensor *dst, const void *src0_dd,
  8152. const int32_t *src1_dd, float *dst_dd,
  8153. dpct::queue_ptr stream) {
  8154. GGML_TENSOR_BINARY_OP_LOCALS
  8155. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  8156. const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
  8157. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  8158. // strides in elements
  8159. //const size_t s0 = nb0 / ggml_element_size(dst);
  8160. const size_t s1 = nb1 / ggml_element_size(dst);
  8161. const size_t s2 = nb2 / ggml_element_size(dst);
  8162. const size_t s3 = nb3 / ggml_element_size(dst);
  8163. const size_t s10 = nb10 / ggml_element_size(src1);
  8164. const size_t s11 = nb11 / ggml_element_size(src1);
  8165. const size_t s12 = nb12 / ggml_element_size(src1);
  8166. //const size_t s13 = nb13 / ggml_element_size(src1);
  8167. GGML_ASSERT(ne00 % 2 == 0);
  8168. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8169. [=](sycl::nd_item<3> item_ct1) {
  8170. k_get_rows<qk, qr, dq>(
  8171. src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  8172. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  8173. });
  8174. (void) dst;
  8175. }
  8176. template <typename src0_t>
  8177. static void get_rows_sycl_float(const ggml_tensor *src0,
  8178. const ggml_tensor *src1, ggml_tensor *dst,
  8179. const src0_t *src0_dd, const int32_t *src1_dd,
  8180. float *dst_dd, dpct::queue_ptr stream) {
  8181. GGML_TENSOR_BINARY_OP_LOCALS
  8182. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  8183. const int block_num_x = (ne00 + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
  8184. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  8185. // strides in elements
  8186. //const size_t s0 = nb0 / ggml_element_size(dst);
  8187. const size_t s1 = nb1 / ggml_element_size(dst);
  8188. const size_t s2 = nb2 / ggml_element_size(dst);
  8189. const size_t s3 = nb3 / ggml_element_size(dst);
  8190. const size_t s10 = nb10 / ggml_element_size(src1);
  8191. const size_t s11 = nb11 / ggml_element_size(src1);
  8192. const size_t s12 = nb12 / ggml_element_size(src1);
  8193. //const size_t s13 = nb13 / ggml_element_size(src1);
  8194. {
  8195. dpct::has_capability_or_fail(stream->get_device(),
  8196. {sycl::aspect::fp16});
  8197. stream->parallel_for(
  8198. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8199. [=](sycl::nd_item<3> item_ct1) {
  8200. k_get_rows_float(src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  8201. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  8202. });
  8203. }
  8204. (void) dst;
  8205. }
  8206. template<float (*bin_op)(const float, const float)>
  8207. struct bin_bcast_sycl {
  8208. template <typename src0_t, typename src1_t, typename dst_t>
  8209. void operator()(const struct ggml_tensor *src0,
  8210. const struct ggml_tensor *src1, struct ggml_tensor *dst,
  8211. const src0_t *src0_dd, const src1_t *src1_dd, dst_t *dst_dd,
  8212. dpct::queue_ptr stream) {
  8213. GGML_TENSOR_BINARY_OP_LOCALS
  8214. int nr0 = ne10/ne0;
  8215. int nr1 = ne11/ne1;
  8216. int nr2 = ne12/ne2;
  8217. int nr3 = ne13/ne3;
  8218. int nr[4] = { nr0, nr1, nr2, nr3 };
  8219. // collapse dimensions until first broadcast dimension
  8220. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  8221. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  8222. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  8223. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  8224. auto collapse = [](int64_t cne[]) {
  8225. cne[0] *= cne[1];
  8226. cne[1] = cne[2];
  8227. cne[2] = cne[3];
  8228. cne[3] = 1;
  8229. };
  8230. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  8231. cnb[1] *= cne[1];
  8232. cnb[2] *= cne[2];
  8233. cnb[3] *= cne[3];
  8234. };
  8235. for (int i = 0; i < 4; i++) {
  8236. if (nr[i] != 1) {
  8237. break;
  8238. }
  8239. if (i > 0) {
  8240. collapse_nb(cnb0, cne0);
  8241. collapse_nb(cnb1, cne1);
  8242. collapse(cne0);
  8243. collapse(cne1);
  8244. }
  8245. }
  8246. {
  8247. int64_t ne0 = cne0[0];
  8248. int64_t ne1 = cne0[1];
  8249. int64_t ne2 = cne0[2];
  8250. int64_t ne3 = cne0[3];
  8251. int64_t ne10 = cne1[0];
  8252. int64_t ne11 = cne1[1];
  8253. int64_t ne12 = cne1[2];
  8254. int64_t ne13 = cne1[3];
  8255. size_t nb0 = cnb0[0];
  8256. size_t nb1 = cnb0[1];
  8257. size_t nb2 = cnb0[2];
  8258. size_t nb3 = cnb0[3];
  8259. size_t nb10 = cnb1[0];
  8260. size_t nb11 = cnb1[1];
  8261. size_t nb12 = cnb1[2];
  8262. size_t nb13 = cnb1[3];
  8263. size_t s0 = nb0 / sizeof(dst_t);
  8264. size_t s1 = nb1 / sizeof(dst_t);
  8265. size_t s2 = nb2 / sizeof(dst_t);
  8266. size_t s3 = nb3 / sizeof(dst_t);
  8267. size_t s10 = nb10 / sizeof(src1_t);
  8268. size_t s11 = nb11 / sizeof(src1_t);
  8269. size_t s12 = nb12 / sizeof(src1_t);
  8270. size_t s13 = nb13 / sizeof(src1_t);
  8271. GGML_ASSERT(s0 == 1);
  8272. GGML_ASSERT(s10 == 1);
  8273. const int block_size = 128;
  8274. int64_t hne0 = std::max(ne0/2LL, 1LL);
  8275. sycl::range<3> block_dims(1, 1, 1);
  8276. block_dims[2] = std::min<unsigned int>(hne0, block_size);
  8277. block_dims[1] = std::min<unsigned int>(
  8278. ne1, block_size / (unsigned int)block_dims[2]);
  8279. block_dims[0] = std::min(
  8280. std::min<unsigned int>(
  8281. ne2 * ne3, block_size / (unsigned int)block_dims[2] /
  8282. (unsigned int)block_dims[1]),
  8283. 64U);
  8284. sycl::range<3> block_nums(
  8285. (ne2 * ne3 + block_dims[0] - 1) / block_dims[0],
  8286. (ne1 + block_dims[1] - 1) / block_dims[1],
  8287. (hne0 + block_dims[2] - 1) / block_dims[2]);
  8288. if (block_nums[0] > 65535) {
  8289. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  8290. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  8291. {
  8292. dpct::has_capability_or_fail(stream->get_device(),
  8293. {sycl::aspect::fp16});
  8294. stream->parallel_for(
  8295. sycl::nd_range<3>(sycl::range<3>(1, 1, block_num) *
  8296. sycl::range<3>(1, 1, block_size),
  8297. sycl::range<3>(1, 1, block_size)),
  8298. [=](sycl::nd_item<3> item_ct1) {
  8299. k_bin_bcast_unravel<bin_op>(
  8300. src0_dd, src1_dd, dst_dd, ne0, ne1, ne2, ne3,
  8301. ne10, ne11, ne12, ne13, s1, s2, s3, s11, s12,
  8302. s13, item_ct1);
  8303. });
  8304. }
  8305. } else {
  8306. /*
  8307. DPCT1049:16: The work-group size passed to the SYCL kernel may
  8308. exceed the limit. To get the device limit, query
  8309. info::device::max_work_group_size. Adjust the work-group size if
  8310. needed.
  8311. */
  8312. dpct::has_capability_or_fail(stream->get_device(),
  8313. {sycl::aspect::fp16});
  8314. stream->parallel_for(
  8315. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8316. [=](sycl::nd_item<3> item_ct1) {
  8317. k_bin_bcast<bin_op>(src0_dd, src1_dd, dst_dd, ne0, ne1,
  8318. ne2, ne3, ne10, ne11, ne12, ne13,
  8319. s1, s2, s3, s11, s12, s13,
  8320. item_ct1);
  8321. });
  8322. }
  8323. }
  8324. }
  8325. };
  8326. static void acc_f32_sycl(const float *x, const float *y, float *dst,
  8327. const int n_elements, const int ne10, const int ne11,
  8328. const int ne12, const int nb1, const int nb2,
  8329. const int offset, dpct::queue_ptr stream) {
  8330. int num_blocks = (n_elements + SYCL_ACC_BLOCK_SIZE - 1) / SYCL_ACC_BLOCK_SIZE;
  8331. stream->parallel_for(
  8332. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8333. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE),
  8334. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE)),
  8335. [=](sycl::nd_item<3> item_ct1) {
  8336. acc_f32(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset,
  8337. item_ct1);
  8338. });
  8339. }
  8340. static void gelu_f32_sycl(const float *x, float *dst, const int k,
  8341. dpct::queue_ptr stream) {
  8342. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  8343. stream->parallel_for(
  8344. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8345. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  8346. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  8347. [=](sycl::nd_item<3> item_ct1) {
  8348. gelu_f32(x, dst, k, item_ct1);
  8349. });
  8350. }
  8351. static void silu_f32_sycl(const float *x, float *dst, const int k,
  8352. dpct::queue_ptr stream) {
  8353. const int num_blocks = (k + SYCL_SILU_BLOCK_SIZE - 1) / SYCL_SILU_BLOCK_SIZE;
  8354. stream->parallel_for(
  8355. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8356. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE),
  8357. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE)),
  8358. [=](sycl::nd_item<3> item_ct1) {
  8359. silu_f32(x, dst, k, item_ct1);
  8360. });
  8361. }
  8362. static void gelu_quick_f32_sycl(const float *x, float *dst, const int k,
  8363. dpct::queue_ptr stream) {
  8364. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  8365. stream->parallel_for(
  8366. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8367. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  8368. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  8369. [=](sycl::nd_item<3> item_ct1) {
  8370. gelu_quick_f32(x, dst, k, item_ct1);
  8371. });
  8372. }
  8373. static void tanh_f32_sycl(const float *x, float *dst, const int k,
  8374. dpct::queue_ptr stream) {
  8375. const int num_blocks = (k + SYCL_TANH_BLOCK_SIZE - 1) / SYCL_TANH_BLOCK_SIZE;
  8376. stream->parallel_for(
  8377. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8378. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE),
  8379. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE)),
  8380. [=](sycl::nd_item<3> item_ct1) {
  8381. tanh_f32(x, dst, k, item_ct1);
  8382. });
  8383. }
  8384. static void relu_f32_sycl(const float *x, float *dst, const int k,
  8385. dpct::queue_ptr stream) {
  8386. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  8387. stream->parallel_for(
  8388. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8389. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  8390. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  8391. [=](sycl::nd_item<3> item_ct1) {
  8392. relu_f32(x, dst, k, item_ct1);
  8393. });
  8394. }
  8395. static void hardsigmoid_f32_sycl(const float *x, float *dst, const int k,
  8396. dpct::queue_ptr stream) {
  8397. const int num_blocks = (k + SYCL_HARDSIGMOID_BLOCK_SIZE - 1) / SYCL_HARDSIGMOID_BLOCK_SIZE;
  8398. stream->parallel_for(
  8399. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8400. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE),
  8401. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE)),
  8402. [=](sycl::nd_item<3> item_ct1) {
  8403. hardsigmoid_f32(x, dst, k, item_ct1);
  8404. });
  8405. }
  8406. static void hardswish_f32_sycl(const float *x, float *dst, const int k,
  8407. dpct::queue_ptr stream) {
  8408. const int num_blocks = (k + SYCL_HARDSWISH_BLOCK_SIZE - 1) / SYCL_HARDSWISH_BLOCK_SIZE;
  8409. stream->parallel_for(
  8410. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8411. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE),
  8412. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE)),
  8413. [=](sycl::nd_item<3> item_ct1) {
  8414. hardswish_f32(x, dst, k, item_ct1);
  8415. });
  8416. }
  8417. static void leaky_relu_f32_sycl(const float *x, float *dst, const int k,
  8418. const float negative_slope,
  8419. dpct::queue_ptr stream) {
  8420. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  8421. stream->parallel_for(
  8422. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8423. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  8424. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  8425. [=](sycl::nd_item<3> item_ct1) {
  8426. leaky_relu_f32(x, dst, k, negative_slope, item_ct1);
  8427. });
  8428. }
  8429. static void sqr_f32_sycl(const float *x, float *dst, const int k,
  8430. dpct::queue_ptr stream) {
  8431. const int num_blocks = (k + SYCL_SQR_BLOCK_SIZE - 1) / SYCL_SQR_BLOCK_SIZE;
  8432. stream->parallel_for(
  8433. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8434. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE),
  8435. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE)),
  8436. [=](sycl::nd_item<3> item_ct1) {
  8437. sqr_f32(x, dst, k, item_ct1);
  8438. });
  8439. }
  8440. static void norm_f32_sycl(const float *x, float *dst, const int ncols,
  8441. const int nrows, const float eps,
  8442. dpct::queue_ptr stream) {
  8443. GGML_ASSERT(ncols % WARP_SIZE == 0);
  8444. if (ncols < 1024) {
  8445. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8446. stream->submit([&](sycl::handler &cgh) {
  8447. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  8448. sycl::range<1>(32), cgh);
  8449. cgh.parallel_for(
  8450. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8451. block_dims),
  8452. [=](sycl::nd_item<3> item_ct1)
  8453. [[intel::reqd_sub_group_size(32)]] {
  8454. norm_f32(x, dst, ncols, eps, item_ct1,
  8455. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8456. });
  8457. });
  8458. } else {
  8459. const int work_group_size = g_work_group_size;
  8460. const sycl::range<3> block_dims(1, 1, work_group_size);
  8461. /*
  8462. DPCT1049:17: The work-group size passed to the SYCL kernel may exceed
  8463. the limit. To get the device limit, query
  8464. info::device::max_work_group_size. Adjust the work-group size if needed.
  8465. */
  8466. stream->submit([&](sycl::handler &cgh) {
  8467. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  8468. sycl::range<1>(32), cgh);
  8469. cgh.parallel_for(
  8470. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8471. block_dims),
  8472. [=](sycl::nd_item<3> item_ct1)
  8473. [[intel::reqd_sub_group_size(32)]] {
  8474. norm_f32(x, dst, ncols, eps, item_ct1,
  8475. s_sum_acc_ct1.get_pointer(), work_group_size);
  8476. });
  8477. });
  8478. }
  8479. }
  8480. static void group_norm_f32_sycl(const float *x, float *dst,
  8481. const int num_groups, const int group_size,
  8482. const int ne_elements, dpct::queue_ptr stream) {
  8483. static const float eps = 1e-6f;
  8484. if (group_size < 1024) {
  8485. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8486. stream->submit([&](sycl::handler &cgh) {
  8487. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8488. cgh);
  8489. const float eps_ct4 = eps;
  8490. cgh.parallel_for(
  8491. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  8492. block_dims),
  8493. [=](sycl::nd_item<3> item_ct1)
  8494. [[intel::reqd_sub_group_size(32)]] {
  8495. group_norm_f32(
  8496. x, dst, group_size, ne_elements, eps_ct4, item_ct1,
  8497. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8498. });
  8499. });
  8500. } else {
  8501. const int work_group_size = g_work_group_size;
  8502. const sycl::range<3> block_dims(1, 1, work_group_size);
  8503. /*
  8504. DPCT1049:18: The work-group size passed to the SYCL kernel may exceed
  8505. the limit. To get the device limit, query
  8506. info::device::max_work_group_size. Adjust the work-group size if needed.
  8507. */
  8508. stream->submit([&](sycl::handler &cgh) {
  8509. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8510. cgh);
  8511. const float eps_ct4 = eps;
  8512. cgh.parallel_for(
  8513. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  8514. block_dims),
  8515. [=](sycl::nd_item<3> item_ct1)
  8516. [[intel::reqd_sub_group_size(32)]] {
  8517. group_norm_f32(x, dst, group_size, ne_elements,
  8518. eps_ct4, item_ct1,
  8519. s_sum_acc_ct1.get_pointer(), work_group_size);
  8520. });
  8521. });
  8522. }
  8523. }
  8524. static void concat_f32_sycl(const float *x, const float *y, float *dst,
  8525. const int ne0, int ne1, int ne2, int ne02,
  8526. dpct::queue_ptr stream) {
  8527. int num_blocks = (ne0 + SYCL_CONCAT_BLOCK_SIZE - 1) / SYCL_CONCAT_BLOCK_SIZE;
  8528. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  8529. stream->parallel_for(
  8530. sycl::nd_range<3>(gridDim *
  8531. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE),
  8532. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE)),
  8533. [=](sycl::nd_item<3> item_ct1) {
  8534. concat_f32(x, y, dst, ne0, ne02, item_ct1);
  8535. });
  8536. }
  8537. static void upscale_f32_sycl(const float *x, float *dst, const int ne00,
  8538. const int ne01, const int ne02,
  8539. const int scale_factor, dpct::queue_ptr stream) {
  8540. int ne0 = (ne00 * scale_factor);
  8541. int num_blocks = (ne0 + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE;
  8542. sycl::range<3> gridDim(ne02, (ne01 * scale_factor), num_blocks);
  8543. stream->parallel_for(
  8544. sycl::nd_range<3>(gridDim *
  8545. sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE),
  8546. sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE)),
  8547. [=](sycl::nd_item<3> item_ct1) {
  8548. upscale_f32(x, dst, ne00, ne00 * ne01, scale_factor, item_ct1);
  8549. });
  8550. }
  8551. static void pad_f32_sycl(const float *x, float *dst, const int ne00,
  8552. const int ne01, const int ne02, const int ne0,
  8553. const int ne1, const int ne2, dpct::queue_ptr stream) {
  8554. int num_blocks = (ne0 + SYCL_PAD_BLOCK_SIZE - 1) / SYCL_PAD_BLOCK_SIZE;
  8555. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  8556. stream->parallel_for(
  8557. sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE),
  8558. sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE)),
  8559. [=](sycl::nd_item<3> item_ct1) {
  8560. pad_f32(x, dst, ne0, ne00, ne01, ne02, item_ct1);
  8561. });
  8562. }
  8563. static void rms_norm_f32_sycl(const float *x, float *dst, const int ncols,
  8564. const int nrows, const float eps,
  8565. dpct::queue_ptr stream) {
  8566. GGML_ASSERT(ncols % WARP_SIZE == 0);
  8567. // printf("%s ncols=%d, nrows=%d, WARP_SIZE=%d\n", __func__, ncols, nrows, WARP_SIZE);
  8568. if (ncols < 1024) {
  8569. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8570. stream->submit([&](sycl::handler &cgh) {
  8571. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8572. cgh);
  8573. cgh.parallel_for(
  8574. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8575. block_dims),
  8576. [=](sycl::nd_item<3> item_ct1)
  8577. [[intel::reqd_sub_group_size(32)]] {
  8578. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  8579. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8580. });
  8581. });
  8582. } else {
  8583. const int work_group_size = g_work_group_size;
  8584. const sycl::range<3> block_dims(1, 1, work_group_size);
  8585. /*
  8586. DPCT1049:19: The work-group size passed to the SYCL kernel may exceed
  8587. the limit. To get the device limit, query
  8588. info::device::max_work_group_size. Adjust the work-group size if needed.
  8589. */
  8590. stream->submit([&](sycl::handler &cgh) {
  8591. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8592. cgh);
  8593. cgh.parallel_for(
  8594. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8595. block_dims),
  8596. [=](sycl::nd_item<3> item_ct1)
  8597. [[intel::reqd_sub_group_size(32)]] {
  8598. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  8599. s_sum_acc_ct1.get_pointer(), work_group_size);
  8600. });
  8601. });
  8602. }
  8603. }
  8604. static void quantize_row_q8_1_sycl(const float *x, void *vy, const int kx,
  8605. const int ky, const int kx_padded,
  8606. dpct::queue_ptr stream) {
  8607. const int block_num_x = (kx_padded + SYCL_QUANTIZE_BLOCK_SIZE - 1) / SYCL_QUANTIZE_BLOCK_SIZE;
  8608. const sycl::range<3> num_blocks(1, ky, block_num_x);
  8609. const sycl::range<3> block_size(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE);
  8610. {
  8611. dpct::has_capability_or_fail(stream->get_device(),
  8612. {sycl::aspect::fp16});
  8613. stream->parallel_for(
  8614. sycl::nd_range<3>(num_blocks * block_size, block_size),
  8615. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8616. quantize_q8_1(x, vy, kx, kx_padded, item_ct1);
  8617. });
  8618. }
  8619. }
  8620. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  8621. static void dequantize_block_sycl(const void *__restrict__ vx,
  8622. dst_t *__restrict__ y, const int k,
  8623. dpct::queue_ptr stream) {
  8624. const int num_blocks = (k + 2*SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / (2*SYCL_DEQUANTIZE_BLOCK_SIZE);
  8625. {
  8626. dpct::has_capability_or_fail(stream->get_device(),
  8627. {sycl::aspect::fp16});
  8628. stream->parallel_for(
  8629. sycl::nd_range<3>(
  8630. sycl::range<3>(1, 1, num_blocks) *
  8631. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  8632. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  8633. [=](sycl::nd_item<3> item_ct1) {
  8634. dequantize_block<qk, qr, dequantize_kernel>(vx, y, k, item_ct1);
  8635. });
  8636. }
  8637. }
  8638. template <typename dst_t>
  8639. static void dequantize_row_q2_K_sycl(const void *vx, dst_t *y, const int k,
  8640. dpct::queue_ptr stream) {
  8641. const int nb = k / QK_K;
  8642. #if QK_K == 256
  8643. {
  8644. dpct::has_capability_or_fail(stream->get_device(),
  8645. {sycl::aspect::fp16});
  8646. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8647. sycl::range<3>(1, 1, 64),
  8648. sycl::range<3>(1, 1, 64)),
  8649. [=](sycl::nd_item<3> item_ct1) {
  8650. dequantize_block_q2_K(vx, y, item_ct1);
  8651. });
  8652. }
  8653. #else
  8654. {
  8655. dpct::has_capability_or_fail(stream->get_device(),
  8656. {sycl::aspect::fp16});
  8657. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8658. sycl::range<3>(1, 1, 32),
  8659. sycl::range<3>(1, 1, 32)),
  8660. [=](sycl::nd_item<3> item_ct1) {
  8661. dequantize_block_q2_K(vx, y, item_ct1);
  8662. });
  8663. }
  8664. #endif
  8665. }
  8666. template <typename dst_t>
  8667. static void dequantize_row_q3_K_sycl(const void *vx, dst_t *y, const int k,
  8668. dpct::queue_ptr stream) {
  8669. const int nb = k / QK_K;
  8670. #if QK_K == 256
  8671. {
  8672. dpct::has_capability_or_fail(stream->get_device(),
  8673. {sycl::aspect::fp16});
  8674. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8675. sycl::range<3>(1, 1, 64),
  8676. sycl::range<3>(1, 1, 64)),
  8677. [=](sycl::nd_item<3> item_ct1) {
  8678. dequantize_block_q3_K(vx, y, item_ct1);
  8679. });
  8680. }
  8681. #else
  8682. {
  8683. dpct::has_capability_or_fail(stream->get_device(),
  8684. {sycl::aspect::fp16});
  8685. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8686. sycl::range<3>(1, 1, 32),
  8687. sycl::range<3>(1, 1, 32)),
  8688. [=](sycl::nd_item<3> item_ct1) {
  8689. dequantize_block_q3_K(vx, y, item_ct1);
  8690. });
  8691. }
  8692. #endif
  8693. }
  8694. template <typename dst_t>
  8695. static void dequantize_row_q4_0_sycl(const void *vx, dst_t *y, const int k,
  8696. dpct::queue_ptr stream) {
  8697. const int nb32 = k / 32;
  8698. const int nb = (k + 255) / 256;
  8699. {
  8700. dpct::has_capability_or_fail(stream->get_device(),
  8701. {sycl::aspect::fp16});
  8702. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8703. sycl::range<3>(1, 1, 32),
  8704. sycl::range<3>(1, 1, 32)),
  8705. [=](sycl::nd_item<3> item_ct1) {
  8706. dequantize_block_q4_0(vx, y, nb32, item_ct1);
  8707. });
  8708. }
  8709. }
  8710. template <typename dst_t>
  8711. static void dequantize_row_q4_1_sycl(const void *vx, dst_t *y, const int k,
  8712. dpct::queue_ptr stream) {
  8713. const int nb32 = k / 32;
  8714. const int nb = (k + 255) / 256;
  8715. {
  8716. dpct::has_capability_or_fail(stream->get_device(),
  8717. {sycl::aspect::fp16});
  8718. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8719. sycl::range<3>(1, 1, 32),
  8720. sycl::range<3>(1, 1, 32)),
  8721. [=](sycl::nd_item<3> item_ct1) {
  8722. dequantize_block_q4_1(vx, y, nb32, item_ct1);
  8723. });
  8724. }
  8725. }
  8726. template <typename dst_t>
  8727. static void dequantize_row_q4_K_sycl(const void *vx, dst_t *y, const int k,
  8728. dpct::queue_ptr stream) {
  8729. const int nb = k / QK_K;
  8730. {
  8731. dpct::has_capability_or_fail(stream->get_device(),
  8732. {sycl::aspect::fp16});
  8733. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8734. sycl::range<3>(1, 1, 32),
  8735. sycl::range<3>(1, 1, 32)),
  8736. [=](sycl::nd_item<3> item_ct1) {
  8737. dequantize_block_q4_K(vx, y, item_ct1);
  8738. });
  8739. }
  8740. }
  8741. template <typename dst_t>
  8742. static void dequantize_row_q5_K_sycl(const void *vx, dst_t *y, const int k,
  8743. dpct::queue_ptr stream) {
  8744. const int nb = k / QK_K;
  8745. #if QK_K == 256
  8746. {
  8747. dpct::has_capability_or_fail(stream->get_device(),
  8748. {sycl::aspect::fp16});
  8749. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8750. sycl::range<3>(1, 1, 64),
  8751. sycl::range<3>(1, 1, 64)),
  8752. [=](sycl::nd_item<3> item_ct1) {
  8753. dequantize_block_q5_K(vx, y, item_ct1);
  8754. });
  8755. }
  8756. #else
  8757. {
  8758. dpct::has_capability_or_fail(stream->get_device(),
  8759. {sycl::aspect::fp16});
  8760. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8761. sycl::range<3>(1, 1, 32),
  8762. sycl::range<3>(1, 1, 32)),
  8763. [=](sycl::nd_item<3> item_ct1) {
  8764. dequantize_block_q5_K(vx, y, item_ct1);
  8765. });
  8766. }
  8767. #endif
  8768. }
  8769. template <typename dst_t>
  8770. static void dequantize_row_q6_K_sycl(const void *vx, dst_t *y, const int k,
  8771. dpct::queue_ptr stream) {
  8772. const int nb = k / QK_K;
  8773. #if QK_K == 256
  8774. {
  8775. dpct::has_capability_or_fail(stream->get_device(),
  8776. {sycl::aspect::fp16});
  8777. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8778. sycl::range<3>(1, 1, 64),
  8779. sycl::range<3>(1, 1, 64)),
  8780. [=](sycl::nd_item<3> item_ct1) {
  8781. dequantize_block_q6_K(vx, y, item_ct1);
  8782. });
  8783. }
  8784. #else
  8785. {
  8786. dpct::has_capability_or_fail(stream->get_device(),
  8787. {sycl::aspect::fp16});
  8788. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8789. sycl::range<3>(1, 1, 32),
  8790. sycl::range<3>(1, 1, 32)),
  8791. [=](sycl::nd_item<3> item_ct1) {
  8792. dequantize_block_q6_K(vx, y, item_ct1);
  8793. });
  8794. }
  8795. #endif
  8796. }
  8797. template <typename dst_t>
  8798. static void dequantize_row_iq1_s_sycl(const void *vx, dst_t *y, const int k,
  8799. dpct::queue_ptr stream) {
  8800. const int nb = k / QK_K;
  8801. {
  8802. dpct::has_capability_or_fail(stream->get_device(),
  8803. {sycl::aspect::fp16});
  8804. stream->submit([&](sycl::handler &cgh) {
  8805. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8806. sycl::range<3>(1, 1, 32),
  8807. sycl::range<3>(1, 1, 32)),
  8808. [=](sycl::nd_item<3> item_ct1) {
  8809. dequantize_block_iq1_s(
  8810. vx, y, item_ct1, iq1s_grid_gpu
  8811. );
  8812. });
  8813. });
  8814. }
  8815. }
  8816. template <typename dst_t>
  8817. static void dequantize_row_iq1_m_sycl(const void *vx, dst_t *y, const int k,
  8818. dpct::queue_ptr stream) {
  8819. const int nb = k / QK_K;
  8820. {
  8821. dpct::has_capability_or_fail(stream->get_device(),
  8822. {sycl::aspect::fp16});
  8823. stream->submit([&](sycl::handler &cgh) {
  8824. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8825. sycl::range<3>(1, 1, 32),
  8826. sycl::range<3>(1, 1, 32)),
  8827. [=](sycl::nd_item<3> item_ct1) {
  8828. dequantize_block_iq1_m(
  8829. vx, y, item_ct1, iq1s_grid_gpu
  8830. );
  8831. });
  8832. });
  8833. }
  8834. }
  8835. template <typename dst_t>
  8836. static void dequantize_row_iq2_xxs_sycl(const void *vx, dst_t *y, const int k,
  8837. dpct::queue_ptr stream) {
  8838. const int nb = k / QK_K;
  8839. {
  8840. dpct::has_capability_or_fail(stream->get_device(),
  8841. {sycl::aspect::fp16});
  8842. stream->submit([&](sycl::handler &cgh) {
  8843. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8844. sycl::range<3>(1, 1, 32),
  8845. sycl::range<3>(1, 1, 32)),
  8846. [=](sycl::nd_item<3> item_ct1) {
  8847. dequantize_block_iq2_xxs(
  8848. vx, y, item_ct1, iq2xxs_grid,
  8849. ksigns_iq2xs, kmask_iq2xs);
  8850. });
  8851. });
  8852. }
  8853. }
  8854. template <typename dst_t>
  8855. static void dequantize_row_iq2_xs_sycl(const void *vx, dst_t *y, const int k,
  8856. dpct::queue_ptr stream) {
  8857. const int nb = k / QK_K;
  8858. {
  8859. dpct::has_capability_or_fail(stream->get_device(),
  8860. {sycl::aspect::fp16});
  8861. stream->submit([&](sycl::handler &cgh) {
  8862. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8863. sycl::range<3>(1, 1, 32),
  8864. sycl::range<3>(1, 1, 32)),
  8865. [=](sycl::nd_item<3> item_ct1) {
  8866. dequantize_block_iq2_xs(
  8867. vx, y, item_ct1, iq2xs_grid,
  8868. ksigns_iq2xs, kmask_iq2xs);
  8869. });
  8870. });
  8871. }
  8872. }
  8873. template <typename dst_t>
  8874. static void dequantize_row_iq2_s_sycl(const void *vx, dst_t *y, const int k,
  8875. dpct::queue_ptr stream) {
  8876. const int nb = k / QK_K;
  8877. {
  8878. dpct::has_capability_or_fail(stream->get_device(),
  8879. {sycl::aspect::fp16});
  8880. stream->submit([&](sycl::handler &cgh) {
  8881. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8882. sycl::range<3>(1, 1, 32),
  8883. sycl::range<3>(1, 1, 32)),
  8884. [=](sycl::nd_item<3> item_ct1) {
  8885. dequantize_block_iq2_s(vx, y, item_ct1);
  8886. });
  8887. });
  8888. }
  8889. }
  8890. template <typename dst_t>
  8891. static void dequantize_row_iq3_xxs_sycl(const void *vx, dst_t *y, const int k,
  8892. dpct::queue_ptr stream) {
  8893. const int nb = k / QK_K;
  8894. {
  8895. dpct::has_capability_or_fail(stream->get_device(),
  8896. {sycl::aspect::fp16});
  8897. stream->submit([&](sycl::handler &cgh) {
  8898. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8899. sycl::range<3>(1, 1, 32),
  8900. sycl::range<3>(1, 1, 32)),
  8901. [=](sycl::nd_item<3> item_ct1) {
  8902. dequantize_block_iq3_xxs(
  8903. vx, y, item_ct1, iq3xxs_grid,
  8904. ksigns_iq2xs, kmask_iq2xs);
  8905. });
  8906. });
  8907. }
  8908. }
  8909. template <typename dst_t>
  8910. static void dequantize_row_iq3_s_sycl(const void *vx, dst_t *y, const int k,
  8911. dpct::queue_ptr stream) {
  8912. const int nb = k / QK_K;
  8913. {
  8914. dpct::has_capability_or_fail(stream->get_device(),
  8915. {sycl::aspect::fp16});
  8916. stream->submit([&](sycl::handler &cgh) {
  8917. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8918. sycl::range<3>(1, 1, 32),
  8919. sycl::range<3>(1, 1, 32)),
  8920. [=](sycl::nd_item<3> item_ct1) {
  8921. dequantize_block_iq3_s(
  8922. vx, y, item_ct1, kmask_iq2xs, iq3s_grid);
  8923. });
  8924. });
  8925. }
  8926. }
  8927. template <typename dst_t>
  8928. static void dequantize_row_iq4_xs_sycl(const void *vx, dst_t *y, const int k,
  8929. dpct::queue_ptr stream) {
  8930. const int nb = (k + QK_K - 1) / QK_K;
  8931. #if QK_K == 64
  8932. dequantize_row_iq4_nl_sycl(vx, y, k, stream);
  8933. #else
  8934. {
  8935. dpct::has_capability_or_fail(stream->get_device(),
  8936. {sycl::aspect::fp16});
  8937. stream->submit([&](sycl::handler &cgh) {
  8938. cgh.parallel_for(
  8939. sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8940. sycl::range<3>(1, 1, 32),
  8941. sycl::range<3>(1, 1, 32)),
  8942. [=](sycl::nd_item<3> item_ct1) {
  8943. dequantize_block_iq4_xs(vx, y, item_ct1);
  8944. });
  8945. });
  8946. }
  8947. #endif
  8948. }
  8949. template <typename dst_t>
  8950. static void dequantize_row_iq4_nl_sycl(const void *vx, dst_t *y, const int k,
  8951. dpct::queue_ptr stream) {
  8952. const int nb = (k + QK_K - 1) / QK_K;
  8953. {
  8954. dpct::has_capability_or_fail(stream->get_device(),
  8955. {sycl::aspect::fp16});
  8956. stream->submit([&](sycl::handler &cgh) {
  8957. cgh.parallel_for(
  8958. sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8959. sycl::range<3>(1, 1, 32),
  8960. sycl::range<3>(1, 1, 32)),
  8961. [=](sycl::nd_item<3> item_ct1) {
  8962. dequantize_block_iq4_nl(vx, y, item_ct1);
  8963. });
  8964. });
  8965. }
  8966. }
  8967. template <typename src_t, typename dst_t>
  8968. static void convert_unary_sycl(const void *__restrict__ vx,
  8969. dst_t *__restrict__ y, const int k,
  8970. dpct::queue_ptr stream) {
  8971. const int num_blocks = (k + SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / SYCL_DEQUANTIZE_BLOCK_SIZE;
  8972. {
  8973. dpct::has_capability_or_fail(stream->get_device(),
  8974. {sycl::aspect::fp16});
  8975. stream->parallel_for(
  8976. sycl::nd_range<3>(
  8977. sycl::range<3>(1, 1, num_blocks) *
  8978. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  8979. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  8980. [=](sycl::nd_item<3> item_ct1) {
  8981. convert_unary<src_t>(vx, y, k, item_ct1);
  8982. });
  8983. }
  8984. }
  8985. static to_fp16_sycl_t ggml_get_to_fp16_sycl(ggml_type type) try {
  8986. int id;
  8987. switch (type) {
  8988. case GGML_TYPE_Q4_0:
  8989. return dequantize_block_sycl<QK4_0, QR4_0, dequantize_q4_0>;
  8990. case GGML_TYPE_Q4_1:
  8991. return dequantize_block_sycl<QK4_1, QR4_1, dequantize_q4_1>;
  8992. case GGML_TYPE_Q5_0:
  8993. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  8994. case GGML_TYPE_Q5_1:
  8995. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  8996. case GGML_TYPE_Q8_0:
  8997. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  8998. case GGML_TYPE_Q2_K:
  8999. return dequantize_row_q2_K_sycl;
  9000. case GGML_TYPE_Q3_K:
  9001. return dequantize_row_q3_K_sycl;
  9002. case GGML_TYPE_Q4_K:
  9003. return dequantize_row_q4_K_sycl;
  9004. case GGML_TYPE_Q5_K:
  9005. return dequantize_row_q5_K_sycl;
  9006. case GGML_TYPE_Q6_K:
  9007. return dequantize_row_q6_K_sycl;
  9008. case GGML_TYPE_IQ1_S:
  9009. return dequantize_row_iq1_s_sycl;
  9010. case GGML_TYPE_IQ1_M:
  9011. return dequantize_row_iq1_m_sycl;
  9012. case GGML_TYPE_IQ2_XXS:
  9013. return dequantize_row_iq2_xxs_sycl;
  9014. case GGML_TYPE_IQ2_XS:
  9015. return dequantize_row_iq2_xs_sycl;
  9016. case GGML_TYPE_IQ2_S:
  9017. return dequantize_row_iq2_s_sycl;
  9018. case GGML_TYPE_IQ3_XXS:
  9019. return dequantize_row_iq3_xxs_sycl;
  9020. case GGML_TYPE_IQ3_S:
  9021. return dequantize_row_iq3_s_sycl;
  9022. case GGML_TYPE_IQ4_XS:
  9023. return dequantize_row_iq4_xs_sycl;
  9024. case GGML_TYPE_IQ4_NL:
  9025. return dequantize_row_iq4_nl_sycl;
  9026. case GGML_TYPE_F32:
  9027. return convert_unary_sycl<float>;
  9028. default:
  9029. return nullptr;
  9030. }
  9031. }
  9032. catch (sycl::exception const &exc) {
  9033. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9034. << ", line:" << __LINE__ << std::endl;
  9035. std::exit(1);
  9036. }
  9037. static to_fp32_sycl_t ggml_get_to_fp32_sycl(ggml_type type) {
  9038. switch (type) {
  9039. case GGML_TYPE_Q4_0:
  9040. return dequantize_row_q4_0_sycl;
  9041. case GGML_TYPE_Q4_1:
  9042. return dequantize_row_q4_1_sycl;
  9043. case GGML_TYPE_Q5_0:
  9044. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  9045. case GGML_TYPE_Q5_1:
  9046. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  9047. case GGML_TYPE_Q8_0:
  9048. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  9049. case GGML_TYPE_Q2_K:
  9050. return dequantize_row_q2_K_sycl;
  9051. case GGML_TYPE_Q3_K:
  9052. return dequantize_row_q3_K_sycl;
  9053. case GGML_TYPE_Q4_K:
  9054. return dequantize_row_q4_K_sycl;
  9055. case GGML_TYPE_Q5_K:
  9056. return dequantize_row_q5_K_sycl;
  9057. case GGML_TYPE_Q6_K:
  9058. return dequantize_row_q6_K_sycl;
  9059. case GGML_TYPE_IQ1_S:
  9060. return dequantize_row_iq1_s_sycl;
  9061. case GGML_TYPE_IQ1_M:
  9062. return dequantize_row_iq1_m_sycl;
  9063. case GGML_TYPE_IQ2_XXS:
  9064. return dequantize_row_iq2_xxs_sycl;
  9065. case GGML_TYPE_IQ2_XS:
  9066. return dequantize_row_iq2_xs_sycl;
  9067. case GGML_TYPE_IQ2_S:
  9068. return dequantize_row_iq2_s_sycl;
  9069. case GGML_TYPE_IQ3_XXS:
  9070. return dequantize_row_iq3_xxs_sycl;
  9071. case GGML_TYPE_IQ3_S:
  9072. return dequantize_row_iq3_s_sycl;
  9073. case GGML_TYPE_IQ4_XS:
  9074. return dequantize_row_iq4_xs_sycl;
  9075. case GGML_TYPE_IQ4_NL:
  9076. return dequantize_row_iq4_nl_sycl;
  9077. case GGML_TYPE_F16:
  9078. return convert_unary_sycl<sycl::half>;
  9079. default:
  9080. return nullptr;
  9081. }
  9082. }
  9083. static void dequantize_mul_mat_vec_q4_0_sycl(const void *vx, const dfloat *y,
  9084. float *dst, const int ncols,
  9085. const int nrows,
  9086. dpct::queue_ptr stream) {
  9087. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9088. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9089. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  9090. const sycl::range<3> block_nums(1, 1, block_num_y);
  9091. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9092. {
  9093. dpct::has_capability_or_fail(stream->get_device(),
  9094. {sycl::aspect::fp16});
  9095. stream->parallel_for(
  9096. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9097. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9098. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>(
  9099. vx, y, dst, ncols, nrows, item_ct1);
  9100. });
  9101. }
  9102. }
  9103. static void dequantize_mul_mat_vec_q4_1_sycl(const void *vx, const dfloat *y,
  9104. float *dst, const int ncols,
  9105. const int nrows,
  9106. dpct::queue_ptr stream) {
  9107. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9108. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9109. const sycl::range<3> block_nums(1, 1, block_num_y);
  9110. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9111. {
  9112. dpct::has_capability_or_fail(stream->get_device(),
  9113. {sycl::aspect::fp16});
  9114. stream->parallel_for(
  9115. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9116. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9117. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>(
  9118. vx, y, dst, ncols, nrows, item_ct1);
  9119. });
  9120. }
  9121. }
  9122. static void dequantize_mul_mat_vec_q5_0_sycl(const void *vx, const dfloat *y,
  9123. float *dst, const int ncols,
  9124. const int nrows,
  9125. dpct::queue_ptr stream) {
  9126. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9127. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9128. const sycl::range<3> block_nums(1, 1, block_num_y);
  9129. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9130. {
  9131. dpct::has_capability_or_fail(stream->get_device(),
  9132. {sycl::aspect::fp16});
  9133. stream->parallel_for(
  9134. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9135. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9136. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>(
  9137. vx, y, dst, ncols, nrows, item_ct1);
  9138. });
  9139. }
  9140. }
  9141. static void dequantize_mul_mat_vec_q5_1_sycl(const void *vx, const dfloat *y,
  9142. float *dst, const int ncols,
  9143. const int nrows,
  9144. dpct::queue_ptr stream) {
  9145. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9146. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9147. const sycl::range<3> block_nums(1, 1, block_num_y);
  9148. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9149. {
  9150. dpct::has_capability_or_fail(stream->get_device(),
  9151. {sycl::aspect::fp16});
  9152. stream->parallel_for(
  9153. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9154. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9155. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>(
  9156. vx, y, dst, ncols, nrows, item_ct1);
  9157. });
  9158. }
  9159. }
  9160. static void dequantize_mul_mat_vec_q8_0_sycl(const void *vx, const dfloat *y,
  9161. float *dst, const int ncols,
  9162. const int nrows,
  9163. dpct::queue_ptr stream) {
  9164. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9165. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9166. const sycl::range<3> block_nums(1, 1, block_num_y);
  9167. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9168. {
  9169. dpct::has_capability_or_fail(stream->get_device(),
  9170. {sycl::aspect::fp16});
  9171. stream->parallel_for(
  9172. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9173. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9174. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>(
  9175. vx, y, dst, ncols, nrows, item_ct1);
  9176. });
  9177. }
  9178. }
  9179. static void dequantize_mul_mat_vec_q2_K_sycl(const void *vx, const float *y,
  9180. float *dst, const int ncols,
  9181. const int nrows,
  9182. dpct::queue_ptr stream) {
  9183. GGML_ASSERT(ncols % QK_K == 0);
  9184. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  9185. const int block_num_y = (nrows + ny - 1) / ny;
  9186. const sycl::range<3> block_nums(1, 1, block_num_y);
  9187. const sycl::range<3> block_dims(1, ny, 32);
  9188. stream->parallel_for(
  9189. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9190. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9191. dequantize_mul_mat_vec_q2_k(vx, y, dst, ncols, nrows, item_ct1);
  9192. });
  9193. }
  9194. static void dequantize_mul_mat_vec_q3_K_sycl(const void *vx, const float *y,
  9195. float *dst, const int ncols,
  9196. const int nrows,
  9197. dpct::queue_ptr stream) {
  9198. GGML_ASSERT(ncols % QK_K == 0);
  9199. const int ny = 2 / K_QUANTS_PER_ITERATION;
  9200. const int block_num_y = (nrows + ny - 1) / ny;
  9201. const sycl::range<3> block_nums(1, 1, block_num_y);
  9202. const sycl::range<3> block_dims(1, ny, 32);
  9203. stream->parallel_for(
  9204. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9205. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9206. dequantize_mul_mat_vec_q3_k(vx, y, dst, ncols, nrows, item_ct1);
  9207. });
  9208. }
  9209. static void dequantize_mul_mat_vec_q4_K_sycl(const void *vx, const float *y,
  9210. float *dst, const int ncols,
  9211. const int nrows,
  9212. dpct::queue_ptr stream) {
  9213. GGML_ASSERT(ncols % QK_K == 0);
  9214. const int ny = 2 / K_QUANTS_PER_ITERATION;
  9215. const int block_num_y = (nrows + ny - 1) / ny;
  9216. const sycl::range<3> block_nums(1, 1, block_num_y);
  9217. const sycl::range<3> block_dims(1, ny, 32);
  9218. stream->parallel_for(
  9219. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9220. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9221. dequantize_mul_mat_vec_q4_k(vx, y, dst, ncols, nrows, item_ct1);
  9222. });
  9223. }
  9224. static void dequantize_mul_mat_vec_q5_K_sycl(const void *vx, const float *y,
  9225. float *dst, const int ncols,
  9226. const int nrows,
  9227. dpct::queue_ptr stream) {
  9228. GGML_ASSERT(ncols % QK_K == 0);
  9229. const sycl::range<3> block_dims(1, 1, 32);
  9230. stream->parallel_for(
  9231. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims, block_dims),
  9232. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9233. dequantize_mul_mat_vec_q5_k(vx, y, dst, ncols, item_ct1);
  9234. });
  9235. }
  9236. static void dequantize_mul_mat_vec_q6_K_sycl(const void *vx, const float *y,
  9237. float *dst, const int ncols,
  9238. const int nrows,
  9239. dpct::queue_ptr stream) {
  9240. GGML_ASSERT(ncols % QK_K == 0);
  9241. const int ny = 2 / K_QUANTS_PER_ITERATION;
  9242. const int block_num_y = (nrows + ny - 1) / ny;
  9243. const sycl::range<3> block_nums(1, 1, block_num_y);
  9244. const sycl::range<3> block_dims(1, ny, 32);
  9245. stream->parallel_for(
  9246. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9247. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9248. dequantize_mul_mat_vec_q6_k(vx, y, dst, ncols, nrows, item_ct1);
  9249. });
  9250. }
  9251. static void convert_mul_mat_vec_f16_sycl(const void *vx, const dfloat *y,
  9252. float *dst, const int ncols,
  9253. const int nrows,
  9254. dpct::queue_ptr stream) {
  9255. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9256. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9257. const sycl::range<3> block_nums(1, 1, block_num_y);
  9258. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9259. {
  9260. dpct::has_capability_or_fail(stream->get_device(),
  9261. {sycl::aspect::fp16});
  9262. stream->parallel_for(
  9263. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9264. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9265. dequantize_mul_mat_vec<1, 1, convert_f16>(vx, y, dst, ncols,
  9266. nrows, item_ct1);
  9267. });
  9268. }
  9269. }
  9270. static void mul_mat_vec_q4_0_q8_1_sycl(const void *vx, const void *vy,
  9271. float *dst, const int ncols,
  9272. const int nrows,
  9273. dpct::queue_ptr stream) {
  9274. GGML_ASSERT(ncols % QK4_0 == 0);
  9275. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9276. const sycl::range<3> block_nums(1, 1, block_num_y);
  9277. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9278. {
  9279. stream->submit([&](sycl::handler &cgh) {
  9280. cgh.parallel_for(
  9281. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9282. [=](sycl::nd_item<3> item_ct1)
  9283. [[intel::reqd_sub_group_size(32)]] {
  9284. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0,
  9285. VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>(
  9286. vx, vy, dst, ncols, nrows, item_ct1);
  9287. });
  9288. });
  9289. }
  9290. }
  9291. static void mul_mat_vec_q4_1_q8_1_sycl(const void *vx, const void *vy,
  9292. float *dst, const int ncols,
  9293. const int nrows,
  9294. dpct::queue_ptr stream) {
  9295. GGML_ASSERT(ncols % QK4_1 == 0);
  9296. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9297. const sycl::range<3> block_nums(1, 1, block_num_y);
  9298. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9299. {
  9300. stream->submit([&](sycl::handler &cgh) {
  9301. cgh.parallel_for(
  9302. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9303. [=](sycl::nd_item<3> item_ct1)
  9304. [[intel::reqd_sub_group_size(32)]] {
  9305. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1,
  9306. VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>(
  9307. vx, vy, dst, ncols, nrows, item_ct1);
  9308. });
  9309. });
  9310. }
  9311. }
  9312. static void mul_mat_vec_q5_0_q8_1_sycl(const void *vx, const void *vy,
  9313. float *dst, const int ncols,
  9314. const int nrows,
  9315. dpct::queue_ptr stream) {
  9316. GGML_ASSERT(ncols % QK5_0 == 0);
  9317. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9318. const sycl::range<3> block_nums(1, 1, block_num_y);
  9319. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9320. {
  9321. stream->submit([&](sycl::handler &cgh) {
  9322. cgh.parallel_for(
  9323. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9324. [=](sycl::nd_item<3> item_ct1)
  9325. [[intel::reqd_sub_group_size(32)]] {
  9326. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0,
  9327. VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>(
  9328. vx, vy, dst, ncols, nrows, item_ct1);
  9329. });
  9330. });
  9331. }
  9332. }
  9333. static void mul_mat_vec_q5_1_q8_1_sycl(const void *vx, const void *vy,
  9334. float *dst, const int ncols,
  9335. const int nrows,
  9336. dpct::queue_ptr stream) {
  9337. GGML_ASSERT(ncols % QK5_1 == 0);
  9338. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9339. const sycl::range<3> block_nums(1, 1, block_num_y);
  9340. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9341. {
  9342. stream->submit([&](sycl::handler &cgh) {
  9343. cgh.parallel_for(
  9344. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9345. [=](sycl::nd_item<3> item_ct1)
  9346. [[intel::reqd_sub_group_size(32)]] {
  9347. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1,
  9348. VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>(
  9349. vx, vy, dst, ncols, nrows, item_ct1);
  9350. });
  9351. });
  9352. }
  9353. }
  9354. static void mul_mat_vec_q8_0_q8_1_sycl(const void *vx, const void *vy,
  9355. float *dst, const int ncols,
  9356. const int nrows,
  9357. dpct::queue_ptr stream) {
  9358. GGML_ASSERT(ncols % QK8_0 == 0);
  9359. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9360. const sycl::range<3> block_nums(1, 1, block_num_y);
  9361. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9362. {
  9363. stream->submit([&](sycl::handler &cgh) {
  9364. cgh.parallel_for(
  9365. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9366. [=](sycl::nd_item<3> item_ct1)
  9367. [[intel::reqd_sub_group_size(32)]] {
  9368. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0,
  9369. VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>(
  9370. vx, vy, dst, ncols, nrows, item_ct1);
  9371. });
  9372. });
  9373. }
  9374. }
  9375. static void mul_mat_vec_q2_K_q8_1_sycl(const void *vx, const void *vy,
  9376. float *dst, const int ncols,
  9377. const int nrows,
  9378. dpct::queue_ptr stream) {
  9379. GGML_ASSERT(ncols % QK_K == 0);
  9380. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9381. const sycl::range<3> block_nums(1, 1, block_num_y);
  9382. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9383. {
  9384. stream->submit([&](sycl::handler &cgh) {
  9385. cgh.parallel_for(
  9386. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9387. [=](sycl::nd_item<3> item_ct1)
  9388. [[intel::reqd_sub_group_size(32)]] {
  9389. mul_mat_vec_q<QK_K, QI2_K, block_q2_K,
  9390. VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>(
  9391. vx, vy, dst, ncols, nrows, item_ct1);
  9392. });
  9393. });
  9394. }
  9395. }
  9396. static void mul_mat_vec_q3_K_q8_1_sycl(const void *vx, const void *vy,
  9397. float *dst, const int ncols,
  9398. const int nrows,
  9399. dpct::queue_ptr stream) {
  9400. GGML_ASSERT(ncols % QK_K == 0);
  9401. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9402. const sycl::range<3> block_nums(1, 1, block_num_y);
  9403. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9404. {
  9405. stream->submit([&](sycl::handler &cgh) {
  9406. cgh.parallel_for(
  9407. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9408. [=](sycl::nd_item<3> item_ct1)
  9409. [[intel::reqd_sub_group_size(32)]] {
  9410. mul_mat_vec_q<QK_K, QI3_K, block_q3_K,
  9411. VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>(
  9412. vx, vy, dst, ncols, nrows, item_ct1);
  9413. });
  9414. });
  9415. }
  9416. }
  9417. static void mul_mat_vec_q4_K_q8_1_sycl(const void *vx, const void *vy,
  9418. float *dst, const int ncols,
  9419. const int nrows,
  9420. dpct::queue_ptr stream) {
  9421. GGML_ASSERT(ncols % QK_K == 0);
  9422. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9423. const sycl::range<3> block_nums(1, 1, block_num_y);
  9424. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9425. {
  9426. stream->submit([&](sycl::handler &cgh) {
  9427. cgh.parallel_for(
  9428. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9429. [=](sycl::nd_item<3> item_ct1)
  9430. [[intel::reqd_sub_group_size(32)]] {
  9431. mul_mat_vec_q<QK_K, QI4_K, block_q4_K,
  9432. VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>(
  9433. vx, vy, dst, ncols, nrows, item_ct1);
  9434. });
  9435. });
  9436. }
  9437. }
  9438. static void mul_mat_vec_q5_K_q8_1_sycl(const void *vx, const void *vy,
  9439. float *dst, const int ncols,
  9440. const int nrows,
  9441. dpct::queue_ptr stream) {
  9442. GGML_ASSERT(ncols % QK_K == 0);
  9443. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9444. const sycl::range<3> block_nums(1, 1, block_num_y);
  9445. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9446. {
  9447. stream->submit([&](sycl::handler &cgh) {
  9448. cgh.parallel_for(
  9449. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9450. [=](sycl::nd_item<3> item_ct1)
  9451. [[intel::reqd_sub_group_size(32)]] {
  9452. mul_mat_vec_q<QK_K, QI5_K, block_q5_K,
  9453. VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>(
  9454. vx, vy, dst, ncols, nrows, item_ct1);
  9455. });
  9456. });
  9457. }
  9458. }
  9459. static void mul_mat_vec_q6_K_q8_1_sycl(const void *vx, const void *vy,
  9460. float *dst, const int ncols,
  9461. const int nrows,
  9462. dpct::queue_ptr stream) {
  9463. GGML_ASSERT(ncols % QK_K == 0);
  9464. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9465. const sycl::range<3> block_nums(1, 1, block_num_y);
  9466. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9467. {
  9468. stream->submit([&](sycl::handler &cgh) {
  9469. cgh.parallel_for(
  9470. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9471. [=](sycl::nd_item<3> item_ct1)
  9472. [[intel::reqd_sub_group_size(32)]] {
  9473. mul_mat_vec_q<QK_K, QI6_K, block_q6_K,
  9474. VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>(
  9475. vx, vy, dst, ncols, nrows, item_ct1);
  9476. });
  9477. });
  9478. }
  9479. }
  9480. static void mul_mat_vec_iq2_xxs_q8_1_sycl(const void *vx, const void *vy,
  9481. float *dst, const int ncols,
  9482. const int nrows,
  9483. dpct::queue_ptr stream) {
  9484. GGML_ASSERT(ncols % QK_K == 0);
  9485. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9486. const sycl::range<3> block_nums(1, 1, block_num_y);
  9487. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9488. {
  9489. stream->submit([&](sycl::handler &cgh) {
  9490. cgh.parallel_for(
  9491. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9492. [=](sycl::nd_item<3> item_ct1)
  9493. [[intel::reqd_sub_group_size(32)]] {
  9494. mul_mat_vec_q_iq2_xxs_q8_1<QK_K, QI2_XXS, block_iq2_xxs, 1>(
  9495. vx, vy, dst, ncols, nrows, item_ct1);
  9496. });
  9497. });
  9498. }
  9499. }
  9500. static void mul_mat_vec_iq2_xs_q8_1_sycl(const void *vx, const void *vy,
  9501. float *dst, const int ncols,
  9502. const int nrows,
  9503. dpct::queue_ptr stream) {
  9504. GGML_ASSERT(ncols % QK_K == 0);
  9505. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9506. const sycl::range<3> block_nums(1, 1, block_num_y);
  9507. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9508. {
  9509. stream->submit([&](sycl::handler &cgh) {
  9510. auto iq2xs_grid_ptr_ct1 = &iq2xs_grid[0];
  9511. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9512. cgh.parallel_for(
  9513. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9514. [=](sycl::nd_item<3> item_ct1)
  9515. [[intel::reqd_sub_group_size(32)]] {
  9516. mul_mat_vec_q_iq2_xs_q8_1<QK_K, QI2_XS, block_iq2_xs, 1>(
  9517. vx, vy, dst, ncols, nrows, item_ct1);
  9518. });
  9519. });
  9520. }
  9521. }
  9522. static void mul_mat_vec_iq2_s_q8_1_sycl(const void *vx, const void *vy,
  9523. float *dst, const int ncols,
  9524. const int nrows,
  9525. dpct::queue_ptr stream) {
  9526. GGML_ASSERT(ncols % QK_K == 0);
  9527. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9528. const sycl::range<3> block_nums(1, 1, block_num_y);
  9529. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9530. {
  9531. stream->submit([&](sycl::handler &cgh) {
  9532. auto iq2xs_grid_ptr_ct1 = &iq2xs_grid[0];
  9533. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9534. cgh.parallel_for(
  9535. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9536. [=](sycl::nd_item<3> item_ct1)
  9537. [[intel::reqd_sub_group_size(32)]] {
  9538. mul_mat_vec_q_iq2_s_q8_1<QK_K, QI2_S, block_iq2_s, 1>(
  9539. vx, vy, dst, ncols, nrows, item_ct1);
  9540. });
  9541. });
  9542. }
  9543. }
  9544. static void mul_mat_vec_iq3_xxs_q8_1_sycl(const void *vx, const void *vy,
  9545. float *dst, const int ncols,
  9546. const int nrows,
  9547. dpct::queue_ptr stream) {
  9548. GGML_ASSERT(ncols % QK_K == 0);
  9549. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9550. const sycl::range<3> block_nums(1, 1, block_num_y);
  9551. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9552. {
  9553. stream->submit([&](sycl::handler &cgh) {
  9554. auto iq3xxs_grid_ptr_ct1 = &iq3xxs_grid[0];
  9555. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9556. cgh.parallel_for(
  9557. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9558. [=](sycl::nd_item<3> item_ct1)
  9559. [[intel::reqd_sub_group_size(32)]] {
  9560. mul_mat_vec_q_iq3_xxs_q8_1<QK_K, QI3_XXS, block_iq3_xxs, 1>(
  9561. vx, vy, dst, ncols, nrows, item_ct1);
  9562. });
  9563. });
  9564. }
  9565. }
  9566. static void mul_mat_vec_iq3_s_q8_1_sycl(const void *vx, const void *vy,
  9567. float *dst, const int ncols,
  9568. const int nrows,
  9569. dpct::queue_ptr stream) {
  9570. GGML_ASSERT(ncols % QK_K == 0);
  9571. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9572. const sycl::range<3> block_nums(1, 1, block_num_y);
  9573. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9574. {
  9575. stream->submit([&](sycl::handler &cgh) {
  9576. auto iq3s_grid_ptr_ct1 = &iq3s_grid[0];
  9577. cgh.parallel_for(
  9578. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9579. [=](sycl::nd_item<3> item_ct1)
  9580. [[intel::reqd_sub_group_size(32)]] {
  9581. mul_mat_vec_q_iq3_s_q8_1<QK_K, QI3_XS, block_iq3_s, 1>(
  9582. vx, vy, dst, ncols, nrows, item_ct1);
  9583. });
  9584. });
  9585. }
  9586. }
  9587. static void mul_mat_vec_iq1_s_q8_1_sycl(const void *vx, const void *vy,
  9588. float *dst, const int ncols,
  9589. const int nrows,
  9590. dpct::queue_ptr stream) {
  9591. GGML_ASSERT(ncols % QK_K == 0);
  9592. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9593. const sycl::range<3> block_nums(1, 1, block_num_y);
  9594. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9595. {
  9596. stream->submit([&](sycl::handler &cgh) {
  9597. auto iq1s_grid_ptr_ct1 = &iq1s_grid_gpu[0];
  9598. auto ksigns64_ptr_ct1 = &ksigns64[0];
  9599. cgh.parallel_for(
  9600. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9601. [=](sycl::nd_item<3> item_ct1)
  9602. [[intel::reqd_sub_group_size(32)]] {
  9603. mul_mat_vec_q_iq1_s_q8_1<QK_K, QI1_S, block_iq1_s, 1>(
  9604. vx, vy, dst, ncols, nrows, item_ct1);
  9605. });
  9606. });
  9607. }
  9608. }
  9609. static void mul_mat_vec_iq1_m_q8_1_sycl(const void *vx, const void *vy,
  9610. float *dst, const int ncols,
  9611. const int nrows,
  9612. dpct::queue_ptr stream) {
  9613. GGML_ASSERT(ncols % QK_K == 0);
  9614. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9615. const sycl::range<3> block_nums(1, 1, block_num_y);
  9616. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9617. {
  9618. stream->submit([&](sycl::handler &cgh) {
  9619. cgh.parallel_for(
  9620. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9621. [=](sycl::nd_item<3> item_ct1)
  9622. [[intel::reqd_sub_group_size(32)]] {
  9623. mul_mat_vec_q_iq1_m_q8_1<QK_K, QI1_S, block_iq1_m, 1>(
  9624. vx, vy, dst, ncols, nrows, item_ct1);
  9625. });
  9626. });
  9627. }
  9628. }
  9629. static void mul_mat_vec_iq4_nl_q8_1_sycl(const void *vx, const void *vy,
  9630. float *dst, const int ncols,
  9631. const int nrows,
  9632. dpct::queue_ptr stream) {
  9633. GGML_ASSERT(ncols % QK4_NL == 0);
  9634. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9635. const sycl::range<3> block_nums(1, 1, block_num_y);
  9636. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9637. {
  9638. stream->submit([&](sycl::handler &cgh) {
  9639. cgh.parallel_for(
  9640. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9641. [=](sycl::nd_item<3> item_ct1)
  9642. [[intel::reqd_sub_group_size(32)]] {
  9643. mul_mat_vec_q_iq4_nl_q8_1<QK4_NL, QI4_NL, block_iq4_nl, 1>(
  9644. vx, vy, dst, ncols, nrows, item_ct1);
  9645. });
  9646. });
  9647. }
  9648. }
  9649. static void mul_mat_vec_iq4_xs_q8_1_sycl(const void *vx, const void *vy,
  9650. float *dst, const int ncols,
  9651. const int nrows,
  9652. dpct::queue_ptr stream) {
  9653. GGML_ASSERT(ncols % QK_K == 0);
  9654. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9655. const sycl::range<3> block_nums(1, 1, block_num_y);
  9656. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9657. {
  9658. stream->submit([&](sycl::handler &cgh) {
  9659. cgh.parallel_for(
  9660. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9661. [=](sycl::nd_item<3> item_ct1)
  9662. [[intel::reqd_sub_group_size(32)]] {
  9663. mul_mat_vec_q_iq4_xs_q8_1<QK_K, QI4_XS, block_iq4_xs, 1>(
  9664. vx, vy, dst, ncols, nrows, item_ct1);
  9665. });
  9666. });
  9667. }
  9668. }
  9669. static void ggml_mul_mat_q4_0_q8_1_sycl(const void *vx, const void *vy,
  9670. float *dst, const int ncols_x,
  9671. const int nrows_x, const int ncols_y,
  9672. const int nrows_y, const int nrows_dst,
  9673. dpct::queue_ptr stream) try {
  9674. int id;
  9675. SYCL_CHECK(
  9676. CHECK_TRY_ERROR(id = get_current_device_id()));
  9677. const int compute_capability = g_device_caps[id].cc;
  9678. int mmq_x, mmq_y, nwarps;
  9679. if (compute_capability >= VER_GEN13) {
  9680. mmq_x = MMQ_X_Q4_0_RDNA2;
  9681. mmq_y = MMQ_Y_Q4_0_RDNA2;
  9682. nwarps = NWARPS_Q4_0_RDNA2;
  9683. } else if (compute_capability >= VER_GEN12) {
  9684. mmq_x = MMQ_X_Q4_0_RDNA1;
  9685. mmq_y = MMQ_Y_Q4_0_RDNA1;
  9686. nwarps = NWARPS_Q4_0_RDNA1;
  9687. } else if (compute_capability >= VER_GEN9) {
  9688. mmq_x = MMQ_X_Q4_0_AMPERE;
  9689. mmq_y = MMQ_Y_Q4_0_AMPERE;
  9690. nwarps = NWARPS_Q4_0_AMPERE;
  9691. } else if (compute_capability >= VER_4VEC) {
  9692. mmq_x = MMQ_X_Q4_0_PASCAL;
  9693. mmq_y = MMQ_Y_Q4_0_PASCAL;
  9694. nwarps = NWARPS_Q4_0_PASCAL;
  9695. } else {
  9696. GGML_ASSERT(false);
  9697. }
  9698. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9699. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9700. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9701. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9702. if (nrows_x % mmq_y == 0) {
  9703. const bool need_check = false;
  9704. /*
  9705. DPCT1049:20: The work-group size passed to the SYCL kernel may exceed
  9706. the limit. To get the device limit, query
  9707. info::device::max_work_group_size. Adjust the work-group size if needed.
  9708. */
  9709. {
  9710. dpct::has_capability_or_fail(stream->get_device(),
  9711. {sycl::aspect::fp16});
  9712. stream->submit([&](sycl::handler &cgh) {
  9713. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  9714. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9715. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  9716. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  9717. cgh);
  9718. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9719. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9720. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9721. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9722. cgh.parallel_for(
  9723. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9724. [=](sycl::nd_item<3> item_ct1) {
  9725. mul_mat_q4_0<need_check>(
  9726. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9727. nrows_dst, item_ct1,
  9728. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  9729. tile_x_d_q4_0_acc_ct1.get_pointer(),
  9730. tile_y_qs_acc_ct1.get_pointer(),
  9731. tile_y_ds_acc_ct1.get_pointer());
  9732. });
  9733. });
  9734. }
  9735. } else {
  9736. const bool need_check = true;
  9737. /*
  9738. DPCT1049:21: The work-group size passed to the SYCL kernel may exceed
  9739. the limit. To get the device limit, query
  9740. info::device::max_work_group_size. Adjust the work-group size if needed.
  9741. */
  9742. {
  9743. dpct::has_capability_or_fail(stream->get_device(),
  9744. {sycl::aspect::fp16});
  9745. stream->submit([&](sycl::handler &cgh) {
  9746. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  9747. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9748. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  9749. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  9750. cgh);
  9751. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9752. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9753. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9754. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9755. cgh.parallel_for(
  9756. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9757. [=](sycl::nd_item<3> item_ct1) {
  9758. mul_mat_q4_0<need_check>(
  9759. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9760. nrows_dst, item_ct1,
  9761. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  9762. tile_x_d_q4_0_acc_ct1.get_pointer(),
  9763. tile_y_qs_acc_ct1.get_pointer(),
  9764. tile_y_ds_acc_ct1.get_pointer());
  9765. });
  9766. });
  9767. }
  9768. }
  9769. }
  9770. catch (sycl::exception const &exc) {
  9771. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9772. << ", line:" << __LINE__ << std::endl;
  9773. std::exit(1);
  9774. }
  9775. static void ggml_mul_mat_q4_1_q8_1_sycl(const void *vx, const void *vy,
  9776. float *dst, const int ncols_x,
  9777. const int nrows_x, const int ncols_y,
  9778. const int nrows_y, const int nrows_dst,
  9779. dpct::queue_ptr stream) try {
  9780. int id;
  9781. SYCL_CHECK(
  9782. CHECK_TRY_ERROR(id = get_current_device_id()));
  9783. const int compute_capability = g_device_caps[id].cc;
  9784. int mmq_x, mmq_y, nwarps;
  9785. if (compute_capability >= VER_GEN13) {
  9786. mmq_x = MMQ_X_Q4_1_RDNA2;
  9787. mmq_y = MMQ_Y_Q4_1_RDNA2;
  9788. nwarps = NWARPS_Q4_1_RDNA2;
  9789. } else if (compute_capability >= VER_GEN12) {
  9790. mmq_x = MMQ_X_Q4_1_RDNA1;
  9791. mmq_y = MMQ_Y_Q4_1_RDNA1;
  9792. nwarps = NWARPS_Q4_1_RDNA1;
  9793. } else if (compute_capability >= VER_GEN9) {
  9794. mmq_x = MMQ_X_Q4_1_AMPERE;
  9795. mmq_y = MMQ_Y_Q4_1_AMPERE;
  9796. nwarps = NWARPS_Q4_1_AMPERE;
  9797. } else if (compute_capability >= VER_4VEC) {
  9798. mmq_x = MMQ_X_Q4_1_PASCAL;
  9799. mmq_y = MMQ_Y_Q4_1_PASCAL;
  9800. nwarps = NWARPS_Q4_1_PASCAL;
  9801. } else {
  9802. GGML_ASSERT(false);
  9803. }
  9804. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9805. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9806. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9807. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9808. if (nrows_x % mmq_y == 0) {
  9809. const bool need_check = false;
  9810. /*
  9811. DPCT1049:22: The work-group size passed to the SYCL kernel may exceed
  9812. the limit. To get the device limit, query
  9813. info::device::max_work_group_size. Adjust the work-group size if needed.
  9814. */
  9815. {
  9816. dpct::has_capability_or_fail(stream->get_device(),
  9817. {sycl::aspect::fp16});
  9818. stream->submit([&](sycl::handler &cgh) {
  9819. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  9820. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  9821. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  9822. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  9823. cgh);
  9824. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9825. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9826. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9827. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9828. cgh.parallel_for(
  9829. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9830. [=](sycl::nd_item<3> item_ct1) {
  9831. mul_mat_q4_1<need_check>(
  9832. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9833. nrows_dst, item_ct1,
  9834. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  9835. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  9836. tile_y_qs_acc_ct1.get_pointer(),
  9837. tile_y_ds_acc_ct1.get_pointer());
  9838. });
  9839. });
  9840. }
  9841. } else {
  9842. const bool need_check = true;
  9843. /*
  9844. DPCT1049:23: The work-group size passed to the SYCL kernel may exceed
  9845. the limit. To get the device limit, query
  9846. info::device::max_work_group_size. Adjust the work-group size if needed.
  9847. */
  9848. {
  9849. dpct::has_capability_or_fail(stream->get_device(),
  9850. {sycl::aspect::fp16});
  9851. stream->submit([&](sycl::handler &cgh) {
  9852. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  9853. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  9854. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  9855. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  9856. cgh);
  9857. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9858. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9859. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9860. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9861. cgh.parallel_for(
  9862. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9863. [=](sycl::nd_item<3> item_ct1) {
  9864. mul_mat_q4_1<need_check>(
  9865. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9866. nrows_dst, item_ct1,
  9867. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  9868. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  9869. tile_y_qs_acc_ct1.get_pointer(),
  9870. tile_y_ds_acc_ct1.get_pointer());
  9871. });
  9872. });
  9873. }
  9874. }
  9875. }
  9876. catch (sycl::exception const &exc) {
  9877. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9878. << ", line:" << __LINE__ << std::endl;
  9879. std::exit(1);
  9880. }
  9881. static void ggml_mul_mat_q5_0_q8_1_sycl(const void *vx, const void *vy,
  9882. float *dst, const int ncols_x,
  9883. const int nrows_x, const int ncols_y,
  9884. const int nrows_y, const int nrows_dst,
  9885. dpct::queue_ptr stream) try {
  9886. int id;
  9887. SYCL_CHECK(
  9888. CHECK_TRY_ERROR(id = get_current_device_id()));
  9889. const int compute_capability = g_device_caps[id].cc;
  9890. int mmq_x, mmq_y, nwarps;
  9891. if (compute_capability >= VER_GEN13) {
  9892. mmq_x = MMQ_X_Q5_0_RDNA2;
  9893. mmq_y = MMQ_Y_Q5_0_RDNA2;
  9894. nwarps = NWARPS_Q5_0_RDNA2;
  9895. } else if (compute_capability >= VER_GEN12) {
  9896. mmq_x = MMQ_X_Q5_0_RDNA1;
  9897. mmq_y = MMQ_Y_Q5_0_RDNA1;
  9898. nwarps = NWARPS_Q5_0_RDNA1;
  9899. } else if (compute_capability >= VER_GEN9) {
  9900. mmq_x = MMQ_X_Q5_0_AMPERE;
  9901. mmq_y = MMQ_Y_Q5_0_AMPERE;
  9902. nwarps = NWARPS_Q5_0_AMPERE;
  9903. } else if (compute_capability >= VER_4VEC) {
  9904. mmq_x = MMQ_X_Q5_0_PASCAL;
  9905. mmq_y = MMQ_Y_Q5_0_PASCAL;
  9906. nwarps = NWARPS_Q5_0_PASCAL;
  9907. } else {
  9908. GGML_ASSERT(false);
  9909. }
  9910. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9911. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9912. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9913. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9914. if (nrows_x % mmq_y == 0) {
  9915. const bool need_check = false;
  9916. /*
  9917. DPCT1049:24: The work-group size passed to the SYCL kernel may exceed
  9918. the limit. To get the device limit, query
  9919. info::device::max_work_group_size. Adjust the work-group size if needed.
  9920. */
  9921. {
  9922. dpct::has_capability_or_fail(stream->get_device(),
  9923. {sycl::aspect::fp16});
  9924. stream->submit([&](sycl::handler &cgh) {
  9925. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  9926. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9927. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  9928. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  9929. cgh);
  9930. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9931. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9932. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9933. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9934. cgh.parallel_for(
  9935. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9936. [=](sycl::nd_item<3> item_ct1) {
  9937. mul_mat_q5_0<need_check>(
  9938. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9939. nrows_dst, item_ct1,
  9940. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  9941. tile_x_d_q5_0_acc_ct1.get_pointer(),
  9942. tile_y_qs_acc_ct1.get_pointer(),
  9943. tile_y_ds_acc_ct1.get_pointer());
  9944. });
  9945. });
  9946. }
  9947. } else {
  9948. const bool need_check = true;
  9949. /*
  9950. DPCT1049:25: The work-group size passed to the SYCL kernel may exceed
  9951. the limit. To get the device limit, query
  9952. info::device::max_work_group_size. Adjust the work-group size if needed.
  9953. */
  9954. {
  9955. dpct::has_capability_or_fail(stream->get_device(),
  9956. {sycl::aspect::fp16});
  9957. stream->submit([&](sycl::handler &cgh) {
  9958. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  9959. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9960. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  9961. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  9962. cgh);
  9963. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9964. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9965. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9966. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9967. cgh.parallel_for(
  9968. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9969. [=](sycl::nd_item<3> item_ct1) {
  9970. mul_mat_q5_0<need_check>(
  9971. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9972. nrows_dst, item_ct1,
  9973. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  9974. tile_x_d_q5_0_acc_ct1.get_pointer(),
  9975. tile_y_qs_acc_ct1.get_pointer(),
  9976. tile_y_ds_acc_ct1.get_pointer());
  9977. });
  9978. });
  9979. }
  9980. }
  9981. }
  9982. catch (sycl::exception const &exc) {
  9983. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9984. << ", line:" << __LINE__ << std::endl;
  9985. std::exit(1);
  9986. }
  9987. static void ggml_mul_mat_q5_1_q8_1_sycl(const void *vx, const void *vy,
  9988. float *dst, const int ncols_x,
  9989. const int nrows_x, const int ncols_y,
  9990. const int nrows_y, const int nrows_dst,
  9991. dpct::queue_ptr stream) try {
  9992. int id;
  9993. SYCL_CHECK(
  9994. CHECK_TRY_ERROR(id = get_current_device_id()));
  9995. const int compute_capability = g_device_caps[id].cc;
  9996. int mmq_x, mmq_y, nwarps;
  9997. if (compute_capability >= VER_GEN13) {
  9998. mmq_x = MMQ_X_Q5_1_RDNA2;
  9999. mmq_y = MMQ_Y_Q5_1_RDNA2;
  10000. nwarps = NWARPS_Q5_1_RDNA2;
  10001. } else if (compute_capability >= VER_GEN12) {
  10002. mmq_x = MMQ_X_Q5_1_RDNA1;
  10003. mmq_y = MMQ_Y_Q5_1_RDNA1;
  10004. nwarps = NWARPS_Q5_1_RDNA1;
  10005. } else if (compute_capability >= VER_GEN9) {
  10006. mmq_x = MMQ_X_Q5_1_AMPERE;
  10007. mmq_y = MMQ_Y_Q5_1_AMPERE;
  10008. nwarps = NWARPS_Q5_1_AMPERE;
  10009. } else if (compute_capability >= VER_4VEC) {
  10010. mmq_x = MMQ_X_Q5_1_PASCAL;
  10011. mmq_y = MMQ_Y_Q5_1_PASCAL;
  10012. nwarps = NWARPS_Q5_1_PASCAL;
  10013. } else {
  10014. GGML_ASSERT(false);
  10015. }
  10016. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10017. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10018. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10019. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10020. if (nrows_x % mmq_y == 0) {
  10021. const bool need_check = false;
  10022. /*
  10023. DPCT1049:26: The work-group size passed to the SYCL kernel may exceed
  10024. the limit. To get the device limit, query
  10025. info::device::max_work_group_size. Adjust the work-group size if needed.
  10026. */
  10027. {
  10028. dpct::has_capability_or_fail(stream->get_device(),
  10029. {sycl::aspect::fp16});
  10030. stream->submit([&](sycl::handler &cgh) {
  10031. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  10032. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10033. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  10034. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  10035. cgh);
  10036. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10037. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10038. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10039. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10040. cgh.parallel_for(
  10041. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10042. [=](sycl::nd_item<3> item_ct1) {
  10043. mul_mat_q5_1<need_check>(
  10044. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10045. nrows_dst, item_ct1,
  10046. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  10047. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  10048. tile_y_qs_acc_ct1.get_pointer(),
  10049. tile_y_ds_acc_ct1.get_pointer());
  10050. });
  10051. });
  10052. }
  10053. } else {
  10054. const bool need_check = true;
  10055. /*
  10056. DPCT1049:27: The work-group size passed to the SYCL kernel may exceed
  10057. the limit. To get the device limit, query
  10058. info::device::max_work_group_size. Adjust the work-group size if needed.
  10059. */
  10060. {
  10061. dpct::has_capability_or_fail(stream->get_device(),
  10062. {sycl::aspect::fp16});
  10063. stream->submit([&](sycl::handler &cgh) {
  10064. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  10065. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10066. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  10067. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  10068. cgh);
  10069. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10070. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10071. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10072. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10073. cgh.parallel_for(
  10074. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10075. [=](sycl::nd_item<3> item_ct1) {
  10076. mul_mat_q5_1<need_check>(
  10077. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10078. nrows_dst, item_ct1,
  10079. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  10080. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  10081. tile_y_qs_acc_ct1.get_pointer(),
  10082. tile_y_ds_acc_ct1.get_pointer());
  10083. });
  10084. });
  10085. }
  10086. }
  10087. }
  10088. catch (sycl::exception const &exc) {
  10089. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10090. << ", line:" << __LINE__ << std::endl;
  10091. std::exit(1);
  10092. }
  10093. static void ggml_mul_mat_q8_0_q8_1_sycl(const void *vx, const void *vy,
  10094. float *dst, const int ncols_x,
  10095. const int nrows_x, const int ncols_y,
  10096. const int nrows_y, const int nrows_dst,
  10097. dpct::queue_ptr stream) try {
  10098. int id;
  10099. SYCL_CHECK(
  10100. CHECK_TRY_ERROR(id = get_current_device_id()));
  10101. const int compute_capability = g_device_caps[id].cc;
  10102. int mmq_x, mmq_y, nwarps;
  10103. if (compute_capability >= VER_GEN13) {
  10104. mmq_x = MMQ_X_Q8_0_RDNA2;
  10105. mmq_y = MMQ_Y_Q8_0_RDNA2;
  10106. nwarps = NWARPS_Q8_0_RDNA2;
  10107. } else if (compute_capability >= VER_GEN12) {
  10108. mmq_x = MMQ_X_Q8_0_RDNA1;
  10109. mmq_y = MMQ_Y_Q8_0_RDNA1;
  10110. nwarps = NWARPS_Q8_0_RDNA1;
  10111. } else if (compute_capability >= VER_GEN9) {
  10112. mmq_x = MMQ_X_Q8_0_AMPERE;
  10113. mmq_y = MMQ_Y_Q8_0_AMPERE;
  10114. nwarps = NWARPS_Q8_0_AMPERE;
  10115. } else if (compute_capability >= VER_4VEC) {
  10116. mmq_x = MMQ_X_Q8_0_PASCAL;
  10117. mmq_y = MMQ_Y_Q8_0_PASCAL;
  10118. nwarps = NWARPS_Q8_0_PASCAL;
  10119. } else {
  10120. GGML_ASSERT(false);
  10121. }
  10122. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10123. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10124. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10125. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10126. if (nrows_x % mmq_y == 0) {
  10127. const bool need_check = false;
  10128. /*
  10129. DPCT1049:28: The work-group size passed to the SYCL kernel may exceed
  10130. the limit. To get the device limit, query
  10131. info::device::max_work_group_size. Adjust the work-group size if needed.
  10132. */
  10133. {
  10134. dpct::has_capability_or_fail(stream->get_device(),
  10135. {sycl::aspect::fp16});
  10136. stream->submit([&](sycl::handler &cgh) {
  10137. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  10138. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10139. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  10140. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  10141. cgh);
  10142. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10143. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10144. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10145. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10146. cgh.parallel_for(
  10147. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10148. [=](sycl::nd_item<3> item_ct1) {
  10149. mul_mat_q8_0<need_check>(
  10150. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10151. nrows_dst, item_ct1,
  10152. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  10153. tile_x_d_q8_0_acc_ct1.get_pointer(),
  10154. tile_y_qs_acc_ct1.get_pointer(),
  10155. tile_y_ds_acc_ct1.get_pointer());
  10156. });
  10157. });
  10158. }
  10159. } else {
  10160. const bool need_check = true;
  10161. /*
  10162. DPCT1049:29: The work-group size passed to the SYCL kernel may exceed
  10163. the limit. To get the device limit, query
  10164. info::device::max_work_group_size. Adjust the work-group size if needed.
  10165. */
  10166. {
  10167. dpct::has_capability_or_fail(stream->get_device(),
  10168. {sycl::aspect::fp16});
  10169. stream->submit([&](sycl::handler &cgh) {
  10170. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  10171. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10172. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  10173. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  10174. cgh);
  10175. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10176. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10177. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10178. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10179. cgh.parallel_for(
  10180. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10181. [=](sycl::nd_item<3> item_ct1) {
  10182. mul_mat_q8_0<need_check>(
  10183. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10184. nrows_dst, item_ct1,
  10185. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  10186. tile_x_d_q8_0_acc_ct1.get_pointer(),
  10187. tile_y_qs_acc_ct1.get_pointer(),
  10188. tile_y_ds_acc_ct1.get_pointer());
  10189. });
  10190. });
  10191. }
  10192. }
  10193. }
  10194. catch (sycl::exception const &exc) {
  10195. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10196. << ", line:" << __LINE__ << std::endl;
  10197. std::exit(1);
  10198. }
  10199. static void ggml_mul_mat_q2_K_q8_1_sycl(const void *vx, const void *vy,
  10200. float *dst, const int ncols_x,
  10201. const int nrows_x, const int ncols_y,
  10202. const int nrows_y, const int nrows_dst,
  10203. dpct::queue_ptr stream) try {
  10204. int id;
  10205. SYCL_CHECK(
  10206. CHECK_TRY_ERROR(id = get_current_device_id()));
  10207. const int compute_capability = g_device_caps[id].cc;
  10208. int mmq_x, mmq_y, nwarps;
  10209. if (compute_capability >= VER_GEN13) {
  10210. mmq_x = MMQ_X_Q2_K_RDNA2;
  10211. mmq_y = MMQ_Y_Q2_K_RDNA2;
  10212. nwarps = NWARPS_Q2_K_RDNA2;
  10213. } else if (compute_capability >= VER_GEN12) {
  10214. mmq_x = MMQ_X_Q2_K_RDNA1;
  10215. mmq_y = MMQ_Y_Q2_K_RDNA1;
  10216. nwarps = NWARPS_Q2_K_RDNA1;
  10217. } else if (compute_capability >= VER_GEN9) {
  10218. mmq_x = MMQ_X_Q2_K_AMPERE;
  10219. mmq_y = MMQ_Y_Q2_K_AMPERE;
  10220. nwarps = NWARPS_Q2_K_AMPERE;
  10221. } else if (compute_capability >= VER_4VEC) {
  10222. mmq_x = MMQ_X_Q2_K_PASCAL;
  10223. mmq_y = MMQ_Y_Q2_K_PASCAL;
  10224. nwarps = NWARPS_Q2_K_PASCAL;
  10225. } else {
  10226. GGML_ASSERT(false);
  10227. }
  10228. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10229. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10230. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10231. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10232. if (nrows_x % mmq_y == 0) {
  10233. const bool need_check = false;
  10234. /*
  10235. DPCT1049:30: The work-group size passed to the SYCL kernel may exceed
  10236. the limit. To get the device limit, query
  10237. info::device::max_work_group_size. Adjust the work-group size if needed.
  10238. */
  10239. {
  10240. dpct::has_capability_or_fail(stream->get_device(),
  10241. {sycl::aspect::fp16});
  10242. stream->submit([&](sycl::handler &cgh) {
  10243. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  10244. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10245. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  10246. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  10247. cgh);
  10248. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  10249. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10250. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10251. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10252. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10253. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10254. cgh.parallel_for(
  10255. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10256. [=](sycl::nd_item<3> item_ct1) {
  10257. mul_mat_q2_K<need_check>(
  10258. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10259. nrows_dst, item_ct1,
  10260. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  10261. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  10262. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  10263. tile_y_qs_acc_ct1.get_pointer(),
  10264. tile_y_ds_acc_ct1.get_pointer());
  10265. });
  10266. });
  10267. }
  10268. } else {
  10269. const bool need_check = true;
  10270. /*
  10271. DPCT1049:31: The work-group size passed to the SYCL kernel may exceed
  10272. the limit. To get the device limit, query
  10273. info::device::max_work_group_size. Adjust the work-group size if needed.
  10274. */
  10275. {
  10276. dpct::has_capability_or_fail(stream->get_device(),
  10277. {sycl::aspect::fp16});
  10278. stream->submit([&](sycl::handler &cgh) {
  10279. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  10280. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10281. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  10282. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  10283. cgh);
  10284. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  10285. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10286. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10287. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10288. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10289. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10290. cgh.parallel_for(
  10291. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10292. [=](sycl::nd_item<3> item_ct1) {
  10293. mul_mat_q2_K<need_check>(
  10294. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10295. nrows_dst, item_ct1,
  10296. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  10297. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  10298. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  10299. tile_y_qs_acc_ct1.get_pointer(),
  10300. tile_y_ds_acc_ct1.get_pointer());
  10301. });
  10302. });
  10303. }
  10304. }
  10305. }
  10306. catch (sycl::exception const &exc) {
  10307. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10308. << ", line:" << __LINE__ << std::endl;
  10309. std::exit(1);
  10310. }
  10311. static void ggml_mul_mat_q3_K_q8_1_sycl(const void *vx, const void *vy,
  10312. float *dst, const int ncols_x,
  10313. const int nrows_x, const int ncols_y,
  10314. const int nrows_y, const int nrows_dst,
  10315. dpct::queue_ptr stream) try {
  10316. #if QK_K == 256
  10317. int id;
  10318. SYCL_CHECK(
  10319. CHECK_TRY_ERROR(id = get_current_device_id()));
  10320. const int compute_capability = g_device_caps[id].cc;
  10321. int mmq_x, mmq_y, nwarps;
  10322. if (compute_capability >= VER_GEN13) {
  10323. mmq_x = MMQ_X_Q3_K_RDNA2;
  10324. mmq_y = MMQ_Y_Q3_K_RDNA2;
  10325. nwarps = NWARPS_Q3_K_RDNA2;
  10326. } else if (compute_capability >= VER_GEN12) {
  10327. mmq_x = MMQ_X_Q3_K_RDNA1;
  10328. mmq_y = MMQ_Y_Q3_K_RDNA1;
  10329. nwarps = NWARPS_Q3_K_RDNA1;
  10330. } else if (compute_capability >= VER_GEN9) {
  10331. mmq_x = MMQ_X_Q3_K_AMPERE;
  10332. mmq_y = MMQ_Y_Q3_K_AMPERE;
  10333. nwarps = NWARPS_Q3_K_AMPERE;
  10334. } else if (compute_capability >= VER_4VEC) {
  10335. mmq_x = MMQ_X_Q3_K_PASCAL;
  10336. mmq_y = MMQ_Y_Q3_K_PASCAL;
  10337. nwarps = NWARPS_Q3_K_PASCAL;
  10338. } else {
  10339. GGML_ASSERT(false);
  10340. }
  10341. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10342. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10343. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10344. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10345. if (nrows_x % mmq_y == 0) {
  10346. const bool need_check = false;
  10347. /*
  10348. DPCT1049:32: The work-group size passed to the SYCL kernel may exceed
  10349. the limit. To get the device limit, query
  10350. info::device::max_work_group_size. Adjust the work-group size if needed.
  10351. */
  10352. {
  10353. dpct::has_capability_or_fail(stream->get_device(),
  10354. {sycl::aspect::fp16});
  10355. stream->submit([&](sycl::handler &cgh) {
  10356. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  10357. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10358. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  10359. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  10360. cgh);
  10361. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  10362. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  10363. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  10364. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10365. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10366. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10367. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10368. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10369. cgh.parallel_for(
  10370. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10371. [=](sycl::nd_item<3> item_ct1) {
  10372. mul_mat_q3_K<need_check>(
  10373. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10374. nrows_dst, item_ct1,
  10375. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  10376. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  10377. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  10378. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  10379. tile_y_qs_acc_ct1.get_pointer(),
  10380. tile_y_ds_acc_ct1.get_pointer());
  10381. });
  10382. });
  10383. }
  10384. } else {
  10385. const bool need_check = true;
  10386. /*
  10387. DPCT1049:33: The work-group size passed to the SYCL kernel may exceed
  10388. the limit. To get the device limit, query
  10389. info::device::max_work_group_size. Adjust the work-group size if needed.
  10390. */
  10391. {
  10392. dpct::has_capability_or_fail(stream->get_device(),
  10393. {sycl::aspect::fp16});
  10394. stream->submit([&](sycl::handler &cgh) {
  10395. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  10396. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10397. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  10398. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  10399. cgh);
  10400. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  10401. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  10402. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  10403. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10404. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10405. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10406. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10407. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10408. cgh.parallel_for(
  10409. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10410. [=](sycl::nd_item<3> item_ct1) {
  10411. mul_mat_q3_K<need_check>(
  10412. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10413. nrows_dst, item_ct1,
  10414. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  10415. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  10416. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  10417. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  10418. tile_y_qs_acc_ct1.get_pointer(),
  10419. tile_y_ds_acc_ct1.get_pointer());
  10420. });
  10421. });
  10422. }
  10423. }
  10424. #endif
  10425. }
  10426. catch (sycl::exception const &exc) {
  10427. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10428. << ", line:" << __LINE__ << std::endl;
  10429. std::exit(1);
  10430. }
  10431. static void ggml_mul_mat_q4_K_q8_1_sycl(const void *vx, const void *vy,
  10432. float *dst, const int ncols_x,
  10433. const int nrows_x, const int ncols_y,
  10434. const int nrows_y, const int nrows_dst,
  10435. dpct::queue_ptr stream) try {
  10436. int id;
  10437. SYCL_CHECK(
  10438. CHECK_TRY_ERROR(id = get_current_device_id()));
  10439. const int compute_capability = g_device_caps[id].cc;
  10440. int mmq_x, mmq_y, nwarps;
  10441. if (compute_capability >= VER_GEN13) {
  10442. mmq_x = MMQ_X_Q4_K_RDNA2;
  10443. mmq_y = MMQ_Y_Q4_K_RDNA2;
  10444. nwarps = NWARPS_Q4_K_RDNA2;
  10445. } else if (compute_capability >= VER_GEN12) {
  10446. mmq_x = MMQ_X_Q4_K_RDNA1;
  10447. mmq_y = MMQ_Y_Q4_K_RDNA1;
  10448. nwarps = NWARPS_Q4_K_RDNA1;
  10449. } else if (compute_capability >= VER_GEN9) {
  10450. mmq_x = MMQ_X_Q4_K_AMPERE;
  10451. mmq_y = MMQ_Y_Q4_K_AMPERE;
  10452. nwarps = NWARPS_Q4_K_AMPERE;
  10453. } else if (compute_capability >= VER_4VEC) {
  10454. mmq_x = MMQ_X_Q4_K_PASCAL;
  10455. mmq_y = MMQ_Y_Q4_K_PASCAL;
  10456. nwarps = NWARPS_Q4_K_PASCAL;
  10457. } else {
  10458. GGML_ASSERT(false);
  10459. }
  10460. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10461. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10462. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10463. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10464. if (nrows_x % mmq_y == 0) {
  10465. const bool need_check = false;
  10466. /*
  10467. DPCT1049:34: The work-group size passed to the SYCL kernel may exceed
  10468. the limit. To get the device limit, query
  10469. info::device::max_work_group_size. Adjust the work-group size if needed.
  10470. */
  10471. {
  10472. dpct::has_capability_or_fail(stream->get_device(),
  10473. {sycl::aspect::fp16});
  10474. stream->submit([&](sycl::handler &cgh) {
  10475. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  10476. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10477. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  10478. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  10479. cgh);
  10480. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  10481. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10482. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10483. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10484. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10485. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10486. cgh.parallel_for(
  10487. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10488. [=](sycl::nd_item<3> item_ct1) {
  10489. mul_mat_q4_K<need_check>(
  10490. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10491. nrows_dst, item_ct1,
  10492. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  10493. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  10494. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  10495. tile_y_qs_acc_ct1.get_pointer(),
  10496. tile_y_ds_acc_ct1.get_pointer());
  10497. });
  10498. });
  10499. }
  10500. } else {
  10501. const bool need_check = true;
  10502. /*
  10503. DPCT1049:35: The work-group size passed to the SYCL kernel may exceed
  10504. the limit. To get the device limit, query
  10505. info::device::max_work_group_size. Adjust the work-group size if needed.
  10506. */
  10507. {
  10508. dpct::has_capability_or_fail(stream->get_device(),
  10509. {sycl::aspect::fp16});
  10510. stream->submit([&](sycl::handler &cgh) {
  10511. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  10512. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10513. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  10514. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  10515. cgh);
  10516. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  10517. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10518. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10519. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10520. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10521. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10522. cgh.parallel_for(
  10523. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10524. [=](sycl::nd_item<3> item_ct1) {
  10525. mul_mat_q4_K<need_check>(
  10526. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10527. nrows_dst, item_ct1,
  10528. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  10529. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  10530. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  10531. tile_y_qs_acc_ct1.get_pointer(),
  10532. tile_y_ds_acc_ct1.get_pointer());
  10533. });
  10534. });
  10535. }
  10536. }
  10537. }
  10538. catch (sycl::exception const &exc) {
  10539. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10540. << ", line:" << __LINE__ << std::endl;
  10541. std::exit(1);
  10542. }
  10543. static void ggml_mul_mat_q5_K_q8_1_sycl(const void *vx, const void *vy,
  10544. float *dst, const int ncols_x,
  10545. const int nrows_x, const int ncols_y,
  10546. const int nrows_y, const int nrows_dst,
  10547. dpct::queue_ptr stream) try {
  10548. int id;
  10549. SYCL_CHECK(
  10550. CHECK_TRY_ERROR(id = get_current_device_id()));
  10551. const int compute_capability = g_device_caps[id].cc;
  10552. int mmq_x, mmq_y, nwarps;
  10553. if (compute_capability >= VER_GEN13) {
  10554. mmq_x = MMQ_X_Q5_K_RDNA2;
  10555. mmq_y = MMQ_Y_Q5_K_RDNA2;
  10556. nwarps = NWARPS_Q5_K_RDNA2;
  10557. } else if (compute_capability >= VER_GEN12) {
  10558. mmq_x = MMQ_X_Q5_K_RDNA1;
  10559. mmq_y = MMQ_Y_Q5_K_RDNA1;
  10560. nwarps = NWARPS_Q5_K_RDNA1;
  10561. } else if (compute_capability >= VER_GEN9) {
  10562. mmq_x = MMQ_X_Q5_K_AMPERE;
  10563. mmq_y = MMQ_Y_Q5_K_AMPERE;
  10564. nwarps = NWARPS_Q5_K_AMPERE;
  10565. } else if (compute_capability >= VER_4VEC) {
  10566. mmq_x = MMQ_X_Q5_K_PASCAL;
  10567. mmq_y = MMQ_Y_Q5_K_PASCAL;
  10568. nwarps = NWARPS_Q5_K_PASCAL;
  10569. } else {
  10570. GGML_ASSERT(false);
  10571. }
  10572. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10573. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10574. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10575. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10576. if (nrows_x % mmq_y == 0) {
  10577. const bool need_check = false;
  10578. /*
  10579. DPCT1049:36: The work-group size passed to the SYCL kernel may exceed
  10580. the limit. To get the device limit, query
  10581. info::device::max_work_group_size. Adjust the work-group size if needed.
  10582. */
  10583. {
  10584. dpct::has_capability_or_fail(stream->get_device(),
  10585. {sycl::aspect::fp16});
  10586. stream->submit([&](sycl::handler &cgh) {
  10587. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  10588. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10589. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  10590. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  10591. cgh);
  10592. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  10593. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10594. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10595. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10596. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10597. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10598. cgh.parallel_for(
  10599. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10600. [=](sycl::nd_item<3> item_ct1) {
  10601. mul_mat_q5_K<need_check>(
  10602. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10603. nrows_dst, item_ct1,
  10604. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  10605. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  10606. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  10607. tile_y_qs_acc_ct1.get_pointer(),
  10608. tile_y_ds_acc_ct1.get_pointer());
  10609. });
  10610. });
  10611. }
  10612. } else {
  10613. const bool need_check = true;
  10614. /*
  10615. DPCT1049:37: The work-group size passed to the SYCL kernel may exceed
  10616. the limit. To get the device limit, query
  10617. info::device::max_work_group_size. Adjust the work-group size if needed.
  10618. */
  10619. {
  10620. dpct::has_capability_or_fail(stream->get_device(),
  10621. {sycl::aspect::fp16});
  10622. stream->submit([&](sycl::handler &cgh) {
  10623. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  10624. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10625. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  10626. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  10627. cgh);
  10628. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  10629. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10630. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10631. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10632. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10633. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10634. cgh.parallel_for(
  10635. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10636. [=](sycl::nd_item<3> item_ct1) {
  10637. mul_mat_q5_K<need_check>(
  10638. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10639. nrows_dst, item_ct1,
  10640. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  10641. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  10642. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  10643. tile_y_qs_acc_ct1.get_pointer(),
  10644. tile_y_ds_acc_ct1.get_pointer());
  10645. });
  10646. });
  10647. }
  10648. }
  10649. }
  10650. catch (sycl::exception const &exc) {
  10651. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10652. << ", line:" << __LINE__ << std::endl;
  10653. std::exit(1);
  10654. }
  10655. static void ggml_mul_mat_q6_K_q8_1_sycl(const void *vx, const void *vy,
  10656. float *dst, const int ncols_x,
  10657. const int nrows_x, const int ncols_y,
  10658. const int nrows_y, const int nrows_dst,
  10659. dpct::queue_ptr stream) try {
  10660. int id;
  10661. SYCL_CHECK(
  10662. CHECK_TRY_ERROR(id = get_current_device_id()));
  10663. const int compute_capability = g_device_caps[id].cc;
  10664. int mmq_x, mmq_y, nwarps;
  10665. if (compute_capability >= VER_GEN13) {
  10666. mmq_x = MMQ_X_Q6_K_RDNA2;
  10667. mmq_y = MMQ_Y_Q6_K_RDNA2;
  10668. nwarps = NWARPS_Q6_K_RDNA2;
  10669. } else if (compute_capability >= VER_GEN12) {
  10670. mmq_x = MMQ_X_Q6_K_RDNA1;
  10671. mmq_y = MMQ_Y_Q6_K_RDNA1;
  10672. nwarps = NWARPS_Q6_K_RDNA1;
  10673. } else if (compute_capability >= VER_GEN9) {
  10674. mmq_x = MMQ_X_Q6_K_AMPERE;
  10675. mmq_y = MMQ_Y_Q6_K_AMPERE;
  10676. nwarps = NWARPS_Q6_K_AMPERE;
  10677. } else if (compute_capability >= VER_4VEC) {
  10678. mmq_x = MMQ_X_Q6_K_PASCAL;
  10679. mmq_y = MMQ_Y_Q6_K_PASCAL;
  10680. nwarps = NWARPS_Q6_K_PASCAL;
  10681. } else {
  10682. GGML_ASSERT(false);
  10683. }
  10684. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10685. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10686. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10687. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10688. if (nrows_x % mmq_y == 0) {
  10689. const bool need_check = false;
  10690. /*
  10691. DPCT1049:38: The work-group size passed to the SYCL kernel may exceed
  10692. the limit. To get the device limit, query
  10693. info::device::max_work_group_size. Adjust the work-group size if needed.
  10694. */
  10695. {
  10696. dpct::has_capability_or_fail(stream->get_device(),
  10697. {sycl::aspect::fp16});
  10698. stream->submit([&](sycl::handler &cgh) {
  10699. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  10700. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10701. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  10702. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  10703. cgh);
  10704. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  10705. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10706. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10707. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10708. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10709. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10710. cgh.parallel_for(
  10711. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10712. [=](sycl::nd_item<3> item_ct1) {
  10713. mul_mat_q6_K<need_check>(
  10714. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10715. nrows_dst, item_ct1,
  10716. tile_x_ql_acc_ct1.get_pointer(),
  10717. tile_x_dm_acc_ct1.get_pointer(),
  10718. tile_x_sc_acc_ct1.get_pointer(),
  10719. tile_y_qs_acc_ct1.get_pointer(),
  10720. tile_y_ds_acc_ct1.get_pointer());
  10721. });
  10722. });
  10723. }
  10724. } else {
  10725. const bool need_check = true;
  10726. /*
  10727. DPCT1049:39: The work-group size passed to the SYCL kernel may exceed
  10728. the limit. To get the device limit, query
  10729. info::device::max_work_group_size. Adjust the work-group size if needed.
  10730. */
  10731. {
  10732. dpct::has_capability_or_fail(stream->get_device(),
  10733. {sycl::aspect::fp16});
  10734. stream->submit([&](sycl::handler &cgh) {
  10735. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  10736. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10737. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  10738. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  10739. cgh);
  10740. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  10741. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10742. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10743. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10744. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10745. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10746. cgh.parallel_for(
  10747. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10748. [=](sycl::nd_item<3> item_ct1) {
  10749. mul_mat_q6_K<need_check>(
  10750. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10751. nrows_dst, item_ct1,
  10752. tile_x_ql_acc_ct1.get_pointer(),
  10753. tile_x_dm_acc_ct1.get_pointer(),
  10754. tile_x_sc_acc_ct1.get_pointer(),
  10755. tile_y_qs_acc_ct1.get_pointer(),
  10756. tile_y_ds_acc_ct1.get_pointer());
  10757. });
  10758. });
  10759. }
  10760. }
  10761. }
  10762. catch (sycl::exception const &exc) {
  10763. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10764. << ", line:" << __LINE__ << std::endl;
  10765. std::exit(1);
  10766. }
  10767. static void ggml_mul_mat_p021_f16_f32_sycl(const void *vx, const float *y,
  10768. float *dst, const int ncols_x,
  10769. const int nrows_x,
  10770. const int nchannels_x,
  10771. const int nchannels_y,
  10772. dpct::queue_ptr stream) {
  10773. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  10774. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10775. {
  10776. dpct::has_capability_or_fail(stream->get_device(),
  10777. {sycl::aspect::fp16});
  10778. stream->parallel_for(
  10779. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10780. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10781. mul_mat_p021_f16_f32(vx, y, dst, ncols_x, nrows_x, nchannels_x,
  10782. nchannels_y, item_ct1);
  10783. });
  10784. }
  10785. }
  10786. static void ggml_mul_mat_vec_nc_f16_f32_sycl(
  10787. const void *vx, const float *y, float *dst, const int ncols_x,
  10788. const int nrows_x, const int row_stride_x, const int nchannels_x,
  10789. const int nchannels_y, const int channel_stride_x, dpct::queue_ptr stream) {
  10790. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  10791. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10792. {
  10793. dpct::has_capability_or_fail(stream->get_device(),
  10794. {sycl::aspect::fp16});
  10795. stream->parallel_for(
  10796. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10797. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10798. mul_mat_vec_nc_f16_f32(vx, y, dst, ncols_x, nrows_x,
  10799. row_stride_x, channel_stride_x,
  10800. nchannels_y / nchannels_x, item_ct1);
  10801. });
  10802. }
  10803. }
  10804. static void
  10805. ggml_cpy_f16_f32_sycl(const char *cx, char *cdst, const int ne, const int ne00,
  10806. const int ne01, const int ne02, const int nb00,
  10807. const int nb01, const int nb02, const int nb03,
  10808. const int ne10, const int ne11, const int ne12,
  10809. const int nb10, const int nb11, const int nb12,
  10810. const int nb13, dpct::queue_ptr stream) {
  10811. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10812. {
  10813. dpct::has_capability_or_fail(stream->get_device(),
  10814. {sycl::aspect::fp16});
  10815. stream->parallel_for(
  10816. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10817. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10818. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10819. [=](sycl::nd_item<3> item_ct1) {
  10820. cpy_f32_f16<cpy_1_f16_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00,
  10821. nb01, nb02, nb03, ne10, ne11, ne12,
  10822. nb10, nb11, nb12, nb13, item_ct1);
  10823. });
  10824. }
  10825. }
  10826. static void ggml_cpy_f32_f32_sycl(const char *cx, char *cdst, const int ne,
  10827. const int ne00, const int ne01,
  10828. const int ne02, const int nb00,
  10829. const int nb01, const int nb02,
  10830. const int nb03, const int ne10,
  10831. const int ne11, const int ne12,
  10832. const int nb10, const int nb11,
  10833. const int nb12, const int nb13,
  10834. dpct::queue_ptr stream) {
  10835. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10836. {
  10837. dpct::has_capability_or_fail(stream->get_device(),
  10838. {sycl::aspect::fp16});
  10839. stream->parallel_for(
  10840. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10841. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10842. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10843. [=](sycl::nd_item<3> item_ct1) {
  10844. cpy_f32_f16<cpy_1_f32_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10845. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10846. item_ct1);
  10847. });
  10848. }
  10849. }
  10850. static void ggml_cpy_f32_f16_sycl(const char *cx, char *cdst, const int ne,
  10851. const int ne00, const int ne01,
  10852. const int ne02, const int nb00,
  10853. const int nb01, const int nb02,
  10854. const int nb03, const int ne10,
  10855. const int ne11, const int ne12,
  10856. const int nb10, const int nb11,
  10857. const int nb12, const int nb13,
  10858. dpct::queue_ptr stream) {
  10859. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10860. {
  10861. dpct::has_capability_or_fail(stream->get_device(),
  10862. {sycl::aspect::fp16});
  10863. stream->parallel_for(
  10864. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10865. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10866. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10867. [=](sycl::nd_item<3> item_ct1) {
  10868. cpy_f32_f16<cpy_1_f32_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10869. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10870. item_ct1);
  10871. });
  10872. }
  10873. }
  10874. static void ggml_cpy_f32_q8_0_sycl(const char *cx, char *cdst, const int ne,
  10875. const int ne00, const int ne01,
  10876. const int ne02, const int nb00,
  10877. const int nb01, const int nb02,
  10878. const int nb03, const int ne10,
  10879. const int ne11, const int ne12,
  10880. const int nb10, const int nb11,
  10881. const int nb12, const int nb13,
  10882. dpct::queue_ptr stream) {
  10883. GGML_ASSERT(ne % QK8_0 == 0);
  10884. const int num_blocks = ne / QK8_0;
  10885. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10886. sycl::range<3>(1, 1, 1)),
  10887. [=](sycl::nd_item<3> item_ct1) {
  10888. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0>(
  10889. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10890. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10891. item_ct1);
  10892. });
  10893. }
  10894. static void ggml_cpy_f32_q4_0_sycl(const char *cx, char *cdst, const int ne,
  10895. const int ne00, const int ne01,
  10896. const int ne02, const int nb00,
  10897. const int nb01, const int nb02,
  10898. const int nb03, const int ne10,
  10899. const int ne11, const int ne12,
  10900. const int nb10, const int nb11,
  10901. const int nb12, const int nb13,
  10902. dpct::queue_ptr stream) {
  10903. GGML_ASSERT(ne % QK4_0 == 0);
  10904. const int num_blocks = ne / QK4_0;
  10905. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10906. sycl::range<3>(1, 1, 1)),
  10907. [=](sycl::nd_item<3> item_ct1) {
  10908. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0>(
  10909. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10910. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10911. item_ct1);
  10912. });
  10913. }
  10914. static void ggml_cpy_f32_q4_1_sycl(const char *cx, char *cdst, const int ne,
  10915. const int ne00, const int ne01,
  10916. const int ne02, const int nb00,
  10917. const int nb01, const int nb02,
  10918. const int nb03, const int ne10,
  10919. const int ne11, const int ne12,
  10920. const int nb10, const int nb11,
  10921. const int nb12, const int nb13,
  10922. dpct::queue_ptr stream) {
  10923. GGML_ASSERT(ne % QK4_1 == 0);
  10924. const int num_blocks = ne / QK4_1;
  10925. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10926. sycl::range<3>(1, 1, 1)),
  10927. [=](sycl::nd_item<3> item_ct1) {
  10928. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1>(
  10929. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10930. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10931. item_ct1);
  10932. });
  10933. }
  10934. static void ggml_cpy_f16_f16_sycl(const char *cx, char *cdst, const int ne,
  10935. const int ne00, const int ne01,
  10936. const int ne02, const int nb00,
  10937. const int nb01, const int nb02,
  10938. const int nb03, const int ne10,
  10939. const int ne11, const int ne12,
  10940. const int nb10, const int nb11,
  10941. const int nb12, const int nb13,
  10942. dpct::queue_ptr stream) {
  10943. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10944. {
  10945. dpct::has_capability_or_fail(stream->get_device(),
  10946. {sycl::aspect::fp16});
  10947. stream->parallel_for(
  10948. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10949. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10950. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10951. [=](sycl::nd_item<3> item_ct1) {
  10952. cpy_f32_f16<cpy_1_f16_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10953. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10954. item_ct1);
  10955. });
  10956. }
  10957. }
  10958. static void ggml_cpy_i16_i16_sycl(const char *cx, char *cdst, const int ne,
  10959. const int ne00, const int ne01,
  10960. const int ne02, const int nb00,
  10961. const int nb01, const int nb02,
  10962. const int nb03, const int ne10,
  10963. const int ne11, const int ne12,
  10964. const int nb10, const int nb11,
  10965. const int nb12, const int nb13,
  10966. dpct::queue_ptr stream) {
  10967. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10968. {
  10969. // dpct::has_capability_or_fail(stream->get_device(),
  10970. // {sycl::aspect::fp16});
  10971. stream->parallel_for(
  10972. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10973. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10974. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10975. [=](sycl::nd_item<3> item_ct1) {
  10976. cpy_f32_f16<cpy_1_i16_i16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10977. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10978. item_ct1);
  10979. });
  10980. }
  10981. }
  10982. static void ggml_cpy_i32_i32_sycl(const char *cx, char *cdst, const int ne,
  10983. const int ne00, const int ne01,
  10984. const int ne02, const int nb00,
  10985. const int nb01, const int nb02,
  10986. const int nb03, const int ne10,
  10987. const int ne11, const int ne12,
  10988. const int nb10, const int nb11,
  10989. const int nb12, const int nb13,
  10990. dpct::queue_ptr stream) {
  10991. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10992. {
  10993. // dpct::has_capability_or_fail(stream->get_device(),
  10994. // {sycl::aspect::fp16});
  10995. stream->parallel_for(
  10996. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10997. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10998. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10999. [=](sycl::nd_item<3> item_ct1) {
  11000. cpy_f32_f16<cpy_1_i32_i32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  11001. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  11002. item_ct1);
  11003. });
  11004. }
  11005. }
  11006. static void scale_f32_sycl(const float *x, float *dst, const float scale,
  11007. const int k, dpct::queue_ptr stream) {
  11008. const int num_blocks = (k + SYCL_SCALE_BLOCK_SIZE - 1) / SYCL_SCALE_BLOCK_SIZE;
  11009. stream->parallel_for(
  11010. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  11011. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE),
  11012. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE)),
  11013. [=](sycl::nd_item<3> item_ct1) {
  11014. scale_f32(x, dst, scale, k, item_ct1);
  11015. });
  11016. }
  11017. static void clamp_f32_sycl(const float *x, float *dst, const float min,
  11018. const float max, const int k,
  11019. dpct::queue_ptr stream) {
  11020. const int num_blocks = (k + SYCL_CLAMP_BLOCK_SIZE - 1) / SYCL_CLAMP_BLOCK_SIZE;
  11021. stream->parallel_for(
  11022. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  11023. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE),
  11024. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE)),
  11025. [=](sycl::nd_item<3> item_ct1) {
  11026. clamp_f32(x, dst, min, max, k, item_ct1);
  11027. });
  11028. }
  11029. template <typename T>
  11030. static void rope_sycl(const T *x, T *dst, int ncols, int nrows,
  11031. const int32_t *pos, float freq_scale, int p_delta_rows,
  11032. float freq_base, float ext_factor, float attn_factor,
  11033. rope_corr_dims corr_dims, dpct::queue_ptr stream) {
  11034. GGML_ASSERT(ncols % 2 == 0);
  11035. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  11036. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  11037. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  11038. if (pos == nullptr) {
  11039. /*
  11040. DPCT1049:40: The work-group size passed to the SYCL kernel may exceed
  11041. the limit. To get the device limit, query
  11042. info::device::max_work_group_size. Adjust the work-group size if needed.
  11043. */
  11044. dpct::has_capability_or_fail(stream->get_device(),
  11045. {sycl::aspect::fp16});
  11046. stream->parallel_for(
  11047. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11048. [=](sycl::nd_item<3> item_ct1) {
  11049. rope<T, false>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  11050. freq_base, ext_factor, attn_factor, corr_dims,
  11051. item_ct1);
  11052. });
  11053. } else {
  11054. /*
  11055. DPCT1049:41: The work-group size passed to the SYCL kernel may exceed
  11056. the limit. To get the device limit, query
  11057. info::device::max_work_group_size. Adjust the work-group size if needed.
  11058. */
  11059. dpct::has_capability_or_fail(stream->get_device(),
  11060. {sycl::aspect::fp16});
  11061. stream->parallel_for(
  11062. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11063. [=](sycl::nd_item<3> item_ct1) {
  11064. rope<T, true>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  11065. freq_base, ext_factor, attn_factor, corr_dims,
  11066. item_ct1);
  11067. });
  11068. }
  11069. }
  11070. template <typename T>
  11071. static void rope_neox_sycl(const T *x, T *dst, int ncols, int n_dims, int nrows,
  11072. const int32_t *pos, float freq_scale,
  11073. int p_delta_rows, float freq_base, float ext_factor,
  11074. float attn_factor, rope_corr_dims corr_dims,
  11075. dpct::queue_ptr stream) {
  11076. GGML_ASSERT(ncols % 2 == 0);
  11077. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  11078. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  11079. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  11080. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  11081. const float inv_ndims = -1.0f / n_dims;
  11082. if (pos == nullptr) {
  11083. /*
  11084. DPCT1049:42: The work-group size passed to the SYCL kernel may exceed
  11085. the limit. To get the device limit, query
  11086. info::device::max_work_group_size. Adjust the work-group size if needed.
  11087. */
  11088. dpct::has_capability_or_fail(stream->get_device(),
  11089. {sycl::aspect::fp16});
  11090. stream->parallel_for(
  11091. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11092. [=](sycl::nd_item<3> item_ct1) {
  11093. rope_neox<T, false>(x, dst, ncols, n_dims, pos, freq_scale,
  11094. p_delta_rows, ext_factor, attn_factor,
  11095. corr_dims, theta_scale, inv_ndims,
  11096. item_ct1);
  11097. });
  11098. } else {
  11099. /*
  11100. DPCT1049:43: The work-group size passed to the SYCL kernel may exceed
  11101. the limit. To get the device limit, query
  11102. info::device::max_work_group_size. Adjust the work-group size if needed.
  11103. */
  11104. dpct::has_capability_or_fail(stream->get_device(),
  11105. {sycl::aspect::fp16});
  11106. stream->parallel_for(
  11107. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11108. [=](sycl::nd_item<3> item_ct1) {
  11109. rope_neox<T, true>(x, dst, ncols, n_dims, pos, freq_scale,
  11110. p_delta_rows, ext_factor, attn_factor,
  11111. corr_dims, theta_scale, inv_ndims, item_ct1);
  11112. });
  11113. }
  11114. }
  11115. static void rope_glm_f32_sycl(const float *x, float *dst, int ncols, int nrows,
  11116. const int32_t *pos, float freq_scale,
  11117. int p_delta_rows, float freq_base, int n_ctx,
  11118. dpct::queue_ptr stream) {
  11119. GGML_ASSERT(ncols % 4 == 0);
  11120. const sycl::range<3> block_dims(1, 1, SYCL_ROPE_BLOCK_SIZE / 4);
  11121. const int num_blocks_x = (ncols + SYCL_ROPE_BLOCK_SIZE - 1) / SYCL_ROPE_BLOCK_SIZE;
  11122. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  11123. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11124. [=](sycl::nd_item<3> item_ct1) {
  11125. rope_glm_f32(x, dst, ncols, pos, freq_scale,
  11126. p_delta_rows, freq_base, n_ctx,
  11127. item_ct1);
  11128. });
  11129. }
  11130. static void alibi_f32_sycl(const float *x, float *dst, const int ncols,
  11131. const int nrows, const int k_rows,
  11132. const int n_heads_log2_floor, const float m0,
  11133. const float m1, dpct::queue_ptr stream) {
  11134. const sycl::range<3> block_dims(1, 1, SYCL_ALIBI_BLOCK_SIZE);
  11135. const int num_blocks_x = (ncols + SYCL_ALIBI_BLOCK_SIZE - 1) / (SYCL_ALIBI_BLOCK_SIZE);
  11136. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  11137. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11138. [=](sycl::nd_item<3> item_ct1) {
  11139. alibi_f32(x, dst, ncols, k_rows,
  11140. n_heads_log2_floor, m0, m1, item_ct1);
  11141. });
  11142. }
  11143. static void sum_rows_f32_sycl(const float *x, float *dst, const int ncols,
  11144. const int nrows, dpct::queue_ptr stream) {
  11145. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  11146. const sycl::range<3> block_nums(1, nrows, 1);
  11147. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11148. [=](sycl::nd_item<3> item_ct1)
  11149. [[intel::reqd_sub_group_size(32)]] {
  11150. k_sum_rows_f32(x, dst, ncols, item_ct1);
  11151. });
  11152. }
  11153. static int next_power_of_2(int x) {
  11154. int n = 1;
  11155. while (n < x) {
  11156. n *= 2;
  11157. }
  11158. return n;
  11159. }
  11160. static void argsort_f32_i32_sycl(const float *x, int *dst, const int ncols,
  11161. const int nrows, ggml_sort_order order,
  11162. dpct::queue_ptr stream) {
  11163. // bitonic sort requires ncols to be power of 2
  11164. const int ncols_pad = next_power_of_2(ncols);
  11165. const sycl::range<3> block_dims(1, 1, ncols_pad);
  11166. const sycl::range<3> block_nums(1, nrows, 1);
  11167. const size_t shared_mem = ncols_pad * sizeof(int);
  11168. // GGML_ASSERT(shared_mem <= ggml_cuda_info().devices[ggml_cuda_get_device()].smpb);
  11169. if (order == GGML_SORT_ORDER_ASC) {
  11170. stream->submit([&](sycl::handler &cgh) {
  11171. sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
  11172. sycl::range<1>(shared_mem), cgh);
  11173. cgh.parallel_for(
  11174. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11175. [=](sycl::nd_item<3> item_ct1) {
  11176. k_argsort_f32_i32<GGML_SORT_ORDER_ASC>(
  11177. x, dst, ncols, ncols_pad, item_ct1,
  11178. dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
  11179. .get());
  11180. });
  11181. });
  11182. } else if (order == GGML_SORT_ORDER_DESC) {
  11183. stream->submit([&](sycl::handler &cgh) {
  11184. sycl::local_accessor<uint8_t, 1> dpct_local_acc_ct1(
  11185. sycl::range<1>(shared_mem), cgh);
  11186. cgh.parallel_for(
  11187. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11188. [=](sycl::nd_item<3> item_ct1) {
  11189. k_argsort_f32_i32<GGML_SORT_ORDER_DESC>(
  11190. x, dst, ncols, ncols_pad, item_ct1,
  11191. dpct_local_acc_ct1.get_multi_ptr<sycl::access::decorated::no>()
  11192. .get());
  11193. });
  11194. });
  11195. } else {
  11196. GGML_ASSERT(false);
  11197. }
  11198. }
  11199. static void diag_mask_inf_f32_sycl(const float *x, float *dst,
  11200. const int ncols_x, const int nrows_x,
  11201. const int rows_per_channel, const int n_past,
  11202. dpct::queue_ptr stream) {
  11203. const sycl::range<3> block_dims(1, SYCL_DIAG_MASK_INF_BLOCK_SIZE, 1);
  11204. const int block_num_x = (ncols_x + SYCL_DIAG_MASK_INF_BLOCK_SIZE - 1) / SYCL_DIAG_MASK_INF_BLOCK_SIZE;
  11205. const sycl::range<3> block_nums(1, block_num_x, nrows_x);
  11206. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11207. [=](sycl::nd_item<3> item_ct1) {
  11208. diag_mask_inf_f32(x, dst, ncols_x,
  11209. rows_per_channel, n_past,
  11210. item_ct1);
  11211. });
  11212. }
  11213. template <bool vals_smem, int ncols_template, int block_size_template>
  11214. static void soft_max_f32_submitter(const float * x, const float * mask, const float *pos, float * dst, const int ncols_par,
  11215. const int nrows_y, const float scale, const float max_bias, const float m0,
  11216. const float m1, uint32_t n_head_log2, sycl::range<3> block_nums, sycl::range<3> block_dims,
  11217. const size_t n_local_scratch, dpct::queue_ptr stream) {
  11218. stream->submit([&](sycl::handler &cgh) {
  11219. sycl::local_accessor<float, 1> local_buf_acc(n_local_scratch, cgh);
  11220. cgh.parallel_for(
  11221. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11222. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  11223. soft_max_f32<vals_smem, ncols_template, block_size_template>(x, mask, pos, dst, ncols_par,
  11224. nrows_y, scale, max_bias, m0,
  11225. m1, n_head_log2, item_ct1,
  11226. local_buf_acc.get_pointer());
  11227. });
  11228. });
  11229. }
  11230. static void soft_max_f32_sycl(const float * x, const float * mask, const float * pos,
  11231. float * dst, const int ncols_x, const int nrows_x,
  11232. const int nrows_y, const float scale, const float max_bias,
  11233. dpct::queue_ptr stream) {
  11234. int nth = WARP_SIZE;
  11235. while (nth < ncols_x && nth < SYCL_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  11236. const sycl::range<3> block_dims(1, 1, nth);
  11237. const sycl::range<3> block_nums(1, 1, nrows_x);
  11238. const size_t n_local_scratch = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE);
  11239. static_assert(SYCL_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  11240. const uint32_t n_head_kv = nrows_x/nrows_y;
  11241. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  11242. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  11243. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  11244. const size_t local_mem_size = stream->get_device().get_info<sycl::info::device::local_mem_size>();
  11245. if (n_local_scratch*sizeof(float) < local_mem_size) {
  11246. switch (ncols_x) {
  11247. case 32:
  11248. soft_max_f32_submitter<true, 32, 32>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11249. max_bias, m0, m1, n_head_log2, block_nums,
  11250. block_dims, n_local_scratch, stream);
  11251. break;
  11252. case 64:
  11253. soft_max_f32_submitter<true, 64, 64>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11254. max_bias, m0, m1, n_head_log2, block_nums,
  11255. block_dims, n_local_scratch, stream);
  11256. break;
  11257. case 128:
  11258. soft_max_f32_submitter<true, 128, 128>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11259. max_bias, m0, m1, n_head_log2, block_nums,
  11260. block_dims, n_local_scratch, stream);
  11261. break;
  11262. case 256:
  11263. soft_max_f32_submitter<true, 256, 256>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11264. max_bias, m0, m1, n_head_log2, block_nums,
  11265. block_dims, n_local_scratch, stream);
  11266. break;
  11267. case 512:
  11268. soft_max_f32_submitter<true, 512, 512>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11269. max_bias, m0, m1, n_head_log2, block_nums,
  11270. block_dims, n_local_scratch, stream);
  11271. break;
  11272. case 1024:
  11273. soft_max_f32_submitter<true, 1024, 1024>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11274. max_bias, m0, m1, n_head_log2, block_nums,
  11275. block_dims, n_local_scratch, stream);
  11276. break;
  11277. case 2048:
  11278. soft_max_f32_submitter<true, 2048, 1024>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11279. max_bias, m0, m1, n_head_log2, block_nums,
  11280. block_dims, n_local_scratch, stream);
  11281. break;
  11282. case 4096:
  11283. soft_max_f32_submitter<true, 4096, 1024>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11284. max_bias, m0, m1, n_head_log2, block_nums,
  11285. block_dims, n_local_scratch, stream);
  11286. break;
  11287. default:
  11288. soft_max_f32_submitter<true, 0, 0>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11289. max_bias, m0, m1, n_head_log2, block_nums,
  11290. block_dims, n_local_scratch, stream);
  11291. break;
  11292. }
  11293. } else {
  11294. soft_max_f32_submitter<false, 0, 0>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11295. max_bias, m0, m1, n_head_log2, block_nums,
  11296. block_dims, WARP_SIZE, stream);
  11297. }
  11298. }
  11299. template <typename T>
  11300. static void im2col_sycl(const float *x, T *dst, int IW, int IH,
  11301. int OW, int OH, int KW, int KH, int IC,
  11302. int offset_delta, int s0, int s1, int p0,
  11303. int p1, int d0, int d1,
  11304. dpct::queue_ptr stream) {
  11305. const int parallel_elements = OW * KW * KH;
  11306. const int num_blocks = (parallel_elements + SYCL_IM2COL_BLOCK_SIZE - 1) / SYCL_IM2COL_BLOCK_SIZE;
  11307. sycl::range<3> block_nums(IC, OH, num_blocks);
  11308. {
  11309. dpct::has_capability_or_fail(stream->get_device(),
  11310. {sycl::aspect::fp16});
  11311. stream->parallel_for(
  11312. sycl::nd_range<3>(block_nums *
  11313. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  11314. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  11315. [=](sycl::nd_item<3> item_ct1) {
  11316. im2col_kernel(x, dst, offset_delta, IW, IH, OW, KW, KH,
  11317. parallel_elements, (IC * KH * KW), s0, s1, p0,
  11318. p1, d0, d1, item_ct1);
  11319. });
  11320. }
  11321. }
  11322. // buffer pool for sycl
  11323. #define MAX_SYCL_BUFFERS 256
  11324. struct scoped_spin_lock {
  11325. std::atomic_flag& lock;
  11326. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  11327. while (lock.test_and_set(std::memory_order_acquire)) {
  11328. ; // spin
  11329. }
  11330. }
  11331. ~scoped_spin_lock() {
  11332. lock.clear(std::memory_order_release);
  11333. }
  11334. scoped_spin_lock(const scoped_spin_lock&) = delete;
  11335. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  11336. };
  11337. static std::atomic_flag g_sycl_pool_lock = ATOMIC_FLAG_INIT;
  11338. // #define DEBUG_SYCL_MALLOC
  11339. struct sycl_buffer {
  11340. void * ptr = nullptr;
  11341. size_t size = 0;
  11342. };
  11343. static sycl_buffer g_sycl_buffer_pool[GGML_SYCL_MAX_DEVICES][MAX_SYCL_BUFFERS];
  11344. static size_t g_sycl_pool_size[GGML_SYCL_MAX_DEVICES] = {0};
  11345. static void *ggml_sycl_pool_malloc_leg(int device_index, size_t size, size_t *actual_size) try {
  11346. scoped_spin_lock lock(g_sycl_pool_lock);
  11347. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg device_index %d size=%lu\n", device_index, size);
  11348. #ifdef DEBUG_SYCL_MALLOC
  11349. int nnz = 0;
  11350. size_t max_size = 0;
  11351. #endif
  11352. size_t best_diff = 1ull << 36;
  11353. int ibest = -1;
  11354. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  11355. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  11356. if (b.ptr != nullptr) {
  11357. #ifdef DEBUG_SYCL_MALLOC
  11358. ++nnz;
  11359. if (b.size > max_size) max_size = b.size;
  11360. #endif
  11361. if (b.size >= size) {
  11362. size_t diff = b.size - size;
  11363. if (diff < best_diff) {
  11364. best_diff = diff;
  11365. ibest = i;
  11366. if (!best_diff) {
  11367. void * ptr = b.ptr;
  11368. *actual_size = b.size;
  11369. b.ptr = nullptr;
  11370. b.size = 0;
  11371. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 1 %p and rm in pool\n", ptr);
  11372. return ptr;
  11373. }
  11374. }
  11375. }
  11376. }
  11377. }
  11378. if (ibest >= 0) {
  11379. sycl_buffer& b = g_sycl_buffer_pool[device_index][ibest];
  11380. void * ptr = b.ptr;
  11381. *actual_size = b.size;
  11382. b.ptr = nullptr;
  11383. b.size = 0;
  11384. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 2 %p and rm in pool\n", ptr);
  11385. return ptr;
  11386. }
  11387. void * ptr;
  11388. size_t look_ahead_size = (size_t) (1.05 * size);
  11389. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  11390. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  11391. SYCL_CHECK(
  11392. CHECK_TRY_ERROR(ptr = (void *)sycl::malloc_device(
  11393. look_ahead_size, *stream)));
  11394. *actual_size = look_ahead_size;
  11395. g_sycl_pool_size[device_index] += look_ahead_size;
  11396. #ifdef DEBUG_SYCL_MALLOC
  11397. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  11398. (uint32_t)(max_size/1024/1024), (uint32_t)(g_sycl_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  11399. #endif
  11400. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg look_ahead_size=%lu, return %p\n", look_ahead_size, ptr);
  11401. return ptr;
  11402. }
  11403. catch (sycl::exception const &exc) {
  11404. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11405. << ", line:" << __LINE__ << std::endl;
  11406. std::exit(1);
  11407. }
  11408. static void ggml_sycl_pool_free_leg(int device_index, void *ptr, size_t size) try {
  11409. scoped_spin_lock lock(g_sycl_pool_lock);
  11410. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  11411. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  11412. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  11413. if (b.ptr == nullptr) {
  11414. b.ptr = ptr;
  11415. b.size = size;
  11416. return;
  11417. }
  11418. }
  11419. fprintf(stderr, "WARNING: sycl buffer pool full, increase MAX_SYCL_BUFFERS\n");
  11420. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *stream)));
  11421. g_sycl_pool_size[device_index] -= size;
  11422. }
  11423. catch (sycl::exception const &exc) {
  11424. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11425. << ", line:" << __LINE__ << std::endl;
  11426. std::exit(1);
  11427. }
  11428. // pool with virtual memory
  11429. /*
  11430. DPCT1082:64: Migration of CUmemGenericAllocationHandle type is not supported.
  11431. */
  11432. // static std::vector<CUmemGenericAllocationHandle>
  11433. // g_sycl_pool_handles[GGML_SYCL_MAX_DEVICES];
  11434. static dpct::device_ptr g_sycl_pool_addr[GGML_SYCL_MAX_DEVICES] = {0};
  11435. static size_t g_sycl_pool_used[GGML_SYCL_MAX_DEVICES] = {0};
  11436. static void *ggml_sycl_pool_malloc_vmm(int device_index, size_t size, size_t *actual_size) try {
  11437. GGML_UNUSED(device_index);
  11438. GGML_UNUSED(size);
  11439. GGML_UNUSED(actual_size);
  11440. return NULL;
  11441. }
  11442. catch (sycl::exception const &exc) {
  11443. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11444. << ", line:" << __LINE__ << std::endl;
  11445. std::exit(1);
  11446. }
  11447. static void ggml_sycl_pool_free_vmm(int device_index, void *ptr, size_t size) try {
  11448. scoped_spin_lock lock(g_sycl_pool_lock);
  11449. #ifdef DEBUG_SYCL_MALLOC
  11450. printf("sycl pool[%d]: freed %llu bytes at %llx\n", device_index, (unsigned long long) size, ptr);
  11451. #endif
  11452. g_sycl_pool_used[device_index] -= size;
  11453. // all deallocations must be in reverse order of the allocations
  11454. GGML_ASSERT(ptr == (void *) (g_sycl_pool_addr[device_index] + g_sycl_pool_used[device_index]));
  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. static void *ggml_sycl_pool_malloc(int device_index, size_t size, size_t *actual_size) try {
  11462. if (g_device_caps[device_index].vmm) {
  11463. return ggml_sycl_pool_malloc_vmm(device_index, size, actual_size);
  11464. } else {
  11465. return ggml_sycl_pool_malloc_leg(device_index, size, actual_size);
  11466. }
  11467. }
  11468. catch (sycl::exception const &exc) {
  11469. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11470. << ", line:" << __LINE__ << std::endl;
  11471. std::exit(1);
  11472. }
  11473. static void ggml_sycl_pool_free(int device_index, void *ptr, size_t size) try {
  11474. if (g_device_caps[device_index].vmm) {
  11475. ggml_sycl_pool_free_vmm(device_index, ptr, size);
  11476. } else {
  11477. ggml_sycl_pool_free_leg(device_index, ptr, size);
  11478. }
  11479. }
  11480. catch (sycl::exception const &exc) {
  11481. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11482. << ", line:" << __LINE__ << std::endl;
  11483. std::exit(1);
  11484. }
  11485. template<typename T>
  11486. struct sycl_pool_alloc {
  11487. int device_index = -1;
  11488. int device_id = -1;
  11489. T * ptr = nullptr;
  11490. size_t actual_size = 0;
  11491. // size is in number of elements
  11492. T * alloc(size_t size) {
  11493. GGML_ASSERT(ptr == nullptr);
  11494. device_id = get_current_device_id();
  11495. device_index = g_sycl_gpu_mgr->get_index(device_id);
  11496. ptr = (T *) ggml_sycl_pool_malloc(device_index, size * sizeof(T), &this->actual_size);
  11497. // GGML_SYCL_DEBUG("sycl_pool_alloc %lu return %p actual size=%lu\n", size * sizeof(T), ptr, this->actual_size);
  11498. return ptr;
  11499. }
  11500. sycl_pool_alloc(size_t size) {
  11501. alloc(size);
  11502. }
  11503. ~sycl_pool_alloc() {
  11504. if (ptr != nullptr) {
  11505. ggml_sycl_pool_free(device_index, ptr, actual_size);
  11506. }
  11507. }
  11508. T * get() {
  11509. return ptr;
  11510. }
  11511. sycl_pool_alloc() = default;
  11512. sycl_pool_alloc(const sycl_pool_alloc &) = delete;
  11513. sycl_pool_alloc(sycl_pool_alloc &&) = delete;
  11514. sycl_pool_alloc& operator=(const sycl_pool_alloc &) = delete;
  11515. sycl_pool_alloc& operator=(sycl_pool_alloc &&) = delete;
  11516. };
  11517. static bool g_sycl_loaded = false;
  11518. bool ggml_sycl_loaded(void) {
  11519. return g_sycl_loaded;
  11520. }
  11521. void print_device_detail(int id, sycl::device &device, std::string device_type) {
  11522. dpct::device_info prop;
  11523. SYCL_CHECK(CHECK_TRY_ERROR(
  11524. dpct::get_device_info(prop, device)));
  11525. std::string version;
  11526. version += std::to_string(prop.get_major_version());
  11527. version += ".";
  11528. version += std::to_string(prop.get_minor_version());
  11529. device_type = std::regex_replace(device_type, std::regex("ext_oneapi_"), "");
  11530. fprintf(stderr, "|%2d|%18s|%45s|%10s|%11d|%8d|%7d|%15lu|\n", id, device_type.c_str(),
  11531. prop.get_name(), version.c_str(), prop.get_max_compute_units(),
  11532. prop.get_max_work_group_size(), prop.get_max_sub_group_size(),
  11533. prop.get_global_mem_size());
  11534. }
  11535. void ggml_backend_sycl_print_sycl_devices() {
  11536. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_print_sycl_devices\n");
  11537. int device_count = dpct::dev_mgr::instance().device_count();
  11538. std::map<std::string, size_t> DeviceNums;
  11539. fprintf(stderr, "found %d SYCL devices:\n", device_count);
  11540. fprintf(stderr, "| | | |Compute |Max compute|Max work|Max sub| |\n");
  11541. fprintf(stderr, "|ID| Device Type| Name|capability|units |group |group |Global mem size|\n");
  11542. fprintf(stderr, "|--|------------------|---------------------------------------------|----------|-----------|--------|-------|---------------|\n");
  11543. for (int id = 0; id < device_count; ++id) {
  11544. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  11545. sycl::backend backend = device.get_backend();
  11546. std::string backend_type = get_device_backend_and_type(device);
  11547. int type_id=DeviceNums[backend_type]++;
  11548. std::stringstream device_type;
  11549. device_type << "[" << backend_type << ":" << std::to_string(type_id) << "]";
  11550. print_device_detail(id, device, device_type.str());
  11551. }
  11552. }
  11553. void print_gpu_device_list() {
  11554. GGML_ASSERT(g_sycl_gpu_mgr);
  11555. char* hint=NULL;
  11556. if (g_ggml_sycl_backend_gpu_mode == SYCL_SINGLE_GPU_MODE) {
  11557. hint = "use %d SYCL GPUs: [%s] with Max compute units:%d\n";
  11558. } else {
  11559. hint = "detect %d SYCL GPUs: [%s] with top Max compute units:%d\n";
  11560. }
  11561. fprintf(stderr, hint,
  11562. g_sycl_gpu_mgr->get_gpu_count(),
  11563. g_sycl_gpu_mgr->gpus_list.c_str(),
  11564. g_sycl_gpu_mgr->max_compute_units);
  11565. }
  11566. int get_sycl_env(const char *env_name, int default_val) {
  11567. char *user_device_string = getenv(env_name);
  11568. int user_number = default_val;
  11569. unsigned n;
  11570. if (user_device_string != NULL &&
  11571. sscanf(user_device_string, " %u", &n) == 1) {
  11572. user_number = (int)n;
  11573. } else {
  11574. user_number = default_val;
  11575. }
  11576. return user_number;
  11577. }
  11578. int get_work_group_size(int user_device_id) {
  11579. dpct::device_info prop;
  11580. dpct::get_device_info(prop,
  11581. dpct::dev_mgr::instance().get_device(user_device_id));
  11582. return prop.get_max_work_group_size();
  11583. }
  11584. static void ggml_init_sycl() try {
  11585. static bool initialized = false;
  11586. if (!initialized) {
  11587. fprintf(stderr, "[SYCL] call ggml_init_sycl\n");
  11588. g_ggml_sycl_debug = get_sycl_env("GGML_SYCL_DEBUG", 0);
  11589. fprintf(stderr, "%s: GGML_SYCL_DEBUG: %d\n", __func__, g_ggml_sycl_debug);
  11590. #if defined(GGML_SYCL_F16)
  11591. fprintf(stderr, "%s: GGML_SYCL_F16: yes\n", __func__);
  11592. #else
  11593. fprintf(stderr, "%s: GGML_SYCL_F16: no\n", __func__);
  11594. #endif
  11595. /* NOT REMOVE, keep it for next optimize for XMX.
  11596. #if defined(SYCL_USE_XMX)
  11597. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  11598. #else
  11599. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  11600. #endif
  11601. */
  11602. if (CHECK_TRY_ERROR(g_all_sycl_device_count =
  11603. dpct::dev_mgr::instance().device_count()) != 0) {
  11604. initialized = true;
  11605. g_sycl_loaded = false;
  11606. return;
  11607. }
  11608. GGML_ASSERT(g_all_sycl_device_count <= GGML_SYCL_MAX_DEVICES);
  11609. ggml_backend_sycl_print_sycl_devices();
  11610. initialized = true;
  11611. g_sycl_loaded = true;
  11612. }
  11613. }
  11614. catch (sycl::exception const &exc) {
  11615. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11616. << ", line:" << __LINE__ << std::endl;
  11617. std::exit(1);
  11618. }
  11619. void ggml_init_by_gpus(int device_count) try {
  11620. g_device_count = device_count;
  11621. g_work_group_size = g_sycl_gpu_mgr->work_group_size;
  11622. int64_t total_vram = 0;
  11623. print_gpu_device_list();
  11624. for (int id = 0; id < GGML_SYCL_MAX_DEVICES; ++id) {
  11625. g_device_caps[id].vmm = 0;
  11626. g_device_caps[id].device_id = -1;
  11627. g_device_caps[id].cc = 0;
  11628. g_tensor_split[id] = 0;
  11629. g_default_tensor_split[id] = 0;
  11630. }
  11631. for (int i = 0; i < g_device_count; ++i) {
  11632. int device_id = g_sycl_gpu_mgr->gpus[i];
  11633. g_device_caps[i].vmm = 0;
  11634. dpct::device_info prop;
  11635. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  11636. prop, dpct::dev_mgr::instance().get_device(device_id))));
  11637. g_default_tensor_split[i] = total_vram;
  11638. total_vram += prop.get_global_mem_size();
  11639. g_device_caps[i].cc =
  11640. 100 * prop.get_major_version() + 10 * prop.get_minor_version();
  11641. }
  11642. for (int i = 0; i < g_device_count; ++i) {
  11643. g_default_tensor_split[i] /= total_vram;
  11644. }
  11645. for (int i = 0; i < g_device_count; ++i) {
  11646. SYCL_CHECK(ggml_sycl_set_device(i));
  11647. // create sycl streams
  11648. for (int is = 0; is < MAX_STREAMS; ++is) {
  11649. SYCL_CHECK(CHECK_TRY_ERROR(
  11650. g_syclStreams[i][is] =
  11651. dpct::get_current_device().create_queue(
  11652. g_sycl_gpu_mgr->get_co_ctx(), dpct::get_current_device())));
  11653. }
  11654. const dpct::queue_ptr stream = g_syclStreams[i][0];
  11655. // create sycl handle
  11656. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[i] = stream));
  11657. }
  11658. }
  11659. catch (sycl::exception const &exc) {
  11660. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11661. << ", line:" << __LINE__ << std::endl;
  11662. std::exit(1);
  11663. }
  11664. void *ggml_sycl_host_malloc(size_t size) try {
  11665. if (getenv("GGML_SYCL_NO_PINNED") != nullptr) {
  11666. return nullptr;
  11667. }
  11668. void * ptr = nullptr;
  11669. //allow to use dpct::get_in_order_queue() for host malloc
  11670. dpct::err0 err = CHECK_TRY_ERROR(
  11671. ptr = (void *)sycl::malloc_host(size, dpct::get_in_order_queue()));
  11672. if (err != 0) {
  11673. // clear the error
  11674. fprintf(
  11675. stderr,
  11676. "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  11677. size / 1024.0 / 1024.0,
  11678. "syclGetErrorString is not supported");
  11679. return nullptr;
  11680. }
  11681. return ptr;
  11682. }
  11683. catch (sycl::exception const &exc) {
  11684. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11685. << ", line:" << __LINE__ << std::endl;
  11686. std::exit(1);
  11687. }
  11688. void ggml_sycl_host_free(void *ptr) try {
  11689. //allow to use dpct::get_in_order_queue() for host malloc
  11690. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, dpct::get_in_order_queue())));
  11691. }
  11692. catch (sycl::exception const &exc) {
  11693. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11694. << ", line:" << __LINE__ << std::endl;
  11695. std::exit(1);
  11696. }
  11697. static dpct::err0 ggml_sycl_cpy_tensor_2d(void *dst,
  11698. const struct ggml_tensor *src,
  11699. int64_t i3, int64_t i2,
  11700. int64_t i1_low, int64_t i1_high,
  11701. dpct::queue_ptr stream) try {
  11702. dpct::memcpy_direction kind;
  11703. char * src_ptr;
  11704. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  11705. kind = dpct::host_to_device;
  11706. src_ptr = (char *) src->data;
  11707. // GGML_SYCL_DEBUG("ggml_sycl_cpy_tensor_2d GGML_BACKEND_TYPE_CPU src_ptr %p\n", src_ptr);
  11708. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  11709. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  11710. kind = dpct::device_to_device;
  11711. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  11712. int id;
  11713. SYCL_CHECK(CHECK_TRY_ERROR(
  11714. id = get_current_device_id()));
  11715. // GGML_SYCL_DEBUG("current device index %d\n", id);
  11716. src_ptr = (char *) extra->data_device[id];
  11717. } else {
  11718. // GGML_SYCL_DEBUG("GGML_ASSERT(false)\n");
  11719. GGML_ASSERT(false);
  11720. }
  11721. char * dst_ptr = (char *) dst;
  11722. GGML_TENSOR_LOCALS_1(int64_t, ne, src, ne);
  11723. GGML_TENSOR_LOCALS(int64_t, nb, src, nb);
  11724. const enum ggml_type type = src->type;
  11725. const int64_t ts = ggml_type_size(type);
  11726. const int64_t bs = ggml_blck_size(type);
  11727. int64_t i1_diff = i1_high - i1_low;
  11728. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  11729. if (nb0 == ts && nb1 == ts*ne0/bs) {
  11730. // GGML_SYCL_DEBUG("stream->memcpy: dst_ptr=%p, x=%p, size=%lu\n", dst_ptr, x, i1_diff * nb1);
  11731. // return CHECK_TRY_ERROR(stream->memcpy(dst_ptr, x, i1_diff * nb1));
  11732. return CHECK_TRY_ERROR(dpct::async_dpct_memcpy(dst_ptr, x, i1_diff * nb1,
  11733. kind, *stream));
  11734. } else if (nb0 == ts) {
  11735. return CHECK_TRY_ERROR(
  11736. dpct::async_dpct_memcpy(dst_ptr, ts * ne0 / bs, x, nb1,
  11737. ts * ne0 / bs, i1_diff, kind, *stream));
  11738. } else {
  11739. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  11740. const void * rx = (const void *) ((const char *) x + i1*nb1);
  11741. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  11742. // pretend the row is a matrix with cols=1
  11743. dpct::err0 r = CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  11744. rd, ts / bs, rx, nb0, ts / bs, ne0, kind, *stream));
  11745. /*
  11746. DPCT1001:85: The statement could not be removed.
  11747. */
  11748. /*
  11749. DPCT1000:86: Error handling if-stmt was detected but could not be
  11750. rewritten.
  11751. */
  11752. if (r != 0) return r;
  11753. }
  11754. return 0;
  11755. }
  11756. }
  11757. catch (sycl::exception const &exc) {
  11758. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11759. << ", line:" << __LINE__ << std::endl;
  11760. std::exit(1);
  11761. }
  11762. static void ggml_sycl_op_get_rows(const ggml_tensor *src0,
  11763. const ggml_tensor *src1, ggml_tensor *dst,
  11764. const float *src0_d, const float *src1_d,
  11765. float *dst_d, const dpct::queue_ptr &stream) {
  11766. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  11767. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11768. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  11769. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  11770. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  11771. const int32_t * src1_i32 = (const int32_t *) src1_d;
  11772. switch (src0->type) {
  11773. case GGML_TYPE_F16:
  11774. get_rows_sycl_float(src0, src1, dst, (const sycl::half *)src0_d,
  11775. src1_i32, dst_d, stream);
  11776. break;
  11777. case GGML_TYPE_F32:
  11778. get_rows_sycl_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11779. break;
  11780. case GGML_TYPE_Q4_0:
  11781. get_rows_sycl<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11782. break;
  11783. case GGML_TYPE_Q4_1:
  11784. get_rows_sycl<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11785. break;
  11786. case GGML_TYPE_Q5_0:
  11787. get_rows_sycl<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11788. break;
  11789. case GGML_TYPE_Q5_1:
  11790. get_rows_sycl<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11791. break;
  11792. case GGML_TYPE_Q8_0:
  11793. get_rows_sycl<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11794. break;
  11795. default:
  11796. // TODO: k-quants
  11797. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  11798. GGML_ASSERT(false);
  11799. break;
  11800. }
  11801. }
  11802. template <class op>
  11803. inline void ggml_sycl_op_bin_bcast(const ggml_tensor *src0,
  11804. const ggml_tensor *src1, ggml_tensor *dst,
  11805. const float *src0_dd, const float *src1_dd,
  11806. float *dst_dd,
  11807. const dpct::queue_ptr &main_stream) {
  11808. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  11809. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11810. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  11811. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd,
  11812. (sycl::half *)dst_dd, main_stream);
  11813. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  11814. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd, dst_dd,
  11815. main_stream);
  11816. } else if (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  11817. op()(src0, src1, dst, (const int32_t *)src0_dd, (const int32_t *)src1_dd, (int32_t *)dst_dd,
  11818. main_stream);
  11819. } else if (src0->type == GGML_TYPE_I16 && dst->type == GGML_TYPE_I16) {
  11820. op()(src0, src1, dst, (const int16_t *)src0_dd, (const int16_t *)src1_dd, (int16_t *)dst_dd,
  11821. main_stream);
  11822. } else {
  11823. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  11824. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  11825. GGML_ASSERT(false);
  11826. }
  11827. }
  11828. static void ggml_sycl_op_repeat(const ggml_tensor *src0,
  11829. const ggml_tensor *src1, ggml_tensor *dst,
  11830. const float *src0_d, const float *src1_d,
  11831. float *dst_d,
  11832. const dpct::queue_ptr &main_stream) {
  11833. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  11834. (void) src1;
  11835. (void) src1_d;
  11836. }
  11837. inline void ggml_sycl_op_add(const ggml_tensor *src0, const ggml_tensor *src1,
  11838. ggml_tensor *dst, const float *src0_dd,
  11839. const float *src1_dd, float *dst_dd,
  11840. const dpct::queue_ptr &main_stream) {
  11841. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11842. }
  11843. inline void ggml_sycl_op_acc(const ggml_tensor *src0, const ggml_tensor *src1,
  11844. ggml_tensor *dst, const float *src0_dd,
  11845. const float *src1_dd, float *dst_dd,
  11846. const dpct::queue_ptr &main_stream) {
  11847. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11848. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11849. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11850. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  11851. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  11852. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  11853. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  11854. int offset = dst->op_params[3] / 4; // offset in bytes
  11855. 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);
  11856. (void) dst;
  11857. }
  11858. inline void ggml_sycl_op_mul(const ggml_tensor *src0, const ggml_tensor *src1,
  11859. ggml_tensor *dst, const float *src0_dd,
  11860. const float *src1_dd, float *dst_dd,
  11861. const dpct::queue_ptr &main_stream) {
  11862. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11863. }
  11864. inline void ggml_sycl_op_div(const ggml_tensor *src0, const ggml_tensor *src1,
  11865. ggml_tensor *dst, const float *src0_dd,
  11866. const float *src1_dd, float *dst_dd,
  11867. const dpct::queue_ptr &main_stream) {
  11868. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11869. }
  11870. inline void ggml_sycl_op_gelu(const ggml_tensor *src0, const ggml_tensor *src1,
  11871. ggml_tensor *dst, const float *src0_dd,
  11872. const float *src1_dd, float *dst_dd,
  11873. const dpct::queue_ptr &main_stream) {
  11874. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11875. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11876. gelu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11877. (void) src1;
  11878. (void) dst;
  11879. (void) src1_dd;
  11880. }
  11881. inline void ggml_sycl_op_silu(const ggml_tensor *src0, const ggml_tensor *src1,
  11882. ggml_tensor *dst, const float *src0_dd,
  11883. const float *src1_dd, float *dst_dd,
  11884. const dpct::queue_ptr &main_stream) {
  11885. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11886. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11887. silu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11888. (void) src1;
  11889. (void) dst;
  11890. (void) src1_dd;
  11891. }
  11892. inline void ggml_sycl_op_gelu_quick(const ggml_tensor *src0,
  11893. const ggml_tensor *src1, ggml_tensor *dst,
  11894. const float *src0_dd, const float *src1_dd,
  11895. float *dst_dd,
  11896. const dpct::queue_ptr &main_stream) {
  11897. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11898. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11899. gelu_quick_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11900. (void) src1;
  11901. (void) dst;
  11902. (void) src1_dd;
  11903. }
  11904. inline void ggml_sycl_op_tanh(const ggml_tensor *src0, const ggml_tensor *src1,
  11905. ggml_tensor *dst, const float *src0_dd,
  11906. const float *src1_dd, float *dst_dd,
  11907. const dpct::queue_ptr &main_stream) {
  11908. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11909. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11910. tanh_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11911. (void) src1;
  11912. (void) dst;
  11913. (void) src1_dd;
  11914. }
  11915. inline void ggml_sycl_op_relu(const ggml_tensor *src0, const ggml_tensor *src1,
  11916. ggml_tensor *dst, const float *src0_dd,
  11917. const float *src1_dd, float *dst_dd,
  11918. const dpct::queue_ptr &main_stream) {
  11919. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11920. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11921. relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11922. (void) src1;
  11923. (void) dst;
  11924. (void) src1_dd;
  11925. }
  11926. static void ggml_sycl_op_hardsigmoid(const ggml_tensor *src0,
  11927. const ggml_tensor *src1, ggml_tensor *dst,
  11928. const float *src0_dd, const float *src1_dd,
  11929. float *dst_dd,
  11930. const dpct::queue_ptr &main_stream) {
  11931. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11932. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11933. hardsigmoid_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11934. (void) src1;
  11935. (void) dst;
  11936. (void) src1_dd;
  11937. }
  11938. static void ggml_sycl_op_hardswish(const ggml_tensor *src0,
  11939. const ggml_tensor *src1, ggml_tensor *dst,
  11940. const float *src0_dd, const float *src1_dd,
  11941. float *dst_dd, const dpct::queue_ptr &main_stream) {
  11942. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11943. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11944. hardswish_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11945. (void) src1;
  11946. (void) dst;
  11947. (void) src1_dd;
  11948. }
  11949. inline void ggml_sycl_op_leaky_relu(const ggml_tensor *src0,
  11950. const ggml_tensor *src1, ggml_tensor *dst,
  11951. const float *src0_dd, const float *src1_dd,
  11952. float *dst_dd,
  11953. const dpct::queue_ptr &main_stream) {
  11954. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11955. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11956. float negative_slope;
  11957. memcpy(&negative_slope, dst->op_params, sizeof(float));
  11958. leaky_relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  11959. (void) src1;
  11960. (void) dst;
  11961. (void) src1_dd;
  11962. }
  11963. inline void ggml_sycl_op_sqr(const ggml_tensor *src0, const ggml_tensor *src1,
  11964. ggml_tensor *dst, const float *src0_dd,
  11965. const float *src1_dd, float *dst_dd,
  11966. const dpct::queue_ptr &main_stream) {
  11967. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11968. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11969. sqr_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11970. (void) src1;
  11971. (void) dst;
  11972. (void) src1_dd;
  11973. }
  11974. inline void ggml_sycl_op_norm(const ggml_tensor *src0, const ggml_tensor *src1,
  11975. ggml_tensor *dst, const float *src0_dd,
  11976. const float *src1_dd, float *dst_dd,
  11977. const dpct::queue_ptr &main_stream) {
  11978. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11979. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11980. const int64_t ne00 = src0->ne[0];
  11981. const int64_t nrows = ggml_nrows(src0);
  11982. float eps;
  11983. memcpy(&eps, dst->op_params, sizeof(float));
  11984. norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  11985. (void) src1;
  11986. (void) dst;
  11987. (void) src1_dd;
  11988. }
  11989. inline void ggml_sycl_op_group_norm(const ggml_tensor *src0,
  11990. const ggml_tensor *src1, ggml_tensor *dst,
  11991. const float *src0_dd, const float *src1_dd,
  11992. float *dst_dd,
  11993. const dpct::queue_ptr &main_stream) {
  11994. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11995. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11996. int num_groups = dst->op_params[0];
  11997. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  11998. group_norm_f32_sycl(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  11999. (void) src1;
  12000. (void) dst;
  12001. (void) src1_dd;
  12002. }
  12003. inline void ggml_sycl_op_concat(const ggml_tensor *src0,
  12004. const ggml_tensor *src1, ggml_tensor *dst,
  12005. const float *src0_dd, const float *src1_dd,
  12006. float *dst_dd,
  12007. const dpct::queue_ptr &main_stream) {
  12008. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12009. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12010. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  12011. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  12012. 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);
  12013. }
  12014. (void) src1;
  12015. (void) dst;
  12016. }
  12017. inline void ggml_sycl_op_upscale(const ggml_tensor *src0,
  12018. const ggml_tensor *src1, ggml_tensor *dst,
  12019. const float *src0_dd, const float *src1_dd,
  12020. float *dst_dd,
  12021. const dpct::queue_ptr &main_stream) {
  12022. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12023. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  12024. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  12025. const int scale_factor = dst->op_params[0];
  12026. upscale_f32_sycl(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  12027. (void) src1;
  12028. (void) dst;
  12029. (void) src1_dd;
  12030. }
  12031. inline void ggml_sycl_op_pad(const ggml_tensor *src0, const ggml_tensor *src1,
  12032. ggml_tensor *dst, const float *src0_dd,
  12033. const float *src1_dd, 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. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  12038. pad_f32_sycl(src0_dd, dst_dd,
  12039. src0->ne[0], src0->ne[1], src0->ne[2],
  12040. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  12041. (void) src1;
  12042. (void) dst;
  12043. (void) src1_dd;
  12044. }
  12045. inline void ggml_sycl_op_rms_norm(const ggml_tensor *src0,
  12046. const ggml_tensor *src1, ggml_tensor *dst,
  12047. const float *src0_dd, const float *src1_dd,
  12048. float *dst_dd,
  12049. const dpct::queue_ptr &main_stream) {
  12050. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12051. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12052. const int64_t ne00 = src0->ne[0];
  12053. const int64_t nrows = ggml_nrows(src0);
  12054. float eps;
  12055. memcpy(&eps, dst->op_params, sizeof(float));
  12056. rms_norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  12057. (void) src1;
  12058. (void) dst;
  12059. (void) src1_dd;
  12060. }
  12061. inline void ggml_sycl_op_mul_mat_q(
  12062. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  12063. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  12064. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  12065. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  12066. const dpct::queue_ptr &stream) try {
  12067. const int64_t ne00 = src0->ne[0];
  12068. const int64_t ne10 = src1->ne[0];
  12069. GGML_ASSERT(ne10 % QK8_1 == 0);
  12070. const int64_t ne0 = dst->ne[0];
  12071. const int64_t row_diff = row_high - row_low;
  12072. int device_id;
  12073. SYCL_CHECK(
  12074. CHECK_TRY_ERROR(device_id = get_current_device_id()));
  12075. // the main device has a larger memory buffer to hold the results from all GPUs
  12076. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  12077. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && device_id == g_main_device ? ne0 : row_diff;
  12078. switch (src0->type) {
  12079. case GGML_TYPE_Q4_0:
  12080. 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);
  12081. break;
  12082. case GGML_TYPE_Q4_1:
  12083. 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);
  12084. break;
  12085. case GGML_TYPE_Q5_0:
  12086. 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);
  12087. break;
  12088. case GGML_TYPE_Q5_1:
  12089. 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);
  12090. break;
  12091. case GGML_TYPE_Q8_0:
  12092. 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);
  12093. break;
  12094. case GGML_TYPE_Q2_K:
  12095. 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);
  12096. break;
  12097. case GGML_TYPE_Q3_K:
  12098. 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);
  12099. break;
  12100. case GGML_TYPE_Q4_K:
  12101. 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);
  12102. break;
  12103. case GGML_TYPE_Q5_K:
  12104. 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);
  12105. break;
  12106. case GGML_TYPE_Q6_K:
  12107. 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);
  12108. break;
  12109. default:
  12110. GGML_ASSERT(false);
  12111. break;
  12112. }
  12113. (void) src1;
  12114. (void) dst;
  12115. (void) src1_ddf_i;
  12116. }
  12117. catch (sycl::exception const &exc) {
  12118. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12119. << ", line:" << __LINE__ << std::endl;
  12120. std::exit(1);
  12121. }
  12122. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split) {
  12123. int64_t min_compute_capability = INT_MAX;
  12124. int64_t max_compute_capability = INT_MIN;
  12125. for (int i = 0; i < g_device_count; ++i) {
  12126. if (tensor_split[i] < (i + 1 < g_device_count ? tensor_split[i + 1] : 1.0f)) {
  12127. if (min_compute_capability > g_device_caps[i].cc) {
  12128. min_compute_capability = g_device_caps[i].cc;
  12129. }
  12130. if (max_compute_capability < g_device_caps[i].cc) {
  12131. max_compute_capability = g_device_caps[i].cc;
  12132. }
  12133. }
  12134. }
  12135. switch(type) {
  12136. case GGML_TYPE_Q4_0:
  12137. case GGML_TYPE_Q4_1:
  12138. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  12139. case GGML_TYPE_Q5_0:
  12140. case GGML_TYPE_Q5_1:
  12141. case GGML_TYPE_Q8_0:
  12142. return 64;
  12143. case GGML_TYPE_F16:
  12144. case GGML_TYPE_F32:
  12145. return 1;
  12146. case GGML_TYPE_Q2_K:
  12147. case GGML_TYPE_Q3_K:
  12148. case GGML_TYPE_Q4_K:
  12149. case GGML_TYPE_Q5_K:
  12150. case GGML_TYPE_IQ2_XXS:
  12151. case GGML_TYPE_IQ2_XS:
  12152. case GGML_TYPE_IQ2_S:
  12153. case GGML_TYPE_IQ1_S:
  12154. case GGML_TYPE_IQ1_M:
  12155. case GGML_TYPE_IQ3_XXS:
  12156. case GGML_TYPE_IQ4_XS:
  12157. case GGML_TYPE_IQ4_NL:
  12158. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  12159. case GGML_TYPE_IQ3_S:
  12160. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  12161. case GGML_TYPE_Q6_K:
  12162. return 64;
  12163. default:
  12164. GGML_ASSERT(false);
  12165. }
  12166. }
  12167. inline void ggml_sycl_op_mul_mat_vec_q(
  12168. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  12169. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  12170. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  12171. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  12172. const dpct::queue_ptr &stream) {
  12173. const int64_t ne10 = src1->ne[0];
  12174. GGML_ASSERT(ne10 % QK8_1 == 0);
  12175. const int64_t ne00 = src0->ne[0];
  12176. const int64_t row_diff = row_high - row_low;
  12177. int id;
  12178. SYCL_CHECK(
  12179. CHECK_TRY_ERROR(id = get_current_device_id()));
  12180. // the main device has a larger memory buffer to hold the results from all GPUs
  12181. // nrows_dst == nrows of the matrix that the kernel writes into
  12182. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne00 : row_diff;
  12183. switch (src0->type) {
  12184. case GGML_TYPE_Q4_0:
  12185. mul_mat_vec_q4_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12186. break;
  12187. case GGML_TYPE_Q4_1:
  12188. mul_mat_vec_q4_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12189. break;
  12190. case GGML_TYPE_Q5_0:
  12191. mul_mat_vec_q5_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12192. break;
  12193. case GGML_TYPE_Q5_1:
  12194. mul_mat_vec_q5_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12195. break;
  12196. case GGML_TYPE_Q8_0:
  12197. mul_mat_vec_q8_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12198. break;
  12199. case GGML_TYPE_Q2_K:
  12200. mul_mat_vec_q2_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12201. break;
  12202. case GGML_TYPE_Q3_K:
  12203. mul_mat_vec_q3_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12204. break;
  12205. case GGML_TYPE_Q4_K:
  12206. mul_mat_vec_q4_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12207. break;
  12208. case GGML_TYPE_Q5_K:
  12209. mul_mat_vec_q5_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12210. break;
  12211. case GGML_TYPE_Q6_K:
  12212. mul_mat_vec_q6_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12213. break;
  12214. case GGML_TYPE_IQ1_S:
  12215. mul_mat_vec_iq1_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12216. break;
  12217. case GGML_TYPE_IQ1_M:
  12218. mul_mat_vec_iq1_m_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12219. break;
  12220. case GGML_TYPE_IQ2_XXS:
  12221. mul_mat_vec_iq2_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12222. break;
  12223. case GGML_TYPE_IQ2_XS:
  12224. mul_mat_vec_iq2_xs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12225. break;
  12226. case GGML_TYPE_IQ2_S:
  12227. mul_mat_vec_iq2_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12228. break;
  12229. case GGML_TYPE_IQ3_XXS:
  12230. mul_mat_vec_iq3_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12231. break;
  12232. case GGML_TYPE_IQ3_S:
  12233. mul_mat_vec_iq3_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12234. break;
  12235. case GGML_TYPE_IQ4_NL:
  12236. mul_mat_vec_iq4_nl_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12237. break;
  12238. case GGML_TYPE_IQ4_XS:
  12239. mul_mat_vec_iq4_xs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12240. break;
  12241. default:
  12242. GGML_ASSERT(false);
  12243. break;
  12244. }
  12245. (void) src1;
  12246. (void) dst;
  12247. (void) src1_ddf_i;
  12248. (void) src1_ncols;
  12249. (void) src1_padded_row_size;
  12250. }
  12251. inline void ggml_sycl_op_dequantize_mul_mat_vec(
  12252. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  12253. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  12254. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  12255. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  12256. const dpct::queue_ptr &stream) {
  12257. const int64_t ne00 = src0->ne[0];
  12258. const int64_t row_diff = row_high - row_low;
  12259. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12260. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  12261. #ifdef GGML_SYCL_F16
  12262. sycl_pool_alloc<sycl::half> src1_dfloat_a;
  12263. sycl::half *src1_dfloat = nullptr; // dfloat == half
  12264. bool src1_convert_f16 =
  12265. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  12266. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  12267. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  12268. if (src1_convert_f16) {
  12269. src1_dfloat = src1_dfloat_a.alloc(ne00);
  12270. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  12271. GGML_ASSERT(to_fp16_sycl != nullptr);
  12272. to_fp16_sycl(src1_ddf_i, src1_dfloat, ne00, stream);
  12273. }
  12274. #else
  12275. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  12276. #endif // GGML_SYCL_F16
  12277. switch (src0->type) {
  12278. case GGML_TYPE_Q4_0:
  12279. dequantize_mul_mat_vec_q4_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12280. break;
  12281. case GGML_TYPE_Q4_1:
  12282. dequantize_mul_mat_vec_q4_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12283. break;
  12284. case GGML_TYPE_Q5_0:
  12285. dequantize_mul_mat_vec_q5_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12286. break;
  12287. case GGML_TYPE_Q5_1:
  12288. dequantize_mul_mat_vec_q5_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12289. break;
  12290. case GGML_TYPE_Q8_0:
  12291. dequantize_mul_mat_vec_q8_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12292. break;
  12293. case GGML_TYPE_Q2_K:
  12294. dequantize_mul_mat_vec_q2_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12295. break;
  12296. case GGML_TYPE_Q3_K:
  12297. dequantize_mul_mat_vec_q3_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12298. break;
  12299. case GGML_TYPE_Q4_K:
  12300. dequantize_mul_mat_vec_q4_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12301. break;
  12302. case GGML_TYPE_Q5_K:
  12303. dequantize_mul_mat_vec_q5_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12304. break;
  12305. case GGML_TYPE_Q6_K:
  12306. dequantize_mul_mat_vec_q6_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12307. break;
  12308. case GGML_TYPE_F16:
  12309. convert_mul_mat_vec_f16_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12310. break;
  12311. default:
  12312. printf("ggml_sycl_op_dequantize_mul_mat_vec unsupported GGML_TYPE %d\n", src0->type);
  12313. GGML_ASSERT(false);
  12314. break;
  12315. }
  12316. (void) src1;
  12317. (void) dst;
  12318. (void) src1_ddq_i;
  12319. (void) src1_ncols;
  12320. (void) src1_padded_row_size;
  12321. }
  12322. inline void ggml_sycl_op_mul_mat_sycl(
  12323. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  12324. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  12325. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  12326. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  12327. const dpct::queue_ptr &stream) try {
  12328. GGML_ASSERT(src0_dd_i != nullptr);
  12329. GGML_ASSERT(src1_ddf_i != nullptr);
  12330. GGML_ASSERT(dst_dd_i != nullptr);
  12331. const int64_t ne00 = src0->ne[0];
  12332. const int64_t ne10 = src1->ne[0];
  12333. const int64_t ne0 = dst->ne[0];
  12334. const int64_t row_diff = row_high - row_low;
  12335. int id;
  12336. SYCL_CHECK(
  12337. CHECK_TRY_ERROR(id = get_current_device_id()));
  12338. // the main device has a larger memory buffer to hold the results from all GPUs
  12339. // ldc == nrows of the matrix that cuBLAS writes into
  12340. int ldc = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  12341. #ifdef GGML_SYCL_F16
  12342. bool use_fp16 = true; // TODO(Yu) SYCL capability check
  12343. #else
  12344. bool use_fp16 = false;
  12345. #endif
  12346. if ((src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  12347. use_fp16 && ggml_is_contiguous(src0) && row_diff == src0->ne[1] &&
  12348. dst->op_params[0] == GGML_PREC_DEFAULT) {
  12349. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp16 path\n");
  12350. sycl_pool_alloc<sycl::half> src0_as_f16;
  12351. if (src0->type != GGML_TYPE_F16) {
  12352. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src0->type);
  12353. GGML_ASSERT(to_fp16_sycl != nullptr);
  12354. size_t ne = row_diff*ne00;
  12355. src0_as_f16.alloc(ne);
  12356. to_fp16_sycl(src0_dd_i, src0_as_f16.get(), ne, stream);
  12357. }
  12358. const sycl::half *src0_ptr = src0->type == GGML_TYPE_F16
  12359. ? (const sycl::half *)src0_dd_i
  12360. : src0_as_f16.get();
  12361. sycl_pool_alloc<sycl::half> src1_as_f16;
  12362. if (src1->type != GGML_TYPE_F16) {
  12363. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  12364. GGML_ASSERT(to_fp16_sycl != nullptr);
  12365. size_t ne = src1_ncols*ne10;
  12366. src1_as_f16.alloc(ne);
  12367. to_fp16_sycl(src1_ddf_i, src1_as_f16.get(), ne, stream);
  12368. }
  12369. const sycl::half *src1_ptr = src1->type == GGML_TYPE_F16
  12370. ? (const sycl::half *)src1->data + src1_padded_row_size
  12371. : src1_as_f16.get();
  12372. sycl_pool_alloc<sycl::half> dst_f16(row_diff * src1_ncols);
  12373. const sycl::half alpha_f16 = 1.0f;
  12374. const sycl::half beta_f16 = 0.0f;
  12375. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  12376. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
  12377. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  12378. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  12379. &alpha_f16, src0_ptr, dpct::library_data_t::real_half, ne00,
  12380. src1_ptr, dpct::library_data_t::real_half, ne10, &beta_f16,
  12381. dst_f16.get(), dpct::library_data_t::real_half, ldc,
  12382. dpct::library_data_t::real_half)));
  12383. g_sycl_handles[id]->wait();
  12384. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  12385. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  12386. }
  12387. else {
  12388. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
  12389. sycl_pool_alloc<float> src0_ddq_as_f32;
  12390. sycl_pool_alloc<float> src1_ddq_as_f32;
  12391. if (src0->type != GGML_TYPE_F32) {
  12392. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src0->type);
  12393. GGML_ASSERT(to_fp32_sycl != nullptr);
  12394. src0_ddq_as_f32.alloc(row_diff*ne00);
  12395. to_fp32_sycl(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  12396. }
  12397. if (src1->type != GGML_TYPE_F32) {
  12398. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src1->type);
  12399. GGML_ASSERT(to_fp32_sycl != nullptr);
  12400. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  12401. to_fp32_sycl(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  12402. }
  12403. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  12404. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  12405. const float alpha = 1.0f;
  12406. const float beta = 0.0f;
  12407. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  12408. SYCL_CHECK(CHECK_TRY_ERROR(oneapi::mkl::blas::column_major::gemm(
  12409. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  12410. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  12411. dpct::get_value(&alpha, *g_sycl_handles[id]), src0_ddf_i, ne00,
  12412. src1_ddf1_i, ne10, dpct::get_value(&beta, *g_sycl_handles[id]),
  12413. dst_dd_i, ldc)));
  12414. g_sycl_handles[id]->wait();
  12415. }
  12416. (void) dst;
  12417. (void) src1_ddq_i;
  12418. (void) src1_padded_row_size;
  12419. }
  12420. catch (sycl::exception const &exc) {
  12421. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12422. << ", line:" << __LINE__ << std::endl;
  12423. std::exit(1);
  12424. }
  12425. inline void ggml_sycl_op_rope(const ggml_tensor *src0, const ggml_tensor *src1,
  12426. ggml_tensor *dst, const float *src0_dd,
  12427. const float *src1_dd, float *dst_dd,
  12428. const dpct::queue_ptr &main_stream) {
  12429. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  12430. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  12431. GGML_ASSERT(src0->type == dst->type);
  12432. const int64_t ne00 = src0->ne[0];
  12433. const int64_t ne01 = src0->ne[1];
  12434. const int64_t ne2 = dst->ne[2];
  12435. const int64_t nrows = ggml_nrows(src0);
  12436. //const int n_past = ((int32_t *) dst->op_params)[0];
  12437. const int n_dims = ((int32_t *) dst->op_params)[1];
  12438. const int mode = ((int32_t *) dst->op_params)[2];
  12439. const int n_ctx = ((int32_t *) dst->op_params)[3];
  12440. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  12441. // RoPE alteration for extended context
  12442. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  12443. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  12444. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  12445. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  12446. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  12447. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  12448. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  12449. const int32_t * pos = nullptr;
  12450. if ((mode & 1) == 0) {
  12451. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  12452. GGML_ASSERT(src1->ne[0] == ne2);
  12453. pos = (const int32_t *) src1_dd;
  12454. }
  12455. const bool is_neox = mode & 2;
  12456. const bool is_glm = mode & 4;
  12457. rope_corr_dims corr_dims;
  12458. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  12459. // compute
  12460. if (is_glm) {
  12461. GGML_ASSERT(false);
  12462. rope_glm_f32_sycl(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  12463. } else if (is_neox) {
  12464. if (src0->type == GGML_TYPE_F32) {
  12465. rope_neox_sycl(
  12466. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12467. attn_factor, corr_dims, main_stream
  12468. );
  12469. } else if (src0->type == GGML_TYPE_F16) {
  12470. rope_neox_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd,
  12471. ne00, n_dims, nrows, pos, freq_scale, ne01,
  12472. freq_base, ext_factor, attn_factor, corr_dims,
  12473. main_stream);
  12474. } else {
  12475. GGML_ASSERT(false);
  12476. }
  12477. } else {
  12478. if (src0->type == GGML_TYPE_F32) {
  12479. rope_sycl(
  12480. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12481. attn_factor, corr_dims, main_stream
  12482. );
  12483. } else if (src0->type == GGML_TYPE_F16) {
  12484. rope_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd, ne00,
  12485. nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12486. attn_factor, corr_dims, main_stream);
  12487. } else {
  12488. GGML_ASSERT(false);
  12489. }
  12490. }
  12491. (void) src1;
  12492. (void) dst;
  12493. (void) src1_dd;
  12494. }
  12495. inline void ggml_sycl_op_alibi(const ggml_tensor *src0, const ggml_tensor *src1,
  12496. ggml_tensor *dst, const float *src0_dd,
  12497. const float *src1_dd, float *dst_dd,
  12498. const dpct::queue_ptr &main_stream) {
  12499. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12500. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12501. GGML_TENSOR_LOCALS_3(int64_t, ne0, src0, ne);
  12502. const int64_t nrows = ggml_nrows(src0);
  12503. //const int n_past = ((int32_t *) dst->op_params)[0];
  12504. const int n_head = ((int32_t *) dst->op_params)[1];
  12505. float max_bias;
  12506. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  12507. //GGML_ASSERT(ne01 + n_past == ne00);
  12508. GGML_ASSERT(n_head == ne02);
  12509. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  12510. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  12511. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  12512. alibi_f32_sycl(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  12513. (void) src1;
  12514. (void) src1_dd;
  12515. }
  12516. static void ggml_sycl_op_pool2d(const ggml_tensor *src0,
  12517. const ggml_tensor *src1, ggml_tensor *dst,
  12518. const float *src0_dd, const float *src1_dd,
  12519. float *dst_dd, const dpct::queue_ptr &main_stream) {
  12520. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12521. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12522. const int32_t * opts = (const int32_t *)dst->op_params;
  12523. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  12524. const int k0 = opts[1];
  12525. const int k1 = opts[2];
  12526. const int s0 = opts[3];
  12527. const int s1 = opts[4];
  12528. const int p0 = opts[5];
  12529. const int p1 = opts[6];
  12530. const int64_t IH = src0->ne[1];
  12531. const int64_t IW = src0->ne[0];
  12532. const int64_t N = dst->ne[3];
  12533. const int64_t OC = dst->ne[2];
  12534. const int64_t OH = dst->ne[1];
  12535. const int64_t OW = dst->ne[0];
  12536. const int parallel_elements = N * OC * OH * OW;
  12537. const int num_blocks = (parallel_elements + SYCL_POOL2D_BLOCK_SIZE - 1) / SYCL_POOL2D_BLOCK_SIZE;
  12538. sycl::range<3> block_nums(1, 1, num_blocks);
  12539. main_stream->parallel_for(
  12540. sycl::nd_range<3>(block_nums *
  12541. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  12542. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  12543. [=](sycl::nd_item<3> item_ct1) {
  12544. pool2d_nchw_kernel(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0,
  12545. parallel_elements, src0_dd, dst_dd, op,
  12546. item_ct1);
  12547. });
  12548. (void) src1;
  12549. (void) src1_dd;
  12550. }
  12551. inline void ggml_sycl_op_im2col(const ggml_tensor *src0,
  12552. const ggml_tensor *src1, ggml_tensor *dst,
  12553. const float *src0_dd, const float *src1_dd,
  12554. float *dst_dd,
  12555. const dpct::queue_ptr &main_stream) {
  12556. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12557. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12558. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  12559. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  12560. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  12561. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  12562. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  12563. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  12564. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  12565. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  12566. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  12567. const int64_t IH = is_2D ? src1->ne[1] : 1;
  12568. const int64_t IW = src1->ne[0];
  12569. const int64_t KH = is_2D ? src0->ne[1] : 1;
  12570. const int64_t KW = src0->ne[0];
  12571. const int64_t OH = is_2D ? dst->ne[2] : 1;
  12572. const int64_t OW = dst->ne[1];
  12573. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  12574. if (dst->type == GGML_TYPE_F16) {
  12575. 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);
  12576. } else {
  12577. im2col_sycl(src1_dd, (float *)dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  12578. }
  12579. (void) src0;
  12580. (void) src0_dd;
  12581. }
  12582. inline void ggml_sycl_op_sum_rows(const ggml_tensor *src0,
  12583. const ggml_tensor *src1, ggml_tensor *dst,
  12584. const float *src0_dd, const float *src1_dd,
  12585. float *dst_dd,
  12586. const dpct::queue_ptr &main_stream) {
  12587. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12588. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12589. const int64_t ncols = src0->ne[0];
  12590. const int64_t nrows = ggml_nrows(src0);
  12591. sum_rows_f32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
  12592. (void) src1;
  12593. (void) dst;
  12594. (void) src1_dd;
  12595. }
  12596. inline void ggml_sycl_op_argsort(const ggml_tensor *src0,
  12597. const ggml_tensor *src1, ggml_tensor *dst,
  12598. const float *src0_dd, const float *src1_dd,
  12599. float *dst_dd,
  12600. const dpct::queue_ptr &main_stream) {
  12601. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12602. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  12603. const int64_t ncols = src0->ne[0];
  12604. const int64_t nrows = ggml_nrows(src0);
  12605. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  12606. argsort_f32_i32_sycl(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  12607. (void) src1;
  12608. (void) dst;
  12609. (void) src1_dd;
  12610. }
  12611. inline void ggml_sycl_op_diag_mask_inf(const ggml_tensor *src0,
  12612. const ggml_tensor *src1,
  12613. ggml_tensor *dst, const float *src0_dd,
  12614. const float *src1_dd, float *dst_dd,
  12615. const dpct::queue_ptr &main_stream) {
  12616. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12617. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12618. const int64_t ne00 = src0->ne[0];
  12619. const int64_t ne01 = src0->ne[1];
  12620. const int nrows0 = ggml_nrows(src0);
  12621. const int n_past = ((int32_t *) dst->op_params)[0];
  12622. diag_mask_inf_f32_sycl(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  12623. (void) src1;
  12624. (void) dst;
  12625. (void) src1_dd;
  12626. }
  12627. inline void ggml_sycl_op_soft_max(const ggml_tensor *src0,
  12628. const ggml_tensor *src1, ggml_tensor *dst,
  12629. const float *src0_dd, const float *src1_dd,
  12630. float *dst_dd,
  12631. const dpct::queue_ptr &main_stream) {
  12632. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12633. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12634. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  12635. const int64_t ne00 = src0->ne[0];
  12636. const int64_t nrows_x = ggml_nrows(src0);
  12637. const int64_t nrows_y = src0->ne[1];
  12638. float scale = 1.0f;
  12639. float max_bias = 0.0f;
  12640. memcpy(&scale, dst->op_params + 0, sizeof(float));
  12641. memcpy(&max_bias, dst->op_params + 1, sizeof(float));
  12642. // positions tensor
  12643. float * src2_dd = nullptr;
  12644. sycl_pool_alloc<float> src2_f;
  12645. ggml_tensor * src2 = dst->src[2];
  12646. const bool use_src2 = src2 != nullptr;
  12647. if (use_src2) {
  12648. const bool src2_on_device = src2->backend == GGML_BACKEND_TYPE_GPU;
  12649. if (src2_on_device) {
  12650. ggml_tensor_extra_gpu * src2_extra = (ggml_tensor_extra_gpu *) src2->extra;
  12651. src2_dd = (float *) src2_extra->data_device[g_main_device];
  12652. } else {
  12653. src2_dd = src2_f.alloc(ggml_nelements(src2));
  12654. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src2_dd, src2, 0, 0, 0, 1, main_stream));
  12655. }
  12656. }
  12657. soft_max_f32_sycl(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00,
  12658. nrows_x, nrows_y, scale, max_bias, main_stream);
  12659. }
  12660. inline void ggml_sycl_op_scale(const ggml_tensor *src0, const ggml_tensor *src1,
  12661. ggml_tensor *dst, const float *src0_dd,
  12662. const float *src1_dd, float *dst_dd,
  12663. const dpct::queue_ptr &main_stream) {
  12664. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12665. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12666. float scale;
  12667. memcpy(&scale, dst->op_params, sizeof(float));
  12668. scale_f32_sycl(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  12669. /*
  12670. DPCT1010:87: SYCL uses exceptions to report errors and does not use the
  12671. error codes. The call was replaced with 0. You need to rewrite this code.
  12672. */
  12673. SYCL_CHECK(0);
  12674. (void) src1;
  12675. (void) dst;
  12676. (void) src1_dd;
  12677. }
  12678. inline void ggml_sycl_op_clamp(const ggml_tensor *src0, const ggml_tensor *src1,
  12679. ggml_tensor *dst, const float *src0_dd,
  12680. const float *src1_dd, float *dst_dd,
  12681. const dpct::queue_ptr &main_stream) {
  12682. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12683. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12684. float min;
  12685. float max;
  12686. memcpy(&min, dst->op_params, sizeof(float));
  12687. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  12688. clamp_f32_sycl(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  12689. /*
  12690. DPCT1010:88: SYCL uses exceptions to report errors and does not use the
  12691. error codes. The call was replaced with 0. You need to rewrite this code.
  12692. */
  12693. SYCL_CHECK(0);
  12694. (void) src1;
  12695. (void) dst;
  12696. (void) src1_dd;
  12697. }
  12698. static void ggml_sycl_op_flatten(const ggml_tensor *src0,
  12699. const ggml_tensor *src1, ggml_tensor *dst,
  12700. const ggml_sycl_op_flatten_t op) try {
  12701. const int64_t nrows0 = ggml_nrows(src0);
  12702. const bool use_src1 = src1 != nullptr;
  12703. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  12704. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12705. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12706. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12707. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  12708. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12709. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12710. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_TYPE_GPU;
  12711. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  12712. // dd = data device
  12713. float * src0_ddf = nullptr;
  12714. float * src1_ddf = nullptr;
  12715. float * dst_ddf = nullptr;
  12716. sycl_pool_alloc<float> src0_f;
  12717. sycl_pool_alloc<float> src1_f;
  12718. sycl_pool_alloc<float> dst_f;
  12719. ggml_sycl_set_device(g_main_device);
  12720. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12721. // GGML_SYCL_DEBUG("g_main_device=%d, main_stream=%p src0_on_device=%d, src1_on_device=%d, dst_on_device=%d\n",
  12722. // g_main_device, main_stream, src0_on_device, src1_on_device, dst_on_device);
  12723. if (src0_on_device) {
  12724. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  12725. } else {
  12726. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  12727. // GGML_SYCL_DEBUG("before ggml_sycl_cpy_tensor_2d src0_ddf=%p, src0=%p\n", src0_ddf, src0);
  12728. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  12729. }
  12730. if (use_src1) {
  12731. if (src1_on_device) {
  12732. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12733. } else {
  12734. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  12735. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  12736. }
  12737. }
  12738. if (dst_on_device) {
  12739. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12740. } else {
  12741. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  12742. }
  12743. // GGML_SYCL_DEBUG("op src0=%p, src1=%p, dst=%p, src0_ddf=%p, src1_ddf=%p, dst_ddf=%p, main_stream=%p\n",
  12744. // src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  12745. // do the computation
  12746. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  12747. /*
  12748. DPCT1010:89: SYCL uses exceptions to report errors and does not use the
  12749. error codes. The call was replaced with 0. You need to rewrite this code.
  12750. */
  12751. SYCL_CHECK(0);
  12752. // copy dst to host if necessary
  12753. if (!dst_on_device) {
  12754. SYCL_CHECK(CHECK_TRY_ERROR(
  12755. main_stream->memcpy(dst->data, dst_ddf, ggml_nbytes(dst)).wait()));
  12756. }
  12757. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12758. SYCL_CHECK(CHECK_TRY_ERROR(
  12759. dpct::get_current_device().queues_wait_and_throw()));
  12760. }
  12761. // print_ggml_tensor("tensor", dst);
  12762. }
  12763. catch (sycl::exception const &exc) {
  12764. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12765. << ", line:" << __LINE__ << std::endl;
  12766. std::exit(1);
  12767. }
  12768. static void ggml_sycl_set_peer_access(const int n_tokens) {
  12769. static bool peer_access_enabled = false;
  12770. const bool enable_peer_access = n_tokens <= GGML_SYCL_PEER_MAX_BATCH_SIZE;
  12771. if (peer_access_enabled == enable_peer_access) {
  12772. return;
  12773. }
  12774. #ifdef NDEBUG
  12775. for (int i = 0; i < g_device_count; ++i) {
  12776. SYCL_CHECK(ggml_sycl_set_device(i));
  12777. // SYCL_CHECK(syclDeviceSynchronize());
  12778. }
  12779. for (int i = 0; i < g_device_count; ++i) {
  12780. SYCL_CHECK(ggml_sycl_set_device(i));
  12781. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  12782. if (i == id_other) {
  12783. continue;
  12784. }
  12785. if (i != g_main_device && id_other != g_main_device) {
  12786. continue;
  12787. }
  12788. // int can_access_peer;
  12789. // SYCL_CHECK(syclDeviceCanAccessPeer(&can_access_peer, id, id_other));
  12790. // if (can_access_peer) {
  12791. // if (enable_peer_access) {
  12792. // SYCL_CHECK(syclDeviceEnablePeerAccess(id_other, 0));
  12793. // } else {
  12794. // SYCL_CHECK(syclDeviceDisablePeerAccess(id_other));
  12795. // }
  12796. // }
  12797. }
  12798. }
  12799. #endif // NDEBUG
  12800. peer_access_enabled = enable_peer_access;
  12801. }
  12802. struct ggml_backend_sycl_split_buffer_type_context {
  12803. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  12804. };
  12805. static void ggml_sycl_op_mul_mat(const ggml_tensor *src0,
  12806. const ggml_tensor *src1, ggml_tensor *dst,
  12807. ggml_sycl_op_mul_mat_t op,
  12808. const bool convert_src1_to_q8_1) try {
  12809. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne);
  12810. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  12811. const int64_t nrows1 = ggml_nrows(src1);
  12812. GGML_ASSERT(ne03 == ne13);
  12813. const int64_t ne0 = dst->ne[0];
  12814. const int64_t ne1 = dst->ne[1];
  12815. const int nb2 = dst->nb[2];
  12816. const int nb3 = dst->nb[3];
  12817. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12818. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12819. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  12820. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  12821. const int64_t i02_divisor = ne12 / ne02;
  12822. const size_t src0_ts = ggml_type_size(src0->type);
  12823. const size_t src0_bs = ggml_blck_size(src0->type);
  12824. const size_t q8_1_ts = sizeof(block_q8_1);
  12825. const size_t q8_1_bs = QK8_1;
  12826. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12827. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12828. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12829. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12830. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  12831. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  12832. int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  12833. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12834. GGML_ASSERT(!(split && ne02 > 1));
  12835. GGML_ASSERT(!(split && ne03 > 1));
  12836. GGML_ASSERT(!(split && ne02 < ne12));
  12837. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  12838. if (split) {
  12839. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  12840. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  12841. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
  12842. tensor_split = buft_ctx->tensor_split;
  12843. }
  12844. struct dev_data {
  12845. sycl_pool_alloc<char> src0_dd_alloc;
  12846. sycl_pool_alloc<float> src1_ddf_alloc;
  12847. sycl_pool_alloc<char> src1_ddq_alloc;
  12848. sycl_pool_alloc<float> dst_dd_alloc;
  12849. char *src0_dd = nullptr;
  12850. float *src1_ddf = nullptr; // float
  12851. char *src1_ddq = nullptr; // q8_1
  12852. float *dst_dd = nullptr;
  12853. int64_t row_low;
  12854. int64_t row_high;
  12855. };
  12856. dev_data dev[GGML_SYCL_MAX_DEVICES];
  12857. int used_devices = 0;
  12858. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12859. for (int i = 0; i < g_device_count; ++i) {
  12860. // by default, use all rows
  12861. dev[i].row_low = 0;
  12862. dev[i].row_high = ne01;
  12863. // for multi GPU, get the row boundaries from tensor split
  12864. // and round to mul_mat_q tile sizes
  12865. if (split) {
  12866. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  12867. if (i != 0) {
  12868. dev[i].row_low = ne01*tensor_split[i];
  12869. if (dev[i].row_low < ne01) {
  12870. dev[i].row_low -= dev[i].row_low % rounding;
  12871. }
  12872. }
  12873. if (i != g_device_count - 1) {
  12874. dev[i].row_high = ne01*tensor_split[i + 1];
  12875. if (dev[i].row_high < ne01) {
  12876. dev[i].row_high -= dev[i].row_high % rounding;
  12877. }
  12878. }
  12879. }
  12880. }
  12881. for (int i = 0; i < g_device_count; ++i) {
  12882. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  12883. continue;
  12884. }
  12885. used_devices++;
  12886. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12887. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12888. ggml_sycl_set_device(i);
  12889. dpct::queue_ptr stream = g_syclStreams[i][0];
  12890. if (src0_on_device && src0_is_contiguous) {
  12891. dev[i].src0_dd = (char *) src0_extra->data_device[i];
  12892. } else {
  12893. dev[i].src0_dd = dev[i].src0_dd_alloc.alloc(ggml_nbytes(src0));
  12894. }
  12895. if (src1_on_device && src1_is_contiguous) {
  12896. dev[i].src1_ddf = (float *) src1_extra->data_device[i];
  12897. } else {
  12898. dev[i].src1_ddf = dev[i].src1_ddf_alloc.alloc(ggml_nelements(src1));
  12899. }
  12900. if (convert_src1_to_q8_1) {
  12901. dev[i].src1_ddq = dev[i].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  12902. if (src1_on_device && src1_is_contiguous) {
  12903. quantize_row_q8_1_sycl(dev[i].src1_ddf, dev[i].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  12904. /*
  12905. DPCT1010:90: SYCL uses exceptions to report errors and does not
  12906. use the error codes. The call was replaced with 0. You need to
  12907. rewrite this code.
  12908. */
  12909. SYCL_CHECK(0);
  12910. }
  12911. }
  12912. if (dst_on_device) {
  12913. dev[i].dst_dd = (float *) dst_extra->data_device[i];
  12914. } else {
  12915. const size_t size_dst_ddf = split ? (dev[i].row_high - dev[i].row_low)*ne1 : ggml_nelements(dst);
  12916. dev[i].dst_dd = dev[i].dst_dd_alloc.alloc(size_dst_ddf);
  12917. }
  12918. }
  12919. // if multiple devices are used they need to wait for the main device
  12920. // here an event is recorded that signals that the main device has finished calculating the input data
  12921. if (split && used_devices > 1) {
  12922. ggml_sycl_set_device(g_main_device);
  12923. /*
  12924. DPCT1024:91: The original code returned the error code that was further
  12925. consumed by the program logic. This original code was replaced with 0.
  12926. You may need to rewrite the program logic consuming the error code.
  12927. */
  12928. SYCL_CHECK(CHECK_TRY_ERROR(
  12929. *src0_extra->events[g_main_device][0] =
  12930. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier()));
  12931. }
  12932. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  12933. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  12934. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  12935. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  12936. for (int i = 0; i < g_device_count; ++i) {
  12937. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  12938. continue;
  12939. }
  12940. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12941. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12942. const int64_t row_diff = dev[i].row_high - dev[i].row_low;
  12943. ggml_sycl_set_device(i);
  12944. dpct::queue_ptr stream = g_syclStreams[i][is];
  12945. // wait for main GPU data if necessary
  12946. if (split && (i != g_main_device || is != 0)) {
  12947. /*
  12948. DPCT1009:163: SYCL uses exceptions to report errors and does not
  12949. use the error codes. The original code was commented out and a
  12950. warning string was inserted. You need to rewrite this code.
  12951. */
  12952. SYCL_CHECK(CHECK_TRY_ERROR(stream->ext_oneapi_submit_barrier(
  12953. {*src0_extra->events[g_main_device][0]})));
  12954. }
  12955. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  12956. const int64_t i03 = i0 / ne12;
  12957. const int64_t i02 = i0 % ne12;
  12958. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  12959. // for split tensors the data begins at i0 == i0_offset_low
  12960. char * src0_dd_i = dev[i].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  12961. float * src1_ddf_i = dev[i].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  12962. char * src1_ddq_i = dev[i].src1_ddq + src1_ddq_i_offset;
  12963. float * dst_dd_i = dev[i].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  12964. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  12965. // in that case an offset on dst_ddf_i is needed
  12966. if (dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device) {
  12967. dst_dd_i += dev[i].row_low; // offset is 0 if no tensor split
  12968. }
  12969. // copy src0, src1 to device if necessary
  12970. if (src1->backend == GGML_BACKEND_TYPE_GPU && src1_is_contiguous) {
  12971. if (i != g_main_device) {
  12972. if (convert_src1_to_q8_1) {
  12973. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  12974. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  12975. src1_ddq_i, src1_ddq_i_source,
  12976. src1_ncols * src1_padded_col_size * q8_1_ts /
  12977. q8_1_bs).wait()));
  12978. } else {
  12979. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  12980. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  12981. SYCL_CHECK(CHECK_TRY_ERROR(dev2dev_memcpy(*stream, *main_stream,
  12982. src1_ddf_i, src1_ddf_i_source,
  12983. src1_ncols * ne10 * sizeof(float))));
  12984. }
  12985. }
  12986. } else if (src1->backend == GGML_BACKEND_TYPE_CPU || (src1_on_device && !src1_is_contiguous)) {
  12987. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(
  12988. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  12989. } else {
  12990. GGML_ASSERT(false);
  12991. }
  12992. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_TYPE_CPU || !src1_is_contiguous)) {
  12993. quantize_row_q8_1_sycl(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  12994. /*
  12995. DPCT1010:92: SYCL uses exceptions to report errors and does
  12996. not use the error codes. The call was replaced with 0. You
  12997. need to rewrite this code.
  12998. */
  12999. SYCL_CHECK(0);
  13000. }
  13001. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  13002. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[i].row_low, dev[i].row_high, stream));
  13003. }
  13004. if (src1->type == GGML_TYPE_F16) {
  13005. src1_padded_col_size = (i0 * ne11 + src1_col_0) * ne10;
  13006. }
  13007. // do the computation
  13008. SYCL_CHECK(CHECK_TRY_ERROR(op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  13009. dev[i].row_low, dev[i].row_high, src1_ncols, src1_padded_col_size, stream)));
  13010. /*
  13011. DPCT1010:93: SYCL uses exceptions to report errors and does not
  13012. use the error codes. The call was replaced with 0. You need to
  13013. rewrite this code.
  13014. */
  13015. SYCL_CHECK(0);
  13016. // copy dst to host or other device if necessary
  13017. if (!dst_on_device) {
  13018. void * dst_off_device;
  13019. dpct::memcpy_direction kind;
  13020. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  13021. dst_off_device = dst->data;
  13022. kind = dpct::device_to_host;
  13023. } else if (dst->backend == GGML_BACKEND_TYPE_GPU) {
  13024. dst_off_device = dst_extra->data_device[g_main_device];
  13025. kind = dpct::device_to_device;
  13026. } else {
  13027. GGML_ASSERT(false);
  13028. }
  13029. if (split) {
  13030. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  13031. // dst is NOT transposed.
  13032. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  13033. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  13034. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  13035. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  13036. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  13037. dhf_dst_i += src1_col_0*ne0 + dev[i].row_low;
  13038. //todo, dirty solution. Need be updated when device2device memcpy() is supported.
  13039. if (kind == dpct::device_to_device) {
  13040. size_t dst_size = ggml_nbytes_pad(dst);
  13041. float *host_buf = (float *)malloc(dst_size);
  13042. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  13043. host_buf, ne0 * sizeof(float), dst_dd_i,
  13044. row_diff * sizeof(float), row_diff * sizeof(float),
  13045. src1_ncols, dpct::device_to_host, *stream)));
  13046. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[i]).queues_wait_and_throw();
  13047. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  13048. dhf_dst_i, ne0 * sizeof(float), host_buf,
  13049. row_diff * sizeof(float), row_diff * sizeof(float),
  13050. src1_ncols, dpct::host_to_device, *main_stream)));
  13051. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[g_main_device]).queues_wait_and_throw();
  13052. free(host_buf);
  13053. } else {
  13054. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  13055. dhf_dst_i, ne0 * sizeof(float), dst_dd_i,
  13056. row_diff * sizeof(float), row_diff * sizeof(float),
  13057. src1_ncols, kind, *stream)));
  13058. }
  13059. } else {
  13060. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  13061. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  13062. dhf_dst_i += src1_col_0*ne0;
  13063. SYCL_CHECK(CHECK_TRY_ERROR(
  13064. stream->memcpy(dhf_dst_i, dst_dd_i,
  13065. src1_ncols * ne0 * sizeof(float)).wait()));
  13066. }
  13067. }
  13068. // add event for the main device to wait on until other device is done
  13069. if (split && (i != g_main_device || is != 0)) {
  13070. /*
  13071. DPCT1024:94: The original code returned the error code that
  13072. was further consumed by the program logic. This original
  13073. code was replaced with 0. You may need to rewrite the
  13074. program logic consuming the error code.
  13075. */
  13076. SYCL_CHECK(CHECK_TRY_ERROR(
  13077. *src0_extra->events[i][is] =
  13078. stream->ext_oneapi_submit_barrier()));
  13079. }
  13080. }
  13081. }
  13082. }
  13083. // main device waits for all other devices to be finished
  13084. if (split && g_device_count > 1) {
  13085. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  13086. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  13087. ggml_sycl_set_device(g_main_device);
  13088. for (int i = 0; i < g_device_count; ++i) {
  13089. if (dev[i].row_low == dev[i].row_high) {
  13090. continue;
  13091. }
  13092. for (int64_t is = 0; is < is_max; ++is) {
  13093. SYCL_CHECK(CHECK_TRY_ERROR(
  13094. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier(
  13095. {*src0_extra->events[i][is]})));
  13096. }
  13097. }
  13098. }
  13099. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  13100. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13101. SYCL_CHECK(CHECK_TRY_ERROR(
  13102. dpct::get_current_device().queues_wait_and_throw()));
  13103. }
  13104. }
  13105. catch (sycl::exception const &exc) {
  13106. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13107. << ", line:" << __LINE__ << std::endl;
  13108. std::exit(1);
  13109. }
  13110. static void ggml_sycl_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13111. GGML_SYCL_DEBUG("call %s\n", __func__);
  13112. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_repeat);
  13113. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13114. }
  13115. static void ggml_sycl_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13116. GGML_SYCL_DEBUG("call %s\n", __func__);
  13117. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_get_rows);
  13118. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13119. }
  13120. static void ggml_sycl_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13121. GGML_SYCL_DEBUG("call %s\n", __func__);
  13122. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_add);
  13123. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13124. }
  13125. static void ggml_sycl_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13126. GGML_SYCL_DEBUG("call %s\n", __func__);
  13127. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_acc);
  13128. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13129. }
  13130. static void ggml_sycl_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13131. GGML_SYCL_DEBUG("call %s\n", __func__);
  13132. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_mul);
  13133. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13134. }
  13135. static void ggml_sycl_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13136. GGML_SYCL_DEBUG("call %s\n", __func__);
  13137. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_div);
  13138. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13139. }
  13140. static void ggml_sycl_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13141. GGML_SYCL_DEBUG("call %s\n", __func__);
  13142. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu);
  13143. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13144. }
  13145. static void ggml_sycl_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13146. GGML_SYCL_DEBUG("call %s\n", __func__);
  13147. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_silu);
  13148. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13149. }
  13150. static void ggml_sycl_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13151. GGML_SYCL_DEBUG("call %s\n", __func__);
  13152. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu_quick);
  13153. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13154. }
  13155. static void ggml_sycl_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13156. GGML_SYCL_DEBUG("call %s\n", __func__);
  13157. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_tanh);
  13158. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13159. }
  13160. static void ggml_sycl_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13161. GGML_SYCL_DEBUG("call %s\n", __func__);
  13162. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_relu);
  13163. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13164. }
  13165. static void ggml_sycl_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13166. GGML_SYCL_DEBUG("call %s\n", __func__);
  13167. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardsigmoid);
  13168. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13169. }
  13170. static void ggml_sycl_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13171. GGML_SYCL_DEBUG("call %s\n", __func__);
  13172. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardswish);
  13173. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13174. }
  13175. static void ggml_sycl_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13176. GGML_SYCL_DEBUG("call %s\n", __func__);
  13177. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_leaky_relu);
  13178. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13179. }
  13180. static void ggml_sycl_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13181. GGML_SYCL_DEBUG("call %s\n", __func__);
  13182. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sqr);
  13183. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13184. }
  13185. static void ggml_sycl_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13186. GGML_SYCL_DEBUG("call %s\n", __func__);
  13187. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_norm);
  13188. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13189. }
  13190. static void ggml_sycl_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13191. GGML_SYCL_DEBUG("call %s\n", __func__);
  13192. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_group_norm);
  13193. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13194. }
  13195. static void ggml_sycl_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13196. GGML_SYCL_DEBUG("call %s\n", __func__);
  13197. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_concat);
  13198. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13199. }
  13200. static void ggml_sycl_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13201. GGML_SYCL_DEBUG("call %s\n", __func__);
  13202. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_upscale);
  13203. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13204. }
  13205. static void ggml_sycl_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13206. GGML_SYCL_DEBUG("call %s\n", __func__);
  13207. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pad);
  13208. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13209. }
  13210. static void ggml_sycl_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13211. GGML_SYCL_DEBUG("call %s\n", __func__);
  13212. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rms_norm);
  13213. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13214. }
  13215. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  13216. if (!g_sycl_loaded) return false;
  13217. const int64_t ne10 = src1->ne[0];
  13218. const int64_t ne0 = dst->ne[0];
  13219. const int64_t ne1 = dst->ne[1];
  13220. // TODO: find the optimal values for these
  13221. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  13222. src1->type == GGML_TYPE_F32 &&
  13223. dst->type == GGML_TYPE_F32 &&
  13224. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  13225. }
  13226. static void ggml_sycl_mul_mat_vec_p021(const ggml_tensor *src0,
  13227. const ggml_tensor *src1,
  13228. ggml_tensor *dst) try {
  13229. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  13230. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13231. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  13232. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  13233. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  13234. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  13235. const int64_t ne00 = src0->ne[0];
  13236. const int64_t ne01 = src0->ne[1];
  13237. const int64_t ne02 = src0->ne[2];
  13238. const int64_t ne12 = src1->ne[2];
  13239. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13240. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13241. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13242. void * src0_ddq = src0_extra->data_device[g_main_device];
  13243. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13244. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13245. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13246. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13247. ggml_mul_mat_p021_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  13248. }
  13249. catch (sycl::exception const &exc) {
  13250. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13251. << ", line:" << __LINE__ << std::endl;
  13252. std::exit(1);
  13253. }
  13254. static void ggml_sycl_mul_mat_vec_nc(const ggml_tensor *src0,
  13255. const ggml_tensor *src1,
  13256. ggml_tensor *dst) try {
  13257. GGML_ASSERT(!ggml_is_transposed(src0));
  13258. GGML_ASSERT(!ggml_is_transposed(src1));
  13259. GGML_ASSERT(!ggml_is_permuted(src0));
  13260. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13261. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  13262. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  13263. const int64_t ne00 = src0->ne[0];
  13264. const int64_t ne01 = src0->ne[1];
  13265. const int64_t ne02 = src0->ne[2];
  13266. const int64_t nb01 = src0->nb[1];
  13267. const int64_t nb02 = src0->nb[2];
  13268. const int64_t ne12 = src1->ne[2];
  13269. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13270. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13271. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13272. void * src0_ddq = src0_extra->data_device[g_main_device];
  13273. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13274. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13275. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13276. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13277. const int64_t row_stride_x = nb01 / sizeof(sycl::half);
  13278. const int64_t channel_stride_x = nb02 / sizeof(sycl::half);
  13279. 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);
  13280. }
  13281. catch (sycl::exception const &exc) {
  13282. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13283. << ", line:" << __LINE__ << std::endl;
  13284. std::exit(1);
  13285. }
  13286. static void k_compute_batched_ptrs(const sycl::half *src0_as_f16,
  13287. const sycl::half *src1_as_f16, char *dst,
  13288. const void **ptrs_src, void **ptrs_dst,
  13289. int64_t ne12, int64_t ne13, int64_t ne23,
  13290. size_t nb02, size_t nb03, size_t nb12,
  13291. size_t nb13, size_t nbd2, size_t nbd3,
  13292. int64_t r2, int64_t r3,
  13293. const sycl::nd_item<3> &item_ct1) {
  13294. int64_t i13 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  13295. item_ct1.get_local_id(2);
  13296. int64_t i12 = item_ct1.get_group(1) * item_ct1.get_local_range(1) +
  13297. item_ct1.get_local_id(1);
  13298. if (i13 >= ne13 || i12 >= ne12) {
  13299. return;
  13300. }
  13301. int64_t i03 = i13 / r3;
  13302. int64_t i02 = i12 / r2;
  13303. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  13304. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  13305. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  13306. }
  13307. static void ggml_sycl_mul_mat_batched_sycl(const ggml_tensor *src0,
  13308. const ggml_tensor *src1,
  13309. ggml_tensor *dst) try {
  13310. GGML_ASSERT(!ggml_is_transposed(src0));
  13311. GGML_ASSERT(!ggml_is_transposed(src1));
  13312. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13313. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  13314. GGML_TENSOR_BINARY_OP_LOCALS
  13315. const int64_t ne_dst = ggml_nelements(dst);
  13316. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13317. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13318. bool no_mixed_dtypes = main_stream->get_backend() == sycl::backend::ext_oneapi_cuda ||
  13319. main_stream->get_backend() == sycl::backend::ext_oneapi_hip;
  13320. SYCL_CHECK(
  13321. CHECK_TRY_ERROR(g_sycl_handles[g_main_device] = main_stream));
  13322. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13323. void * src0_ddq = src0_extra->data_device[g_main_device];
  13324. sycl::half *src0_as_f16 = (sycl::half *)src0_ddq;
  13325. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13326. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13327. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13328. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13329. // convert src1 to fp16
  13330. sycl_pool_alloc<sycl::half> src1_f16_alloc;
  13331. if (src1->type != GGML_TYPE_F16) {
  13332. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  13333. const int64_t ne_src1 = ggml_nelements(src1);
  13334. src1_f16_alloc.alloc(ne_src1);
  13335. GGML_ASSERT(to_fp16_sycl != nullptr);
  13336. to_fp16_sycl(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  13337. }
  13338. sycl::half *src1_f16 = src1->type == GGML_TYPE_F16 ? (sycl::half *)src1_ddf
  13339. : src1_f16_alloc.get();
  13340. sycl_pool_alloc<sycl::half> dst_f16;
  13341. char * dst_t;
  13342. dpct::library_data_t cu_compute_type = dpct::library_data_t::real_float;
  13343. dpct::library_data_t cu_data_type = dpct::library_data_t::real_float;
  13344. if (no_mixed_dtypes) {
  13345. cu_compute_type = dpct::library_data_t::real_half;
  13346. cu_data_type = dpct::library_data_t::real_half;
  13347. }
  13348. // dst strides
  13349. size_t nbd2 = dst->nb[2];
  13350. size_t nbd3 = dst->nb[3];
  13351. const float alpha_f32 = 1.0f;
  13352. const float beta_f32 = 0.0f;
  13353. const sycl::half alpha_f16 = 1.0f;
  13354. const sycl::half beta_f16 = 0.0f;
  13355. const void * alpha = &alpha_f32;
  13356. const void * beta = &beta_f32;
  13357. if (no_mixed_dtypes) {
  13358. alpha = &alpha_f16;
  13359. beta = &beta_f16;
  13360. }
  13361. // TODO: Renable (dst->op_params[0] =! GGML_PREC_DEFAULT) pathway
  13362. // when oneMKL open source supports half, half, float, float: datatypes
  13363. dst_t = (char *) dst_ddf;
  13364. if (no_mixed_dtypes) {
  13365. dst_t = (char *) dst_f16.alloc(ne_dst);
  13366. nbd2 /= sizeof(float) / sizeof(sycl::half);
  13367. nbd3 /= sizeof(float) / sizeof(sycl::half);
  13368. }
  13369. GGML_ASSERT(ne12 % ne02 == 0);
  13370. GGML_ASSERT(ne13 % ne03 == 0);
  13371. // broadcast factors
  13372. const int64_t r2 = ne12/ne02;
  13373. const int64_t r3 = ne13/ne03;
  13374. #if 0
  13375. // use syclGemmEx
  13376. {
  13377. for (int i13 = 0; i13 < ne13; ++i13) {
  13378. for (int i12 = 0; i12 < ne12; ++i12) {
  13379. int i03 = i13 / r3;
  13380. int i02 = i12 / r2;
  13381. SYCL_CHECK(
  13382. syclGemmEx(g_sycl_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  13383. ne01, ne11, ne10,
  13384. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , SYCL_R_16F, nb01/sizeof(half),
  13385. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, SYCL_R_16F, nb11/sizeof(float),
  13386. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  13387. cu_compute_type,
  13388. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  13389. }
  13390. }
  13391. }
  13392. #else
  13393. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  13394. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  13395. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  13396. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  13397. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  13398. (const char *)src0_as_f16, dpct::library_data_t::real_half,
  13399. nb01 / nb00, nb02 / nb00,
  13400. (const char *)src1_f16, dpct::library_data_t::real_half,
  13401. nb11 / nb10, nb12 / nb10, beta,
  13402. (char *)dst_t, cu_data_type, ne01, nb2 / nb0,
  13403. ne12 * ne13, cu_compute_type)));
  13404. g_sycl_handles[g_main_device]->wait();
  13405. } else {
  13406. const int ne23 = ne12*ne13;
  13407. sycl_pool_alloc<const void *> ptrs_src(2*ne23);
  13408. sycl_pool_alloc< void *> ptrs_dst(1*ne23);
  13409. sycl::range<3> block_dims(1, ne12, ne13);
  13410. /*
  13411. DPCT1049:47: The work-group size passed to the SYCL kernel may exceed
  13412. the limit. To get the device limit, query
  13413. info::device::max_work_group_size. Adjust the work-group size if needed.
  13414. */
  13415. {
  13416. dpct::has_capability_or_fail(main_stream->get_device(),
  13417. {sycl::aspect::fp16});
  13418. main_stream->submit([&](sycl::handler &cgh) {
  13419. const void **ptrs_src_get = ptrs_src.get();
  13420. void **ptrs_dst_get = ptrs_dst.get();
  13421. size_t nb12_scaled = src1->type == GGML_TYPE_F16 ? nb12 : nb12 / 2;
  13422. size_t nb13_scaled = src1->type == GGML_TYPE_F16 ? nb13 : nb13 / 2;
  13423. cgh.parallel_for(sycl::nd_range<3>(block_dims, block_dims),
  13424. [=](sycl::nd_item<3> item_ct1) {
  13425. k_compute_batched_ptrs(
  13426. src0_as_f16, src1_f16,
  13427. dst_t, ptrs_src_get,
  13428. ptrs_dst_get, ne12, ne13, ne23,
  13429. nb02, nb03, nb12_scaled, nb13_scaled,
  13430. nbd2, nbd3, r2, r3, item_ct1);
  13431. });
  13432. }).wait();
  13433. }
  13434. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  13435. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  13436. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  13437. (const void **)(ptrs_src.get() + 0 * ne23),
  13438. dpct::library_data_t::real_half, nb01 / nb00,
  13439. (const void **)(ptrs_src.get() + 1 * ne23),
  13440. dpct::library_data_t::real_half, nb11 / nb10, beta,
  13441. (void **)(ptrs_dst.get() + 0 * ne23), cu_data_type, ne01, ne23,
  13442. cu_compute_type)));
  13443. g_sycl_handles[g_main_device]->wait();
  13444. }
  13445. #endif
  13446. if (no_mixed_dtypes) {
  13447. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  13448. to_fp32_sycl(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  13449. }
  13450. }
  13451. catch (sycl::exception const &exc) {
  13452. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13453. << ", line:" << __LINE__ << std::endl;
  13454. std::exit(1);
  13455. }
  13456. static void ggml_sycl_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13457. const bool all_on_device =
  13458. (src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT) &&
  13459. (src1->backend == GGML_BACKEND_TYPE_GPU) &&
  13460. ( dst->backend == GGML_BACKEND_TYPE_GPU);
  13461. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  13462. int64_t min_compute_capability = INT_MAX;
  13463. for (int i = 0; i < g_device_count; ++i) {
  13464. 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)) {
  13465. min_compute_capability = g_device_caps[i].cc;
  13466. }
  13467. }
  13468. #ifdef SYCL_USE_XMX
  13469. const bool use_xmx = true;
  13470. #else
  13471. const bool use_xmx = false;
  13472. #endif
  13473. // debug helpers
  13474. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  13475. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  13476. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  13477. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  13478. //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);
  13479. //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);
  13480. if (!split && all_on_device && !use_xmx && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  13481. // KQ single-batch
  13482. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_p021\n");
  13483. ggml_sycl_mul_mat_vec_p021(src0, src1, dst);
  13484. } 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) {
  13485. // KQV single-batch
  13486. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_nc\n");
  13487. ggml_sycl_mul_mat_vec_nc(src0, src1, dst);
  13488. } else if (!split && all_on_device && use_xmx && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  13489. // KQ + KQV multi-batch
  13490. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_batched_sycl\n");
  13491. ggml_sycl_mul_mat_batched_sycl(src0, src1, dst);
  13492. } else if (src0->type == GGML_TYPE_F32) {
  13493. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat\n");
  13494. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  13495. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  13496. // GGML_SYCL_DEBUG("ggml_is_quantized or GGML_TYPE_F16\n");
  13497. if (src1->ne[1] == 1 && src0->ne[0] % GGML_SYCL_DMMV_X == 0) {
  13498. #ifdef GGML_SYCL_FORCE_DMMV
  13499. const bool use_mul_mat_vec_q = false;
  13500. #else
  13501. bool use_mul_mat_vec_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type);
  13502. use_mul_mat_vec_q = use_mul_mat_vec_q ||
  13503. (src0->type == GGML_TYPE_IQ2_XXS) || (src0->type == GGML_TYPE_IQ2_XS) || (src0->type == GGML_TYPE_IQ2_S) ||
  13504. (src0->type == GGML_TYPE_IQ3_XXS) || (src0->type == GGML_TYPE_IQ3_S) ||
  13505. (src0->type == GGML_TYPE_IQ4_NL) || (src0->type == GGML_TYPE_IQ4_XS) ||
  13506. (src0->type == GGML_TYPE_IQ1_S) || (src0->type == GGML_TYPE_IQ1_M);
  13507. #endif // GGML_SYCL_FORCE_DMMV
  13508. if (use_mul_mat_vec_q) {
  13509. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_vec_q path\n");
  13510. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_vec_q, true);
  13511. } else {
  13512. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_dequantize_mul_mat_vec path\n");
  13513. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_dequantize_mul_mat_vec, false);
  13514. }
  13515. } else {
  13516. bool use_mul_mat_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type);
  13517. if (use_xmx && min_compute_capability >= VER_GEN9 && src1->ne[1] > XMX_MAX_BATCH_SIZE) {
  13518. use_mul_mat_q = false;
  13519. }
  13520. if (use_mul_mat_q) {
  13521. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_q path\n");
  13522. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_q, true);
  13523. } else {
  13524. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_sycl path\n");
  13525. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  13526. }
  13527. }
  13528. } else {
  13529. GGML_ASSERT(false);
  13530. }
  13531. }
  13532. #if 0
  13533. template<typename ... Srcs>
  13534. static __global__ void k_compute_batched_ptrs_id(
  13535. const void ** ptrs_src, void ** ptrs_dst,
  13536. int ne12, int ne13,
  13537. int ne23,
  13538. int nb02, int nb03,
  13539. int nb12, int nb13,
  13540. int nb2, int nb3,
  13541. int r2, int r3,
  13542. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  13543. const half * src1_f16, half * dst_f16,
  13544. const int32_t * ids, const int id,
  13545. Srcs... src0s) {
  13546. int i = ids[id];
  13547. half * src0_f16;
  13548. const void * srcs_ar[] = { (const half *) src0s... };
  13549. if (src0_type == GGML_TYPE_F16) {
  13550. src0_f16 = (half *) srcs_ar[i];
  13551. } else {
  13552. src0_f16 = src0_as_f16;
  13553. if (item_ct1.get_local_id(2) == 0 && threadIdx.y == 0) {
  13554. const to_fp16_sycl_t to_fp16 = ggml_get_to_fp16_sycl(src0_type);
  13555. to_fp16(srcs_ar[i], src0_f16, src0_ne, syclStreamFireAndForget);
  13556. }
  13557. }
  13558. int i13 = blockIdx.x * blockDim.x + item_ct1.get_local_id(2);
  13559. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  13560. if (i13 >= ne13 || i12 >= ne12) {
  13561. return;
  13562. }
  13563. int i03 = i13 / r3;
  13564. int i02 = i12 / r2;
  13565. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  13566. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  13567. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  13568. }
  13569. static void ggml_sycl_mul_mat_id_sycl(ggml_tensor * dst) {
  13570. const struct ggml_tensor * ids = dst->src[0];
  13571. const struct ggml_tensor * src1 = dst->src[1];
  13572. const struct ggml_tensor * src00 = dst->src[2];
  13573. const int id = dst->op_params[0];
  13574. GGML_ASSERT(!ggml_is_transposed(src00));
  13575. GGML_ASSERT(!ggml_is_transposed(src1));
  13576. GGML_ASSERT(src00->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13577. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  13578. GGML_TENSOR_LOCALS(int64_t, ne0, src00, ne);
  13579. //const int64_t nb01 = src00->nb[1];
  13580. GGML_TENSOR_LOCALS(int64_t, nb0, src00, nb);
  13581. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  13582. GGML_TENSOR_LOCALS(int64_t, nb1, src1, nb);
  13583. //const int64_t nb11 = src1->nb[1];
  13584. const int64_t ne1 = ggml_nelements(src1);
  13585. const int64_t ne = ggml_nelements(dst);
  13586. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13587. syclStream_t main_stream = g_syclStreams[g_main_device][0];
  13588. SYCL_CHECK(syclSetStream(g_sycl_handles[g_main_device], main_stream));
  13589. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13590. //void * src0_ddq = src0_extra->data_device[g_main_device];
  13591. //half * src0_as_f16 = (half *) src0_ddq;
  13592. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13593. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13594. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13595. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13596. // convert src1 to fp16
  13597. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  13598. GGML_ASSERT(to_fp16_sycl != nullptr);
  13599. size_t src1_as = 0;
  13600. half * src1_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne1 * sizeof(half), &src1_as);
  13601. to_fp16_sycl(src1_ddf, src1_as_f16, ne1, main_stream);
  13602. size_t dst_as = 0;
  13603. half * dst_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne * sizeof(half), &dst_as);
  13604. GGML_ASSERT(ne12 % ne02 == 0);
  13605. GGML_ASSERT(ne13 % ne03 == 0);
  13606. // broadcast factors
  13607. const int64_t r2 = ne12/ne02;
  13608. const int64_t r3 = ne13/ne03;
  13609. const half alpha_f16 = 1.0f;
  13610. const half beta_f16 = 0.0f;
  13611. // use syclGemmBatchedEx
  13612. const int ne23 = ne12*ne13;
  13613. const void ** ptrs_src = nullptr;
  13614. void ** ptrs_dst = nullptr;
  13615. size_t ptrs_src_s = 0;
  13616. size_t ptrs_dst_s = 0;
  13617. ptrs_src = (const void **) ggml_sycl_pool_malloc(g_main_device, 2*ne23*sizeof(void *), &ptrs_src_s);
  13618. ptrs_dst = ( void **) ggml_sycl_pool_malloc(g_main_device, 1*ne23*sizeof(void *), &ptrs_dst_s);
  13619. int64_t src0_ne = ggml_nelements(src00);
  13620. half * src0_as_f16 = nullptr;
  13621. size_t src0_as = 0;
  13622. if (src00->type != GGML_TYPE_F16) {
  13623. src0_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, src0_ne * sizeof(half), &src0_as);
  13624. }
  13625. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  13626. dim3 block_dims(ne13, ne12);
  13627. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  13628. ptrs_src, ptrs_dst,
  13629. ne12, ne13,
  13630. ne23,
  13631. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  13632. nb12, nb13,
  13633. dst->nb[2], dst->nb[3],
  13634. r2, r3,
  13635. src00->type, src0_as_f16, src0_ne,
  13636. src1_as_f16, dst_f16,
  13637. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  13638. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  13639. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  13640. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  13641. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  13642. );
  13643. SYCL_CHECK(syclGetLastError());
  13644. SYCL_CHECK(
  13645. syclGemmBatchedEx(g_sycl_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  13646. ne01, ne11, ne10,
  13647. &alpha_f16, (const void **) (ptrs_src + 0*ne23), SYCL_R_16F, ne00,
  13648. (const void **) (ptrs_src + 1*ne23), SYCL_R_16F, ne10,
  13649. &beta_f16, ( void **) (ptrs_dst + 0*ne23), SYCL_R_16F, ne01,
  13650. ne23,
  13651. CUBLAS_COMPUTE_16F,
  13652. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  13653. if (src0_as != 0) {
  13654. ggml_sycl_pool_free(g_main_device, src0_as_f16, src0_as);
  13655. }
  13656. if (ptrs_src_s != 0) {
  13657. ggml_sycl_pool_free(g_main_device, ptrs_src, ptrs_src_s);
  13658. }
  13659. if (ptrs_dst_s != 0) {
  13660. ggml_sycl_pool_free(g_main_device, ptrs_dst, ptrs_dst_s);
  13661. }
  13662. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  13663. to_fp32_sycl(dst_f16, dst_ddf, ne, main_stream);
  13664. ggml_sycl_pool_free(g_main_device, src1_as_f16, src1_as);
  13665. ggml_sycl_pool_free(g_main_device, dst_f16, dst_as);
  13666. }
  13667. #endif
  13668. static void ggml_sycl_mul_mat_id(const ggml_tensor *src0,
  13669. const ggml_tensor *src1,
  13670. ggml_tensor *dst) try {
  13671. #if 0
  13672. ggml_sycl_mul_mat_id_sycl(dst);
  13673. // TODO: mmq/mmv support
  13674. #endif
  13675. const int64_t nb11 = src1->nb[1];
  13676. const int64_t nb1 = dst->nb[1];
  13677. const struct ggml_tensor * ids = src0;
  13678. const int32_t id = ((int32_t *) dst->op_params)[0];
  13679. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  13680. std::vector<char> ids_host(ggml_nbytes(ids));
  13681. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13682. if (ids->backend == GGML_BACKEND_TYPE_GPU) {
  13683. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  13684. SYCL_CHECK(CHECK_TRY_ERROR(
  13685. stream->memcpy(ids_host.data(), ids_dev, ggml_nbytes(ids)).wait()));
  13686. // SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  13687. } else {
  13688. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  13689. }
  13690. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  13691. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  13692. ggml_tensor_extra_gpu src1_row_extra;
  13693. ggml_tensor_extra_gpu dst_row_extra;
  13694. ggml_tensor src1_row = *src1;
  13695. ggml_tensor dst_row = *dst;
  13696. src1_row.backend = GGML_BACKEND_TYPE_GPU;
  13697. dst_row.backend = GGML_BACKEND_TYPE_GPU;
  13698. src1_row.extra = &src1_row_extra;
  13699. dst_row.extra = &dst_row_extra;
  13700. char * src1_original = src1->backend == GGML_BACKEND_TYPE_CPU ?
  13701. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  13702. char * dst_original = dst->backend == GGML_BACKEND_TYPE_CPU ?
  13703. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  13704. if (src1->ne[1] == 1) {
  13705. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  13706. GGML_ASSERT(dst->backend == GGML_BACKEND_TYPE_GPU);
  13707. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13708. //int32_t row_id;
  13709. //SYCL_CHECK(syclMemcpyAsync(&row_id, ids_dev + i01*ids->nb[1] + id*ids->nb[0], sizeof(int32_t), syclMemcpyDeviceToHost, g_syclStreams[g_main_device][0]));
  13710. //SYCL_CHECK(syclStreamSynchronize(g_syclStreams[g_main_device][0]));
  13711. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13712. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13713. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  13714. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  13715. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  13716. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  13717. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  13718. ggml_sycl_mul_mat(src0_row, &src1_row, &dst_row);
  13719. }
  13720. } else {
  13721. sycl_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  13722. sycl_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  13723. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  13724. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  13725. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  13726. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  13727. int64_t num_src1_rows = 0;
  13728. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13729. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13730. if (row_id_i != row_id) {
  13731. continue;
  13732. }
  13733. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13734. SYCL_CHECK(CHECK_TRY_ERROR(
  13735. stream->memcpy(src1_contiguous.get() + num_src1_rows * nb11,
  13736. src1_original + i01 * nb11, nb11).wait()));
  13737. num_src1_rows++;
  13738. }
  13739. if (num_src1_rows == 0) {
  13740. continue;
  13741. }
  13742. src1_row.ne[1] = num_src1_rows;
  13743. dst_row.ne[1] = num_src1_rows;
  13744. src1_row.nb[1] = nb11;
  13745. src1_row.nb[2] = num_src1_rows*nb11;
  13746. src1_row.nb[3] = num_src1_rows*nb11;
  13747. dst_row.nb[1] = nb1;
  13748. dst_row.nb[2] = num_src1_rows*nb1;
  13749. dst_row.nb[3] = num_src1_rows*nb1;
  13750. ggml_sycl_mul_mat(src0_row, &src1_row, &dst_row);
  13751. num_src1_rows = 0;
  13752. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13753. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13754. if (row_id_i != row_id) {
  13755. continue;
  13756. }
  13757. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13758. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  13759. dst_original + i01 * nb1,
  13760. dst_contiguous.get() + num_src1_rows * nb1, nb1).wait()));
  13761. num_src1_rows++;
  13762. }
  13763. }
  13764. }
  13765. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  13766. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  13767. }
  13768. }
  13769. catch (sycl::exception const &exc) {
  13770. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13771. << ", line:" << __LINE__ << std::endl;
  13772. std::exit(1);
  13773. }
  13774. static void ggml_sycl_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13775. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_scale);
  13776. }
  13777. static void ggml_sycl_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13778. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_clamp);
  13779. }
  13780. static void ggml_sycl_cpy(const ggml_tensor *src0, const ggml_tensor *src1,
  13781. ggml_tensor *dst) try {
  13782. const int64_t ne = ggml_nelements(src0);
  13783. GGML_ASSERT(ne == ggml_nelements(src1));
  13784. GGML_ASSERT(src0->backend == GGML_BACKEND_TYPE_GPU);
  13785. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  13786. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  13787. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  13788. GGML_TENSOR_BINARY_OP_LOCALS;
  13789. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13790. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13791. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13792. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13793. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  13794. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  13795. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  13796. 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);
  13797. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  13798. 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);
  13799. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  13800. 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);
  13801. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  13802. 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);
  13803. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  13804. 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);
  13805. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  13806. 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);
  13807. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  13808. 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);
  13809. } else if (src0->type == GGML_TYPE_I16 && src1->type == GGML_TYPE_I16) {
  13810. 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);
  13811. } else if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32) {
  13812. 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);
  13813. } else {
  13814. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  13815. ggml_type_name(src0->type), ggml_type_name(src1->type));
  13816. GGML_ASSERT(false);
  13817. }
  13818. (void) dst;
  13819. }
  13820. catch (sycl::exception const &exc) {
  13821. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13822. << ", line:" << __LINE__ << std::endl;
  13823. std::exit(1);
  13824. }
  13825. static void ggml_sycl_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13826. // TODO: why do we pass dst as src1 here?
  13827. ggml_sycl_cpy(src0, dst, nullptr);
  13828. (void) src1;
  13829. }
  13830. static void ggml_sycl_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13831. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_diag_mask_inf);
  13832. }
  13833. static void ggml_sycl_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13834. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_soft_max);
  13835. }
  13836. static void ggml_sycl_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13837. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  13838. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rope);
  13839. }
  13840. static void ggml_sycl_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13841. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_alibi);
  13842. }
  13843. static void ggml_sycl_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13844. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pool2d);
  13845. }
  13846. static void ggml_sycl_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13847. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_im2col);
  13848. }
  13849. static void ggml_sycl_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13850. GGML_ASSERT(ggml_is_contiguous(src0));
  13851. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sum_rows);
  13852. }
  13853. static void ggml_sycl_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13854. GGML_ASSERT(ggml_is_contiguous(src0));
  13855. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_argsort);
  13856. }
  13857. static void ggml_sycl_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13858. (void) src0;
  13859. (void) src1;
  13860. (void) dst;
  13861. }
  13862. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  13863. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  13864. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  13865. }
  13866. void ggml_sycl_free_data(struct ggml_tensor *tensor) try {
  13867. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_TYPE_GPU && tensor->backend != GGML_BACKEND_TYPE_GPU_SPLIT) ) {
  13868. return;
  13869. }
  13870. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  13871. for (int i = 0; i < g_device_count; ++i) {
  13872. const dpct::queue_ptr stream = g_syclStreams[i][0];
  13873. if (extra->data_device[i] != nullptr) {
  13874. SYCL_CHECK(ggml_sycl_set_device(i));
  13875. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(extra->data_device[i], *stream)));
  13876. }
  13877. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  13878. if (extra->events[i][is] != nullptr) {
  13879. SYCL_CHECK(ggml_sycl_set_device(i));
  13880. SYCL_CHECK(CHECK_TRY_ERROR(
  13881. dpct::destroy_event(extra->events[i][is])));
  13882. }
  13883. }
  13884. }
  13885. delete extra;
  13886. }
  13887. catch (sycl::exception const &exc) {
  13888. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13889. << ", line:" << __LINE__ << std::endl;
  13890. std::exit(1);
  13891. }
  13892. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  13893. static size_t g_temp_tensor_extra_index = 0;
  13894. static ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  13895. if (g_temp_tensor_extras == nullptr) {
  13896. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  13897. }
  13898. size_t alloc_index = g_temp_tensor_extra_index;
  13899. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  13900. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  13901. memset(extra, 0, sizeof(*extra));
  13902. return extra;
  13903. }
  13904. static void ggml_sycl_assign_buffers_impl(struct ggml_tensor *tensor,
  13905. bool scratch, bool force_inplace,
  13906. bool no_alloc) try {
  13907. if (scratch && g_scratch_size == 0) {
  13908. return;
  13909. }
  13910. tensor->backend = GGML_BACKEND_TYPE_GPU;
  13911. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_CPU) {
  13912. const ggml_op src0_op = tensor->src[0]->op;
  13913. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  13914. ggml_sycl_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  13915. }
  13916. }
  13917. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_TYPE_CPU) {
  13918. ggml_sycl_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  13919. }
  13920. if (scratch && no_alloc) {
  13921. return;
  13922. }
  13923. ggml_tensor_extra_gpu * extra;
  13924. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  13925. tensor->op == GGML_OP_VIEW ||
  13926. force_inplace;
  13927. const size_t size = ggml_nbytes(tensor);
  13928. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13929. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13930. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT)) {
  13931. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  13932. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  13933. size_t offset = 0;
  13934. if (tensor->op == GGML_OP_VIEW) {
  13935. memcpy(&offset, tensor->op_params, sizeof(size_t));
  13936. }
  13937. extra = ggml_sycl_alloc_temp_tensor_extra();
  13938. extra->data_device[g_main_device] = src0_ddc + offset;
  13939. } else if (tensor->op == GGML_OP_CPY) {
  13940. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  13941. void * src1_ddv = src1_extra->data_device[g_main_device];
  13942. extra = ggml_sycl_alloc_temp_tensor_extra();
  13943. extra->data_device[g_main_device] = src1_ddv;
  13944. } else if (scratch) {
  13945. GGML_ASSERT(size <= g_scratch_size);
  13946. if (g_scratch_offset + size > g_scratch_size) {
  13947. g_scratch_offset = 0;
  13948. }
  13949. char * data = (char *) g_scratch_buffer;
  13950. if (data == nullptr) {
  13951. SYCL_CHECK(CHECK_TRY_ERROR(
  13952. data = (char *)sycl::malloc_device(
  13953. g_scratch_size, *stream)));
  13954. g_scratch_buffer = data;
  13955. }
  13956. extra = ggml_sycl_alloc_temp_tensor_extra();
  13957. extra->data_device[g_main_device] = data + g_scratch_offset;
  13958. g_scratch_offset += size;
  13959. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  13960. } else { // allocate new buffers outside of scratch
  13961. void * data;
  13962. SYCL_CHECK(CHECK_TRY_ERROR(data = (void *)sycl::malloc_device(
  13963. size, *stream)));
  13964. SYCL_CHECK(CHECK_TRY_ERROR(
  13965. (*stream).memset(data, 0, size).wait()));
  13966. extra = new ggml_tensor_extra_gpu;
  13967. memset(extra, 0, sizeof(*extra));
  13968. extra->data_device[g_main_device] = data;
  13969. }
  13970. tensor->extra = extra;
  13971. }
  13972. catch (sycl::exception const &exc) {
  13973. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13974. << ", line:" << __LINE__ << std::endl;
  13975. std::exit(1);
  13976. }
  13977. void ggml_sycl_copy_to_device(struct ggml_tensor *tensor) try {
  13978. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  13979. GGML_ASSERT(ggml_is_contiguous(tensor));
  13980. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  13981. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13982. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13983. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  13984. .memcpy(extra->data_device[g_main_device],
  13985. tensor->data, ggml_nbytes(tensor))
  13986. .wait()));
  13987. }
  13988. catch (sycl::exception const &exc) {
  13989. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13990. << ", line:" << __LINE__ << std::endl;
  13991. std::exit(1);
  13992. }
  13993. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor) {
  13994. ggml_sycl_assign_buffers_impl(tensor, true, false, false);
  13995. }
  13996. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  13997. ggml_sycl_assign_buffers_impl(tensor, true, false, true);
  13998. }
  13999. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  14000. ggml_sycl_assign_buffers_impl(tensor, false, false, false);
  14001. }
  14002. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  14003. ggml_sycl_assign_buffers_impl(tensor, false, true, false);
  14004. }
  14005. void ggml_sycl_set_main_device(const int main_device) try {
  14006. if (g_main_device == main_device) return;
  14007. check_allow_gpu_index(main_device);
  14008. g_main_device = main_device;
  14009. g_main_device_id = g_sycl_gpu_mgr->gpus[main_device];
  14010. if (g_ggml_sycl_debug) {
  14011. dpct::device_info prop;
  14012. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  14013. prop, dpct::dev_mgr::instance().get_device(g_main_device_id))));
  14014. fprintf(stderr, "Using device %d (%s) as main device\n",
  14015. g_main_device_id, prop.get_name());
  14016. }
  14017. }
  14018. catch (sycl::exception const &exc) {
  14019. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14020. << ", line:" << __LINE__ << std::endl;
  14021. std::exit(1);
  14022. }
  14023. void ggml_sycl_set_scratch_size(const size_t scratch_size) {
  14024. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  14025. // it still won't always work as expected, but it's better than nothing
  14026. if (scratch_size > g_scratch_size) {
  14027. ggml_sycl_free_scratch();
  14028. }
  14029. g_scratch_size = std::max(g_scratch_size, scratch_size);
  14030. }
  14031. void ggml_sycl_free_scratch() try {
  14032. if (g_scratch_buffer == nullptr) {
  14033. return;
  14034. }
  14035. ggml_sycl_set_device(g_main_device);
  14036. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  14037. SYCL_CHECK(CHECK_TRY_ERROR(
  14038. sycl::free(g_scratch_buffer, *stream)));
  14039. g_scratch_buffer = nullptr;
  14040. }
  14041. catch (sycl::exception const &exc) {
  14042. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14043. << ", line:" << __LINE__ << std::endl;
  14044. std::exit(1);
  14045. }
  14046. bool ggml_sycl_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  14047. if (!g_sycl_loaded) return false;
  14048. ggml_sycl_func_t func;
  14049. const bool any_on_device = tensor->backend == GGML_BACKEND_TYPE_GPU
  14050. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT))
  14051. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_TYPE_GPU);
  14052. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  14053. return false;
  14054. }
  14055. if (tensor->op == GGML_OP_MUL_MAT) {
  14056. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  14057. #ifndef NDEBUG
  14058. 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]);
  14059. #endif
  14060. return false;
  14061. }
  14062. }
  14063. switch (tensor->op) {
  14064. case GGML_OP_REPEAT:
  14065. func = ggml_sycl_repeat;
  14066. break;
  14067. case GGML_OP_GET_ROWS:
  14068. func = ggml_sycl_get_rows;
  14069. break;
  14070. case GGML_OP_DUP:
  14071. func = ggml_sycl_dup;
  14072. break;
  14073. case GGML_OP_ADD:
  14074. func = ggml_sycl_add;
  14075. break;
  14076. case GGML_OP_ACC:
  14077. func = ggml_sycl_acc;
  14078. break;
  14079. case GGML_OP_MUL:
  14080. func = ggml_sycl_mul;
  14081. break;
  14082. case GGML_OP_DIV:
  14083. func = ggml_sycl_div;
  14084. break;
  14085. case GGML_OP_UNARY:
  14086. switch (ggml_get_unary_op(tensor)) {
  14087. case GGML_UNARY_OP_GELU:
  14088. func = ggml_sycl_gelu;
  14089. break;
  14090. case GGML_UNARY_OP_SILU:
  14091. func = ggml_sycl_silu;
  14092. break;
  14093. case GGML_UNARY_OP_GELU_QUICK:
  14094. func = ggml_sycl_gelu_quick;
  14095. break;
  14096. case GGML_UNARY_OP_TANH:
  14097. func = ggml_sycl_tanh;
  14098. break;
  14099. case GGML_UNARY_OP_RELU:
  14100. func = ggml_sycl_relu;
  14101. break;
  14102. case GGML_UNARY_OP_HARDSIGMOID:
  14103. func = ggml_sycl_hardsigmoid;
  14104. break;
  14105. case GGML_UNARY_OP_HARDSWISH:
  14106. func = ggml_sycl_hardswish;
  14107. break;
  14108. default:
  14109. return false;
  14110. }
  14111. break;
  14112. case GGML_OP_NORM:
  14113. func = ggml_sycl_norm;
  14114. break;
  14115. case GGML_OP_GROUP_NORM:
  14116. func = ggml_sycl_group_norm;
  14117. break;
  14118. case GGML_OP_CONCAT:
  14119. func = ggml_sycl_concat;
  14120. break;
  14121. case GGML_OP_UPSCALE:
  14122. func = ggml_sycl_upscale;
  14123. break;
  14124. case GGML_OP_PAD:
  14125. func = ggml_sycl_pad;
  14126. break;
  14127. case GGML_OP_LEAKY_RELU:
  14128. func = ggml_sycl_leaky_relu;
  14129. break;
  14130. case GGML_OP_RMS_NORM:
  14131. func = ggml_sycl_rms_norm;
  14132. break;
  14133. case GGML_OP_MUL_MAT:
  14134. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  14135. return false;
  14136. }
  14137. func = ggml_sycl_mul_mat;
  14138. break;
  14139. case GGML_OP_MUL_MAT_ID:
  14140. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  14141. return false;
  14142. }
  14143. func = ggml_sycl_mul_mat_id;
  14144. break;
  14145. case GGML_OP_SCALE:
  14146. func = ggml_sycl_scale;
  14147. break;
  14148. case GGML_OP_SQR:
  14149. func = ggml_sycl_sqr;
  14150. break;
  14151. case GGML_OP_CLAMP:
  14152. func = ggml_sycl_clamp;
  14153. break;
  14154. case GGML_OP_CPY:
  14155. func = ggml_sycl_cpy;
  14156. break;
  14157. case GGML_OP_CONT:
  14158. func = ggml_sycl_dup;
  14159. break;
  14160. case GGML_OP_NONE:
  14161. case GGML_OP_RESHAPE:
  14162. case GGML_OP_VIEW:
  14163. case GGML_OP_PERMUTE:
  14164. case GGML_OP_TRANSPOSE:
  14165. func = ggml_sycl_nop;
  14166. break;
  14167. case GGML_OP_DIAG_MASK_INF:
  14168. func = ggml_sycl_diag_mask_inf;
  14169. break;
  14170. case GGML_OP_SOFT_MAX:
  14171. func = ggml_sycl_soft_max;
  14172. break;
  14173. case GGML_OP_ROPE:
  14174. func = ggml_sycl_rope;
  14175. break;
  14176. case GGML_OP_ALIBI:
  14177. func = ggml_sycl_alibi;
  14178. break;
  14179. case GGML_OP_IM2COL:
  14180. func = ggml_sycl_im2col;
  14181. break;
  14182. case GGML_OP_POOL_2D:
  14183. func = ggml_sycl_pool2d;
  14184. break;
  14185. case GGML_OP_SUM_ROWS:
  14186. func = ggml_sycl_sum_rows;
  14187. break;
  14188. case GGML_OP_ARGSORT:
  14189. func = ggml_sycl_argsort;
  14190. break;
  14191. default:
  14192. return false;
  14193. }
  14194. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  14195. ggml_sycl_set_peer_access(tensor->src[1]->ne[1]);
  14196. }
  14197. if (params->ith != 0) {
  14198. return true;
  14199. }
  14200. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  14201. return true;
  14202. }
  14203. func(tensor->src[0], tensor->src[1], tensor);
  14204. return true;
  14205. }
  14206. GGML_API GGML_CALL void ggml_sycl_get_gpu_list(int *id_list, int max_len) try {
  14207. GGML_SYCL_DEBUG("[SYCL] call ggml_sycl_get_gpu_list\n");
  14208. for(int i=0;i<max_len;i++) id_list[i] = -1;
  14209. if (!g_sycl_gpu_mgr) {
  14210. g_sycl_gpu_mgr = new sycl_gpu_mgr();
  14211. }
  14212. for (int i=0;i< g_sycl_gpu_mgr->gpus.size();i++){
  14213. if (i>=max_len) break;
  14214. id_list[i] = g_sycl_gpu_mgr->gpus[i];
  14215. }
  14216. return;
  14217. }
  14218. catch (sycl::exception const &exc) {
  14219. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14220. << ", line:" << __LINE__ << std::endl;
  14221. std::exit(1);
  14222. }
  14223. int ggml_sycl_get_device_count() try {
  14224. int device_count;
  14225. if (CHECK_TRY_ERROR(device_count =
  14226. dpct::dev_mgr::instance().device_count()) != 0) {
  14227. return 0;
  14228. }
  14229. return device_count;
  14230. }
  14231. catch (sycl::exception const &exc) {
  14232. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14233. << ", line:" << __LINE__ << std::endl;
  14234. std::exit(1);
  14235. }
  14236. GGML_API GGML_CALL void ggml_sycl_get_device_description(int device, char *description,
  14237. size_t description_size) try {
  14238. GGML_SYCL_DEBUG("[SYCL] call ggml_sycl_get_device_description\n");
  14239. dpct::device_info prop;
  14240. int device_id = g_sycl_gpu_mgr->gpus[device];
  14241. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  14242. prop, dpct::dev_mgr::instance().get_device(device_id))));
  14243. snprintf(description, description_size, "%s", prop.get_name());
  14244. }
  14245. catch (sycl::exception const &exc) {
  14246. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14247. << ", line:" << __LINE__ << std::endl;
  14248. std::exit(1);
  14249. }
  14250. GGML_CALL void ggml_backend_sycl_get_device_memory(int device, size_t *free,
  14251. size_t *total) try {
  14252. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_memory\n");
  14253. ggml_sycl_set_device(device);
  14254. /*
  14255. DPCT1009:218: SYCL uses exceptions to report errors and does not use the
  14256. error codes. The original code was commented out and a warning string was
  14257. inserted. You need to rewrite this code.
  14258. */
  14259. /*
  14260. DPCT1106:217: 'cudaMemGetInfo' was migrated with the Intel extensions for
  14261. device information which may not be supported by all compilers or runtimes.
  14262. You may need to adjust the code.
  14263. */
  14264. int device_id = g_sycl_gpu_mgr->gpus[device];
  14265. SYCL_CHECK(CHECK_TRY_ERROR(
  14266. dpct::dev_mgr::instance().get_device(device_id).get_memory_info(*free, *total)));
  14267. }
  14268. catch (sycl::exception const &exc) {
  14269. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14270. << ", line:" << __LINE__ << std::endl;
  14271. std::exit(1);
  14272. }
  14273. ////////////////////////////////////////////////////////////////////////////////
  14274. // backend interface
  14275. #define UNUSED GGML_UNUSED
  14276. // sycl buffer
  14277. struct ggml_backend_sycl_buffer_context {
  14278. int device;
  14279. void * dev_ptr = nullptr;
  14280. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  14281. size_t temp_tensor_extra_index = 0;
  14282. std::string name;
  14283. ggml_backend_sycl_buffer_context(int device, void * dev_ptr) :
  14284. device(device), dev_ptr(dev_ptr) {
  14285. check_allow_gpu_index(device);
  14286. int id = g_sycl_gpu_mgr->gpus[device];
  14287. name = (GGML_SYCL_NAME + std::to_string(id));
  14288. }
  14289. ~ ggml_backend_sycl_buffer_context() {
  14290. delete[] temp_tensor_extras;
  14291. }
  14292. ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  14293. if (temp_tensor_extras == nullptr) {
  14294. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  14295. }
  14296. size_t alloc_index = temp_tensor_extra_index;
  14297. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  14298. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  14299. memset(extra, 0, sizeof(*extra));
  14300. return extra;
  14301. }
  14302. };
  14303. GGML_CALL static const char * ggml_backend_sycl_buffer_get_name(ggml_backend_buffer_t buffer) {
  14304. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  14305. return ctx->name.c_str();
  14306. }
  14307. GGML_CALL static bool ggml_backend_buffer_is_sycl(ggml_backend_buffer_t buffer) {
  14308. return buffer->iface.get_name == ggml_backend_sycl_buffer_get_name;
  14309. }
  14310. static void
  14311. ggml_backend_sycl_buffer_free_buffer(ggml_backend_buffer_t buffer) try {
  14312. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14313. ggml_sycl_set_device(ctx->device);
  14314. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14315. SYCL_CHECK(
  14316. CHECK_TRY_ERROR(sycl::free(ctx->dev_ptr, *stream)));
  14317. delete ctx;
  14318. }
  14319. catch (sycl::exception const &exc) {
  14320. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14321. << ", line:" << __LINE__ << std::endl;
  14322. std::exit(1);
  14323. }
  14324. static void * ggml_backend_sycl_buffer_get_base(ggml_backend_buffer_t buffer) {
  14325. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14326. return ctx->dev_ptr;
  14327. }
  14328. GGML_CALL static void
  14329. ggml_backend_sycl_buffer_init_tensor(ggml_backend_buffer_t buffer,
  14330. ggml_tensor *tensor) try {
  14331. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  14332. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  14333. assert(tensor->view_src->buffer->buft == buffer->buft);
  14334. tensor->backend = tensor->view_src->backend;
  14335. tensor->extra = tensor->view_src->extra;
  14336. return;
  14337. }
  14338. ggml_tensor_extra_gpu * extra = ctx->ggml_sycl_alloc_temp_tensor_extra();
  14339. extra->data_device[ctx->device] = tensor->data;
  14340. tensor->backend = GGML_BACKEND_TYPE_GPU;
  14341. tensor->extra = extra;
  14342. if (ggml_is_quantized(tensor->type)) {
  14343. // initialize padding to 0 to avoid possible NaN values
  14344. size_t original_size = ggml_nbytes(tensor);
  14345. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  14346. if (padded_size > original_size && tensor->view_src == nullptr) {
  14347. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[ctx->device][0]->memset(
  14348. (char *)tensor->data + original_size, 0,
  14349. padded_size - original_size).wait()));
  14350. }
  14351. }
  14352. }
  14353. catch (sycl::exception const &exc) {
  14354. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14355. << ", line:" << __LINE__ << std::endl;
  14356. std::exit(1);
  14357. }
  14358. static void ggml_backend_sycl_buffer_set_tensor(ggml_backend_buffer_t buffer,
  14359. ggml_tensor *tensor,
  14360. const void *data, size_t offset,
  14361. size_t size) try {
  14362. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14363. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14364. ggml_sycl_set_device(ctx->device);
  14365. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14366. SYCL_CHECK(
  14367. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  14368. SYCL_CHECK(
  14369. CHECK_TRY_ERROR((*stream)
  14370. .memcpy((char *)tensor->data + offset, data, size)
  14371. .wait()));
  14372. }
  14373. catch (sycl::exception const &exc) {
  14374. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14375. << ", line:" << __LINE__ << std::endl;
  14376. std::exit(1);
  14377. }
  14378. static void ggml_backend_sycl_buffer_get_tensor(ggml_backend_buffer_t buffer,
  14379. const ggml_tensor *tensor,
  14380. void *data, size_t offset,
  14381. size_t size) try {
  14382. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14383. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14384. ggml_sycl_set_device(ctx->device);
  14385. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14386. SYCL_CHECK(
  14387. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  14388. SYCL_CHECK(CHECK_TRY_ERROR(
  14389. (*stream)
  14390. .memcpy(data, (const char *)tensor->data + offset, size)
  14391. .wait()));
  14392. }
  14393. catch (sycl::exception const &exc) {
  14394. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14395. << ", line:" << __LINE__ << std::endl;
  14396. std::exit(1);
  14397. }
  14398. GGML_CALL static bool
  14399. ggml_backend_sycl_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
  14400. const ggml_tensor *src,
  14401. ggml_tensor *dst) try {
  14402. if (ggml_backend_buffer_is_sycl(src->buffer)) {
  14403. ggml_backend_sycl_buffer_context * src_ctx = (ggml_backend_sycl_buffer_context *)src->buffer->context;
  14404. ggml_backend_sycl_buffer_context * dst_ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  14405. ggml_sycl_set_device(src_ctx->device);
  14406. /*
  14407. DPCT1009:198: SYCL uses exceptions to report errors and does not use the
  14408. error codes. The original code was commented out and a warning string
  14409. was inserted. You need to rewrite this code.
  14410. */
  14411. SYCL_CHECK(CHECK_TRY_ERROR(
  14412. dpct::dev_mgr::instance().get_device(src_ctx->device).queues_wait_and_throw()));
  14413. ggml_sycl_set_device(dst_ctx->device);
  14414. /*
  14415. DPCT1009:199: SYCL uses exceptions to report errors and does not use the
  14416. error codes. The original code was commented out and a warning string
  14417. was inserted. You need to rewrite this code.
  14418. */
  14419. SYCL_CHECK(CHECK_TRY_ERROR(
  14420. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  14421. /*
  14422. DPCT1009:200: SYCL uses exceptions to report errors and does not use the
  14423. error codes. The original code was commented out and a warning string
  14424. was inserted. You need to rewrite this code.
  14425. */
  14426. dpct::queue_ptr stream_dst = g_syclStreams[dst_ctx->device][0];
  14427. dpct::queue_ptr stream_src = g_syclStreams[src_ctx->device][0];
  14428. size_t size = ggml_nbytes(src);
  14429. //todo. it's dirty solutino to walkaroud known issue:device2device cross GPUs.
  14430. dev2dev_memcpy(*stream_dst, *stream_src, dst->data, src->data, size);
  14431. //todo, it's known issue:error in device2device cross GPUs. reused when the issue is fixed. DON"T remove
  14432. #if 0
  14433. SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy(
  14434. (char *)dst->data, (const char *)src->data, size).wait()));
  14435. /*
  14436. DPCT1009:201: SYCL uses exceptions to report errors and does not use the
  14437. error codes. The original code was commented out and a warning string
  14438. was inserted. You need to rewrite this code.
  14439. */
  14440. SYCL_CHECK(CHECK_TRY_ERROR(
  14441. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  14442. #endif
  14443. return true;
  14444. }
  14445. return false;
  14446. }
  14447. catch (sycl::exception const &exc) {
  14448. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14449. << ", line:" << __LINE__ << std::endl;
  14450. std::exit(1);
  14451. }
  14452. static void ggml_backend_sycl_buffer_clear(ggml_backend_buffer_t buffer,
  14453. uint8_t value) try {
  14454. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14455. ggml_sycl_set_device(ctx->device);
  14456. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14457. SYCL_CHECK(
  14458. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  14459. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  14460. .memset(ctx->dev_ptr, value, buffer->size)
  14461. .wait()));
  14462. }
  14463. catch (sycl::exception const &exc) {
  14464. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14465. << ", line:" << __LINE__ << std::endl;
  14466. std::exit(1);
  14467. }
  14468. static struct ggml_backend_buffer_i ggml_backend_sycl_buffer_interface = {
  14469. /* .get_name = */ ggml_backend_sycl_buffer_get_name,
  14470. /* .free_buffer = */ ggml_backend_sycl_buffer_free_buffer,
  14471. /* .get_base = */ ggml_backend_sycl_buffer_get_base,
  14472. /* .init_tensor = */ ggml_backend_sycl_buffer_init_tensor,
  14473. /* .set_tensor = */ ggml_backend_sycl_buffer_set_tensor,
  14474. /* .get_tensor = */ ggml_backend_sycl_buffer_get_tensor,
  14475. /* .cpy_tensor = */ ggml_backend_sycl_buffer_cpy_tensor,
  14476. /* .clear = */ ggml_backend_sycl_buffer_clear,
  14477. /* .reset = */ NULL,
  14478. };
  14479. // sycl buffer type
  14480. struct ggml_backend_sycl_buffer_type_context {
  14481. int device;
  14482. std::string name;
  14483. };
  14484. struct ggml_backend_sycl_context {
  14485. int device;
  14486. std::string name;
  14487. };
  14488. GGML_CALL static const char * ggml_backend_sycl_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14489. ggml_backend_sycl_buffer_type_context * ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14490. return ctx->name.c_str();
  14491. }
  14492. GGML_CALL static ggml_backend_buffer_t
  14493. ggml_backend_sycl_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
  14494. size_t size) try {
  14495. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14496. ggml_sycl_set_device(buft_ctx->device);
  14497. const dpct::queue_ptr stream = g_syclStreams[buft_ctx->device][0];
  14498. size = std::max(size, (size_t)1); // syclMalloc returns null for size 0
  14499. void * dev_ptr;
  14500. SYCL_CHECK(CHECK_TRY_ERROR(dev_ptr = (void *)sycl::malloc_device(
  14501. size, *stream)));
  14502. ggml_backend_sycl_buffer_context * ctx = new ggml_backend_sycl_buffer_context(buft_ctx->device, dev_ptr);
  14503. return ggml_backend_buffer_init(buft, ggml_backend_sycl_buffer_interface, ctx, size);
  14504. }
  14505. catch (sycl::exception const &exc) {
  14506. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14507. << ", line:" << __LINE__ << std::endl;
  14508. std::exit(1);
  14509. }
  14510. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  14511. return 128;
  14512. UNUSED(buft);
  14513. }
  14514. static size_t ggml_backend_sycl_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  14515. return dpct::get_current_device().get_max_mem_alloc_size();
  14516. UNUSED(buft);
  14517. }
  14518. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  14519. size_t size = ggml_nbytes(tensor);
  14520. int64_t ne0 = tensor->ne[0];
  14521. if (ggml_is_quantized(tensor->type)) {
  14522. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14523. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14524. }
  14525. }
  14526. return size;
  14527. UNUSED(buft);
  14528. }
  14529. GGML_CALL static bool ggml_backend_sycl_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  14530. if (!ggml_backend_is_sycl(backend)) {
  14531. return false;
  14532. }
  14533. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14534. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14535. return buft_ctx->device == sycl_ctx->device;
  14536. }
  14537. static ggml_backend_buffer_type_i ggml_backend_sycl_buffer_type_interface = {
  14538. /* .get_name = */ ggml_backend_sycl_buffer_type_name,
  14539. /* .alloc_buffer = */ ggml_backend_sycl_buffer_type_alloc_buffer,
  14540. /* .get_alignment = */ ggml_backend_sycl_buffer_type_get_alignment,
  14541. /* .get_max_size = */ ggml_backend_sycl_buffer_type_get_max_size,
  14542. /* .get_alloc_size = */ ggml_backend_sycl_buffer_type_get_alloc_size,
  14543. /* .supports_backend = */ ggml_backend_sycl_buffer_type_supports_backend,
  14544. /* .is_host = */ nullptr,
  14545. };
  14546. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(int device_index) {
  14547. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_buffer_type\n");
  14548. if (device_index>=g_device_count or device_index<0) {
  14549. 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",
  14550. device_index, g_device_count-1);
  14551. GGML_ASSERT(device_index<g_device_count);
  14552. }
  14553. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  14554. if (!g_ggml_backend_sycl_buffer_type_initialized) {
  14555. for (int i = 0; i < g_device_count; i++) {
  14556. ggml_backend_sycl_buffer_types[i] = {
  14557. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  14558. /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(g_sycl_gpu_mgr->gpus[i])},
  14559. };
  14560. }
  14561. g_ggml_backend_sycl_buffer_type_initialized = true;
  14562. }
  14563. return &ggml_backend_sycl_buffer_types[device_index];
  14564. }
  14565. // sycl split buffer type
  14566. 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) {
  14567. const int64_t nrows = ggml_nrows(tensor);
  14568. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  14569. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  14570. *row_low -= *row_low % rounding;
  14571. if (id == g_device_count - 1) {
  14572. *row_high = nrows;
  14573. } else {
  14574. *row_high = nrows*tensor_split[id + 1];
  14575. *row_high -= *row_high % rounding;
  14576. }
  14577. }
  14578. struct ggml_backend_sycl_split_buffer_context {
  14579. ~ggml_backend_sycl_split_buffer_context() try {
  14580. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  14581. for (int i = 0; i < g_device_count; ++i) {
  14582. // int id = g_sycl_gpu_mgr->gpus[i];
  14583. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  14584. if (extra->events[i][is] != nullptr) {
  14585. /*
  14586. DPCT1009:206: SYCL uses exceptions to report errors and
  14587. does not use the error codes. The original code was
  14588. commented out and a warning string was inserted. You
  14589. need to rewrite this code.
  14590. */
  14591. SYCL_CHECK(CHECK_TRY_ERROR(
  14592. dpct::destroy_event(extra->events[i][is])));
  14593. }
  14594. }
  14595. if (extra->data_device[i] != nullptr) {
  14596. /*
  14597. DPCT1009:207: SYCL uses exceptions to report errors and does
  14598. not use the error codes. The original code was commented out
  14599. and a warning string was inserted. You need to rewrite this
  14600. code.
  14601. */
  14602. ggml_sycl_set_device(i);
  14603. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(
  14604. extra->data_device[i], *g_syclStreams[i][0])));
  14605. }
  14606. }
  14607. delete extra;
  14608. }
  14609. }
  14610. catch (sycl::exception const &exc) {
  14611. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14612. << ", line:" << __LINE__ << std::endl;
  14613. std::exit(1);
  14614. }
  14615. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  14616. };
  14617. GGML_CALL static const char * ggml_backend_sycl_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  14618. return GGML_SYCL_NAME "_Split";
  14619. UNUSED(buffer);
  14620. }
  14621. // unused at the moment
  14622. //static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer) {
  14623. // return buffer->iface.get_name == ggml_backend_sycl_split_buffer_get_name;
  14624. //}
  14625. GGML_CALL static void ggml_backend_sycl_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  14626. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  14627. delete ctx;
  14628. }
  14629. GGML_CALL static void * ggml_backend_sycl_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  14630. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  14631. return (void *)0x1000;
  14632. UNUSED(buffer);
  14633. }
  14634. GGML_CALL static void
  14635. ggml_backend_sycl_split_buffer_init_tensor(ggml_backend_buffer_t buffer,
  14636. ggml_tensor *tensor) try {
  14637. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  14638. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  14639. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14640. const int64_t ne0 = tensor->ne[0];
  14641. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  14642. ctx->tensor_extras.push_back(extra);
  14643. for (int i = 0; i < g_device_count; ++i) {
  14644. // int id = g_sycl_gpu_mgr->gpus[i];
  14645. int64_t row_low, row_high;
  14646. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14647. int64_t nrows_split = row_high - row_low;
  14648. if (nrows_split == 0) {
  14649. continue;
  14650. }
  14651. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14652. const size_t original_size = size;
  14653. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14654. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14655. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14656. }
  14657. // FIXME: do not crash if cudaMalloc fails
  14658. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  14659. ggml_sycl_set_device(i);
  14660. char * buf;
  14661. /*
  14662. DPCT1009:208: SYCL uses exceptions to report errors and does not use the
  14663. error codes. The original code was commented out and a warning string
  14664. was inserted. You need to rewrite this code.
  14665. */
  14666. SYCL_CHECK(CHECK_TRY_ERROR(buf = (char *)sycl::malloc_device(
  14667. size, *g_syclStreams[i][0])));
  14668. // set padding to 0 to avoid possible NaN values
  14669. if (size > original_size) {
  14670. /*
  14671. DPCT1009:209: SYCL uses exceptions to report errors and does not use
  14672. the error codes. The original code was commented out and a warning
  14673. string was inserted. You need to rewrite this code.
  14674. */
  14675. SYCL_CHECK(CHECK_TRY_ERROR(
  14676. (*g_syclStreams[i][0])
  14677. .memset(buf + original_size, 0, size - original_size)
  14678. .wait()));
  14679. }
  14680. extra->data_device[i] = buf;
  14681. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  14682. /*
  14683. DPCT1009:210: SYCL uses exceptions to report errors and does not use
  14684. the error codes. The original code was commented out and a warning
  14685. string was inserted. You need to rewrite this code.
  14686. */
  14687. SYCL_CHECK(
  14688. CHECK_TRY_ERROR(extra->events[i][is] = new sycl::event()));
  14689. }
  14690. }
  14691. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  14692. tensor->extra = extra;
  14693. }
  14694. catch (sycl::exception const &exc) {
  14695. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14696. << ", line:" << __LINE__ << std::endl;
  14697. std::exit(1);
  14698. }
  14699. GGML_CALL static void
  14700. ggml_backend_sycl_split_buffer_set_tensor(ggml_backend_buffer_t buffer,
  14701. ggml_tensor *tensor, const void *data,
  14702. size_t offset, size_t size) try {
  14703. // split tensors must always be set in their entirety at once
  14704. GGML_ASSERT(offset == 0);
  14705. GGML_ASSERT(size == ggml_nbytes(tensor));
  14706. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14707. const int64_t ne0 = tensor->ne[0];
  14708. const size_t nb1 = tensor->nb[1];
  14709. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  14710. for (int i = 0; i < g_device_count; ++i) {
  14711. // int id = g_sycl_gpu_mgr->gpus[i];
  14712. int64_t row_low, row_high;
  14713. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14714. int64_t nrows_split = row_high - row_low;
  14715. if (nrows_split == 0) {
  14716. continue;
  14717. }
  14718. const size_t offset_split = row_low*nb1;
  14719. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14720. const size_t original_size = size;
  14721. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14722. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14723. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14724. }
  14725. const char * buf_host = (const char *)data + offset_split;
  14726. /*
  14727. DPCT1009:211: SYCL uses exceptions to report errors and does not use the
  14728. error codes. The original code was commented out and a warning string
  14729. was inserted. You need to rewrite this code.
  14730. */
  14731. ggml_sycl_set_device(i);
  14732. SYCL_CHECK(CHECK_TRY_ERROR(
  14733. (*g_syclStreams[i][0])
  14734. .memcpy(extra->data_device[i], buf_host, original_size)
  14735. .wait()));
  14736. }
  14737. }
  14738. catch (sycl::exception const &exc) {
  14739. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14740. << ", line:" << __LINE__ << std::endl;
  14741. std::exit(1);
  14742. }
  14743. GGML_CALL static void
  14744. ggml_backend_sycl_split_buffer_get_tensor(ggml_backend_buffer_t buffer,
  14745. const ggml_tensor *tensor, void *data,
  14746. size_t offset, size_t size) try {
  14747. // split tensors must always be set in their entirety at once
  14748. GGML_ASSERT(offset == 0);
  14749. GGML_ASSERT(size == ggml_nbytes(tensor));
  14750. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14751. const int64_t ne0 = tensor->ne[0];
  14752. const size_t nb1 = tensor->nb[1];
  14753. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  14754. for (int i = 0; i < g_device_count; ++i) {
  14755. // int id = g_sycl_gpu_mgr->gpus[i];
  14756. int64_t row_low, row_high;
  14757. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14758. int64_t nrows_split = row_high - row_low;
  14759. if (nrows_split == 0) {
  14760. continue;
  14761. }
  14762. const size_t offset_split = row_low*nb1;
  14763. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14764. const size_t original_size = size;
  14765. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14766. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14767. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14768. }
  14769. char * buf_host = (char *)data + offset_split;
  14770. /*
  14771. DPCT1009:212: SYCL uses exceptions to report errors and does not use the
  14772. error codes. The original code was commented out and a warning string
  14773. was inserted. You need to rewrite this code.
  14774. */
  14775. ggml_sycl_set_device(i);
  14776. SYCL_CHECK(CHECK_TRY_ERROR(
  14777. (*g_syclStreams[i][0])
  14778. .memcpy(buf_host, extra->data_device[i], original_size)
  14779. .wait()));
  14780. }
  14781. }
  14782. catch (sycl::exception const &exc) {
  14783. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14784. << ", line:" << __LINE__ << std::endl;
  14785. std::exit(1);
  14786. }
  14787. GGML_CALL static void ggml_backend_sycl_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  14788. UNUSED(buffer);
  14789. UNUSED(value);
  14790. }
  14791. static struct ggml_backend_buffer_i ggml_backend_sycl_split_buffer_interface = {
  14792. /* .get_name = */ ggml_backend_sycl_split_buffer_get_name,
  14793. /* .free_buffer = */ ggml_backend_sycl_split_buffer_free_buffer,
  14794. /* .get_base = */ ggml_backend_sycl_split_buffer_get_base,
  14795. /* .init_tensor = */ ggml_backend_sycl_split_buffer_init_tensor,
  14796. /* .set_tensor = */ ggml_backend_sycl_split_buffer_set_tensor,
  14797. /* .get_tensor = */ ggml_backend_sycl_split_buffer_get_tensor,
  14798. /* .cpy_tensor = */ NULL,
  14799. /* .clear = */ ggml_backend_sycl_split_buffer_clear,
  14800. /* .reset = */ NULL,
  14801. };
  14802. GGML_CALL static const char * ggml_backend_sycl_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14803. return GGML_SYCL_NAME "_Split";
  14804. UNUSED(buft);
  14805. }
  14806. GGML_CALL static ggml_backend_buffer_t ggml_backend_sycl_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  14807. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  14808. // instead, we allocate them for each tensor separately in init_tensor
  14809. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  14810. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  14811. ggml_backend_sycl_split_buffer_context * ctx = new ggml_backend_sycl_split_buffer_context();
  14812. return ggml_backend_buffer_init(buft, ggml_backend_sycl_split_buffer_interface, ctx, size);
  14813. }
  14814. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  14815. return 128;
  14816. UNUSED(buft);
  14817. }
  14818. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  14819. ggml_backend_sycl_split_buffer_type_context * ctx = (ggml_backend_sycl_split_buffer_type_context *)buft->context;
  14820. size_t total_size = 0;
  14821. const int64_t ne0 = tensor->ne[0];
  14822. for (int i = 0; i < g_device_count; ++i) {
  14823. // int id = g_sycl_gpu_mgr->gpus[i];
  14824. int64_t row_low, row_high;
  14825. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, i);
  14826. int64_t nrows_split = row_high - row_low;
  14827. if (nrows_split == 0) {
  14828. continue;
  14829. }
  14830. total_size += ggml_nbytes_split(tensor, nrows_split);
  14831. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14832. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14833. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14834. }
  14835. }
  14836. return total_size;
  14837. }
  14838. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  14839. return ggml_backend_is_sycl(backend);
  14840. UNUSED(buft);
  14841. }
  14842. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  14843. return false;
  14844. UNUSED(buft);
  14845. }
  14846. static ggml_backend_buffer_type_i ggml_backend_sycl_split_buffer_type_interface = {
  14847. /* .get_name = */ ggml_backend_sycl_split_buffer_type_name,
  14848. /* .alloc_buffer = */ ggml_backend_sycl_split_buffer_type_alloc_buffer,
  14849. /* .get_alignment = */ ggml_backend_sycl_split_buffer_type_get_alignment,
  14850. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  14851. /* .get_alloc_size = */ ggml_backend_sycl_split_buffer_type_get_alloc_size,
  14852. /* .supports_backend = */ ggml_backend_sycl_split_buffer_type_supports_backend,
  14853. /* .is_host = */ ggml_backend_sycl_split_buffer_type_is_host,
  14854. };
  14855. GGML_CALL ggml_backend_buffer_type_t ggml_backend_sycl_split_buffer_type(const float * tensor_split) {
  14856. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_split_buffer_type\n");
  14857. ggml_init_sycl();
  14858. // FIXME: this is not thread safe
  14859. static std::map<std::array<float, GGML_SYCL_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  14860. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split_arr = {};
  14861. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_SYCL_MAX_DEVICES, [](float x) { return x == 0.0f; });
  14862. if (all_zero) {
  14863. tensor_split_arr = g_default_tensor_split;
  14864. } else {
  14865. float split_sum = 0.0f;
  14866. for (int i = 0; i < g_device_count; ++i) {
  14867. // int id = g_sycl_gpu_mgr->gpus[i];
  14868. tensor_split_arr[i] = split_sum;
  14869. split_sum += tensor_split[i];
  14870. }
  14871. for (int i = 0; i < g_device_count; ++i) {
  14872. // int id = g_sycl_gpu_mgr->gpus[i];
  14873. tensor_split_arr[i] /= split_sum;
  14874. }
  14875. }
  14876. auto it = buft_map.find(tensor_split_arr);
  14877. if (it != buft_map.end()) {
  14878. return &it->second;
  14879. }
  14880. struct ggml_backend_buffer_type buft {
  14881. /* .iface = */ ggml_backend_sycl_split_buffer_type_interface,
  14882. /* .context = */ new ggml_backend_sycl_split_buffer_type_context{tensor_split_arr},
  14883. };
  14884. auto result = buft_map.emplace(tensor_split_arr, buft);
  14885. return &result.first->second;
  14886. }
  14887. // host buffer type
  14888. GGML_CALL static const char * ggml_backend_sycl_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14889. return GGML_SYCL_NAME "_Host";
  14890. UNUSED(buft);
  14891. }
  14892. GGML_CALL static const char * ggml_backend_sycl_host_buffer_name(ggml_backend_buffer_t buffer) {
  14893. return GGML_SYCL_NAME "_Host";
  14894. UNUSED(buffer);
  14895. }
  14896. static void ggml_backend_sycl_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  14897. ggml_sycl_host_free(buffer->context);
  14898. }
  14899. static ggml_backend_buffer_t ggml_backend_sycl_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  14900. void * ptr = ggml_sycl_host_malloc(size);
  14901. if (ptr == nullptr) {
  14902. // fallback to cpu buffer
  14903. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  14904. }
  14905. // FIXME: this is a hack to avoid having to implement a new buffer type
  14906. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  14907. buffer->buft = buft;
  14908. buffer->iface.get_name = ggml_backend_sycl_host_buffer_name;
  14909. buffer->iface.free_buffer = ggml_backend_sycl_host_buffer_free_buffer;
  14910. return buffer;
  14911. }
  14912. ggml_backend_buffer_type_t ggml_backend_sycl_host_buffer_type() {
  14913. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_host_buffer_type\n");
  14914. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_type_host = {
  14915. /* .iface = */ {
  14916. /* .get_name = */ ggml_backend_sycl_host_buffer_type_name,
  14917. /* .alloc_buffer = */ ggml_backend_sycl_host_buffer_type_alloc_buffer,
  14918. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  14919. /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
  14920. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  14921. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  14922. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  14923. },
  14924. /* .context = */ nullptr,
  14925. };
  14926. return &ggml_backend_sycl_buffer_type_host;
  14927. }
  14928. // backend
  14929. GGML_CALL static const char * ggml_backend_sycl_name(ggml_backend_t backend) {
  14930. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14931. return sycl_ctx->name.c_str();
  14932. }
  14933. GGML_CALL static void ggml_backend_sycl_free(ggml_backend_t backend) {
  14934. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14935. delete sycl_ctx;
  14936. delete backend;
  14937. }
  14938. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_sycl_get_default_buffer_type(ggml_backend_t backend) {
  14939. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14940. return ggml_backend_sycl_buffer_type(sycl_ctx->device);
  14941. }
  14942. GGML_CALL static void ggml_backend_sycl_set_tensor_async(ggml_backend_t backend,
  14943. ggml_tensor *tensor,
  14944. const void *data, size_t offset,
  14945. size_t size) try {
  14946. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14947. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  14948. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14949. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14950. (char *)tensor->data + offset, data, size).wait()));
  14951. }
  14952. catch (sycl::exception const &exc) {
  14953. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14954. << ", line:" << __LINE__ << std::endl;
  14955. std::exit(1);
  14956. }
  14957. GGML_CALL static void ggml_backend_sycl_get_tensor_async(ggml_backend_t backend,
  14958. const ggml_tensor *tensor,
  14959. void *data, size_t offset,
  14960. size_t size) try {
  14961. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14962. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  14963. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14964. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14965. data, (const char *)tensor->data + offset, size).wait()));
  14966. }
  14967. catch (sycl::exception const &exc) {
  14968. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14969. << ", line:" << __LINE__ << std::endl;
  14970. std::exit(1);
  14971. }
  14972. GGML_CALL static bool ggml_backend_sycl_cpy_tensor_async(ggml_backend_t backend,
  14973. const ggml_tensor *src,
  14974. ggml_tensor *dst) try {
  14975. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14976. if (dst->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && ggml_backend_buffer_is_sycl(src->buffer)) {
  14977. /*
  14978. DPCT1009:215: SYCL uses exceptions to report errors and does not use the
  14979. error codes. The original code was commented out and a warning string
  14980. was inserted. You need to rewrite this code.
  14981. */
  14982. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14983. dst->data, src->data, ggml_nbytes(dst)).wait()));
  14984. return true;
  14985. }
  14986. return false;
  14987. }
  14988. catch (sycl::exception const &exc) {
  14989. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14990. << ", line:" << __LINE__ << std::endl;
  14991. std::exit(1);
  14992. }
  14993. static void ggml_backend_sycl_synchronize(ggml_backend_t backend) try {
  14994. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14995. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->wait()));
  14996. UNUSED(backend);
  14997. }
  14998. catch (sycl::exception const &exc) {
  14999. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  15000. << ", line:" << __LINE__ << std::endl;
  15001. std::exit(1);
  15002. }
  15003. GGML_CALL static ggml_status ggml_backend_sycl_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  15004. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  15005. ggml_sycl_set_main_device(sycl_ctx->device);
  15006. ggml_compute_params params = {};
  15007. params.type = GGML_TASK_TYPE_COMPUTE;
  15008. params.ith = 0;
  15009. for (int i = 0; i < cgraph->n_nodes; i++) {
  15010. ggml_tensor * node = cgraph->nodes[i];
  15011. 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) {
  15012. continue;
  15013. }
  15014. #ifndef NDEBUG
  15015. assert(node->backend == GGML_BACKEND_TYPE_GPU || node->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  15016. assert(node->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  15017. assert(node->extra != nullptr);
  15018. for (int j = 0; j < GGML_MAX_SRC; j++) {
  15019. if (node->src[j] != nullptr) {
  15020. assert(node->src[j]->backend == GGML_BACKEND_TYPE_GPU || node->src[j]->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  15021. assert(node->src[j]->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  15022. assert(node->src[j]->extra != nullptr);
  15023. }
  15024. }
  15025. #endif
  15026. bool ok = ggml_sycl_compute_forward(&params, node);
  15027. if (!ok) {
  15028. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  15029. }
  15030. GGML_ASSERT(ok);
  15031. }
  15032. return GGML_STATUS_SUCCESS;
  15033. }
  15034. GGML_CALL static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  15035. switch (op->op) {
  15036. case GGML_OP_UNARY:
  15037. switch (ggml_get_unary_op(op)) {
  15038. case GGML_UNARY_OP_GELU:
  15039. case GGML_UNARY_OP_SILU:
  15040. case GGML_UNARY_OP_RELU:
  15041. case GGML_UNARY_OP_HARDSIGMOID:
  15042. case GGML_UNARY_OP_HARDSWISH:
  15043. case GGML_UNARY_OP_GELU_QUICK:
  15044. case GGML_UNARY_OP_TANH:
  15045. return true;
  15046. default:
  15047. return false;
  15048. }
  15049. break;
  15050. case GGML_OP_MUL_MAT:
  15051. case GGML_OP_MUL_MAT_ID:
  15052. {
  15053. struct ggml_tensor * a;
  15054. struct ggml_tensor * b;
  15055. if (op->op == GGML_OP_MUL_MAT) {
  15056. a = op->src[0];
  15057. b = op->src[1];
  15058. } else {
  15059. a = op->src[2];
  15060. b = op->src[1];
  15061. }
  15062. if (a->ne[3] != b->ne[3]) {
  15063. return false;
  15064. }
  15065. ggml_type a_type = a->type;
  15066. if (a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ4_XS ||
  15067. a_type == GGML_TYPE_IQ3_XXS || a_type == GGML_TYPE_IQ3_S ||
  15068. a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ2_S ||
  15069. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ1_M
  15070. ) {
  15071. if (b->ne[1] == 1 && ggml_nrows(b) > 1) {
  15072. return false;
  15073. }
  15074. }
  15075. return true;
  15076. } break;
  15077. case GGML_OP_GET_ROWS:
  15078. {
  15079. switch (op->src[0]->type) {
  15080. case GGML_TYPE_F16:
  15081. case GGML_TYPE_F32:
  15082. case GGML_TYPE_Q4_0:
  15083. case GGML_TYPE_Q4_1:
  15084. case GGML_TYPE_Q5_0:
  15085. case GGML_TYPE_Q5_1:
  15086. case GGML_TYPE_Q8_0:
  15087. return true;
  15088. default:
  15089. return false;
  15090. }
  15091. } break;
  15092. case GGML_OP_CPY:
  15093. {
  15094. ggml_type src0_type = op->src[0]->type;
  15095. ggml_type src1_type = op->src[1]->type;
  15096. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  15097. return true;
  15098. }
  15099. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  15100. return true;
  15101. }
  15102. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  15103. return true;
  15104. }
  15105. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  15106. return true;
  15107. }
  15108. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  15109. return true;
  15110. }
  15111. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  15112. return true;
  15113. }
  15114. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  15115. return true;
  15116. }
  15117. return false;
  15118. } break;
  15119. case GGML_OP_CONCAT:
  15120. {
  15121. ggml_type src0_type = op->src[0]->type;
  15122. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  15123. } break;
  15124. case GGML_OP_DUP:
  15125. case GGML_OP_NONE:
  15126. case GGML_OP_RESHAPE:
  15127. case GGML_OP_REPEAT:
  15128. case GGML_OP_VIEW:
  15129. case GGML_OP_PERMUTE:
  15130. case GGML_OP_TRANSPOSE:
  15131. case GGML_OP_NORM:
  15132. case GGML_OP_ADD:
  15133. case GGML_OP_MUL:
  15134. case GGML_OP_DIV:
  15135. case GGML_OP_RMS_NORM:
  15136. case GGML_OP_SCALE:
  15137. case GGML_OP_SQR:
  15138. case GGML_OP_CLAMP:
  15139. case GGML_OP_CONT:
  15140. case GGML_OP_DIAG_MASK_INF:
  15141. case GGML_OP_SOFT_MAX:
  15142. case GGML_OP_ROPE:
  15143. case GGML_OP_ALIBI:
  15144. case GGML_OP_IM2COL:
  15145. case GGML_OP_POOL_2D:
  15146. case GGML_OP_SUM_ROWS:
  15147. case GGML_OP_ARGSORT:
  15148. case GGML_OP_ACC:
  15149. case GGML_OP_GROUP_NORM:
  15150. case GGML_OP_UPSCALE:
  15151. case GGML_OP_PAD:
  15152. case GGML_OP_LEAKY_RELU:
  15153. return true;
  15154. default:
  15155. return false;
  15156. }
  15157. UNUSED(backend);
  15158. }
  15159. GGML_CALL static bool ggml_backend_sycl_offload_op(ggml_backend_t backend, const ggml_tensor * op) {
  15160. const int min_batch_size = 32;
  15161. return op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS;
  15162. GGML_UNUSED(backend);
  15163. }
  15164. static ggml_backend_i ggml_backend_sycl_interface = {
  15165. /* .get_name = */ ggml_backend_sycl_name,
  15166. /* .free = */ ggml_backend_sycl_free,
  15167. /* .get_default_buffer_type = */ ggml_backend_sycl_get_default_buffer_type,
  15168. /* .set_tensor_async = */ ggml_backend_sycl_set_tensor_async,
  15169. /* .get_tensor_async = */ ggml_backend_sycl_get_tensor_async,
  15170. /* .cpy_tensor_async = */ NULL, //ggml_backend_sycl_cpy_tensor_async, // TODO: update for the new interface
  15171. /* .synchronize = */ ggml_backend_sycl_synchronize,
  15172. /* .graph_plan_create = */ NULL,
  15173. /* .graph_plan_free = */ NULL,
  15174. /* .graph_plan_compute = */ NULL,
  15175. /* .graph_compute = */ ggml_backend_sycl_graph_compute,
  15176. /* .supports_op = */ ggml_backend_sycl_supports_op,
  15177. /* .offload_op = */ ggml_backend_sycl_offload_op,
  15178. /* .event_new = */ NULL,
  15179. /* .event_free = */ NULL,
  15180. /* .event_record = */ NULL,
  15181. /* .event_wait = */ NULL,
  15182. /* .event_synchronize = */ NULL,
  15183. };
  15184. static ggml_guid_t ggml_backend_sycl_guid() {
  15185. static ggml_guid guid = { 0x58, 0x05, 0x13, 0x8f, 0xcd, 0x3a, 0x61, 0x9d, 0xe7, 0xcd, 0x98, 0xa9, 0x03, 0xfd, 0x7c, 0x53 };
  15186. return &guid;
  15187. }
  15188. GGML_CALL ggml_backend_t ggml_backend_sycl_init(int device) {
  15189. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_init\n");
  15190. ggml_init_sycl();
  15191. check_allow_gpu_index(device);
  15192. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  15193. ggml_sycl_set_main_device(device);
  15194. int id = g_sycl_gpu_mgr->gpus[device];
  15195. ggml_backend_sycl_context * ctx = new ggml_backend_sycl_context {
  15196. /* .device = */ device,
  15197. /* .name = */ GGML_SYCL_NAME + std::to_string(id),
  15198. };
  15199. ggml_backend_t sycl_backend = new ggml_backend {
  15200. /* .guid = */ ggml_backend_sycl_guid(),
  15201. /* .interface = */ ggml_backend_sycl_interface,
  15202. /* .context = */ ctx
  15203. };
  15204. return sycl_backend;
  15205. }
  15206. bool ggml_backend_is_sycl(ggml_backend_t backend) {
  15207. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_sycl_guid());
  15208. }
  15209. GGML_CALL int ggml_backend_sycl_get_device_count() {
  15210. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_count\n");
  15211. if (!g_sycl_gpu_mgr) g_sycl_gpu_mgr = new sycl_gpu_mgr();
  15212. return g_sycl_gpu_mgr->get_gpu_count();
  15213. }
  15214. GGML_CALL static ggml_backend_t ggml_backend_reg_sycl_init(const char * params, void * user_data) {
  15215. ggml_backend_t sycl_backend = ggml_backend_sycl_init((int) (intptr_t) user_data);
  15216. return sycl_backend;
  15217. UNUSED(params);
  15218. }
  15219. GGML_API GGML_CALL int ggml_backend_sycl_get_device_index(int device_id) {
  15220. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_index\n");
  15221. return g_sycl_gpu_mgr->get_index(device_id);
  15222. }
  15223. GGML_API GGML_CALL int ggml_backend_sycl_get_device_id(int device_index) {
  15224. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_id\n");
  15225. return g_sycl_gpu_mgr->gpus[device_index];
  15226. }
  15227. GGML_API GGML_CALL void ggml_backend_sycl_set_single_device_mode(int main_gpu_id) {
  15228. ggml_init_sycl();
  15229. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_set_single_device_mode\n");
  15230. fprintf(stderr, "ggml_backend_sycl_set_single_device: use single device: [%d]\n", main_gpu_id);
  15231. GGML_ASSERT(main_gpu_id<g_all_sycl_device_count);
  15232. if (g_sycl_gpu_mgr) {
  15233. delete g_sycl_gpu_mgr;
  15234. }
  15235. g_sycl_gpu_mgr = new sycl_gpu_mgr(main_gpu_id);
  15236. g_ggml_sycl_backend_gpu_mode = SYCL_SINGLE_GPU_MODE;
  15237. ggml_init_by_gpus(g_sycl_gpu_mgr->get_gpu_count());
  15238. g_ggml_backend_sycl_buffer_type_initialized = false;
  15239. }
  15240. GGML_API GGML_CALL void ggml_backend_sycl_set_mul_device_mode() {
  15241. ggml_init_sycl();
  15242. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_set_mul_device_mode\n");
  15243. if (g_ggml_sycl_backend_gpu_mode == SYCL_MUL_GPU_MODE) {
  15244. return;
  15245. }
  15246. fprintf(stderr, "ggml_backend_sycl_set_mul_device_mode: true\n");
  15247. if (g_sycl_gpu_mgr) {
  15248. delete g_sycl_gpu_mgr;
  15249. }
  15250. g_sycl_gpu_mgr = new sycl_gpu_mgr();
  15251. g_ggml_sycl_backend_gpu_mode = SYCL_MUL_GPU_MODE;
  15252. ggml_init_by_gpus(g_sycl_gpu_mgr->get_gpu_count());
  15253. g_ggml_backend_sycl_buffer_type_initialized = false;
  15254. }
  15255. extern "C" int ggml_backend_sycl_reg_devices();
  15256. int ggml_backend_sycl_reg_devices() {
  15257. ggml_backend_sycl_set_mul_device_mode();
  15258. assert(g_device_count>0);
  15259. for (int i = 0; i < g_device_count; i++) {
  15260. int id = g_sycl_gpu_mgr->gpus[i];
  15261. char name[128];
  15262. snprintf(name, sizeof(name), "%s%d", GGML_SYCL_NAME, id);
  15263. ggml_backend_register(name, ggml_backend_reg_sycl_init, ggml_backend_sycl_buffer_type(i), (void *) (intptr_t) i);
  15264. }
  15265. return g_device_count;
  15266. }