ggml-sycl.cpp 688 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512
  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 <float.h>
  18. #include <limits>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <vector>
  22. #include <cmath>
  23. #include <iostream>
  24. #include <fstream>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <sycl/sycl.hpp>
  28. #include <sycl/half_type.hpp>
  29. #include "ggml-sycl.h"
  30. #include "ggml.h"
  31. #include "ggml-backend-impl.h"
  32. /*
  33. Following definition copied from DPCT head files, which are used by ggml-sycl.cpp
  34. */
  35. // COPY from DPCT head files
  36. #include <sycl/sycl.hpp>
  37. #include <oneapi/mkl.hpp>
  38. #include <map>
  39. #if defined(__linux__)
  40. #include <sys/mman.h>
  41. #elif defined(_WIN64)
  42. #ifndef NOMINMAX
  43. #define NOMINMAX
  44. #endif
  45. #include <windows.h>
  46. #else
  47. #error "Only support Windows and Linux."
  48. #endif
  49. #if defined(__linux__)
  50. #include <unistd.h>
  51. #include <sys/syscall.h>
  52. #endif
  53. #if defined(_WIN64)
  54. #ifndef NOMINMAX
  55. #define NOMINMAX
  56. #endif
  57. #include <windows.h>
  58. #endif
  59. #define DPCT_COMPATIBILITY_TEMP (900)
  60. #if defined(_MSC_VER)
  61. #define __dpct_align__(n) __declspec(align(n))
  62. #define __dpct_inline__ __forceinline
  63. #else
  64. #define __dpct_align__(n) __attribute__((aligned(n)))
  65. #define __dpct_inline__ __inline__ __attribute__((always_inline))
  66. #endif
  67. #if defined(_MSC_VER)
  68. #define __dpct_noinline__ __declspec(noinline)
  69. #else
  70. #define __dpct_noinline__ __attribute__((noinline))
  71. #endif
  72. namespace dpct
  73. {
  74. typedef sycl::queue *queue_ptr;
  75. typedef sycl::event *event_ptr;
  76. typedef char *device_ptr;
  77. typedef uint8_t byte_t;
  78. typedef sycl::buffer<byte_t> buffer_t;
  79. /// SYCL default exception handler
  80. inline auto exception_handler = [](sycl::exception_list exceptions)
  81. {
  82. for (std::exception_ptr const &e : exceptions)
  83. {
  84. try
  85. {
  86. std::rethrow_exception(e);
  87. }
  88. catch (sycl::exception const &e)
  89. {
  90. std::cerr << "Caught asynchronous SYCL exception:" << std::endl
  91. << e.what() << std::endl
  92. << "Exception caught at file:" << __FILE__
  93. << ", line:" << __LINE__ << std::endl;
  94. }
  95. }
  96. };
  97. enum error_code
  98. {
  99. success = 0,
  100. default_error = 999
  101. };
  102. enum memcpy_direction
  103. {
  104. host_to_host,
  105. host_to_device,
  106. device_to_host,
  107. device_to_device,
  108. automatic
  109. };
  110. enum memory_region
  111. {
  112. global = 0, // device global memory
  113. constant, // device constant memory
  114. local, // device local memory
  115. shared, // memory which can be accessed by host and device
  116. };
  117. enum class library_data_t : unsigned char
  118. {
  119. real_float = 0,
  120. complex_float,
  121. real_double,
  122. complex_double,
  123. real_half,
  124. complex_half,
  125. real_bfloat16,
  126. complex_bfloat16,
  127. real_int4,
  128. complex_int4,
  129. real_uint4,
  130. complex_uint4,
  131. real_int8,
  132. complex_int8,
  133. real_uint8,
  134. complex_uint8,
  135. real_int16,
  136. complex_int16,
  137. real_uint16,
  138. complex_uint16,
  139. real_int32,
  140. complex_int32,
  141. real_uint32,
  142. complex_uint32,
  143. real_int64,
  144. complex_int64,
  145. real_uint64,
  146. complex_uint64,
  147. real_int8_4,
  148. real_int8_32,
  149. real_uint8_4,
  150. library_data_t_size
  151. };
  152. template <typename T>
  153. struct DataType
  154. {
  155. using T2 = T;
  156. };
  157. template <typename T>
  158. struct DataType<sycl::vec<T, 2>>
  159. {
  160. using T2 = std::complex<T>;
  161. };
  162. static void destroy_event(event_ptr event)
  163. {
  164. delete event;
  165. }
  166. static inline unsigned int get_tid()
  167. {
  168. #if defined(__linux__)
  169. return syscall(SYS_gettid);
  170. #elif defined(_WIN64)
  171. return GetCurrentThreadId();
  172. #else
  173. #error "Only support Windows and Linux."
  174. #endif
  175. }
  176. namespace detail
  177. {
  178. static void get_version(const sycl::device &dev, int &major, int &minor)
  179. {
  180. // Version string has the following format:
  181. // a. OpenCL<space><major.minor><space><vendor-specific-information>
  182. // b. <major.minor>
  183. std::string ver;
  184. ver = dev.get_info<sycl::info::device::version>();
  185. std::string::size_type i = 0;
  186. while (i < ver.size())
  187. {
  188. if (isdigit(ver[i]))
  189. break;
  190. i++;
  191. }
  192. major = std::stoi(&(ver[i]));
  193. while (i < ver.size())
  194. {
  195. if (ver[i] == '.')
  196. break;
  197. i++;
  198. }
  199. i++;
  200. minor = std::stoi(&(ver[i]));
  201. }
  202. template <typename tag, typename T>
  203. class generic_error_type
  204. {
  205. public:
  206. generic_error_type() = default;
  207. generic_error_type(T value) : value{value} {}
  208. operator T() const { return value; }
  209. private:
  210. T value;
  211. };
  212. } // namespace detail
  213. /// Pitched 2D/3D memory data.
  214. class pitched_data
  215. {
  216. public:
  217. pitched_data() : pitched_data(nullptr, 0, 0, 0) {}
  218. pitched_data(void *data, size_t pitch, size_t x, size_t y)
  219. : _data(data), _pitch(pitch), _x(x), _y(y) {}
  220. void *get_data_ptr() { return _data; }
  221. void set_data_ptr(void *data) { _data = data; }
  222. size_t get_pitch() { return _pitch; }
  223. void set_pitch(size_t pitch) { _pitch = pitch; }
  224. size_t get_x() { return _x; }
  225. void set_x(size_t x) { _x = x; };
  226. size_t get_y() { return _y; }
  227. void set_y(size_t y) { _y = y; }
  228. private:
  229. void *_data;
  230. size_t _pitch, _x, _y;
  231. };
  232. class device_info
  233. {
  234. public:
  235. // get interface
  236. const char *get_name() const { return _name; }
  237. char *get_name() { return _name; }
  238. template <typename WorkItemSizesTy = sycl::range<3>,
  239. std::enable_if_t<std::is_same_v<WorkItemSizesTy, sycl::range<3>> ||
  240. std::is_same_v<WorkItemSizesTy, int *>,
  241. int> = 0>
  242. auto get_max_work_item_sizes() const
  243. {
  244. if constexpr (std::is_same_v<WorkItemSizesTy, sycl::range<3>>)
  245. return sycl::range<3>(_max_work_item_sizes_i[0],
  246. _max_work_item_sizes_i[1],
  247. _max_work_item_sizes_i[2]);
  248. else
  249. {
  250. return _max_work_item_sizes_i;
  251. }
  252. }
  253. template <typename WorkItemSizesTy = sycl::range<3>,
  254. std::enable_if_t<std::is_same_v<WorkItemSizesTy, sycl::range<3>> ||
  255. std::is_same_v<WorkItemSizesTy, int *>,
  256. int> = 0>
  257. auto get_max_work_item_sizes()
  258. {
  259. if constexpr (std::is_same_v<WorkItemSizesTy, sycl::range<3>>)
  260. return sycl::range<3>(_max_work_item_sizes_i[0],
  261. _max_work_item_sizes_i[1],
  262. _max_work_item_sizes_i[2]);
  263. else
  264. {
  265. return _max_work_item_sizes_i;
  266. }
  267. }
  268. bool get_host_unified_memory() const { return _host_unified_memory; }
  269. int get_major_version() const { return _major; }
  270. int get_minor_version() const { return _minor; }
  271. int get_integrated() const { return _integrated; }
  272. int get_max_clock_frequency() const { return _frequency; }
  273. int get_max_compute_units() const { return _max_compute_units; }
  274. int get_max_work_group_size() const { return _max_work_group_size; }
  275. int get_max_sub_group_size() const { return _max_sub_group_size; }
  276. int get_max_work_items_per_compute_unit() const
  277. {
  278. return _max_work_items_per_compute_unit;
  279. }
  280. int get_max_register_size_per_work_group() const
  281. {
  282. return _max_register_size_per_work_group;
  283. }
  284. template <typename NDRangeSizeTy = size_t *,
  285. std::enable_if_t<std::is_same_v<NDRangeSizeTy, size_t *> ||
  286. std::is_same_v<NDRangeSizeTy, int *>,
  287. int> = 0>
  288. auto get_max_nd_range_size() const
  289. {
  290. if constexpr (std::is_same_v<NDRangeSizeTy, size_t *>)
  291. return _max_nd_range_size;
  292. else
  293. return _max_nd_range_size_i;
  294. }
  295. template <typename NDRangeSizeTy = size_t *,
  296. std::enable_if_t<std::is_same_v<NDRangeSizeTy, size_t *> ||
  297. std::is_same_v<NDRangeSizeTy, int *>,
  298. int> = 0>
  299. auto get_max_nd_range_size()
  300. {
  301. if constexpr (std::is_same_v<NDRangeSizeTy, size_t *>)
  302. return _max_nd_range_size;
  303. else
  304. return _max_nd_range_size_i;
  305. }
  306. size_t get_global_mem_size() const { return _global_mem_size; }
  307. size_t get_local_mem_size() const { return _local_mem_size; }
  308. size_t get_max_mem_alloc_size() const { return _max_mem_alloc_size; }
  309. /// Returns the maximum clock rate of device's global memory in kHz. If
  310. /// compiler does not support this API then returns default value 3200000 kHz.
  311. unsigned int get_memory_clock_rate() const { return _memory_clock_rate; }
  312. /// Returns the maximum bus width between device and memory in bits. If
  313. /// compiler does not support this API then returns default value 64 bits.
  314. unsigned int get_memory_bus_width() const { return _memory_bus_width; }
  315. uint32_t get_device_id() const { return _device_id; }
  316. std::array<unsigned char, 16> get_uuid() const { return _uuid; }
  317. /// Returns global memory cache size in bytes.
  318. unsigned int get_global_mem_cache_size() const
  319. {
  320. return _global_mem_cache_size;
  321. }
  322. // set interface
  323. void set_name(const char *name)
  324. {
  325. size_t length = strlen(name);
  326. if (length < 256)
  327. {
  328. std::memcpy(_name, name, length + 1);
  329. }
  330. else
  331. {
  332. std::memcpy(_name, name, 255);
  333. _name[255] = '\0';
  334. }
  335. }
  336. void set_max_work_item_sizes(const sycl::range<3> max_work_item_sizes)
  337. {
  338. for (int i = 0; i < 3; ++i)
  339. _max_work_item_sizes_i[i] = max_work_item_sizes[i];
  340. }
  341. [[deprecated]] void
  342. set_max_work_item_sizes(const sycl::id<3> max_work_item_sizes)
  343. {
  344. for (int i = 0; i < 3; ++i)
  345. {
  346. _max_work_item_sizes_i[i] = max_work_item_sizes[i];
  347. }
  348. }
  349. void set_host_unified_memory(bool host_unified_memory)
  350. {
  351. _host_unified_memory = host_unified_memory;
  352. }
  353. void set_major_version(int major) { _major = major; }
  354. void set_minor_version(int minor) { _minor = minor; }
  355. void set_integrated(int integrated) { _integrated = integrated; }
  356. void set_max_clock_frequency(int frequency) { _frequency = frequency; }
  357. void set_max_compute_units(int max_compute_units)
  358. {
  359. _max_compute_units = max_compute_units;
  360. }
  361. void set_global_mem_size(size_t global_mem_size)
  362. {
  363. _global_mem_size = global_mem_size;
  364. }
  365. void set_local_mem_size(size_t local_mem_size)
  366. {
  367. _local_mem_size = local_mem_size;
  368. }
  369. void set_max_mem_alloc_size(size_t max_mem_alloc_size)
  370. {
  371. _max_mem_alloc_size = max_mem_alloc_size;
  372. }
  373. void set_max_work_group_size(int max_work_group_size)
  374. {
  375. _max_work_group_size = max_work_group_size;
  376. }
  377. void set_max_sub_group_size(int max_sub_group_size)
  378. {
  379. _max_sub_group_size = max_sub_group_size;
  380. }
  381. void
  382. set_max_work_items_per_compute_unit(int max_work_items_per_compute_unit)
  383. {
  384. _max_work_items_per_compute_unit = max_work_items_per_compute_unit;
  385. }
  386. void set_max_nd_range_size(int max_nd_range_size[])
  387. {
  388. for (int i = 0; i < 3; i++)
  389. {
  390. _max_nd_range_size[i] = max_nd_range_size[i];
  391. _max_nd_range_size_i[i] = max_nd_range_size[i];
  392. }
  393. }
  394. void set_memory_clock_rate(unsigned int memory_clock_rate)
  395. {
  396. _memory_clock_rate = memory_clock_rate;
  397. }
  398. void set_memory_bus_width(unsigned int memory_bus_width)
  399. {
  400. _memory_bus_width = memory_bus_width;
  401. }
  402. void
  403. set_max_register_size_per_work_group(int max_register_size_per_work_group)
  404. {
  405. _max_register_size_per_work_group = max_register_size_per_work_group;
  406. }
  407. void set_device_id(uint32_t device_id)
  408. {
  409. _device_id = device_id;
  410. }
  411. void set_uuid(std::array<unsigned char, 16> uuid)
  412. {
  413. _uuid = std::move(uuid);
  414. }
  415. void set_global_mem_cache_size(unsigned int global_mem_cache_size)
  416. {
  417. _global_mem_cache_size = global_mem_cache_size;
  418. }
  419. private:
  420. char _name[256];
  421. int _max_work_item_sizes_i[3];
  422. bool _host_unified_memory = false;
  423. int _major;
  424. int _minor;
  425. int _integrated = 0;
  426. int _frequency;
  427. // Set estimated value 3200000 kHz as default value.
  428. unsigned int _memory_clock_rate = 3200000;
  429. // Set estimated value 64 bits as default value.
  430. unsigned int _memory_bus_width = 64;
  431. unsigned int _global_mem_cache_size;
  432. int _max_compute_units;
  433. int _max_work_group_size;
  434. int _max_sub_group_size;
  435. int _max_work_items_per_compute_unit;
  436. int _max_register_size_per_work_group;
  437. size_t _global_mem_size;
  438. size_t _local_mem_size;
  439. size_t _max_mem_alloc_size;
  440. size_t _max_nd_range_size[3];
  441. int _max_nd_range_size_i[3];
  442. uint32_t _device_id;
  443. std::array<unsigned char, 16> _uuid;
  444. };
  445. static int get_major_version(const sycl::device &dev)
  446. {
  447. int major, minor;
  448. detail::get_version(dev, major, minor);
  449. return major;
  450. }
  451. static int get_minor_version(const sycl::device &dev)
  452. {
  453. int major, minor;
  454. detail::get_version(dev, major, minor);
  455. return minor;
  456. }
  457. static void get_device_info(device_info &out, const sycl::device &dev)
  458. {
  459. device_info prop;
  460. prop.set_name(dev.get_info<sycl::info::device::name>().c_str());
  461. int major, minor;
  462. detail::get_version(dev, major, minor);
  463. prop.set_major_version(major);
  464. prop.set_minor_version(minor);
  465. prop.set_max_work_item_sizes(
  466. #if (__SYCL_COMPILER_VERSION && __SYCL_COMPILER_VERSION < 20220902)
  467. // oneAPI DPC++ compiler older than 2022/09/02, where max_work_item_sizes
  468. // is an enum class element
  469. dev.get_info<sycl::info::device::max_work_item_sizes>());
  470. #else
  471. // SYCL 2020-conformant code, max_work_item_sizes is a struct templated by
  472. // an int
  473. dev.get_info<sycl::info::device::max_work_item_sizes<3>>());
  474. #endif
  475. prop.set_host_unified_memory(dev.has(sycl::aspect::usm_host_allocations));
  476. prop.set_max_clock_frequency(
  477. dev.get_info<sycl::info::device::max_clock_frequency>() * 1000);
  478. prop.set_max_compute_units(
  479. dev.get_info<sycl::info::device::max_compute_units>());
  480. prop.set_max_work_group_size(
  481. dev.get_info<sycl::info::device::max_work_group_size>());
  482. prop.set_global_mem_size(dev.get_info<sycl::info::device::global_mem_size>());
  483. prop.set_local_mem_size(dev.get_info<sycl::info::device::local_mem_size>());
  484. prop.set_max_mem_alloc_size(dev.get_info<sycl::info::device::max_mem_alloc_size>());
  485. #if (defined(SYCL_EXT_INTEL_DEVICE_INFO) && SYCL_EXT_INTEL_DEVICE_INFO >= 6)
  486. if (dev.has(sycl::aspect::ext_intel_memory_clock_rate))
  487. {
  488. unsigned int tmp =
  489. dev.get_info<sycl::ext::intel::info::device::memory_clock_rate>();
  490. if (tmp != 0)
  491. prop.set_memory_clock_rate(1000 * tmp);
  492. }
  493. if (dev.has(sycl::aspect::ext_intel_memory_bus_width))
  494. {
  495. prop.set_memory_bus_width(
  496. dev.get_info<sycl::ext::intel::info::device::memory_bus_width>());
  497. }
  498. if (dev.has(sycl::aspect::ext_intel_device_id))
  499. {
  500. prop.set_device_id(
  501. dev.get_info<sycl::ext::intel::info::device::device_id>());
  502. }
  503. if (dev.has(sycl::aspect::ext_intel_device_info_uuid))
  504. {
  505. prop.set_uuid(dev.get_info<sycl::ext::intel::info::device::uuid>());
  506. }
  507. #elif defined(_MSC_VER) && !defined(__clang__)
  508. #pragma message("get_device_info: querying memory_clock_rate and \
  509. memory_bus_width are not supported by the compiler used. \
  510. Use 3200000 kHz as memory_clock_rate default value. \
  511. Use 64 bits as memory_bus_width default value.")
  512. #else
  513. #warning "get_device_info: querying memory_clock_rate and \
  514. memory_bus_width are not supported by the compiler used. \
  515. Use 3200000 kHz as memory_clock_rate default value. \
  516. Use 64 bits as memory_bus_width default value."
  517. #endif
  518. size_t max_sub_group_size = 1;
  519. std::vector<size_t> sub_group_sizes =
  520. dev.get_info<sycl::info::device::sub_group_sizes>();
  521. for (const auto &sub_group_size : sub_group_sizes)
  522. {
  523. if (max_sub_group_size < sub_group_size)
  524. max_sub_group_size = sub_group_size;
  525. }
  526. prop.set_max_sub_group_size(max_sub_group_size);
  527. prop.set_max_work_items_per_compute_unit(
  528. dev.get_info<sycl::info::device::max_work_group_size>());
  529. int max_nd_range_size[] = {0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF};
  530. prop.set_max_nd_range_size(max_nd_range_size);
  531. // Estimates max register size per work group, feel free to update the value
  532. // according to device properties.
  533. prop.set_max_register_size_per_work_group(65536);
  534. prop.set_global_mem_cache_size(
  535. dev.get_info<sycl::info::device::global_mem_cache_size>());
  536. out = prop;
  537. }
  538. /// dpct device extension
  539. class device_ext : public sycl::device
  540. {
  541. typedef std::mutex mutex_type;
  542. public:
  543. device_ext() : sycl::device(), _ctx(*this) {}
  544. ~device_ext()
  545. {
  546. std::lock_guard<mutex_type> lock(m_mutex);
  547. clear_queues();
  548. }
  549. device_ext(const sycl::device &base) : sycl::device(base), _ctx(*this)
  550. {
  551. std::lock_guard<mutex_type> lock(m_mutex);
  552. init_queues();
  553. }
  554. int is_native_atomic_supported() { return 0; }
  555. int get_major_version() const
  556. {
  557. return dpct::get_major_version(*this);
  558. }
  559. int get_minor_version() const
  560. {
  561. return dpct::get_minor_version(*this);
  562. }
  563. int get_max_compute_units() const
  564. {
  565. return get_device_info().get_max_compute_units();
  566. }
  567. /// Return the maximum clock frequency of this device in KHz.
  568. int get_max_clock_frequency() const
  569. {
  570. return get_device_info().get_max_clock_frequency();
  571. }
  572. int get_integrated() const { return get_device_info().get_integrated(); }
  573. int get_max_sub_group_size() const
  574. {
  575. return get_device_info().get_max_sub_group_size();
  576. }
  577. int get_max_register_size_per_work_group() const
  578. {
  579. return get_device_info().get_max_register_size_per_work_group();
  580. }
  581. int get_max_work_group_size() const
  582. {
  583. return get_device_info().get_max_work_group_size();
  584. }
  585. int get_mem_base_addr_align() const
  586. {
  587. return get_info<sycl::info::device::mem_base_addr_align>();
  588. }
  589. size_t get_global_mem_size() const
  590. {
  591. return get_device_info().get_global_mem_size();
  592. }
  593. size_t get_max_mem_alloc_size() const
  594. {
  595. return get_device_info().get_max_mem_alloc_size();
  596. }
  597. /// Get the number of bytes of free and total memory on the SYCL device.
  598. /// \param [out] free_memory The number of bytes of free memory on the SYCL device.
  599. /// \param [out] total_memory The number of bytes of total memory on the SYCL device.
  600. void get_memory_info(size_t &free_memory, size_t &total_memory)
  601. {
  602. total_memory = get_device_info().get_global_mem_size();
  603. const char *warning_info = "get_memory_info: [warning] ext_intel_free_memory is not "
  604. "supported (export/set ZES_ENABLE_SYSMAN=1 to support), "
  605. "use total memory as free memory";
  606. #if (defined(__SYCL_COMPILER_VERSION) && __SYCL_COMPILER_VERSION >= 20221105)
  607. if (!has(sycl::aspect::ext_intel_free_memory))
  608. {
  609. std::cerr << warning_info << std::endl;
  610. free_memory = total_memory;
  611. }
  612. else
  613. {
  614. free_memory = get_info<sycl::ext::intel::info::device::free_memory>();
  615. }
  616. #else
  617. std::cerr << warning_info << std::endl;
  618. free_memory = total_memory;
  619. #if defined(_MSC_VER) && !defined(__clang__)
  620. #pragma message("Querying the number of bytes of free memory is not supported")
  621. #else
  622. #warning "Querying the number of bytes of free memory is not supported"
  623. #endif
  624. #endif
  625. }
  626. void get_device_info(device_info &out) const
  627. {
  628. dpct::get_device_info(out, *this);
  629. }
  630. device_info get_device_info() const
  631. {
  632. device_info prop;
  633. dpct::get_device_info(prop, *this);
  634. return prop;
  635. }
  636. void reset()
  637. {
  638. std::lock_guard<mutex_type> lock(m_mutex);
  639. clear_queues();
  640. init_queues();
  641. }
  642. sycl::queue &in_order_queue() { return *_q_in_order; }
  643. sycl::queue &out_of_order_queue() { return *_q_out_of_order; }
  644. sycl::queue &default_queue()
  645. {
  646. #ifdef DPCT_USM_LEVEL_NONE
  647. return out_of_order_queue();
  648. #else
  649. return in_order_queue();
  650. #endif // DPCT_USM_LEVEL_NONE
  651. }
  652. void queues_wait_and_throw()
  653. {
  654. std::unique_lock<mutex_type> lock(m_mutex);
  655. std::vector<std::shared_ptr<sycl::queue>> current_queues(
  656. _queues);
  657. lock.unlock();
  658. for (const auto &q : current_queues)
  659. {
  660. q->wait_and_throw();
  661. }
  662. // Guard the destruct of current_queues to make sure the ref count is safe.
  663. lock.lock();
  664. }
  665. sycl::queue *create_queue(bool enable_exception_handler = false)
  666. {
  667. #ifdef DPCT_USM_LEVEL_NONE
  668. return create_out_of_order_queue(enable_exception_handler);
  669. #else
  670. return create_in_order_queue(enable_exception_handler);
  671. #endif // DPCT_USM_LEVEL_NONE
  672. }
  673. sycl::queue *create_queue(sycl::context context, sycl::device device,
  674. bool enable_exception_handler = false) {
  675. return create_in_order_queue(context, device, enable_exception_handler);
  676. }
  677. sycl::queue *create_in_order_queue(bool enable_exception_handler = false) {
  678. std::lock_guard<mutex_type> lock(m_mutex);
  679. return create_queue_impl(enable_exception_handler,
  680. sycl::property::queue::in_order());
  681. }
  682. sycl::queue *create_in_order_queue(sycl::context context, sycl::device device,
  683. bool enable_exception_handler = false) {
  684. std::lock_guard<mutex_type> lock(m_mutex);
  685. return create_queue_impl(context, device, enable_exception_handler,
  686. sycl::property::queue::in_order());
  687. }
  688. sycl::queue *create_out_of_order_queue(bool enable_exception_handler = false) {
  689. std::lock_guard<mutex_type> lock(m_mutex);
  690. return create_queue_impl(enable_exception_handler);
  691. }
  692. void destroy_queue(sycl::queue *&queue)
  693. {
  694. std::lock_guard<mutex_type> lock(m_mutex);
  695. _queues.erase(std::remove_if(_queues.begin(), _queues.end(),
  696. [=](const std::shared_ptr<sycl::queue> &q) -> bool
  697. {
  698. return q.get() == queue;
  699. }),
  700. _queues.end());
  701. queue = nullptr;
  702. }
  703. void set_saved_queue(sycl::queue *q)
  704. {
  705. std::lock_guard<mutex_type> lock(m_mutex);
  706. _saved_queue = q;
  707. }
  708. sycl::queue *get_saved_queue() const
  709. {
  710. std::lock_guard<mutex_type> lock(m_mutex);
  711. return _saved_queue;
  712. }
  713. sycl::context get_context() const { return _ctx; }
  714. private:
  715. void clear_queues()
  716. {
  717. _queues.clear();
  718. _q_in_order = _q_out_of_order = _saved_queue = nullptr;
  719. }
  720. void init_queues()
  721. {
  722. _q_in_order = create_queue_impl(true, sycl::property::queue::in_order());
  723. _q_out_of_order = create_queue_impl(true);
  724. _saved_queue = &default_queue();
  725. }
  726. /// Caller should acquire resource \p m_mutex before calling this function.
  727. template <class... Properties>
  728. sycl::queue *create_queue_impl(bool enable_exception_handler,
  729. Properties... properties)
  730. {
  731. sycl::async_handler eh = {};
  732. if (enable_exception_handler)
  733. {
  734. eh = exception_handler;
  735. }
  736. _queues.push_back(std::make_shared<sycl::queue>(
  737. _ctx, *this, eh,
  738. sycl::property_list(
  739. #ifdef DPCT_PROFILING_ENABLED
  740. sycl::property::queue::enable_profiling(),
  741. #endif
  742. properties...)));
  743. return _queues.back().get();
  744. }
  745. template <class... Properties>
  746. sycl::queue *create_queue_impl(sycl::context context, sycl::device device,
  747. bool enable_exception_handler,
  748. Properties... properties) {
  749. sycl::async_handler eh = {};
  750. if (enable_exception_handler) {
  751. eh = exception_handler;
  752. }
  753. _queues.push_back(std::make_shared<sycl::queue>(
  754. context, device, eh,
  755. sycl::property_list(
  756. #ifdef DPCT_PROFILING_ENABLED
  757. sycl::property::queue::enable_profiling(),
  758. #endif
  759. properties...)));
  760. return _queues.back().get();
  761. }
  762. void get_version(int &major, int &minor) const
  763. {
  764. detail::get_version(*this, major, minor);
  765. }
  766. sycl::queue *_q_in_order, *_q_out_of_order;
  767. sycl::queue *_saved_queue;
  768. sycl::context _ctx;
  769. std::vector<std::shared_ptr<sycl::queue>> _queues;
  770. mutable mutex_type m_mutex;
  771. };
  772. /// device manager
  773. class dev_mgr
  774. {
  775. public:
  776. device_ext &current_device()
  777. {
  778. unsigned int dev_id = current_device_id();
  779. check_id(dev_id);
  780. return *_devs[dev_id];
  781. }
  782. device_ext &cpu_device() const
  783. {
  784. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  785. if (_cpu_device == -1)
  786. {
  787. throw std::runtime_error("no valid cpu device");
  788. }
  789. else
  790. {
  791. return *_devs[_cpu_device];
  792. }
  793. }
  794. device_ext &get_device(unsigned int id) const
  795. {
  796. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  797. check_id(id);
  798. return *_devs[id];
  799. }
  800. unsigned int current_device_id() const
  801. {
  802. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  803. auto it = _thread2dev_map.find(get_tid());
  804. if (it != _thread2dev_map.end())
  805. return it->second;
  806. return DEFAULT_DEVICE_ID;
  807. }
  808. /// Select device with a device ID.
  809. /// \param [in] id The id of the device which can
  810. /// be obtained through get_device_id(const sycl::device).
  811. void select_device(unsigned int id)
  812. {
  813. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  814. check_id(id);
  815. _thread2dev_map[get_tid()] = id;
  816. }
  817. unsigned int device_count() { return _devs.size(); }
  818. unsigned int get_device_id(const sycl::device &dev)
  819. {
  820. unsigned int id = 0;
  821. for (auto dev_item : _devs)
  822. {
  823. if (*dev_item == dev)
  824. {
  825. break;
  826. }
  827. id++;
  828. }
  829. return id;
  830. }
  831. template <class DeviceSelector>
  832. std::enable_if_t<
  833. std::is_invocable_r_v<int, DeviceSelector, const sycl::device &>>
  834. select_device(const DeviceSelector &selector = sycl::gpu_selector_v)
  835. {
  836. sycl::device selected_device = sycl::device(selector);
  837. unsigned int selected_device_id = get_device_id(selected_device);
  838. select_device(selected_device_id);
  839. }
  840. /// Returns the instance of device manager singleton.
  841. static dev_mgr &instance()
  842. {
  843. static dev_mgr d_m;
  844. return d_m;
  845. }
  846. dev_mgr(const dev_mgr &) = delete;
  847. dev_mgr &operator=(const dev_mgr &) = delete;
  848. dev_mgr(dev_mgr &&) = delete;
  849. dev_mgr &operator=(dev_mgr &&) = delete;
  850. private:
  851. mutable std::recursive_mutex m_mutex;
  852. dev_mgr()
  853. {
  854. sycl::device default_device =
  855. sycl::device(sycl::default_selector_v);
  856. _devs.push_back(std::make_shared<device_ext>(default_device));
  857. std::vector<sycl::device> sycl_all_devs =
  858. sycl::device::get_devices(sycl::info::device_type::all);
  859. // Collect other devices except for the default device.
  860. if (default_device.is_cpu())
  861. _cpu_device = 0;
  862. for (auto &dev : sycl_all_devs)
  863. {
  864. if (dev == default_device)
  865. {
  866. continue;
  867. }
  868. _devs.push_back(std::make_shared<device_ext>(dev));
  869. if (_cpu_device == -1 && dev.is_cpu())
  870. {
  871. _cpu_device = _devs.size() - 1;
  872. }
  873. }
  874. }
  875. void check_id(unsigned int id) const
  876. {
  877. if (id >= _devs.size())
  878. {
  879. throw std::runtime_error("invalid device id");
  880. }
  881. }
  882. std::vector<std::shared_ptr<device_ext>> _devs;
  883. /// DEFAULT_DEVICE_ID is used, if current_device_id() can not find current
  884. /// thread id in _thread2dev_map, which means default device should be used
  885. /// for the current thread.
  886. const unsigned int DEFAULT_DEVICE_ID = 0;
  887. /// thread-id to device-id map.
  888. std::map<unsigned int, unsigned int> _thread2dev_map;
  889. int _cpu_device = -1;
  890. };
  891. static inline sycl::queue &get_default_queue()
  892. {
  893. return dev_mgr::instance().current_device().default_queue();
  894. }
  895. namespace detail
  896. {
  897. enum class pointer_access_attribute
  898. {
  899. host_only = 0,
  900. device_only,
  901. host_device,
  902. end
  903. };
  904. static pointer_access_attribute get_pointer_attribute(sycl::queue &q,
  905. const void *ptr)
  906. {
  907. #ifdef DPCT_USM_LEVEL_NONE
  908. return mem_mgr::instance().is_device_ptr(ptr)
  909. ? pointer_access_attribute::device_only
  910. : pointer_access_attribute::host_only;
  911. #else
  912. switch (sycl::get_pointer_type(ptr, q.get_context()))
  913. {
  914. case sycl::usm::alloc::unknown:
  915. return pointer_access_attribute::host_only;
  916. case sycl::usm::alloc::device:
  917. return pointer_access_attribute::device_only;
  918. case sycl::usm::alloc::shared:
  919. case sycl::usm::alloc::host:
  920. return pointer_access_attribute::host_device;
  921. }
  922. #endif
  923. }
  924. template <typename ArgT>
  925. inline constexpr std::uint64_t get_type_combination_id(ArgT Val)
  926. {
  927. static_assert((unsigned char)library_data_t::library_data_t_size <=
  928. std::numeric_limits<unsigned char>::max() &&
  929. "library_data_t size exceeds limit.");
  930. static_assert(std::is_same_v<ArgT, library_data_t>, "Unsupported ArgT");
  931. return (std::uint64_t)Val;
  932. }
  933. template <typename FirstT, typename... RestT>
  934. inline constexpr std::uint64_t get_type_combination_id(FirstT FirstVal,
  935. RestT... RestVal)
  936. {
  937. static_assert((std::uint8_t)library_data_t::library_data_t_size <=
  938. std::numeric_limits<unsigned char>::max() &&
  939. "library_data_t size exceeds limit.");
  940. static_assert(sizeof...(RestT) <= 8 && "Too many parameters");
  941. static_assert(std::is_same_v<FirstT, library_data_t>, "Unsupported FirstT");
  942. return get_type_combination_id(RestVal...) << 8 | ((std::uint64_t)FirstVal);
  943. }
  944. class mem_mgr
  945. {
  946. mem_mgr()
  947. {
  948. // Reserved address space, no real memory allocation happens here.
  949. #if defined(__linux__)
  950. mapped_address_space =
  951. (byte_t *)mmap(nullptr, mapped_region_size, PROT_NONE,
  952. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  953. #elif defined(_WIN64)
  954. mapped_address_space = (byte_t *)VirtualAlloc(
  955. NULL, // NULL specified as the base address parameter
  956. mapped_region_size, // Size of allocation
  957. MEM_RESERVE, // Allocate reserved pages
  958. PAGE_NOACCESS); // Protection = no access
  959. #else
  960. #error "Only support Windows and Linux."
  961. #endif
  962. next_free = mapped_address_space;
  963. };
  964. public:
  965. using buffer_id_t = int;
  966. struct allocation
  967. {
  968. buffer_t buffer;
  969. byte_t *alloc_ptr;
  970. size_t size;
  971. };
  972. ~mem_mgr()
  973. {
  974. #if defined(__linux__)
  975. munmap(mapped_address_space, mapped_region_size);
  976. #elif defined(_WIN64)
  977. VirtualFree(mapped_address_space, 0, MEM_RELEASE);
  978. #else
  979. #error "Only support Windows and Linux."
  980. #endif
  981. };
  982. mem_mgr(const mem_mgr &) = delete;
  983. mem_mgr &operator=(const mem_mgr &) = delete;
  984. mem_mgr(mem_mgr &&) = delete;
  985. mem_mgr &operator=(mem_mgr &&) = delete;
  986. /// Allocate
  987. void *mem_alloc(size_t size)
  988. {
  989. if (!size)
  990. return nullptr;
  991. std::lock_guard<std::mutex> lock(m_mutex);
  992. if (next_free + size > mapped_address_space + mapped_region_size)
  993. {
  994. throw std::runtime_error("dpct_malloc: out of memory for virtual memory pool");
  995. }
  996. // Allocation
  997. sycl::range<1> r(size);
  998. buffer_t buf(r);
  999. allocation A{buf, next_free, size};
  1000. // Map allocation to device pointer
  1001. void *result = next_free;
  1002. m_map.emplace(next_free + size, A);
  1003. // Update pointer to the next free space.
  1004. next_free += (size + extra_padding + alignment - 1) & ~(alignment - 1);
  1005. return result;
  1006. }
  1007. /// Deallocate
  1008. void mem_free(const void *ptr)
  1009. {
  1010. if (!ptr)
  1011. return;
  1012. std::lock_guard<std::mutex> lock(m_mutex);
  1013. auto it = get_map_iterator(ptr);
  1014. m_map.erase(it);
  1015. }
  1016. /// map: device pointer -> allocation(buffer, alloc_ptr, size)
  1017. allocation translate_ptr(const void *ptr)
  1018. {
  1019. std::lock_guard<std::mutex> lock(m_mutex);
  1020. auto it = get_map_iterator(ptr);
  1021. return it->second;
  1022. }
  1023. /// Check if the pointer represents device pointer or not.
  1024. bool is_device_ptr(const void *ptr) const
  1025. {
  1026. std::lock_guard<std::mutex> lock(m_mutex);
  1027. return (mapped_address_space <= ptr) &&
  1028. (ptr < mapped_address_space + mapped_region_size);
  1029. }
  1030. /// Returns the instance of memory manager singleton.
  1031. static mem_mgr &instance()
  1032. {
  1033. static mem_mgr m;
  1034. return m;
  1035. }
  1036. private:
  1037. std::map<byte_t *, allocation> m_map;
  1038. mutable std::mutex m_mutex;
  1039. byte_t *mapped_address_space;
  1040. byte_t *next_free;
  1041. const size_t mapped_region_size = 128ull * 1024 * 1024 * 1024;
  1042. const size_t alignment = 256;
  1043. /// This padding may be defined to some positive value to debug
  1044. /// out of bound accesses.
  1045. const size_t extra_padding = 0;
  1046. std::map<byte_t *, allocation>::iterator get_map_iterator(const void *ptr)
  1047. {
  1048. auto it = m_map.upper_bound((byte_t *)ptr);
  1049. if (it == m_map.end())
  1050. {
  1051. // Not a virtual pointer.
  1052. throw std::runtime_error("can not get buffer from non-virtual pointer");
  1053. }
  1054. const allocation &alloc = it->second;
  1055. if (ptr < alloc.alloc_ptr)
  1056. {
  1057. // Out of bound.
  1058. // This may happen if there's a gap between allocations due to alignment
  1059. // or extra padding and pointer points to this gap.
  1060. throw std::runtime_error("invalid virtual pointer");
  1061. }
  1062. return it;
  1063. }
  1064. };
  1065. template <class T, memory_region Memory, size_t Dimension>
  1066. class accessor;
  1067. template <memory_region Memory, class T = byte_t>
  1068. class memory_traits
  1069. {
  1070. public:
  1071. static constexpr sycl::access::target target =
  1072. sycl::access::target::device;
  1073. static constexpr sycl::access_mode mode =
  1074. (Memory == constant) ? sycl::access_mode::read
  1075. : sycl::access_mode::read_write;
  1076. static constexpr size_t type_size = sizeof(T);
  1077. using element_t =
  1078. typename std::conditional<Memory == constant, const T, T>::type;
  1079. using value_t = typename std::remove_cv<T>::type;
  1080. template <size_t Dimension = 1>
  1081. using accessor_t = typename std::conditional<
  1082. Memory == local, sycl::local_accessor<value_t, Dimension>,
  1083. sycl::accessor<T, Dimension, mode, target>>::type;
  1084. using pointer_t = T *;
  1085. };
  1086. static inline void *dpct_malloc(size_t size, sycl::queue &q)
  1087. {
  1088. #ifdef DPCT_USM_LEVEL_NONE
  1089. return mem_mgr::instance().mem_alloc(size * sizeof(byte_t));
  1090. #else
  1091. return sycl::malloc_device(size, q.get_device(), q.get_context());
  1092. #endif // DPCT_USM_LEVEL_NONE
  1093. }
  1094. #define PITCH_DEFAULT_ALIGN(x) (((x) + 31) & ~(0x1F))
  1095. static inline void *dpct_malloc(size_t &pitch, size_t x, size_t y, size_t z,
  1096. sycl::queue &q)
  1097. {
  1098. pitch = PITCH_DEFAULT_ALIGN(x);
  1099. return dpct_malloc(pitch * y * z, q);
  1100. }
  1101. /**
  1102. * @brief Sets \p value to the first \p size elements starting from \p dev_ptr in \p q.
  1103. * @tparam valueT The type of the element to be set.
  1104. * @param [in] q The queue in which the operation is done.
  1105. * @param [in] dev_ptr Pointer to the virtual device memory address.
  1106. * @param [in] value The value to be set.
  1107. * @param [in] size Number of elements to be set to the value.
  1108. * @return An event representing the memset operation.
  1109. */
  1110. template <typename valueT>
  1111. static inline sycl::event dpct_memset(sycl::queue &q, void *dev_ptr,
  1112. valueT value, size_t size)
  1113. {
  1114. #ifdef DPCT_USM_LEVEL_NONE
  1115. auto &mm = mem_mgr::instance();
  1116. assert(mm.is_device_ptr(dev_ptr));
  1117. auto alloc = mm.translate_ptr(dev_ptr);
  1118. size_t offset = (valueT *)dev_ptr - (valueT *)alloc.alloc_ptr;
  1119. return q.submit([&](sycl::handler &cgh)
  1120. {
  1121. auto r = sycl::range<1>(size);
  1122. auto o = sycl::id<1>(offset);
  1123. auto new_buffer = alloc.buffer.reinterpret<valueT>(
  1124. sycl::range<1>(alloc.size / sizeof(valueT)));
  1125. sycl::accessor<valueT, 1, sycl::access_mode::write,
  1126. sycl::access::target::device>
  1127. acc(new_buffer, cgh, r, o);
  1128. cgh.fill(acc, value); });
  1129. #else
  1130. return q.fill(dev_ptr, value, size);
  1131. #endif // DPCT_USM_LEVEL_NONE
  1132. }
  1133. /**
  1134. * @brief Sets \p value to the 3D memory region pointed by \p data in \p q.
  1135. * @tparam valueT The type of the element to be set.
  1136. * @param [in] q The queue in which the operation is done.
  1137. * @param [in] data Pointer to the pitched device memory region.
  1138. * @param [in] value The value to be set.
  1139. * @param [in] size 3D memory region by number of elements.
  1140. * @return An event list representing the memset operations.
  1141. */
  1142. template <typename valueT>
  1143. static inline std::vector<sycl::event>
  1144. dpct_memset(sycl::queue &q, pitched_data data, valueT value,
  1145. sycl::range<3> size)
  1146. {
  1147. std::vector<sycl::event> event_list;
  1148. size_t slice = data.get_pitch() * data.get_y();
  1149. unsigned char *data_surface = (unsigned char *)data.get_data_ptr();
  1150. for (size_t z = 0; z < size.get(2); ++z)
  1151. {
  1152. unsigned char *data_ptr = data_surface;
  1153. for (size_t y = 0; y < size.get(1); ++y)
  1154. {
  1155. event_list.push_back(dpct_memset(q, data_ptr, value, size.get(0)));
  1156. data_ptr += data.get_pitch();
  1157. }
  1158. data_surface += slice;
  1159. }
  1160. return event_list;
  1161. }
  1162. /**
  1163. * @brief Sets \p val to the pitched 2D memory region pointed by \p ptr in \p q.
  1164. * @tparam valueT The type of the element to be set.
  1165. * @param [in] q The queue in which the operation is done.
  1166. * @param [in] ptr Pointer to the virtual device memory.
  1167. * @param [in] pitch The pitch size by number of elements, including padding.
  1168. * @param [in] val The value to be set.
  1169. * @param [in] x The width of memory region by number of elements.
  1170. * @param [in] y The height of memory region by number of elements.
  1171. * @return An event list representing the memset operations.
  1172. */
  1173. template <typename valueT>
  1174. static inline std::vector<sycl::event>
  1175. dpct_memset(sycl::queue &q, void *ptr, size_t pitch, valueT val, size_t x,
  1176. size_t y)
  1177. {
  1178. return dpct_memset(q, pitched_data(ptr, pitch, x, 1), val,
  1179. sycl::range<3>(x, y, 1));
  1180. }
  1181. static memcpy_direction deduce_memcpy_direction(sycl::queue &q, void *to_ptr,
  1182. const void *from_ptr,
  1183. memcpy_direction dir)
  1184. {
  1185. switch (dir)
  1186. {
  1187. case memcpy_direction::host_to_host:
  1188. case memcpy_direction::host_to_device:
  1189. case memcpy_direction::device_to_host:
  1190. case memcpy_direction::device_to_device:
  1191. return dir;
  1192. case memcpy_direction::automatic:
  1193. {
  1194. // table[to_attribute][from_attribute]
  1195. static const memcpy_direction
  1196. direction_table[static_cast<unsigned>(pointer_access_attribute::end)]
  1197. [static_cast<unsigned>(pointer_access_attribute::end)] =
  1198. {{memcpy_direction::host_to_host,
  1199. memcpy_direction::device_to_host,
  1200. memcpy_direction::host_to_host},
  1201. {memcpy_direction::host_to_device,
  1202. memcpy_direction::device_to_device,
  1203. memcpy_direction::device_to_device},
  1204. {memcpy_direction::host_to_host,
  1205. memcpy_direction::device_to_device,
  1206. memcpy_direction::device_to_device}};
  1207. return direction_table[static_cast<unsigned>(get_pointer_attribute(
  1208. q, to_ptr))][static_cast<unsigned>(get_pointer_attribute(q, from_ptr))];
  1209. }
  1210. default:
  1211. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1212. }
  1213. }
  1214. static sycl::event
  1215. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1216. memcpy_direction direction,
  1217. const std::vector<sycl::event> &dep_events = {})
  1218. {
  1219. if (!size)
  1220. return sycl::event{};
  1221. #ifdef DPCT_USM_LEVEL_NONE
  1222. auto &mm = mem_mgr::instance();
  1223. auto real_direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1224. switch (real_direction)
  1225. {
  1226. case host_to_host:
  1227. return q.submit([&](sycl::handler &cgh)
  1228. {
  1229. cgh.depends_on(dep_events);
  1230. cgh.host_task([=] { std::memcpy(to_ptr, from_ptr, size); }); });
  1231. case host_to_device:
  1232. {
  1233. auto alloc = mm.translate_ptr(to_ptr);
  1234. size_t offset = (byte_t *)to_ptr - alloc.alloc_ptr;
  1235. return q.submit([&](sycl::handler &cgh)
  1236. {
  1237. cgh.depends_on(dep_events);
  1238. auto r = sycl::range<1>(size);
  1239. auto o = sycl::id<1>(offset);
  1240. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1241. sycl::access::target::device>
  1242. acc(alloc.buffer, cgh, r, o);
  1243. cgh.copy(from_ptr, acc); });
  1244. }
  1245. case device_to_host:
  1246. {
  1247. auto alloc = mm.translate_ptr(from_ptr);
  1248. size_t offset = (byte_t *)from_ptr - alloc.alloc_ptr;
  1249. return q.submit([&](sycl::handler &cgh)
  1250. {
  1251. cgh.depends_on(dep_events);
  1252. auto r = sycl::range<1>(size);
  1253. auto o = sycl::id<1>(offset);
  1254. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1255. sycl::access::target::device>
  1256. acc(alloc.buffer, cgh, r, o);
  1257. cgh.copy(acc, to_ptr); });
  1258. }
  1259. case device_to_device:
  1260. {
  1261. auto to_alloc = mm.translate_ptr(to_ptr);
  1262. auto from_alloc = mm.translate_ptr(from_ptr);
  1263. size_t to_offset = (byte_t *)to_ptr - to_alloc.alloc_ptr;
  1264. size_t from_offset = (byte_t *)from_ptr - from_alloc.alloc_ptr;
  1265. return q.submit([&](sycl::handler &cgh)
  1266. {
  1267. cgh.depends_on(dep_events);
  1268. auto r = sycl::range<1>(size);
  1269. auto to_o = sycl::id<1>(to_offset);
  1270. auto from_o = sycl::id<1>(from_offset);
  1271. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1272. sycl::access::target::device>
  1273. to_acc(to_alloc.buffer, cgh, r, to_o);
  1274. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1275. sycl::access::target::device>
  1276. from_acc(from_alloc.buffer, cgh, r, from_o);
  1277. cgh.copy(from_acc, to_acc); });
  1278. }
  1279. default:
  1280. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1281. }
  1282. #else
  1283. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  1284. GGML_UNUSED(direction);
  1285. #endif // DPCT_USM_LEVEL_NONE
  1286. }
  1287. // Get actual copy range and make sure it will not exceed range.
  1288. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  1289. size_t pitch)
  1290. {
  1291. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  1292. }
  1293. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  1294. size_t pitch)
  1295. {
  1296. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  1297. }
  1298. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  1299. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  1300. static inline std::vector<sycl::event>
  1301. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1302. sycl::range<3> to_range, sycl::range<3> from_range,
  1303. sycl::id<3> to_id, sycl::id<3> from_id,
  1304. sycl::range<3> size, memcpy_direction direction,
  1305. const std::vector<sycl::event> &dep_events = {})
  1306. {
  1307. // RAII for host pointer
  1308. class host_buffer
  1309. {
  1310. void *_buf;
  1311. size_t _size;
  1312. sycl::queue &_q;
  1313. const std::vector<sycl::event> &_deps; // free operation depends
  1314. public:
  1315. host_buffer(size_t size, sycl::queue &q,
  1316. const std::vector<sycl::event> &deps)
  1317. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  1318. void *get_ptr() const { return _buf; }
  1319. size_t get_size() const { return _size; }
  1320. ~host_buffer()
  1321. {
  1322. if (_buf)
  1323. {
  1324. _q.submit([&](sycl::handler &cgh)
  1325. {
  1326. cgh.depends_on(_deps);
  1327. cgh.host_task([buf = _buf] { std::free(buf); }); });
  1328. }
  1329. }
  1330. };
  1331. std::vector<sycl::event> event_list;
  1332. size_t to_slice = to_range.get(1) * to_range.get(0),
  1333. from_slice = from_range.get(1) * from_range.get(0);
  1334. unsigned char *to_surface =
  1335. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  1336. const unsigned char *from_surface =
  1337. (const unsigned char *)from_ptr +
  1338. get_offset(from_id, from_slice, from_range.get(0));
  1339. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  1340. {
  1341. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  1342. direction, dep_events)};
  1343. }
  1344. direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1345. size_t size_slice = size.get(1) * size.get(0);
  1346. switch (direction)
  1347. {
  1348. case host_to_host:
  1349. for (size_t z = 0; z < size.get(2); ++z)
  1350. {
  1351. unsigned char *to_ptr = to_surface;
  1352. const unsigned char *from_ptr = from_surface;
  1353. if (to_range.get(0) == from_range.get(0) &&
  1354. to_range.get(0) == size.get(0))
  1355. {
  1356. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  1357. direction, dep_events));
  1358. }
  1359. else
  1360. {
  1361. for (size_t y = 0; y < size.get(1); ++y)
  1362. {
  1363. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  1364. direction, dep_events));
  1365. to_ptr += to_range.get(0);
  1366. from_ptr += from_range.get(0);
  1367. }
  1368. }
  1369. to_surface += to_slice;
  1370. from_surface += from_slice;
  1371. }
  1372. break;
  1373. case host_to_device:
  1374. {
  1375. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  1376. event_list);
  1377. std::vector<sycl::event> host_events;
  1378. if (to_slice == size_slice)
  1379. {
  1380. // Copy host data to a temp host buffer with the shape of target.
  1381. host_events =
  1382. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  1383. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  1384. host_to_host, dep_events);
  1385. }
  1386. else
  1387. {
  1388. // Copy host data to a temp host buffer with the shape of target.
  1389. host_events = dpct_memcpy(
  1390. q, buf.get_ptr(), from_surface, to_range, from_range,
  1391. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  1392. // If has padding data, not sure whether it is useless. So fill temp
  1393. // buffer with it.
  1394. std::vector<sycl::event>{
  1395. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  1396. device_to_host, dep_events)});
  1397. }
  1398. // Copy from temp host buffer to device with only one submit.
  1399. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  1400. buf.get_size(), host_to_device,
  1401. host_events));
  1402. break;
  1403. }
  1404. case device_to_host:
  1405. {
  1406. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  1407. event_list);
  1408. // Copy from host temp buffer to host target with reshaping.
  1409. event_list = dpct_memcpy(
  1410. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  1411. sycl::id<3>(0, 0, 0), size, host_to_host,
  1412. // Copy from device to temp host buffer with only one submit.
  1413. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  1414. buf.get_size(),
  1415. device_to_host, dep_events)});
  1416. break;
  1417. }
  1418. case device_to_device:
  1419. #ifdef DPCT_USM_LEVEL_NONE
  1420. {
  1421. auto &mm = mem_mgr::instance();
  1422. auto to_alloc = mm.translate_ptr(to_surface);
  1423. auto from_alloc = mm.translate_ptr(from_surface);
  1424. size_t to_offset = (byte_t *)to_surface - to_alloc.alloc_ptr;
  1425. size_t from_offset = (byte_t *)from_surface - from_alloc.alloc_ptr;
  1426. event_list.push_back(q.submit([&](sycl::handler &cgh)
  1427. {
  1428. cgh.depends_on(dep_events);
  1429. auto to_o = sycl::id<1>(to_offset);
  1430. auto from_o = sycl::id<1>(from_offset);
  1431. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1432. sycl::access::target::device>
  1433. to_acc(to_alloc.buffer, cgh,
  1434. get_copy_range(size, to_slice, to_range.get(0)), to_o);
  1435. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1436. sycl::access::target::device>
  1437. from_acc(from_alloc.buffer, cgh,
  1438. get_copy_range(size, from_slice, from_range.get(0)), from_o);
  1439. cgh.parallel_for<class dpct_memcpy_3d_detail_usmnone>(
  1440. size,
  1441. [=](sycl::id<3> id) {
  1442. to_acc[get_offset(id, to_slice, to_range.get(0))] =
  1443. from_acc[get_offset(id, from_slice, from_range.get(0))];
  1444. }); }));
  1445. }
  1446. #else
  1447. event_list.push_back(q.submit([&](sycl::handler &cgh)
  1448. {
  1449. cgh.depends_on(dep_events);
  1450. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  1451. size,
  1452. [=](sycl::id<3> id) {
  1453. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  1454. from_surface[get_offset(id, from_slice, from_range.get(0))];
  1455. }); }));
  1456. #endif
  1457. break;
  1458. default:
  1459. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1460. }
  1461. return event_list;
  1462. }
  1463. /// memcpy 2D/3D matrix specified by pitched_data.
  1464. static inline std::vector<sycl::event>
  1465. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  1466. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  1467. memcpy_direction direction = automatic)
  1468. {
  1469. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  1470. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  1471. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  1472. size, direction);
  1473. }
  1474. /// memcpy 2D matrix with pitch.
  1475. static inline std::vector<sycl::event>
  1476. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1477. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  1478. memcpy_direction direction = automatic)
  1479. {
  1480. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  1481. sycl::range<3>(from_pitch, y, 1),
  1482. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  1483. sycl::range<3>(x, y, 1), direction);
  1484. }
  1485. namespace deprecated
  1486. {
  1487. template <typename T, sycl::usm::alloc AllocKind>
  1488. class usm_allocator
  1489. {
  1490. private:
  1491. using Alloc = sycl::usm_allocator<T, AllocKind>;
  1492. Alloc _impl;
  1493. public:
  1494. using value_type = typename std::allocator_traits<Alloc>::value_type;
  1495. using pointer = typename std::allocator_traits<Alloc>::pointer;
  1496. using const_pointer = typename std::allocator_traits<Alloc>::const_pointer;
  1497. using void_pointer = typename std::allocator_traits<Alloc>::void_pointer;
  1498. using const_void_pointer =
  1499. typename std::allocator_traits<Alloc>::const_void_pointer;
  1500. using reference = typename std::allocator_traits<Alloc>::value_type &;
  1501. using const_reference =
  1502. const typename std::allocator_traits<Alloc>::value_type &;
  1503. using difference_type =
  1504. typename std::allocator_traits<Alloc>::difference_type;
  1505. using size_type = typename std::allocator_traits<Alloc>::size_type;
  1506. using propagate_on_container_copy_assignment = typename std::allocator_traits<
  1507. Alloc>::propagate_on_container_copy_assignment;
  1508. using propagate_on_container_move_assignment = typename std::allocator_traits<
  1509. Alloc>::propagate_on_container_move_assignment;
  1510. using propagate_on_container_swap =
  1511. typename std::allocator_traits<Alloc>::propagate_on_container_swap;
  1512. using is_always_equal =
  1513. typename std::allocator_traits<Alloc>::is_always_equal;
  1514. template <typename U>
  1515. struct rebind
  1516. {
  1517. typedef usm_allocator<U, AllocKind> other;
  1518. };
  1519. usm_allocator() : _impl(dpct::get_default_queue()) {}
  1520. ~usm_allocator() {}
  1521. usm_allocator(const usm_allocator &other) : _impl(other._impl) {}
  1522. usm_allocator(usm_allocator &&other) : _impl(std::move(other._impl)) {}
  1523. pointer address(reference r) { return &r; }
  1524. const_pointer address(const_reference r) { return &r; }
  1525. pointer allocate(size_type cnt, const_void_pointer hint = nullptr)
  1526. {
  1527. return std::allocator_traits<Alloc>::allocate(_impl, cnt, hint);
  1528. }
  1529. void deallocate(pointer p, size_type cnt)
  1530. {
  1531. std::allocator_traits<Alloc>::deallocate(_impl, p, cnt);
  1532. }
  1533. size_type max_size() const
  1534. {
  1535. return std::allocator_traits<Alloc>::max_size(_impl);
  1536. }
  1537. bool operator==(const usm_allocator &other) const { return _impl == other._impl; }
  1538. bool operator!=(const usm_allocator &other) const { return _impl != other._impl; }
  1539. };
  1540. } // namespace deprecated
  1541. inline void dpct_free(void *ptr,
  1542. const sycl::queue &q)
  1543. {
  1544. if (ptr)
  1545. {
  1546. #ifdef DPCT_USM_LEVEL_NONE
  1547. detail::mem_mgr::instance().mem_free(ptr);
  1548. #else
  1549. sycl::free(ptr, q.get_context());
  1550. #endif // DPCT_USM_LEVEL_NONE
  1551. }
  1552. }
  1553. template <typename T>
  1554. inline auto get_memory(const void *x)
  1555. {
  1556. T *new_x = reinterpret_cast<T *>(const_cast<void *>(x));
  1557. #ifdef DPCT_USM_LEVEL_NONE
  1558. return dpct::get_buffer<std::remove_cv_t<T>>(new_x);
  1559. #else
  1560. return new_x;
  1561. #endif
  1562. }
  1563. template <typename T>
  1564. inline typename DataType<T>::T2 get_value(const T *s, sycl::queue &q)
  1565. {
  1566. using Ty = typename DataType<T>::T2;
  1567. Ty s_h;
  1568. if (get_pointer_attribute(q, s) == pointer_access_attribute::device_only)
  1569. detail::dpct_memcpy(q, (void *)&s_h, (const void *)s, sizeof(T), device_to_host)
  1570. .wait();
  1571. else
  1572. s_h = *reinterpret_cast<const Ty *>(s);
  1573. return s_h;
  1574. }
  1575. } // namespace detail
  1576. template <typename T>
  1577. inline auto get_value(const T *s, sycl::queue &q)
  1578. {
  1579. return detail::get_value(s, q);
  1580. }
  1581. namespace detail
  1582. {
  1583. template <class Ta, class Tb, class Tc, class Ts>
  1584. inline void gemm_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1585. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1586. const void *alpha, const void *a, int lda, const void *b,
  1587. int ldb, const void *beta, void *c, int ldc)
  1588. {
  1589. #ifndef __INTEL_MKL__
  1590. GGML_UNUSED(q);
  1591. GGML_UNUSED(a_trans);
  1592. GGML_UNUSED(b_trans);
  1593. GGML_UNUSED(m);
  1594. GGML_UNUSED(n);
  1595. GGML_UNUSED(k);
  1596. GGML_UNUSED(alpha);
  1597. GGML_UNUSED(a);
  1598. GGML_UNUSED(lda);
  1599. GGML_UNUSED(b);
  1600. GGML_UNUSED(ldb);
  1601. GGML_UNUSED(beta);
  1602. GGML_UNUSED(c);
  1603. GGML_UNUSED(ldc);
  1604. throw std::runtime_error("The oneAPI Math Kernel Library (oneMKL) Interfaces "
  1605. "Project does not support this API.");
  1606. #else
  1607. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1608. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1609. auto data_a = get_memory<const Ta>(a);
  1610. auto data_b = get_memory<const Tb>(b);
  1611. auto data_c = get_memory<Tc>(c);
  1612. oneapi::mkl::blas::column_major::gemm(
  1613. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1614. data_b, ldb, beta_value, data_c, ldc);
  1615. #endif
  1616. }
  1617. template <typename VecT, class BinaryOperation, class = void>
  1618. class vectorized_binary
  1619. {
  1620. public:
  1621. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1622. {
  1623. VecT v4;
  1624. for (size_t i = 0; i < v4.size(); ++i)
  1625. {
  1626. v4[i] = binary_op(a[i], b[i]);
  1627. }
  1628. return v4;
  1629. }
  1630. };
  1631. template <typename VecT, class BinaryOperation>
  1632. class vectorized_binary<
  1633. VecT, BinaryOperation,
  1634. std::void_t<std::invoke_result_t<BinaryOperation, VecT, VecT>>>
  1635. {
  1636. public:
  1637. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1638. {
  1639. return binary_op(a, b).template as<VecT>();
  1640. }
  1641. };
  1642. template <class Ta, class Tb, class Tc, class Ts>
  1643. inline void gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1644. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1645. const void *alpha, const void **a, int lda,
  1646. const void **b, int ldb, const void *beta, void **c,
  1647. int ldc, int batch_size)
  1648. {
  1649. struct matrix_info_t
  1650. {
  1651. oneapi::mkl::transpose transpose_info[2];
  1652. Ts value_info[2];
  1653. std::int64_t size_info[3];
  1654. std::int64_t ld_info[3];
  1655. std::int64_t groupsize_info;
  1656. };
  1657. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1658. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1659. matrix_info_t *matrix_info =
  1660. (matrix_info_t *)std::malloc(sizeof(matrix_info_t));
  1661. matrix_info->transpose_info[0] = a_trans;
  1662. matrix_info->transpose_info[1] = b_trans;
  1663. matrix_info->value_info[0] = alpha_value;
  1664. matrix_info->value_info[1] = beta_value;
  1665. matrix_info->size_info[0] = m;
  1666. matrix_info->size_info[1] = n;
  1667. matrix_info->size_info[2] = k;
  1668. matrix_info->ld_info[0] = lda;
  1669. matrix_info->ld_info[1] = ldb;
  1670. matrix_info->ld_info[2] = ldc;
  1671. matrix_info->groupsize_info = batch_size;
  1672. sycl::event e = oneapi::mkl::blas::column_major::gemm_batch(
  1673. q, matrix_info->transpose_info, matrix_info->transpose_info + 1,
  1674. matrix_info->size_info, matrix_info->size_info + 1,
  1675. matrix_info->size_info + 2, matrix_info->value_info,
  1676. reinterpret_cast<const Ta **>(a), matrix_info->ld_info,
  1677. reinterpret_cast<const Tb **>(b), matrix_info->ld_info + 1,
  1678. matrix_info->value_info + 1, reinterpret_cast<Tc **>(c),
  1679. matrix_info->ld_info + 2, 1, &(matrix_info->groupsize_info));
  1680. q.submit([&](sycl::handler &cgh)
  1681. {
  1682. cgh.depends_on(e);
  1683. cgh.host_task([=] { std::free(matrix_info); }); });
  1684. }
  1685. template <class Ta, class Tb, class Tc, class Ts>
  1686. inline void
  1687. gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1688. oneapi::mkl::transpose b_trans, int m, int n,
  1689. int k, const void *alpha, const void *a, int lda,
  1690. long long int stride_a, const void *b, int ldb,
  1691. long long int stride_b, const void *beta, void *c,
  1692. int ldc, long long int stride_c, int batch_size)
  1693. {
  1694. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1695. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1696. auto data_a = get_memory<const Ta>(a);
  1697. auto data_b = get_memory<const Tb>(b);
  1698. auto data_c = get_memory<Tc>(c);
  1699. oneapi::mkl::blas::column_major::gemm_batch(
  1700. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1701. stride_a, data_b, ldb, stride_b, beta_value,
  1702. data_c, ldc, stride_c, batch_size);
  1703. }
  1704. } // namespace detail
  1705. template <typename VecT, class BinaryOperation>
  1706. inline unsigned vectorized_binary(unsigned a, unsigned b,
  1707. const BinaryOperation binary_op)
  1708. {
  1709. sycl::vec<unsigned, 1> v0{a}, v1{b};
  1710. auto v2 = v0.as<VecT>();
  1711. auto v3 = v1.as<VecT>();
  1712. auto v4 =
  1713. detail::vectorized_binary<VecT, BinaryOperation>()(v2, v3, binary_op);
  1714. v0 = v4.template as<sycl::vec<unsigned, 1>>();
  1715. return v0;
  1716. }
  1717. static void async_dpct_memcpy(void *to_ptr, const void *from_ptr, size_t size,
  1718. memcpy_direction direction = automatic,
  1719. sycl::queue &q = dpct::get_default_queue())
  1720. {
  1721. detail::dpct_memcpy(q, to_ptr, from_ptr, size, direction);
  1722. }
  1723. static inline unsigned int select_device(unsigned int id)
  1724. {
  1725. dev_mgr::instance().select_device(id);
  1726. return id;
  1727. }
  1728. template <typename T>
  1729. T permute_sub_group_by_xor(sycl::sub_group g, T x, unsigned int mask,
  1730. unsigned int logical_sub_group_size = 32)
  1731. {
  1732. unsigned int id = g.get_local_linear_id();
  1733. unsigned int start_index =
  1734. id / logical_sub_group_size * logical_sub_group_size;
  1735. unsigned int target_offset = (id % logical_sub_group_size) ^ mask;
  1736. return sycl::select_from_group(g, x,
  1737. target_offset < logical_sub_group_size
  1738. ? start_index + target_offset
  1739. : id);
  1740. }
  1741. template <typename T>
  1742. sycl::vec<T, 4> extract_and_sign_or_zero_extend4(T val)
  1743. {
  1744. return sycl::vec<T, 1>(val)
  1745. .template as<sycl::vec<
  1746. std::conditional_t<std::is_signed_v<T>, int8_t, uint8_t>, 4>>()
  1747. .template convert<T>();
  1748. }
  1749. template <typename T1, typename T2>
  1750. using dot_product_acc_t =
  1751. std::conditional_t<std::is_unsigned_v<T1> && std::is_unsigned_v<T2>,
  1752. uint32_t, int32_t>;
  1753. template <typename T1, typename T2, typename T3>
  1754. inline auto dp4a(T1 a, T2 b, T3 c)
  1755. {
  1756. dot_product_acc_t<T1, T2> res = c;
  1757. auto va = extract_and_sign_or_zero_extend4(a);
  1758. auto vb = extract_and_sign_or_zero_extend4(b);
  1759. res += va[0] * vb[0];
  1760. res += va[1] * vb[1];
  1761. res += va[2] * vb[2];
  1762. res += va[3] * vb[3];
  1763. return res;
  1764. }
  1765. struct sub_sat
  1766. {
  1767. template <typename T>
  1768. auto operator()(const T x, const T y) const
  1769. {
  1770. return sycl::sub_sat(x, y);
  1771. }
  1772. };
  1773. template <typename S, typename T>
  1774. inline T vectorized_min(T a, T b)
  1775. {
  1776. sycl::vec<T, 1> v0{a}, v1{b};
  1777. auto v2 = v0.template as<S>();
  1778. auto v3 = v1.template as<S>();
  1779. auto v4 = sycl::min(v2, v3);
  1780. v0 = v4.template as<sycl::vec<T, 1>>();
  1781. return v0;
  1782. }
  1783. inline float pow(const float a, const int b) { return sycl::pown(a, b); }
  1784. inline double pow(const double a, const int b) { return sycl::pown(a, b); }
  1785. inline float pow(const float a, const float b) { return sycl::pow(a, b); }
  1786. inline double pow(const double a, const double b) { return sycl::pow(a, b); }
  1787. template <typename T, typename U>
  1788. inline typename std::enable_if_t<std::is_floating_point_v<T>, T>
  1789. pow(const T a, const U b)
  1790. {
  1791. return sycl::pow(a, static_cast<T>(b));
  1792. }
  1793. template <typename T, typename U>
  1794. inline typename std::enable_if_t<!std::is_floating_point_v<T>, double>
  1795. pow(const T a, const U b)
  1796. {
  1797. return sycl::pow(static_cast<double>(a), static_cast<double>(b));
  1798. }
  1799. inline double min(const double a, const float b)
  1800. {
  1801. return sycl::fmin(a, static_cast<double>(b));
  1802. }
  1803. inline double min(const float a, const double b)
  1804. {
  1805. return sycl::fmin(static_cast<double>(a), b);
  1806. }
  1807. inline float min(const float a, const float b) { return sycl::fmin(a, b); }
  1808. inline double min(const double a, const double b) { return sycl::fmin(a, b); }
  1809. inline std::uint32_t min(const std::uint32_t a, const std::int32_t b)
  1810. {
  1811. return sycl::min(a, static_cast<std::uint32_t>(b));
  1812. }
  1813. inline std::uint32_t min(const std::int32_t a, const std::uint32_t b)
  1814. {
  1815. return sycl::min(static_cast<std::uint32_t>(a), b);
  1816. }
  1817. inline std::int32_t min(const std::int32_t a, const std::int32_t b)
  1818. {
  1819. return sycl::min(a, b);
  1820. }
  1821. inline std::uint32_t min(const std::uint32_t a, const std::uint32_t b)
  1822. {
  1823. return sycl::min(a, b);
  1824. }
  1825. inline std::uint64_t min(const std::uint64_t a, const std::int64_t b)
  1826. {
  1827. return sycl::min(a, static_cast<std::uint64_t>(b));
  1828. }
  1829. inline std::uint64_t min(const std::int64_t a, const std::uint64_t b)
  1830. {
  1831. return sycl::min(static_cast<std::uint64_t>(a), b);
  1832. }
  1833. inline std::int64_t min(const std::int64_t a, const std::int64_t b)
  1834. {
  1835. return sycl::min(a, b);
  1836. }
  1837. inline std::uint64_t min(const std::uint64_t a, const std::uint64_t b)
  1838. {
  1839. return sycl::min(a, b);
  1840. }
  1841. inline std::uint64_t min(const std::uint64_t a, const std::int32_t b)
  1842. {
  1843. return sycl::min(a, static_cast<std::uint64_t>(b));
  1844. }
  1845. inline std::uint64_t min(const std::int32_t a, const std::uint64_t b)
  1846. {
  1847. return sycl::min(static_cast<std::uint64_t>(a), b);
  1848. }
  1849. inline std::uint64_t min(const std::uint64_t a, const std::uint32_t b)
  1850. {
  1851. return sycl::min(a, static_cast<std::uint64_t>(b));
  1852. }
  1853. inline std::uint64_t min(const std::uint32_t a, const std::uint64_t b)
  1854. {
  1855. return sycl::min(static_cast<std::uint64_t>(a), b);
  1856. }
  1857. // max function overloads.
  1858. // For floating-point types, `float` or `double` arguments are acceptable.
  1859. // For integer types, `std::uint32_t`, `std::int32_t`, `std::uint64_t` or
  1860. // `std::int64_t` type arguments are acceptable.
  1861. inline double max(const double a, const float b)
  1862. {
  1863. return sycl::fmax(a, static_cast<double>(b));
  1864. }
  1865. inline double max(const float a, const double b)
  1866. {
  1867. return sycl::fmax(static_cast<double>(a), b);
  1868. }
  1869. inline float max(const float a, const float b) { return sycl::fmax(a, b); }
  1870. inline double max(const double a, const double b) { return sycl::fmax(a, b); }
  1871. inline std::uint32_t max(const std::uint32_t a, const std::int32_t b)
  1872. {
  1873. return sycl::max(a, static_cast<std::uint32_t>(b));
  1874. }
  1875. inline std::uint32_t max(const std::int32_t a, const std::uint32_t b)
  1876. {
  1877. return sycl::max(static_cast<std::uint32_t>(a), b);
  1878. }
  1879. inline std::int32_t max(const std::int32_t a, const std::int32_t b)
  1880. {
  1881. return sycl::max(a, b);
  1882. }
  1883. inline std::uint32_t max(const std::uint32_t a, const std::uint32_t b)
  1884. {
  1885. return sycl::max(a, b);
  1886. }
  1887. inline std::uint64_t max(const std::uint64_t a, const std::int64_t b)
  1888. {
  1889. return sycl::max(a, static_cast<std::uint64_t>(b));
  1890. }
  1891. inline std::uint64_t max(const std::int64_t a, const std::uint64_t b)
  1892. {
  1893. return sycl::max(static_cast<std::uint64_t>(a), b);
  1894. }
  1895. inline std::int64_t max(const std::int64_t a, const std::int64_t b)
  1896. {
  1897. return sycl::max(a, b);
  1898. }
  1899. inline std::uint64_t max(const std::uint64_t a, const std::uint64_t b)
  1900. {
  1901. return sycl::max(a, b);
  1902. }
  1903. inline std::uint64_t max(const std::uint64_t a, const std::int32_t b)
  1904. {
  1905. return sycl::max(a, static_cast<std::uint64_t>(b));
  1906. }
  1907. inline std::uint64_t max(const std::int32_t a, const std::uint64_t b)
  1908. {
  1909. return sycl::max(static_cast<std::uint64_t>(a), b);
  1910. }
  1911. inline std::uint64_t max(const std::uint64_t a, const std::uint32_t b)
  1912. {
  1913. return sycl::max(a, static_cast<std::uint64_t>(b));
  1914. }
  1915. inline std::uint64_t max(const std::uint32_t a, const std::uint64_t b)
  1916. {
  1917. return sycl::max(static_cast<std::uint64_t>(a), b);
  1918. }
  1919. inline void
  1920. has_capability_or_fail(const sycl::device &dev,
  1921. const std::initializer_list<sycl::aspect> &props)
  1922. {
  1923. for (const auto &it : props)
  1924. {
  1925. if (dev.has(it))
  1926. continue;
  1927. switch (it)
  1928. {
  1929. case sycl::aspect::fp64:
  1930. throw std::runtime_error("'double' is not supported in '" +
  1931. dev.get_info<sycl::info::device::name>() +
  1932. "' device");
  1933. break;
  1934. case sycl::aspect::fp16:
  1935. throw std::runtime_error("'half' is not supported in '" +
  1936. dev.get_info<sycl::info::device::name>() +
  1937. "' device");
  1938. break;
  1939. default:
  1940. #define __SYCL_ASPECT(ASPECT, ID) \
  1941. case sycl::aspect::ASPECT: \
  1942. return #ASPECT;
  1943. #define __SYCL_ASPECT_DEPRECATED(ASPECT, ID, MESSAGE) __SYCL_ASPECT(ASPECT, ID)
  1944. #define __SYCL_ASPECT_DEPRECATED_ALIAS(ASPECT, ID, MESSAGE)
  1945. auto getAspectNameStr = [](sycl::aspect AspectNum) -> std::string
  1946. {
  1947. switch (AspectNum)
  1948. {
  1949. #include <sycl/info/aspects.def>
  1950. #include <sycl/info/aspects_deprecated.def>
  1951. default:
  1952. return "unknown aspect";
  1953. }
  1954. };
  1955. #undef __SYCL_ASPECT_DEPRECATED_ALIAS
  1956. #undef __SYCL_ASPECT_DEPRECATED
  1957. #undef __SYCL_ASPECT
  1958. throw std::runtime_error(
  1959. "'" + getAspectNameStr(it) + "' is not supported in '" +
  1960. dev.get_info<sycl::info::device::name>() + "' device");
  1961. }
  1962. break;
  1963. }
  1964. }
  1965. static inline unsigned int get_current_device_id()
  1966. {
  1967. return dev_mgr::instance().current_device_id();
  1968. }
  1969. static inline device_ext &get_current_device()
  1970. {
  1971. return dev_mgr::instance().current_device();
  1972. }
  1973. static inline sycl::queue &get_in_order_queue()
  1974. {
  1975. return dev_mgr::instance().current_device().in_order_queue();
  1976. }
  1977. static sycl::event
  1978. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1979. memcpy_direction direction,
  1980. const std::vector<sycl::event> &dep_events = {})
  1981. {
  1982. if (!size)
  1983. return sycl::event{};
  1984. #ifdef DPCT_USM_LEVEL_NONE
  1985. auto &mm = mem_mgr::instance();
  1986. auto real_direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1987. switch (real_direction)
  1988. {
  1989. case host_to_host:
  1990. return q.submit([&](sycl::handler &cgh)
  1991. {
  1992. cgh.depends_on(dep_events);
  1993. cgh.host_task([=] { std::memcpy(to_ptr, from_ptr, size); }); });
  1994. case host_to_device:
  1995. {
  1996. auto alloc = mm.translate_ptr(to_ptr);
  1997. size_t offset = (byte_t *)to_ptr - alloc.alloc_ptr;
  1998. return q.submit([&](sycl::handler &cgh)
  1999. {
  2000. cgh.depends_on(dep_events);
  2001. auto r = sycl::range<1>(size);
  2002. auto o = sycl::id<1>(offset);
  2003. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  2004. sycl::access::target::device>
  2005. acc(alloc.buffer, cgh, r, o);
  2006. cgh.copy(from_ptr, acc); });
  2007. }
  2008. case device_to_host:
  2009. {
  2010. auto alloc = mm.translate_ptr(from_ptr);
  2011. size_t offset = (byte_t *)from_ptr - alloc.alloc_ptr;
  2012. return q.submit([&](sycl::handler &cgh)
  2013. {
  2014. cgh.depends_on(dep_events);
  2015. auto r = sycl::range<1>(size);
  2016. auto o = sycl::id<1>(offset);
  2017. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  2018. sycl::access::target::device>
  2019. acc(alloc.buffer, cgh, r, o);
  2020. cgh.copy(acc, to_ptr); });
  2021. }
  2022. case device_to_device:
  2023. {
  2024. auto to_alloc = mm.translate_ptr(to_ptr);
  2025. auto from_alloc = mm.translate_ptr(from_ptr);
  2026. size_t to_offset = (byte_t *)to_ptr - to_alloc.alloc_ptr;
  2027. size_t from_offset = (byte_t *)from_ptr - from_alloc.alloc_ptr;
  2028. return q.submit([&](sycl::handler &cgh)
  2029. {
  2030. cgh.depends_on(dep_events);
  2031. auto r = sycl::range<1>(size);
  2032. auto to_o = sycl::id<1>(to_offset);
  2033. auto from_o = sycl::id<1>(from_offset);
  2034. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  2035. sycl::access::target::device>
  2036. to_acc(to_alloc.buffer, cgh, r, to_o);
  2037. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  2038. sycl::access::target::device>
  2039. from_acc(from_alloc.buffer, cgh, r, from_o);
  2040. cgh.copy(from_acc, to_acc); });
  2041. }
  2042. default:
  2043. throw std::runtime_error("dpct_memcpy: invalid direction value");
  2044. }
  2045. #else
  2046. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  2047. GGML_UNUSED(direction);
  2048. #endif // DPCT_USM_LEVEL_NONE
  2049. }
  2050. // Get actual copy range and make sure it will not exceed range.
  2051. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  2052. size_t pitch)
  2053. {
  2054. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  2055. }
  2056. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  2057. size_t pitch)
  2058. {
  2059. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  2060. }
  2061. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  2062. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  2063. static inline std::vector<sycl::event>
  2064. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  2065. sycl::range<3> to_range, sycl::range<3> from_range,
  2066. sycl::id<3> to_id, sycl::id<3> from_id,
  2067. sycl::range<3> size, memcpy_direction direction,
  2068. const std::vector<sycl::event> &dep_events = {})
  2069. {
  2070. // RAII for host pointer
  2071. class host_buffer
  2072. {
  2073. void *_buf;
  2074. size_t _size;
  2075. sycl::queue &_q;
  2076. const std::vector<sycl::event> &_deps; // free operation depends
  2077. public:
  2078. host_buffer(size_t size, sycl::queue &q,
  2079. const std::vector<sycl::event> &deps)
  2080. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  2081. void *get_ptr() const { return _buf; }
  2082. size_t get_size() const { return _size; }
  2083. ~host_buffer()
  2084. {
  2085. if (_buf)
  2086. {
  2087. _q.submit([&](sycl::handler &cgh)
  2088. {
  2089. cgh.depends_on(_deps);
  2090. cgh.host_task([buf = _buf] { std::free(buf); }); });
  2091. }
  2092. }
  2093. };
  2094. std::vector<sycl::event> event_list;
  2095. size_t to_slice = to_range.get(1) * to_range.get(0),
  2096. from_slice = from_range.get(1) * from_range.get(0);
  2097. unsigned char *to_surface =
  2098. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  2099. const unsigned char *from_surface =
  2100. (const unsigned char *)from_ptr +
  2101. get_offset(from_id, from_slice, from_range.get(0));
  2102. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  2103. {
  2104. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  2105. direction, dep_events)};
  2106. }
  2107. direction = detail::deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  2108. size_t size_slice = size.get(1) * size.get(0);
  2109. switch (direction)
  2110. {
  2111. case host_to_host:
  2112. for (size_t z = 0; z < size.get(2); ++z)
  2113. {
  2114. unsigned char *to_ptr = to_surface;
  2115. const unsigned char *from_ptr = from_surface;
  2116. if (to_range.get(0) == from_range.get(0) &&
  2117. to_range.get(0) == size.get(0))
  2118. {
  2119. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  2120. direction, dep_events));
  2121. }
  2122. else
  2123. {
  2124. for (size_t y = 0; y < size.get(1); ++y)
  2125. {
  2126. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  2127. direction, dep_events));
  2128. to_ptr += to_range.get(0);
  2129. from_ptr += from_range.get(0);
  2130. }
  2131. }
  2132. to_surface += to_slice;
  2133. from_surface += from_slice;
  2134. }
  2135. break;
  2136. case host_to_device:
  2137. {
  2138. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  2139. event_list);
  2140. std::vector<sycl::event> host_events;
  2141. if (to_slice == size_slice)
  2142. {
  2143. // Copy host data to a temp host buffer with the shape of target.
  2144. host_events =
  2145. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  2146. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  2147. host_to_host, dep_events);
  2148. }
  2149. else
  2150. {
  2151. // Copy host data to a temp host buffer with the shape of target.
  2152. host_events = dpct_memcpy(
  2153. q, buf.get_ptr(), from_surface, to_range, from_range,
  2154. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  2155. // If has padding data, not sure whether it is useless. So fill temp
  2156. // buffer with it.
  2157. std::vector<sycl::event>{
  2158. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  2159. device_to_host, dep_events)});
  2160. }
  2161. // Copy from temp host buffer to device with only one submit.
  2162. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  2163. buf.get_size(), host_to_device,
  2164. host_events));
  2165. break;
  2166. }
  2167. case device_to_host:
  2168. {
  2169. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  2170. event_list);
  2171. // Copy from host temp buffer to host target with reshaping.
  2172. event_list = dpct_memcpy(
  2173. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  2174. sycl::id<3>(0, 0, 0), size, host_to_host,
  2175. // Copy from device to temp host buffer with only one submit.
  2176. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  2177. buf.get_size(),
  2178. device_to_host, dep_events)});
  2179. break;
  2180. }
  2181. case device_to_device:
  2182. #ifdef DPCT_USM_LEVEL_NONE
  2183. {
  2184. auto &mm = mem_mgr::instance();
  2185. auto to_alloc = mm.translate_ptr(to_surface);
  2186. auto from_alloc = mm.translate_ptr(from_surface);
  2187. size_t to_offset = (byte_t *)to_surface - to_alloc.alloc_ptr;
  2188. size_t from_offset = (byte_t *)from_surface - from_alloc.alloc_ptr;
  2189. event_list.push_back(q.submit([&](sycl::handler &cgh)
  2190. {
  2191. cgh.depends_on(dep_events);
  2192. auto to_o = sycl::id<1>(to_offset);
  2193. auto from_o = sycl::id<1>(from_offset);
  2194. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  2195. sycl::access::target::device>
  2196. to_acc(to_alloc.buffer, cgh,
  2197. get_copy_range(size, to_slice, to_range.get(0)), to_o);
  2198. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  2199. sycl::access::target::device>
  2200. from_acc(from_alloc.buffer, cgh,
  2201. get_copy_range(size, from_slice, from_range.get(0)), from_o);
  2202. cgh.parallel_for<class dpct_memcpy_3d_detail_usmnone>(
  2203. size,
  2204. [=](sycl::id<3> id) {
  2205. to_acc[get_offset(id, to_slice, to_range.get(0))] =
  2206. from_acc[get_offset(id, from_slice, from_range.get(0))];
  2207. }); }));
  2208. }
  2209. #else
  2210. event_list.push_back(q.submit([&](sycl::handler &cgh)
  2211. {
  2212. cgh.depends_on(dep_events);
  2213. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  2214. size,
  2215. [=](sycl::id<3> id) {
  2216. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  2217. from_surface[get_offset(id, from_slice, from_range.get(0))];
  2218. }); }));
  2219. #endif
  2220. break;
  2221. default:
  2222. throw std::runtime_error("dpct_memcpy: invalid direction value");
  2223. }
  2224. return event_list;
  2225. }
  2226. /// memcpy 2D/3D matrix specified by pitched_data.
  2227. static inline std::vector<sycl::event>
  2228. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  2229. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  2230. memcpy_direction direction = automatic)
  2231. {
  2232. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  2233. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  2234. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  2235. size, direction);
  2236. }
  2237. /// memcpy 2D matrix with pitch.
  2238. static inline std::vector<sycl::event>
  2239. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  2240. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  2241. memcpy_direction direction = automatic)
  2242. {
  2243. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  2244. sycl::range<3>(from_pitch, y, 1),
  2245. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  2246. sycl::range<3>(x, y, 1), direction);
  2247. }
  2248. inline void gemm(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2249. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2250. const void *alpha, const void *a, library_data_t a_type,
  2251. int lda, const void *b, library_data_t b_type, int ldb,
  2252. const void *beta, void *c, library_data_t c_type, int ldc,
  2253. library_data_t scaling_type)
  2254. {
  2255. if (scaling_type == library_data_t::real_float &&
  2256. c_type == library_data_t::complex_float)
  2257. {
  2258. scaling_type = library_data_t::complex_float;
  2259. }
  2260. else if (scaling_type == library_data_t::real_double &&
  2261. c_type == library_data_t::complex_double)
  2262. {
  2263. scaling_type = library_data_t::complex_double;
  2264. }
  2265. std::uint64_t key =
  2266. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2267. switch (key)
  2268. {
  2269. case detail::get_type_combination_id(
  2270. library_data_t::real_float, library_data_t::real_float,
  2271. library_data_t::real_float, library_data_t::real_float):
  2272. {
  2273. detail::gemm_impl<float, float, float, float>(
  2274. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2275. break;
  2276. }
  2277. case detail::get_type_combination_id(
  2278. library_data_t::real_double, library_data_t::real_double,
  2279. library_data_t::real_double, library_data_t::real_double):
  2280. {
  2281. detail::gemm_impl<double, double, double, double>(
  2282. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2283. break;
  2284. }
  2285. case detail::get_type_combination_id(
  2286. library_data_t::complex_float, library_data_t::complex_float,
  2287. library_data_t::complex_float, library_data_t::complex_float):
  2288. {
  2289. detail::gemm_impl<std::complex<float>, std::complex<float>,
  2290. std::complex<float>, std::complex<float>>(
  2291. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2292. break;
  2293. }
  2294. case detail::get_type_combination_id(
  2295. library_data_t::complex_double, library_data_t::complex_double,
  2296. library_data_t::complex_double, library_data_t::complex_double):
  2297. {
  2298. detail::gemm_impl<std::complex<double>, std::complex<double>,
  2299. std::complex<double>, std::complex<double>>(
  2300. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2301. break;
  2302. }
  2303. case detail::get_type_combination_id(
  2304. library_data_t::real_half, library_data_t::real_half,
  2305. library_data_t::real_half, library_data_t::real_half):
  2306. {
  2307. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2308. sycl::half>(q, a_trans, b_trans, m, n, k, alpha, a,
  2309. lda, b, ldb, beta, c, ldc);
  2310. break;
  2311. }
  2312. case detail::get_type_combination_id(
  2313. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2314. library_data_t::real_float, library_data_t::real_float):
  2315. {
  2316. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2317. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda, b,
  2318. ldb, beta, c, ldc);
  2319. break;
  2320. }
  2321. case detail::get_type_combination_id(
  2322. library_data_t::real_half, library_data_t::real_half,
  2323. library_data_t::real_float, library_data_t::real_float):
  2324. {
  2325. detail::gemm_impl<sycl::half, sycl::half, float, float>(
  2326. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2327. break;
  2328. }
  2329. case detail::get_type_combination_id(
  2330. library_data_t::real_half, library_data_t::real_half,
  2331. library_data_t::real_half, library_data_t::real_float):
  2332. {
  2333. float alpha_value =
  2334. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2335. float beta_value =
  2336. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2337. sycl::half alpha_half(alpha_value);
  2338. sycl::half beta_half(beta_value);
  2339. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2340. sycl::half>(q, a_trans, b_trans, m, n, k, &alpha_half,
  2341. a, lda, b, ldb, &beta_half, c, ldc);
  2342. break;
  2343. }
  2344. case detail::get_type_combination_id(
  2345. library_data_t::real_int8, library_data_t::real_int8,
  2346. library_data_t::real_float, library_data_t::real_float):
  2347. {
  2348. detail::gemm_impl<std::int8_t, std::int8_t, float, float>(
  2349. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2350. break;
  2351. }
  2352. case detail::get_type_combination_id(
  2353. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2354. library_data_t::real_bfloat16, library_data_t::real_float):
  2355. {
  2356. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2357. oneapi::mkl::bfloat16, float>(
  2358. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2359. break;
  2360. }
  2361. case detail::get_type_combination_id(
  2362. library_data_t::real_int8, library_data_t::real_int8,
  2363. library_data_t::real_int32, library_data_t::real_int32):
  2364. {
  2365. float alpha_float =
  2366. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2367. float beta_float =
  2368. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2369. detail::gemm_impl<std::int8_t, std::int8_t, std::int32_t, float>(
  2370. q, a_trans, b_trans, m, n, k, &alpha_float, a, lda, b, ldb, &beta_float, c, ldc);
  2371. break;
  2372. }
  2373. default:
  2374. throw std::runtime_error("the combination of data type is unsupported");
  2375. }
  2376. } // gemm()
  2377. /// Computes a batch of matrix-matrix product with general matrices.
  2378. /// \param [in] q The queue where the routine should be executed.
  2379. /// \param [in] a_trans Specifies the operation applied to A.
  2380. /// \param [in] b_trans Specifies the operation applied to B.
  2381. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2382. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2383. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2384. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2385. /// \param [in] a Input matrix A.
  2386. /// \param [in] a_type Data type of the matrix A.
  2387. /// \param [in] lda Leading dimension of A.
  2388. /// \param [in] b Input matrix B.
  2389. /// \param [in] b_type Data type of the matrix B.
  2390. /// \param [in] ldb Leading dimension of B.
  2391. /// \param [in] beta Scaling factor for matrix C.
  2392. /// \param [in, out] c Input/Output matrix C.
  2393. /// \param [in] c_type Data type of the matrix C.
  2394. /// \param [in] ldc Leading dimension of C.
  2395. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2396. /// \param [in] scaling_type Data type of the scaling factors.
  2397. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2398. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2399. const void *alpha, const void *a[],
  2400. library_data_t a_type, int lda, const void *b[],
  2401. library_data_t b_type, int ldb, const void *beta,
  2402. void *c[], library_data_t c_type, int ldc,
  2403. int batch_size, library_data_t scaling_type)
  2404. {
  2405. #ifdef DPCT_USM_LEVEL_NONE
  2406. throw std::runtime_error("this API is unsupported when USM level is none");
  2407. #else
  2408. if (scaling_type == library_data_t::real_float &&
  2409. c_type == library_data_t::complex_float)
  2410. {
  2411. scaling_type = library_data_t::complex_float;
  2412. }
  2413. else if (scaling_type == library_data_t::real_double &&
  2414. c_type == library_data_t::complex_double)
  2415. {
  2416. scaling_type = library_data_t::complex_double;
  2417. }
  2418. std::uint64_t key =
  2419. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2420. switch (key)
  2421. {
  2422. case detail::get_type_combination_id(
  2423. library_data_t::real_float, library_data_t::real_float,
  2424. library_data_t::real_float, library_data_t::real_float):
  2425. {
  2426. detail::gemm_batch_impl<float, float, float, float>(
  2427. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2428. batch_size);
  2429. break;
  2430. }
  2431. case detail::get_type_combination_id(
  2432. library_data_t::real_double, library_data_t::real_double,
  2433. library_data_t::real_double, library_data_t::real_double):
  2434. {
  2435. detail::gemm_batch_impl<double, double, double, double>(
  2436. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2437. batch_size);
  2438. break;
  2439. }
  2440. case detail::get_type_combination_id(
  2441. library_data_t::complex_float, library_data_t::complex_float,
  2442. library_data_t::complex_float, library_data_t::complex_float):
  2443. {
  2444. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2445. std::complex<float>, std::complex<float>>(
  2446. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2447. batch_size);
  2448. break;
  2449. }
  2450. case detail::get_type_combination_id(
  2451. library_data_t::complex_double, library_data_t::complex_double,
  2452. library_data_t::complex_double, library_data_t::complex_double):
  2453. {
  2454. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2455. std::complex<double>, std::complex<double>>(
  2456. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2457. batch_size);
  2458. break;
  2459. }
  2460. case detail::get_type_combination_id(
  2461. library_data_t::real_half, library_data_t::real_half,
  2462. library_data_t::real_half, library_data_t::real_half):
  2463. {
  2464. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2465. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2466. a, lda, b, ldb, beta, c, ldc,
  2467. batch_size);
  2468. break;
  2469. }
  2470. #ifdef __INTEL_MKL__
  2471. case detail::get_type_combination_id(
  2472. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2473. library_data_t::real_bfloat16, library_data_t::real_float):
  2474. {
  2475. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2476. oneapi::mkl::bfloat16, float>(
  2477. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2478. batch_size);
  2479. break;
  2480. }
  2481. case detail::get_type_combination_id(
  2482. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2483. library_data_t::real_float, library_data_t::real_float):
  2484. {
  2485. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2486. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2487. b, ldb, beta, c, ldc, batch_size);
  2488. break;
  2489. }
  2490. case detail::get_type_combination_id(
  2491. library_data_t::real_int8, library_data_t::real_int8,
  2492. library_data_t::real_int32, library_data_t::real_int32):
  2493. {
  2494. float alpha_float =
  2495. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2496. float beta_float =
  2497. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2498. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2499. float>(q, a_trans, b_trans, m, n, k, &alpha_float,
  2500. a, lda, b, ldb, &beta_float, c, ldc,
  2501. batch_size);
  2502. break;
  2503. }
  2504. case detail::get_type_combination_id(
  2505. library_data_t::real_int8, library_data_t::real_int8,
  2506. library_data_t::real_float, library_data_t::real_float):
  2507. {
  2508. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2509. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2510. batch_size);
  2511. break;
  2512. }
  2513. case detail::get_type_combination_id(
  2514. library_data_t::real_half, library_data_t::real_half,
  2515. library_data_t::real_float, library_data_t::real_float):
  2516. {
  2517. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2518. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2519. batch_size);
  2520. break;
  2521. }
  2522. #endif
  2523. case detail::get_type_combination_id(
  2524. library_data_t::real_half, library_data_t::real_half,
  2525. library_data_t::real_half, library_data_t::real_float):
  2526. {
  2527. float alpha_value =
  2528. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2529. float beta_value =
  2530. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2531. sycl::half alpha_half(alpha_value);
  2532. sycl::half beta_half(beta_value);
  2533. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2534. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, b, ldb, &beta_half, c, ldc,
  2535. batch_size);
  2536. break;
  2537. }
  2538. default:
  2539. throw std::runtime_error("the combination of data type is unsupported");
  2540. }
  2541. #endif
  2542. }
  2543. /// Computes a batch of matrix-matrix product with general matrices.
  2544. /// \param [in] q The queue where the routine should be executed.
  2545. /// \param [in] a_trans Specifies the operation applied to A.
  2546. /// \param [in] b_trans Specifies the operation applied to B.
  2547. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2548. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2549. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2550. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2551. /// \param [in] a Input matrix A.
  2552. /// \param [in] a_type Data type of the matrix A.
  2553. /// \param [in] lda Leading dimension of A.
  2554. /// \param [in] stride_a Stride between the different A matrices.
  2555. /// \param [in] b Input matrix B.
  2556. /// \param [in] b_type Data type of the matrix B.
  2557. /// \param [in] ldb Leading dimension of B.
  2558. /// \param [in] stride_b Stride between the different B matrices.
  2559. /// \param [in] beta Scaling factor for matrix C.
  2560. /// \param [in, out] c Input/Output matrix C.
  2561. /// \param [in] c_type Data type of the matrix C.
  2562. /// \param [in] ldc Leading dimension of C.
  2563. /// \param [in] stride_c Stride between the different C matrices.
  2564. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2565. /// \param [in] scaling_type Data type of the scaling factors.
  2566. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2567. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2568. const void *alpha, const void *a, library_data_t a_type,
  2569. int lda, long long int stride_a, const void *b,
  2570. library_data_t b_type, int ldb, long long int stride_b,
  2571. const void *beta, void *c, library_data_t c_type,
  2572. int ldc, long long int stride_c, int batch_size,
  2573. library_data_t scaling_type)
  2574. {
  2575. if (scaling_type == library_data_t::real_float &&
  2576. c_type == library_data_t::complex_float)
  2577. {
  2578. scaling_type = library_data_t::complex_float;
  2579. }
  2580. else if (scaling_type == library_data_t::real_double &&
  2581. c_type == library_data_t::complex_double)
  2582. {
  2583. scaling_type = library_data_t::complex_double;
  2584. }
  2585. std::uint64_t key =
  2586. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2587. switch (key)
  2588. {
  2589. case detail::get_type_combination_id(
  2590. library_data_t::real_float, library_data_t::real_float,
  2591. library_data_t::real_float, library_data_t::real_float):
  2592. {
  2593. detail::gemm_batch_impl<float, float, float, float>(
  2594. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2595. beta, c, ldc, stride_c, batch_size);
  2596. break;
  2597. }
  2598. case detail::get_type_combination_id(
  2599. library_data_t::real_double, library_data_t::real_double,
  2600. library_data_t::real_double, library_data_t::real_double):
  2601. {
  2602. detail::gemm_batch_impl<double, double, double, double>(
  2603. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2604. beta, c, ldc, stride_c, batch_size);
  2605. break;
  2606. }
  2607. case detail::get_type_combination_id(
  2608. library_data_t::complex_float, library_data_t::complex_float,
  2609. library_data_t::complex_float, library_data_t::complex_float):
  2610. {
  2611. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2612. std::complex<float>, std::complex<float>>(
  2613. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2614. beta, c, ldc, stride_c, batch_size);
  2615. break;
  2616. }
  2617. case detail::get_type_combination_id(
  2618. library_data_t::complex_double, library_data_t::complex_double,
  2619. library_data_t::complex_double, library_data_t::complex_double):
  2620. {
  2621. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2622. std::complex<double>, std::complex<double>>(
  2623. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2624. beta, c, ldc, stride_c, batch_size);
  2625. break;
  2626. }
  2627. case detail::get_type_combination_id(
  2628. library_data_t::real_half, library_data_t::real_half,
  2629. library_data_t::real_half, library_data_t::real_half):
  2630. {
  2631. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2632. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2633. a, lda, stride_a, b, ldb, stride_b,
  2634. beta, c, ldc, stride_c, batch_size);
  2635. break;
  2636. }
  2637. #ifdef __INTEL_MKL__
  2638. case detail::get_type_combination_id(
  2639. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2640. library_data_t::real_bfloat16, library_data_t::real_float):
  2641. {
  2642. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2643. oneapi::mkl::bfloat16, float>(
  2644. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2645. beta, c, ldc, stride_c, batch_size);
  2646. break;
  2647. }
  2648. case detail::get_type_combination_id(
  2649. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2650. library_data_t::real_float, library_data_t::real_float):
  2651. {
  2652. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2653. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2654. stride_a, b, ldb, stride_b, beta, c, ldc,
  2655. stride_c, batch_size);
  2656. break;
  2657. }
  2658. case detail::get_type_combination_id(
  2659. library_data_t::real_int8, library_data_t::real_int8,
  2660. library_data_t::real_int32, library_data_t::real_int32):
  2661. {
  2662. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2663. std::int32_t>(q, a_trans, b_trans, m, n, k, alpha,
  2664. a, lda, stride_a, b, ldb, stride_b,
  2665. beta, c, ldc, stride_c, batch_size);
  2666. break;
  2667. }
  2668. case detail::get_type_combination_id(
  2669. library_data_t::real_int8, library_data_t::real_int8,
  2670. library_data_t::real_float, library_data_t::real_float):
  2671. {
  2672. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2673. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2674. beta, c, ldc, stride_c, batch_size);
  2675. break;
  2676. }
  2677. case detail::get_type_combination_id(
  2678. library_data_t::real_half, library_data_t::real_half,
  2679. library_data_t::real_float, library_data_t::real_float):
  2680. {
  2681. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2682. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2683. beta, c, ldc, stride_c, batch_size);
  2684. break;
  2685. }
  2686. #endif
  2687. case detail::get_type_combination_id(
  2688. library_data_t::real_half, library_data_t::real_half,
  2689. library_data_t::real_half, library_data_t::real_float):
  2690. {
  2691. float alpha_value =
  2692. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2693. float beta_value =
  2694. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2695. sycl::half alpha_half(alpha_value);
  2696. sycl::half beta_half(beta_value);
  2697. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2698. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, stride_a, b, ldb, stride_b,
  2699. &beta_half, c, ldc, stride_c, batch_size);
  2700. break;
  2701. }
  2702. default:
  2703. throw std::runtime_error("the combination of data type is unsupported");
  2704. }
  2705. }
  2706. static inline void
  2707. async_dpct_memcpy(void *to_ptr, size_t to_pitch, const void *from_ptr,
  2708. size_t from_pitch, size_t x, size_t y,
  2709. memcpy_direction direction = automatic,
  2710. sycl::queue &q = get_default_queue())
  2711. {
  2712. detail::dpct_memcpy(q, to_ptr, from_ptr, to_pitch, from_pitch, x, y,
  2713. direction);
  2714. }
  2715. using err0 = detail::generic_error_type<struct err0_tag, int>;
  2716. using err1 = detail::generic_error_type<struct err1_tag, int>;
  2717. static inline void dpct_free(void *ptr, sycl::queue &q = get_default_queue()) {
  2718. detail::dpct_free(ptr, q);
  2719. }
  2720. /// dpct accessor used as device function parameter.
  2721. template <class T, memory_region Memory, size_t Dimension> class accessor;
  2722. template <class T, memory_region Memory> class accessor<T, Memory, 3> {
  2723. public:
  2724. using memory_t = detail::memory_traits<Memory, T>;
  2725. using element_t = typename memory_t::element_t;
  2726. using pointer_t = typename memory_t::pointer_t;
  2727. using accessor_t = typename memory_t::template accessor_t<3>;
  2728. accessor(pointer_t data, const sycl::range<3> &in_range)
  2729. : _data(data), _range(in_range) {}
  2730. template <memory_region M = Memory>
  2731. accessor(typename std::enable_if<M != local, const accessor_t>::type &acc)
  2732. : accessor(acc, acc.get_range()) {}
  2733. accessor(const accessor_t &acc, const sycl::range<3> &in_range)
  2734. : accessor(acc.get_pointer(), in_range) {}
  2735. accessor<T, Memory, 2> operator[](size_t index) const {
  2736. sycl::range<2> sub(_range.get(1), _range.get(2));
  2737. return accessor<T, Memory, 2>(_data + index * sub.size(), sub);
  2738. }
  2739. pointer_t get_ptr() const { return _data; }
  2740. private:
  2741. pointer_t _data;
  2742. sycl::range<3> _range;
  2743. };
  2744. template <class T, memory_region Memory> class accessor<T, Memory, 2> {
  2745. public:
  2746. using memory_t = detail::memory_traits<Memory, T>;
  2747. using element_t = typename memory_t::element_t;
  2748. using pointer_t = typename memory_t::pointer_t;
  2749. using accessor_t = typename memory_t::template accessor_t<2>;
  2750. accessor(pointer_t data, const sycl::range<2> &in_range)
  2751. : _data(data), _range(in_range) {}
  2752. template <memory_region M = Memory>
  2753. accessor(typename std::enable_if<M != local, const accessor_t>::type &acc)
  2754. : accessor(acc, acc.get_range()) {}
  2755. accessor(const accessor_t &acc, const sycl::range<2> &in_range)
  2756. : accessor(acc.get_pointer(), in_range) {}
  2757. pointer_t operator[](size_t index) const {
  2758. return _data + _range.get(1) * index;
  2759. }
  2760. pointer_t get_ptr() const { return _data; }
  2761. private:
  2762. pointer_t _data;
  2763. sycl::range<2> _range;
  2764. };
  2765. namespace detail {
  2766. /// Device variable with address space of shared, global or constant.
  2767. template <class T, memory_region Memory, size_t Dimension> class device_memory {
  2768. public:
  2769. using accessor_t =
  2770. typename detail::memory_traits<Memory,
  2771. T>::template accessor_t<Dimension>;
  2772. using value_t = typename detail::memory_traits<Memory, T>::value_t;
  2773. using dpct_accessor_t = dpct::accessor<T, Memory, Dimension>;
  2774. device_memory() : device_memory(sycl::range<Dimension>(1)) {}
  2775. /// Constructor of 1-D array with initializer list
  2776. device_memory(const sycl::range<Dimension> &in_range,
  2777. std::initializer_list<value_t> &&init_list)
  2778. : device_memory(in_range) {
  2779. assert(init_list.size() <= in_range.size());
  2780. _host_ptr = (value_t *)std::malloc(_size);
  2781. std::memset(_host_ptr, 0, _size);
  2782. std::memcpy(_host_ptr, init_list.begin(), init_list.size() * sizeof(T));
  2783. }
  2784. /// Constructor of 2-D array with initializer list
  2785. template <size_t D = Dimension>
  2786. device_memory(
  2787. const typename std::enable_if<D == 2, sycl::range<2>>::type &in_range,
  2788. std::initializer_list<std::initializer_list<value_t>> &&init_list)
  2789. : device_memory(in_range) {
  2790. assert(init_list.size() <= in_range[0]);
  2791. _host_ptr = (value_t *)std::malloc(_size);
  2792. std::memset(_host_ptr, 0, _size);
  2793. auto tmp_data = _host_ptr;
  2794. for (auto sub_list : init_list) {
  2795. assert(sub_list.size() <= in_range[1]);
  2796. std::memcpy(tmp_data, sub_list.begin(),
  2797. sub_list.size() * sizeof(T));
  2798. tmp_data += in_range[1];
  2799. }
  2800. }
  2801. /// Constructor with range
  2802. device_memory(const sycl::range<Dimension> &range_in)
  2803. : _size(range_in.size() * sizeof(T)), _range(range_in),
  2804. _reference(false), _host_ptr(nullptr), _device_ptr(nullptr) {
  2805. static_assert(
  2806. (Memory == global) || (Memory == constant) || (Memory == shared),
  2807. "device memory region should be global, constant or shared");
  2808. // Make sure that singleton class mem_mgr and dev_mgr will destruct
  2809. // later than this.
  2810. detail::mem_mgr::instance();
  2811. dev_mgr::instance();
  2812. }
  2813. /// Constructor with range
  2814. template <class... Args>
  2815. device_memory(Args... Arguments)
  2816. : device_memory(sycl::range<Dimension>(Arguments...)) {}
  2817. ~device_memory() {
  2818. if (_device_ptr && !_reference)
  2819. dpct::dpct_free(_device_ptr);
  2820. if (_host_ptr)
  2821. std::free(_host_ptr);
  2822. }
  2823. /// Allocate memory with default queue, and init memory if has initial
  2824. /// value.
  2825. void init() { init(dpct::get_default_queue()); }
  2826. /// Allocate memory with specified queue, and init memory if has initial
  2827. /// value.
  2828. void init(sycl::queue &q) {
  2829. if (_device_ptr)
  2830. return;
  2831. if (!_size)
  2832. return;
  2833. allocate_device(q);
  2834. if (_host_ptr)
  2835. detail::dpct_memcpy(q, _device_ptr, _host_ptr, _size,
  2836. host_to_device);
  2837. }
  2838. /// The variable is assigned to a device pointer.
  2839. void assign(value_t *src, size_t size) {
  2840. this->~device_memory();
  2841. new (this) device_memory(src, size);
  2842. }
  2843. /// Get memory pointer of the memory object, which is virtual pointer when
  2844. /// usm is not used, and device pointer when usm is used.
  2845. value_t *get_ptr() { return get_ptr(get_default_queue()); }
  2846. /// Get memory pointer of the memory object, which is virtual pointer when
  2847. /// usm is not used, and device pointer when usm is used.
  2848. value_t *get_ptr(sycl::queue &q) {
  2849. init(q);
  2850. return _device_ptr;
  2851. }
  2852. /// Get the device memory object size in bytes.
  2853. size_t get_size() { return _size; }
  2854. template <size_t D = Dimension>
  2855. typename std::enable_if<D == 1, T>::type &operator[](size_t index) {
  2856. init();
  2857. #ifdef DPCT_USM_LEVEL_NONE
  2858. return dpct::get_buffer<typename std::enable_if<D == 1, T>::type>(
  2859. _device_ptr)
  2860. .template get_access<sycl::access_mode::read_write>()[index];
  2861. #else
  2862. return _device_ptr[index];
  2863. #endif // DPCT_USM_LEVEL_NONE
  2864. }
  2865. #ifdef DPCT_USM_LEVEL_NONE
  2866. /// Get sycl::accessor for the device memory object when usm is not used.
  2867. accessor_t get_access(sycl::handler &cgh) {
  2868. return get_buffer(_device_ptr)
  2869. .template reinterpret<T, Dimension>(_range)
  2870. .template get_access<detail::memory_traits<Memory, T>::mode,
  2871. detail::memory_traits<Memory, T>::target>(cgh);
  2872. }
  2873. #else
  2874. /// Get dpct::accessor with dimension info for the device memory object
  2875. /// when usm is used and dimension is greater than 1.
  2876. template <size_t D = Dimension>
  2877. typename std::enable_if<D != 1, dpct_accessor_t>::type
  2878. get_access(sycl::handler &cgh) {
  2879. return dpct_accessor_t((T *)_device_ptr, _range);
  2880. }
  2881. #endif // DPCT_USM_LEVEL_NONE
  2882. private:
  2883. device_memory(value_t *memory_ptr, size_t size)
  2884. : _size(size), _range(size / sizeof(T)), _reference(true),
  2885. _device_ptr(memory_ptr) {}
  2886. void allocate_device(sycl::queue &q) {
  2887. #ifndef DPCT_USM_LEVEL_NONE
  2888. if (Memory == shared) {
  2889. _device_ptr = (value_t *)sycl::malloc_shared(_size, q.get_device(),
  2890. q.get_context());
  2891. return;
  2892. }
  2893. #ifdef SYCL_EXT_ONEAPI_USM_DEVICE_READ_ONLY
  2894. if (Memory == constant) {
  2895. _device_ptr = (value_t *)sycl::malloc_device(
  2896. _size, q.get_device(), q.get_context(),
  2897. sycl::ext::oneapi::property::usm::device_read_only());
  2898. return;
  2899. }
  2900. #endif
  2901. #endif
  2902. _device_ptr = (value_t *)detail::dpct_malloc(_size, q);
  2903. }
  2904. size_t _size;
  2905. sycl::range<Dimension> _range;
  2906. bool _reference;
  2907. value_t *_host_ptr;
  2908. value_t *_device_ptr;
  2909. };
  2910. template <class T, memory_region Memory>
  2911. class device_memory<T, Memory, 0> : public device_memory<T, Memory, 1> {
  2912. public:
  2913. using base = device_memory<T, Memory, 1>;
  2914. using value_t = typename base::value_t;
  2915. using accessor_t =
  2916. typename detail::memory_traits<Memory, T>::template accessor_t<0>;
  2917. /// Constructor with initial value.
  2918. device_memory(const value_t &val) : base(sycl::range<1>(1), {val}) {}
  2919. /// Default constructor
  2920. device_memory() : base(1) {}
  2921. #ifdef DPCT_USM_LEVEL_NONE
  2922. /// Get sycl::accessor for the device memory object when usm is not used.
  2923. accessor_t get_access(sycl::handler &cgh) {
  2924. auto buf = get_buffer(base::get_ptr())
  2925. .template reinterpret<T, 1>(sycl::range<1>(1));
  2926. return accessor_t(buf, cgh);
  2927. }
  2928. #endif // DPCT_USM_LEVEL_NONE
  2929. };
  2930. } // namespace detail
  2931. template <class T, size_t Dimension>
  2932. using global_memory = detail::device_memory<T, global, Dimension>;
  2933. template <class T, size_t Dimension>
  2934. using constant_memory = detail::device_memory<T, constant, Dimension>;
  2935. template <class T, size_t Dimension>
  2936. using shared_memory = detail::device_memory<T, shared, Dimension>;
  2937. } // COPY from DPCT head files
  2938. static int g_ggml_sycl_debug=0;
  2939. #define GGML_SYCL_DEBUG(...) do{if(g_ggml_sycl_debug) printf(__VA_ARGS__);}while(0)
  2940. #define CHECK_TRY_ERROR(expr) \
  2941. [&]() { \
  2942. try { \
  2943. expr; \
  2944. return dpct::success; \
  2945. } catch (std::exception const &e) { \
  2946. std::cerr << e.what()<< "\nException caught at file:" << __FILE__ \
  2947. << ", line:" << __LINE__ <<", func:"<<__func__<< std::endl; \
  2948. return dpct::default_error; \
  2949. } \
  2950. }()
  2951. // #define DEBUG_SYCL_MALLOC
  2952. static int g_work_group_size = 0;
  2953. // typedef sycl::half ggml_fp16_t;
  2954. #define __SYCL_ARCH__ DPCT_COMPATIBILITY_TEMP
  2955. #define VER_4VEC 610 //todo for hardward optimize.
  2956. #define VER_GEN9 700 //todo for hardward optimize.
  2957. #define VER_GEN12 1000000 //todo for hardward optimize.
  2958. #define VER_GEN13 (VER_GEN12 + 1030) //todo for hardward optimize.
  2959. #define GGML_SYCL_MAX_NODES 8192 //TODO: adapt to hardwares
  2960. //define for XMX in Intel GPU
  2961. //TODO: currently, it's not used for XMX really.
  2962. #define SYCL_USE_XMX
  2963. // max batch size to use MMQ kernels when tensor cores are available
  2964. #define XMX_MAX_BATCH_SIZE 32
  2965. #if defined(_MSC_VER)
  2966. #pragma warning(disable: 4244 4267) // possible loss of data
  2967. #endif
  2968. // dmmv = dequantize_mul_mat_vec
  2969. #ifndef GGML_SYCL_DMMV_X
  2970. #define GGML_SYCL_DMMV_X 32
  2971. #endif
  2972. #ifndef GGML_SYCL_MMV_Y
  2973. #define GGML_SYCL_MMV_Y 1
  2974. #endif
  2975. static_assert(sizeof(sycl::half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  2976. static void crash(){
  2977. int *ptr = NULL;
  2978. *ptr = 0;
  2979. }
  2980. static void ggml_sycl_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  2981. fprintf(stderr, "SYCL error: %s: %s\n", stmt, msg);
  2982. fprintf(stderr, " in function %s at %s:%d\n", func, file, line);
  2983. GGML_ASSERT(!"SYCL error");
  2984. }
  2985. #define SYCL_CHECK(err) do { \
  2986. auto err_ = (err); if (err_ != 0) ggml_sycl_error( \
  2987. #err, __func__, __FILE__, __LINE__, \
  2988. "Meet error in this line code!"); \
  2989. } while (0)
  2990. #if DPCT_COMPAT_RT_VERSION >= 11100
  2991. #define GGML_SYCL_ASSUME(x) __builtin_assume(x)
  2992. #else
  2993. #define GGML_SYCL_ASSUME(x)
  2994. #endif // DPCT_COMPAT_RT_VERSION >= 11100
  2995. #ifdef GGML_SYCL_F16
  2996. typedef sycl::half dfloat; // dequantize float
  2997. typedef sycl::half2 dfloat2;
  2998. #else
  2999. typedef float dfloat; // dequantize float
  3000. typedef sycl::float2 dfloat2;
  3001. #endif //GGML_SYCL_F16
  3002. bool ggml_sycl_loaded(void);
  3003. void * ggml_sycl_host_malloc(size_t size);
  3004. void ggml_sycl_host_free(void * ptr);
  3005. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
  3006. void ggml_sycl_free_data(struct ggml_tensor * tensor);
  3007. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor);
  3008. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor);
  3009. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor);
  3010. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor);
  3011. void ggml_sycl_copy_to_device(struct ggml_tensor * tensor);
  3012. void ggml_sycl_set_main_device(int main_device);
  3013. void ggml_sycl_set_mul_mat_q(bool mul_mat_q);
  3014. void ggml_sycl_set_scratch_size(size_t scratch_size);
  3015. void ggml_sycl_free_scratch(void);
  3016. void ggml_sycl_get_device_description(int device, char * description, size_t description_size);
  3017. bool ggml_backend_is_sycl(ggml_backend_t backend);
  3018. int ggml_backend_sycl_get_device(ggml_backend_t backend);
  3019. int get_main_device();
  3020. void print_ggml_tensor(const char*name, struct ggml_tensor *src);
  3021. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt);
  3022. void dev2dev_memcpy(sycl::queue &q_dst, sycl::queue &q_src, void *ptr_dst,
  3023. const void *ptr_src, size_t size) {
  3024. char *host_buf = (char *)malloc(size);
  3025. q_src.memcpy(host_buf, (const char *)ptr_src, size).wait();
  3026. q_dst.memcpy((char *)ptr_dst, host_buf, size).wait();
  3027. free(host_buf);
  3028. }
  3029. static __dpct_inline__ int get_int_from_int8(const int8_t *x8, const int &i32) {
  3030. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  3031. int x32 = 0;
  3032. x32 |= x16[0] << 0;
  3033. x32 |= x16[1] << 16;
  3034. return x32;
  3035. }
  3036. static __dpct_inline__ int get_int_from_uint8(const uint8_t *x8,
  3037. const int &i32) {
  3038. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  3039. int x32 = 0;
  3040. x32 |= x16[0] << 0;
  3041. x32 |= x16[1] << 16;
  3042. return x32;
  3043. }
  3044. static __dpct_inline__ int get_int_from_int8_aligned(const int8_t *x8,
  3045. const int &i32) {
  3046. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  3047. }
  3048. static __dpct_inline__ int get_int_from_uint8_aligned(const uint8_t *x8,
  3049. const int &i32) {
  3050. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  3051. }
  3052. template <typename T>
  3053. using to_t_sycl_t = void (*)(const void *__restrict__ x, T *__restrict__ y,
  3054. int k, dpct::queue_ptr stream);
  3055. typedef to_t_sycl_t<float> to_fp32_sycl_t;
  3056. typedef to_t_sycl_t<sycl::half> to_fp16_sycl_t;
  3057. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  3058. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  3059. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  3060. typedef void (*ggml_sycl_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  3061. typedef void (*ggml_sycl_op_mul_mat_t)(
  3062. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  3063. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  3064. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  3065. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  3066. const dpct::queue_ptr &stream);
  3067. typedef void (*ggml_sycl_op_flatten_t)(const ggml_tensor *src0,
  3068. const ggml_tensor *src1,
  3069. ggml_tensor *dst, const float *src0_dd,
  3070. const float *src1_dd, float *dst_dd,
  3071. const dpct::queue_ptr &main_stream);
  3072. // QK = number of values after dequantization
  3073. // QR = QK / number of values before dequantization
  3074. // QI = number of 32 bit integers before dequantization
  3075. #define QK4_0 32
  3076. #define QR4_0 2
  3077. #define QI4_0 (QK4_0 / (4 * QR4_0))
  3078. typedef struct dpct_type_block_q4_0 {
  3079. sycl::half d; // delta
  3080. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  3081. } block_q4_0;
  3082. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  3083. #define QK4_1 32
  3084. #define QR4_1 2
  3085. #define QI4_1 (QK4_1 / (4 * QR4_1))
  3086. typedef struct dpct_type_block_q4_1 {
  3087. sycl::half2 dm; // dm.x = delta, dm.y = min
  3088. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  3089. } block_q4_1;
  3090. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  3091. #define QK5_0 32
  3092. #define QR5_0 2
  3093. #define QI5_0 (QK5_0 / (4 * QR5_0))
  3094. typedef struct dpct_type_block_q5_0 {
  3095. sycl::half d; // delta
  3096. uint8_t qh[4]; // 5-th bit of quants
  3097. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  3098. } block_q5_0;
  3099. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  3100. #define QK5_1 32
  3101. #define QR5_1 2
  3102. #define QI5_1 (QK5_1 / (4 * QR5_1))
  3103. typedef struct dpct_type_block_q5_1 {
  3104. sycl::half2 dm; // dm.x = delta, dm.y = min
  3105. uint8_t qh[4]; // 5-th bit of quants
  3106. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  3107. } block_q5_1;
  3108. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  3109. #define QK8_0 32
  3110. #define QR8_0 1
  3111. #define QI8_0 (QK8_0 / (4 * QR8_0))
  3112. typedef struct dpct_type_block_q8_0 {
  3113. sycl::half d; // delta
  3114. int8_t qs[QK8_0]; // quants
  3115. } block_q8_0;
  3116. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  3117. #define QK8_1 32
  3118. #define QR8_1 1
  3119. #define QI8_1 (QK8_1 / (4 * QR8_1))
  3120. typedef struct dpct_type_block_q8_1 {
  3121. sycl::half2 ds; // ds.x = delta, ds.y = sum
  3122. int8_t qs[QK8_0]; // quants
  3123. } block_q8_1;
  3124. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  3125. typedef float (*vec_dot_q_sycl_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  3126. typedef void (*allocate_tiles_sycl_t)(int **x_ql, sycl::half2 **x_dm,
  3127. int **x_qh, int **x_sc);
  3128. typedef void (*load_tiles_sycl_t)(const void *__restrict__ vx,
  3129. int *__restrict__ x_ql,
  3130. sycl::half2 *__restrict__ x_dm,
  3131. int *__restrict__ x_qh,
  3132. int *__restrict__ x_sc, const int &i_offset,
  3133. const int &i_max, const int &k,
  3134. const int &blocks_per_row);
  3135. typedef float (*vec_dot_q_mul_mat_sycl_t)(
  3136. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  3137. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  3138. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ms,
  3139. const int &i, const int &j, const int &k);
  3140. //================================= k-quants
  3141. #ifdef GGML_QKK_64
  3142. #define QK_K 64
  3143. #define K_SCALE_SIZE 4
  3144. #else
  3145. #define QK_K 256
  3146. #define K_SCALE_SIZE 12
  3147. #endif
  3148. #define QR2_K 4
  3149. #define QI2_K (QK_K / (4*QR2_K))
  3150. typedef struct dpct_type_block_q2_K {
  3151. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  3152. uint8_t qs[QK_K/4]; // quants
  3153. sycl::half2 dm; // super-block scale for quantized scales/mins
  3154. } block_q2_K;
  3155. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  3156. #define QR3_K 4
  3157. #define QI3_K (QK_K / (4*QR3_K))
  3158. typedef struct dpct_type_block_q3_K {
  3159. uint8_t hmask[QK_K/8]; // quants - high bit
  3160. uint8_t qs[QK_K/4]; // quants - low 2 bits
  3161. #ifdef GGML_QKK_64
  3162. uint8_t scales[2]; // scales, quantized with 8 bits
  3163. #else
  3164. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  3165. #endif
  3166. sycl::half d; // super-block scale
  3167. } block_q3_K;
  3168. //static_assert(sizeof(block_q3_K) == sizeof(ggml_fp16_t) + QK_K / 4 + QK_K / 8 + K_SCALE_SIZE, "wrong q3_K block size/padding");
  3169. #define QR4_K 2
  3170. #define QI4_K (QK_K / (4*QR4_K))
  3171. #ifdef GGML_QKK_64
  3172. typedef struct {
  3173. sycl::half dm[2]; // super-block scales/mins
  3174. uint8_t scales[2]; // 4-bit block scales/mins
  3175. uint8_t qs[QK_K/2]; // 4--bit quants
  3176. } block_q4_K;
  3177. static_assert(sizeof(block_q4_K) == sizeof(sycl::half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  3178. #else
  3179. typedef struct dpct_type_block_q4_K {
  3180. sycl::half2 dm; // super-block scale for quantized scales/mins
  3181. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  3182. uint8_t qs[QK_K/2]; // 4--bit quants
  3183. } block_q4_K;
  3184. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  3185. #endif
  3186. #define QR5_K 2
  3187. #define QI5_K (QK_K / (4*QR5_K))
  3188. #ifdef GGML_QKK_64
  3189. typedef struct {
  3190. sycl::half d; // super-block scale
  3191. int8_t scales[QK_K/16]; // block scales
  3192. uint8_t qh[QK_K/8]; // quants, high bit
  3193. uint8_t qs[QK_K/2]; // quants, low 4 bits
  3194. } block_q5_K;
  3195. static_assert(sizeof(block_q5_K) == sizeof(ggml_fp16_t) + QK_K/2 + QK_K/8 + QK_K/16, "wrong q5_K block size/padding");
  3196. #else
  3197. typedef struct dpct_type_block_q5_K {
  3198. sycl::half2 dm; // super-block scale for quantized scales/mins
  3199. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  3200. uint8_t qh[QK_K/8]; // quants, high bit
  3201. uint8_t qs[QK_K/2]; // quants, low 4 bits
  3202. } block_q5_K;
  3203. static_assert(sizeof(block_q5_K) == 2*sizeof(ggml_fp16_t) + K_SCALE_SIZE + QK_K/2 + QK_K/8, "wrong q5_K block size/padding");
  3204. #endif
  3205. #define QR6_K 2
  3206. #define QI6_K (QK_K / (4*QR6_K))
  3207. typedef struct dpct_type_block_q6_K {
  3208. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  3209. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  3210. int8_t scales[QK_K/16]; // scales
  3211. sycl::half d; // delta
  3212. } block_q6_K;
  3213. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  3214. #define QR2_XXS 8
  3215. #define QI2_XXS (QK_K / (4*QR2_XXS))
  3216. typedef struct dpct_type_block_iq2_xxs {
  3217. sycl::half d;
  3218. uint16_t qs[QK_K/8];
  3219. } block_iq2_xxs;
  3220. static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");
  3221. #define QR2_XS 8
  3222. #define QI2_XS (QK_K / (4*QR2_XS))
  3223. typedef struct dpct_type_block_iq2_xs {
  3224. sycl::half d;
  3225. uint16_t qs[QK_K/8];
  3226. uint8_t scales[QK_K/32];
  3227. } block_iq2_xs;
  3228. static_assert(sizeof(block_iq2_xs) == sizeof(ggml_fp16_t) + QK_K/8*sizeof(uint16_t) + QK_K/32, "wrong iq2_xs block size/padding");
  3229. #define QR3_XXS 8
  3230. #define QI3_XXS (QK_K / (4*QR3_XXS))
  3231. typedef struct dpct_type_block_iq3_xxs {
  3232. sycl::half d;
  3233. uint8_t qs[3*(QK_K/8)];
  3234. } block_iq3_xxs;
  3235. static_assert(sizeof(block_iq3_xxs) == sizeof(ggml_fp16_t) + 3*(QK_K/8), "wrong iq3_xxs block size/padding");
  3236. #define WARP_SIZE 32
  3237. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  3238. #define SYCL_GELU_BLOCK_SIZE 256
  3239. #define SYCL_SILU_BLOCK_SIZE 256
  3240. #define SYCL_TANH_BLOCK_SIZE 256
  3241. #define SYCL_RELU_BLOCK_SIZE 256
  3242. #define SYCL_HARDSIGMOID_BLOCK_SIZE 256
  3243. #define SYCL_HARDSWISH_BLOCK_SIZE 256
  3244. #define SYCL_SQR_BLOCK_SIZE 256
  3245. #define SYCL_CPY_BLOCK_SIZE 32
  3246. #define SYCL_SCALE_BLOCK_SIZE 256
  3247. #define SYCL_CLAMP_BLOCK_SIZE 256
  3248. #define SYCL_ROPE_BLOCK_SIZE 256
  3249. #define SYCL_SOFT_MAX_BLOCK_SIZE 1024
  3250. #define SYCL_ALIBI_BLOCK_SIZE 32
  3251. #define SYCL_DIAG_MASK_INF_BLOCK_SIZE 32
  3252. #define SYCL_QUANTIZE_BLOCK_SIZE 256
  3253. #define SYCL_DEQUANTIZE_BLOCK_SIZE 256
  3254. #define SYCL_GET_ROWS_BLOCK_SIZE 256
  3255. #define SYCL_UPSCALE_BLOCK_SIZE 256
  3256. #define SYCL_CONCAT_BLOCK_SIZE 256
  3257. #define SYCL_PAD_BLOCK_SIZE 256
  3258. #define SYCL_ACC_BLOCK_SIZE 256
  3259. #define SYCL_IM2COL_BLOCK_SIZE 256
  3260. #define SYCL_POOL2D_BLOCK_SIZE 256
  3261. // dmmv = dequantize_mul_mat_vec
  3262. #ifndef GGML_SYCL_DMMV_X
  3263. #define GGML_SYCL_DMMV_X 32
  3264. #endif
  3265. #ifndef GGML_SYCL_MMV_Y
  3266. #define GGML_SYCL_MMV_Y 1
  3267. #endif
  3268. #ifndef K_QUANTS_PER_ITERATION
  3269. #define K_QUANTS_PER_ITERATION 2
  3270. #else
  3271. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  3272. #endif
  3273. #ifndef GGML_SYCL_PEER_MAX_BATCH_SIZE
  3274. #define GGML_SYCL_PEER_MAX_BATCH_SIZE 128
  3275. #endif // GGML_SYCL_PEER_MAX_BATCH_SIZE
  3276. #define MUL_MAT_SRC1_COL_STRIDE 128
  3277. #define MAX_STREAMS 8
  3278. static dpct::queue_ptr g_syclStreams[GGML_SYCL_MAX_DEVICES][MAX_STREAMS] = {{0}};
  3279. struct ggml_tensor_extra_gpu {
  3280. void * data_device[GGML_SYCL_MAX_DEVICES]; // 1 pointer for each device for split tensors
  3281. dpct::event_ptr
  3282. events[GGML_SYCL_MAX_DEVICES]
  3283. [MAX_STREAMS]; // events for synchronizing multiple GPUs
  3284. };
  3285. class sycl_gpu_mgr {
  3286. public:
  3287. std::vector<int> gpus;
  3288. std::vector<sycl::device> devices;
  3289. sycl::queue *first_queue;
  3290. sycl::context co_ctx;
  3291. int max_compute_units = 0;
  3292. int work_group_size = 0;
  3293. std::string gpus_list = "";
  3294. sycl_gpu_mgr() {
  3295. detect_sycl_gpu_list_with_max_cu();
  3296. get_allow_gpus();
  3297. create_context_with_gpus();
  3298. }
  3299. void create_context_with_gpus() {
  3300. sycl::context ctx = sycl::context(devices);
  3301. assert(gpus.size() > 0);
  3302. first_queue = dpct::get_current_device().create_queue(ctx, devices[0]);
  3303. co_ctx = first_queue->get_context();
  3304. }
  3305. sycl::context &get_co_ctx() { return co_ctx; }
  3306. void get_allow_gpus() {
  3307. gpus_list = "";
  3308. for (size_t i = 0; i < gpus.size(); ++i) {
  3309. gpus_list += std::to_string(gpus[i]);
  3310. gpus_list += ",";
  3311. }
  3312. if (gpus_list.length() > 2) {
  3313. gpus_list.pop_back();
  3314. }
  3315. }
  3316. bool is_allowed_gpu(int device_id) {
  3317. return std::find(gpus.begin(), gpus.end(), device_id) != gpus.end();
  3318. }
  3319. void detect_sycl_gpu_list_with_max_cu() try {
  3320. int device_count = dpct::dev_mgr::instance().device_count();
  3321. for (int id = 0; id < device_count; id++) {
  3322. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  3323. if (!device.is_gpu())
  3324. continue;
  3325. dpct::device_info prop;
  3326. dpct::get_device_info(prop, device);
  3327. if (max_compute_units < prop.get_max_compute_units())
  3328. max_compute_units = prop.get_max_compute_units();
  3329. }
  3330. for (int id = 0; id < device_count; id++) {
  3331. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  3332. if (!device.is_gpu())
  3333. continue;
  3334. dpct::device_info prop;
  3335. dpct::get_device_info(prop, device);
  3336. if (max_compute_units == prop.get_max_compute_units() &&
  3337. prop.get_major_version() == 1) {
  3338. gpus.push_back(id);
  3339. devices.push_back(device);
  3340. work_group_size = prop.get_max_work_group_size();
  3341. }
  3342. }
  3343. return;
  3344. } catch (sycl::exception const &exc) {
  3345. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3346. << ", line:" << __LINE__ << std::endl;
  3347. std::exit(1);
  3348. }
  3349. int get_gpu_count() { return (int)gpus.size(); }
  3350. int get_index(int id) {
  3351. for (int i = 0; i < (int)gpus.size(); i++) {
  3352. if (gpus[i] == id)
  3353. return i;
  3354. }
  3355. assert(false);
  3356. return -1;
  3357. }
  3358. int get_next_index(int id) {
  3359. int cur_index = get_index(id);
  3360. for (int i = cur_index + 1; i < (int)gpus.size(); i++) {
  3361. if (gpus[i] == id)
  3362. return i;
  3363. }
  3364. assert(false);
  3365. return -1;
  3366. }
  3367. };
  3368. static sycl_gpu_mgr *g_sycl_gpu_mgr = NULL;
  3369. static int g_device_count = -1;
  3370. static int g_all_sycl_device_count = -1;
  3371. static int g_main_device = -1;
  3372. static int g_main_device_id = -1;
  3373. static std::array<float, GGML_SYCL_MAX_DEVICES> g_default_tensor_split = {};
  3374. static float g_tensor_split[GGML_SYCL_MAX_DEVICES] = {0};
  3375. struct sycl_device_capabilities {
  3376. int cc; // compute capability
  3377. bool vmm; // virtual memory support
  3378. size_t vmm_granularity; // granularity of virtual memory
  3379. int device_id;
  3380. };
  3381. static sycl_device_capabilities g_device_caps[GGML_SYCL_MAX_DEVICES] = { {0, false, 0, -1} };
  3382. struct sycl_device_id2index {
  3383. int index;
  3384. };
  3385. static void * g_scratch_buffer = nullptr;
  3386. static size_t g_scratch_size = 0; // disabled by default
  3387. static size_t g_scratch_offset = 0;
  3388. static dpct::queue_ptr g_sycl_handles[GGML_SYCL_MAX_DEVICES] = {nullptr};
  3389. int get_main_device(){
  3390. return g_main_device;
  3391. }
  3392. [[noreturn]]
  3393. static void bad_arch(const sycl::stream &stream_ct1) {
  3394. stream_ct1 << "ERROR: ggml-sycl was compiled without support for the "
  3395. "current GPU architecture.\n";
  3396. // __trap();
  3397. std::exit(1);
  3398. (void) bad_arch; // suppress unused function warning
  3399. }
  3400. /*
  3401. device_index: device index from 0 to n (continue numbers).
  3402. It is used for device select/set in SYCL backend internal data structure.
  3403. */
  3404. void check_allow_gpu_index(const int device_index) {
  3405. if (device_index >= g_device_count) {
  3406. char error_buf[256];
  3407. snprintf(error_buf, sizeof(error_buf),
  3408. "%s error: device_index:%d is out of range: [0-%d]", __func__,
  3409. device_index, g_device_count - 1);
  3410. fprintf(stderr, "%s\n", error_buf);
  3411. assert(false);
  3412. }
  3413. }
  3414. /*
  3415. device_id: device ID is shown by ggml_backend_sycl_print_sycl_devices().
  3416. It is only used to set current working device.
  3417. */
  3418. void check_allow_gpu_id(const int device_id) {
  3419. if (!g_sycl_gpu_mgr->is_allowed_gpu(device_id)) {
  3420. char error_buf[256];
  3421. snprintf(error_buf, sizeof(error_buf),
  3422. "error: cannot set device=%d, which is not allowed. Please "
  3423. "set GPU ID in: [%s]",
  3424. device_id, g_sycl_gpu_mgr->gpus_list.c_str());
  3425. fprintf(stderr, "%s\n", error_buf);
  3426. throw std::invalid_argument(error_buf);
  3427. }
  3428. }
  3429. int get_current_device_id() {
  3430. return dpct::dev_mgr::instance().current_device_id();
  3431. }
  3432. inline dpct::err0 ggml_sycl_set_device(const int device) try {
  3433. int device_id = g_sycl_gpu_mgr->gpus[device];
  3434. check_allow_gpu_id(device_id);
  3435. int current_device_id;
  3436. SYCL_CHECK(CHECK_TRY_ERROR(current_device_id = get_current_device_id()));
  3437. // GGML_SYCL_DEBUG("ggml_sycl_set_device device_id=%d,
  3438. // current_device_id=%d\n", device, current_device);
  3439. if (device_id == current_device_id) {
  3440. return 0;
  3441. }
  3442. return CHECK_TRY_ERROR(dpct::select_device(device_id));
  3443. } catch (sycl::exception const &exc) {
  3444. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3445. << ", line:" << __LINE__ << std::endl;
  3446. crash();
  3447. std::exit(1);
  3448. }
  3449. void log_ggml_var_device(const char*name, float *src, size_t total_elements, bool src_on_device){
  3450. if(!g_ggml_sycl_debug) return;
  3451. if(!src){
  3452. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3453. return;
  3454. }
  3455. char filename[1024];
  3456. sprintf(filename, "%s.txt", name);
  3457. printf("GGML Tensor:%s save to %s\n", name, filename);
  3458. size_t total_size = total_elements*sizeof(float);
  3459. float *local_buf = NULL;
  3460. if(src_on_device) {
  3461. local_buf = (float *) ggml_sycl_host_malloc(total_size);
  3462. ggml_sycl_set_device(g_main_device);
  3463. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3464. main_stream->memcpy(local_buf, src, total_size).wait();
  3465. }
  3466. else {
  3467. local_buf = (float *)src;
  3468. }
  3469. std::ofstream logfile;
  3470. logfile.open(filename);
  3471. for(size_t i=0; i<total_elements; i++){
  3472. logfile << local_buf[i] <<" ";
  3473. if((i+1)%20 ==0) logfile <<std::endl;
  3474. }
  3475. logfile <<std::endl;
  3476. logfile.close();
  3477. if(src_on_device) ggml_sycl_host_free(local_buf);
  3478. }
  3479. void log_ggml_var_device_fp16(const char*name, sycl::half *src, size_t total_elements, bool src_on_device){
  3480. if(!g_ggml_sycl_debug) return;
  3481. if(!src){
  3482. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3483. return;
  3484. }
  3485. char filename[1024];
  3486. sprintf(filename, "%s.txt", name);
  3487. printf("GGML Tensor:%s save to %s\n", name, filename);
  3488. size_t total_size = total_elements*sizeof(sycl::half);
  3489. sycl::half *local_buf = NULL;
  3490. if(src_on_device) {
  3491. local_buf = (sycl::half *) ggml_sycl_host_malloc(total_size);
  3492. ggml_sycl_set_device(g_main_device);
  3493. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3494. main_stream->memcpy(local_buf, src, total_size).wait();
  3495. }
  3496. else {
  3497. local_buf = (sycl::half *)src;
  3498. }
  3499. std::ofstream logfile;
  3500. logfile.open(filename);
  3501. for(size_t i=0; i<total_elements; i++){
  3502. logfile << local_buf[i] <<" ";
  3503. if((i+1)%20 ==0) logfile <<std::endl;
  3504. }
  3505. logfile <<std::endl;
  3506. logfile.close();
  3507. if(src_on_device) ggml_sycl_host_free(local_buf);
  3508. }
  3509. //todo: debug for crash in some case
  3510. void print_ggml_tensor(const char*name, struct ggml_tensor *src){
  3511. if(!g_ggml_sycl_debug) return;
  3512. if(!src){
  3513. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3514. return;
  3515. }
  3516. size_t total_elements = ggml_nelements(src);
  3517. const bool src_on_device = src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  3518. float *src_data =NULL;
  3519. if(src_on_device) {
  3520. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  3521. src_data = (float*)src_extra->data_device[g_main_device];
  3522. }
  3523. else {
  3524. src_data = (float *)src->data;
  3525. }
  3526. log_ggml_var_device(name, src_data, total_elements, src_on_device);
  3527. }
  3528. static int log_file_name_idx=0;
  3529. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt) {
  3530. stop_cnt = 4;
  3531. if(log_file_name_idx>=stop_cnt) return;
  3532. char filename[1280];
  3533. sprintf(filename, "%s_%07d", name, log_file_name_idx);
  3534. log_file_name_idx++;
  3535. print_ggml_tensor(filename, src);
  3536. }
  3537. static __dpct_inline__ float warp_reduce_sum(float x,
  3538. const sycl::nd_item<3> &item_ct1) {
  3539. #pragma unroll
  3540. for (int mask = 16; mask > 0; mask >>= 1) {
  3541. /*
  3542. DPCT1096:98: The right-most dimension of the work-group used in the SYCL
  3543. kernel that calls this function may be less than "32". The function
  3544. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3545. CPU device. Modify the size of the work-group to ensure that the value
  3546. of the right-most dimension is a multiple of "32".
  3547. */
  3548. x += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), x, mask);
  3549. }
  3550. return x;
  3551. }
  3552. static __dpct_inline__ sycl::float2
  3553. warp_reduce_sum(sycl::float2 a, const sycl::nd_item<3> &item_ct1) {
  3554. #pragma unroll
  3555. for (int mask = 16; mask > 0; mask >>= 1) {
  3556. a.x() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.x(),
  3557. mask);
  3558. a.y() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.y(),
  3559. mask);
  3560. }
  3561. return a;
  3562. }
  3563. static __dpct_inline__ float warp_reduce_max(float x,
  3564. const sycl::nd_item<3> &item_ct1) {
  3565. #pragma unroll
  3566. for (int mask = 16; mask > 0; mask >>= 1) {
  3567. /*
  3568. DPCT1096:97: The right-most dimension of the work-group used in the SYCL
  3569. kernel that calls this function may be less than "32". The function
  3570. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3571. CPU device. Modify the size of the work-group to ensure that the value
  3572. of the right-most dimension is a multiple of "32".
  3573. */
  3574. x = sycl::fmax(x, dpct::permute_sub_group_by_xor(
  3575. item_ct1.get_sub_group(), x, mask));
  3576. }
  3577. return x;
  3578. }
  3579. static __dpct_inline__ float op_repeat(const float a, const float b) {
  3580. return b;
  3581. GGML_UNUSED(a);
  3582. }
  3583. static __dpct_inline__ float op_add(const float a, const float b) {
  3584. return a + b;
  3585. }
  3586. static __dpct_inline__ float op_mul(const float a, const float b) {
  3587. return a * b;
  3588. }
  3589. static __dpct_inline__ float op_div(const float a, const float b) {
  3590. return a / b;
  3591. }
  3592. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3593. static void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3594. int ne0, int ne1, int ne2, int ne3,
  3595. int ne10, int ne11, int ne12, int ne13,
  3596. /*int s0, */ int s1, int s2, int s3,
  3597. /*int s10,*/ int s11, int s12, int s13,
  3598. const sycl::nd_item<3> &item_ct1) {
  3599. const int i0s = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3600. item_ct1.get_local_id(2);
  3601. const int i1 = (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  3602. item_ct1.get_local_id(1));
  3603. const int i2 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3604. item_ct1.get_local_id(0)) /
  3605. ne3;
  3606. const int i3 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3607. item_ct1.get_local_id(0)) %
  3608. ne3;
  3609. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3610. return;
  3611. }
  3612. const int i11 = i1 % ne11;
  3613. const int i12 = i2 % ne12;
  3614. const int i13 = i3 % ne13;
  3615. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3616. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3617. const size_t i_dst = i_src0;
  3618. const src0_t * src0_row = src0 + i_src0;
  3619. const src1_t * src1_row = src1 + i_src1;
  3620. dst_t * dst_row = dst + i_dst;
  3621. for (int i0 = i0s; i0 < ne0;
  3622. i0 += item_ct1.get_local_range(2) * item_ct1.get_group_range(2)) {
  3623. const int i10 = i0 % ne10;
  3624. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3625. }
  3626. }
  3627. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3628. static void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3629. int ne0, int ne1, int ne2, int ne3,
  3630. int ne10, int ne11, int ne12, int ne13,
  3631. /*int s0, */ int s1, int s2, int s3,
  3632. /*int s10,*/ int s11, int s12, int s13,
  3633. const sycl::nd_item<3> &item_ct1) {
  3634. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3635. item_ct1.get_local_id(2);
  3636. const int i3 = i/(ne2*ne1*ne0);
  3637. const int i2 = (i/(ne1*ne0)) % ne2;
  3638. const int i1 = (i/ne0) % ne1;
  3639. const int i0 = i % ne0;
  3640. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3641. return;
  3642. }
  3643. const int i11 = i1 % ne11;
  3644. const int i12 = i2 % ne12;
  3645. const int i13 = i3 % ne13;
  3646. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3647. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3648. const size_t i_dst = i_src0;
  3649. const src0_t * src0_row = src0 + i_src0;
  3650. const src1_t * src1_row = src1 + i_src1;
  3651. dst_t * dst_row = dst + i_dst;
  3652. const int i10 = i0 % ne10;
  3653. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3654. }
  3655. static void acc_f32(const float * x, const float * y, float * dst, const int ne,
  3656. const int ne10, const int ne11, const int ne12,
  3657. const int nb1, const int nb2, int offset, const sycl::nd_item<3> &item_ct1) {
  3658. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3659. item_ct1.get_local_id(2);
  3660. if (i >= ne) {
  3661. return;
  3662. }
  3663. int src1_idx = i - offset;
  3664. int oz = src1_idx / nb2;
  3665. int oy = (src1_idx - (oz * nb2)) / nb1;
  3666. int ox = src1_idx % nb1;
  3667. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  3668. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  3669. } else {
  3670. dst[i] = x[i];
  3671. }
  3672. }
  3673. static void gelu_f32(const float * x, float * dst, const int k,
  3674. const sycl::nd_item<3> &item_ct1) {
  3675. const float GELU_COEF_A = 0.044715f;
  3676. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  3677. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3678. item_ct1.get_local_id(2);
  3679. if (i >= k) {
  3680. return;
  3681. }
  3682. float xi = x[i];
  3683. dst[i] = 0.5f * xi *
  3684. (1.0f +
  3685. sycl::tanh(SQRT_2_OVER_PI * xi * (1.0f + GELU_COEF_A * xi * xi)));
  3686. }
  3687. static void silu_f32(const float * x, float * dst, const int k,
  3688. const sycl::nd_item<3> &item_ct1) {
  3689. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3690. item_ct1.get_local_id(2);
  3691. if (i >= k) {
  3692. return;
  3693. }
  3694. dst[i] = x[i] / (1.0f + sycl::native::exp(-x[i]));
  3695. }
  3696. static void gelu_quick_f32(const float *x, float *dst, int k,
  3697. const sycl::nd_item<3> &item_ct1) {
  3698. const float GELU_QUICK_COEF = -1.702f;
  3699. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3700. item_ct1.get_local_id(2);
  3701. if (i >= k) {
  3702. return;
  3703. }
  3704. dst[i] = x[i] * (1.0f / (1.0f + sycl::native::exp(GELU_QUICK_COEF * x[i])));
  3705. }
  3706. static void tanh_f32(const float *x, float *dst, int k,
  3707. const sycl::nd_item<3> &item_ct1) {
  3708. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3709. item_ct1.get_local_id(2);
  3710. if (i >= k) {
  3711. return;
  3712. }
  3713. dst[i] = sycl::tanh((float)(x[i]));
  3714. }
  3715. static void relu_f32(const float * x, float * dst, const int k,
  3716. const sycl::nd_item<3> &item_ct1) {
  3717. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3718. item_ct1.get_local_id(2);
  3719. if (i >= k) {
  3720. return;
  3721. }
  3722. dst[i] = sycl::fmax((float)(x[i]), (float)0);
  3723. }
  3724. static void hardsigmoid_f32(const float * x, float * dst, const int k,
  3725. const sycl::nd_item<3> &item_ct1) {
  3726. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3727. item_ct1.get_local_id(2);
  3728. if (i >= k) {
  3729. return;
  3730. }
  3731. dst[i] = sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3732. }
  3733. static void hardswish_f32(const float * x, float * dst, const int k,
  3734. const sycl::nd_item<3> &item_ct1) {
  3735. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3736. item_ct1.get_local_id(2);
  3737. if (i >= k) {
  3738. return;
  3739. }
  3740. dst[i] = x[i] * sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3741. }
  3742. static void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope,
  3743. const sycl::nd_item<3> &item_ct1) {
  3744. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3745. item_ct1.get_local_id(2);
  3746. if (i >= k) {
  3747. return;
  3748. }
  3749. dst[i] = sycl::fmax((float)(x[i]), (float)0) +
  3750. sycl::fmin((float)(x[i]), 0.0f) * negative_slope;
  3751. }
  3752. static void sqr_f32(const float * x, float * dst, const int k,
  3753. const sycl::nd_item<3> &item_ct1) {
  3754. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3755. item_ct1.get_local_id(2);
  3756. if (i >= k) {
  3757. return;
  3758. }
  3759. dst[i] = x[i] * x[i];
  3760. }
  3761. static void norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3762. const sycl::nd_item<3> &item_ct1, sycl::float2 *s_sum, int block_size) {
  3763. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3764. item_ct1.get_local_id(1);
  3765. const int tid = item_ct1.get_local_id(2);
  3766. sycl::float2 mean_var = sycl::float2(0.f, 0.f);
  3767. for (int col = tid; col < ncols; col += block_size) {
  3768. const float xi = x[row*ncols + col];
  3769. mean_var.x() += xi;
  3770. mean_var.y() += xi * xi;
  3771. }
  3772. // sum up partial sums
  3773. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3774. if (block_size > WARP_SIZE) {
  3775. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3776. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3777. if (lane_id == 0) {
  3778. s_sum[warp_id] = mean_var;
  3779. }
  3780. /*
  3781. DPCT1118:0: SYCL group functions and algorithms must be encountered in
  3782. converged control flow. You may need to adjust the code.
  3783. */
  3784. item_ct1.barrier(sycl::access::fence_space::local_space);
  3785. mean_var = s_sum[lane_id];
  3786. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3787. }
  3788. const float mean = mean_var.x() / ncols;
  3789. const float var = mean_var.y() / ncols - mean * mean;
  3790. const float inv_std = sycl::rsqrt(var + eps);
  3791. for (int col = tid; col < ncols; col += block_size) {
  3792. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  3793. }
  3794. }
  3795. static void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02,
  3796. const sycl::nd_item<3> &item_ct1) {
  3797. int nidx = item_ct1.get_local_id(2) +
  3798. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3799. if (nidx >= ne0) {
  3800. return;
  3801. }
  3802. // operation
  3803. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3804. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3805. if (item_ct1.get_group(0) < ne02) { // src0
  3806. int offset_src =
  3807. nidx + item_ct1.get_group(1) * ne0 +
  3808. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3809. dst[offset_dst] = x[offset_src];
  3810. } else {
  3811. int offset_src =
  3812. nidx + item_ct1.get_group(1) * ne0 +
  3813. (item_ct1.get_group(0) - ne02) * ne0 * item_ct1.get_group_range(1);
  3814. dst[offset_dst] = y[offset_src];
  3815. }
  3816. }
  3817. static void upscale_f32(const float *x, float *dst, const int ne00, const int nb02, const int scale_factor,
  3818. const sycl::nd_item<3> &item_ct1) {
  3819. int ne0 = ne00 * scale_factor;
  3820. int nidx = item_ct1.get_local_id(2) +
  3821. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3822. if (nidx >= ne0) {
  3823. return;
  3824. }
  3825. // operation
  3826. int i00 = nidx / scale_factor;
  3827. int i01 = item_ct1.get_group(1) / scale_factor;
  3828. int offset_src = i00 + i01 * ne00 + item_ct1.get_group(0) * nb02;
  3829. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3830. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3831. dst[offset_dst] = x[offset_src];
  3832. }
  3833. static void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02,
  3834. const sycl::nd_item<3> &item_ct1) {
  3835. int nidx = item_ct1.get_local_id(2) +
  3836. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3837. if (nidx >= ne0) {
  3838. return;
  3839. }
  3840. // operation
  3841. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3842. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3843. if (nidx < ne00 && item_ct1.get_group(1) < ne01 &&
  3844. item_ct1.get_group(0) < ne02) {
  3845. int offset_src = nidx + item_ct1.get_group(1) * ne00 +
  3846. item_ct1.get_group(0) * ne00 * ne01;
  3847. dst[offset_dst] = x[offset_src];
  3848. } else {
  3849. dst[offset_dst] = 0.0f;
  3850. }
  3851. }
  3852. static void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps,
  3853. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3854. int start = item_ct1.get_group(2) * group_size;
  3855. int end = start + group_size;
  3856. start += item_ct1.get_local_id(2);
  3857. if (end >= ne_elements) {
  3858. end = ne_elements;
  3859. }
  3860. float tmp = 0.0f; // partial sum for thread in warp
  3861. for (int j = start; j < end; j += block_size) {
  3862. tmp += x[j];
  3863. }
  3864. tmp = warp_reduce_sum(tmp, item_ct1);
  3865. if (block_size > WARP_SIZE) {
  3866. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3867. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3868. if (lane_id == 0) {
  3869. s_sum[warp_id] = tmp;
  3870. }
  3871. /*
  3872. DPCT1118:1: SYCL group functions and algorithms must be encountered in
  3873. converged control flow. You may need to adjust the code.
  3874. */
  3875. /*
  3876. DPCT1065:54: Consider replacing sycl::nd_item::barrier() with
  3877. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3878. better performance if there is no access to global memory.
  3879. */
  3880. item_ct1.barrier();
  3881. tmp = s_sum[lane_id];
  3882. tmp = warp_reduce_sum(tmp, item_ct1);
  3883. }
  3884. float mean = tmp / group_size;
  3885. tmp = 0.0f;
  3886. for (int j = start; j < end; j += block_size) {
  3887. float xi = x[j] - mean;
  3888. dst[j] = xi;
  3889. tmp += xi * xi;
  3890. }
  3891. tmp = warp_reduce_sum(tmp, item_ct1);
  3892. if (block_size > WARP_SIZE) {
  3893. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3894. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3895. if (lane_id == 0) {
  3896. s_sum[warp_id] = tmp;
  3897. }
  3898. /*
  3899. DPCT1118:2: SYCL group functions and algorithms must be encountered in
  3900. converged control flow. You may need to adjust the code.
  3901. */
  3902. /*
  3903. DPCT1065:55: Consider replacing sycl::nd_item::barrier() with
  3904. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3905. better performance if there is no access to global memory.
  3906. */
  3907. item_ct1.barrier();
  3908. tmp = s_sum[lane_id];
  3909. tmp = warp_reduce_sum(tmp, item_ct1);
  3910. }
  3911. float variance = tmp / group_size;
  3912. float scale = sycl::rsqrt(variance + eps);
  3913. for (int j = start; j < end; j += block_size) {
  3914. dst[j] *= scale;
  3915. }
  3916. }
  3917. static void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3918. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3919. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3920. item_ct1.get_local_id(1);
  3921. const int tid = item_ct1.get_local_id(2);
  3922. float tmp = 0.0f; // partial sum for thread in warp
  3923. for (int col = tid; col < ncols; col += block_size) {
  3924. const float xi = x[row*ncols + col];
  3925. tmp += xi * xi;
  3926. }
  3927. // sum up partial sums
  3928. tmp = warp_reduce_sum(tmp, item_ct1);
  3929. if (block_size > WARP_SIZE) {
  3930. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3931. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3932. if (lane_id == 0) {
  3933. s_sum[warp_id] = tmp;
  3934. }
  3935. /*
  3936. DPCT1118:3: SYCL group functions and algorithms must be encountered in
  3937. converged control flow. You may need to adjust the code.
  3938. */
  3939. item_ct1.barrier(sycl::access::fence_space::local_space);
  3940. tmp = s_sum[lane_id];
  3941. tmp = warp_reduce_sum(tmp, item_ct1);
  3942. }
  3943. const float mean = tmp / ncols;
  3944. const float scale = sycl::rsqrt(mean + eps);
  3945. for (int col = tid; col < ncols; col += block_size) {
  3946. dst[row*ncols + col] = scale * x[row*ncols + col];
  3947. }
  3948. }
  3949. static __dpct_inline__ void dequantize_q4_0(const void *vx, const int ib,
  3950. const int iqs, dfloat2 &v) {
  3951. const block_q4_0 * x = (const block_q4_0 *) vx;
  3952. const dfloat d = x[ib].d;
  3953. const int vui = x[ib].qs[iqs];
  3954. v.x() = vui & 0xF;
  3955. v.y() = vui >> 4;
  3956. #ifdef GGML_SYCL_F16
  3957. // v = v - {8.0f, 8.0f};
  3958. // v = v * {d, d};
  3959. v.s0() = (v.s0() - 8.0f) * d;
  3960. v.s1() = (v.s1() - 8.0f) * d;
  3961. #else
  3962. v.x() = (v.x() - 8.0f) * d;
  3963. v.y() = (v.y() - 8.0f) * d;
  3964. #endif // GGML_SYCL_F16
  3965. }
  3966. static __dpct_inline__ void dequantize_q4_1(const void *vx, const int ib,
  3967. const int iqs, dfloat2 &v) {
  3968. const block_q4_1 * x = (const block_q4_1 *) vx;
  3969. const dfloat d = x[ib].dm[0];
  3970. const dfloat m = x[ib].dm[1];
  3971. const int vui = x[ib].qs[iqs];
  3972. v.x() = vui & 0xF;
  3973. v.y() = vui >> 4;
  3974. #ifdef GGML_SYCL_F16
  3975. // v = v * {d, d};
  3976. // v = v + {m, m};
  3977. v.s0() = (v.s0() * d) + m;
  3978. v.s1() = (v.s1() * d) + m;
  3979. #else
  3980. v.x() = (v.x() * d) + m;
  3981. v.y() = (v.y() * d) + m;
  3982. #endif // GGML_SYCL_F16
  3983. }
  3984. static __dpct_inline__ void dequantize_q5_0(const void *vx, const int ib,
  3985. const int iqs, dfloat2 &v) {
  3986. const block_q5_0 * x = (const block_q5_0 *) vx;
  3987. const dfloat d = x[ib].d;
  3988. uint32_t qh;
  3989. memcpy(&qh, x[ib].qh, sizeof(qh));
  3990. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3991. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3992. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3993. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3994. #ifdef GGML_SYCL_F16
  3995. // v = v - {16.0f, 16.0f};
  3996. // v = v * {d, d};
  3997. v.s0() = (v.s0() - 16.0f) * d;
  3998. v.s1() = (v.s1() - 16.0f) * d;
  3999. #else
  4000. v.x() = (v.x() - 16.0f) * d;
  4001. v.y() = (v.y() - 16.0f) * d;
  4002. #endif // GGML_SYCL_F16
  4003. }
  4004. static __dpct_inline__ void dequantize_q5_1(const void *vx, const int ib,
  4005. const int iqs, dfloat2 &v) {
  4006. const block_q5_1 * x = (const block_q5_1 *) vx;
  4007. const dfloat d = x[ib].dm[0];
  4008. const dfloat m = x[ib].dm[1];
  4009. uint32_t qh;
  4010. memcpy(&qh, x[ib].qh, sizeof(qh));
  4011. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  4012. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  4013. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  4014. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  4015. #ifdef GGML_SYCL_F16
  4016. // v = v * {d, d};
  4017. // v = v + {m, m};
  4018. v.s0() = (v.s0() * d) + m;
  4019. v.s1() = (v.s1() * d) + m;
  4020. #else
  4021. v.x() = (v.x() * d) + m;
  4022. v.y() = (v.y() * d) + m;
  4023. #endif // GGML_SYCL_F16
  4024. }
  4025. static __dpct_inline__ void dequantize_q8_0(const void *vx, const int ib,
  4026. const int iqs, dfloat2 &v) {
  4027. const block_q8_0 * x = (const block_q8_0 *) vx;
  4028. const dfloat d = x[ib].d;
  4029. v.x() = x[ib].qs[iqs + 0];
  4030. v.y() = x[ib].qs[iqs + 1];
  4031. #ifdef GGML_SYCL_F16
  4032. // v = v * {d, d};
  4033. v.s0() *= d;
  4034. v.s1() *= d;
  4035. #else
  4036. v.x() *= d;
  4037. v.y() *= d;
  4038. #endif // GGML_SYCL_F16
  4039. }
  4040. template<typename dst_t>
  4041. static void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32,
  4042. const sycl::nd_item<3> &item_ct1) {
  4043. const int i = item_ct1.get_group(2);
  4044. // assume 32 threads
  4045. const int tid = item_ct1.get_local_id(2);
  4046. const int il = tid/8;
  4047. const int ir = tid%8;
  4048. const int ib = 8*i + ir;
  4049. if (ib >= nb32) {
  4050. return;
  4051. }
  4052. dst_t * y = yy + 256*i + 32*ir + 4*il;
  4053. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  4054. const float d = sycl::vec<sycl::half, 1>(x->d)
  4055. .convert<float, sycl::rounding_mode::automatic>()[0];
  4056. const float dm = -8*d;
  4057. const uint8_t * q = x->qs + 4*il;
  4058. for (int l = 0; l < 4; ++l) {
  4059. y[l+ 0] = d * (q[l] & 0xF) + dm;
  4060. y[l+16] = d * (q[l] >> 4) + dm;
  4061. }
  4062. }
  4063. template<typename dst_t>
  4064. static void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32,
  4065. const sycl::nd_item<3> &item_ct1) {
  4066. const int i = item_ct1.get_group(2);
  4067. // assume 32 threads
  4068. const int tid = item_ct1.get_local_id(2);
  4069. const int il = tid/8;
  4070. const int ir = tid%8;
  4071. const int ib = 8*i + ir;
  4072. if (ib >= nb32) {
  4073. return;
  4074. }
  4075. dst_t * y = yy + 256*i + 32*ir + 4*il;
  4076. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  4077. const sycl::float2 d =
  4078. x->dm.convert<float, sycl::rounding_mode::automatic>();
  4079. const uint8_t * q = x->qs + 4*il;
  4080. for (int l = 0; l < 4; ++l) {
  4081. y[l + 0] = d.x() * (q[l] & 0xF) + d.y();
  4082. y[l + 16] = d.x() * (q[l] >> 4) + d.y();
  4083. }
  4084. }
  4085. //================================== k-quants
  4086. template<typename dst_t>
  4087. static void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4088. const sycl::nd_item<3> &item_ct1) {
  4089. const int i = item_ct1.get_group(2);
  4090. const block_q2_K * x = (const block_q2_K *) vx;
  4091. const int tid = item_ct1.get_local_id(2);
  4092. #if QK_K == 256
  4093. const int n = tid/32;
  4094. const int l = tid - 32*n;
  4095. const int is = 8*n + l/16;
  4096. const uint8_t q = x[i].qs[32*n + l];
  4097. dst_t * y = yy + i*QK_K + 128*n;
  4098. float dall = x[i].dm[0];
  4099. float dmin = x[i].dm[1];
  4100. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  4101. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  4102. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  4103. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  4104. #else
  4105. const int is = tid/16; // 0 or 1
  4106. const int il = tid%16; // 0...15
  4107. const uint8_t q = x[i].qs[il] >> (2*is);
  4108. dst_t * y = yy + i*QK_K + 16*is + il;
  4109. float dall = x[i].dm[0];
  4110. float dmin = x[i].dm[1];
  4111. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  4112. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  4113. #endif
  4114. }
  4115. template<typename dst_t>
  4116. static void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4117. const sycl::nd_item<3> &item_ct1) {
  4118. const int i = item_ct1.get_group(2);
  4119. const block_q3_K * x = (const block_q3_K *) vx;
  4120. #if QK_K == 256
  4121. const int r = item_ct1.get_local_id(2) / 4;
  4122. const int tid = r/2;
  4123. const int is0 = r%2;
  4124. const int l0 = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4);
  4125. const int n = tid / 4;
  4126. const int j = tid - 4*n;
  4127. uint8_t m = 1 << (4*n + j);
  4128. int is = 8*n + 2*j + is0;
  4129. int shift = 2*j;
  4130. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  4131. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  4132. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  4133. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  4134. float d_all = x[i].d;
  4135. float dl = d_all * (us - 32);
  4136. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  4137. const uint8_t * q = x[i].qs + 32*n;
  4138. const uint8_t * hm = x[i].hmask;
  4139. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  4140. #else
  4141. const int tid = item_ct1.get_local_id(2);
  4142. const int is = tid/16; // 0 or 1
  4143. const int il = tid%16; // 0...15
  4144. const int im = il/8; // 0...1
  4145. const int in = il%8; // 0...7
  4146. dst_t * y = yy + i*QK_K + 16*is + il;
  4147. const uint8_t q = x[i].qs[il] >> (2*is);
  4148. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  4149. const float d = (float)x[i].d;
  4150. if (is == 0) {
  4151. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  4152. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  4153. } else {
  4154. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  4155. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  4156. }
  4157. #endif
  4158. }
  4159. #if QK_K == 256
  4160. static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  4161. if (j < 4) {
  4162. d = q[j] & 63; m = q[j + 4] & 63;
  4163. } else {
  4164. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  4165. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  4166. }
  4167. }
  4168. #endif
  4169. template<typename dst_t>
  4170. static void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4171. const sycl::nd_item<3> &item_ct1) {
  4172. const block_q4_K * x = (const block_q4_K *) vx;
  4173. const int i = item_ct1.get_group(2);
  4174. #if QK_K == 256
  4175. // assume 32 threads
  4176. const int tid = item_ct1.get_local_id(2);
  4177. const int il = tid/8;
  4178. const int ir = tid%8;
  4179. const int is = 2*il;
  4180. const int n = 4;
  4181. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  4182. const float dall = x[i].dm[0];
  4183. const float dmin = x[i].dm[1];
  4184. const uint8_t * q = x[i].qs + 32*il + n*ir;
  4185. uint8_t sc, m;
  4186. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  4187. const float d1 = dall * sc; const float m1 = dmin * m;
  4188. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  4189. const float d2 = dall * sc; const float m2 = dmin * m;
  4190. for (int l = 0; l < n; ++l) {
  4191. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  4192. y[l +32] = d2 * (q[l] >> 4) - m2;
  4193. }
  4194. #else
  4195. const int tid = item_ct1.get_local_id(2);
  4196. const uint8_t * q = x[i].qs;
  4197. dst_t * y = yy + i*QK_K;
  4198. const float d = (float)x[i].dm[0];
  4199. const float m = (float)x[i].dm[1];
  4200. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  4201. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  4202. #endif
  4203. }
  4204. template<typename dst_t>
  4205. static void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4206. const sycl::nd_item<3> &item_ct1) {
  4207. const block_q5_K * x = (const block_q5_K *) vx;
  4208. const int i = item_ct1.get_group(2);
  4209. #if QK_K == 256
  4210. // assume 64 threads - this is very slightly better than the one below
  4211. const int tid = item_ct1.get_local_id(2);
  4212. const int il = tid/16; // il is in 0...3
  4213. const int ir = tid%16; // ir is in 0...15
  4214. const int is = 2*il; // is is in 0...6
  4215. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  4216. const float dall = x[i].dm[0];
  4217. const float dmin = x[i].dm[1];
  4218. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  4219. const uint8_t * qh = x[i].qh + 2*ir;
  4220. uint8_t sc, m;
  4221. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  4222. const float d1 = dall * sc; const float m1 = dmin * m;
  4223. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  4224. const float d2 = dall * sc; const float m2 = dmin * m;
  4225. uint8_t hm = 1 << (2*il);
  4226. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  4227. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  4228. hm <<= 1;
  4229. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  4230. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  4231. #else
  4232. const int tid = item_ct1.get_local_id(2);
  4233. const uint8_t q = x[i].qs[tid];
  4234. const int im = tid/8; // 0...3
  4235. const int in = tid%8; // 0...7
  4236. const int is = tid/16; // 0 or 1
  4237. const uint8_t h = x[i].qh[in] >> im;
  4238. const float d = x[i].d;
  4239. dst_t * y = yy + i*QK_K + tid;
  4240. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  4241. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  4242. #endif
  4243. }
  4244. template<typename dst_t>
  4245. static void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4246. const sycl::nd_item<3> &item_ct1) {
  4247. const block_q6_K * x = (const block_q6_K *) vx;
  4248. const int i = item_ct1.get_group(2);
  4249. #if QK_K == 256
  4250. // assume 64 threads - this is very slightly better than the one below
  4251. const int tid = item_ct1.get_local_id(2);
  4252. const int ip = tid/32; // ip is 0 or 1
  4253. const int il = tid - 32*ip; // 0...32
  4254. const int is = 8*ip + il/16;
  4255. dst_t * y = yy + i*QK_K + 128*ip + il;
  4256. const float d = x[i].d;
  4257. const uint8_t * ql = x[i].ql + 64*ip + il;
  4258. const uint8_t qh = x[i].qh[32*ip + il];
  4259. const int8_t * sc = x[i].scales + is;
  4260. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  4261. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  4262. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  4263. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  4264. #else
  4265. // assume 32 threads
  4266. const int tid = item_ct1.get_local_id(2);
  4267. const int ip = tid/16; // 0 or 1
  4268. const int il = tid - 16*ip; // 0...15
  4269. dst_t * y = yy + i*QK_K + 16*ip + il;
  4270. const float d = x[i].d;
  4271. const uint8_t ql = x[i].ql[16*ip + il];
  4272. const uint8_t qh = x[i].qh[il] >> (2*ip);
  4273. const int8_t * sc = x[i].scales;
  4274. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  4275. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  4276. #endif
  4277. }
  4278. static dpct::global_memory<const uint64_t, 1>
  4279. iq2xxs_grid(sycl::range<1>(256),
  4280. {
  4281. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919,
  4282. 0x0808080808082b08, 0x0808080808082b2b, 0x0808080808190819,
  4283. 0x0808080808191908, 0x08080808082b0808, 0x08080808082b082b,
  4284. 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  4285. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08,
  4286. 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  4287. 0x080808082b08082b, 0x080808082b082b2b, 0x080808082b2b082b,
  4288. 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  4289. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908,
  4290. 0x080808192b192b08, 0x0808082b08080808, 0x0808082b0808082b,
  4291. 0x0808082b082b082b, 0x0808082b2b08082b, 0x0808190808080819,
  4292. 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  4293. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b,
  4294. 0x0808190819082b08, 0x08081908192b0808, 0x080819082b080819,
  4295. 0x080819082b081908, 0x080819082b190808, 0x080819082b2b1908,
  4296. 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  4297. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19,
  4298. 0x080819192b080808, 0x080819192b190819, 0x0808192b08082b19,
  4299. 0x0808192b08190808, 0x0808192b19080808, 0x0808192b2b081908,
  4300. 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  4301. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08,
  4302. 0x08082b0819080819, 0x08082b0819081908, 0x08082b0819190808,
  4303. 0x08082b081919082b, 0x08082b082b082b08, 0x08082b1908081908,
  4304. 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  4305. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808,
  4306. 0x08190808082b0819, 0x0819080819080808, 0x08190808192b0808,
  4307. 0x081908082b081908, 0x081908082b190808, 0x081908082b191919,
  4308. 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  4309. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808,
  4310. 0x0819082b082b1908, 0x0819082b19081919, 0x0819190808080808,
  4311. 0x0819190808082b08, 0x08191908082b0808, 0x08191908082b1919,
  4312. 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  4313. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b,
  4314. 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  4315. 0x08192b0819080808, 0x08192b082b080819, 0x08192b1908080808,
  4316. 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  4317. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b,
  4318. 0x082b080819081908, 0x082b0808192b0819, 0x082b08082b080808,
  4319. 0x082b08082b08082b, 0x082b0819082b2b19, 0x082b081919082b08,
  4320. 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  4321. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808,
  4322. 0x082b19081919192b, 0x082b191908080808, 0x082b191919080819,
  4323. 0x082b1919192b1908, 0x082b192b2b190808, 0x082b2b0808082b08,
  4324. 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  4325. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808,
  4326. 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  4327. 0x1908080819080808, 0x1908080819082b08, 0x190808081919192b,
  4328. 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  4329. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808,
  4330. 0x19080819192b0819, 0x190808192b080808, 0x190808192b081919,
  4331. 0x1908082b08080819, 0x1908082b08190808, 0x1908082b19082b08,
  4332. 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  4333. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808,
  4334. 0x190819082b192b19, 0x190819190819082b, 0x19081919082b1908,
  4335. 0x1908192b08080808, 0x19082b0808080819, 0x19082b0808081908,
  4336. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  4337. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819,
  4338. 0x19082b192b08082b, 0x19082b2b19081919, 0x19082b2b2b190808,
  4339. 0x1919080808080808, 0x1919080808082b08, 0x1919080808190819,
  4340. 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  4341. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b,
  4342. 0x191908192b2b1908, 0x1919082b2b190819, 0x191919082b190808,
  4343. 0x191919082b19082b, 0x1919191908082b2b, 0x1919192b08080819,
  4344. 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  4345. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808,
  4346. 0x19192b2b08082b08, 0x192b080808081908, 0x192b080808190808,
  4347. 0x192b080819080808, 0x192b0808192b2b08, 0x192b081908080808,
  4348. 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  4349. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808,
  4350. 0x192b19190819082b, 0x192b19192b081908, 0x192b2b081908082b,
  4351. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808082b2b,
  4352. 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  4353. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819,
  4354. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808,
  4355. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808,
  4356. 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  4357. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808,
  4358. 0x2b082b080808082b, 0x2b082b1908081908, 0x2b082b2b08190819,
  4359. 0x2b19080808081908, 0x2b19080808190808, 0x2b190808082b1908,
  4360. 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  4361. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808,
  4362. 0x2b191908082b082b, 0x2b19190819081908, 0x2b19191919190819,
  4363. 0x2b192b082b080819, 0x2b192b19082b0808, 0x2b2b08080808082b,
  4364. 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  4365. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808,
  4366. 0x2b2b2b1908081908,
  4367. });
  4368. static dpct::global_memory<const uint64_t, 1>
  4369. iq2xs_grid(sycl::range<1>(512),
  4370. {
  4371. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919,
  4372. 0x0808080808082b08, 0x0808080808082b2b, 0x0808080808190819,
  4373. 0x0808080808191908, 0x080808080819192b, 0x0808080808192b19,
  4374. 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  4375. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908,
  4376. 0x080808081908192b, 0x0808080819082b19, 0x0808080819190808,
  4377. 0x080808081919082b, 0x0808080819191919, 0x0808080819192b08,
  4378. 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  4379. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08,
  4380. 0x080808082b190819, 0x080808082b191908, 0x080808082b192b19,
  4381. 0x080808082b2b0808, 0x0808081908080819, 0x0808081908081908,
  4382. 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  4383. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08,
  4384. 0x0808081908192b2b, 0x08080819082b0819, 0x08080819082b1908,
  4385. 0x0808081919080808, 0x080808191908082b, 0x0808081919081919,
  4386. 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  4387. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819,
  4388. 0x080808192b081908, 0x080808192b190808, 0x0808082b08080808,
  4389. 0x0808082b0808082b, 0x0808082b08081919, 0x0808082b08082b08,
  4390. 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  4391. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808,
  4392. 0x0808082b19191919, 0x0808082b2b080808, 0x0808082b2b082b2b,
  4393. 0x0808190808080819, 0x0808190808081908, 0x080819080808192b,
  4394. 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  4395. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819,
  4396. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b,
  4397. 0x0808190819081919, 0x0808190819082b08, 0x0808190819190819,
  4398. 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  4399. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  4400. 0x0808191908080808, 0x080819190808082b, 0x0808191908081919,
  4401. 0x0808191908082b08, 0x0808191908190819, 0x0808191908191908,
  4402. 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  4403. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808,
  4404. 0x0808192b08080819, 0x0808192b08081908, 0x0808192b08190808,
  4405. 0x0808192b082b192b, 0x0808192b19080808, 0x0808192b1908082b,
  4406. 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  4407. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b,
  4408. 0x08082b0808190819, 0x08082b0808191908, 0x08082b08082b0808,
  4409. 0x08082b08082b1919, 0x08082b0819080819, 0x08082b0819081908,
  4410. 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  4411. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819,
  4412. 0x08082b1908081908, 0x08082b1908190808, 0x08082b1919080808,
  4413. 0x08082b192b080819, 0x08082b192b082b19, 0x08082b2b08080808,
  4414. 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  4415. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908,
  4416. 0x081908080808192b, 0x0819080808082b19, 0x0819080808190808,
  4417. 0x081908080819082b, 0x0819080808191919, 0x0819080808192b08,
  4418. 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  4419. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08,
  4420. 0x0819080819190819, 0x0819080819191908, 0x08190808192b0808,
  4421. 0x08190808192b2b2b, 0x081908082b080819, 0x081908082b081908,
  4422. 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  4423. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819,
  4424. 0x0819081908191908, 0x08190819082b0808, 0x0819081919080819,
  4425. 0x0819081919081908, 0x0819081919190808, 0x081908192b080808,
  4426. 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  4427. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808,
  4428. 0x0819082b19080808, 0x0819082b192b0808, 0x0819190808080808,
  4429. 0x081919080808082b, 0x0819190808081919, 0x0819190808082b08,
  4430. 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  4431. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19,
  4432. 0x0819190819190808, 0x08191908192b1908, 0x081919082b080808,
  4433. 0x0819191908080819, 0x0819191908081908, 0x0819191908190808,
  4434. 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  4435. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908,
  4436. 0x08192b0808190808, 0x08192b080819082b, 0x08192b0819080808,
  4437. 0x08192b0819191908, 0x08192b082b08192b, 0x08192b1908080808,
  4438. 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  4439. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b,
  4440. 0x082b080808081919, 0x082b080808082b08, 0x082b080808082b2b,
  4441. 0x082b080808190819, 0x082b080808191908, 0x082b0808082b0808,
  4442. 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  4443. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819,
  4444. 0x082b081908081908, 0x082b081908190808, 0x082b081919080808,
  4445. 0x082b081919082b08, 0x082b0819192b1919, 0x082b082b08080808,
  4446. 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  4447. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808,
  4448. 0x082b1908082b2b19, 0x082b190819080808, 0x082b191908080808,
  4449. 0x082b191919080819, 0x082b19191919082b, 0x082b19192b192b19,
  4450. 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  4451. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b,
  4452. 0x082b2b08082b0808, 0x082b2b0819191919, 0x082b2b082b082b08,
  4453. 0x082b2b082b2b082b, 0x082b2b19192b2b08, 0x082b2b192b190808,
  4454. 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  4455. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819,
  4456. 0x1908080808081908, 0x190808080808192b, 0x1908080808082b19,
  4457. 0x1908080808190808, 0x190808080819082b, 0x1908080808191919,
  4458. 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  4459. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919,
  4460. 0x1908080819082b08, 0x1908080819082b2b, 0x1908080819190819,
  4461. 0x1908080819191908, 0x19080808192b0808, 0x19080808192b1919,
  4462. 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  4463. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919,
  4464. 0x1908081908082b08, 0x1908081908190819, 0x1908081908191908,
  4465. 0x19080819082b0808, 0x1908081919080819, 0x1908081919081908,
  4466. 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  4467. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908,
  4468. 0x1908082b08190808, 0x1908082b0819082b, 0x1908082b082b2b19,
  4469. 0x1908082b19080808, 0x1908190808080808, 0x190819080808082b,
  4470. 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  4471. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808,
  4472. 0x1908190819080819, 0x1908190819081908, 0x1908190819190808,
  4473. 0x190819082b080808, 0x190819082b191908, 0x1908191908080819,
  4474. 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  4475. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808,
  4476. 0x1908192b08082b2b, 0x1908192b19081908, 0x1908192b19190808,
  4477. 0x19082b0808080819, 0x19082b0808081908, 0x19082b0808190808,
  4478. 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  4479. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819,
  4480. 0x19082b1919081908, 0x19082b1919190808, 0x19082b19192b2b19,
  4481. 0x19082b2b08081908, 0x1919080808080808, 0x191908080808082b,
  4482. 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  4483. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08,
  4484. 0x1919080819080819, 0x1919080819081908, 0x1919080819190808,
  4485. 0x191908082b080808, 0x1919081908080819, 0x1919081908081908,
  4486. 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  4487. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908,
  4488. 0x1919082b2b2b2b2b, 0x1919190808080819, 0x1919190808081908,
  4489. 0x1919190808190808, 0x19191908082b0819, 0x1919190819080808,
  4490. 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  4491. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808,
  4492. 0x191919192b082b08, 0x1919192b082b0819, 0x1919192b192b2b08,
  4493. 0x1919192b2b2b0819, 0x19192b0808080808, 0x19192b0808191908,
  4494. 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  4495. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b,
  4496. 0x19192b2b2b081919, 0x192b080808080819, 0x192b080808081908,
  4497. 0x192b080808190808, 0x192b080819080808, 0x192b080819191908,
  4498. 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  4499. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b,
  4500. 0x192b082b2b19082b, 0x192b190808080808, 0x192b19080819192b,
  4501. 0x192b191908190808, 0x192b191919080808, 0x192b191919081919,
  4502. 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  4503. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908,
  4504. 0x192b2b2b192b082b, 0x2b08080808080808, 0x2b0808080808082b,
  4505. 0x2b08080808081919, 0x2b08080808082b08, 0x2b08080808190819,
  4506. 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  4507. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808,
  4508. 0x2b0808082b080808, 0x2b0808082b08082b, 0x2b0808082b2b2b08,
  4509. 0x2b0808082b2b2b2b, 0x2b08081908080819, 0x2b08081908081908,
  4510. 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  4511. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808,
  4512. 0x2b08082b082b0808, 0x2b08082b2b080808, 0x2b08082b2b08082b,
  4513. 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08, 0x2b08190808080819,
  4514. 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  4515. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808,
  4516. 0x2b0819082b082b19, 0x2b08191908080808, 0x2b08191919081908,
  4517. 0x2b0819192b2b1919, 0x2b08192b08192b08, 0x2b08192b192b2b2b,
  4518. 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  4519. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b,
  4520. 0x2b082b082b2b2b08, 0x2b082b190808192b, 0x2b082b2b082b082b,
  4521. 0x2b082b2b2b080808, 0x2b082b2b2b082b08, 0x2b082b2b2b19192b,
  4522. 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  4523. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b,
  4524. 0x2b1908082b081908, 0x2b19081908080808, 0x2b190819082b082b,
  4525. 0x2b190819192b1908, 0x2b19082b1919192b, 0x2b19082b2b082b19,
  4526. 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  4527. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19,
  4528. 0x2b1919192b190808, 0x2b1919192b19082b, 0x2b19192b19080819,
  4529. 0x2b192b0819190819, 0x2b192b082b2b192b, 0x2b192b1919082b19,
  4530. 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  4531. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b,
  4532. 0x2b2b0808082b0808, 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808,
  4533. 0x2b2b081919190819, 0x2b2b081919192b19, 0x2b2b08192b2b192b,
  4534. 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  4535. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808,
  4536. 0x2b2b190819080808, 0x2b2b19082b191919, 0x2b2b192b192b1919,
  4537. 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b, 0x2b2b2b08082b0808,
  4538. 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  4539. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908,
  4540. 0x2b2b2b192b08192b, 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b,
  4541. 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  4542. });
  4543. static dpct::global_memory<const uint32_t, 1> iq3xxs_grid(
  4544. sycl::range<1>(256),
  4545. {
  4546. 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e,
  4547. 0x04041404, 0x04041414, 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c,
  4548. 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14, 0x040c140c, 0x040c142c,
  4549. 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
  4550. 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c,
  4551. 0x04141c1c, 0x04141c3e, 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c,
  4552. 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c, 0x041c3e04, 0x04240c1c,
  4553. 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
  4554. 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04,
  4555. 0x043e0c24, 0x043e0c34, 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c,
  4556. 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c, 0x0c041c04, 0x0c041c14,
  4557. 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
  4558. 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14,
  4559. 0x0c14140c, 0x0c141c04, 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404,
  4560. 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c, 0x0c24042c, 0x0c242c04,
  4561. 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
  4562. 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404,
  4563. 0x14041414, 0x14041434, 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c,
  4564. 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c, 0x140c1c04, 0x140c341c,
  4565. 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
  4566. 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c,
  4567. 0x141c0c04, 0x141c0c24, 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c,
  4568. 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24, 0x143e040c, 0x143e041c,
  4569. 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
  4570. 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414,
  4571. 0x1c0c1404, 0x1c0c1c0c, 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c,
  4572. 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14, 0x1c1c0c0c, 0x1c1c1c1c,
  4573. 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
  4574. 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404,
  4575. 0x24040424, 0x24040c3e, 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e,
  4576. 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404, 0x24143404, 0x24143434,
  4577. 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
  4578. 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04,
  4579. 0x2c040c14, 0x2c04240c, 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434,
  4580. 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14, 0x2c1c0414, 0x2c1c2c1c,
  4581. 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
  4582. 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434,
  4583. 0x34043424, 0x340c140c, 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04,
  4584. 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14, 0x34341c1c, 0x343e041c,
  4585. 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
  4586. 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14,
  4587. 0x3e1c0404, 0x3e1c0c2c, 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c,
  4588. 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
  4589. });
  4590. static dpct::global_memory<const uint8_t, 1> ksigns_iq2xs(
  4591. sycl::range<1>(128),
  4592. {
  4593. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12,
  4594. 141, 142, 15, 144, 17, 18, 147, 20, 149, 150, 23, 24, 153,
  4595. 154, 27, 156, 29, 30, 159, 160, 33, 34, 163, 36, 165, 166,
  4596. 39, 40, 169, 170, 43, 172, 45, 46, 175, 48, 177, 178, 51,
  4597. 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63, 192,
  4598. 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77,
  4599. 78, 207, 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90,
  4600. 219, 92, 221, 222, 95, 96, 225, 226, 99, 228, 101, 102, 231,
  4601. 232, 105, 106, 235, 108, 237, 238, 111, 240, 113, 114, 243, 116,
  4602. 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  4603. });
  4604. static dpct::global_memory<const uint64_t, 1>
  4605. ksigns64(sycl::range<1>(128),
  4606. {
  4607. 0x0000000000000000, 0xff000000000000ff, 0xff0000000000ff00,
  4608. 0x000000000000ffff, 0xff00000000ff0000, 0x0000000000ff00ff,
  4609. 0x0000000000ffff00, 0xff00000000ffffff, 0xff000000ff000000,
  4610. 0x00000000ff0000ff, 0x00000000ff00ff00, 0xff000000ff00ffff,
  4611. 0x00000000ffff0000, 0xff000000ffff00ff, 0xff000000ffffff00,
  4612. 0x00000000ffffffff, 0xff0000ff00000000, 0x000000ff000000ff,
  4613. 0x000000ff0000ff00, 0xff0000ff0000ffff, 0x000000ff00ff0000,
  4614. 0xff0000ff00ff00ff, 0xff0000ff00ffff00, 0x000000ff00ffffff,
  4615. 0x000000ffff000000, 0xff0000ffff0000ff, 0xff0000ffff00ff00,
  4616. 0x000000ffff00ffff, 0xff0000ffffff0000, 0x000000ffffff00ff,
  4617. 0x000000ffffffff00, 0xff0000ffffffffff, 0xff00ff0000000000,
  4618. 0x0000ff00000000ff, 0x0000ff000000ff00, 0xff00ff000000ffff,
  4619. 0x0000ff0000ff0000, 0xff00ff0000ff00ff, 0xff00ff0000ffff00,
  4620. 0x0000ff0000ffffff, 0x0000ff00ff000000, 0xff00ff00ff0000ff,
  4621. 0xff00ff00ff00ff00, 0x0000ff00ff00ffff, 0xff00ff00ffff0000,
  4622. 0x0000ff00ffff00ff, 0x0000ff00ffffff00, 0xff00ff00ffffffff,
  4623. 0x0000ffff00000000, 0xff00ffff000000ff, 0xff00ffff0000ff00,
  4624. 0x0000ffff0000ffff, 0xff00ffff00ff0000, 0x0000ffff00ff00ff,
  4625. 0x0000ffff00ffff00, 0xff00ffff00ffffff, 0xff00ffffff000000,
  4626. 0x0000ffffff0000ff, 0x0000ffffff00ff00, 0xff00ffffff00ffff,
  4627. 0x0000ffffffff0000, 0xff00ffffffff00ff, 0xff00ffffffffff00,
  4628. 0x0000ffffffffffff, 0xffff000000000000, 0x00ff0000000000ff,
  4629. 0x00ff00000000ff00, 0xffff00000000ffff, 0x00ff000000ff0000,
  4630. 0xffff000000ff00ff, 0xffff000000ffff00, 0x00ff000000ffffff,
  4631. 0x00ff0000ff000000, 0xffff0000ff0000ff, 0xffff0000ff00ff00,
  4632. 0x00ff0000ff00ffff, 0xffff0000ffff0000, 0x00ff0000ffff00ff,
  4633. 0x00ff0000ffffff00, 0xffff0000ffffffff, 0x00ff00ff00000000,
  4634. 0xffff00ff000000ff, 0xffff00ff0000ff00, 0x00ff00ff0000ffff,
  4635. 0xffff00ff00ff0000, 0x00ff00ff00ff00ff, 0x00ff00ff00ffff00,
  4636. 0xffff00ff00ffffff, 0xffff00ffff000000, 0x00ff00ffff0000ff,
  4637. 0x00ff00ffff00ff00, 0xffff00ffff00ffff, 0x00ff00ffffff0000,
  4638. 0xffff00ffffff00ff, 0xffff00ffffffff00, 0x00ff00ffffffffff,
  4639. 0x00ffff0000000000, 0xffffff00000000ff, 0xffffff000000ff00,
  4640. 0x00ffff000000ffff, 0xffffff0000ff0000, 0x00ffff0000ff00ff,
  4641. 0x00ffff0000ffff00, 0xffffff0000ffffff, 0xffffff00ff000000,
  4642. 0x00ffff00ff0000ff, 0x00ffff00ff00ff00, 0xffffff00ff00ffff,
  4643. 0x00ffff00ffff0000, 0xffffff00ffff00ff, 0xffffff00ffffff00,
  4644. 0x00ffff00ffffffff, 0xffffffff00000000, 0x00ffffff000000ff,
  4645. 0x00ffffff0000ff00, 0xffffffff0000ffff, 0x00ffffff00ff0000,
  4646. 0xffffffff00ff00ff, 0xffffffff00ffff00, 0x00ffffff00ffffff,
  4647. 0x00ffffffff000000, 0xffffffffff0000ff, 0xffffffffff00ff00,
  4648. 0x00ffffffff00ffff, 0xffffffffffff0000, 0x00ffffffffff00ff,
  4649. 0x00ffffffffffff00, 0xffffffffffffffff,
  4650. });
  4651. //#endif
  4652. static dpct::global_memory<const uint8_t, 1>
  4653. kmask_iq2xs(sycl::range<1>(8), {1, 2, 4, 8, 16, 32, 64, 128});
  4654. template<typename dst_t>
  4655. static void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4656. const sycl::nd_item<3> &item_ct1,
  4657. const uint64_t *iq2xxs_grid_ptr,
  4658. const uint8_t *ksigns_iq2xs_ptr,
  4659. const uint8_t *kmask_iq2xs_ptr) {
  4660. const int i = item_ct1.get_group(2);
  4661. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  4662. const int tid = item_ct1.get_local_id(2);
  4663. #if QK_K == 256
  4664. const int il = tid/8; // 0...3
  4665. const int ib = tid%8; // 0...7
  4666. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4667. const uint16_t * q2 = x[i].qs + 4*ib;
  4668. const uint8_t * aux8 = (const uint8_t *)q2;
  4669. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid_ptr + aux8[il]);
  4670. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  4671. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  4672. const uint8_t signs = ksigns_iq2xs_ptr[(aux32 >> 7*il) & 127];
  4673. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs_ptr[j] ? -1.f : 1.f);
  4674. #else
  4675. assert(false);
  4676. #endif
  4677. }
  4678. template<typename dst_t>
  4679. static void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4680. const sycl::nd_item<3> &item_ct1,
  4681. const uint64_t *iq2xs_grid,
  4682. const uint8_t *ksigns_iq2xs,
  4683. const uint8_t *kmask_iq2xs) {
  4684. const int i = item_ct1.get_group(2);
  4685. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  4686. const int tid = item_ct1.get_local_id(2);
  4687. #if QK_K == 256
  4688. const int il = tid/8; // 0...3
  4689. const int ib = tid%8; // 0...7
  4690. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4691. const uint16_t * q2 = x[i].qs + 4*ib;
  4692. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  4693. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  4694. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  4695. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  4696. #else
  4697. assert(false);
  4698. #endif
  4699. }
  4700. template<typename dst_t>
  4701. static void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4702. const sycl::nd_item<3> &item_ct1,
  4703. const uint32_t *iq3xxs_grid,
  4704. const uint8_t *ksigns_iq2xs,
  4705. const uint8_t *kmask_iq2xs) {
  4706. const int i = item_ct1.get_group(2);
  4707. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  4708. const int tid = item_ct1.get_local_id(2);
  4709. #if QK_K == 256
  4710. const int il = tid/8; // 0...3
  4711. const int ib = tid%8; // 0...7
  4712. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4713. const uint8_t * q3 = x[i].qs + 8*ib;
  4714. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  4715. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  4716. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  4717. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4718. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  4719. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  4720. for (int j = 0; j < 4; ++j) {
  4721. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4722. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4723. }
  4724. #else
  4725. assert(false);
  4726. #endif
  4727. }
  4728. /*
  4729. DPCT1110:4: The total declared local variable size in device function
  4730. dequantize_mul_mat_vec_q2_k exceeds 128 bytes and may cause high register
  4731. pressure. Consult with your hardware vendor to find the total register size
  4732. available and adjust the code, or use smaller sub-group size to avoid high
  4733. register pressure.
  4734. */
  4735. static void dequantize_mul_mat_vec_q2_k(const void *__restrict__ vx,
  4736. const float *__restrict__ yy,
  4737. float *__restrict__ dst,
  4738. const int ncols, int nrows,
  4739. const sycl::nd_item<3> &item_ct1) {
  4740. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4741. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4742. item_ct1.get_local_id(1);
  4743. if (row > nrows) return;
  4744. const int num_blocks_per_row = ncols / QK_K;
  4745. const int ib0 = row*num_blocks_per_row;
  4746. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  4747. float tmp = 0; // partial sum for thread in warp
  4748. #if QK_K == 256
  4749. const int tid =
  4750. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  4751. const int ix =
  4752. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4753. const int step = 16/K_QUANTS_PER_ITERATION;
  4754. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4755. const int in = tid - step*im; // 0...15 or 0...7
  4756. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  4757. const int q_offset = 32*im + l0;
  4758. const int s_offset = 8*im;
  4759. const int y_offset = 128*im + l0;
  4760. uint32_t aux[4];
  4761. const uint8_t * d = (const uint8_t *)aux;
  4762. const uint8_t * m = (const uint8_t *)(aux + 2);
  4763. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4764. const float * y = yy + i * QK_K + y_offset;
  4765. const uint8_t * q = x[i].qs + q_offset;
  4766. const float dall = x[i].dm[0];
  4767. const float dmin = x[i].dm[1];
  4768. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  4769. aux[0] = a[0] & 0x0f0f0f0f;
  4770. aux[1] = a[1] & 0x0f0f0f0f;
  4771. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  4772. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  4773. float sum1 = 0, sum2 = 0;
  4774. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4775. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  4776. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  4777. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  4778. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  4779. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  4780. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  4781. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  4782. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  4783. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  4784. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  4785. }
  4786. tmp += dall * sum1 - dmin * sum2;
  4787. }
  4788. #else
  4789. const int tid = item_ct1.get_local_id(2) /
  4790. (2 * K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  4791. const int ix = item_ct1.get_local_id(2) %
  4792. (2 * K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  4793. const int offset = tid * K_QUANTS_PER_ITERATION;
  4794. uint32_t uaux[2];
  4795. const uint8_t * d = (const uint8_t *)uaux;
  4796. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4797. const float * y = yy + i * QK_K + offset;
  4798. const uint8_t * q = x[i].qs + offset;
  4799. const uint32_t * s = (const uint32_t *)x[i].scales;
  4800. uaux[0] = s[0] & 0x0f0f0f0f;
  4801. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  4802. const sycl::float2 dall =
  4803. x[i].dm.convert<float, sycl::rounding_mode::automatic>();
  4804. float sum1 = 0, sum2 = 0;
  4805. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4806. const uint8_t ql = q[l];
  4807. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  4808. + y[l+16] * d[1] * ((ql >> 2) & 3)
  4809. + y[l+32] * d[2] * ((ql >> 4) & 3)
  4810. + y[l+48] * d[3] * ((ql >> 6) & 3);
  4811. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  4812. }
  4813. tmp += dall.x() * sum1 - dall.y() * sum2;
  4814. }
  4815. #endif
  4816. // sum up partial sums and write back result
  4817. #pragma unroll
  4818. for (int mask = 16; mask > 0; mask >>= 1) {
  4819. tmp +=
  4820. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4821. }
  4822. if (item_ct1.get_local_id(2) == 0) {
  4823. dst[row] = tmp;
  4824. }
  4825. }
  4826. /*
  4827. DPCT1110:5: The total declared local variable size in device function
  4828. dequantize_mul_mat_vec_q3_k exceeds 128 bytes and may cause high register
  4829. pressure. Consult with your hardware vendor to find the total register size
  4830. available and adjust the code, or use smaller sub-group size to avoid high
  4831. register pressure.
  4832. */
  4833. static void dequantize_mul_mat_vec_q3_k(const void *__restrict__ vx,
  4834. const float *__restrict__ yy,
  4835. float *__restrict__ dst,
  4836. const int ncols, int nrows,
  4837. const sycl::nd_item<3> &item_ct1) {
  4838. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4839. item_ct1.get_local_id(1);
  4840. if (row > nrows) return;
  4841. const int num_blocks_per_row = ncols / QK_K;
  4842. const int ib0 = row*num_blocks_per_row;
  4843. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  4844. float tmp = 0; // partial sum for thread in warp
  4845. #if QK_K == 256
  4846. const uint16_t kmask1 = 0x0303;
  4847. const uint16_t kmask2 = 0x0f0f;
  4848. const int tid =
  4849. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4850. const int ix =
  4851. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4852. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  4853. const int step = 16/K_QUANTS_PER_ITERATION;
  4854. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4855. const int in = tid - step*im; // 0....15 or 0...7
  4856. const uint8_t m = 1 << (4*im);
  4857. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  4858. const int q_offset = 32*im + l0;
  4859. const int y_offset = 128*im + l0;
  4860. uint16_t utmp[4];
  4861. const int8_t * s = (const int8_t *)utmp;
  4862. const uint16_t s_shift = 4*im;
  4863. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4864. const float * y = yy + i * QK_K + y_offset;
  4865. const uint8_t * q = x[i].qs + q_offset;
  4866. const uint8_t * h = x[i].hmask + l0;
  4867. const uint16_t * a = (const uint16_t *)x[i].scales;
  4868. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  4869. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  4870. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  4871. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  4872. const float d = x[i].d;
  4873. float sum = 0;
  4874. for (int l = 0; l < n; ++l) {
  4875. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  4876. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  4877. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  4878. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  4879. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  4880. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  4881. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  4882. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  4883. }
  4884. tmp += d * sum;
  4885. }
  4886. #else
  4887. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  4888. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  4889. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  4890. const int in = offset/8; // 0 or 1
  4891. const int im = offset%8; // 0...7
  4892. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4893. const float * y = yy + i * QK_K + offset;
  4894. const uint8_t * q = x[i].qs + offset;
  4895. const uint8_t * s = x[i].scales;
  4896. const float dall = (float)x[i].d;
  4897. float sum = 0;
  4898. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4899. const uint8_t hl = x[i].hmask[im+l] >> in;
  4900. const uint8_t ql = q[l];
  4901. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  4902. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  4903. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  4904. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  4905. }
  4906. tmp += sum;
  4907. }
  4908. #endif
  4909. // sum up partial sums and write back result
  4910. #pragma unroll
  4911. for (int mask = 16; mask > 0; mask >>= 1) {
  4912. tmp +=
  4913. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4914. }
  4915. if (item_ct1.get_local_id(2) == 0) {
  4916. dst[row] = tmp;
  4917. }
  4918. }
  4919. /*
  4920. DPCT1110:6: The total declared local variable size in device function
  4921. dequantize_mul_mat_vec_q4_k exceeds 128 bytes and may cause high register
  4922. pressure. Consult with your hardware vendor to find the total register size
  4923. available and adjust the code, or use smaller sub-group size to avoid high
  4924. register pressure.
  4925. */
  4926. static void dequantize_mul_mat_vec_q4_k(const void *__restrict__ vx,
  4927. const float *__restrict__ yy,
  4928. float *__restrict__ dst,
  4929. const int ncols, int nrows,
  4930. const sycl::nd_item<3> &item_ct1) {
  4931. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4932. item_ct1.get_local_id(1);
  4933. if (row > nrows) return;
  4934. const int num_blocks_per_row = ncols / QK_K;
  4935. const int ib0 = row*num_blocks_per_row;
  4936. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  4937. #if QK_K == 256
  4938. const uint16_t kmask1 = 0x3f3f;
  4939. const uint16_t kmask2 = 0x0f0f;
  4940. const uint16_t kmask3 = 0xc0c0;
  4941. const int tid =
  4942. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4943. const int ix =
  4944. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4945. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  4946. const int il = tid/step; // 0...3
  4947. const int ir = tid - step*il; // 0...7 or 0...3
  4948. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  4949. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4950. const int in = il%2;
  4951. const int l0 = n*(2*ir + in);
  4952. const int q_offset = 32*im + l0;
  4953. const int y_offset = 64*im + l0;
  4954. uint16_t aux[4];
  4955. const uint8_t * sc = (const uint8_t *)aux;
  4956. #if K_QUANTS_PER_ITERATION == 2
  4957. uint32_t q32[4];
  4958. const uint8_t * q4 = (const uint8_t *)q32;
  4959. #else
  4960. uint16_t q16[4];
  4961. const uint8_t * q4 = (const uint8_t *)q16;
  4962. #endif
  4963. float tmp = 0; // partial sum for thread in warp
  4964. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4965. const float * y1 = yy + i*QK_K + y_offset;
  4966. const float * y2 = y1 + 128;
  4967. const float dall = x[i].dm[0];
  4968. const float dmin = x[i].dm[1];
  4969. const uint16_t * a = (const uint16_t *)x[i].scales;
  4970. aux[0] = a[im+0] & kmask1;
  4971. aux[1] = a[im+2] & kmask1;
  4972. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4973. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4974. #if K_QUANTS_PER_ITERATION == 2
  4975. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  4976. const uint32_t * q2 = q1 + 16;
  4977. q32[0] = q1[0] & 0x0f0f0f0f;
  4978. q32[1] = q1[0] & 0xf0f0f0f0;
  4979. q32[2] = q2[0] & 0x0f0f0f0f;
  4980. q32[3] = q2[0] & 0xf0f0f0f0;
  4981. sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
  4982. float smin = 0;
  4983. for (int l = 0; l < 4; ++l) {
  4984. s.x() += y1[l] * q4[l + 0]; s.y() += y1[l + 32] * q4[l + 4];
  4985. s.z() += y2[l] * q4[l + 8]; s.w() += y2[l + 32] * q4[l + 12];
  4986. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4987. }
  4988. tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f / 16.f +
  4989. s.z() * sc[4] + s.w() * sc[5] * 1.f / 16.f) -
  4990. dmin * smin;
  4991. #else
  4992. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  4993. const uint16_t * q2 = q1 + 32;
  4994. q16[0] = q1[0] & 0x0f0f;
  4995. q16[1] = q1[0] & 0xf0f0;
  4996. q16[2] = q2[0] & 0x0f0f;
  4997. q16[3] = q2[0] & 0xf0f0;
  4998. float4 s = {0.f, 0.f, 0.f, 0.f};
  4999. float smin = 0;
  5000. for (int l = 0; l < 2; ++l) {
  5001. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  5002. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  5003. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  5004. }
  5005. 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;
  5006. #endif
  5007. }
  5008. #else
  5009. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15
  5010. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION);
  5011. const int step = tid * K_QUANTS_PER_ITERATION;
  5012. uint16_t aux16[2];
  5013. const uint8_t * s = (const uint8_t *)aux16;
  5014. float tmp = 0;
  5015. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  5016. const uint8_t * q = x[i].qs + step;
  5017. const float * y = yy + i*QK_K + step;
  5018. const uint16_t * a = (const uint16_t *)x[i].scales;
  5019. aux16[0] = a[0] & 0x0f0f;
  5020. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5021. const float d = (float)x[i].dm[0];
  5022. const float m = (float)x[i].dm[1];
  5023. float sum = 0.f;
  5024. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  5025. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  5026. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  5027. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  5028. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  5029. }
  5030. tmp += sum;
  5031. }
  5032. #endif
  5033. // sum up partial sums and write back result
  5034. #pragma unroll
  5035. for (int mask = 16; mask > 0; mask >>= 1) {
  5036. tmp +=
  5037. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  5038. }
  5039. if (tid == 0) {
  5040. dst[row] = tmp;
  5041. }
  5042. }
  5043. /*
  5044. DPCT1110:7: The total declared local variable size in device function
  5045. dequantize_mul_mat_vec_q5_k exceeds 128 bytes and may cause high register
  5046. pressure. Consult with your hardware vendor to find the total register size
  5047. available and adjust the code, or use smaller sub-group size to avoid high
  5048. register pressure.
  5049. */
  5050. static void dequantize_mul_mat_vec_q5_k(const void *__restrict__ vx,
  5051. const float *__restrict__ yy,
  5052. float *__restrict__ dst,
  5053. const int ncols,
  5054. const sycl::nd_item<3> &item_ct1) {
  5055. const int row = item_ct1.get_group(2);
  5056. const int num_blocks_per_row = ncols / QK_K;
  5057. const int ib0 = row*num_blocks_per_row;
  5058. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  5059. float tmp = 0; // partial sum for thread in warp
  5060. #if QK_K == 256
  5061. const uint16_t kmask1 = 0x3f3f;
  5062. const uint16_t kmask2 = 0x0f0f;
  5063. const uint16_t kmask3 = 0xc0c0;
  5064. const int tid = item_ct1.get_local_id(2) / 2; // 0...15
  5065. const int ix = item_ct1.get_local_id(2) % 2;
  5066. const int il = tid/4; // 0...3
  5067. const int ir = tid - 4*il;// 0...3
  5068. const int n = 2;
  5069. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  5070. const int in = il%2;
  5071. const int l0 = n*(2*ir + in);
  5072. const int q_offset = 32*im + l0;
  5073. const int y_offset = 64*im + l0;
  5074. const uint8_t hm1 = 1 << (2*im);
  5075. const uint8_t hm2 = hm1 << 4;
  5076. uint16_t aux[4];
  5077. const uint8_t * sc = (const uint8_t *)aux;
  5078. uint16_t q16[8];
  5079. const uint8_t * q4 = (const uint8_t *)q16;
  5080. for (int i = ix; i < num_blocks_per_row; i += 2) {
  5081. const uint8_t * ql1 = x[i].qs + q_offset;
  5082. const uint8_t * qh = x[i].qh + l0;
  5083. const float * y1 = yy + i*QK_K + y_offset;
  5084. const float * y2 = y1 + 128;
  5085. const float dall = x[i].dm[0];
  5086. const float dmin = x[i].dm[1];
  5087. const uint16_t * a = (const uint16_t *)x[i].scales;
  5088. aux[0] = a[im+0] & kmask1;
  5089. aux[1] = a[im+2] & kmask1;
  5090. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  5091. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  5092. sycl::float4 sum = {0.f, 0.f, 0.f, 0.f};
  5093. float smin = 0;
  5094. const uint16_t * q1 = (const uint16_t *)ql1;
  5095. const uint16_t * q2 = q1 + 32;
  5096. q16[0] = q1[0] & 0x0f0f;
  5097. q16[1] = q1[8] & 0x0f0f;
  5098. q16[2] = (q1[0] >> 4) & 0x0f0f;
  5099. q16[3] = (q1[8] >> 4) & 0x0f0f;
  5100. q16[4] = q2[0] & 0x0f0f;
  5101. q16[5] = q2[8] & 0x0f0f;
  5102. q16[6] = (q2[0] >> 4) & 0x0f0f;
  5103. q16[7] = (q2[8] >> 4) & 0x0f0f;
  5104. for (int l = 0; l < n; ++l) {
  5105. sum.x() +=
  5106. y1[l + 0] * (q4[l + 0] + (qh[l + 0] & (hm1 << 0) ? 16 : 0)) +
  5107. y1[l + 16] * (q4[l + 2] + (qh[l + 16] & (hm1 << 0) ? 16 : 0));
  5108. sum.y() +=
  5109. y1[l + 32] * (q4[l + 4] + (qh[l + 0] & (hm1 << 1) ? 16 : 0)) +
  5110. y1[l + 48] * (q4[l + 6] + (qh[l + 16] & (hm1 << 1) ? 16 : 0));
  5111. sum.z() +=
  5112. y2[l + 0] * (q4[l + 8] + (qh[l + 0] & (hm2 << 0) ? 16 : 0)) +
  5113. y2[l + 16] * (q4[l + 10] + (qh[l + 16] & (hm2 << 0) ? 16 : 0));
  5114. sum.w() +=
  5115. y2[l + 32] * (q4[l + 12] + (qh[l + 0] & (hm2 << 1) ? 16 : 0)) +
  5116. y2[l + 48] * (q4[l + 14] + (qh[l + 16] & (hm2 << 1) ? 16 : 0));
  5117. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  5118. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  5119. }
  5120. tmp += dall * (sum.x() * sc[0] + sum.y() * sc[1] + sum.z() * sc[4] +
  5121. sum.w() * sc[5]) -
  5122. dmin * smin;
  5123. }
  5124. #else
  5125. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15
  5126. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION);
  5127. const int step = tid * K_QUANTS_PER_ITERATION;
  5128. const int im = step/8;
  5129. const int in = step%8;
  5130. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  5131. const uint8_t * q = x[i].qs + step;
  5132. const int8_t * s = x[i].scales;
  5133. const float * y = yy + i*QK_K + step;
  5134. const float d = x[i].d;
  5135. float sum = 0.f;
  5136. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  5137. const uint8_t h = x[i].qh[in+j] >> im;
  5138. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  5139. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  5140. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  5141. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  5142. }
  5143. tmp += sum;
  5144. }
  5145. #endif
  5146. // sum up partial sums and write back result
  5147. #pragma unroll
  5148. for (int mask = 16; mask > 0; mask >>= 1) {
  5149. tmp +=
  5150. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  5151. }
  5152. if (item_ct1.get_local_id(2) == 0) {
  5153. dst[row] = tmp;
  5154. }
  5155. }
  5156. static void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows,
  5157. const sycl::nd_item<3> &item_ct1) {
  5158. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  5159. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  5160. item_ct1.get_local_id(1);
  5161. if (row > nrows) return;
  5162. const int num_blocks_per_row = ncols / QK_K;
  5163. const int ib0 = row*num_blocks_per_row;
  5164. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  5165. #if QK_K == 256
  5166. const int tid =
  5167. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  5168. const int ix =
  5169. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0, 1
  5170. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  5171. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  5172. const int in = tid - step*im; // 0...15 or 0...7
  5173. #if K_QUANTS_PER_ITERATION == 1
  5174. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  5175. const int is = 0;
  5176. #else
  5177. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  5178. const int is = in / 4;
  5179. #endif
  5180. const int ql_offset = 64*im + l0;
  5181. const int qh_offset = 32*im + l0;
  5182. const int s_offset = 8*im + is;
  5183. const int y_offset = 128*im + l0;
  5184. float tmp = 0; // partial sum for thread in warp
  5185. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  5186. const float * y = yy + i * QK_K + y_offset;
  5187. const uint8_t * ql = x[i].ql + ql_offset;
  5188. const uint8_t * qh = x[i].qh + qh_offset;
  5189. const int8_t * s = x[i].scales + s_offset;
  5190. const float d = x[i].d;
  5191. #if K_QUANTS_PER_ITERATION == 1
  5192. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  5193. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  5194. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  5195. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  5196. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  5197. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  5198. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  5199. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  5200. tmp += sum;
  5201. #else
  5202. float sum = 0;
  5203. for (int l = 0; l < 4; ++l) {
  5204. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  5205. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  5206. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  5207. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  5208. }
  5209. tmp += sum;
  5210. #endif
  5211. }
  5212. #else
  5213. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...7
  5214. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION); // 0...3
  5215. const int step = tid * K_QUANTS_PER_ITERATION;
  5216. float tmp = 0; // partial sum for thread in warp
  5217. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  5218. const float * y = yy + i * QK_K + step;
  5219. const uint8_t * ql = x[i].ql + step;
  5220. const uint8_t * qh = x[i].qh + step;
  5221. const int8_t * s = x[i].scales;
  5222. const float d = x[i+0].d;
  5223. float sum = 0;
  5224. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  5225. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  5226. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  5227. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  5228. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  5229. }
  5230. tmp += sum;
  5231. }
  5232. #endif
  5233. // sum up partial sums and write back result
  5234. #pragma unroll
  5235. for (int mask = 16; mask > 0; mask >>= 1) {
  5236. tmp +=
  5237. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  5238. }
  5239. if (tid == 0) {
  5240. dst[row] = tmp;
  5241. }
  5242. }
  5243. static void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  5244. const sycl::half *x = (const sycl::half *)vx;
  5245. // automatic half -> float type cast if dfloat == float
  5246. v.x() = x[ib + iqs + 0];
  5247. v.y() = x[ib + iqs + 1];
  5248. }
  5249. static void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  5250. const float * x = (const float *) vx;
  5251. // automatic half -> float type cast if dfloat == float
  5252. v.x() = x[ib + iqs + 0];
  5253. v.y() = x[ib + iqs + 1];
  5254. }
  5255. static void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded,
  5256. const sycl::nd_item<3> &item_ct1) {
  5257. const int ix = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  5258. item_ct1.get_local_id(2);
  5259. if (ix >= kx_padded) {
  5260. return;
  5261. }
  5262. const int iy = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  5263. item_ct1.get_local_id(1);
  5264. const int i_padded = iy*kx_padded + ix;
  5265. block_q8_1 * y = (block_q8_1 *) vy;
  5266. const int ib = i_padded / QK8_1; // block index
  5267. const int iqs = i_padded % QK8_1; // quant index
  5268. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  5269. float amax = sycl::fabs((float)xi);
  5270. float sum = xi;
  5271. #pragma unroll
  5272. for (int mask = 16; mask > 0; mask >>= 1) {
  5273. amax = sycl::fmax(amax, dpct::permute_sub_group_by_xor(
  5274. item_ct1.get_sub_group(), amax, mask));
  5275. sum +=
  5276. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), sum, mask);
  5277. }
  5278. const float d = amax / 127;
  5279. const int8_t q = amax == 0.0f ? 0 : sycl::round(xi / d);
  5280. y[ib].qs[iqs] = q;
  5281. if (iqs > 0) {
  5282. return;
  5283. }
  5284. reinterpret_cast<sycl::half &>(y[ib].ds.x()) = d;
  5285. reinterpret_cast<sycl::half &>(y[ib].ds.y()) = sum;
  5286. }
  5287. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  5288. static void k_get_rows(
  5289. const void * src0, const int32_t * src1, dst_t * dst,
  5290. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  5291. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  5292. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  5293. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  5294. size_t s10, size_t s11, size_t s12,
  5295. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  5296. const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  5297. item_ct1.get_local_id(2)) *
  5298. 2;
  5299. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  5300. item_ct1.get_local_id(1);
  5301. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  5302. item_ct1.get_local_id(0)) /
  5303. ne12;
  5304. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  5305. item_ct1.get_local_id(0)) %
  5306. ne12;
  5307. if (i00 >= ne00) {
  5308. return;
  5309. }
  5310. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  5311. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  5312. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  5313. const int ib = i00/qk; // block index
  5314. const int iqs = (i00%qk)/qr; // quant index
  5315. const int iybs = i00 - i00%qk; // dst block start index
  5316. const int y_offset = qr == 1 ? 1 : qk/2;
  5317. // dequantize
  5318. dfloat2 v;
  5319. dequantize_kernel(src0_row, ib, iqs, v);
  5320. dst_row[iybs + iqs + 0] = v.x();
  5321. dst_row[iybs + iqs + y_offset] = v.y();
  5322. }
  5323. template<typename src0_t, typename dst_t>
  5324. static void k_get_rows_float(
  5325. const src0_t * src0, const int32_t * src1, dst_t * dst,
  5326. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  5327. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  5328. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  5329. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  5330. size_t s10, size_t s11, size_t s12,
  5331. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  5332. const int i00 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  5333. item_ct1.get_local_id(2);
  5334. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  5335. item_ct1.get_local_id(1);
  5336. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  5337. item_ct1.get_local_id(0)) /
  5338. ne12;
  5339. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  5340. item_ct1.get_local_id(0)) %
  5341. ne12;
  5342. if (i00 >= ne00) {
  5343. return;
  5344. }
  5345. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  5346. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  5347. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  5348. dst_row[i00] = src0_row[i00];
  5349. }
  5350. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  5351. static void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  5352. const sycl::nd_item<3> &item_ct1) {
  5353. const int i = 2 * (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  5354. item_ct1.get_local_id(2));
  5355. if (i >= k) {
  5356. return;
  5357. }
  5358. const int ib = i/qk; // block index
  5359. const int iqs = (i%qk)/qr; // quant index
  5360. const int iybs = i - i%qk; // y block start index
  5361. const int y_offset = qr == 1 ? 1 : qk/2;
  5362. // dequantize
  5363. dfloat2 v;
  5364. dequantize_kernel(vx, ib, iqs, v);
  5365. y[iybs + iqs + 0] = v.x();
  5366. y[iybs + iqs + y_offset] = v.y();
  5367. }
  5368. template <typename src_t, typename dst_t>
  5369. static void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  5370. const sycl::nd_item<3> &item_ct1) {
  5371. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  5372. item_ct1.get_local_id(2);
  5373. if (i >= k) {
  5374. return;
  5375. }
  5376. const src_t * x = (src_t *) vx;
  5377. y[i] = x[i];
  5378. }
  5379. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  5380. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  5381. #define VDR_Q4_0_Q8_1_MMVQ 2
  5382. #define VDR_Q4_0_Q8_1_MMQ 4
  5383. template <int vdr>
  5384. static __dpct_inline__ float vec_dot_q4_0_q8_1_impl(const int *v, const int *u,
  5385. const float &d4,
  5386. const sycl::half2 &ds8) {
  5387. int sumi = 0;
  5388. #pragma unroll
  5389. for (int i = 0; i < vdr; ++i) {
  5390. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  5391. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  5392. // SIMD dot product of quantized values
  5393. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  5394. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  5395. }
  5396. const sycl::float2 ds8f =
  5397. ds8.convert<float, sycl::rounding_mode::automatic>();
  5398. // second part effectively subtracts 8 from each quant value
  5399. return d4 * (sumi * ds8f.x() - (8 * vdr / QI4_0) * ds8f.y());
  5400. }
  5401. #define VDR_Q4_1_Q8_1_MMVQ 2
  5402. #define VDR_Q4_1_Q8_1_MMQ 4
  5403. template <int vdr>
  5404. static __dpct_inline__ float vec_dot_q4_1_q8_1_impl(const int *v, const int *u,
  5405. const sycl::half2 &dm4,
  5406. const sycl::half2 &ds8) {
  5407. int sumi = 0;
  5408. #pragma unroll
  5409. for (int i = 0; i < vdr; ++i) {
  5410. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  5411. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  5412. // SIMD dot product of quantized values
  5413. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  5414. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  5415. }
  5416. #ifdef GGML_SYCL_F16
  5417. const sycl::float2 tmp =
  5418. (dm4 * ds8).convert<float, sycl::rounding_mode::automatic>();
  5419. const float d4d8 = tmp.x();
  5420. const float m4s8 = tmp.y();
  5421. #else
  5422. const sycl::float2 dm4f =
  5423. dm4.convert<float, sycl::rounding_mode::automatic>();
  5424. const sycl::float2 ds8f =
  5425. ds8.convert<float, sycl::rounding_mode::automatic>();
  5426. const float d4d8 = dm4f.x() * ds8f.x();
  5427. const float m4s8 = dm4f.y() * ds8f.y();
  5428. #endif // GGML_SYCL_F16
  5429. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  5430. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  5431. }
  5432. #define VDR_Q5_0_Q8_1_MMVQ 2
  5433. #define VDR_Q5_0_Q8_1_MMQ 4
  5434. template <int vdr>
  5435. static __dpct_inline__ float
  5436. vec_dot_q5_0_q8_1_impl(const int *vl, const int *vh, const int *u,
  5437. const float &d5, const sycl::half2 &ds8) {
  5438. int sumi = 0;
  5439. #pragma unroll
  5440. for (int i = 0; i < vdr; ++i) {
  5441. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  5442. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  5443. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  5444. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  5445. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  5446. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  5447. sumi); // SIMD dot product of quantized values
  5448. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  5449. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  5450. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  5451. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  5452. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  5453. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  5454. sumi); // SIMD dot product of quantized values
  5455. }
  5456. const sycl::float2 ds8f =
  5457. ds8.convert<float, sycl::rounding_mode::automatic>();
  5458. // second part effectively subtracts 16 from each quant value
  5459. return d5 * (sumi * ds8f.x() - (16 * vdr / QI5_0) * ds8f.y());
  5460. }
  5461. #define VDR_Q5_1_Q8_1_MMVQ 2
  5462. #define VDR_Q5_1_Q8_1_MMQ 4
  5463. template <int vdr>
  5464. static __dpct_inline__ float
  5465. vec_dot_q5_1_q8_1_impl(const int *vl, const int *vh, const int *u,
  5466. const sycl::half2 &dm5, const sycl::half2 &ds8) {
  5467. int sumi = 0;
  5468. #pragma unroll
  5469. for (int i = 0; i < vdr; ++i) {
  5470. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  5471. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  5472. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  5473. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  5474. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  5475. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  5476. sumi); // SIMD dot product of quantized values
  5477. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  5478. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  5479. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  5480. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  5481. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  5482. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  5483. sumi); // SIMD dot product of quantized values
  5484. }
  5485. #ifdef GGML_SYCL_F16
  5486. const sycl::float2 tmp =
  5487. (dm5 * ds8).convert<float, sycl::rounding_mode::automatic>();
  5488. const float d5d8 = tmp.x();
  5489. const float m5s8 = tmp.y();
  5490. #else
  5491. const sycl::float2 dm5f =
  5492. dm5.convert<float, sycl::rounding_mode::automatic>();
  5493. const sycl::float2 ds8f =
  5494. ds8.convert<float, sycl::rounding_mode::automatic>();
  5495. const float d5d8 = dm5f.x() * ds8f.x();
  5496. const float m5s8 = dm5f.y() * ds8f.y();
  5497. #endif // GGML_SYCL_F16
  5498. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  5499. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  5500. }
  5501. #define VDR_Q8_0_Q8_1_MMVQ 2
  5502. #define VDR_Q8_0_Q8_1_MMQ 8
  5503. template <int vdr>
  5504. static __dpct_inline__ float vec_dot_q8_0_q8_1_impl(const int *v, const int *u,
  5505. const float &d8_0,
  5506. const float &d8_1) {
  5507. int sumi = 0;
  5508. #pragma unroll
  5509. for (int i = 0; i < vdr; ++i) {
  5510. // SIMD dot product of quantized values
  5511. sumi = dpct::dp4a(v[i], u[i], sumi);
  5512. }
  5513. return d8_0*d8_1 * sumi;
  5514. }
  5515. template <int vdr>
  5516. static __dpct_inline__ float vec_dot_q8_1_q8_1_impl(const int *v, const int *u,
  5517. const sycl::half2 &dm8,
  5518. const sycl::half2 &ds8) {
  5519. int sumi = 0;
  5520. #pragma unroll
  5521. for (int i = 0; i < vdr; ++i) {
  5522. // SIMD dot product of quantized values
  5523. sumi = dpct::dp4a(v[i], u[i], sumi);
  5524. }
  5525. #ifdef GGML_SYCL_F16
  5526. const sycl::float2 tmp =
  5527. (dm8 * ds8).convert<float, sycl::rounding_mode::automatic>();
  5528. const float d8d8 = tmp.x();
  5529. const float m8s8 = tmp.y();
  5530. #else
  5531. const sycl::float2 dm8f =
  5532. dm8.convert<float, sycl::rounding_mode::automatic>();
  5533. const sycl::float2 ds8f =
  5534. ds8.convert<float, sycl::rounding_mode::automatic>();
  5535. const float d8d8 = dm8f.x() * ds8f.x();
  5536. const float m8s8 = dm8f.y() * ds8f.y();
  5537. #endif // GGML_SYCL_F16
  5538. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  5539. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  5540. }
  5541. #define VDR_Q2_K_Q8_1_MMVQ 1
  5542. #define VDR_Q2_K_Q8_1_MMQ 2
  5543. // contiguous v/x values
  5544. static __dpct_inline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  5545. const int &v, const int *__restrict__ u, const uint8_t *__restrict__ scales,
  5546. const sycl::half2 &dm2, const float *__restrict__ d8) {
  5547. float sumf_d = 0.0f;
  5548. float sumf_m = 0.0f;
  5549. #pragma unroll
  5550. for (int i = 0; i < QR2_K; ++i) {
  5551. const int sc = scales[2*i];
  5552. const int vi = (v >> (2*i)) & 0x03030303;
  5553. sumf_d +=
  5554. d8[i] * (dpct::dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  5555. // fill int with 4x m
  5556. int m = sc >> 4;
  5557. m |= m << 8;
  5558. m |= m << 16;
  5559. sumf_m += d8[i] *
  5560. dpct::dp4a(
  5561. m, u[i],
  5562. 0); // multiply constant q2_K part with sum of q8_1 values
  5563. }
  5564. const sycl::float2 dm2f =
  5565. dm2.convert<float, sycl::rounding_mode::automatic>();
  5566. return dm2f.x() * sumf_d - dm2f.y() * sumf_m;
  5567. }
  5568. // contiguous u/y values
  5569. static __dpct_inline__ float
  5570. vec_dot_q2_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  5571. const uint8_t *__restrict__ scales,
  5572. const sycl::half2 &dm2, const float &d8) {
  5573. int sumi_d = 0;
  5574. int sumi_m = 0;
  5575. #pragma unroll
  5576. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  5577. int sumi_d_sc = 0;
  5578. const int sc = scales[i0 / (QI8_1/2)];
  5579. // fill int with 4x m
  5580. int m = sc >> 4;
  5581. m |= m << 8;
  5582. m |= m << 16;
  5583. #pragma unroll
  5584. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  5585. sumi_d_sc = dpct::dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  5586. sumi_m = dpct::dp4a(m, u[i],
  5587. sumi_m); // multiply sum of q8_1 values with m
  5588. }
  5589. sumi_d += sumi_d_sc * (sc & 0xF);
  5590. }
  5591. const sycl::float2 dm2f =
  5592. dm2.convert<float, sycl::rounding_mode::automatic>();
  5593. return d8 * (dm2f.x() * sumi_d - dm2f.y() * sumi_m);
  5594. }
  5595. #define VDR_Q3_K_Q8_1_MMVQ 1
  5596. #define VDR_Q3_K_Q8_1_MMQ 2
  5597. // contiguous v/x values
  5598. static __dpct_inline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  5599. const int &vl, const int &vh, const int *__restrict__ u,
  5600. const uint8_t *__restrict__ scales, const int &scale_offset,
  5601. const float &d3, const float *__restrict__ d8) {
  5602. float sumf = 0.0f;
  5603. #pragma unroll
  5604. for (int i = 0; i < QR3_K; ++i) {
  5605. const int isc = scale_offset + 2*i;
  5606. const int isc_low = isc % (QK_K/32);
  5607. const int sc_shift_low = 4 * (isc / (QK_K/32));
  5608. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  5609. const int isc_high = isc % (QK_K/64);
  5610. const int sc_shift_high = 2 * (isc / (QK_K/64));
  5611. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  5612. const int sc = (sc_low | sc_high) - 32;
  5613. const int vil = (vl >> (2*i)) & 0x03030303;
  5614. const int vih = ((vh >> i) << 2) & 0x04040404;
  5615. const int vi =
  5616. dpct::vectorized_binary<sycl::char4>(vil, vih, dpct::sub_sat());
  5617. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  5618. }
  5619. return d3 * sumf;
  5620. }
  5621. // contiguous u/y values
  5622. static __dpct_inline__ float
  5623. vec_dot_q3_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  5624. const int8_t *__restrict__ scales, const float &d3,
  5625. const float &d8) {
  5626. int sumi = 0;
  5627. #pragma unroll
  5628. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  5629. int sumi_sc = 0;
  5630. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  5631. sumi_sc = dpct::dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  5632. }
  5633. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  5634. }
  5635. return d3*d8 * sumi;
  5636. }
  5637. #define VDR_Q4_K_Q8_1_MMVQ 2
  5638. #define VDR_Q4_K_Q8_1_MMQ 8
  5639. // contiguous v/x values
  5640. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  5641. const int *__restrict__ v, const int *__restrict__ u,
  5642. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5643. const sycl::half2 &dm4, const float *__restrict__ d8) {
  5644. float sumf_d = 0.0f;
  5645. float sumf_m = 0.0f;
  5646. #pragma unroll
  5647. for (int i = 0; i < QR4_K; ++i) {
  5648. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  5649. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  5650. const int dot1 =
  5651. dpct::dp4a(v1i, u[2 * i + 1],
  5652. dpct::dp4a(v0i, u[2 * i + 0], 0)); // SIMD dot product
  5653. const int dot2 =
  5654. dpct::dp4a(0x01010101, u[2 * i + 1],
  5655. dpct::dp4a(0x01010101, u[2 * i + 0], 0)); // sum of u
  5656. sumf_d += d8[i] * (dot1 * sc[i]);
  5657. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  5658. }
  5659. const sycl::float2 dm4f =
  5660. dm4.convert<float, sycl::rounding_mode::automatic>();
  5661. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5662. }
  5663. // contiguous u/y values
  5664. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_mmq(
  5665. const int *__restrict__ v, const int *__restrict__ u,
  5666. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5667. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  5668. float sumf_d = 0.0f;
  5669. float sumf_m = 0.0f;
  5670. #pragma unroll
  5671. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  5672. int sumi_d = 0;
  5673. #pragma unroll
  5674. for (int j = 0; j < QI8_1; ++j) {
  5675. sumi_d = dpct::dp4a((v[j] >> (4 * i)) & 0x0F0F0F0F,
  5676. u[i * QI8_1 + j], sumi_d); // SIMD dot product
  5677. }
  5678. const sycl::float2 ds8f =
  5679. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  5680. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  5681. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  5682. }
  5683. const sycl::float2 dm4f =
  5684. dm4.convert<float, sycl::rounding_mode::automatic>();
  5685. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5686. }
  5687. #define VDR_Q5_K_Q8_1_MMVQ 2
  5688. #define VDR_Q5_K_Q8_1_MMQ 8
  5689. // contiguous v/x values
  5690. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  5691. const int *__restrict__ vl, const int *__restrict__ vh,
  5692. const int *__restrict__ u, const uint8_t *__restrict__ sc,
  5693. const uint8_t *__restrict__ m, const sycl::half2 &dm5,
  5694. const float *__restrict__ d8) {
  5695. float sumf_d = 0.0f;
  5696. float sumf_m = 0.0f;
  5697. #pragma unroll
  5698. for (int i = 0; i < QR5_K; ++i) {
  5699. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  5700. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  5701. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  5702. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  5703. const int v0i = vl0i | vh0i;
  5704. const int v1i = vl1i | vh1i;
  5705. const int dot1 =
  5706. dpct::dp4a(v0i, u[2 * i + 0],
  5707. dpct::dp4a(v1i, u[2 * i + 1], 0)); // SIMD dot product
  5708. const int dot2 =
  5709. dpct::dp4a(0x01010101, u[2 * i + 0],
  5710. dpct::dp4a(0x01010101, u[2 * i + 1], 0)); // sum of u
  5711. sumf_d += d8[i] * (dot1 * sc[i]);
  5712. sumf_m += d8[i] * (dot2 * m[i]);
  5713. }
  5714. const sycl::float2 dm5f =
  5715. dm5.convert<float, sycl::rounding_mode::automatic>();
  5716. return dm5f.x() * sumf_d - dm5f.y() * sumf_m;
  5717. }
  5718. // contiguous u/y values
  5719. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_mmq(
  5720. const int *__restrict__ v, const int *__restrict__ u,
  5721. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5722. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  5723. float sumf_d = 0.0f;
  5724. float sumf_m = 0.0f;
  5725. #pragma unroll
  5726. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  5727. int sumi_d = 0;
  5728. #pragma unroll
  5729. for (int j = 0; j < QI8_1; ++j) {
  5730. sumi_d = dpct::dp4a(v[i * QI8_1 + j], u[i * QI8_1 + j],
  5731. sumi_d); // SIMD dot product
  5732. }
  5733. const sycl::float2 ds8f =
  5734. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  5735. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  5736. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  5737. }
  5738. const sycl::float2 dm4f =
  5739. dm4.convert<float, sycl::rounding_mode::automatic>();
  5740. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5741. }
  5742. #define VDR_Q6_K_Q8_1_MMVQ 1
  5743. #define VDR_Q6_K_Q8_1_MMQ 8
  5744. // contiguous v/x values
  5745. static __dpct_inline__ float
  5746. vec_dot_q6_K_q8_1_impl_mmvq(const int &vl, const int &vh,
  5747. const int *__restrict__ u,
  5748. const int8_t *__restrict__ scales, const float &d,
  5749. const float *__restrict__ d8) {
  5750. float sumf = 0.0f;
  5751. #pragma unroll
  5752. for (int i = 0; i < QR6_K; ++i) {
  5753. const int sc = scales[4*i];
  5754. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  5755. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  5756. const int vi = dpct::vectorized_binary<sycl::char4>(
  5757. (vil | vih), 0x20202020, dpct::sub_sat()); // vi = (vil | vih) - 32
  5758. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  5759. }
  5760. return d*sumf;
  5761. }
  5762. // contiguous u/y values
  5763. static __dpct_inline__ float
  5764. vec_dot_q6_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  5765. const int8_t *__restrict__ sc, const float &d6,
  5766. const float *__restrict__ d8) {
  5767. float sumf_d = 0.0f;
  5768. #pragma unroll
  5769. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  5770. sycl::int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  5771. #pragma unroll
  5772. for (int i = i0; i < i0 + 2; ++i) {
  5773. sumi_d.x() = dpct::dp4a(v[2 * i + 0], u[2 * i + 0],
  5774. sumi_d.x()); // SIMD dot product
  5775. sumi_d.x() = dpct::dp4a(v[2 * i + 1], u[2 * i + 1],
  5776. sumi_d.x()); // SIMD dot product
  5777. sumi_d.y() = dpct::dp4a(v[2 * i + 4], u[2 * i + 4],
  5778. sumi_d.y()); // SIMD dot product
  5779. sumi_d.y() = dpct::dp4a(v[2 * i + 5], u[2 * i + 5],
  5780. sumi_d.y()); // SIMD dot product
  5781. }
  5782. sumf_d += d8[i0 / 4] *
  5783. (sc[i0 / 2 + 0] * sumi_d.x() + sc[i0 / 2 + 1] * sumi_d.y());
  5784. }
  5785. return d6 * sumf_d;
  5786. }
  5787. static __dpct_inline__ float
  5788. vec_dot_q4_0_q8_1(const void *__restrict__ vbq,
  5789. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5790. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  5791. int v[VDR_Q4_0_Q8_1_MMVQ];
  5792. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  5793. #pragma unroll
  5794. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  5795. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  5796. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5797. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  5798. }
  5799. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  5800. }
  5801. template <int mmq_y>
  5802. static __dpct_inline__ void
  5803. allocate_tiles_q4_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5804. int *tile_x_qs_q4_0, float *tile_x_d_q4_0) {
  5805. (void)x_qh; (void)x_sc;
  5806. *x_ql = tile_x_qs_q4_0;
  5807. *x_dm = (sycl::half2 *)tile_x_d_q4_0;
  5808. }
  5809. template <int mmq_y, int nwarps, bool need_check>
  5810. static __dpct_inline__ void
  5811. load_tiles_q4_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5812. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5813. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5814. const int &k, const int &blocks_per_row) {
  5815. (void)x_qh; (void)x_sc;
  5816. GGML_SYCL_ASSUME(i_offset >= 0);
  5817. GGML_SYCL_ASSUME(i_offset < nwarps);
  5818. GGML_SYCL_ASSUME(k >= 0);
  5819. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5820. const int kbx = k / QI4_0;
  5821. const int kqsx = k % QI4_0;
  5822. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  5823. float * x_dmf = (float *) x_dm;
  5824. #pragma unroll
  5825. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5826. int i = i0 + i_offset;
  5827. if (need_check) {
  5828. i = sycl::min(i, i_max);
  5829. }
  5830. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5831. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5832. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  5833. }
  5834. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  5835. const int kbxd = k % blocks_per_tile_x_row;
  5836. #pragma unroll
  5837. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  5838. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  5839. if (need_check) {
  5840. i = sycl::min(i, i_max);
  5841. }
  5842. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5843. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  5844. }
  5845. }
  5846. static __dpct_inline__ float vec_dot_q4_0_q8_1_mul_mat(
  5847. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5848. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5849. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5850. const int &i, const int &j, const int &k) {
  5851. (void)x_qh; (void)x_sc;
  5852. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5853. const float * x_dmf = (const float *) x_dm;
  5854. int u[2*VDR_Q4_0_Q8_1_MMQ];
  5855. #pragma unroll
  5856. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  5857. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5858. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  5859. }
  5860. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  5861. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  5862. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5863. }
  5864. static __dpct_inline__ float
  5865. vec_dot_q4_1_q8_1(const void *__restrict__ vbq,
  5866. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5867. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  5868. int v[VDR_Q4_1_Q8_1_MMVQ];
  5869. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  5870. #pragma unroll
  5871. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  5872. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  5873. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5874. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  5875. }
  5876. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  5877. }
  5878. template <int mmq_y>
  5879. static __dpct_inline__ void
  5880. allocate_tiles_q4_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5881. int *tile_x_qs_q4_1, sycl::half2 *tile_x_dm_q4_1) {
  5882. (void)x_qh; (void)x_sc;
  5883. *x_ql = tile_x_qs_q4_1;
  5884. *x_dm = tile_x_dm_q4_1;
  5885. }
  5886. template <int mmq_y, int nwarps, bool need_check>
  5887. static __dpct_inline__ void
  5888. load_tiles_q4_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5889. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5890. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5891. const int &k, const int &blocks_per_row) {
  5892. (void)x_qh; (void)x_sc;
  5893. GGML_SYCL_ASSUME(i_offset >= 0);
  5894. GGML_SYCL_ASSUME(i_offset < nwarps);
  5895. GGML_SYCL_ASSUME(k >= 0);
  5896. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5897. const int kbx = k / QI4_1;
  5898. const int kqsx = k % QI4_1;
  5899. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  5900. #pragma unroll
  5901. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5902. int i = i0 + i_offset;
  5903. if (need_check) {
  5904. i = sycl::min(i, i_max);
  5905. }
  5906. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5907. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5908. }
  5909. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  5910. const int kbxd = k % blocks_per_tile_x_row;
  5911. #pragma unroll
  5912. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  5913. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  5914. if (need_check) {
  5915. i = sycl::min(i, i_max);
  5916. }
  5917. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5918. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  5919. }
  5920. }
  5921. static __dpct_inline__ float vec_dot_q4_1_q8_1_mul_mat(
  5922. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5923. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5924. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5925. const int &i, const int &j, const int &k) {
  5926. (void)x_qh; (void)x_sc;
  5927. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5928. int u[2*VDR_Q4_1_Q8_1_MMQ];
  5929. #pragma unroll
  5930. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  5931. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5932. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  5933. }
  5934. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  5935. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  5936. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5937. }
  5938. static __dpct_inline__ float
  5939. vec_dot_q5_0_q8_1(const void *__restrict__ vbq,
  5940. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5941. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  5942. int vl[VDR_Q5_0_Q8_1_MMVQ];
  5943. int vh[VDR_Q5_0_Q8_1_MMVQ];
  5944. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  5945. #pragma unroll
  5946. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  5947. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  5948. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  5949. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5950. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  5951. }
  5952. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  5953. }
  5954. template <int mmq_y>
  5955. static __dpct_inline__ void
  5956. allocate_tiles_q5_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5957. int *tile_x_ql_q5_0, float *tile_x_d_q5_0) {
  5958. (void)x_qh; (void)x_sc;
  5959. *x_ql = tile_x_ql_q5_0;
  5960. *x_dm = (sycl::half2 *)tile_x_d_q5_0;
  5961. }
  5962. template <int mmq_y, int nwarps, bool need_check>
  5963. static __dpct_inline__ void
  5964. load_tiles_q5_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5965. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5966. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5967. const int &k, const int &blocks_per_row) {
  5968. (void)x_qh; (void)x_sc;
  5969. GGML_SYCL_ASSUME(i_offset >= 0);
  5970. GGML_SYCL_ASSUME(i_offset < nwarps);
  5971. GGML_SYCL_ASSUME(k >= 0);
  5972. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5973. const int kbx = k / QI5_0;
  5974. const int kqsx = k % QI5_0;
  5975. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  5976. #pragma unroll
  5977. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5978. int i = i0 + i_offset;
  5979. if (need_check) {
  5980. i = sycl::min(i, i_max);
  5981. }
  5982. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5983. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  5984. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  5985. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5986. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5987. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5988. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5989. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5990. qs0 = dpct::vectorized_binary<sycl::char4>(
  5991. qs0, 0x10101010, dpct::sub_sat()); // subtract 16
  5992. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5993. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5994. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5995. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5996. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5997. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5998. qs1 = dpct::vectorized_binary<sycl::char4>(
  5999. qs1, 0x10101010, dpct::sub_sat()); // subtract 16
  6000. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  6001. }
  6002. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  6003. const int kbxd = k % blocks_per_tile_x_row;
  6004. float * x_dmf = (float *) x_dm;
  6005. #pragma unroll
  6006. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  6007. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  6008. if (need_check) {
  6009. i = sycl::min(i, i_max);
  6010. }
  6011. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  6012. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  6013. }
  6014. }
  6015. static __dpct_inline__ float vec_dot_q5_0_q8_1_mul_mat(
  6016. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6017. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6018. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6019. const int &i, const int &j, const int &k) {
  6020. (void)x_qh; (void)x_sc;
  6021. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  6022. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  6023. const float * x_dmf = (const float *) x_dm;
  6024. const float * y_df = (const float *) y_ds;
  6025. int u[2*VDR_Q5_0_Q8_1_MMQ];
  6026. #pragma unroll
  6027. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  6028. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  6029. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  6030. }
  6031. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  6032. (&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)]);
  6033. }
  6034. static __dpct_inline__ float
  6035. vec_dot_q5_1_q8_1(const void *__restrict__ vbq,
  6036. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6037. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  6038. int vl[VDR_Q5_1_Q8_1_MMVQ];
  6039. int vh[VDR_Q5_1_Q8_1_MMVQ];
  6040. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  6041. #pragma unroll
  6042. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  6043. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  6044. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  6045. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  6046. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  6047. }
  6048. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  6049. }
  6050. template <int mmq_y>
  6051. static __dpct_inline__ void
  6052. allocate_tiles_q5_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  6053. int *tile_x_ql_q5_1, sycl::half2 *tile_x_dm_q5_1) {
  6054. (void)x_qh; (void)x_sc;
  6055. *x_ql = tile_x_ql_q5_1;
  6056. *x_dm = tile_x_dm_q5_1;
  6057. }
  6058. template <int mmq_y, int nwarps, bool need_check>
  6059. static __dpct_inline__ void
  6060. load_tiles_q5_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  6061. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6062. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6063. const int &k, const int &blocks_per_row) {
  6064. (void)x_qh; (void)x_sc;
  6065. GGML_SYCL_ASSUME(i_offset >= 0);
  6066. GGML_SYCL_ASSUME(i_offset < nwarps);
  6067. GGML_SYCL_ASSUME(k >= 0);
  6068. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6069. const int kbx = k / QI5_1;
  6070. const int kqsx = k % QI5_1;
  6071. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  6072. #pragma unroll
  6073. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6074. int i = i0 + i_offset;
  6075. if (need_check) {
  6076. i = sycl::min(i, i_max);
  6077. }
  6078. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  6079. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  6080. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  6081. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  6082. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  6083. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  6084. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  6085. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  6086. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  6087. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  6088. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  6089. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  6090. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  6091. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  6092. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  6093. }
  6094. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  6095. const int kbxd = k % blocks_per_tile_x_row;
  6096. #pragma unroll
  6097. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  6098. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  6099. if (need_check) {
  6100. i = sycl::min(i, i_max);
  6101. }
  6102. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  6103. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  6104. }
  6105. }
  6106. static __dpct_inline__ float vec_dot_q5_1_q8_1_mul_mat(
  6107. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6108. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6109. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6110. const int &i, const int &j, const int &k) {
  6111. (void)x_qh; (void)x_sc;
  6112. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  6113. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  6114. int u[2*VDR_Q5_1_Q8_1_MMQ];
  6115. #pragma unroll
  6116. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  6117. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  6118. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  6119. }
  6120. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  6121. (&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)]);
  6122. }
  6123. static __dpct_inline__ float
  6124. vec_dot_q8_0_q8_1(const void *__restrict__ vbq,
  6125. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6126. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  6127. int v[VDR_Q8_0_Q8_1_MMVQ];
  6128. int u[VDR_Q8_0_Q8_1_MMVQ];
  6129. #pragma unroll
  6130. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  6131. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  6132. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  6133. }
  6134. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d,
  6135. bq8_1->ds[0]);
  6136. }
  6137. template <int mmq_y>
  6138. static __dpct_inline__ void
  6139. allocate_tiles_q8_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  6140. int *tile_x_qs_q8_0, float *tile_x_d_q8_0) {
  6141. (void)x_qh; (void)x_sc;
  6142. *x_ql = tile_x_qs_q8_0;
  6143. *x_dm = (sycl::half2 *)tile_x_d_q8_0;
  6144. }
  6145. template <int mmq_y, int nwarps, bool need_check>
  6146. static __dpct_inline__ void
  6147. load_tiles_q8_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  6148. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6149. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6150. const int &k, const int &blocks_per_row) {
  6151. (void)x_qh; (void)x_sc;
  6152. GGML_SYCL_ASSUME(i_offset >= 0);
  6153. GGML_SYCL_ASSUME(i_offset < nwarps);
  6154. GGML_SYCL_ASSUME(k >= 0);
  6155. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6156. const int kbx = k / QI8_0;
  6157. const int kqsx = k % QI8_0;
  6158. float * x_dmf = (float *) x_dm;
  6159. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  6160. #pragma unroll
  6161. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6162. int i = i0 + i_offset;
  6163. if (need_check) {
  6164. i = sycl::min(i, i_max);
  6165. }
  6166. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  6167. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  6168. }
  6169. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  6170. const int kbxd = k % blocks_per_tile_x_row;
  6171. #pragma unroll
  6172. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  6173. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  6174. if (need_check) {
  6175. i = sycl::min(i, i_max);
  6176. }
  6177. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  6178. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  6179. }
  6180. }
  6181. static __dpct_inline__ float vec_dot_q8_0_q8_1_mul_mat(
  6182. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6183. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6184. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6185. const int &i, const int &j, const int &k) {
  6186. (void)x_qh; (void)x_sc;
  6187. const float * x_dmf = (const float *) x_dm;
  6188. const float * y_df = (const float *) y_ds;
  6189. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  6190. (&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],
  6191. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  6192. }
  6193. static __dpct_inline__ float
  6194. vec_dot_q2_K_q8_1(const void *__restrict__ vbq,
  6195. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6196. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  6197. const int bq8_offset = QR2_K * (iqs / QI8_1);
  6198. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  6199. const uint8_t * scales = bq2_K->scales + scale_offset;
  6200. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  6201. int u[QR2_K];
  6202. float d8[QR2_K];
  6203. #pragma unroll
  6204. for (int i = 0; i < QR2_K; ++ i) {
  6205. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  6206. d8[i] = bq8_1[bq8_offset + i].ds[0];
  6207. }
  6208. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  6209. }
  6210. template <int mmq_y>
  6211. static __dpct_inline__ void
  6212. allocate_tiles_q2_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  6213. int *tile_x_ql_q2_K, sycl::half2 *tile_x_dm_q2_K,
  6214. int *tile_x_sc_q2_K) {
  6215. (void)x_qh;
  6216. *x_ql = tile_x_ql_q2_K;
  6217. *x_dm = tile_x_dm_q2_K;
  6218. *x_sc = tile_x_sc_q2_K;
  6219. }
  6220. template <int mmq_y, int nwarps, bool need_check>
  6221. static __dpct_inline__ void
  6222. load_tiles_q2_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  6223. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6224. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6225. const int &k, const int &blocks_per_row) {
  6226. (void)x_qh;
  6227. GGML_SYCL_ASSUME(i_offset >= 0);
  6228. GGML_SYCL_ASSUME(i_offset < nwarps);
  6229. GGML_SYCL_ASSUME(k >= 0);
  6230. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6231. const int kbx = k / QI2_K;
  6232. const int kqsx = k % QI2_K;
  6233. const block_q2_K * bx0 = (const block_q2_K *) vx;
  6234. #pragma unroll
  6235. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6236. int i = i0 + i_offset;
  6237. if (need_check) {
  6238. i = sycl::min(i, i_max);
  6239. }
  6240. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  6241. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  6242. }
  6243. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  6244. const int kbxd = k % blocks_per_tile_x_row;
  6245. #pragma unroll
  6246. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  6247. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  6248. if (need_check) {
  6249. i = sycl::min(i, i_max);
  6250. }
  6251. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  6252. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  6253. }
  6254. #pragma unroll
  6255. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  6256. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  6257. if (need_check) {
  6258. i = sycl::min(i, i_max);
  6259. }
  6260. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  6261. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  6262. }
  6263. }
  6264. static __dpct_inline__ float vec_dot_q2_K_q8_1_mul_mat(
  6265. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6266. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6267. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6268. const int &i, const int &j, const int &k) {
  6269. (void)x_qh;
  6270. const int kbx = k / QI2_K;
  6271. const int ky = (k % QI2_K) * QR2_K;
  6272. const float * y_df = (const float *) y_ds;
  6273. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  6274. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  6275. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  6276. #pragma unroll
  6277. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  6278. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  6279. }
  6280. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  6281. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  6282. 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]);
  6283. }
  6284. static __dpct_inline__ float
  6285. vec_dot_q3_K_q8_1(const void *__restrict__ vbq,
  6286. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6287. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  6288. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  6289. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  6290. const float d = bq3_K->d;
  6291. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  6292. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  6293. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  6294. int u[QR3_K];
  6295. float d8[QR3_K];
  6296. #pragma unroll
  6297. for (int i = 0; i < QR3_K; ++i) {
  6298. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  6299. d8[i] = bq8_1[bq8_offset + i].ds[0];
  6300. }
  6301. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  6302. }
  6303. template <int mmq_y>
  6304. static __dpct_inline__ void
  6305. allocate_tiles_q3_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  6306. int *tile_x_ql_q3_K, sycl::half2 *tile_x_dm_q3_K,
  6307. int *tile_x_qh_q3_K, int *tile_x_sc_q3_K) {
  6308. *x_ql = tile_x_ql_q3_K;
  6309. *x_dm = tile_x_dm_q3_K;
  6310. *x_qh = tile_x_qh_q3_K;
  6311. *x_sc = tile_x_sc_q3_K;
  6312. }
  6313. template <int mmq_y, int nwarps, bool need_check>
  6314. static __dpct_inline__ void
  6315. load_tiles_q3_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  6316. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6317. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6318. const int &k, const int &blocks_per_row) {
  6319. GGML_SYCL_ASSUME(i_offset >= 0);
  6320. GGML_SYCL_ASSUME(i_offset < nwarps);
  6321. GGML_SYCL_ASSUME(k >= 0);
  6322. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6323. const int kbx = k / QI3_K;
  6324. const int kqsx = k % QI3_K;
  6325. const block_q3_K * bx0 = (const block_q3_K *) vx;
  6326. #pragma unroll
  6327. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6328. int i = i0 + i_offset;
  6329. if (need_check) {
  6330. i = sycl::min(i, i_max);
  6331. }
  6332. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  6333. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  6334. }
  6335. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  6336. const int kbxd = k % blocks_per_tile_x_row;
  6337. float * x_dmf = (float *) x_dm;
  6338. #pragma unroll
  6339. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  6340. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  6341. if (need_check) {
  6342. i = sycl::min(i, i_max);
  6343. }
  6344. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  6345. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  6346. }
  6347. #pragma unroll
  6348. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  6349. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  6350. if (need_check) {
  6351. i = sycl::min(i, i_max);
  6352. }
  6353. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  6354. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  6355. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  6356. }
  6357. #pragma unroll
  6358. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  6359. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  6360. if (need_check) {
  6361. i = sycl::min(i, i_max);
  6362. }
  6363. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  6364. const int ksc = k % (QI3_K/4);
  6365. const int ksc_low = ksc % (QI3_K/8);
  6366. const int shift_low = 4 * (ksc / (QI3_K/8));
  6367. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  6368. const int ksc_high = QI3_K/8;
  6369. const int shift_high = 2 * ksc;
  6370. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  6371. const int sc = dpct::vectorized_binary<sycl::char4>(
  6372. sc_low | sc_high, 0x20202020, dpct::sub_sat());
  6373. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  6374. }
  6375. }
  6376. static __dpct_inline__ float vec_dot_q3_K_q8_1_mul_mat(
  6377. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6378. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6379. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6380. const int &i, const int &j, const int &k) {
  6381. const int kbx = k / QI3_K;
  6382. const int ky = (k % QI3_K) * QR3_K;
  6383. const float * x_dmf = (const float *) x_dm;
  6384. const float * y_df = (const float *) y_ds;
  6385. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  6386. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  6387. #pragma unroll
  6388. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  6389. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  6390. const int shift = 2 * ((ky % 32) / 8);
  6391. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  6392. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  6393. const int vlh = (vh << 2) & 0x04040404;
  6394. v[l] = dpct::vectorized_binary<sycl::char4>(vll, vlh, dpct::sub_sat());
  6395. }
  6396. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  6397. 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]);
  6398. }
  6399. static __dpct_inline__ float
  6400. vec_dot_q4_K_q8_1(const void *__restrict__ vbq,
  6401. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6402. #ifndef GGML_QKK_64
  6403. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  6404. int v[2];
  6405. int u[2*QR4_K];
  6406. float d8[QR4_K];
  6407. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  6408. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  6409. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  6410. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  6411. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  6412. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  6413. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  6414. v[0] = q4[0];
  6415. v[1] = q4[4];
  6416. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  6417. uint16_t aux[2];
  6418. const int j = bq8_offset/2;
  6419. if (j < 2) {
  6420. aux[0] = scales[j+0] & 0x3f3f;
  6421. aux[1] = scales[j+2] & 0x3f3f;
  6422. } else {
  6423. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  6424. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  6425. }
  6426. const uint8_t * sc = (const uint8_t *)aux;
  6427. const uint8_t * m = sc + 2;
  6428. for (int i = 0; i < QR4_K; ++i) {
  6429. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  6430. d8[i] = bq8i->ds[0];
  6431. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  6432. u[2*i+0] = q8[0];
  6433. u[2*i+1] = q8[4];
  6434. }
  6435. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  6436. #else
  6437. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  6438. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  6439. float sumf_d = 0.0f;
  6440. float sumf_m = 0.0f;
  6441. uint16_t aux16[2];
  6442. const uint8_t * s = (const uint8_t *)aux16;
  6443. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  6444. aux16[0] = a[0] & 0x0f0f;
  6445. aux16[1] = (a[0] >> 4) & 0x0f0f;
  6446. const float dall = bq4_K->dm[0];
  6447. const float dmin = bq4_K->dm[1];
  6448. const float d8_1 = bq8_1[0].ds[0];
  6449. const float d8_2 = bq8_1[1].ds[1];
  6450. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  6451. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  6452. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  6453. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  6454. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  6455. const int v1 = q4[0];
  6456. const int v2 = q4[4];
  6457. const int dot1 = dpct::dp4a(ui2, v2 & 0x0f0f0f0f, dpct::dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  6458. const int dot2 = dpct::dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, dpct::dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  6459. const int dot3 = dpct::dp4a(0x01010101, ui2, dpct::dp4a(0x01010101, ui1, 0));
  6460. const int dot4 = dpct::dp4a(0x01010101, ui4, dpct::dp4a(0x01010101, ui3, 0));
  6461. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  6462. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  6463. return dall * sumf_d - dmin * sumf_m;
  6464. #else
  6465. bad_arch();
  6466. #endif // __SYCL_ARCH__ >= VER_4VEC
  6467. #endif
  6468. }
  6469. template <int mmq_y>
  6470. static __dpct_inline__ void
  6471. allocate_tiles_q4_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  6472. int *tile_x_ql_q4_K, sycl::half2 *tile_x_dm_q4_K,
  6473. int *tile_x_sc_q4_K) {
  6474. (void)x_qh;
  6475. *x_ql = tile_x_ql_q4_K;
  6476. *x_dm = tile_x_dm_q4_K;
  6477. *x_sc = tile_x_sc_q4_K;
  6478. }
  6479. template <int mmq_y, int nwarps, bool need_check>
  6480. static __dpct_inline__ void
  6481. load_tiles_q4_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  6482. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6483. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6484. const int &k, const int &blocks_per_row) {
  6485. (void)x_qh;
  6486. GGML_SYCL_ASSUME(i_offset >= 0);
  6487. GGML_SYCL_ASSUME(i_offset < nwarps);
  6488. GGML_SYCL_ASSUME(k >= 0);
  6489. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6490. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  6491. const int kqsx = k % QI4_K; // == k if QK_K == 256
  6492. const block_q4_K * bx0 = (const block_q4_K *) vx;
  6493. #pragma unroll
  6494. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6495. int i = i0 + i_offset;
  6496. if (need_check) {
  6497. i = sycl::min(i, i_max);
  6498. }
  6499. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  6500. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  6501. }
  6502. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  6503. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  6504. #pragma unroll
  6505. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  6506. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  6507. if (need_check) {
  6508. i = sycl::min(i, i_max);
  6509. }
  6510. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  6511. #if QK_K == 256
  6512. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  6513. #else
  6514. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  6515. #endif
  6516. }
  6517. #pragma unroll
  6518. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  6519. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  6520. if (need_check) {
  6521. i = sycl::min(i, i_max);
  6522. }
  6523. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  6524. const int * scales = (const int *) bxi->scales;
  6525. const int ksc = k % (WARP_SIZE/8);
  6526. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  6527. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  6528. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  6529. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  6530. }
  6531. }
  6532. static __dpct_inline__ float vec_dot_q4_K_q8_1_mul_mat(
  6533. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6534. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6535. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6536. const int &i, const int &j, const int &k) {
  6537. (void)x_qh;
  6538. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  6539. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  6540. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  6541. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  6542. }
  6543. static __dpct_inline__ float
  6544. vec_dot_q5_K_q8_1(const void *__restrict__ vbq,
  6545. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6546. #ifndef GGML_QKK_64
  6547. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  6548. int vl[2];
  6549. int vh[2];
  6550. int u[2*QR5_K];
  6551. float d8[QR5_K];
  6552. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  6553. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  6554. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  6555. vl[0] = ql[0];
  6556. vl[1] = ql[4];
  6557. vh[0] = qh[0] >> bq8_offset;
  6558. vh[1] = qh[4] >> bq8_offset;
  6559. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  6560. uint16_t aux[2];
  6561. const int j = bq8_offset/2;
  6562. if (j < 2) {
  6563. aux[0] = scales[j+0] & 0x3f3f;
  6564. aux[1] = scales[j+2] & 0x3f3f;
  6565. } else {
  6566. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  6567. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  6568. }
  6569. const uint8_t * sc = (const uint8_t *)aux;
  6570. const uint8_t * m = sc + 2;
  6571. #pragma unroll
  6572. for (int i = 0; i < QR5_K; ++i) {
  6573. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  6574. d8[i] = bq8i->ds[0];
  6575. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  6576. u[2*i+0] = q8[0];
  6577. u[2*i+1] = q8[4];
  6578. }
  6579. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  6580. #else
  6581. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  6582. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  6583. const int8_t * s = bq5_K->scales;
  6584. const float d = bq5_K->d;
  6585. const float d8_1 = bq8_1[0].ds[0];
  6586. const float d8_2 = bq8_1[1].ds[1];
  6587. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  6588. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  6589. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  6590. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  6591. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  6592. const int vl1 = ql[0];
  6593. const int vl2 = ql[4];
  6594. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  6595. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  6596. const int in = step%8; // 0, 4, 0, 4
  6597. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  6598. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  6599. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  6600. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  6601. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  6602. const float sumf_d = d8_1 * (dpct::dp4a(ui1, v1, 0) * s[0] + dpct::dp4a(ui2, v2, 0) * s[1])
  6603. + d8_2 * (dpct::dp4a(ui3, v3, 0) * s[2] + dpct::dp4a(ui4, v4, 0) * s[3]);
  6604. return d * sumf_d;
  6605. #else
  6606. bad_arch();
  6607. #endif // __SYCL_ARCH__ >= VER_4VEC
  6608. #endif
  6609. }
  6610. template <int mmq_y>
  6611. static __dpct_inline__ void
  6612. allocate_tiles_q5_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  6613. int *tile_x_ql_q5_K, sycl::half2 *tile_x_dm_q5_K,
  6614. int *tile_x_sc_q5_K) {
  6615. (void)x_qh;
  6616. *x_ql = tile_x_ql_q5_K;
  6617. *x_dm = tile_x_dm_q5_K;
  6618. *x_sc = tile_x_sc_q5_K;
  6619. }
  6620. template <int mmq_y, int nwarps, bool need_check>
  6621. static __dpct_inline__ void
  6622. load_tiles_q5_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  6623. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6624. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6625. const int &k, const int &blocks_per_row) {
  6626. (void)x_qh;
  6627. GGML_SYCL_ASSUME(i_offset >= 0);
  6628. GGML_SYCL_ASSUME(i_offset < nwarps);
  6629. GGML_SYCL_ASSUME(k >= 0);
  6630. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6631. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  6632. const int kqsx = k % QI5_K; // == k if QK_K == 256
  6633. const block_q5_K * bx0 = (const block_q5_K *) vx;
  6634. #pragma unroll
  6635. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6636. int i = i0 + i_offset;
  6637. if (need_check) {
  6638. i = sycl::min(i, i_max);
  6639. }
  6640. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  6641. const int ky = QR5_K*kqsx;
  6642. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  6643. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  6644. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  6645. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  6646. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  6647. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  6648. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  6649. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  6650. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  6651. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  6652. }
  6653. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  6654. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  6655. #pragma unroll
  6656. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  6657. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  6658. if (need_check) {
  6659. i = sycl::min(i, i_max);
  6660. }
  6661. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  6662. #if QK_K == 256
  6663. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  6664. #endif
  6665. }
  6666. #pragma unroll
  6667. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  6668. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  6669. if (need_check) {
  6670. i = sycl::min(i, i_max);
  6671. }
  6672. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  6673. const int * scales = (const int *) bxi->scales;
  6674. const int ksc = k % (WARP_SIZE/8);
  6675. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  6676. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  6677. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  6678. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  6679. }
  6680. }
  6681. static __dpct_inline__ float vec_dot_q5_K_q8_1_mul_mat(
  6682. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6683. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6684. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6685. const int &i, const int &j, const int &k) {
  6686. (void)x_qh;
  6687. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  6688. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  6689. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  6690. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  6691. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  6692. }
  6693. static __dpct_inline__ float
  6694. vec_dot_q6_K_q8_1(const void *__restrict__ vbq,
  6695. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6696. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  6697. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  6698. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  6699. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  6700. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  6701. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  6702. const int8_t * scales = bq6_K->scales + scale_offset;
  6703. int u[QR6_K];
  6704. float d8[QR6_K];
  6705. #pragma unroll
  6706. for (int i = 0; i < QR6_K; ++i) {
  6707. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  6708. d8[i] = bq8_1[bq8_offset + 2 * i].ds[0];
  6709. }
  6710. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  6711. }
  6712. template <int mmq_y>
  6713. static __dpct_inline__ void
  6714. allocate_tiles_q6_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  6715. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_sc) {
  6716. (void)x_qh;
  6717. *x_ql = tile_x_ql;
  6718. *x_dm = tile_x_dm;
  6719. *x_sc = tile_x_sc;
  6720. }
  6721. template <int mmq_y, int nwarps, bool need_check>
  6722. static __dpct_inline__ void
  6723. load_tiles_q6_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  6724. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6725. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6726. const int &k, const int &blocks_per_row) {
  6727. (void)x_qh;
  6728. GGML_SYCL_ASSUME(i_offset >= 0);
  6729. GGML_SYCL_ASSUME(i_offset < nwarps);
  6730. GGML_SYCL_ASSUME(k >= 0);
  6731. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6732. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  6733. const int kqsx = k % QI6_K; // == k if QK_K == 256
  6734. const block_q6_K * bx0 = (const block_q6_K *) vx;
  6735. #pragma unroll
  6736. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6737. int i = i0 + i_offset;
  6738. if (need_check) {
  6739. i = sycl::min(i, i_max);
  6740. }
  6741. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  6742. const int ky = QR6_K*kqsx;
  6743. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  6744. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  6745. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  6746. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  6747. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  6748. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  6749. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  6750. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  6751. x_ql[i * (2 * WARP_SIZE + 1) + kq0] =
  6752. dpct::vectorized_binary<sycl::char4>(ql0 | qh0, 0x20202020,
  6753. dpct::sub_sat());
  6754. x_ql[i * (2 * WARP_SIZE + 1) + kq1] =
  6755. dpct::vectorized_binary<sycl::char4>(ql1 | qh1, 0x20202020,
  6756. dpct::sub_sat());
  6757. }
  6758. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  6759. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  6760. float * x_dmf = (float *) x_dm;
  6761. #pragma unroll
  6762. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  6763. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  6764. if (need_check) {
  6765. i = sycl::min(i, i_max);
  6766. }
  6767. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  6768. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  6769. }
  6770. #pragma unroll
  6771. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  6772. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  6773. if (need_check) {
  6774. i = sycl::min(i, i_max);
  6775. }
  6776. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  6777. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  6778. }
  6779. }
  6780. static __dpct_inline__ float vec_dot_q6_K_q8_1_mul_mat(
  6781. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6782. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6783. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6784. const int &i, const int &j, const int &k) {
  6785. (void)x_qh;
  6786. const float * x_dmf = (const float *) x_dm;
  6787. const float * y_df = (const float *) y_ds;
  6788. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  6789. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  6790. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  6791. 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]);
  6792. }
  6793. static __dpct_inline__ float
  6794. vec_dot_iq2_xxs_q8_1(const void *__restrict__ vbq,
  6795. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6796. const uint64_t *iq2xxs_grid, const uint8_t *ksigns_iq2xs,
  6797. const uint8_t *kmask_iq2xs) {
  6798. #if QK_K == 256
  6799. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  6800. #if QR2_XXS == 8
  6801. const int ib32 = iqs;
  6802. const uint16_t * q2 = bq2->qs + 4*ib32;
  6803. const uint8_t * aux8 = (const uint8_t *)q2;
  6804. const int8_t * q8 = bq8_1[ib32].qs;
  6805. uint32_t aux32 = q2[2] | (q2[3] << 16);
  6806. int sumi = 0;
  6807. for (int l = 0; l < 4; ++l) {
  6808. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6809. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  6810. for (int j = 0; j < 8; ++j) {
  6811. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6812. }
  6813. q8 += 8;
  6814. aux32 >>= 7;
  6815. }
  6816. const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.25f;
  6817. return d * sumi;
  6818. #else
  6819. // iqs is 0...15
  6820. const int ib32 = iqs/2;
  6821. const int il = iqs%2;
  6822. const uint16_t * q2 = bq2->qs + 4*ib32;
  6823. const uint8_t * aux8 = (const uint8_t *)q2;
  6824. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  6825. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  6826. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  6827. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * bq8_1[ib32].ds[0] * 0.25f;
  6828. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  6829. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  6830. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  6831. int sumi1 = 0, sumi2 = 0;
  6832. for (int j = 0; j < 8; ++j) {
  6833. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  6834. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  6835. }
  6836. return d * (sumi1 + sumi2);
  6837. #endif
  6838. #else
  6839. assert(false);
  6840. return 0.f;
  6841. #endif
  6842. }
  6843. static __dpct_inline__ float
  6844. vec_dot_iq2_xs_q8_1(const void *__restrict__ vbq,
  6845. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6846. const uint64_t *iq2xs_grid, const uint64_t *ksigns64) {
  6847. #if DPCT_COMPATIBILITY_TEMP >= \
  6848. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6849. #if QK_K == 256
  6850. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  6851. const int ib32 = iqs;
  6852. const uint16_t * q2 = bq2->qs + 4*ib32;
  6853. const int8_t * q8 = bq8_1[ib32].qs;
  6854. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  6855. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  6856. int sumi1 = 0;
  6857. for (int l = 0; l < 2; ++l) {
  6858. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  6859. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  6860. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6861. grid[0] ^ signs[0], signs[0], std::minus<>());
  6862. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6863. grid[1] ^ signs[1], signs[1], std::minus<>());
  6864. sumi1 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  6865. sumi1 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  6866. q8 += 8;
  6867. }
  6868. int sumi2 = 0;
  6869. for (int l = 2; l < 4; ++l) {
  6870. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  6871. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  6872. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6873. grid[0] ^ signs[0], signs[0], std::minus<>());
  6874. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6875. grid[1] ^ signs[1], signs[1], std::minus<>());
  6876. sumi2 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  6877. sumi2 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  6878. q8 += 8;
  6879. }
  6880. const float d = (float)bq2->d * bq8_1[ib32].ds[0] * 0.25f;
  6881. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  6882. #else
  6883. assert(false);
  6884. return 0.f;
  6885. #endif
  6886. #else
  6887. assert(false);
  6888. return 0.f;
  6889. #endif
  6890. }
  6891. static __dpct_inline__ float
  6892. vec_dot_iq3_xxs_q8_1(const void *__restrict__ vbq,
  6893. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6894. const uint32_t *iq3xxs_grid, const uint64_t *ksigns64) {
  6895. #if DPCT_COMPATIBILITY_TEMP >= \
  6896. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6897. #if QK_K == 256
  6898. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  6899. const int ib32 = iqs;
  6900. const uint8_t * q3 = bq2->qs + 8*ib32;
  6901. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  6902. const int8_t * q8 = bq8_1[ib32].qs;
  6903. uint32_t aux32 = gas[0] | (gas[1] << 16);
  6904. int sumi = 0;
  6905. for (int l = 0; l < 4; ++l) {
  6906. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  6907. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  6908. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  6909. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6910. grid1[0] ^ signs[0], signs[0], std::minus<>());
  6911. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6912. grid2[0] ^ signs[1], signs[1], std::minus<>());
  6913. sumi = dpct::dp4a(grid_l, *((int *)q8 + 0), sumi);
  6914. sumi = dpct::dp4a(grid_h, *((int *)q8 + 1), sumi);
  6915. q8 += 8;
  6916. aux32 >>= 7;
  6917. }
  6918. const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.5f;
  6919. return d * sumi;
  6920. #else
  6921. assert(false);
  6922. return 0.f;
  6923. #endif
  6924. #else
  6925. assert(false);
  6926. return 0.f;
  6927. #endif
  6928. }
  6929. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x,
  6930. int mmq_y, int nwarps, load_tiles_sycl_t load_tiles, int vdr,
  6931. vec_dot_q_mul_mat_sycl_t vec_dot>
  6932. /*
  6933. DPCT1110:8: The total declared local variable size in device function mul_mat_q
  6934. exceeds 128 bytes and may cause high register pressure. Consult with your
  6935. hardware vendor to find the total register size available and adjust the code,
  6936. or use smaller sub-group size to avoid high register pressure.
  6937. */
  6938. static __dpct_inline__ void
  6939. mul_mat_q(const void *__restrict__ vx, const void *__restrict__ vy,
  6940. float *__restrict__ dst, const int ncols_x, const int nrows_x,
  6941. const int ncols_y, const int nrows_y, const int nrows_dst,
  6942. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_qh,
  6943. int *tile_x_sc, const sycl::nd_item<3> &item_ct1, int *tile_y_qs,
  6944. sycl::half2 *tile_y_ds) {
  6945. const block_q_t * x = (const block_q_t *) vx;
  6946. const block_q8_1 * y = (const block_q8_1 *) vy;
  6947. const int blocks_per_row_x = ncols_x / qk;
  6948. const int blocks_per_col_y = nrows_y / QK8_1;
  6949. const int blocks_per_warp = WARP_SIZE / qi;
  6950. const int & ncols_dst = ncols_y;
  6951. const int row_dst_0 = item_ct1.get_group(2) * mmq_y;
  6952. const int & row_x_0 = row_dst_0;
  6953. const int col_dst_0 = item_ct1.get_group(1) * mmq_x;
  6954. const int & col_y_0 = col_dst_0;
  6955. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  6956. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  6957. load_tiles(x + row_x_0 * blocks_per_row_x + ib0, tile_x_ql, tile_x_dm,
  6958. tile_x_qh, tile_x_sc, item_ct1.get_local_id(1),
  6959. nrows_x - row_x_0 - 1, item_ct1.get_local_id(2),
  6960. blocks_per_row_x);
  6961. #pragma unroll
  6962. for (int ir = 0; ir < qr; ++ir) {
  6963. const int kqs = ir * WARP_SIZE + item_ct1.get_local_id(2);
  6964. const int kbxd = kqs / QI8_1;
  6965. #pragma unroll
  6966. for (int i = 0; i < mmq_x; i += nwarps) {
  6967. const int col_y_eff = dpct::min(
  6968. (unsigned int)(col_y_0 + item_ct1.get_local_id(1) + i),
  6969. ncols_y - 1); // to prevent out-of-bounds memory accesses
  6970. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  6971. const int index_y = (item_ct1.get_local_id(1) + i) * WARP_SIZE +
  6972. kqs % WARP_SIZE;
  6973. tile_y_qs[index_y] = get_int_from_int8_aligned(
  6974. by0->qs, item_ct1.get_local_id(2) % QI8_1);
  6975. }
  6976. #pragma unroll
  6977. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  6978. const int ids =
  6979. (ids0 + item_ct1.get_local_id(1) * QI8_1 +
  6980. item_ct1.get_local_id(2) / (WARP_SIZE / QI8_1)) %
  6981. mmq_x;
  6982. const int kby = item_ct1.get_local_id(2) % (WARP_SIZE / QI8_1);
  6983. const int col_y_eff = sycl::min(col_y_0 + ids, ncols_y - 1);
  6984. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  6985. const sycl::half2 *dsi_src =
  6986. &y[col_y_eff * blocks_per_col_y + ib0 * (qk / QK8_1) +
  6987. ir * (WARP_SIZE / QI8_1) + kby]
  6988. .ds;
  6989. sycl::half2 *dsi_dst =
  6990. &tile_y_ds[ids * (WARP_SIZE / QI8_1) + kby];
  6991. if (need_sum) {
  6992. *dsi_dst = *dsi_src;
  6993. } else {
  6994. float * dfi_dst = (float *) dsi_dst;
  6995. *dfi_dst = (*dsi_src)[0];
  6996. }
  6997. }
  6998. /*
  6999. DPCT1118:9: SYCL group functions and algorithms must be encountered
  7000. in converged control flow. You may need to adjust the code.
  7001. */
  7002. /*
  7003. DPCT1065:56: Consider replacing sycl::nd_item::barrier() with
  7004. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  7005. better performance if there is no access to global memory.
  7006. */
  7007. item_ct1.barrier();
  7008. // #pragma unroll // unrolling this loop causes too much register pressure
  7009. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  7010. #pragma unroll
  7011. for (int j = 0; j < mmq_x; j += nwarps) {
  7012. #pragma unroll
  7013. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  7014. sum[i / WARP_SIZE][j / nwarps] += vec_dot(
  7015. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  7016. tile_y_qs, tile_y_ds, item_ct1.get_local_id(2) + i,
  7017. item_ct1.get_local_id(1) + j, k);
  7018. }
  7019. }
  7020. }
  7021. /*
  7022. DPCT1118:10: SYCL group functions and algorithms must be encountered
  7023. in converged control flow. You may need to adjust the code.
  7024. */
  7025. /*
  7026. DPCT1065:57: Consider replacing sycl::nd_item::barrier() with
  7027. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  7028. better performance if there is no access to global memory.
  7029. */
  7030. item_ct1.barrier();
  7031. }
  7032. }
  7033. #pragma unroll
  7034. for (int j = 0; j < mmq_x; j += nwarps) {
  7035. const int col_dst = col_dst_0 + j + item_ct1.get_local_id(1);
  7036. if (col_dst >= ncols_dst) {
  7037. return;
  7038. }
  7039. #pragma unroll
  7040. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  7041. const int row_dst = row_dst_0 + item_ct1.get_local_id(2) + i;
  7042. if (row_dst >= nrows_dst) {
  7043. continue;
  7044. }
  7045. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  7046. }
  7047. }
  7048. }
  7049. #define MMQ_X_Q4_0_RDNA2 64
  7050. #define MMQ_Y_Q4_0_RDNA2 128
  7051. #define NWARPS_Q4_0_RDNA2 8
  7052. #define MMQ_X_Q4_0_RDNA1 64
  7053. #define MMQ_Y_Q4_0_RDNA1 64
  7054. #define NWARPS_Q4_0_RDNA1 8
  7055. #if defined(SYCL_USE_XMX)
  7056. #define MMQ_X_Q4_0_AMPERE 4
  7057. #define MMQ_Y_Q4_0_AMPERE 32
  7058. #define NWARPS_Q4_0_AMPERE 4
  7059. #else
  7060. #define MMQ_X_Q4_0_AMPERE 64
  7061. #define MMQ_Y_Q4_0_AMPERE 128
  7062. #define NWARPS_Q4_0_AMPERE 4
  7063. #endif
  7064. #define MMQ_X_Q4_0_PASCAL 64
  7065. #define MMQ_Y_Q4_0_PASCAL 64
  7066. #define NWARPS_Q4_0_PASCAL 8
  7067. template <bool need_check> static void
  7068. mul_mat_q4_0(
  7069. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7070. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7071. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_0, float *tile_x_d_q4_0,
  7072. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  7073. int * tile_x_ql = nullptr;
  7074. sycl::half2 *tile_x_dm = nullptr;
  7075. int * tile_x_qh = nullptr;
  7076. int * tile_x_sc = nullptr;
  7077. //sycl_todo: change according to hardware
  7078. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  7079. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  7080. const int nwarps = NWARPS_Q4_0_AMPERE;
  7081. allocate_tiles_q4_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7082. tile_x_qs_q4_0, tile_x_d_q4_0);
  7083. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps,
  7084. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ,
  7085. vec_dot_q4_0_q8_1_mul_mat>(
  7086. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7087. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7088. }
  7089. #define MMQ_X_Q4_1_RDNA2 64
  7090. #define MMQ_Y_Q4_1_RDNA2 128
  7091. #define NWARPS_Q4_1_RDNA2 8
  7092. #define MMQ_X_Q4_1_RDNA1 64
  7093. #define MMQ_Y_Q4_1_RDNA1 64
  7094. #define NWARPS_Q4_1_RDNA1 8
  7095. #if defined(SYCL_USE_XMX)
  7096. #define MMQ_X_Q4_1_AMPERE 4
  7097. #define MMQ_Y_Q4_1_AMPERE 32
  7098. #define NWARPS_Q4_1_AMPERE 4
  7099. #else
  7100. #define MMQ_X_Q4_1_AMPERE 64
  7101. #define MMQ_Y_Q4_1_AMPERE 128
  7102. #define NWARPS_Q4_1_AMPERE 4
  7103. #endif
  7104. #define MMQ_X_Q4_1_PASCAL 64
  7105. #define MMQ_Y_Q4_1_PASCAL 64
  7106. #define NWARPS_Q4_1_PASCAL 8
  7107. template <bool need_check> static void
  7108. mul_mat_q4_1(
  7109. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7110. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7111. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_1,
  7112. sycl::half2 *tile_x_dm_q4_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  7113. int * tile_x_ql = nullptr;
  7114. sycl::half2 *tile_x_dm = nullptr;
  7115. int * tile_x_qh = nullptr;
  7116. int * tile_x_sc = nullptr;
  7117. //sycl_todo: change according to hardware
  7118. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  7119. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  7120. const int nwarps = NWARPS_Q4_1_AMPERE;
  7121. allocate_tiles_q4_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7122. tile_x_qs_q4_1, tile_x_dm_q4_1);
  7123. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps,
  7124. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ,
  7125. vec_dot_q4_1_q8_1_mul_mat>(
  7126. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7127. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7128. }
  7129. #define MMQ_X_Q5_0_RDNA2 64
  7130. #define MMQ_Y_Q5_0_RDNA2 128
  7131. #define NWARPS_Q5_0_RDNA2 8
  7132. #define MMQ_X_Q5_0_RDNA1 64
  7133. #define MMQ_Y_Q5_0_RDNA1 64
  7134. #define NWARPS_Q5_0_RDNA1 8
  7135. #if defined(SYCL_USE_XMX)
  7136. #define MMQ_X_Q5_0_AMPERE 4
  7137. #define MMQ_Y_Q5_0_AMPERE 32
  7138. #define NWARPS_Q5_0_AMPERE 4
  7139. #else
  7140. #define MMQ_X_Q5_0_AMPERE 128
  7141. #define MMQ_Y_Q5_0_AMPERE 64
  7142. #define NWARPS_Q5_0_AMPERE 4
  7143. #endif
  7144. #define MMQ_X_Q5_0_PASCAL 64
  7145. #define MMQ_Y_Q5_0_PASCAL 64
  7146. #define NWARPS_Q5_0_PASCAL 8
  7147. template <bool need_check> static void
  7148. mul_mat_q5_0(
  7149. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7150. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7151. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_0, float *tile_x_d_q5_0,
  7152. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  7153. int * tile_x_ql = nullptr;
  7154. sycl::half2 *tile_x_dm = nullptr;
  7155. int * tile_x_qh = nullptr;
  7156. int * tile_x_sc = nullptr;
  7157. //sycl_todo: change according to hardware
  7158. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  7159. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  7160. const int nwarps = NWARPS_Q5_0_AMPERE;
  7161. allocate_tiles_q5_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7162. tile_x_ql_q5_0, tile_x_d_q5_0);
  7163. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps,
  7164. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ,
  7165. vec_dot_q5_0_q8_1_mul_mat>(
  7166. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7167. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7168. }
  7169. #define MMQ_X_Q5_1_RDNA2 64
  7170. #define MMQ_Y_Q5_1_RDNA2 128
  7171. #define NWARPS_Q5_1_RDNA2 8
  7172. #define MMQ_X_Q5_1_RDNA1 64
  7173. #define MMQ_Y_Q5_1_RDNA1 64
  7174. #define NWARPS_Q5_1_RDNA1 8
  7175. #if defined(SYCL_USE_XMX)
  7176. #define MMQ_X_Q5_1_AMPERE 4
  7177. #define MMQ_Y_Q5_1_AMPERE 32
  7178. #define NWARPS_Q5_1_AMPERE 4
  7179. #else
  7180. #define MMQ_X_Q5_1_AMPERE 128
  7181. #define MMQ_Y_Q5_1_AMPERE 64
  7182. #define NWARPS_Q5_1_AMPERE 4
  7183. #endif
  7184. #define MMQ_X_Q5_1_PASCAL 64
  7185. #define MMQ_Y_Q5_1_PASCAL 64
  7186. #define NWARPS_Q5_1_PASCAL 8
  7187. template <bool need_check> static void
  7188. mul_mat_q5_1(
  7189. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7190. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7191. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_1,
  7192. sycl::half2 *tile_x_dm_q5_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  7193. int * tile_x_ql = nullptr;
  7194. sycl::half2 *tile_x_dm = nullptr;
  7195. int * tile_x_qh = nullptr;
  7196. int * tile_x_sc = nullptr;
  7197. //sycl_todo: change according to hardware
  7198. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  7199. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  7200. const int nwarps = NWARPS_Q5_1_AMPERE;
  7201. allocate_tiles_q5_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7202. tile_x_ql_q5_1, tile_x_dm_q5_1);
  7203. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps,
  7204. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ,
  7205. vec_dot_q5_1_q8_1_mul_mat>(
  7206. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7207. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7208. }
  7209. #define MMQ_X_Q8_0_RDNA2 64
  7210. #define MMQ_Y_Q8_0_RDNA2 128
  7211. #define NWARPS_Q8_0_RDNA2 8
  7212. #define MMQ_X_Q8_0_RDNA1 64
  7213. #define MMQ_Y_Q8_0_RDNA1 64
  7214. #define NWARPS_Q8_0_RDNA1 8
  7215. #if defined(SYCL_USE_XMX)
  7216. #define MMQ_X_Q8_0_AMPERE 4
  7217. #define MMQ_Y_Q8_0_AMPERE 32
  7218. #define NWARPS_Q8_0_AMPERE 4
  7219. #else
  7220. #define MMQ_X_Q8_0_AMPERE 128
  7221. #define MMQ_Y_Q8_0_AMPERE 64
  7222. #define NWARPS_Q8_0_AMPERE 4
  7223. #endif
  7224. #define MMQ_X_Q8_0_PASCAL 64
  7225. #define MMQ_Y_Q8_0_PASCAL 64
  7226. #define NWARPS_Q8_0_PASCAL 8
  7227. template <bool need_check> static void
  7228. mul_mat_q8_0(
  7229. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7230. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7231. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q8_0, float *tile_x_d_q8_0,
  7232. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  7233. int * tile_x_ql = nullptr;
  7234. sycl::half2 *tile_x_dm = nullptr;
  7235. int * tile_x_qh = nullptr;
  7236. int * tile_x_sc = nullptr;
  7237. //sycl_todo: change according to hardware
  7238. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  7239. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  7240. const int nwarps = NWARPS_Q8_0_AMPERE;
  7241. allocate_tiles_q8_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7242. tile_x_qs_q8_0, tile_x_d_q8_0);
  7243. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps,
  7244. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ,
  7245. vec_dot_q8_0_q8_1_mul_mat>(
  7246. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7247. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7248. }
  7249. #define MMQ_X_Q2_K_RDNA2 64
  7250. #define MMQ_Y_Q2_K_RDNA2 128
  7251. #define NWARPS_Q2_K_RDNA2 8
  7252. #define MMQ_X_Q2_K_RDNA1 128
  7253. #define MMQ_Y_Q2_K_RDNA1 32
  7254. #define NWARPS_Q2_K_RDNA1 8
  7255. #if defined(SYCL_USE_XMX)
  7256. #define MMQ_X_Q2_K_AMPERE 4
  7257. #define MMQ_Y_Q2_K_AMPERE 32
  7258. #define NWARPS_Q2_K_AMPERE 4
  7259. #else
  7260. #define MMQ_X_Q2_K_AMPERE 64
  7261. #define MMQ_Y_Q2_K_AMPERE 128
  7262. #define NWARPS_Q2_K_AMPERE 4
  7263. #endif
  7264. #define MMQ_X_Q2_K_PASCAL 64
  7265. #define MMQ_Y_Q2_K_PASCAL 64
  7266. #define NWARPS_Q2_K_PASCAL 8
  7267. template <bool need_check> static void
  7268. mul_mat_q2_K(
  7269. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7270. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7271. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q2_K,
  7272. sycl::half2 *tile_x_dm_q2_K, int *tile_x_sc_q2_K, int *tile_y_qs,
  7273. sycl::half2 *tile_y_ds) {
  7274. int * tile_x_ql = nullptr;
  7275. sycl::half2 *tile_x_dm = nullptr;
  7276. int * tile_x_qh = nullptr;
  7277. int * tile_x_sc = nullptr;
  7278. //sycl_todo: change according to hardware
  7279. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  7280. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  7281. const int nwarps = NWARPS_Q2_K_AMPERE;
  7282. allocate_tiles_q2_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7283. tile_x_ql_q2_K, tile_x_dm_q2_K, tile_x_sc_q2_K);
  7284. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps,
  7285. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ,
  7286. vec_dot_q2_K_q8_1_mul_mat>(
  7287. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7288. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7289. }
  7290. #define MMQ_X_Q3_K_RDNA2 128
  7291. #define MMQ_Y_Q3_K_RDNA2 64
  7292. #define NWARPS_Q3_K_RDNA2 8
  7293. #define MMQ_X_Q3_K_RDNA1 32
  7294. #define MMQ_Y_Q3_K_RDNA1 128
  7295. #define NWARPS_Q3_K_RDNA1 8
  7296. #if defined(SYCL_USE_XMX)
  7297. #define MMQ_X_Q3_K_AMPERE 4
  7298. #define MMQ_Y_Q3_K_AMPERE 32
  7299. #define NWARPS_Q3_K_AMPERE 4
  7300. #else
  7301. #define MMQ_X_Q3_K_AMPERE 128
  7302. #define MMQ_Y_Q3_K_AMPERE 128
  7303. #define NWARPS_Q3_K_AMPERE 4
  7304. #endif
  7305. #define MMQ_X_Q3_K_PASCAL 64
  7306. #define MMQ_Y_Q3_K_PASCAL 64
  7307. #define NWARPS_Q3_K_PASCAL 8
  7308. template <bool need_check> static void
  7309. mul_mat_q3_K(
  7310. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7311. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7312. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q3_K,
  7313. sycl::half2 *tile_x_dm_q3_K, int *tile_x_qh_q3_K, int *tile_x_sc_q3_K,
  7314. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  7315. int * tile_x_ql = nullptr;
  7316. sycl::half2 *tile_x_dm = nullptr;
  7317. int * tile_x_qh = nullptr;
  7318. int * tile_x_sc = nullptr;
  7319. //sycl_todo: change according to hardware
  7320. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  7321. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  7322. const int nwarps = NWARPS_Q3_K_AMPERE;
  7323. allocate_tiles_q3_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7324. tile_x_ql_q3_K, tile_x_dm_q3_K, tile_x_qh_q3_K,
  7325. tile_x_sc_q3_K);
  7326. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps,
  7327. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ,
  7328. vec_dot_q3_K_q8_1_mul_mat>(
  7329. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7330. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7331. }
  7332. #define MMQ_X_Q4_K_RDNA2 64
  7333. #define MMQ_Y_Q4_K_RDNA2 128
  7334. #define NWARPS_Q4_K_RDNA2 8
  7335. #define MMQ_X_Q4_K_RDNA1 32
  7336. #define MMQ_Y_Q4_K_RDNA1 64
  7337. #define NWARPS_Q4_K_RDNA1 8
  7338. #if defined(SYCL_USE_XMX)
  7339. #define MMQ_X_Q4_K_AMPERE 4
  7340. #define MMQ_Y_Q4_K_AMPERE 32
  7341. #define NWARPS_Q4_K_AMPERE 4
  7342. #else
  7343. #define MMQ_X_Q4_K_AMPERE 64
  7344. #define MMQ_Y_Q4_K_AMPERE 128
  7345. #define NWARPS_Q4_K_AMPERE 4
  7346. #endif
  7347. #define MMQ_X_Q4_K_PASCAL 64
  7348. #define MMQ_Y_Q4_K_PASCAL 64
  7349. #define NWARPS_Q4_K_PASCAL 8
  7350. template <bool need_check> static void
  7351. mul_mat_q4_K(
  7352. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7353. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7354. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q4_K,
  7355. sycl::half2 *tile_x_dm_q4_K, int *tile_x_sc_q4_K, int *tile_y_qs,
  7356. sycl::half2 *tile_y_ds) {
  7357. int * tile_x_ql = nullptr;
  7358. sycl::half2 *tile_x_dm = nullptr;
  7359. int * tile_x_qh = nullptr;
  7360. int * tile_x_sc = nullptr;
  7361. //sycl_todo: change according to hardware
  7362. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  7363. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  7364. const int nwarps = NWARPS_Q4_K_AMPERE;
  7365. allocate_tiles_q4_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7366. tile_x_ql_q4_K, tile_x_dm_q4_K, tile_x_sc_q4_K);
  7367. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps,
  7368. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ,
  7369. vec_dot_q4_K_q8_1_mul_mat>(
  7370. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7371. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7372. }
  7373. #define MMQ_X_Q5_K_RDNA2 64
  7374. #define MMQ_Y_Q5_K_RDNA2 128
  7375. #define NWARPS_Q5_K_RDNA2 8
  7376. #define MMQ_X_Q5_K_RDNA1 32
  7377. #define MMQ_Y_Q5_K_RDNA1 64
  7378. #define NWARPS_Q5_K_RDNA1 8
  7379. #if defined(SYCL_USE_XMX)
  7380. #define MMQ_X_Q5_K_AMPERE 4
  7381. #define MMQ_Y_Q5_K_AMPERE 32
  7382. #define NWARPS_Q5_K_AMPERE 4
  7383. #else
  7384. #define MMQ_X_Q5_K_AMPERE 64
  7385. #define MMQ_Y_Q5_K_AMPERE 128
  7386. #define NWARPS_Q5_K_AMPERE 4
  7387. #endif
  7388. #define MMQ_X_Q5_K_PASCAL 64
  7389. #define MMQ_Y_Q5_K_PASCAL 64
  7390. #define NWARPS_Q5_K_PASCAL 8
  7391. template <bool need_check> static void
  7392. mul_mat_q5_K(
  7393. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7394. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7395. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_K,
  7396. sycl::half2 *tile_x_dm_q5_K, int *tile_x_sc_q5_K, int *tile_y_qs,
  7397. sycl::half2 *tile_y_ds) {
  7398. int * tile_x_ql = nullptr;
  7399. sycl::half2 *tile_x_dm = nullptr;
  7400. int * tile_x_qh = nullptr;
  7401. int * tile_x_sc = nullptr;
  7402. //sycl_todo: change according to hardware
  7403. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  7404. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  7405. const int nwarps = NWARPS_Q5_K_AMPERE;
  7406. allocate_tiles_q5_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7407. tile_x_ql_q5_K, tile_x_dm_q5_K, tile_x_sc_q5_K);
  7408. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps,
  7409. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ,
  7410. vec_dot_q5_K_q8_1_mul_mat>(
  7411. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7412. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7413. }
  7414. #define MMQ_X_Q6_K_RDNA2 64
  7415. #define MMQ_Y_Q6_K_RDNA2 128
  7416. #define NWARPS_Q6_K_RDNA2 8
  7417. #define MMQ_X_Q6_K_RDNA1 32
  7418. #define MMQ_Y_Q6_K_RDNA1 64
  7419. #define NWARPS_Q6_K_RDNA1 8
  7420. #if defined(SYCL_USE_XMX)
  7421. #define MMQ_X_Q6_K_AMPERE 4
  7422. #define MMQ_Y_Q6_K_AMPERE 32
  7423. #define NWARPS_Q6_K_AMPERE 4
  7424. #else
  7425. #define MMQ_X_Q6_K_AMPERE 64
  7426. #define MMQ_Y_Q6_K_AMPERE 64
  7427. #define NWARPS_Q6_K_AMPERE 4
  7428. #endif
  7429. #define MMQ_X_Q6_K_PASCAL 64
  7430. #define MMQ_Y_Q6_K_PASCAL 64
  7431. #define NWARPS_Q6_K_PASCAL 8
  7432. template <bool need_check> static void
  7433. mul_mat_q6_K(
  7434. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  7435. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  7436. const sycl::nd_item<3> &item_ct1, int *tile_x_ql, sycl::half2 *tile_x_dm,
  7437. int *tile_x_sc, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  7438. // int * tile_x_ql = nullptr;
  7439. // sycl::half2 *tile_x_dm = nullptr;
  7440. int * tile_x_qh = nullptr;
  7441. // int * tile_x_sc = nullptr;
  7442. //sycl_todo: change according to hardware
  7443. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  7444. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  7445. const int nwarps = NWARPS_Q6_K_AMPERE;
  7446. allocate_tiles_q6_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  7447. tile_x_ql, tile_x_dm, tile_x_sc);
  7448. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps,
  7449. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ,
  7450. vec_dot_q6_K_q8_1_mul_mat>(
  7451. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  7452. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  7453. }
  7454. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl>
  7455. static void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  7456. const sycl::nd_item<3> &item_ct1,
  7457. const uint32_t *iq3xxs_grid_ptr, const uint64_t *ksigns64_ptr) {
  7458. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7459. item_ct1.get_local_id(1);
  7460. if (row >= nrows) {
  7461. return;
  7462. }
  7463. const int blocks_per_row = ncols / qk;
  7464. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7465. // partial sum for each thread
  7466. float tmp = 0.0f;
  7467. const block_q_t * x = (const block_q_t *) vx;
  7468. const block_q8_1 * y = (const block_q8_1 *) vy;
  7469. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7470. i += blocks_per_warp) {
  7471. const int ibx = row*blocks_per_row + i; // x block index
  7472. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7473. const int iqs =
  7474. vdr *
  7475. (item_ct1.get_local_id(2) %
  7476. (qi / vdr)); // x block quant index when casting the quants to int
  7477. tmp += vec_dot_q_sycl(&x[ibx], &y[iby], iqs);
  7478. }
  7479. // sum up partial sums and write back result
  7480. #pragma unroll
  7481. for (int mask = 16; mask > 0; mask >>= 1) {
  7482. tmp +=
  7483. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7484. }
  7485. if (item_ct1.get_local_id(2) == 0) {
  7486. dst[row] = tmp;
  7487. }
  7488. }
  7489. template <int qk, int qi, typename block_q_t, int vdr>
  7490. static void mul_mat_vec_q_iq2_xxs_q8_1(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  7491. const sycl::nd_item<3> &item_ct1,
  7492. const uint64_t *iq2xxs_grid_ptr, const uint8_t *ksigns_iq2xs_ptr,
  7493. const uint8_t *kmask_iq2xs_ptr ) {
  7494. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7495. item_ct1.get_local_id(1);
  7496. if (row >= nrows) {
  7497. return;
  7498. }
  7499. const int blocks_per_row = ncols / qk;
  7500. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7501. // partial sum for each thread
  7502. float tmp = 0.0f;
  7503. const block_q_t * x = (const block_q_t *) vx;
  7504. const block_q8_1 * y = (const block_q8_1 *) vy;
  7505. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7506. i += blocks_per_warp) {
  7507. const int ibx = row*blocks_per_row + i; // x block index
  7508. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7509. const int iqs =
  7510. vdr *
  7511. (item_ct1.get_local_id(2) %
  7512. (qi / vdr)); // x block quant index when casting the quants to int
  7513. tmp += vec_dot_iq2_xxs_q8_1(&x[ibx], &y[iby], iqs, iq2xxs_grid_ptr, ksigns_iq2xs_ptr, kmask_iq2xs_ptr);
  7514. }
  7515. // sum up partial sums and write back result
  7516. #pragma unroll
  7517. for (int mask = 16; mask > 0; mask >>= 1) {
  7518. tmp +=
  7519. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7520. }
  7521. if (item_ct1.get_local_id(2) == 0) {
  7522. dst[row] = tmp;
  7523. }
  7524. }
  7525. template <int qk, int qi, typename block_q_t, int vdr>
  7526. static void mul_mat_vec_q_iq2_xs_q8_1(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  7527. const sycl::nd_item<3> &item_ct1,
  7528. const uint64_t *iq2xs_grid_ptr, const uint64_t *ksigns64_ptr ) {
  7529. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7530. item_ct1.get_local_id(1);
  7531. if (row >= nrows) {
  7532. return;
  7533. }
  7534. const int blocks_per_row = ncols / qk;
  7535. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7536. // partial sum for each thread
  7537. float tmp = 0.0f;
  7538. const block_q_t * x = (const block_q_t *) vx;
  7539. const block_q8_1 * y = (const block_q8_1 *) vy;
  7540. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7541. i += blocks_per_warp) {
  7542. const int ibx = row*blocks_per_row + i; // x block index
  7543. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7544. const int iqs =
  7545. vdr *
  7546. (item_ct1.get_local_id(2) %
  7547. (qi / vdr)); // x block quant index when casting the quants to int
  7548. tmp += vec_dot_iq2_xs_q8_1(&x[ibx], &y[iby], iqs, iq2xs_grid_ptr, ksigns64_ptr);
  7549. }
  7550. // sum up partial sums and write back result
  7551. #pragma unroll
  7552. for (int mask = 16; mask > 0; mask >>= 1) {
  7553. tmp +=
  7554. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7555. }
  7556. if (item_ct1.get_local_id(2) == 0) {
  7557. dst[row] = tmp;
  7558. }
  7559. }
  7560. template <int qk, int qi, typename block_q_t, int vdr>
  7561. static void mul_mat_vec_q_iq3_xxs_q8_1(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  7562. const sycl::nd_item<3> &item_ct1,
  7563. const uint32_t *iq3xxs_grid_ptr, const uint64_t *ksigns64_ptr ) {
  7564. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7565. item_ct1.get_local_id(1);
  7566. if (row >= nrows) {
  7567. return;
  7568. }
  7569. const int blocks_per_row = ncols / qk;
  7570. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7571. // partial sum for each thread
  7572. float tmp = 0.0f;
  7573. const block_q_t * x = (const block_q_t *) vx;
  7574. const block_q8_1 * y = (const block_q8_1 *) vy;
  7575. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7576. i += blocks_per_warp) {
  7577. const int ibx = row*blocks_per_row + i; // x block index
  7578. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7579. const int iqs =
  7580. vdr *
  7581. (item_ct1.get_local_id(2) %
  7582. (qi / vdr)); // x block quant index when casting the quants to int
  7583. tmp += vec_dot_iq3_xxs_q8_1(&x[ibx], &y[iby], iqs, iq3xxs_grid_ptr, ksigns64_ptr);
  7584. }
  7585. // sum up partial sums and write back result
  7586. #pragma unroll
  7587. for (int mask = 16; mask > 0; mask >>= 1) {
  7588. tmp +=
  7589. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7590. }
  7591. if (item_ct1.get_local_id(2) == 0) {
  7592. dst[row] = tmp;
  7593. }
  7594. }
  7595. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  7596. static void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows,
  7597. const sycl::nd_item<3> &item_ct1) {
  7598. // qk = quantized weights per x block
  7599. // qr = number of quantized weights per data value in x block
  7600. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7601. item_ct1.get_local_id(1);
  7602. if (row >= nrows) {
  7603. return;
  7604. }
  7605. const int tid = item_ct1.get_local_id(2);
  7606. const int iter_stride = 2*GGML_SYCL_DMMV_X;
  7607. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  7608. const int y_offset = qr == 1 ? 1 : qk/2;
  7609. // partial sum for each thread
  7610. #ifdef GGML_SYCL_F16
  7611. sycl::half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  7612. #else
  7613. float tmp = 0.0f;
  7614. #endif // GGML_SYCL_F16
  7615. for (int i = 0; i < ncols; i += iter_stride) {
  7616. const int col = i + vals_per_iter*tid;
  7617. const int ib = (row*ncols + col)/qk; // x block index
  7618. const int iqs = (col%qk)/qr; // x quant index
  7619. const int iybs = col - col%qk; // y block start index
  7620. // processing >2 values per i iter is faster for fast GPUs
  7621. #pragma unroll
  7622. for (int j = 0; j < vals_per_iter; j += 2) {
  7623. // process 2 vals per j iter
  7624. // dequantize
  7625. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  7626. dfloat2 v;
  7627. dequantize_kernel(vx, ib, iqs + j/qr, v);
  7628. // matrix multiplication
  7629. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  7630. #ifdef GGML_SYCL_F16
  7631. dfloat2 t1{y[iybs + iqs + j / qr + 0],
  7632. y[iybs + iqs + j / qr + y_offset]};
  7633. tmp += v * t1;
  7634. #else
  7635. tmp += v.x() * y[iybs + iqs + j / qr + 0];
  7636. tmp += v.y() * y[iybs + iqs + j / qr + y_offset];
  7637. #endif // GGML_SYCL_F16
  7638. }
  7639. }
  7640. // sum up partial sums and write back result
  7641. #pragma unroll
  7642. for (int mask = 16; mask > 0; mask >>= 1) {
  7643. tmp +=
  7644. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7645. }
  7646. if (tid == 0) {
  7647. #ifdef GGML_SYCL_F16
  7648. dst[row] = tmp.x() + tmp.y();
  7649. #else
  7650. dst[row] = tmp;
  7651. #endif // GGML_SYCL_F16
  7652. }
  7653. }
  7654. static void mul_mat_p021_f16_f32(
  7655. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  7656. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y,
  7657. const sycl::nd_item<3> &item_ct1) {
  7658. const sycl::half *x = (const sycl::half *)vx;
  7659. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7660. item_ct1.get_local_id(1);
  7661. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  7662. item_ct1.get_local_id(0);
  7663. const int channel_x = channel / (nchannels_y / nchannels_x);
  7664. const int nrows_y = ncols_x;
  7665. const int nrows_dst = nrows_x;
  7666. const int row_dst = row_x;
  7667. float tmp = 0.0f;
  7668. for (int col_x0 = 0; col_x0 < ncols_x;
  7669. col_x0 += item_ct1.get_local_range(2)) {
  7670. const int col_x = col_x0 + item_ct1.get_local_id(2);
  7671. if (col_x >= ncols_x) {
  7672. break;
  7673. }
  7674. // x is transposed and permuted
  7675. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  7676. const float xi =
  7677. sycl::vec<sycl::half, 1>(x[ix])
  7678. .convert<float, sycl::rounding_mode::automatic>()[0];
  7679. const int row_y = col_x;
  7680. // y is not transposed but permuted
  7681. const int iy = channel*nrows_y + row_y;
  7682. tmp += xi * y[iy];
  7683. }
  7684. // dst is not transposed and not permuted
  7685. const int idst = channel*nrows_dst + row_dst;
  7686. // sum up partial sums and write back result
  7687. #pragma unroll
  7688. for (int mask = 16; mask > 0; mask >>= 1) {
  7689. tmp +=
  7690. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7691. }
  7692. if (item_ct1.get_local_id(2) == 0) {
  7693. dst[idst] = tmp;
  7694. }
  7695. }
  7696. static void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  7697. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  7698. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor,
  7699. const sycl::nd_item<3> &item_ct1) {
  7700. const sycl::half *x = (const sycl::half *)vx;
  7701. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7702. item_ct1.get_local_id(1);
  7703. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  7704. item_ct1.get_local_id(0);
  7705. const int channel_x = channel / channel_x_divisor;
  7706. const int nrows_y = ncols_x;
  7707. const int nrows_dst = nrows_x;
  7708. const int row_dst = row_x;
  7709. const int idst = channel*nrows_dst + row_dst;
  7710. float tmp = 0.0f;
  7711. for (int col_x0 = 0; col_x0 < ncols_x;
  7712. col_x0 += item_ct1.get_local_range(2)) {
  7713. const int col_x = col_x0 + item_ct1.get_local_id(2);
  7714. if (col_x >= ncols_x) {
  7715. break;
  7716. }
  7717. const int row_y = col_x;
  7718. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  7719. const int iy = channel*nrows_y + row_y;
  7720. const float xi =
  7721. sycl::vec<sycl::half, 1>(x[ix])
  7722. .convert<float, sycl::rounding_mode::automatic>()[0];
  7723. tmp += xi * y[iy];
  7724. }
  7725. // sum up partial sums and write back result
  7726. #pragma unroll
  7727. for (int mask = 16; mask > 0; mask >>= 1) {
  7728. tmp +=
  7729. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7730. }
  7731. if (item_ct1.get_local_id(2) == 0) {
  7732. dst[idst] = tmp;
  7733. }
  7734. }
  7735. static void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  7736. const float * xi = (const float *) cxi;
  7737. float * dsti = (float *) cdsti;
  7738. *dsti = *xi;
  7739. }
  7740. static void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  7741. const float * xi = (const float *) cxi;
  7742. sycl::half *dsti = (sycl::half *)cdsti;
  7743. *dsti = sycl::vec<float, 1>(*xi)
  7744. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7745. }
  7746. static void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  7747. const sycl::half *xi = (const sycl::half *)cxi;
  7748. sycl::half *dsti = (sycl::half *)cdsti;
  7749. *dsti = *xi;
  7750. }
  7751. static void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  7752. const sycl::half *xi = (const sycl::half *)cxi;
  7753. float * dsti = (float *) cdsti;
  7754. *dsti = *xi;
  7755. }
  7756. static void cpy_1_i16_i16(const char * cxi, char * cdsti) {
  7757. const int16_t *xi = (const int16_t *)cxi;
  7758. int16_t *dsti = (int16_t *)cdsti;
  7759. *dsti = *xi;
  7760. }
  7761. static void cpy_1_i32_i32(const char * cxi, char * cdsti) {
  7762. const int32_t *xi = (const int32_t *)cxi;
  7763. int32_t *dsti = (int32_t *)cdsti;
  7764. *dsti = *xi;
  7765. }
  7766. template <cpy_kernel_t cpy_1>
  7767. static void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  7768. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7769. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  7770. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  7771. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7772. item_ct1.get_local_id(2);
  7773. if (i >= ne) {
  7774. return;
  7775. }
  7776. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  7777. // then combine those indices with the corresponding byte offsets to get the total offsets
  7778. const int i03 = i/(ne00 * ne01 * ne02);
  7779. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  7780. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  7781. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  7782. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  7783. const int i13 = i/(ne10 * ne11 * ne12);
  7784. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  7785. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  7786. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  7787. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  7788. cpy_1(cx + x_offset, cdst + dst_offset);
  7789. }
  7790. static void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  7791. const float * xi = (const float *) cxi;
  7792. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  7793. float amax = 0.0f; // absolute max
  7794. for (int j = 0; j < QK8_0; j++) {
  7795. const float v = xi[j];
  7796. amax = sycl::fmax(amax, sycl::fabs((float)v));
  7797. }
  7798. const float d = amax / ((1 << 7) - 1);
  7799. const float id = d ? 1.0f/d : 0.0f;
  7800. dsti->d = d;
  7801. for (int j = 0; j < QK8_0; ++j) {
  7802. const float x0 = xi[j]*id;
  7803. dsti->qs[j] = sycl::round((float)x0);
  7804. }
  7805. }
  7806. static void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  7807. const float * xi = (const float *) cxi;
  7808. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  7809. float amax = 0.0f;
  7810. float vmax = 0.0f;
  7811. for (int j = 0; j < QK4_0; ++j) {
  7812. const float v = xi[j];
  7813. if (amax < sycl::fabs((float)v)) {
  7814. amax = sycl::fabs((float)v);
  7815. vmax = v;
  7816. }
  7817. }
  7818. const float d = vmax / -8;
  7819. const float id = d ? 1.0f/d : 0.0f;
  7820. dsti->d = d;
  7821. for (int j = 0; j < QK4_0/2; ++j) {
  7822. const float x0 = xi[0 + j]*id;
  7823. const float x1 = xi[QK4_0/2 + j]*id;
  7824. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 8.5f));
  7825. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 8.5f));
  7826. dsti->qs[j] = xi0;
  7827. dsti->qs[j] |= xi1 << 4;
  7828. }
  7829. }
  7830. static void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  7831. const float * xi = (const float *) cxi;
  7832. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  7833. float vmin = FLT_MAX;
  7834. float vmax = -FLT_MAX;
  7835. for (int j = 0; j < QK4_1; ++j) {
  7836. const float v = xi[j];
  7837. if (v < vmin) vmin = v;
  7838. if (v > vmax) vmax = v;
  7839. }
  7840. const float d = (vmax - vmin) / ((1 << 4) - 1);
  7841. const float id = d ? 1.0f/d : 0.0f;
  7842. dsti->dm.x() = d;
  7843. dsti->dm.y() = vmin;
  7844. for (int j = 0; j < QK4_1/2; ++j) {
  7845. const float x0 = (xi[0 + j] - vmin)*id;
  7846. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  7847. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 0.5f));
  7848. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 0.5f));
  7849. dsti->qs[j] = xi0;
  7850. dsti->qs[j] |= xi1 << 4;
  7851. }
  7852. }
  7853. template <cpy_kernel_t cpy_blck, int qk>
  7854. static void cpy_f32_q(const char * cx, char * cdst, const int ne,
  7855. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7856. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  7857. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  7858. const int i = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7859. item_ct1.get_local_id(2)) *
  7860. qk;
  7861. if (i >= ne) {
  7862. return;
  7863. }
  7864. const int i03 = i/(ne00 * ne01 * ne02);
  7865. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  7866. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  7867. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  7868. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  7869. const int i13 = i/(ne10 * ne11 * ne12);
  7870. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  7871. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  7872. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  7873. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  7874. cpy_blck(cx + x_offset, cdst + dst_offset);
  7875. }
  7876. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  7877. const float y = (i0 / 2 - low) / sycl::max(0.001f, high - low);
  7878. return 1.0f - sycl::min(1.0f, sycl::max(0.0f, y));
  7879. }
  7880. struct rope_corr_dims {
  7881. float v[4];
  7882. };
  7883. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  7884. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  7885. static void rope_yarn(
  7886. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  7887. float * cos_theta, float * sin_theta
  7888. ) {
  7889. // Get n-d rotational scaling corrected for extrapolation
  7890. float theta_interp = freq_scale * theta_extrap;
  7891. float theta = theta_interp;
  7892. if (ext_factor != 0.0f) {
  7893. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  7894. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  7895. // Get n-d magnitude scaling corrected for interpolation
  7896. mscale *= 1.0f + 0.1f * sycl::log(1.0f / freq_scale);
  7897. }
  7898. *cos_theta = sycl::cos(theta) * mscale;
  7899. *sin_theta = sycl::sin(theta) * mscale;
  7900. }
  7901. // rope == RoPE == rotary positional embedding
  7902. template<typename T, bool has_pos>
  7903. static void rope(
  7904. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  7905. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  7906. ,
  7907. const sycl::nd_item<3> &item_ct1) {
  7908. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7909. item_ct1.get_local_id(1));
  7910. if (col >= ncols) {
  7911. return;
  7912. }
  7913. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7914. item_ct1.get_local_id(2);
  7915. const int i = row*ncols + col;
  7916. const int i2 = row/p_delta_rows;
  7917. const int p = has_pos ? pos[i2] : 0;
  7918. const float theta_base = p * dpct::pow(freq_base, -float(col) / ncols);
  7919. float cos_theta, sin_theta;
  7920. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  7921. const float x0 = x[i + 0];
  7922. const float x1 = x[i + 1];
  7923. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7924. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  7925. }
  7926. template<typename T, bool has_pos>
  7927. static void rope_neox(
  7928. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  7929. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  7930. ,
  7931. const sycl::nd_item<3> &item_ct1) {
  7932. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7933. item_ct1.get_local_id(1));
  7934. if (col >= ncols) {
  7935. return;
  7936. }
  7937. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7938. item_ct1.get_local_id(2);
  7939. const int ib = col / n_dims;
  7940. const int ic = col % n_dims;
  7941. if (ib > 0) {
  7942. const int i = row*ncols + ib*n_dims + ic;
  7943. dst[i + 0] = x[i + 0];
  7944. dst[i + 1] = x[i + 1];
  7945. return;
  7946. }
  7947. const int i = row*ncols + ib*n_dims + ic/2;
  7948. const int i2 = row/p_delta_rows;
  7949. float cur_rot = inv_ndims * ic - ib;
  7950. const int p = has_pos ? pos[i2] : 0;
  7951. const float theta_base =
  7952. p * freq_scale * dpct::pow(theta_scale, col / 2.0f);
  7953. float cos_theta, sin_theta;
  7954. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  7955. const float x0 = x[i + 0];
  7956. const float x1 = x[i + n_dims/2];
  7957. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7958. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  7959. }
  7960. static void rope_glm_f32(
  7961. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  7962. int n_ctx
  7963. , const sycl::nd_item<3> &item_ct1) {
  7964. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7965. item_ct1.get_local_id(2);
  7966. const int half_n_dims = ncols/4;
  7967. if (col >= half_n_dims) {
  7968. return;
  7969. }
  7970. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7971. item_ct1.get_local_id(1);
  7972. const int i = row*ncols + col;
  7973. const int i2 = row/p_delta_rows;
  7974. const float col_theta_scale = dpct::pow(freq_base, -2.0f * col / ncols);
  7975. // FIXME: this is likely wrong
  7976. const int p = pos != nullptr ? pos[i2] : 0;
  7977. const float theta = sycl::min(p, n_ctx - 2) * freq_scale * col_theta_scale;
  7978. const float sin_theta = sycl::sin((float)theta);
  7979. const float cos_theta = sycl::cos((float)theta);
  7980. const float x0 = x[i + 0];
  7981. const float x1 = x[i + half_n_dims];
  7982. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7983. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  7984. const float block_theta =
  7985. ((float)sycl::max(p - n_ctx - 2, 0)) * col_theta_scale;
  7986. const float sin_block_theta = sycl::sin((float)block_theta);
  7987. const float cos_block_theta = sycl::cos((float)block_theta);
  7988. const float x2 = x[i + half_n_dims * 2];
  7989. const float x3 = x[i + half_n_dims * 3];
  7990. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  7991. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  7992. }
  7993. static void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  7994. const int n_heads_log2_floor, const float m0, const float m1,
  7995. const sycl::nd_item<3> &item_ct1) {
  7996. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7997. item_ct1.get_local_id(2);
  7998. if (col >= ncols) {
  7999. return;
  8000. }
  8001. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  8002. item_ct1.get_local_id(1);
  8003. const int i = row*ncols + col;
  8004. const int k = row/k_rows;
  8005. float m_k;
  8006. if (k < n_heads_log2_floor) {
  8007. m_k = dpct::pow(m0, k + 1);
  8008. } else {
  8009. m_k = dpct::pow(m1, 2 * (k - n_heads_log2_floor) + 1);
  8010. }
  8011. dst[i] = col * m_k + x[i];
  8012. }
  8013. static void k_sum_rows_f32(const float * x, float * dst, const int ncols,
  8014. const sycl::nd_item<3> &item_ct1) {
  8015. const int row = item_ct1.get_group(1);
  8016. const int col = item_ct1.get_local_id(2);
  8017. float sum = 0.0f;
  8018. for (int i = col; i < ncols; i += item_ct1.get_local_range(2)) {
  8019. sum += x[row * ncols + i];
  8020. }
  8021. sum = warp_reduce_sum(sum, item_ct1);
  8022. if (col == 0) {
  8023. dst[row] = sum;
  8024. }
  8025. }
  8026. template<typename T>
  8027. static inline void swap(T & a, T & b) {
  8028. T tmp = a;
  8029. a = b;
  8030. b = tmp;
  8031. }
  8032. template<ggml_sort_order order>
  8033. static void k_argsort_f32_i32(const float * x, int * dst, const int ncols,
  8034. const sycl::nd_item<3> &item_ct1) {
  8035. // bitonic sort
  8036. int col = item_ct1.get_local_id(2);
  8037. int row = item_ct1.get_group(1);
  8038. if (col >= ncols) return;
  8039. const float * x_row = x + row * ncols;
  8040. int * dst_row = dst + row * ncols;
  8041. // initialize indices
  8042. if (col < ncols) {
  8043. dst_row[col] = col;
  8044. }
  8045. /*
  8046. DPCT1065:58: Consider replacing sycl::nd_item::barrier() with
  8047. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better
  8048. performance if there is no access to global memory.
  8049. */
  8050. item_ct1.barrier();
  8051. for (int k = 2; k <= ncols; k *= 2) {
  8052. for (int j = k / 2; j > 0; j /= 2) {
  8053. int ixj = col ^ j;
  8054. if (ixj > col) {
  8055. if ((col & k) == 0) {
  8056. if (order == GGML_SORT_ORDER_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  8057. swap(dst_row[col], dst_row[ixj]);
  8058. }
  8059. } else {
  8060. if (order == GGML_SORT_ORDER_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  8061. swap(dst_row[col], dst_row[ixj]);
  8062. }
  8063. }
  8064. }
  8065. /*
  8066. DPCT1118:11: SYCL group functions and algorithms must be encountered
  8067. in converged control flow. You may need to adjust the code.
  8068. */
  8069. /*
  8070. DPCT1065:59: Consider replacing sycl::nd_item::barrier() with
  8071. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  8072. better performance if there is no access to global memory.
  8073. */
  8074. item_ct1.barrier();
  8075. }
  8076. }
  8077. }
  8078. static void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past,
  8079. const sycl::nd_item<3> &item_ct1) {
  8080. const int col = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  8081. item_ct1.get_local_id(1);
  8082. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  8083. item_ct1.get_local_id(2);
  8084. if (col >= ncols) {
  8085. return;
  8086. }
  8087. const int i = row*ncols + col;
  8088. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  8089. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  8090. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  8091. }
  8092. template <bool vals_smem, int ncols_template, int block_size_template>
  8093. static void soft_max_f32(const float * x, const float * mask, const float *pos, float * dst, const int ncols_par,
  8094. const int nrows_y, const float scale, const float max_bias, const float m0,
  8095. const float m1, uint32_t n_head_log2, const sycl::nd_item<3> &item_ct1, float *buf) {
  8096. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  8097. const int tid = item_ct1.get_local_id(2);
  8098. const int rowx = item_ct1.get_group(2);
  8099. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  8100. const int block_size = block_size_template == 0 ? item_ct1.get_local_range(2) : block_size_template;
  8101. const int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  8102. const int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  8103. float slope = 0.0f;
  8104. // ALiBi
  8105. if (max_bias > 0.0f) {
  8106. const uint32_t h = rowx/nrows_y; // head index
  8107. const float base = h < n_head_log2 ? m0 : m1;
  8108. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  8109. slope = sycl::pow(base, float(exp));
  8110. }
  8111. float * vals = vals_smem ? buf + WARP_SIZE : dst + rowx*ncols;
  8112. float max_val = -INFINITY;
  8113. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  8114. const int col = col0 + tid;
  8115. if (ncols_template == 0 && col >= ncols) {
  8116. break;
  8117. }
  8118. const int ix = rowx*ncols + col;
  8119. const int iy = rowy*ncols + col;
  8120. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
  8121. vals[col] = val;
  8122. max_val = sycl::max(max_val, val);
  8123. }
  8124. // find the max value in the block
  8125. max_val = warp_reduce_max(max_val, item_ct1);
  8126. if (block_size > WARP_SIZE) {
  8127. if (warp_id == 0) {
  8128. buf[lane_id] = -INFINITY;
  8129. }
  8130. item_ct1.barrier(sycl::access::fence_space::local_space);
  8131. if (lane_id == 0) {
  8132. buf[warp_id] = max_val;
  8133. }
  8134. item_ct1.barrier(sycl::access::fence_space::local_space);
  8135. max_val = buf[lane_id];
  8136. max_val = warp_reduce_max(max_val, item_ct1);
  8137. }
  8138. float tmp = 0.f;
  8139. #pragma unroll
  8140. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  8141. const int col = col0 + tid;
  8142. if (ncols_template == 0 && col >= ncols) {
  8143. break;
  8144. }
  8145. const float val = sycl::native::exp(vals[col] - max_val);
  8146. tmp += val;
  8147. vals[col] = val;
  8148. }
  8149. // find the sum of exps in the block
  8150. tmp = warp_reduce_sum(tmp, item_ct1);
  8151. if (block_size > WARP_SIZE) {
  8152. if (warp_id == 0) {
  8153. buf[lane_id] = 0.f;
  8154. }
  8155. item_ct1.barrier(sycl::access::fence_space::local_space);
  8156. if (lane_id == 0) {
  8157. buf[warp_id] = tmp;
  8158. }
  8159. item_ct1.barrier(sycl::access::fence_space::local_space);
  8160. tmp = buf[lane_id];
  8161. tmp = warp_reduce_sum(tmp, item_ct1);
  8162. }
  8163. const float inv_sum = 1.f / tmp;
  8164. #pragma unroll
  8165. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  8166. const int col = col0 + tid;
  8167. if (ncols_template == 0 && col >= ncols) {
  8168. return;
  8169. }
  8170. const int idst = rowx*ncols + col;
  8171. dst[idst] = vals[col] * inv_sum;
  8172. }
  8173. }
  8174. static void scale_f32(const float * x, float * dst, const float scale, const int k,
  8175. const sycl::nd_item<3> &item_ct1) {
  8176. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  8177. item_ct1.get_local_id(2);
  8178. if (i >= k) {
  8179. return;
  8180. }
  8181. dst[i] = scale * x[i];
  8182. }
  8183. static void clamp_f32(const float * x, float * dst, const float min, const float max, const int k,
  8184. const sycl::nd_item<3> &item_ct1) {
  8185. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  8186. item_ct1.get_local_id(2);
  8187. if (i >= k) {
  8188. return;
  8189. }
  8190. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  8191. }
  8192. template <typename T>
  8193. static void im2col_kernel(const float *x, T *dst, int offset_delta,
  8194. int IW, int IH, int OW, int KW, int KH,
  8195. int pelements, int CHW, int s0, int s1, int p0,
  8196. int p1, int d0, int d1,
  8197. const sycl::nd_item<3> &item_ct1) {
  8198. const int i = item_ct1.get_local_id(2) +
  8199. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  8200. if (i >= pelements) {
  8201. return;
  8202. }
  8203. const int ksize = OW * (KH > 1 ? KW : 1);
  8204. const int kx = i / ksize;
  8205. const int kd = kx * ksize;
  8206. const int ky = (i - kd) / OW;
  8207. const int ix = i % OW;
  8208. const int64_t iiw = ix * s0 + kx * d0 - p0;
  8209. const int64_t iih = item_ct1.get_group(1) * s1 + ky * d1 - p1;
  8210. const int64_t offset_dst =
  8211. (item_ct1.get_group(1) * OW + ix) * CHW +
  8212. (item_ct1.get_group(0) * (KW * KH) + ky * KW + kx);
  8213. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  8214. dst[offset_dst] =
  8215. sycl::vec<float, 1>(0.0f)
  8216. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  8217. } else {
  8218. const int64_t offset_src = item_ct1.get_group(0) * offset_delta;
  8219. dst[offset_dst] =
  8220. sycl::vec<float, 1>(x[offset_src + iih * IW + iiw])
  8221. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  8222. }
  8223. }
  8224. template <typename Ti, typename To>
  8225. static void pool2d_nchw_kernel(
  8226. const int ih, const int iw, const int oh, const int ow,
  8227. const int kh, const int kw, const int sh, const int sw,
  8228. const int ph, const int pw, const int parallel_elements,
  8229. const Ti* src, To* dst, const enum ggml_op_pool op,
  8230. const sycl::nd_item<3> &item_ct1) {
  8231. int idx = item_ct1.get_local_id(2) +
  8232. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  8233. if (idx >= parallel_elements) {
  8234. return;
  8235. }
  8236. const int I_HW = ih * iw;
  8237. const int O_HW = oh * ow;
  8238. const int nc = idx / O_HW;
  8239. const int cur_oh = idx % O_HW / ow;
  8240. const int cur_ow = idx % O_HW % ow;
  8241. const Ti* i_ptr = src + nc * I_HW;
  8242. To* o_ptr = dst + nc * O_HW;
  8243. const int start_h = cur_oh * sh - ph;
  8244. const int bh = sycl::max(0, start_h);
  8245. const int eh = sycl::min(ih, start_h + kh);
  8246. const int start_w = cur_ow * sw - pw;
  8247. const int bw = sycl::max(0, start_w);
  8248. const int ew = sycl::min(iw, start_w + kw);
  8249. To res = 0;
  8250. switch (op) {
  8251. case GGML_OP_POOL_AVG: res = 0; break;
  8252. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  8253. }
  8254. for (int i = bh; i < eh; i += 1) {
  8255. for (int j = bw; j < ew; j += 1) {
  8256. #if DPCT_COMPATIBILITY_TEMP >= 350
  8257. /*
  8258. DPCT1098:106: The '*' expression is used instead of the __ldg
  8259. call. These two expressions do not provide the exact same
  8260. functionality. Check the generated code for potential precision
  8261. and/or performance issues.
  8262. */
  8263. Ti cur = *(i_ptr + i * iw + j);
  8264. #else
  8265. Ti cur = i_ptr[i * iw + j];
  8266. #endif
  8267. switch (op) {
  8268. case GGML_OP_POOL_AVG: res += (cur / (kh * kw)); break;
  8269. case GGML_OP_POOL_MAX: res = sycl::max(res, (To)cur); break;
  8270. }
  8271. }
  8272. }
  8273. o_ptr[cur_oh * ow + cur_ow] = res;
  8274. }
  8275. template <int qk, int qr, dequantize_kernel_t dq>
  8276. static void get_rows_sycl(const ggml_tensor *src0, const ggml_tensor *src1,
  8277. ggml_tensor *dst, const void *src0_dd,
  8278. const int32_t *src1_dd, float *dst_dd,
  8279. dpct::queue_ptr stream) {
  8280. GGML_TENSOR_BINARY_OP_LOCALS
  8281. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  8282. const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
  8283. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  8284. // strides in elements
  8285. //const size_t s0 = nb0 / ggml_element_size(dst);
  8286. const size_t s1 = nb1 / ggml_element_size(dst);
  8287. const size_t s2 = nb2 / ggml_element_size(dst);
  8288. const size_t s3 = nb3 / ggml_element_size(dst);
  8289. const size_t s10 = nb10 / ggml_element_size(src1);
  8290. const size_t s11 = nb11 / ggml_element_size(src1);
  8291. const size_t s12 = nb12 / ggml_element_size(src1);
  8292. //const size_t s13 = nb13 / ggml_element_size(src1);
  8293. GGML_ASSERT(ne00 % 2 == 0);
  8294. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8295. [=](sycl::nd_item<3> item_ct1) {
  8296. k_get_rows<qk, qr, dq>(
  8297. src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  8298. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  8299. });
  8300. (void) dst;
  8301. }
  8302. template <typename src0_t>
  8303. static void get_rows_sycl_float(const ggml_tensor *src0,
  8304. const ggml_tensor *src1, ggml_tensor *dst,
  8305. const src0_t *src0_dd, const int32_t *src1_dd,
  8306. float *dst_dd, dpct::queue_ptr stream) {
  8307. GGML_TENSOR_BINARY_OP_LOCALS
  8308. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  8309. const int block_num_x = (ne00 + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
  8310. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  8311. // strides in elements
  8312. //const size_t s0 = nb0 / ggml_element_size(dst);
  8313. const size_t s1 = nb1 / ggml_element_size(dst);
  8314. const size_t s2 = nb2 / ggml_element_size(dst);
  8315. const size_t s3 = nb3 / ggml_element_size(dst);
  8316. const size_t s10 = nb10 / ggml_element_size(src1);
  8317. const size_t s11 = nb11 / ggml_element_size(src1);
  8318. const size_t s12 = nb12 / ggml_element_size(src1);
  8319. //const size_t s13 = nb13 / ggml_element_size(src1);
  8320. {
  8321. dpct::has_capability_or_fail(stream->get_device(),
  8322. {sycl::aspect::fp16});
  8323. stream->parallel_for(
  8324. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8325. [=](sycl::nd_item<3> item_ct1) {
  8326. k_get_rows_float(src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  8327. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  8328. });
  8329. }
  8330. (void) dst;
  8331. }
  8332. template<float (*bin_op)(const float, const float)>
  8333. struct bin_bcast_sycl {
  8334. template <typename src0_t, typename src1_t, typename dst_t>
  8335. void operator()(const struct ggml_tensor *src0,
  8336. const struct ggml_tensor *src1, struct ggml_tensor *dst,
  8337. const src0_t *src0_dd, const src1_t *src1_dd, dst_t *dst_dd,
  8338. dpct::queue_ptr stream) {
  8339. GGML_TENSOR_BINARY_OP_LOCALS
  8340. int nr0 = ne10/ne0;
  8341. int nr1 = ne11/ne1;
  8342. int nr2 = ne12/ne2;
  8343. int nr3 = ne13/ne3;
  8344. int nr[4] = { nr0, nr1, nr2, nr3 };
  8345. // collapse dimensions until first broadcast dimension
  8346. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  8347. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  8348. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  8349. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  8350. auto collapse = [](int64_t cne[]) {
  8351. cne[0] *= cne[1];
  8352. cne[1] = cne[2];
  8353. cne[2] = cne[3];
  8354. cne[3] = 1;
  8355. };
  8356. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  8357. cnb[1] *= cne[1];
  8358. cnb[2] *= cne[2];
  8359. cnb[3] *= cne[3];
  8360. };
  8361. for (int i = 0; i < 4; i++) {
  8362. if (nr[i] != 1) {
  8363. break;
  8364. }
  8365. if (i > 0) {
  8366. collapse_nb(cnb0, cne0);
  8367. collapse_nb(cnb1, cne1);
  8368. collapse(cne0);
  8369. collapse(cne1);
  8370. }
  8371. }
  8372. {
  8373. int64_t ne0 = cne0[0];
  8374. int64_t ne1 = cne0[1];
  8375. int64_t ne2 = cne0[2];
  8376. int64_t ne3 = cne0[3];
  8377. int64_t ne10 = cne1[0];
  8378. int64_t ne11 = cne1[1];
  8379. int64_t ne12 = cne1[2];
  8380. int64_t ne13 = cne1[3];
  8381. size_t nb0 = cnb0[0];
  8382. size_t nb1 = cnb0[1];
  8383. size_t nb2 = cnb0[2];
  8384. size_t nb3 = cnb0[3];
  8385. size_t nb10 = cnb1[0];
  8386. size_t nb11 = cnb1[1];
  8387. size_t nb12 = cnb1[2];
  8388. size_t nb13 = cnb1[3];
  8389. size_t s0 = nb0 / sizeof(dst_t);
  8390. size_t s1 = nb1 / sizeof(dst_t);
  8391. size_t s2 = nb2 / sizeof(dst_t);
  8392. size_t s3 = nb3 / sizeof(dst_t);
  8393. size_t s10 = nb10 / sizeof(src1_t);
  8394. size_t s11 = nb11 / sizeof(src1_t);
  8395. size_t s12 = nb12 / sizeof(src1_t);
  8396. size_t s13 = nb13 / sizeof(src1_t);
  8397. GGML_ASSERT(s0 == 1);
  8398. GGML_ASSERT(s10 == 1);
  8399. const int block_size = 128;
  8400. int64_t hne0 = std::max(ne0/2LL, 1LL);
  8401. sycl::range<3> block_dims(1, 1, 1);
  8402. block_dims[2] = std::min<unsigned int>(hne0, block_size);
  8403. block_dims[1] = std::min<unsigned int>(
  8404. ne1, block_size / (unsigned int)block_dims[2]);
  8405. block_dims[0] = std::min(
  8406. std::min<unsigned int>(
  8407. ne2 * ne3, block_size / (unsigned int)block_dims[2] /
  8408. (unsigned int)block_dims[1]),
  8409. 64U);
  8410. sycl::range<3> block_nums(
  8411. (ne2 * ne3 + block_dims[0] - 1) / block_dims[0],
  8412. (ne1 + block_dims[1] - 1) / block_dims[1],
  8413. (hne0 + block_dims[2] - 1) / block_dims[2]);
  8414. if (block_nums[0] > 65535) {
  8415. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  8416. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  8417. {
  8418. dpct::has_capability_or_fail(stream->get_device(),
  8419. {sycl::aspect::fp16});
  8420. stream->parallel_for(
  8421. sycl::nd_range<3>(sycl::range<3>(1, 1, block_num) *
  8422. sycl::range<3>(1, 1, block_size),
  8423. sycl::range<3>(1, 1, block_size)),
  8424. [=](sycl::nd_item<3> item_ct1) {
  8425. k_bin_bcast_unravel<bin_op>(
  8426. src0_dd, src1_dd, dst_dd, ne0, ne1, ne2, ne3,
  8427. ne10, ne11, ne12, ne13, s1, s2, s3, s11, s12,
  8428. s13, item_ct1);
  8429. });
  8430. }
  8431. } else {
  8432. /*
  8433. DPCT1049:16: The work-group size passed to the SYCL kernel may
  8434. exceed the limit. To get the device limit, query
  8435. info::device::max_work_group_size. Adjust the work-group size if
  8436. needed.
  8437. */
  8438. dpct::has_capability_or_fail(stream->get_device(),
  8439. {sycl::aspect::fp16});
  8440. stream->parallel_for(
  8441. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8442. [=](sycl::nd_item<3> item_ct1) {
  8443. k_bin_bcast<bin_op>(src0_dd, src1_dd, dst_dd, ne0, ne1,
  8444. ne2, ne3, ne10, ne11, ne12, ne13,
  8445. s1, s2, s3, s11, s12, s13,
  8446. item_ct1);
  8447. });
  8448. }
  8449. }
  8450. }
  8451. };
  8452. static void acc_f32_sycl(const float *x, const float *y, float *dst,
  8453. const int n_elements, const int ne10, const int ne11,
  8454. const int ne12, const int nb1, const int nb2,
  8455. const int offset, dpct::queue_ptr stream) {
  8456. int num_blocks = (n_elements + SYCL_ACC_BLOCK_SIZE - 1) / SYCL_ACC_BLOCK_SIZE;
  8457. stream->parallel_for(
  8458. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8459. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE),
  8460. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE)),
  8461. [=](sycl::nd_item<3> item_ct1) {
  8462. acc_f32(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset,
  8463. item_ct1);
  8464. });
  8465. }
  8466. static void gelu_f32_sycl(const float *x, float *dst, const int k,
  8467. dpct::queue_ptr stream) {
  8468. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  8469. stream->parallel_for(
  8470. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8471. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  8472. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  8473. [=](sycl::nd_item<3> item_ct1) {
  8474. gelu_f32(x, dst, k, item_ct1);
  8475. });
  8476. }
  8477. static void silu_f32_sycl(const float *x, float *dst, const int k,
  8478. dpct::queue_ptr stream) {
  8479. const int num_blocks = (k + SYCL_SILU_BLOCK_SIZE - 1) / SYCL_SILU_BLOCK_SIZE;
  8480. stream->parallel_for(
  8481. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8482. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE),
  8483. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE)),
  8484. [=](sycl::nd_item<3> item_ct1) {
  8485. silu_f32(x, dst, k, item_ct1);
  8486. });
  8487. }
  8488. static void gelu_quick_f32_sycl(const float *x, float *dst, const int k,
  8489. dpct::queue_ptr stream) {
  8490. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  8491. stream->parallel_for(
  8492. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8493. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  8494. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  8495. [=](sycl::nd_item<3> item_ct1) {
  8496. gelu_quick_f32(x, dst, k, item_ct1);
  8497. });
  8498. }
  8499. static void tanh_f32_sycl(const float *x, float *dst, const int k,
  8500. dpct::queue_ptr stream) {
  8501. const int num_blocks = (k + SYCL_TANH_BLOCK_SIZE - 1) / SYCL_TANH_BLOCK_SIZE;
  8502. stream->parallel_for(
  8503. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8504. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE),
  8505. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE)),
  8506. [=](sycl::nd_item<3> item_ct1) {
  8507. tanh_f32(x, dst, k, item_ct1);
  8508. });
  8509. }
  8510. static void relu_f32_sycl(const float *x, float *dst, const int k,
  8511. dpct::queue_ptr stream) {
  8512. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  8513. stream->parallel_for(
  8514. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8515. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  8516. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  8517. [=](sycl::nd_item<3> item_ct1) {
  8518. relu_f32(x, dst, k, item_ct1);
  8519. });
  8520. }
  8521. static void hardsigmoid_f32_sycl(const float *x, float *dst, const int k,
  8522. dpct::queue_ptr stream) {
  8523. const int num_blocks = (k + SYCL_HARDSIGMOID_BLOCK_SIZE - 1) / SYCL_HARDSIGMOID_BLOCK_SIZE;
  8524. stream->parallel_for(
  8525. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8526. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE),
  8527. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE)),
  8528. [=](sycl::nd_item<3> item_ct1) {
  8529. hardsigmoid_f32(x, dst, k, item_ct1);
  8530. });
  8531. }
  8532. static void hardswish_f32_sycl(const float *x, float *dst, const int k,
  8533. dpct::queue_ptr stream) {
  8534. const int num_blocks = (k + SYCL_HARDSWISH_BLOCK_SIZE - 1) / SYCL_HARDSWISH_BLOCK_SIZE;
  8535. stream->parallel_for(
  8536. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8537. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE),
  8538. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE)),
  8539. [=](sycl::nd_item<3> item_ct1) {
  8540. hardswish_f32(x, dst, k, item_ct1);
  8541. });
  8542. }
  8543. static void leaky_relu_f32_sycl(const float *x, float *dst, const int k,
  8544. const float negative_slope,
  8545. dpct::queue_ptr stream) {
  8546. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  8547. stream->parallel_for(
  8548. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8549. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  8550. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  8551. [=](sycl::nd_item<3> item_ct1) {
  8552. leaky_relu_f32(x, dst, k, negative_slope, item_ct1);
  8553. });
  8554. }
  8555. static void sqr_f32_sycl(const float *x, float *dst, const int k,
  8556. dpct::queue_ptr stream) {
  8557. const int num_blocks = (k + SYCL_SQR_BLOCK_SIZE - 1) / SYCL_SQR_BLOCK_SIZE;
  8558. stream->parallel_for(
  8559. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8560. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE),
  8561. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE)),
  8562. [=](sycl::nd_item<3> item_ct1) {
  8563. sqr_f32(x, dst, k, item_ct1);
  8564. });
  8565. }
  8566. static void norm_f32_sycl(const float *x, float *dst, const int ncols,
  8567. const int nrows, const float eps,
  8568. dpct::queue_ptr stream) {
  8569. GGML_ASSERT(ncols % WARP_SIZE == 0);
  8570. if (ncols < 1024) {
  8571. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8572. stream->submit([&](sycl::handler &cgh) {
  8573. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  8574. sycl::range<1>(32), cgh);
  8575. cgh.parallel_for(
  8576. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8577. block_dims),
  8578. [=](sycl::nd_item<3> item_ct1)
  8579. [[intel::reqd_sub_group_size(32)]] {
  8580. norm_f32(x, dst, ncols, eps, item_ct1,
  8581. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8582. });
  8583. });
  8584. } else {
  8585. const int work_group_size = g_work_group_size;
  8586. const sycl::range<3> block_dims(1, 1, work_group_size);
  8587. /*
  8588. DPCT1049:17: The work-group size passed to the SYCL kernel may exceed
  8589. the limit. To get the device limit, query
  8590. info::device::max_work_group_size. Adjust the work-group size if needed.
  8591. */
  8592. stream->submit([&](sycl::handler &cgh) {
  8593. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  8594. sycl::range<1>(32), cgh);
  8595. cgh.parallel_for(
  8596. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8597. block_dims),
  8598. [=](sycl::nd_item<3> item_ct1)
  8599. [[intel::reqd_sub_group_size(32)]] {
  8600. norm_f32(x, dst, ncols, eps, item_ct1,
  8601. s_sum_acc_ct1.get_pointer(), work_group_size);
  8602. });
  8603. });
  8604. }
  8605. }
  8606. static void group_norm_f32_sycl(const float *x, float *dst,
  8607. const int num_groups, const int group_size,
  8608. const int ne_elements, dpct::queue_ptr stream) {
  8609. static const float eps = 1e-6f;
  8610. if (group_size < 1024) {
  8611. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8612. stream->submit([&](sycl::handler &cgh) {
  8613. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8614. cgh);
  8615. const float eps_ct4 = eps;
  8616. cgh.parallel_for(
  8617. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  8618. block_dims),
  8619. [=](sycl::nd_item<3> item_ct1)
  8620. [[intel::reqd_sub_group_size(32)]] {
  8621. group_norm_f32(
  8622. x, dst, group_size, ne_elements, eps_ct4, item_ct1,
  8623. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8624. });
  8625. });
  8626. } else {
  8627. const int work_group_size = g_work_group_size;
  8628. const sycl::range<3> block_dims(1, 1, work_group_size);
  8629. /*
  8630. DPCT1049:18: The work-group size passed to the SYCL kernel may exceed
  8631. the limit. To get the device limit, query
  8632. info::device::max_work_group_size. Adjust the work-group size if needed.
  8633. */
  8634. stream->submit([&](sycl::handler &cgh) {
  8635. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8636. cgh);
  8637. const float eps_ct4 = eps;
  8638. cgh.parallel_for(
  8639. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  8640. block_dims),
  8641. [=](sycl::nd_item<3> item_ct1)
  8642. [[intel::reqd_sub_group_size(32)]] {
  8643. group_norm_f32(x, dst, group_size, ne_elements,
  8644. eps_ct4, item_ct1,
  8645. s_sum_acc_ct1.get_pointer(), work_group_size);
  8646. });
  8647. });
  8648. }
  8649. }
  8650. static void concat_f32_sycl(const float *x, const float *y, float *dst,
  8651. const int ne0, int ne1, int ne2, int ne02,
  8652. dpct::queue_ptr stream) {
  8653. int num_blocks = (ne0 + SYCL_CONCAT_BLOCK_SIZE - 1) / SYCL_CONCAT_BLOCK_SIZE;
  8654. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  8655. stream->parallel_for(
  8656. sycl::nd_range<3>(gridDim *
  8657. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE),
  8658. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE)),
  8659. [=](sycl::nd_item<3> item_ct1) {
  8660. concat_f32(x, y, dst, ne0, ne02, item_ct1);
  8661. });
  8662. }
  8663. static void upscale_f32_sycl(const float *x, float *dst, const int ne00,
  8664. const int ne01, const int ne02,
  8665. const int scale_factor, dpct::queue_ptr stream) {
  8666. int ne0 = (ne00 * scale_factor);
  8667. int num_blocks = (ne0 + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE;
  8668. sycl::range<3> gridDim(ne02, (ne01 * scale_factor), num_blocks);
  8669. stream->parallel_for(
  8670. sycl::nd_range<3>(gridDim *
  8671. sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE),
  8672. sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE)),
  8673. [=](sycl::nd_item<3> item_ct1) {
  8674. upscale_f32(x, dst, ne00, ne00 * ne01, scale_factor, item_ct1);
  8675. });
  8676. }
  8677. static void pad_f32_sycl(const float *x, float *dst, const int ne00,
  8678. const int ne01, const int ne02, const int ne0,
  8679. const int ne1, const int ne2, dpct::queue_ptr stream) {
  8680. int num_blocks = (ne0 + SYCL_PAD_BLOCK_SIZE - 1) / SYCL_PAD_BLOCK_SIZE;
  8681. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  8682. stream->parallel_for(
  8683. sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE),
  8684. sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE)),
  8685. [=](sycl::nd_item<3> item_ct1) {
  8686. pad_f32(x, dst, ne0, ne00, ne01, ne02, item_ct1);
  8687. });
  8688. }
  8689. static void rms_norm_f32_sycl(const float *x, float *dst, const int ncols,
  8690. const int nrows, const float eps,
  8691. dpct::queue_ptr stream) {
  8692. GGML_ASSERT(ncols % WARP_SIZE == 0);
  8693. // printf("%s ncols=%d, nrows=%d, WARP_SIZE=%d\n", __func__, ncols, nrows, WARP_SIZE);
  8694. if (ncols < 1024) {
  8695. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8696. stream->submit([&](sycl::handler &cgh) {
  8697. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8698. cgh);
  8699. cgh.parallel_for(
  8700. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8701. block_dims),
  8702. [=](sycl::nd_item<3> item_ct1)
  8703. [[intel::reqd_sub_group_size(32)]] {
  8704. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  8705. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8706. });
  8707. });
  8708. } else {
  8709. const int work_group_size = g_work_group_size;
  8710. const sycl::range<3> block_dims(1, 1, work_group_size);
  8711. /*
  8712. DPCT1049:19: The work-group size passed to the SYCL kernel may exceed
  8713. the limit. To get the device limit, query
  8714. info::device::max_work_group_size. Adjust the work-group size if needed.
  8715. */
  8716. stream->submit([&](sycl::handler &cgh) {
  8717. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8718. cgh);
  8719. cgh.parallel_for(
  8720. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8721. block_dims),
  8722. [=](sycl::nd_item<3> item_ct1)
  8723. [[intel::reqd_sub_group_size(32)]] {
  8724. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  8725. s_sum_acc_ct1.get_pointer(), work_group_size);
  8726. });
  8727. });
  8728. }
  8729. }
  8730. static void quantize_row_q8_1_sycl(const float *x, void *vy, const int kx,
  8731. const int ky, const int kx_padded,
  8732. dpct::queue_ptr stream) {
  8733. const int block_num_x = (kx_padded + SYCL_QUANTIZE_BLOCK_SIZE - 1) / SYCL_QUANTIZE_BLOCK_SIZE;
  8734. const sycl::range<3> num_blocks(1, ky, block_num_x);
  8735. const sycl::range<3> block_size(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE);
  8736. {
  8737. dpct::has_capability_or_fail(stream->get_device(),
  8738. {sycl::aspect::fp16});
  8739. stream->parallel_for(
  8740. sycl::nd_range<3>(num_blocks * block_size, block_size),
  8741. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8742. quantize_q8_1(x, vy, kx, kx_padded, item_ct1);
  8743. });
  8744. }
  8745. }
  8746. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  8747. static void dequantize_block_sycl(const void *__restrict__ vx,
  8748. dst_t *__restrict__ y, const int k,
  8749. dpct::queue_ptr stream) {
  8750. const int num_blocks = (k + 2*SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / (2*SYCL_DEQUANTIZE_BLOCK_SIZE);
  8751. {
  8752. dpct::has_capability_or_fail(stream->get_device(),
  8753. {sycl::aspect::fp16});
  8754. stream->parallel_for(
  8755. sycl::nd_range<3>(
  8756. sycl::range<3>(1, 1, num_blocks) *
  8757. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  8758. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  8759. [=](sycl::nd_item<3> item_ct1) {
  8760. dequantize_block<qk, qr, dequantize_kernel>(vx, y, k, item_ct1);
  8761. });
  8762. }
  8763. }
  8764. template <typename dst_t>
  8765. static void dequantize_row_q2_K_sycl(const void *vx, dst_t *y, const int k,
  8766. dpct::queue_ptr stream) {
  8767. const int nb = k / QK_K;
  8768. #if QK_K == 256
  8769. {
  8770. dpct::has_capability_or_fail(stream->get_device(),
  8771. {sycl::aspect::fp16});
  8772. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8773. sycl::range<3>(1, 1, 64),
  8774. sycl::range<3>(1, 1, 64)),
  8775. [=](sycl::nd_item<3> item_ct1) {
  8776. dequantize_block_q2_K(vx, y, item_ct1);
  8777. });
  8778. }
  8779. #else
  8780. {
  8781. dpct::has_capability_or_fail(stream->get_device(),
  8782. {sycl::aspect::fp16});
  8783. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8784. sycl::range<3>(1, 1, 32),
  8785. sycl::range<3>(1, 1, 32)),
  8786. [=](sycl::nd_item<3> item_ct1) {
  8787. dequantize_block_q2_K(vx, y, item_ct1);
  8788. });
  8789. }
  8790. #endif
  8791. }
  8792. template <typename dst_t>
  8793. static void dequantize_row_q3_K_sycl(const void *vx, dst_t *y, const int k,
  8794. dpct::queue_ptr stream) {
  8795. const int nb = k / QK_K;
  8796. #if QK_K == 256
  8797. {
  8798. dpct::has_capability_or_fail(stream->get_device(),
  8799. {sycl::aspect::fp16});
  8800. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8801. sycl::range<3>(1, 1, 64),
  8802. sycl::range<3>(1, 1, 64)),
  8803. [=](sycl::nd_item<3> item_ct1) {
  8804. dequantize_block_q3_K(vx, y, item_ct1);
  8805. });
  8806. }
  8807. #else
  8808. {
  8809. dpct::has_capability_or_fail(stream->get_device(),
  8810. {sycl::aspect::fp16});
  8811. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8812. sycl::range<3>(1, 1, 32),
  8813. sycl::range<3>(1, 1, 32)),
  8814. [=](sycl::nd_item<3> item_ct1) {
  8815. dequantize_block_q3_K(vx, y, item_ct1);
  8816. });
  8817. }
  8818. #endif
  8819. }
  8820. template <typename dst_t>
  8821. static void dequantize_row_q4_0_sycl(const void *vx, dst_t *y, const int k,
  8822. dpct::queue_ptr stream) {
  8823. const int nb32 = k / 32;
  8824. const int nb = (k + 255) / 256;
  8825. {
  8826. dpct::has_capability_or_fail(stream->get_device(),
  8827. {sycl::aspect::fp16});
  8828. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8829. sycl::range<3>(1, 1, 32),
  8830. sycl::range<3>(1, 1, 32)),
  8831. [=](sycl::nd_item<3> item_ct1) {
  8832. dequantize_block_q4_0(vx, y, nb32, item_ct1);
  8833. });
  8834. }
  8835. }
  8836. template <typename dst_t>
  8837. static void dequantize_row_q4_1_sycl(const void *vx, dst_t *y, const int k,
  8838. dpct::queue_ptr stream) {
  8839. const int nb32 = k / 32;
  8840. const int nb = (k + 255) / 256;
  8841. {
  8842. dpct::has_capability_or_fail(stream->get_device(),
  8843. {sycl::aspect::fp16});
  8844. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8845. sycl::range<3>(1, 1, 32),
  8846. sycl::range<3>(1, 1, 32)),
  8847. [=](sycl::nd_item<3> item_ct1) {
  8848. dequantize_block_q4_1(vx, y, nb32, item_ct1);
  8849. });
  8850. }
  8851. }
  8852. template <typename dst_t>
  8853. static void dequantize_row_q4_K_sycl(const void *vx, dst_t *y, const int k,
  8854. dpct::queue_ptr stream) {
  8855. const int nb = k / QK_K;
  8856. {
  8857. dpct::has_capability_or_fail(stream->get_device(),
  8858. {sycl::aspect::fp16});
  8859. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8860. sycl::range<3>(1, 1, 32),
  8861. sycl::range<3>(1, 1, 32)),
  8862. [=](sycl::nd_item<3> item_ct1) {
  8863. dequantize_block_q4_K(vx, y, item_ct1);
  8864. });
  8865. }
  8866. }
  8867. template <typename dst_t>
  8868. static void dequantize_row_q5_K_sycl(const void *vx, dst_t *y, const int k,
  8869. dpct::queue_ptr stream) {
  8870. const int nb = k / QK_K;
  8871. #if QK_K == 256
  8872. {
  8873. dpct::has_capability_or_fail(stream->get_device(),
  8874. {sycl::aspect::fp16});
  8875. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8876. sycl::range<3>(1, 1, 64),
  8877. sycl::range<3>(1, 1, 64)),
  8878. [=](sycl::nd_item<3> item_ct1) {
  8879. dequantize_block_q5_K(vx, y, item_ct1);
  8880. });
  8881. }
  8882. #else
  8883. {
  8884. dpct::has_capability_or_fail(stream->get_device(),
  8885. {sycl::aspect::fp16});
  8886. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8887. sycl::range<3>(1, 1, 32),
  8888. sycl::range<3>(1, 1, 32)),
  8889. [=](sycl::nd_item<3> item_ct1) {
  8890. dequantize_block_q5_K(vx, y, item_ct1);
  8891. });
  8892. }
  8893. #endif
  8894. }
  8895. template <typename dst_t>
  8896. static void dequantize_row_q6_K_sycl(const void *vx, dst_t *y, const int k,
  8897. dpct::queue_ptr stream) {
  8898. const int nb = k / QK_K;
  8899. #if QK_K == 256
  8900. {
  8901. dpct::has_capability_or_fail(stream->get_device(),
  8902. {sycl::aspect::fp16});
  8903. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8904. sycl::range<3>(1, 1, 64),
  8905. sycl::range<3>(1, 1, 64)),
  8906. [=](sycl::nd_item<3> item_ct1) {
  8907. dequantize_block_q6_K(vx, y, item_ct1);
  8908. });
  8909. }
  8910. #else
  8911. {
  8912. dpct::has_capability_or_fail(stream->get_device(),
  8913. {sycl::aspect::fp16});
  8914. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8915. sycl::range<3>(1, 1, 32),
  8916. sycl::range<3>(1, 1, 32)),
  8917. [=](sycl::nd_item<3> item_ct1) {
  8918. dequantize_block_q6_K(vx, y, item_ct1);
  8919. });
  8920. }
  8921. #endif
  8922. }
  8923. template <typename dst_t>
  8924. static void dequantize_row_iq2_xxs_sycl(const void *vx, dst_t *y, const int k,
  8925. dpct::queue_ptr stream) {
  8926. const int nb = k / QK_K;
  8927. {
  8928. iq2xxs_grid.init(*stream);
  8929. ksigns_iq2xs.init(*stream);
  8930. kmask_iq2xs.init(*stream);
  8931. dpct::has_capability_or_fail(stream->get_device(),
  8932. {sycl::aspect::fp16});
  8933. stream->submit([&](sycl::handler &cgh) {
  8934. auto iq2xxs_grid_ptr_ct1 = iq2xxs_grid.get_ptr();
  8935. auto ksigns_iq2xs_ptr_ct1 = ksigns_iq2xs.get_ptr();
  8936. auto kmask_iq2xs_ptr_ct1 = kmask_iq2xs.get_ptr();
  8937. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8938. sycl::range<3>(1, 1, 32),
  8939. sycl::range<3>(1, 1, 32)),
  8940. [=](sycl::nd_item<3> item_ct1) {
  8941. dequantize_block_iq2_xxs(
  8942. vx, y, item_ct1, iq2xxs_grid_ptr_ct1,
  8943. ksigns_iq2xs_ptr_ct1, kmask_iq2xs_ptr_ct1);
  8944. });
  8945. });
  8946. }
  8947. }
  8948. template <typename dst_t>
  8949. static void dequantize_row_iq2_xs_sycl(const void *vx, dst_t *y, const int k,
  8950. dpct::queue_ptr stream) {
  8951. const int nb = k / QK_K;
  8952. {
  8953. iq2xs_grid.init(*stream);
  8954. ksigns_iq2xs.init(*stream);
  8955. kmask_iq2xs.init(*stream);
  8956. dpct::has_capability_or_fail(stream->get_device(),
  8957. {sycl::aspect::fp16});
  8958. stream->submit([&](sycl::handler &cgh) {
  8959. auto iq2xs_grid_ptr_ct1 = iq2xs_grid.get_ptr();
  8960. auto ksigns_iq2xs_ptr_ct1 = ksigns_iq2xs.get_ptr();
  8961. auto kmask_iq2xs_ptr_ct1 = kmask_iq2xs.get_ptr();
  8962. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8963. sycl::range<3>(1, 1, 32),
  8964. sycl::range<3>(1, 1, 32)),
  8965. [=](sycl::nd_item<3> item_ct1) {
  8966. dequantize_block_iq2_xs(
  8967. vx, y, item_ct1, iq2xs_grid_ptr_ct1,
  8968. ksigns_iq2xs_ptr_ct1, kmask_iq2xs_ptr_ct1);
  8969. });
  8970. });
  8971. }
  8972. }
  8973. template <typename dst_t>
  8974. static void dequantize_row_iq3_xxs_sycl(const void *vx, dst_t *y, const int k,
  8975. dpct::queue_ptr stream) {
  8976. const int nb = k / QK_K;
  8977. {
  8978. iq3xxs_grid.init(*stream);
  8979. ksigns_iq2xs.init(*stream);
  8980. kmask_iq2xs.init(*stream);
  8981. dpct::has_capability_or_fail(stream->get_device(),
  8982. {sycl::aspect::fp16});
  8983. stream->submit([&](sycl::handler &cgh) {
  8984. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  8985. auto ksigns_iq2xs_ptr_ct1 = ksigns_iq2xs.get_ptr();
  8986. auto kmask_iq2xs_ptr_ct1 = kmask_iq2xs.get_ptr();
  8987. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8988. sycl::range<3>(1, 1, 32),
  8989. sycl::range<3>(1, 1, 32)),
  8990. [=](sycl::nd_item<3> item_ct1) {
  8991. dequantize_block_iq3_xxs(
  8992. vx, y, item_ct1, iq3xxs_grid_ptr_ct1,
  8993. ksigns_iq2xs_ptr_ct1, kmask_iq2xs_ptr_ct1);
  8994. });
  8995. });
  8996. }
  8997. }
  8998. template <typename src_t, typename dst_t>
  8999. static void convert_unary_sycl(const void *__restrict__ vx,
  9000. dst_t *__restrict__ y, const int k,
  9001. dpct::queue_ptr stream) {
  9002. const int num_blocks = (k + SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / SYCL_DEQUANTIZE_BLOCK_SIZE;
  9003. {
  9004. dpct::has_capability_or_fail(stream->get_device(),
  9005. {sycl::aspect::fp16});
  9006. stream->parallel_for(
  9007. sycl::nd_range<3>(
  9008. sycl::range<3>(1, 1, num_blocks) *
  9009. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  9010. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  9011. [=](sycl::nd_item<3> item_ct1) {
  9012. convert_unary<src_t>(vx, y, k, item_ct1);
  9013. });
  9014. }
  9015. }
  9016. static to_fp16_sycl_t ggml_get_to_fp16_sycl(ggml_type type) try {
  9017. int id;
  9018. switch (type) {
  9019. case GGML_TYPE_Q4_0:
  9020. return dequantize_block_sycl<QK4_0, QR4_0, dequantize_q4_0>;
  9021. case GGML_TYPE_Q4_1:
  9022. return dequantize_block_sycl<QK4_1, QR4_1, dequantize_q4_1>;
  9023. case GGML_TYPE_Q5_0:
  9024. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  9025. case GGML_TYPE_Q5_1:
  9026. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  9027. case GGML_TYPE_Q8_0:
  9028. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  9029. case GGML_TYPE_Q2_K:
  9030. return dequantize_row_q2_K_sycl;
  9031. case GGML_TYPE_Q3_K:
  9032. return dequantize_row_q3_K_sycl;
  9033. case GGML_TYPE_Q4_K:
  9034. return dequantize_row_q4_K_sycl;
  9035. case GGML_TYPE_Q5_K:
  9036. return dequantize_row_q5_K_sycl;
  9037. case GGML_TYPE_Q6_K:
  9038. return dequantize_row_q6_K_sycl;
  9039. case GGML_TYPE_IQ2_XXS:
  9040. return dequantize_row_iq2_xxs_sycl;
  9041. case GGML_TYPE_IQ2_XS:
  9042. return dequantize_row_iq2_xs_sycl;
  9043. case GGML_TYPE_IQ3_XXS:
  9044. return dequantize_row_iq3_xxs_sycl;
  9045. case GGML_TYPE_F32:
  9046. return convert_unary_sycl<float>;
  9047. default:
  9048. return nullptr;
  9049. }
  9050. }
  9051. catch (sycl::exception const &exc) {
  9052. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9053. << ", line:" << __LINE__ << std::endl;
  9054. std::exit(1);
  9055. }
  9056. static to_fp32_sycl_t ggml_get_to_fp32_sycl(ggml_type type) {
  9057. switch (type) {
  9058. case GGML_TYPE_Q4_0:
  9059. return dequantize_row_q4_0_sycl;
  9060. case GGML_TYPE_Q4_1:
  9061. return dequantize_row_q4_1_sycl;
  9062. case GGML_TYPE_Q5_0:
  9063. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  9064. case GGML_TYPE_Q5_1:
  9065. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  9066. case GGML_TYPE_Q8_0:
  9067. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  9068. case GGML_TYPE_Q2_K:
  9069. return dequantize_row_q2_K_sycl;
  9070. case GGML_TYPE_Q3_K:
  9071. return dequantize_row_q3_K_sycl;
  9072. case GGML_TYPE_Q4_K:
  9073. return dequantize_row_q4_K_sycl;
  9074. case GGML_TYPE_Q5_K:
  9075. return dequantize_row_q5_K_sycl;
  9076. case GGML_TYPE_Q6_K:
  9077. return dequantize_row_q6_K_sycl;
  9078. case GGML_TYPE_IQ2_XXS:
  9079. return dequantize_row_iq2_xxs_sycl;
  9080. case GGML_TYPE_IQ2_XS:
  9081. return dequantize_row_iq2_xs_sycl;
  9082. case GGML_TYPE_IQ3_XXS:
  9083. return dequantize_row_iq3_xxs_sycl;
  9084. case GGML_TYPE_F16:
  9085. return convert_unary_sycl<sycl::half>;
  9086. default:
  9087. return nullptr;
  9088. }
  9089. }
  9090. static void dequantize_mul_mat_vec_q4_0_sycl(const void *vx, const dfloat *y,
  9091. float *dst, const int ncols,
  9092. const int nrows,
  9093. dpct::queue_ptr stream) {
  9094. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9095. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9096. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  9097. const sycl::range<3> block_nums(1, 1, block_num_y);
  9098. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9099. {
  9100. dpct::has_capability_or_fail(stream->get_device(),
  9101. {sycl::aspect::fp16});
  9102. stream->parallel_for(
  9103. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9104. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9105. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>(
  9106. vx, y, dst, ncols, nrows, item_ct1);
  9107. });
  9108. }
  9109. }
  9110. static void dequantize_mul_mat_vec_q4_1_sycl(const void *vx, const dfloat *y,
  9111. float *dst, const int ncols,
  9112. const int nrows,
  9113. dpct::queue_ptr stream) {
  9114. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9115. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9116. const sycl::range<3> block_nums(1, 1, block_num_y);
  9117. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9118. {
  9119. dpct::has_capability_or_fail(stream->get_device(),
  9120. {sycl::aspect::fp16});
  9121. stream->parallel_for(
  9122. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9123. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9124. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>(
  9125. vx, y, dst, ncols, nrows, item_ct1);
  9126. });
  9127. }
  9128. }
  9129. static void dequantize_mul_mat_vec_q5_0_sycl(const void *vx, const dfloat *y,
  9130. float *dst, const int ncols,
  9131. const int nrows,
  9132. dpct::queue_ptr stream) {
  9133. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9134. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9135. const sycl::range<3> block_nums(1, 1, block_num_y);
  9136. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9137. {
  9138. dpct::has_capability_or_fail(stream->get_device(),
  9139. {sycl::aspect::fp16});
  9140. stream->parallel_for(
  9141. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9142. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9143. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>(
  9144. vx, y, dst, ncols, nrows, item_ct1);
  9145. });
  9146. }
  9147. }
  9148. static void dequantize_mul_mat_vec_q5_1_sycl(const void *vx, const dfloat *y,
  9149. float *dst, const int ncols,
  9150. const int nrows,
  9151. dpct::queue_ptr stream) {
  9152. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9153. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9154. const sycl::range<3> block_nums(1, 1, block_num_y);
  9155. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9156. {
  9157. dpct::has_capability_or_fail(stream->get_device(),
  9158. {sycl::aspect::fp16});
  9159. stream->parallel_for(
  9160. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9161. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9162. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>(
  9163. vx, y, dst, ncols, nrows, item_ct1);
  9164. });
  9165. }
  9166. }
  9167. static void dequantize_mul_mat_vec_q8_0_sycl(const void *vx, const dfloat *y,
  9168. float *dst, const int ncols,
  9169. const int nrows,
  9170. dpct::queue_ptr stream) {
  9171. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9172. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9173. const sycl::range<3> block_nums(1, 1, block_num_y);
  9174. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9175. {
  9176. dpct::has_capability_or_fail(stream->get_device(),
  9177. {sycl::aspect::fp16});
  9178. stream->parallel_for(
  9179. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9180. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9181. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>(
  9182. vx, y, dst, ncols, nrows, item_ct1);
  9183. });
  9184. }
  9185. }
  9186. static void dequantize_mul_mat_vec_q2_K_sycl(const void *vx, const float *y,
  9187. float *dst, const int ncols,
  9188. const int nrows,
  9189. dpct::queue_ptr stream) {
  9190. GGML_ASSERT(ncols % QK_K == 0);
  9191. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  9192. const int block_num_y = (nrows + ny - 1) / ny;
  9193. const sycl::range<3> block_nums(1, 1, block_num_y);
  9194. const sycl::range<3> block_dims(1, ny, 32);
  9195. stream->parallel_for(
  9196. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9197. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9198. dequantize_mul_mat_vec_q2_k(vx, y, dst, ncols, nrows, item_ct1);
  9199. });
  9200. }
  9201. static void dequantize_mul_mat_vec_q3_K_sycl(const void *vx, const float *y,
  9202. float *dst, const int ncols,
  9203. const int nrows,
  9204. dpct::queue_ptr stream) {
  9205. GGML_ASSERT(ncols % QK_K == 0);
  9206. const int ny = 2 / K_QUANTS_PER_ITERATION;
  9207. const int block_num_y = (nrows + ny - 1) / ny;
  9208. const sycl::range<3> block_nums(1, 1, block_num_y);
  9209. const sycl::range<3> block_dims(1, ny, 32);
  9210. stream->parallel_for(
  9211. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9212. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9213. dequantize_mul_mat_vec_q3_k(vx, y, dst, ncols, nrows, item_ct1);
  9214. });
  9215. }
  9216. static void dequantize_mul_mat_vec_q4_K_sycl(const void *vx, const float *y,
  9217. float *dst, const int ncols,
  9218. const int nrows,
  9219. dpct::queue_ptr stream) {
  9220. GGML_ASSERT(ncols % QK_K == 0);
  9221. const int ny = 2 / K_QUANTS_PER_ITERATION;
  9222. const int block_num_y = (nrows + ny - 1) / ny;
  9223. const sycl::range<3> block_nums(1, 1, block_num_y);
  9224. const sycl::range<3> block_dims(1, ny, 32);
  9225. stream->parallel_for(
  9226. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9227. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9228. dequantize_mul_mat_vec_q4_k(vx, y, dst, ncols, nrows, item_ct1);
  9229. });
  9230. }
  9231. static void dequantize_mul_mat_vec_q5_K_sycl(const void *vx, const float *y,
  9232. float *dst, const int ncols,
  9233. const int nrows,
  9234. dpct::queue_ptr stream) {
  9235. GGML_ASSERT(ncols % QK_K == 0);
  9236. const sycl::range<3> block_dims(1, 1, 32);
  9237. stream->parallel_for(
  9238. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims, block_dims),
  9239. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9240. dequantize_mul_mat_vec_q5_k(vx, y, dst, ncols, item_ct1);
  9241. });
  9242. }
  9243. static void dequantize_mul_mat_vec_q6_K_sycl(const void *vx, const float *y,
  9244. float *dst, const int ncols,
  9245. const int nrows,
  9246. dpct::queue_ptr stream) {
  9247. GGML_ASSERT(ncols % QK_K == 0);
  9248. const int ny = 2 / K_QUANTS_PER_ITERATION;
  9249. const int block_num_y = (nrows + ny - 1) / ny;
  9250. const sycl::range<3> block_nums(1, 1, block_num_y);
  9251. const sycl::range<3> block_dims(1, ny, 32);
  9252. stream->parallel_for(
  9253. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9254. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9255. dequantize_mul_mat_vec_q6_k(vx, y, dst, ncols, nrows, item_ct1);
  9256. });
  9257. }
  9258. static void convert_mul_mat_vec_f16_sycl(const void *vx, const dfloat *y,
  9259. float *dst, const int ncols,
  9260. const int nrows,
  9261. dpct::queue_ptr stream) {
  9262. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  9263. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9264. const sycl::range<3> block_nums(1, 1, block_num_y);
  9265. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9266. {
  9267. dpct::has_capability_or_fail(stream->get_device(),
  9268. {sycl::aspect::fp16});
  9269. stream->parallel_for(
  9270. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9271. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9272. dequantize_mul_mat_vec<1, 1, convert_f16>(vx, y, dst, ncols,
  9273. nrows, item_ct1);
  9274. });
  9275. }
  9276. }
  9277. static void mul_mat_vec_q4_0_q8_1_sycl(const void *vx, const void *vy,
  9278. float *dst, const int ncols,
  9279. const int nrows,
  9280. dpct::queue_ptr stream) {
  9281. GGML_ASSERT(ncols % QK4_0 == 0);
  9282. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9283. const sycl::range<3> block_nums(1, 1, block_num_y);
  9284. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9285. {
  9286. iq3xxs_grid.init(*stream);
  9287. ksigns64.init(*stream);
  9288. stream->submit([&](sycl::handler &cgh) {
  9289. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9290. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9291. cgh.parallel_for(
  9292. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9293. [=](sycl::nd_item<3> item_ct1)
  9294. [[intel::reqd_sub_group_size(32)]] {
  9295. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0,
  9296. VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>(
  9297. vx, vy, dst, ncols, nrows, item_ct1,
  9298. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9299. });
  9300. });
  9301. }
  9302. }
  9303. static void mul_mat_vec_q4_1_q8_1_sycl(const void *vx, const void *vy,
  9304. float *dst, const int ncols,
  9305. const int nrows,
  9306. dpct::queue_ptr stream) {
  9307. GGML_ASSERT(ncols % QK4_1 == 0);
  9308. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9309. const sycl::range<3> block_nums(1, 1, block_num_y);
  9310. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9311. {
  9312. iq3xxs_grid.init(*stream);
  9313. ksigns64.init(*stream);
  9314. stream->submit([&](sycl::handler &cgh) {
  9315. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9316. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9317. cgh.parallel_for(
  9318. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9319. [=](sycl::nd_item<3> item_ct1)
  9320. [[intel::reqd_sub_group_size(32)]] {
  9321. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1,
  9322. VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>(
  9323. vx, vy, dst, ncols, nrows, item_ct1,
  9324. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9325. });
  9326. });
  9327. }
  9328. }
  9329. static void mul_mat_vec_q5_0_q8_1_sycl(const void *vx, const void *vy,
  9330. float *dst, const int ncols,
  9331. const int nrows,
  9332. dpct::queue_ptr stream) {
  9333. GGML_ASSERT(ncols % QK5_0 == 0);
  9334. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9335. const sycl::range<3> block_nums(1, 1, block_num_y);
  9336. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9337. {
  9338. iq3xxs_grid.init(*stream);
  9339. ksigns64.init(*stream);
  9340. stream->submit([&](sycl::handler &cgh) {
  9341. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9342. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  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_0, QI5_0, block_q5_0,
  9348. VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>(
  9349. vx, vy, dst, ncols, nrows, item_ct1,
  9350. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9351. });
  9352. });
  9353. }
  9354. }
  9355. static void mul_mat_vec_q5_1_q8_1_sycl(const void *vx, const void *vy,
  9356. float *dst, const int ncols,
  9357. const int nrows,
  9358. dpct::queue_ptr stream) {
  9359. GGML_ASSERT(ncols % QK5_1 == 0);
  9360. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9361. const sycl::range<3> block_nums(1, 1, block_num_y);
  9362. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9363. {
  9364. iq3xxs_grid.init(*stream);
  9365. ksigns64.init(*stream);
  9366. stream->submit([&](sycl::handler &cgh) {
  9367. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9368. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9369. cgh.parallel_for(
  9370. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9371. [=](sycl::nd_item<3> item_ct1)
  9372. [[intel::reqd_sub_group_size(32)]] {
  9373. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1,
  9374. VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>(
  9375. vx, vy, dst, ncols, nrows, item_ct1,
  9376. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9377. });
  9378. });
  9379. }
  9380. }
  9381. static void mul_mat_vec_q8_0_q8_1_sycl(const void *vx, const void *vy,
  9382. float *dst, const int ncols,
  9383. const int nrows,
  9384. dpct::queue_ptr stream) {
  9385. GGML_ASSERT(ncols % QK8_0 == 0);
  9386. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9387. const sycl::range<3> block_nums(1, 1, block_num_y);
  9388. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9389. {
  9390. iq3xxs_grid.init(*stream);
  9391. ksigns64.init(*stream);
  9392. stream->submit([&](sycl::handler &cgh) {
  9393. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9394. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9395. cgh.parallel_for(
  9396. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9397. [=](sycl::nd_item<3> item_ct1)
  9398. [[intel::reqd_sub_group_size(32)]] {
  9399. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0,
  9400. VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>(
  9401. vx, vy, dst, ncols, nrows, item_ct1,
  9402. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9403. });
  9404. });
  9405. }
  9406. }
  9407. static void mul_mat_vec_q2_K_q8_1_sycl(const void *vx, const void *vy,
  9408. float *dst, const int ncols,
  9409. const int nrows,
  9410. dpct::queue_ptr stream) {
  9411. GGML_ASSERT(ncols % QK_K == 0);
  9412. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9413. const sycl::range<3> block_nums(1, 1, block_num_y);
  9414. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9415. {
  9416. iq3xxs_grid.init(*stream);
  9417. ksigns64.init(*stream);
  9418. stream->submit([&](sycl::handler &cgh) {
  9419. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9420. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9421. cgh.parallel_for(
  9422. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9423. [=](sycl::nd_item<3> item_ct1)
  9424. [[intel::reqd_sub_group_size(32)]] {
  9425. mul_mat_vec_q<QK_K, QI2_K, block_q2_K,
  9426. VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>(
  9427. vx, vy, dst, ncols, nrows, item_ct1,
  9428. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9429. });
  9430. });
  9431. }
  9432. }
  9433. static void mul_mat_vec_q3_K_q8_1_sycl(const void *vx, const void *vy,
  9434. float *dst, const int ncols,
  9435. const int nrows,
  9436. dpct::queue_ptr stream) {
  9437. GGML_ASSERT(ncols % QK_K == 0);
  9438. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9439. const sycl::range<3> block_nums(1, 1, block_num_y);
  9440. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9441. {
  9442. iq3xxs_grid.init(*stream);
  9443. ksigns64.init(*stream);
  9444. stream->submit([&](sycl::handler &cgh) {
  9445. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9446. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9447. cgh.parallel_for(
  9448. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9449. [=](sycl::nd_item<3> item_ct1)
  9450. [[intel::reqd_sub_group_size(32)]] {
  9451. mul_mat_vec_q<QK_K, QI3_K, block_q3_K,
  9452. VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>(
  9453. vx, vy, dst, ncols, nrows, item_ct1,
  9454. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9455. });
  9456. });
  9457. }
  9458. }
  9459. static void mul_mat_vec_q4_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. iq3xxs_grid.init(*stream);
  9469. ksigns64.init(*stream);
  9470. stream->submit([&](sycl::handler &cgh) {
  9471. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9472. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9473. cgh.parallel_for(
  9474. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9475. [=](sycl::nd_item<3> item_ct1)
  9476. [[intel::reqd_sub_group_size(32)]] {
  9477. mul_mat_vec_q<QK_K, QI4_K, block_q4_K,
  9478. VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>(
  9479. vx, vy, dst, ncols, nrows, item_ct1,
  9480. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9481. });
  9482. });
  9483. }
  9484. }
  9485. static void mul_mat_vec_q5_K_q8_1_sycl(const void *vx, const void *vy,
  9486. float *dst, const int ncols,
  9487. const int nrows,
  9488. dpct::queue_ptr stream) {
  9489. GGML_ASSERT(ncols % QK_K == 0);
  9490. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9491. const sycl::range<3> block_nums(1, 1, block_num_y);
  9492. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9493. {
  9494. iq3xxs_grid.init(*stream);
  9495. ksigns64.init(*stream);
  9496. stream->submit([&](sycl::handler &cgh) {
  9497. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9498. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9499. cgh.parallel_for(
  9500. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9501. [=](sycl::nd_item<3> item_ct1)
  9502. [[intel::reqd_sub_group_size(32)]] {
  9503. mul_mat_vec_q<QK_K, QI5_K, block_q5_K,
  9504. VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>(
  9505. vx, vy, dst, ncols, nrows, item_ct1,
  9506. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9507. });
  9508. });
  9509. }
  9510. }
  9511. static void mul_mat_vec_q6_K_q8_1_sycl(const void *vx, const void *vy,
  9512. float *dst, const int ncols,
  9513. const int nrows,
  9514. dpct::queue_ptr stream) {
  9515. GGML_ASSERT(ncols % QK_K == 0);
  9516. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9517. const sycl::range<3> block_nums(1, 1, block_num_y);
  9518. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9519. {
  9520. iq3xxs_grid.init(*stream);
  9521. ksigns64.init(*stream);
  9522. stream->submit([&](sycl::handler &cgh) {
  9523. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9524. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9525. cgh.parallel_for(
  9526. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9527. [=](sycl::nd_item<3> item_ct1)
  9528. [[intel::reqd_sub_group_size(32)]] {
  9529. mul_mat_vec_q<QK_K, QI6_K, block_q6_K,
  9530. VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>(
  9531. vx, vy, dst, ncols, nrows, item_ct1,
  9532. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9533. });
  9534. });
  9535. }
  9536. }
  9537. static void mul_mat_vec_iq2_xxs_q8_1_sycl(const void *vx, const void *vy,
  9538. float *dst, const int ncols,
  9539. const int nrows,
  9540. dpct::queue_ptr stream) {
  9541. GGML_ASSERT(ncols % QK_K == 0);
  9542. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9543. const sycl::range<3> block_nums(1, 1, block_num_y);
  9544. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9545. {
  9546. iq2xxs_grid.init(*stream);
  9547. ksigns_iq2xs.init(*stream);
  9548. kmask_iq2xs.init(*stream);
  9549. stream->submit([&](sycl::handler &cgh) {
  9550. auto iq2xxs_grid_ptr_ct1 = iq2xxs_grid.get_ptr();
  9551. auto ksigns_iq2xs_ptr_ct1 = ksigns_iq2xs.get_ptr();
  9552. auto kmask_iq2xs_ptr_ct1 = kmask_iq2xs.get_ptr();
  9553. cgh.parallel_for(
  9554. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9555. [=](sycl::nd_item<3> item_ct1)
  9556. [[intel::reqd_sub_group_size(32)]] {
  9557. mul_mat_vec_q_iq2_xxs_q8_1<QK_K, QI2_XXS, block_iq2_xxs, 1>(
  9558. vx, vy, dst, ncols, nrows, item_ct1,
  9559. iq2xxs_grid_ptr_ct1, ksigns_iq2xs_ptr_ct1, kmask_iq2xs_ptr_ct1);
  9560. });
  9561. });
  9562. }
  9563. }
  9564. static void mul_mat_vec_iq2_xs_q8_1_sycl(const void *vx, const void *vy,
  9565. float *dst, const int ncols,
  9566. const int nrows,
  9567. dpct::queue_ptr stream) {
  9568. GGML_ASSERT(ncols % QK_K == 0);
  9569. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9570. const sycl::range<3> block_nums(1, 1, block_num_y);
  9571. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9572. {
  9573. iq2xs_grid.init(*stream);
  9574. ksigns64.init(*stream);
  9575. stream->submit([&](sycl::handler &cgh) {
  9576. auto iq2xs_grid_ptr_ct1 = iq2xs_grid.get_ptr();
  9577. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9578. cgh.parallel_for(
  9579. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9580. [=](sycl::nd_item<3> item_ct1)
  9581. [[intel::reqd_sub_group_size(32)]] {
  9582. mul_mat_vec_q_iq2_xs_q8_1<QK_K, QI2_XS, block_iq2_xs, 1>(
  9583. vx, vy, dst, ncols, nrows, item_ct1,
  9584. iq2xs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9585. });
  9586. });
  9587. }
  9588. }
  9589. static void mul_mat_vec_iq3_xxs_q8_1_sycl(const void *vx, const void *vy,
  9590. float *dst, const int ncols,
  9591. const int nrows,
  9592. dpct::queue_ptr stream) {
  9593. GGML_ASSERT(ncols % QK_K == 0);
  9594. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9595. const sycl::range<3> block_nums(1, 1, block_num_y);
  9596. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9597. {
  9598. iq3xxs_grid.init(*stream);
  9599. ksigns64.init(*stream);
  9600. stream->submit([&](sycl::handler &cgh) {
  9601. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9602. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9603. cgh.parallel_for(
  9604. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9605. [=](sycl::nd_item<3> item_ct1)
  9606. [[intel::reqd_sub_group_size(32)]] {
  9607. mul_mat_vec_q_iq3_xxs_q8_1<QK_K, QI3_XXS, block_iq3_xxs, 1>(
  9608. vx, vy, dst, ncols, nrows, item_ct1,
  9609. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9610. });
  9611. });
  9612. }
  9613. }
  9614. static void ggml_mul_mat_q4_0_q8_1_sycl(const void *vx, const void *vy,
  9615. float *dst, const int ncols_x,
  9616. const int nrows_x, const int ncols_y,
  9617. const int nrows_y, const int nrows_dst,
  9618. dpct::queue_ptr stream) try {
  9619. int id;
  9620. SYCL_CHECK(
  9621. CHECK_TRY_ERROR(id = get_current_device_id()));
  9622. const int compute_capability = g_device_caps[id].cc;
  9623. int mmq_x, mmq_y, nwarps;
  9624. if (compute_capability >= VER_GEN13) {
  9625. mmq_x = MMQ_X_Q4_0_RDNA2;
  9626. mmq_y = MMQ_Y_Q4_0_RDNA2;
  9627. nwarps = NWARPS_Q4_0_RDNA2;
  9628. } else if (compute_capability >= VER_GEN12) {
  9629. mmq_x = MMQ_X_Q4_0_RDNA1;
  9630. mmq_y = MMQ_Y_Q4_0_RDNA1;
  9631. nwarps = NWARPS_Q4_0_RDNA1;
  9632. } else if (compute_capability >= VER_GEN9) {
  9633. mmq_x = MMQ_X_Q4_0_AMPERE;
  9634. mmq_y = MMQ_Y_Q4_0_AMPERE;
  9635. nwarps = NWARPS_Q4_0_AMPERE;
  9636. } else if (compute_capability >= VER_4VEC) {
  9637. mmq_x = MMQ_X_Q4_0_PASCAL;
  9638. mmq_y = MMQ_Y_Q4_0_PASCAL;
  9639. nwarps = NWARPS_Q4_0_PASCAL;
  9640. } else {
  9641. GGML_ASSERT(false);
  9642. }
  9643. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9644. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9645. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9646. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9647. if (nrows_x % mmq_y == 0) {
  9648. const bool need_check = false;
  9649. /*
  9650. DPCT1049:20: The work-group size passed to the SYCL kernel may exceed
  9651. the limit. To get the device limit, query
  9652. info::device::max_work_group_size. Adjust the work-group size if needed.
  9653. */
  9654. {
  9655. dpct::has_capability_or_fail(stream->get_device(),
  9656. {sycl::aspect::fp16});
  9657. stream->submit([&](sycl::handler &cgh) {
  9658. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  9659. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9660. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  9661. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  9662. cgh);
  9663. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9664. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9665. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9666. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9667. cgh.parallel_for(
  9668. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9669. [=](sycl::nd_item<3> item_ct1) {
  9670. mul_mat_q4_0<need_check>(
  9671. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9672. nrows_dst, item_ct1,
  9673. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  9674. tile_x_d_q4_0_acc_ct1.get_pointer(),
  9675. tile_y_qs_acc_ct1.get_pointer(),
  9676. tile_y_ds_acc_ct1.get_pointer());
  9677. });
  9678. });
  9679. }
  9680. } else {
  9681. const bool need_check = true;
  9682. /*
  9683. DPCT1049:21: The work-group size passed to the SYCL kernel may exceed
  9684. the limit. To get the device limit, query
  9685. info::device::max_work_group_size. Adjust the work-group size if needed.
  9686. */
  9687. {
  9688. dpct::has_capability_or_fail(stream->get_device(),
  9689. {sycl::aspect::fp16});
  9690. stream->submit([&](sycl::handler &cgh) {
  9691. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  9692. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9693. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  9694. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  9695. cgh);
  9696. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9697. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9698. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9699. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9700. cgh.parallel_for(
  9701. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9702. [=](sycl::nd_item<3> item_ct1) {
  9703. mul_mat_q4_0<need_check>(
  9704. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9705. nrows_dst, item_ct1,
  9706. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  9707. tile_x_d_q4_0_acc_ct1.get_pointer(),
  9708. tile_y_qs_acc_ct1.get_pointer(),
  9709. tile_y_ds_acc_ct1.get_pointer());
  9710. });
  9711. });
  9712. }
  9713. }
  9714. }
  9715. catch (sycl::exception const &exc) {
  9716. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9717. << ", line:" << __LINE__ << std::endl;
  9718. std::exit(1);
  9719. }
  9720. static void ggml_mul_mat_q4_1_q8_1_sycl(const void *vx, const void *vy,
  9721. float *dst, const int ncols_x,
  9722. const int nrows_x, const int ncols_y,
  9723. const int nrows_y, const int nrows_dst,
  9724. dpct::queue_ptr stream) try {
  9725. int id;
  9726. SYCL_CHECK(
  9727. CHECK_TRY_ERROR(id = get_current_device_id()));
  9728. const int compute_capability = g_device_caps[id].cc;
  9729. int mmq_x, mmq_y, nwarps;
  9730. if (compute_capability >= VER_GEN13) {
  9731. mmq_x = MMQ_X_Q4_1_RDNA2;
  9732. mmq_y = MMQ_Y_Q4_1_RDNA2;
  9733. nwarps = NWARPS_Q4_1_RDNA2;
  9734. } else if (compute_capability >= VER_GEN12) {
  9735. mmq_x = MMQ_X_Q4_1_RDNA1;
  9736. mmq_y = MMQ_Y_Q4_1_RDNA1;
  9737. nwarps = NWARPS_Q4_1_RDNA1;
  9738. } else if (compute_capability >= VER_GEN9) {
  9739. mmq_x = MMQ_X_Q4_1_AMPERE;
  9740. mmq_y = MMQ_Y_Q4_1_AMPERE;
  9741. nwarps = NWARPS_Q4_1_AMPERE;
  9742. } else if (compute_capability >= VER_4VEC) {
  9743. mmq_x = MMQ_X_Q4_1_PASCAL;
  9744. mmq_y = MMQ_Y_Q4_1_PASCAL;
  9745. nwarps = NWARPS_Q4_1_PASCAL;
  9746. } else {
  9747. GGML_ASSERT(false);
  9748. }
  9749. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9750. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9751. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9752. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9753. if (nrows_x % mmq_y == 0) {
  9754. const bool need_check = false;
  9755. /*
  9756. DPCT1049:22: The work-group size passed to the SYCL kernel may exceed
  9757. the limit. To get the device limit, query
  9758. info::device::max_work_group_size. Adjust the work-group size if needed.
  9759. */
  9760. {
  9761. dpct::has_capability_or_fail(stream->get_device(),
  9762. {sycl::aspect::fp16});
  9763. stream->submit([&](sycl::handler &cgh) {
  9764. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  9765. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  9766. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  9767. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  9768. cgh);
  9769. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9770. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9771. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9772. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9773. cgh.parallel_for(
  9774. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9775. [=](sycl::nd_item<3> item_ct1) {
  9776. mul_mat_q4_1<need_check>(
  9777. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9778. nrows_dst, item_ct1,
  9779. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  9780. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  9781. tile_y_qs_acc_ct1.get_pointer(),
  9782. tile_y_ds_acc_ct1.get_pointer());
  9783. });
  9784. });
  9785. }
  9786. } else {
  9787. const bool need_check = true;
  9788. /*
  9789. DPCT1049:23: The work-group size passed to the SYCL kernel may exceed
  9790. the limit. To get the device limit, query
  9791. info::device::max_work_group_size. Adjust the work-group size if needed.
  9792. */
  9793. {
  9794. dpct::has_capability_or_fail(stream->get_device(),
  9795. {sycl::aspect::fp16});
  9796. stream->submit([&](sycl::handler &cgh) {
  9797. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  9798. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  9799. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  9800. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  9801. cgh);
  9802. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9803. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9804. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9805. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9806. cgh.parallel_for(
  9807. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9808. [=](sycl::nd_item<3> item_ct1) {
  9809. mul_mat_q4_1<need_check>(
  9810. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9811. nrows_dst, item_ct1,
  9812. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  9813. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  9814. tile_y_qs_acc_ct1.get_pointer(),
  9815. tile_y_ds_acc_ct1.get_pointer());
  9816. });
  9817. });
  9818. }
  9819. }
  9820. }
  9821. catch (sycl::exception const &exc) {
  9822. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9823. << ", line:" << __LINE__ << std::endl;
  9824. std::exit(1);
  9825. }
  9826. static void ggml_mul_mat_q5_0_q8_1_sycl(const void *vx, const void *vy,
  9827. float *dst, const int ncols_x,
  9828. const int nrows_x, const int ncols_y,
  9829. const int nrows_y, const int nrows_dst,
  9830. dpct::queue_ptr stream) try {
  9831. int id;
  9832. SYCL_CHECK(
  9833. CHECK_TRY_ERROR(id = get_current_device_id()));
  9834. const int compute_capability = g_device_caps[id].cc;
  9835. int mmq_x, mmq_y, nwarps;
  9836. if (compute_capability >= VER_GEN13) {
  9837. mmq_x = MMQ_X_Q5_0_RDNA2;
  9838. mmq_y = MMQ_Y_Q5_0_RDNA2;
  9839. nwarps = NWARPS_Q5_0_RDNA2;
  9840. } else if (compute_capability >= VER_GEN12) {
  9841. mmq_x = MMQ_X_Q5_0_RDNA1;
  9842. mmq_y = MMQ_Y_Q5_0_RDNA1;
  9843. nwarps = NWARPS_Q5_0_RDNA1;
  9844. } else if (compute_capability >= VER_GEN9) {
  9845. mmq_x = MMQ_X_Q5_0_AMPERE;
  9846. mmq_y = MMQ_Y_Q5_0_AMPERE;
  9847. nwarps = NWARPS_Q5_0_AMPERE;
  9848. } else if (compute_capability >= VER_4VEC) {
  9849. mmq_x = MMQ_X_Q5_0_PASCAL;
  9850. mmq_y = MMQ_Y_Q5_0_PASCAL;
  9851. nwarps = NWARPS_Q5_0_PASCAL;
  9852. } else {
  9853. GGML_ASSERT(false);
  9854. }
  9855. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9856. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9857. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9858. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9859. if (nrows_x % mmq_y == 0) {
  9860. const bool need_check = false;
  9861. /*
  9862. DPCT1049:24: The work-group size passed to the SYCL kernel may exceed
  9863. the limit. To get the device limit, query
  9864. info::device::max_work_group_size. Adjust the work-group size if needed.
  9865. */
  9866. {
  9867. dpct::has_capability_or_fail(stream->get_device(),
  9868. {sycl::aspect::fp16});
  9869. stream->submit([&](sycl::handler &cgh) {
  9870. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  9871. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9872. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  9873. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  9874. cgh);
  9875. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9876. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9877. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9878. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9879. cgh.parallel_for(
  9880. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9881. [=](sycl::nd_item<3> item_ct1) {
  9882. mul_mat_q5_0<need_check>(
  9883. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9884. nrows_dst, item_ct1,
  9885. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  9886. tile_x_d_q5_0_acc_ct1.get_pointer(),
  9887. tile_y_qs_acc_ct1.get_pointer(),
  9888. tile_y_ds_acc_ct1.get_pointer());
  9889. });
  9890. });
  9891. }
  9892. } else {
  9893. const bool need_check = true;
  9894. /*
  9895. DPCT1049:25: The work-group size passed to the SYCL kernel may exceed
  9896. the limit. To get the device limit, query
  9897. info::device::max_work_group_size. Adjust the work-group size if needed.
  9898. */
  9899. {
  9900. dpct::has_capability_or_fail(stream->get_device(),
  9901. {sycl::aspect::fp16});
  9902. stream->submit([&](sycl::handler &cgh) {
  9903. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  9904. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9905. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  9906. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  9907. cgh);
  9908. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9909. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9910. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9911. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9912. cgh.parallel_for(
  9913. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9914. [=](sycl::nd_item<3> item_ct1) {
  9915. mul_mat_q5_0<need_check>(
  9916. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9917. nrows_dst, item_ct1,
  9918. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  9919. tile_x_d_q5_0_acc_ct1.get_pointer(),
  9920. tile_y_qs_acc_ct1.get_pointer(),
  9921. tile_y_ds_acc_ct1.get_pointer());
  9922. });
  9923. });
  9924. }
  9925. }
  9926. }
  9927. catch (sycl::exception const &exc) {
  9928. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9929. << ", line:" << __LINE__ << std::endl;
  9930. std::exit(1);
  9931. }
  9932. static void ggml_mul_mat_q5_1_q8_1_sycl(const void *vx, const void *vy,
  9933. float *dst, const int ncols_x,
  9934. const int nrows_x, const int ncols_y,
  9935. const int nrows_y, const int nrows_dst,
  9936. dpct::queue_ptr stream) try {
  9937. int id;
  9938. SYCL_CHECK(
  9939. CHECK_TRY_ERROR(id = get_current_device_id()));
  9940. const int compute_capability = g_device_caps[id].cc;
  9941. int mmq_x, mmq_y, nwarps;
  9942. if (compute_capability >= VER_GEN13) {
  9943. mmq_x = MMQ_X_Q5_1_RDNA2;
  9944. mmq_y = MMQ_Y_Q5_1_RDNA2;
  9945. nwarps = NWARPS_Q5_1_RDNA2;
  9946. } else if (compute_capability >= VER_GEN12) {
  9947. mmq_x = MMQ_X_Q5_1_RDNA1;
  9948. mmq_y = MMQ_Y_Q5_1_RDNA1;
  9949. nwarps = NWARPS_Q5_1_RDNA1;
  9950. } else if (compute_capability >= VER_GEN9) {
  9951. mmq_x = MMQ_X_Q5_1_AMPERE;
  9952. mmq_y = MMQ_Y_Q5_1_AMPERE;
  9953. nwarps = NWARPS_Q5_1_AMPERE;
  9954. } else if (compute_capability >= VER_4VEC) {
  9955. mmq_x = MMQ_X_Q5_1_PASCAL;
  9956. mmq_y = MMQ_Y_Q5_1_PASCAL;
  9957. nwarps = NWARPS_Q5_1_PASCAL;
  9958. } else {
  9959. GGML_ASSERT(false);
  9960. }
  9961. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9962. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9963. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9964. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9965. if (nrows_x % mmq_y == 0) {
  9966. const bool need_check = false;
  9967. /*
  9968. DPCT1049:26: The work-group size passed to the SYCL kernel may exceed
  9969. the limit. To get the device limit, query
  9970. info::device::max_work_group_size. Adjust the work-group size if needed.
  9971. */
  9972. {
  9973. dpct::has_capability_or_fail(stream->get_device(),
  9974. {sycl::aspect::fp16});
  9975. stream->submit([&](sycl::handler &cgh) {
  9976. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  9977. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9978. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  9979. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  9980. cgh);
  9981. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9982. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9983. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9984. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9985. cgh.parallel_for(
  9986. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9987. [=](sycl::nd_item<3> item_ct1) {
  9988. mul_mat_q5_1<need_check>(
  9989. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9990. nrows_dst, item_ct1,
  9991. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  9992. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  9993. tile_y_qs_acc_ct1.get_pointer(),
  9994. tile_y_ds_acc_ct1.get_pointer());
  9995. });
  9996. });
  9997. }
  9998. } else {
  9999. const bool need_check = true;
  10000. /*
  10001. DPCT1049:27: The work-group size passed to the SYCL kernel may exceed
  10002. the limit. To get the device limit, query
  10003. info::device::max_work_group_size. Adjust the work-group size if needed.
  10004. */
  10005. {
  10006. dpct::has_capability_or_fail(stream->get_device(),
  10007. {sycl::aspect::fp16});
  10008. stream->submit([&](sycl::handler &cgh) {
  10009. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  10010. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10011. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  10012. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  10013. cgh);
  10014. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10015. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10016. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10017. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10018. cgh.parallel_for(
  10019. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10020. [=](sycl::nd_item<3> item_ct1) {
  10021. mul_mat_q5_1<need_check>(
  10022. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10023. nrows_dst, item_ct1,
  10024. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  10025. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  10026. tile_y_qs_acc_ct1.get_pointer(),
  10027. tile_y_ds_acc_ct1.get_pointer());
  10028. });
  10029. });
  10030. }
  10031. }
  10032. }
  10033. catch (sycl::exception const &exc) {
  10034. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10035. << ", line:" << __LINE__ << std::endl;
  10036. std::exit(1);
  10037. }
  10038. static void ggml_mul_mat_q8_0_q8_1_sycl(const void *vx, const void *vy,
  10039. float *dst, const int ncols_x,
  10040. const int nrows_x, const int ncols_y,
  10041. const int nrows_y, const int nrows_dst,
  10042. dpct::queue_ptr stream) try {
  10043. int id;
  10044. SYCL_CHECK(
  10045. CHECK_TRY_ERROR(id = get_current_device_id()));
  10046. const int compute_capability = g_device_caps[id].cc;
  10047. int mmq_x, mmq_y, nwarps;
  10048. if (compute_capability >= VER_GEN13) {
  10049. mmq_x = MMQ_X_Q8_0_RDNA2;
  10050. mmq_y = MMQ_Y_Q8_0_RDNA2;
  10051. nwarps = NWARPS_Q8_0_RDNA2;
  10052. } else if (compute_capability >= VER_GEN12) {
  10053. mmq_x = MMQ_X_Q8_0_RDNA1;
  10054. mmq_y = MMQ_Y_Q8_0_RDNA1;
  10055. nwarps = NWARPS_Q8_0_RDNA1;
  10056. } else if (compute_capability >= VER_GEN9) {
  10057. mmq_x = MMQ_X_Q8_0_AMPERE;
  10058. mmq_y = MMQ_Y_Q8_0_AMPERE;
  10059. nwarps = NWARPS_Q8_0_AMPERE;
  10060. } else if (compute_capability >= VER_4VEC) {
  10061. mmq_x = MMQ_X_Q8_0_PASCAL;
  10062. mmq_y = MMQ_Y_Q8_0_PASCAL;
  10063. nwarps = NWARPS_Q8_0_PASCAL;
  10064. } else {
  10065. GGML_ASSERT(false);
  10066. }
  10067. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10068. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10069. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10070. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10071. if (nrows_x % mmq_y == 0) {
  10072. const bool need_check = false;
  10073. /*
  10074. DPCT1049:28: The work-group size passed to the SYCL kernel may exceed
  10075. the limit. To get the device limit, query
  10076. info::device::max_work_group_size. Adjust the work-group size if needed.
  10077. */
  10078. {
  10079. dpct::has_capability_or_fail(stream->get_device(),
  10080. {sycl::aspect::fp16});
  10081. stream->submit([&](sycl::handler &cgh) {
  10082. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  10083. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10084. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  10085. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  10086. cgh);
  10087. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10088. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10089. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10090. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10091. cgh.parallel_for(
  10092. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10093. [=](sycl::nd_item<3> item_ct1) {
  10094. mul_mat_q8_0<need_check>(
  10095. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10096. nrows_dst, item_ct1,
  10097. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  10098. tile_x_d_q8_0_acc_ct1.get_pointer(),
  10099. tile_y_qs_acc_ct1.get_pointer(),
  10100. tile_y_ds_acc_ct1.get_pointer());
  10101. });
  10102. });
  10103. }
  10104. } else {
  10105. const bool need_check = true;
  10106. /*
  10107. DPCT1049:29: The work-group size passed to the SYCL kernel may exceed
  10108. the limit. To get the device limit, query
  10109. info::device::max_work_group_size. Adjust the work-group size if needed.
  10110. */
  10111. {
  10112. dpct::has_capability_or_fail(stream->get_device(),
  10113. {sycl::aspect::fp16});
  10114. stream->submit([&](sycl::handler &cgh) {
  10115. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  10116. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10117. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  10118. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  10119. cgh);
  10120. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10121. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10122. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10123. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10124. cgh.parallel_for(
  10125. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10126. [=](sycl::nd_item<3> item_ct1) {
  10127. mul_mat_q8_0<need_check>(
  10128. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10129. nrows_dst, item_ct1,
  10130. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  10131. tile_x_d_q8_0_acc_ct1.get_pointer(),
  10132. tile_y_qs_acc_ct1.get_pointer(),
  10133. tile_y_ds_acc_ct1.get_pointer());
  10134. });
  10135. });
  10136. }
  10137. }
  10138. }
  10139. catch (sycl::exception const &exc) {
  10140. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10141. << ", line:" << __LINE__ << std::endl;
  10142. std::exit(1);
  10143. }
  10144. static void ggml_mul_mat_q2_K_q8_1_sycl(const void *vx, const void *vy,
  10145. float *dst, const int ncols_x,
  10146. const int nrows_x, const int ncols_y,
  10147. const int nrows_y, const int nrows_dst,
  10148. dpct::queue_ptr stream) try {
  10149. int id;
  10150. SYCL_CHECK(
  10151. CHECK_TRY_ERROR(id = get_current_device_id()));
  10152. const int compute_capability = g_device_caps[id].cc;
  10153. int mmq_x, mmq_y, nwarps;
  10154. if (compute_capability >= VER_GEN13) {
  10155. mmq_x = MMQ_X_Q2_K_RDNA2;
  10156. mmq_y = MMQ_Y_Q2_K_RDNA2;
  10157. nwarps = NWARPS_Q2_K_RDNA2;
  10158. } else if (compute_capability >= VER_GEN12) {
  10159. mmq_x = MMQ_X_Q2_K_RDNA1;
  10160. mmq_y = MMQ_Y_Q2_K_RDNA1;
  10161. nwarps = NWARPS_Q2_K_RDNA1;
  10162. } else if (compute_capability >= VER_GEN9) {
  10163. mmq_x = MMQ_X_Q2_K_AMPERE;
  10164. mmq_y = MMQ_Y_Q2_K_AMPERE;
  10165. nwarps = NWARPS_Q2_K_AMPERE;
  10166. } else if (compute_capability >= VER_4VEC) {
  10167. mmq_x = MMQ_X_Q2_K_PASCAL;
  10168. mmq_y = MMQ_Y_Q2_K_PASCAL;
  10169. nwarps = NWARPS_Q2_K_PASCAL;
  10170. } else {
  10171. GGML_ASSERT(false);
  10172. }
  10173. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10174. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10175. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10176. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10177. if (nrows_x % mmq_y == 0) {
  10178. const bool need_check = false;
  10179. /*
  10180. DPCT1049:30: The work-group size passed to the SYCL kernel may exceed
  10181. the limit. To get the device limit, query
  10182. info::device::max_work_group_size. Adjust the work-group size if needed.
  10183. */
  10184. {
  10185. dpct::has_capability_or_fail(stream->get_device(),
  10186. {sycl::aspect::fp16});
  10187. stream->submit([&](sycl::handler &cgh) {
  10188. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  10189. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10190. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  10191. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  10192. cgh);
  10193. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  10194. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10195. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10196. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10197. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10198. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10199. cgh.parallel_for(
  10200. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10201. [=](sycl::nd_item<3> item_ct1) {
  10202. mul_mat_q2_K<need_check>(
  10203. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10204. nrows_dst, item_ct1,
  10205. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  10206. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  10207. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  10208. tile_y_qs_acc_ct1.get_pointer(),
  10209. tile_y_ds_acc_ct1.get_pointer());
  10210. });
  10211. });
  10212. }
  10213. } else {
  10214. const bool need_check = true;
  10215. /*
  10216. DPCT1049:31: The work-group size passed to the SYCL kernel may exceed
  10217. the limit. To get the device limit, query
  10218. info::device::max_work_group_size. Adjust the work-group size if needed.
  10219. */
  10220. {
  10221. dpct::has_capability_or_fail(stream->get_device(),
  10222. {sycl::aspect::fp16});
  10223. stream->submit([&](sycl::handler &cgh) {
  10224. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  10225. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10226. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  10227. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  10228. cgh);
  10229. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  10230. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10231. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10232. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10233. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10234. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10235. cgh.parallel_for(
  10236. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10237. [=](sycl::nd_item<3> item_ct1) {
  10238. mul_mat_q2_K<need_check>(
  10239. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10240. nrows_dst, item_ct1,
  10241. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  10242. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  10243. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  10244. tile_y_qs_acc_ct1.get_pointer(),
  10245. tile_y_ds_acc_ct1.get_pointer());
  10246. });
  10247. });
  10248. }
  10249. }
  10250. }
  10251. catch (sycl::exception const &exc) {
  10252. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10253. << ", line:" << __LINE__ << std::endl;
  10254. std::exit(1);
  10255. }
  10256. static void ggml_mul_mat_q3_K_q8_1_sycl(const void *vx, const void *vy,
  10257. float *dst, const int ncols_x,
  10258. const int nrows_x, const int ncols_y,
  10259. const int nrows_y, const int nrows_dst,
  10260. dpct::queue_ptr stream) try {
  10261. #if QK_K == 256
  10262. int id;
  10263. SYCL_CHECK(
  10264. CHECK_TRY_ERROR(id = get_current_device_id()));
  10265. const int compute_capability = g_device_caps[id].cc;
  10266. int mmq_x, mmq_y, nwarps;
  10267. if (compute_capability >= VER_GEN13) {
  10268. mmq_x = MMQ_X_Q3_K_RDNA2;
  10269. mmq_y = MMQ_Y_Q3_K_RDNA2;
  10270. nwarps = NWARPS_Q3_K_RDNA2;
  10271. } else if (compute_capability >= VER_GEN12) {
  10272. mmq_x = MMQ_X_Q3_K_RDNA1;
  10273. mmq_y = MMQ_Y_Q3_K_RDNA1;
  10274. nwarps = NWARPS_Q3_K_RDNA1;
  10275. } else if (compute_capability >= VER_GEN9) {
  10276. mmq_x = MMQ_X_Q3_K_AMPERE;
  10277. mmq_y = MMQ_Y_Q3_K_AMPERE;
  10278. nwarps = NWARPS_Q3_K_AMPERE;
  10279. } else if (compute_capability >= VER_4VEC) {
  10280. mmq_x = MMQ_X_Q3_K_PASCAL;
  10281. mmq_y = MMQ_Y_Q3_K_PASCAL;
  10282. nwarps = NWARPS_Q3_K_PASCAL;
  10283. } else {
  10284. GGML_ASSERT(false);
  10285. }
  10286. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10287. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10288. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10289. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10290. if (nrows_x % mmq_y == 0) {
  10291. const bool need_check = false;
  10292. /*
  10293. DPCT1049:32: The work-group size passed to the SYCL kernel may exceed
  10294. the limit. To get the device limit, query
  10295. info::device::max_work_group_size. Adjust the work-group size if needed.
  10296. */
  10297. {
  10298. dpct::has_capability_or_fail(stream->get_device(),
  10299. {sycl::aspect::fp16});
  10300. stream->submit([&](sycl::handler &cgh) {
  10301. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  10302. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10303. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  10304. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  10305. cgh);
  10306. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  10307. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  10308. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  10309. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10310. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10311. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10312. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10313. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10314. cgh.parallel_for(
  10315. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10316. [=](sycl::nd_item<3> item_ct1) {
  10317. mul_mat_q3_K<need_check>(
  10318. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10319. nrows_dst, item_ct1,
  10320. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  10321. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  10322. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  10323. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  10324. tile_y_qs_acc_ct1.get_pointer(),
  10325. tile_y_ds_acc_ct1.get_pointer());
  10326. });
  10327. });
  10328. }
  10329. } else {
  10330. const bool need_check = true;
  10331. /*
  10332. DPCT1049:33: The work-group size passed to the SYCL kernel may exceed
  10333. the limit. To get the device limit, query
  10334. info::device::max_work_group_size. Adjust the work-group size if needed.
  10335. */
  10336. {
  10337. dpct::has_capability_or_fail(stream->get_device(),
  10338. {sycl::aspect::fp16});
  10339. stream->submit([&](sycl::handler &cgh) {
  10340. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  10341. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10342. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  10343. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  10344. cgh);
  10345. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  10346. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  10347. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  10348. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  10349. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10350. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10351. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10352. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10353. cgh.parallel_for(
  10354. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10355. [=](sycl::nd_item<3> item_ct1) {
  10356. mul_mat_q3_K<need_check>(
  10357. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10358. nrows_dst, item_ct1,
  10359. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  10360. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  10361. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  10362. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  10363. tile_y_qs_acc_ct1.get_pointer(),
  10364. tile_y_ds_acc_ct1.get_pointer());
  10365. });
  10366. });
  10367. }
  10368. }
  10369. #endif
  10370. }
  10371. catch (sycl::exception const &exc) {
  10372. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10373. << ", line:" << __LINE__ << std::endl;
  10374. std::exit(1);
  10375. }
  10376. static void ggml_mul_mat_q4_K_q8_1_sycl(const void *vx, const void *vy,
  10377. float *dst, const int ncols_x,
  10378. const int nrows_x, const int ncols_y,
  10379. const int nrows_y, const int nrows_dst,
  10380. dpct::queue_ptr stream) try {
  10381. int id;
  10382. SYCL_CHECK(
  10383. CHECK_TRY_ERROR(id = get_current_device_id()));
  10384. const int compute_capability = g_device_caps[id].cc;
  10385. int mmq_x, mmq_y, nwarps;
  10386. if (compute_capability >= VER_GEN13) {
  10387. mmq_x = MMQ_X_Q4_K_RDNA2;
  10388. mmq_y = MMQ_Y_Q4_K_RDNA2;
  10389. nwarps = NWARPS_Q4_K_RDNA2;
  10390. } else if (compute_capability >= VER_GEN12) {
  10391. mmq_x = MMQ_X_Q4_K_RDNA1;
  10392. mmq_y = MMQ_Y_Q4_K_RDNA1;
  10393. nwarps = NWARPS_Q4_K_RDNA1;
  10394. } else if (compute_capability >= VER_GEN9) {
  10395. mmq_x = MMQ_X_Q4_K_AMPERE;
  10396. mmq_y = MMQ_Y_Q4_K_AMPERE;
  10397. nwarps = NWARPS_Q4_K_AMPERE;
  10398. } else if (compute_capability >= VER_4VEC) {
  10399. mmq_x = MMQ_X_Q4_K_PASCAL;
  10400. mmq_y = MMQ_Y_Q4_K_PASCAL;
  10401. nwarps = NWARPS_Q4_K_PASCAL;
  10402. } else {
  10403. GGML_ASSERT(false);
  10404. }
  10405. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10406. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10407. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10408. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10409. if (nrows_x % mmq_y == 0) {
  10410. const bool need_check = false;
  10411. /*
  10412. DPCT1049:34: The work-group size passed to the SYCL kernel may exceed
  10413. the limit. To get the device limit, query
  10414. info::device::max_work_group_size. Adjust the work-group size if needed.
  10415. */
  10416. {
  10417. dpct::has_capability_or_fail(stream->get_device(),
  10418. {sycl::aspect::fp16});
  10419. stream->submit([&](sycl::handler &cgh) {
  10420. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  10421. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10422. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  10423. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  10424. cgh);
  10425. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  10426. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10427. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10428. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10429. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10430. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10431. cgh.parallel_for(
  10432. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10433. [=](sycl::nd_item<3> item_ct1) {
  10434. mul_mat_q4_K<need_check>(
  10435. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10436. nrows_dst, item_ct1,
  10437. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  10438. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  10439. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  10440. tile_y_qs_acc_ct1.get_pointer(),
  10441. tile_y_ds_acc_ct1.get_pointer());
  10442. });
  10443. });
  10444. }
  10445. } else {
  10446. const bool need_check = true;
  10447. /*
  10448. DPCT1049:35: The work-group size passed to the SYCL kernel may exceed
  10449. the limit. To get the device limit, query
  10450. info::device::max_work_group_size. Adjust the work-group size if needed.
  10451. */
  10452. {
  10453. dpct::has_capability_or_fail(stream->get_device(),
  10454. {sycl::aspect::fp16});
  10455. stream->submit([&](sycl::handler &cgh) {
  10456. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  10457. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10458. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  10459. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  10460. cgh);
  10461. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  10462. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10463. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10464. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10465. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10466. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10467. cgh.parallel_for(
  10468. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10469. [=](sycl::nd_item<3> item_ct1) {
  10470. mul_mat_q4_K<need_check>(
  10471. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10472. nrows_dst, item_ct1,
  10473. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  10474. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  10475. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  10476. tile_y_qs_acc_ct1.get_pointer(),
  10477. tile_y_ds_acc_ct1.get_pointer());
  10478. });
  10479. });
  10480. }
  10481. }
  10482. }
  10483. catch (sycl::exception const &exc) {
  10484. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10485. << ", line:" << __LINE__ << std::endl;
  10486. std::exit(1);
  10487. }
  10488. static void ggml_mul_mat_q5_K_q8_1_sycl(const void *vx, const void *vy,
  10489. float *dst, const int ncols_x,
  10490. const int nrows_x, const int ncols_y,
  10491. const int nrows_y, const int nrows_dst,
  10492. dpct::queue_ptr stream) try {
  10493. int id;
  10494. SYCL_CHECK(
  10495. CHECK_TRY_ERROR(id = get_current_device_id()));
  10496. const int compute_capability = g_device_caps[id].cc;
  10497. int mmq_x, mmq_y, nwarps;
  10498. if (compute_capability >= VER_GEN13) {
  10499. mmq_x = MMQ_X_Q5_K_RDNA2;
  10500. mmq_y = MMQ_Y_Q5_K_RDNA2;
  10501. nwarps = NWARPS_Q5_K_RDNA2;
  10502. } else if (compute_capability >= VER_GEN12) {
  10503. mmq_x = MMQ_X_Q5_K_RDNA1;
  10504. mmq_y = MMQ_Y_Q5_K_RDNA1;
  10505. nwarps = NWARPS_Q5_K_RDNA1;
  10506. } else if (compute_capability >= VER_GEN9) {
  10507. mmq_x = MMQ_X_Q5_K_AMPERE;
  10508. mmq_y = MMQ_Y_Q5_K_AMPERE;
  10509. nwarps = NWARPS_Q5_K_AMPERE;
  10510. } else if (compute_capability >= VER_4VEC) {
  10511. mmq_x = MMQ_X_Q5_K_PASCAL;
  10512. mmq_y = MMQ_Y_Q5_K_PASCAL;
  10513. nwarps = NWARPS_Q5_K_PASCAL;
  10514. } else {
  10515. GGML_ASSERT(false);
  10516. }
  10517. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10518. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10519. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10520. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10521. if (nrows_x % mmq_y == 0) {
  10522. const bool need_check = false;
  10523. /*
  10524. DPCT1049:36: The work-group size passed to the SYCL kernel may exceed
  10525. the limit. To get the device limit, query
  10526. info::device::max_work_group_size. Adjust the work-group size if needed.
  10527. */
  10528. {
  10529. dpct::has_capability_or_fail(stream->get_device(),
  10530. {sycl::aspect::fp16});
  10531. stream->submit([&](sycl::handler &cgh) {
  10532. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  10533. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10534. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  10535. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  10536. cgh);
  10537. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  10538. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10539. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10540. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10541. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10542. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10543. cgh.parallel_for(
  10544. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10545. [=](sycl::nd_item<3> item_ct1) {
  10546. mul_mat_q5_K<need_check>(
  10547. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10548. nrows_dst, item_ct1,
  10549. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  10550. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  10551. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  10552. tile_y_qs_acc_ct1.get_pointer(),
  10553. tile_y_ds_acc_ct1.get_pointer());
  10554. });
  10555. });
  10556. }
  10557. } else {
  10558. const bool need_check = true;
  10559. /*
  10560. DPCT1049:37: The work-group size passed to the SYCL kernel may exceed
  10561. the limit. To get the device limit, query
  10562. info::device::max_work_group_size. Adjust the work-group size if needed.
  10563. */
  10564. {
  10565. dpct::has_capability_or_fail(stream->get_device(),
  10566. {sycl::aspect::fp16});
  10567. stream->submit([&](sycl::handler &cgh) {
  10568. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  10569. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10570. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  10571. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  10572. cgh);
  10573. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  10574. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10575. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10576. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10577. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10578. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10579. cgh.parallel_for(
  10580. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10581. [=](sycl::nd_item<3> item_ct1) {
  10582. mul_mat_q5_K<need_check>(
  10583. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10584. nrows_dst, item_ct1,
  10585. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  10586. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  10587. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  10588. tile_y_qs_acc_ct1.get_pointer(),
  10589. tile_y_ds_acc_ct1.get_pointer());
  10590. });
  10591. });
  10592. }
  10593. }
  10594. }
  10595. catch (sycl::exception const &exc) {
  10596. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10597. << ", line:" << __LINE__ << std::endl;
  10598. std::exit(1);
  10599. }
  10600. static void ggml_mul_mat_q6_K_q8_1_sycl(const void *vx, const void *vy,
  10601. float *dst, const int ncols_x,
  10602. const int nrows_x, const int ncols_y,
  10603. const int nrows_y, const int nrows_dst,
  10604. dpct::queue_ptr stream) try {
  10605. int id;
  10606. SYCL_CHECK(
  10607. CHECK_TRY_ERROR(id = get_current_device_id()));
  10608. const int compute_capability = g_device_caps[id].cc;
  10609. int mmq_x, mmq_y, nwarps;
  10610. if (compute_capability >= VER_GEN13) {
  10611. mmq_x = MMQ_X_Q6_K_RDNA2;
  10612. mmq_y = MMQ_Y_Q6_K_RDNA2;
  10613. nwarps = NWARPS_Q6_K_RDNA2;
  10614. } else if (compute_capability >= VER_GEN12) {
  10615. mmq_x = MMQ_X_Q6_K_RDNA1;
  10616. mmq_y = MMQ_Y_Q6_K_RDNA1;
  10617. nwarps = NWARPS_Q6_K_RDNA1;
  10618. } else if (compute_capability >= VER_GEN9) {
  10619. mmq_x = MMQ_X_Q6_K_AMPERE;
  10620. mmq_y = MMQ_Y_Q6_K_AMPERE;
  10621. nwarps = NWARPS_Q6_K_AMPERE;
  10622. } else if (compute_capability >= VER_4VEC) {
  10623. mmq_x = MMQ_X_Q6_K_PASCAL;
  10624. mmq_y = MMQ_Y_Q6_K_PASCAL;
  10625. nwarps = NWARPS_Q6_K_PASCAL;
  10626. } else {
  10627. GGML_ASSERT(false);
  10628. }
  10629. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10630. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10631. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10632. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10633. if (nrows_x % mmq_y == 0) {
  10634. const bool need_check = false;
  10635. /*
  10636. DPCT1049:38: The work-group size passed to the SYCL kernel may exceed
  10637. the limit. To get the device limit, query
  10638. info::device::max_work_group_size. Adjust the work-group size if needed.
  10639. */
  10640. {
  10641. dpct::has_capability_or_fail(stream->get_device(),
  10642. {sycl::aspect::fp16});
  10643. stream->submit([&](sycl::handler &cgh) {
  10644. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  10645. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10646. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  10647. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  10648. cgh);
  10649. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  10650. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10651. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10652. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10653. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10654. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10655. cgh.parallel_for(
  10656. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10657. [=](sycl::nd_item<3> item_ct1) {
  10658. mul_mat_q6_K<need_check>(
  10659. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10660. nrows_dst, item_ct1,
  10661. tile_x_ql_acc_ct1.get_pointer(),
  10662. tile_x_dm_acc_ct1.get_pointer(),
  10663. tile_x_sc_acc_ct1.get_pointer(),
  10664. tile_y_qs_acc_ct1.get_pointer(),
  10665. tile_y_ds_acc_ct1.get_pointer());
  10666. });
  10667. });
  10668. }
  10669. } else {
  10670. const bool need_check = true;
  10671. /*
  10672. DPCT1049:39: The work-group size passed to the SYCL kernel may exceed
  10673. the limit. To get the device limit, query
  10674. info::device::max_work_group_size. Adjust the work-group size if needed.
  10675. */
  10676. {
  10677. dpct::has_capability_or_fail(stream->get_device(),
  10678. {sycl::aspect::fp16});
  10679. stream->submit([&](sycl::handler &cgh) {
  10680. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  10681. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10682. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  10683. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  10684. cgh);
  10685. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  10686. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10687. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10688. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10689. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10690. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10691. cgh.parallel_for(
  10692. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10693. [=](sycl::nd_item<3> item_ct1) {
  10694. mul_mat_q6_K<need_check>(
  10695. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10696. nrows_dst, item_ct1,
  10697. tile_x_ql_acc_ct1.get_pointer(),
  10698. tile_x_dm_acc_ct1.get_pointer(),
  10699. tile_x_sc_acc_ct1.get_pointer(),
  10700. tile_y_qs_acc_ct1.get_pointer(),
  10701. tile_y_ds_acc_ct1.get_pointer());
  10702. });
  10703. });
  10704. }
  10705. }
  10706. }
  10707. catch (sycl::exception const &exc) {
  10708. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10709. << ", line:" << __LINE__ << std::endl;
  10710. std::exit(1);
  10711. }
  10712. static void ggml_mul_mat_p021_f16_f32_sycl(const void *vx, const float *y,
  10713. float *dst, const int ncols_x,
  10714. const int nrows_x,
  10715. const int nchannels_x,
  10716. const int nchannels_y,
  10717. dpct::queue_ptr stream) {
  10718. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  10719. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10720. {
  10721. dpct::has_capability_or_fail(stream->get_device(),
  10722. {sycl::aspect::fp16});
  10723. stream->parallel_for(
  10724. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10725. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10726. mul_mat_p021_f16_f32(vx, y, dst, ncols_x, nrows_x, nchannels_x,
  10727. nchannels_y, item_ct1);
  10728. });
  10729. }
  10730. }
  10731. static void ggml_mul_mat_vec_nc_f16_f32_sycl(
  10732. const void *vx, const float *y, float *dst, const int ncols_x,
  10733. const int nrows_x, const int row_stride_x, const int nchannels_x,
  10734. const int nchannels_y, const int channel_stride_x, dpct::queue_ptr stream) {
  10735. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  10736. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10737. {
  10738. dpct::has_capability_or_fail(stream->get_device(),
  10739. {sycl::aspect::fp16});
  10740. stream->parallel_for(
  10741. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10742. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10743. mul_mat_vec_nc_f16_f32(vx, y, dst, ncols_x, nrows_x,
  10744. row_stride_x, channel_stride_x,
  10745. nchannels_y / nchannels_x, item_ct1);
  10746. });
  10747. }
  10748. }
  10749. static void
  10750. ggml_cpy_f16_f32_sycl(const char *cx, char *cdst, const int ne, const int ne00,
  10751. const int ne01, const int ne02, const int nb00,
  10752. const int nb01, const int nb02, const int nb03,
  10753. const int ne10, const int ne11, const int ne12,
  10754. const int nb10, const int nb11, const int nb12,
  10755. const int nb13, dpct::queue_ptr stream) {
  10756. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10757. {
  10758. dpct::has_capability_or_fail(stream->get_device(),
  10759. {sycl::aspect::fp16});
  10760. stream->parallel_for(
  10761. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10762. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10763. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10764. [=](sycl::nd_item<3> item_ct1) {
  10765. cpy_f32_f16<cpy_1_f16_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00,
  10766. nb01, nb02, nb03, ne10, ne11, ne12,
  10767. nb10, nb11, nb12, nb13, item_ct1);
  10768. });
  10769. }
  10770. }
  10771. static void ggml_cpy_f32_f32_sycl(const char *cx, char *cdst, const int ne,
  10772. const int ne00, const int ne01,
  10773. const int ne02, const int nb00,
  10774. const int nb01, const int nb02,
  10775. const int nb03, const int ne10,
  10776. const int ne11, const int ne12,
  10777. const int nb10, const int nb11,
  10778. const int nb12, const int nb13,
  10779. dpct::queue_ptr stream) {
  10780. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10781. {
  10782. dpct::has_capability_or_fail(stream->get_device(),
  10783. {sycl::aspect::fp16});
  10784. stream->parallel_for(
  10785. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10786. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10787. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10788. [=](sycl::nd_item<3> item_ct1) {
  10789. cpy_f32_f16<cpy_1_f32_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10790. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10791. item_ct1);
  10792. });
  10793. }
  10794. }
  10795. static void ggml_cpy_f32_f16_sycl(const char *cx, char *cdst, const int ne,
  10796. const int ne00, const int ne01,
  10797. const int ne02, const int nb00,
  10798. const int nb01, const int nb02,
  10799. const int nb03, const int ne10,
  10800. const int ne11, const int ne12,
  10801. const int nb10, const int nb11,
  10802. const int nb12, const int nb13,
  10803. dpct::queue_ptr stream) {
  10804. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10805. {
  10806. dpct::has_capability_or_fail(stream->get_device(),
  10807. {sycl::aspect::fp16});
  10808. stream->parallel_for(
  10809. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10810. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10811. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10812. [=](sycl::nd_item<3> item_ct1) {
  10813. cpy_f32_f16<cpy_1_f32_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10814. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10815. item_ct1);
  10816. });
  10817. }
  10818. }
  10819. static void ggml_cpy_f32_q8_0_sycl(const char *cx, char *cdst, const int ne,
  10820. const int ne00, const int ne01,
  10821. const int ne02, const int nb00,
  10822. const int nb01, const int nb02,
  10823. const int nb03, const int ne10,
  10824. const int ne11, const int ne12,
  10825. const int nb10, const int nb11,
  10826. const int nb12, const int nb13,
  10827. dpct::queue_ptr stream) {
  10828. GGML_ASSERT(ne % QK8_0 == 0);
  10829. const int num_blocks = ne / QK8_0;
  10830. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10831. sycl::range<3>(1, 1, 1)),
  10832. [=](sycl::nd_item<3> item_ct1) {
  10833. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0>(
  10834. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10835. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10836. item_ct1);
  10837. });
  10838. }
  10839. static void ggml_cpy_f32_q4_0_sycl(const char *cx, char *cdst, const int ne,
  10840. const int ne00, const int ne01,
  10841. const int ne02, const int nb00,
  10842. const int nb01, const int nb02,
  10843. const int nb03, const int ne10,
  10844. const int ne11, const int ne12,
  10845. const int nb10, const int nb11,
  10846. const int nb12, const int nb13,
  10847. dpct::queue_ptr stream) {
  10848. GGML_ASSERT(ne % QK4_0 == 0);
  10849. const int num_blocks = ne / QK4_0;
  10850. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10851. sycl::range<3>(1, 1, 1)),
  10852. [=](sycl::nd_item<3> item_ct1) {
  10853. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0>(
  10854. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10855. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10856. item_ct1);
  10857. });
  10858. }
  10859. static void ggml_cpy_f32_q4_1_sycl(const char *cx, char *cdst, const int ne,
  10860. const int ne00, const int ne01,
  10861. const int ne02, const int nb00,
  10862. const int nb01, const int nb02,
  10863. const int nb03, const int ne10,
  10864. const int ne11, const int ne12,
  10865. const int nb10, const int nb11,
  10866. const int nb12, const int nb13,
  10867. dpct::queue_ptr stream) {
  10868. GGML_ASSERT(ne % QK4_1 == 0);
  10869. const int num_blocks = ne / QK4_1;
  10870. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10871. sycl::range<3>(1, 1, 1)),
  10872. [=](sycl::nd_item<3> item_ct1) {
  10873. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1>(
  10874. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10875. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10876. item_ct1);
  10877. });
  10878. }
  10879. static void ggml_cpy_f16_f16_sycl(const char *cx, char *cdst, const int ne,
  10880. const int ne00, const int ne01,
  10881. const int ne02, const int nb00,
  10882. const int nb01, const int nb02,
  10883. const int nb03, const int ne10,
  10884. const int ne11, const int ne12,
  10885. const int nb10, const int nb11,
  10886. const int nb12, const int nb13,
  10887. dpct::queue_ptr stream) {
  10888. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10889. {
  10890. dpct::has_capability_or_fail(stream->get_device(),
  10891. {sycl::aspect::fp16});
  10892. stream->parallel_for(
  10893. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10894. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10895. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10896. [=](sycl::nd_item<3> item_ct1) {
  10897. cpy_f32_f16<cpy_1_f16_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10898. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10899. item_ct1);
  10900. });
  10901. }
  10902. }
  10903. static void ggml_cpy_i16_i16_sycl(const char *cx, char *cdst, const int ne,
  10904. const int ne00, const int ne01,
  10905. const int ne02, const int nb00,
  10906. const int nb01, const int nb02,
  10907. const int nb03, const int ne10,
  10908. const int ne11, const int ne12,
  10909. const int nb10, const int nb11,
  10910. const int nb12, const int nb13,
  10911. dpct::queue_ptr stream) {
  10912. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10913. {
  10914. // dpct::has_capability_or_fail(stream->get_device(),
  10915. // {sycl::aspect::fp16});
  10916. stream->parallel_for(
  10917. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10918. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10919. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10920. [=](sycl::nd_item<3> item_ct1) {
  10921. cpy_f32_f16<cpy_1_i16_i16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10922. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10923. item_ct1);
  10924. });
  10925. }
  10926. }
  10927. static void ggml_cpy_i32_i32_sycl(const char *cx, char *cdst, const int ne,
  10928. const int ne00, const int ne01,
  10929. const int ne02, const int nb00,
  10930. const int nb01, const int nb02,
  10931. const int nb03, const int ne10,
  10932. const int ne11, const int ne12,
  10933. const int nb10, const int nb11,
  10934. const int nb12, const int nb13,
  10935. dpct::queue_ptr stream) {
  10936. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10937. {
  10938. // dpct::has_capability_or_fail(stream->get_device(),
  10939. // {sycl::aspect::fp16});
  10940. stream->parallel_for(
  10941. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10942. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10943. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10944. [=](sycl::nd_item<3> item_ct1) {
  10945. cpy_f32_f16<cpy_1_i32_i32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10946. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10947. item_ct1);
  10948. });
  10949. }
  10950. }
  10951. static void scale_f32_sycl(const float *x, float *dst, const float scale,
  10952. const int k, dpct::queue_ptr stream) {
  10953. const int num_blocks = (k + SYCL_SCALE_BLOCK_SIZE - 1) / SYCL_SCALE_BLOCK_SIZE;
  10954. stream->parallel_for(
  10955. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10956. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE),
  10957. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE)),
  10958. [=](sycl::nd_item<3> item_ct1) {
  10959. scale_f32(x, dst, scale, k, item_ct1);
  10960. });
  10961. }
  10962. static void clamp_f32_sycl(const float *x, float *dst, const float min,
  10963. const float max, const int k,
  10964. dpct::queue_ptr stream) {
  10965. const int num_blocks = (k + SYCL_CLAMP_BLOCK_SIZE - 1) / SYCL_CLAMP_BLOCK_SIZE;
  10966. stream->parallel_for(
  10967. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10968. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE),
  10969. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE)),
  10970. [=](sycl::nd_item<3> item_ct1) {
  10971. clamp_f32(x, dst, min, max, k, item_ct1);
  10972. });
  10973. }
  10974. template <typename T>
  10975. static void rope_sycl(const T *x, T *dst, int ncols, int nrows,
  10976. const int32_t *pos, float freq_scale, int p_delta_rows,
  10977. float freq_base, float ext_factor, float attn_factor,
  10978. rope_corr_dims corr_dims, dpct::queue_ptr stream) {
  10979. GGML_ASSERT(ncols % 2 == 0);
  10980. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  10981. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  10982. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  10983. if (pos == nullptr) {
  10984. /*
  10985. DPCT1049:40: The work-group size passed to the SYCL kernel may exceed
  10986. the limit. To get the device limit, query
  10987. info::device::max_work_group_size. Adjust the work-group size if needed.
  10988. */
  10989. dpct::has_capability_or_fail(stream->get_device(),
  10990. {sycl::aspect::fp16});
  10991. stream->parallel_for(
  10992. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10993. [=](sycl::nd_item<3> item_ct1) {
  10994. rope<T, false>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  10995. freq_base, ext_factor, attn_factor, corr_dims,
  10996. item_ct1);
  10997. });
  10998. } else {
  10999. /*
  11000. DPCT1049:41: The work-group size passed to the SYCL kernel may exceed
  11001. the limit. To get the device limit, query
  11002. info::device::max_work_group_size. Adjust the work-group size if needed.
  11003. */
  11004. dpct::has_capability_or_fail(stream->get_device(),
  11005. {sycl::aspect::fp16});
  11006. stream->parallel_for(
  11007. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11008. [=](sycl::nd_item<3> item_ct1) {
  11009. rope<T, true>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  11010. freq_base, ext_factor, attn_factor, corr_dims,
  11011. item_ct1);
  11012. });
  11013. }
  11014. }
  11015. template <typename T>
  11016. static void rope_neox_sycl(const T *x, T *dst, int ncols, int n_dims, int nrows,
  11017. const int32_t *pos, float freq_scale,
  11018. int p_delta_rows, float freq_base, float ext_factor,
  11019. float attn_factor, rope_corr_dims corr_dims,
  11020. dpct::queue_ptr stream) {
  11021. GGML_ASSERT(ncols % 2 == 0);
  11022. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  11023. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  11024. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  11025. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  11026. const float inv_ndims = -1.0f / n_dims;
  11027. if (pos == nullptr) {
  11028. /*
  11029. DPCT1049:42: The work-group size passed to the SYCL kernel may exceed
  11030. the limit. To get the device limit, query
  11031. info::device::max_work_group_size. Adjust the work-group size if needed.
  11032. */
  11033. dpct::has_capability_or_fail(stream->get_device(),
  11034. {sycl::aspect::fp16});
  11035. stream->parallel_for(
  11036. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11037. [=](sycl::nd_item<3> item_ct1) {
  11038. rope_neox<T, false>(x, dst, ncols, n_dims, pos, freq_scale,
  11039. p_delta_rows, ext_factor, attn_factor,
  11040. corr_dims, theta_scale, inv_ndims,
  11041. item_ct1);
  11042. });
  11043. } else {
  11044. /*
  11045. DPCT1049:43: The work-group size passed to the SYCL kernel may exceed
  11046. the limit. To get the device limit, query
  11047. info::device::max_work_group_size. Adjust the work-group size if needed.
  11048. */
  11049. dpct::has_capability_or_fail(stream->get_device(),
  11050. {sycl::aspect::fp16});
  11051. stream->parallel_for(
  11052. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11053. [=](sycl::nd_item<3> item_ct1) {
  11054. rope_neox<T, true>(x, dst, ncols, n_dims, pos, freq_scale,
  11055. p_delta_rows, ext_factor, attn_factor,
  11056. corr_dims, theta_scale, inv_ndims, item_ct1);
  11057. });
  11058. }
  11059. }
  11060. static void rope_glm_f32_sycl(const float *x, float *dst, int ncols, int nrows,
  11061. const int32_t *pos, float freq_scale,
  11062. int p_delta_rows, float freq_base, int n_ctx,
  11063. dpct::queue_ptr stream) {
  11064. GGML_ASSERT(ncols % 4 == 0);
  11065. const sycl::range<3> block_dims(1, 1, SYCL_ROPE_BLOCK_SIZE / 4);
  11066. const int num_blocks_x = (ncols + SYCL_ROPE_BLOCK_SIZE - 1) / SYCL_ROPE_BLOCK_SIZE;
  11067. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  11068. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11069. [=](sycl::nd_item<3> item_ct1) {
  11070. rope_glm_f32(x, dst, ncols, pos, freq_scale,
  11071. p_delta_rows, freq_base, n_ctx,
  11072. item_ct1);
  11073. });
  11074. }
  11075. static void alibi_f32_sycl(const float *x, float *dst, const int ncols,
  11076. const int nrows, const int k_rows,
  11077. const int n_heads_log2_floor, const float m0,
  11078. const float m1, dpct::queue_ptr stream) {
  11079. const sycl::range<3> block_dims(1, 1, SYCL_ALIBI_BLOCK_SIZE);
  11080. const int num_blocks_x = (ncols + SYCL_ALIBI_BLOCK_SIZE - 1) / (SYCL_ALIBI_BLOCK_SIZE);
  11081. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  11082. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11083. [=](sycl::nd_item<3> item_ct1) {
  11084. alibi_f32(x, dst, ncols, k_rows,
  11085. n_heads_log2_floor, m0, m1, item_ct1);
  11086. });
  11087. }
  11088. static void sum_rows_f32_sycl(const float *x, float *dst, const int ncols,
  11089. const int nrows, dpct::queue_ptr stream) {
  11090. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  11091. const sycl::range<3> block_nums(1, nrows, 1);
  11092. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11093. [=](sycl::nd_item<3> item_ct1)
  11094. [[intel::reqd_sub_group_size(32)]] {
  11095. k_sum_rows_f32(x, dst, ncols, item_ct1);
  11096. });
  11097. }
  11098. static void argsort_f32_i32_sycl(const float *x, int *dst, const int ncols,
  11099. const int nrows, ggml_sort_order order,
  11100. dpct::queue_ptr stream) {
  11101. // bitonic sort requires ncols to be power of 2
  11102. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  11103. const sycl::range<3> block_dims(1, 1, ncols);
  11104. const sycl::range<3> block_nums(1, nrows, 1);
  11105. if (order == GGML_SORT_ORDER_ASC) {
  11106. /*
  11107. DPCT1049:44: The work-group size passed to the SYCL kernel may exceed
  11108. the limit. To get the device limit, query
  11109. info::device::max_work_group_size. Adjust the work-group size if needed.
  11110. */
  11111. stream->parallel_for(
  11112. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11113. [=](sycl::nd_item<3> item_ct1) {
  11114. k_argsort_f32_i32<GGML_SORT_ORDER_ASC>(x, dst, ncols, item_ct1);
  11115. });
  11116. } else if (order == GGML_SORT_ORDER_DESC) {
  11117. /*
  11118. DPCT1049:45: The work-group size passed to the SYCL kernel may exceed
  11119. the limit. To get the device limit, query
  11120. info::device::max_work_group_size. Adjust the work-group size if needed.
  11121. */
  11122. stream->parallel_for(
  11123. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11124. [=](sycl::nd_item<3> item_ct1) {
  11125. k_argsort_f32_i32<GGML_SORT_ORDER_DESC>(x, dst, ncols, item_ct1);
  11126. });
  11127. } else {
  11128. GGML_ASSERT(false);
  11129. }
  11130. }
  11131. static void diag_mask_inf_f32_sycl(const float *x, float *dst,
  11132. const int ncols_x, const int nrows_x,
  11133. const int rows_per_channel, const int n_past,
  11134. dpct::queue_ptr stream) {
  11135. const sycl::range<3> block_dims(1, SYCL_DIAG_MASK_INF_BLOCK_SIZE, 1);
  11136. const int block_num_x = (ncols_x + SYCL_DIAG_MASK_INF_BLOCK_SIZE - 1) / SYCL_DIAG_MASK_INF_BLOCK_SIZE;
  11137. const sycl::range<3> block_nums(1, block_num_x, nrows_x);
  11138. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11139. [=](sycl::nd_item<3> item_ct1) {
  11140. diag_mask_inf_f32(x, dst, ncols_x,
  11141. rows_per_channel, n_past,
  11142. item_ct1);
  11143. });
  11144. }
  11145. template <bool vals_smem, int ncols_template, int block_size_template>
  11146. static void soft_max_f32_submitter(const float * x, const float * mask, const float *pos, float * dst, const int ncols_par,
  11147. const int nrows_y, const float scale, const float max_bias, const float m0,
  11148. const float m1, uint32_t n_head_log2, sycl::range<3> block_nums, sycl::range<3> block_dims,
  11149. const size_t n_local_scratch, dpct::queue_ptr stream) {
  11150. stream->submit([&](sycl::handler &cgh) {
  11151. sycl::local_accessor<float, 1> local_buf_acc(n_local_scratch, cgh);
  11152. cgh.parallel_for(
  11153. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  11154. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  11155. soft_max_f32<vals_smem, ncols_template, block_size_template>(x, mask, pos, dst, ncols_par,
  11156. nrows_y, scale, max_bias, m0,
  11157. m1, n_head_log2, item_ct1,
  11158. local_buf_acc.get_pointer());
  11159. });
  11160. });
  11161. }
  11162. static void soft_max_f32_sycl(const float * x, const float * mask, const float * pos,
  11163. float * dst, const int ncols_x, const int nrows_x,
  11164. const int nrows_y, const float scale, const float max_bias,
  11165. dpct::queue_ptr stream) {
  11166. int nth = WARP_SIZE;
  11167. while (nth < ncols_x && nth < SYCL_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  11168. const sycl::range<3> block_dims(1, 1, nth);
  11169. const sycl::range<3> block_nums(1, 1, nrows_x);
  11170. const size_t n_local_scratch = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE);
  11171. static_assert(SYCL_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  11172. const uint32_t n_head_kv = nrows_x/nrows_y;
  11173. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  11174. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  11175. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  11176. const size_t local_mem_size = stream->get_device().get_info<sycl::info::device::local_mem_size>();
  11177. if (n_local_scratch*sizeof(float) < local_mem_size) {
  11178. switch (ncols_x) {
  11179. case 32:
  11180. soft_max_f32_submitter<true, 32, 32>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11181. max_bias, m0, m1, n_head_log2, block_nums,
  11182. block_dims, n_local_scratch, stream);
  11183. break;
  11184. case 64:
  11185. soft_max_f32_submitter<true, 64, 64>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11186. max_bias, m0, m1, n_head_log2, block_nums,
  11187. block_dims, n_local_scratch, stream);
  11188. break;
  11189. case 128:
  11190. soft_max_f32_submitter<true, 128, 128>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11191. max_bias, m0, m1, n_head_log2, block_nums,
  11192. block_dims, n_local_scratch, stream);
  11193. break;
  11194. case 256:
  11195. soft_max_f32_submitter<true, 256, 256>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11196. max_bias, m0, m1, n_head_log2, block_nums,
  11197. block_dims, n_local_scratch, stream);
  11198. break;
  11199. case 512:
  11200. soft_max_f32_submitter<true, 512, 512>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11201. max_bias, m0, m1, n_head_log2, block_nums,
  11202. block_dims, n_local_scratch, stream);
  11203. break;
  11204. case 1024:
  11205. soft_max_f32_submitter<true, 1024, 1024>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11206. max_bias, m0, m1, n_head_log2, block_nums,
  11207. block_dims, n_local_scratch, stream);
  11208. break;
  11209. case 2048:
  11210. soft_max_f32_submitter<true, 2048, 1024>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11211. max_bias, m0, m1, n_head_log2, block_nums,
  11212. block_dims, n_local_scratch, stream);
  11213. break;
  11214. case 4096:
  11215. soft_max_f32_submitter<true, 4096, 1024>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11216. max_bias, m0, m1, n_head_log2, block_nums,
  11217. block_dims, n_local_scratch, stream);
  11218. break;
  11219. default:
  11220. soft_max_f32_submitter<true, 0, 0>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11221. max_bias, m0, m1, n_head_log2, block_nums,
  11222. block_dims, n_local_scratch, stream);
  11223. break;
  11224. }
  11225. } else {
  11226. soft_max_f32_submitter<false, 0, 0>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  11227. max_bias, m0, m1, n_head_log2, block_nums,
  11228. block_dims, WARP_SIZE, stream);
  11229. }
  11230. }
  11231. template <typename T>
  11232. static void im2col_sycl(const float *x, T *dst, int IW, int IH,
  11233. int OW, int OH, int KW, int KH, int IC,
  11234. int offset_delta, int s0, int s1, int p0,
  11235. int p1, int d0, int d1,
  11236. dpct::queue_ptr stream) {
  11237. const int parallel_elements = OW * KW * KH;
  11238. const int num_blocks = (parallel_elements + SYCL_IM2COL_BLOCK_SIZE - 1) / SYCL_IM2COL_BLOCK_SIZE;
  11239. sycl::range<3> block_nums(IC, OH, num_blocks);
  11240. {
  11241. dpct::has_capability_or_fail(stream->get_device(),
  11242. {sycl::aspect::fp16});
  11243. stream->parallel_for(
  11244. sycl::nd_range<3>(block_nums *
  11245. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  11246. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  11247. [=](sycl::nd_item<3> item_ct1) {
  11248. im2col_kernel(x, dst, offset_delta, IW, IH, OW, KW, KH,
  11249. parallel_elements, (IC * KH * KW), s0, s1, p0,
  11250. p1, d0, d1, item_ct1);
  11251. });
  11252. }
  11253. }
  11254. // buffer pool for sycl
  11255. #define MAX_SYCL_BUFFERS 256
  11256. struct scoped_spin_lock {
  11257. std::atomic_flag& lock;
  11258. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  11259. while (lock.test_and_set(std::memory_order_acquire)) {
  11260. ; // spin
  11261. }
  11262. }
  11263. ~scoped_spin_lock() {
  11264. lock.clear(std::memory_order_release);
  11265. }
  11266. scoped_spin_lock(const scoped_spin_lock&) = delete;
  11267. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  11268. };
  11269. static std::atomic_flag g_sycl_pool_lock = ATOMIC_FLAG_INIT;
  11270. // #define DEBUG_SYCL_MALLOC
  11271. struct sycl_buffer {
  11272. void * ptr = nullptr;
  11273. size_t size = 0;
  11274. };
  11275. static sycl_buffer g_sycl_buffer_pool[GGML_SYCL_MAX_DEVICES][MAX_SYCL_BUFFERS];
  11276. static size_t g_sycl_pool_size[GGML_SYCL_MAX_DEVICES] = {0};
  11277. static void *ggml_sycl_pool_malloc_leg(int device_index, size_t size, size_t *actual_size) try {
  11278. scoped_spin_lock lock(g_sycl_pool_lock);
  11279. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg device_index %d size=%lu\n", device_index, size);
  11280. #ifdef DEBUG_SYCL_MALLOC
  11281. int nnz = 0;
  11282. size_t max_size = 0;
  11283. #endif
  11284. size_t best_diff = 1ull << 36;
  11285. int ibest = -1;
  11286. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  11287. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  11288. if (b.ptr != nullptr) {
  11289. #ifdef DEBUG_SYCL_MALLOC
  11290. ++nnz;
  11291. if (b.size > max_size) max_size = b.size;
  11292. #endif
  11293. if (b.size >= size) {
  11294. size_t diff = b.size - size;
  11295. if (diff < best_diff) {
  11296. best_diff = diff;
  11297. ibest = i;
  11298. if (!best_diff) {
  11299. void * ptr = b.ptr;
  11300. *actual_size = b.size;
  11301. b.ptr = nullptr;
  11302. b.size = 0;
  11303. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 1 %p and rm in pool\n", ptr);
  11304. return ptr;
  11305. }
  11306. }
  11307. }
  11308. }
  11309. }
  11310. if (ibest >= 0) {
  11311. sycl_buffer& b = g_sycl_buffer_pool[device_index][ibest];
  11312. void * ptr = b.ptr;
  11313. *actual_size = b.size;
  11314. b.ptr = nullptr;
  11315. b.size = 0;
  11316. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 2 %p and rm in pool\n", ptr);
  11317. return ptr;
  11318. }
  11319. void * ptr;
  11320. size_t look_ahead_size = (size_t) (1.05 * size);
  11321. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  11322. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  11323. SYCL_CHECK(
  11324. CHECK_TRY_ERROR(ptr = (void *)sycl::malloc_device(
  11325. look_ahead_size, *stream)));
  11326. *actual_size = look_ahead_size;
  11327. g_sycl_pool_size[device_index] += look_ahead_size;
  11328. #ifdef DEBUG_SYCL_MALLOC
  11329. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  11330. (uint32_t)(max_size/1024/1024), (uint32_t)(g_sycl_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  11331. #endif
  11332. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg look_ahead_size=%lu, return %p\n", look_ahead_size, ptr);
  11333. return ptr;
  11334. }
  11335. catch (sycl::exception const &exc) {
  11336. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11337. << ", line:" << __LINE__ << std::endl;
  11338. std::exit(1);
  11339. }
  11340. static void ggml_sycl_pool_free_leg(int device_index, void *ptr, size_t size) try {
  11341. scoped_spin_lock lock(g_sycl_pool_lock);
  11342. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  11343. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  11344. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  11345. if (b.ptr == nullptr) {
  11346. b.ptr = ptr;
  11347. b.size = size;
  11348. return;
  11349. }
  11350. }
  11351. fprintf(stderr, "WARNING: sycl buffer pool full, increase MAX_SYCL_BUFFERS\n");
  11352. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *stream)));
  11353. g_sycl_pool_size[device_index] -= size;
  11354. }
  11355. catch (sycl::exception const &exc) {
  11356. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11357. << ", line:" << __LINE__ << std::endl;
  11358. std::exit(1);
  11359. }
  11360. // pool with virtual memory
  11361. /*
  11362. DPCT1082:64: Migration of CUmemGenericAllocationHandle type is not supported.
  11363. */
  11364. // static std::vector<CUmemGenericAllocationHandle>
  11365. // g_sycl_pool_handles[GGML_SYCL_MAX_DEVICES];
  11366. static dpct::device_ptr g_sycl_pool_addr[GGML_SYCL_MAX_DEVICES] = {0};
  11367. static size_t g_sycl_pool_used[GGML_SYCL_MAX_DEVICES] = {0};
  11368. static void *ggml_sycl_pool_malloc_vmm(int device_index, size_t size, size_t *actual_size) try {
  11369. GGML_UNUSED(device_index);
  11370. GGML_UNUSED(size);
  11371. GGML_UNUSED(actual_size);
  11372. return NULL;
  11373. }
  11374. catch (sycl::exception const &exc) {
  11375. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11376. << ", line:" << __LINE__ << std::endl;
  11377. std::exit(1);
  11378. }
  11379. static void ggml_sycl_pool_free_vmm(int device_index, void *ptr, size_t size) try {
  11380. scoped_spin_lock lock(g_sycl_pool_lock);
  11381. #ifdef DEBUG_SYCL_MALLOC
  11382. printf("sycl pool[%d]: freed %llu bytes at %llx\n", device_index, (unsigned long long) size, ptr);
  11383. #endif
  11384. g_sycl_pool_used[device_index] -= size;
  11385. // all deallocations must be in reverse order of the allocations
  11386. GGML_ASSERT(ptr == (void *) (g_sycl_pool_addr[device_index] + g_sycl_pool_used[device_index]));
  11387. }
  11388. catch (sycl::exception const &exc) {
  11389. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11390. << ", line:" << __LINE__ << std::endl;
  11391. std::exit(1);
  11392. }
  11393. static void *ggml_sycl_pool_malloc(int device_index, size_t size, size_t *actual_size) try {
  11394. if (g_device_caps[device_index].vmm) {
  11395. return ggml_sycl_pool_malloc_vmm(device_index, size, actual_size);
  11396. } else {
  11397. return ggml_sycl_pool_malloc_leg(device_index, size, actual_size);
  11398. }
  11399. }
  11400. catch (sycl::exception const &exc) {
  11401. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11402. << ", line:" << __LINE__ << std::endl;
  11403. std::exit(1);
  11404. }
  11405. static void ggml_sycl_pool_free(int device_index, void *ptr, size_t size) try {
  11406. if (g_device_caps[device_index].vmm) {
  11407. ggml_sycl_pool_free_vmm(device_index, ptr, size);
  11408. } else {
  11409. ggml_sycl_pool_free_leg(device_index, ptr, size);
  11410. }
  11411. }
  11412. catch (sycl::exception const &exc) {
  11413. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11414. << ", line:" << __LINE__ << std::endl;
  11415. std::exit(1);
  11416. }
  11417. template<typename T>
  11418. struct sycl_pool_alloc {
  11419. int device_index = -1;
  11420. int device_id = -1;
  11421. T * ptr = nullptr;
  11422. size_t actual_size = 0;
  11423. // size is in number of elements
  11424. T * alloc(size_t size) {
  11425. GGML_ASSERT(ptr == nullptr);
  11426. device_id = get_current_device_id();
  11427. device_index = g_sycl_gpu_mgr->get_index(device_id);
  11428. ptr = (T *) ggml_sycl_pool_malloc(device_index, size * sizeof(T), &this->actual_size);
  11429. // GGML_SYCL_DEBUG("sycl_pool_alloc %lu return %p actual size=%lu\n", size * sizeof(T), ptr, this->actual_size);
  11430. return ptr;
  11431. }
  11432. sycl_pool_alloc(size_t size) {
  11433. alloc(size);
  11434. }
  11435. ~sycl_pool_alloc() {
  11436. if (ptr != nullptr) {
  11437. ggml_sycl_pool_free(device_index, ptr, actual_size);
  11438. }
  11439. }
  11440. T * get() {
  11441. return ptr;
  11442. }
  11443. sycl_pool_alloc() = default;
  11444. sycl_pool_alloc(const sycl_pool_alloc &) = delete;
  11445. sycl_pool_alloc(sycl_pool_alloc &&) = delete;
  11446. sycl_pool_alloc& operator=(const sycl_pool_alloc &) = delete;
  11447. sycl_pool_alloc& operator=(sycl_pool_alloc &&) = delete;
  11448. };
  11449. static bool g_sycl_loaded = false;
  11450. bool ggml_sycl_loaded(void) {
  11451. return g_sycl_loaded;
  11452. }
  11453. void print_device_detail(int id) {
  11454. dpct::device_info prop;
  11455. SYCL_CHECK(CHECK_TRY_ERROR(
  11456. dpct::get_device_info(prop, dpct::dev_mgr::instance().get_device(id))));
  11457. sycl::device cur_device = dpct::dev_mgr::instance().get_device(id);
  11458. std::string version;
  11459. version += std::to_string(prop.get_major_version());
  11460. version += ".";
  11461. version += std::to_string(prop.get_minor_version());
  11462. fprintf(stderr, "|%2d|%45s|%18s|%17d|%14d|%13d|%15lu|\n", id,
  11463. prop.get_name(), version.c_str(), prop.get_max_compute_units(),
  11464. prop.get_max_work_group_size(), prop.get_max_sub_group_size(),
  11465. prop.get_global_mem_size());
  11466. }
  11467. void ggml_backend_sycl_print_sycl_devices() {
  11468. int device_count = dpct::dev_mgr::instance().device_count();
  11469. fprintf(stderr, "found %d SYCL devices:\n", device_count);
  11470. fprintf(stderr, "|ID| Name |compute capability|Max compute units|Max work group|Max sub group|Global mem size|\n");
  11471. fprintf(stderr, "|--|---------------------------------------------|------------------|-----------------|--------------|-------------|---------------|\n");
  11472. for (int id = 0; id < device_count; ++id) {
  11473. print_device_detail(id);
  11474. }
  11475. }
  11476. void print_gpu_device_list() {
  11477. fprintf(stderr, "detect %d SYCL GPUs: [%s] with Max compute units:%d\n",
  11478. g_sycl_gpu_mgr->get_gpu_count(),
  11479. g_sycl_gpu_mgr->gpus_list.c_str(),
  11480. g_sycl_gpu_mgr->max_compute_units);
  11481. }
  11482. int get_sycl_env(const char *env_name, int default_val) {
  11483. char *user_device_string = getenv(env_name);
  11484. int user_number = default_val;
  11485. unsigned n;
  11486. if (user_device_string != NULL &&
  11487. sscanf(user_device_string, " %u", &n) == 1) {
  11488. user_number = (int)n;
  11489. } else {
  11490. user_number = default_val;
  11491. }
  11492. return user_number;
  11493. }
  11494. int get_work_group_size(int user_device_id) {
  11495. dpct::device_info prop;
  11496. dpct::get_device_info(prop,
  11497. dpct::dev_mgr::instance().get_device(user_device_id));
  11498. return prop.get_max_work_group_size();
  11499. }
  11500. void ggml_init_sycl() try {
  11501. static bool initialized = false;
  11502. if (!initialized) {
  11503. g_ggml_sycl_debug = get_sycl_env("GGML_SYCL_DEBUG", 0);
  11504. fprintf(stderr, "%s: GGML_SYCL_DEBUG: %d\n", __func__, g_ggml_sycl_debug);
  11505. #if defined(GGML_SYCL_F16)
  11506. fprintf(stderr, "%s: GGML_SYCL_F16: yes\n", __func__);
  11507. #else
  11508. fprintf(stderr, "%s: GGML_SYCL_F16: no\n", __func__);
  11509. #endif
  11510. if (CHECK_TRY_ERROR(g_all_sycl_device_count =
  11511. dpct::dev_mgr::instance().device_count()) != 0) {
  11512. initialized = true;
  11513. g_sycl_loaded = false;
  11514. return;
  11515. }
  11516. GGML_ASSERT(g_all_sycl_device_count <= GGML_SYCL_MAX_DEVICES);
  11517. ggml_backend_sycl_print_sycl_devices();
  11518. if (!g_sycl_gpu_mgr) g_sycl_gpu_mgr = new sycl_gpu_mgr();
  11519. g_device_count = g_sycl_gpu_mgr->get_gpu_count();
  11520. g_work_group_size = g_sycl_gpu_mgr->work_group_size;
  11521. print_gpu_device_list();
  11522. int64_t total_vram = 0;
  11523. /* NOT REMOVE, keep it for next optimize for XMX.
  11524. #if defined(SYCL_USE_XMX)
  11525. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  11526. #else
  11527. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  11528. #endif
  11529. */
  11530. for (int id = 0; id < GGML_SYCL_MAX_DEVICES; ++id) {
  11531. g_device_caps[id].vmm = 0;
  11532. g_device_caps[id].device_id = -1;
  11533. g_device_caps[id].cc = 0;
  11534. g_tensor_split[id] = 0;
  11535. g_default_tensor_split[id] = 0;
  11536. }
  11537. for (int i = 0; i < g_device_count; ++i) {
  11538. int device_id = g_sycl_gpu_mgr->gpus[i];
  11539. g_device_caps[i].vmm = 0;
  11540. dpct::device_info prop;
  11541. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  11542. prop, dpct::dev_mgr::instance().get_device(device_id))));
  11543. g_default_tensor_split[i] = total_vram;
  11544. total_vram += prop.get_global_mem_size();
  11545. g_device_caps[i].cc =
  11546. 100 * prop.get_major_version() + 10 * prop.get_minor_version();
  11547. }
  11548. for (int i = 0; i < g_device_count; ++i) {
  11549. g_default_tensor_split[i] /= total_vram;
  11550. }
  11551. for (int i = 0; i < g_device_count; ++i) {
  11552. SYCL_CHECK(ggml_sycl_set_device(i));
  11553. // create sycl streams
  11554. for (int is = 0; is < MAX_STREAMS; ++is) {
  11555. SYCL_CHECK(CHECK_TRY_ERROR(
  11556. g_syclStreams[i][is] =
  11557. dpct::get_current_device().create_queue(
  11558. g_sycl_gpu_mgr->get_co_ctx(), dpct::get_current_device())));
  11559. }
  11560. const dpct::queue_ptr stream = g_syclStreams[i][0];
  11561. // create sycl handle
  11562. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[i] = stream));
  11563. }
  11564. initialized = true;
  11565. g_sycl_loaded = true;
  11566. }
  11567. }
  11568. catch (sycl::exception const &exc) {
  11569. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11570. << ", line:" << __LINE__ << std::endl;
  11571. std::exit(1);
  11572. }
  11573. void *ggml_sycl_host_malloc(size_t size) try {
  11574. if (getenv("GGML_SYCL_NO_PINNED") != nullptr) {
  11575. return nullptr;
  11576. }
  11577. void * ptr = nullptr;
  11578. //allow to use dpct::get_in_order_queue() for host malloc
  11579. dpct::err0 err = CHECK_TRY_ERROR(
  11580. ptr = (void *)sycl::malloc_host(size, dpct::get_in_order_queue()));
  11581. if (err != 0) {
  11582. // clear the error
  11583. fprintf(
  11584. stderr,
  11585. "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  11586. size / 1024.0 / 1024.0,
  11587. "syclGetErrorString is not supported");
  11588. return nullptr;
  11589. }
  11590. return ptr;
  11591. }
  11592. catch (sycl::exception const &exc) {
  11593. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11594. << ", line:" << __LINE__ << std::endl;
  11595. std::exit(1);
  11596. }
  11597. void ggml_sycl_host_free(void *ptr) try {
  11598. //allow to use dpct::get_in_order_queue() for host malloc
  11599. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, dpct::get_in_order_queue())));
  11600. }
  11601. catch (sycl::exception const &exc) {
  11602. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11603. << ", line:" << __LINE__ << std::endl;
  11604. std::exit(1);
  11605. }
  11606. static dpct::err0 ggml_sycl_cpy_tensor_2d(void *dst,
  11607. const struct ggml_tensor *src,
  11608. int64_t i3, int64_t i2,
  11609. int64_t i1_low, int64_t i1_high,
  11610. dpct::queue_ptr stream) try {
  11611. dpct::memcpy_direction kind;
  11612. char * src_ptr;
  11613. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  11614. kind = dpct::host_to_device;
  11615. src_ptr = (char *) src->data;
  11616. // GGML_SYCL_DEBUG("ggml_sycl_cpy_tensor_2d GGML_BACKEND_TYPE_CPU src_ptr %p\n", src_ptr);
  11617. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  11618. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  11619. kind = dpct::device_to_device;
  11620. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  11621. int id;
  11622. SYCL_CHECK(CHECK_TRY_ERROR(
  11623. id = get_current_device_id()));
  11624. // GGML_SYCL_DEBUG("current device index %d\n", id);
  11625. src_ptr = (char *) extra->data_device[id];
  11626. } else {
  11627. // GGML_SYCL_DEBUG("GGML_ASSERT(false)\n");
  11628. GGML_ASSERT(false);
  11629. }
  11630. char * dst_ptr = (char *) dst;
  11631. GGML_TENSOR_LOCALS_1(int64_t, ne, src, ne);
  11632. GGML_TENSOR_LOCALS(int64_t, nb, src, nb);
  11633. const enum ggml_type type = src->type;
  11634. const int64_t ts = ggml_type_size(type);
  11635. const int64_t bs = ggml_blck_size(type);
  11636. int64_t i1_diff = i1_high - i1_low;
  11637. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  11638. if (nb0 == ts && nb1 == ts*ne0/bs) {
  11639. // GGML_SYCL_DEBUG("stream->memcpy: dst_ptr=%p, x=%p, size=%lu\n", dst_ptr, x, i1_diff * nb1);
  11640. // return CHECK_TRY_ERROR(stream->memcpy(dst_ptr, x, i1_diff * nb1));
  11641. return CHECK_TRY_ERROR(dpct::async_dpct_memcpy(dst_ptr, x, i1_diff * nb1,
  11642. kind, *stream));
  11643. } else if (nb0 == ts) {
  11644. return CHECK_TRY_ERROR(
  11645. dpct::async_dpct_memcpy(dst_ptr, ts * ne0 / bs, x, nb1,
  11646. ts * ne0 / bs, i1_diff, kind, *stream));
  11647. } else {
  11648. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  11649. const void * rx = (const void *) ((const char *) x + i1*nb1);
  11650. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  11651. // pretend the row is a matrix with cols=1
  11652. dpct::err0 r = CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  11653. rd, ts / bs, rx, nb0, ts / bs, ne0, kind, *stream));
  11654. /*
  11655. DPCT1001:85: The statement could not be removed.
  11656. */
  11657. /*
  11658. DPCT1000:86: Error handling if-stmt was detected but could not be
  11659. rewritten.
  11660. */
  11661. if (r != 0) return r;
  11662. }
  11663. return 0;
  11664. }
  11665. }
  11666. catch (sycl::exception const &exc) {
  11667. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11668. << ", line:" << __LINE__ << std::endl;
  11669. std::exit(1);
  11670. }
  11671. static void ggml_sycl_op_get_rows(const ggml_tensor *src0,
  11672. const ggml_tensor *src1, ggml_tensor *dst,
  11673. const float *src0_d, const float *src1_d,
  11674. float *dst_d, const dpct::queue_ptr &stream) {
  11675. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  11676. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11677. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  11678. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  11679. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  11680. const int32_t * src1_i32 = (const int32_t *) src1_d;
  11681. switch (src0->type) {
  11682. case GGML_TYPE_F16:
  11683. get_rows_sycl_float(src0, src1, dst, (const sycl::half *)src0_d,
  11684. src1_i32, dst_d, stream);
  11685. break;
  11686. case GGML_TYPE_F32:
  11687. get_rows_sycl_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11688. break;
  11689. case GGML_TYPE_Q4_0:
  11690. get_rows_sycl<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11691. break;
  11692. case GGML_TYPE_Q4_1:
  11693. get_rows_sycl<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11694. break;
  11695. case GGML_TYPE_Q5_0:
  11696. get_rows_sycl<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11697. break;
  11698. case GGML_TYPE_Q5_1:
  11699. get_rows_sycl<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11700. break;
  11701. case GGML_TYPE_Q8_0:
  11702. get_rows_sycl<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11703. break;
  11704. default:
  11705. // TODO: k-quants
  11706. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  11707. GGML_ASSERT(false);
  11708. break;
  11709. }
  11710. }
  11711. template <class op>
  11712. inline void ggml_sycl_op_bin_bcast(const ggml_tensor *src0,
  11713. const ggml_tensor *src1, ggml_tensor *dst,
  11714. const float *src0_dd, const float *src1_dd,
  11715. float *dst_dd,
  11716. const dpct::queue_ptr &main_stream) {
  11717. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  11718. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11719. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  11720. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd,
  11721. (sycl::half *)dst_dd, main_stream);
  11722. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  11723. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd, dst_dd,
  11724. main_stream);
  11725. } else if (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  11726. op()(src0, src1, dst, (const int32_t *)src0_dd, (const int32_t *)src1_dd, (int32_t *)dst_dd,
  11727. main_stream);
  11728. } else if (src0->type == GGML_TYPE_I16 && dst->type == GGML_TYPE_I16) {
  11729. op()(src0, src1, dst, (const int16_t *)src0_dd, (const int16_t *)src1_dd, (int16_t *)dst_dd,
  11730. main_stream);
  11731. } else {
  11732. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  11733. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  11734. GGML_ASSERT(false);
  11735. }
  11736. }
  11737. static void ggml_sycl_op_repeat(const ggml_tensor *src0,
  11738. const ggml_tensor *src1, ggml_tensor *dst,
  11739. const float *src0_d, const float *src1_d,
  11740. float *dst_d,
  11741. const dpct::queue_ptr &main_stream) {
  11742. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  11743. (void) src1;
  11744. (void) src1_d;
  11745. }
  11746. inline void ggml_sycl_op_add(const ggml_tensor *src0, const ggml_tensor *src1,
  11747. ggml_tensor *dst, const float *src0_dd,
  11748. const float *src1_dd, float *dst_dd,
  11749. const dpct::queue_ptr &main_stream) {
  11750. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11751. }
  11752. inline void ggml_sycl_op_acc(const ggml_tensor *src0, const ggml_tensor *src1,
  11753. ggml_tensor *dst, const float *src0_dd,
  11754. const float *src1_dd, float *dst_dd,
  11755. const dpct::queue_ptr &main_stream) {
  11756. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11757. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11758. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11759. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  11760. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  11761. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  11762. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  11763. int offset = dst->op_params[3] / 4; // offset in bytes
  11764. 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);
  11765. (void) dst;
  11766. }
  11767. inline void ggml_sycl_op_mul(const ggml_tensor *src0, const ggml_tensor *src1,
  11768. ggml_tensor *dst, const float *src0_dd,
  11769. const float *src1_dd, float *dst_dd,
  11770. const dpct::queue_ptr &main_stream) {
  11771. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11772. }
  11773. inline void ggml_sycl_op_div(const ggml_tensor *src0, const ggml_tensor *src1,
  11774. ggml_tensor *dst, const float *src0_dd,
  11775. const float *src1_dd, float *dst_dd,
  11776. const dpct::queue_ptr &main_stream) {
  11777. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11778. }
  11779. inline void ggml_sycl_op_gelu(const ggml_tensor *src0, const ggml_tensor *src1,
  11780. ggml_tensor *dst, const float *src0_dd,
  11781. const float *src1_dd, float *dst_dd,
  11782. const dpct::queue_ptr &main_stream) {
  11783. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11784. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11785. gelu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11786. (void) src1;
  11787. (void) dst;
  11788. (void) src1_dd;
  11789. }
  11790. inline void ggml_sycl_op_silu(const ggml_tensor *src0, const ggml_tensor *src1,
  11791. ggml_tensor *dst, const float *src0_dd,
  11792. const float *src1_dd, float *dst_dd,
  11793. const dpct::queue_ptr &main_stream) {
  11794. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11795. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11796. silu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11797. (void) src1;
  11798. (void) dst;
  11799. (void) src1_dd;
  11800. }
  11801. inline void ggml_sycl_op_gelu_quick(const ggml_tensor *src0,
  11802. const ggml_tensor *src1, ggml_tensor *dst,
  11803. const float *src0_dd, const float *src1_dd,
  11804. float *dst_dd,
  11805. const dpct::queue_ptr &main_stream) {
  11806. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11807. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11808. gelu_quick_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11809. (void) src1;
  11810. (void) dst;
  11811. (void) src1_dd;
  11812. }
  11813. inline void ggml_sycl_op_tanh(const ggml_tensor *src0, const ggml_tensor *src1,
  11814. ggml_tensor *dst, const float *src0_dd,
  11815. const float *src1_dd, float *dst_dd,
  11816. const dpct::queue_ptr &main_stream) {
  11817. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11818. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11819. tanh_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11820. (void) src1;
  11821. (void) dst;
  11822. (void) src1_dd;
  11823. }
  11824. inline void ggml_sycl_op_relu(const ggml_tensor *src0, const ggml_tensor *src1,
  11825. ggml_tensor *dst, const float *src0_dd,
  11826. const float *src1_dd, float *dst_dd,
  11827. const dpct::queue_ptr &main_stream) {
  11828. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11829. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11830. relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11831. (void) src1;
  11832. (void) dst;
  11833. (void) src1_dd;
  11834. }
  11835. static void ggml_sycl_op_hardsigmoid(const ggml_tensor *src0,
  11836. const ggml_tensor *src1, ggml_tensor *dst,
  11837. const float *src0_dd, const float *src1_dd,
  11838. float *dst_dd,
  11839. const dpct::queue_ptr &main_stream) {
  11840. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11841. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11842. hardsigmoid_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11843. (void) src1;
  11844. (void) dst;
  11845. (void) src1_dd;
  11846. }
  11847. static void ggml_sycl_op_hardswish(const ggml_tensor *src0,
  11848. const ggml_tensor *src1, ggml_tensor *dst,
  11849. const float *src0_dd, const float *src1_dd,
  11850. float *dst_dd, const dpct::queue_ptr &main_stream) {
  11851. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11852. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11853. hardswish_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11854. (void) src1;
  11855. (void) dst;
  11856. (void) src1_dd;
  11857. }
  11858. inline void ggml_sycl_op_leaky_relu(const ggml_tensor *src0,
  11859. const ggml_tensor *src1, ggml_tensor *dst,
  11860. const float *src0_dd, const float *src1_dd,
  11861. float *dst_dd,
  11862. const dpct::queue_ptr &main_stream) {
  11863. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11864. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11865. float negative_slope;
  11866. memcpy(&negative_slope, dst->op_params, sizeof(float));
  11867. leaky_relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  11868. (void) src1;
  11869. (void) dst;
  11870. (void) src1_dd;
  11871. }
  11872. inline void ggml_sycl_op_sqr(const ggml_tensor *src0, const ggml_tensor *src1,
  11873. ggml_tensor *dst, const float *src0_dd,
  11874. const float *src1_dd, float *dst_dd,
  11875. const dpct::queue_ptr &main_stream) {
  11876. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11877. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11878. sqr_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11879. (void) src1;
  11880. (void) dst;
  11881. (void) src1_dd;
  11882. }
  11883. inline void ggml_sycl_op_norm(const ggml_tensor *src0, const ggml_tensor *src1,
  11884. ggml_tensor *dst, const float *src0_dd,
  11885. const float *src1_dd, float *dst_dd,
  11886. const dpct::queue_ptr &main_stream) {
  11887. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11888. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11889. const int64_t ne00 = src0->ne[0];
  11890. const int64_t nrows = ggml_nrows(src0);
  11891. float eps;
  11892. memcpy(&eps, dst->op_params, sizeof(float));
  11893. norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  11894. (void) src1;
  11895. (void) dst;
  11896. (void) src1_dd;
  11897. }
  11898. inline void ggml_sycl_op_group_norm(const ggml_tensor *src0,
  11899. const ggml_tensor *src1, ggml_tensor *dst,
  11900. const float *src0_dd, const float *src1_dd,
  11901. float *dst_dd,
  11902. const dpct::queue_ptr &main_stream) {
  11903. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11904. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11905. int num_groups = dst->op_params[0];
  11906. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  11907. group_norm_f32_sycl(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  11908. (void) src1;
  11909. (void) dst;
  11910. (void) src1_dd;
  11911. }
  11912. inline void ggml_sycl_op_concat(const ggml_tensor *src0,
  11913. const ggml_tensor *src1, ggml_tensor *dst,
  11914. const float *src0_dd, const float *src1_dd,
  11915. float *dst_dd,
  11916. const dpct::queue_ptr &main_stream) {
  11917. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11918. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11919. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11920. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  11921. 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);
  11922. }
  11923. (void) src1;
  11924. (void) dst;
  11925. }
  11926. inline void ggml_sycl_op_upscale(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. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  11934. const int scale_factor = dst->op_params[0];
  11935. upscale_f32_sycl(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  11936. (void) src1;
  11937. (void) dst;
  11938. (void) src1_dd;
  11939. }
  11940. inline void ggml_sycl_op_pad(const ggml_tensor *src0, const ggml_tensor *src1,
  11941. ggml_tensor *dst, const float *src0_dd,
  11942. const float *src1_dd, float *dst_dd,
  11943. const dpct::queue_ptr &main_stream) {
  11944. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11945. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11946. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  11947. pad_f32_sycl(src0_dd, dst_dd,
  11948. src0->ne[0], src0->ne[1], src0->ne[2],
  11949. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  11950. (void) src1;
  11951. (void) dst;
  11952. (void) src1_dd;
  11953. }
  11954. inline void ggml_sycl_op_rms_norm(const ggml_tensor *src0,
  11955. const ggml_tensor *src1, ggml_tensor *dst,
  11956. const float *src0_dd, const float *src1_dd,
  11957. float *dst_dd,
  11958. const dpct::queue_ptr &main_stream) {
  11959. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11960. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11961. const int64_t ne00 = src0->ne[0];
  11962. const int64_t nrows = ggml_nrows(src0);
  11963. float eps;
  11964. memcpy(&eps, dst->op_params, sizeof(float));
  11965. rms_norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  11966. (void) src1;
  11967. (void) dst;
  11968. (void) src1_dd;
  11969. }
  11970. inline void ggml_sycl_op_mul_mat_q(
  11971. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11972. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11973. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11974. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11975. const dpct::queue_ptr &stream) try {
  11976. const int64_t ne00 = src0->ne[0];
  11977. const int64_t ne10 = src1->ne[0];
  11978. GGML_ASSERT(ne10 % QK8_1 == 0);
  11979. const int64_t ne0 = dst->ne[0];
  11980. const int64_t row_diff = row_high - row_low;
  11981. int device_id;
  11982. SYCL_CHECK(
  11983. CHECK_TRY_ERROR(device_id = get_current_device_id()));
  11984. // the main device has a larger memory buffer to hold the results from all GPUs
  11985. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  11986. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && device_id == g_main_device ? ne0 : row_diff;
  11987. switch (src0->type) {
  11988. case GGML_TYPE_Q4_0:
  11989. 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);
  11990. break;
  11991. case GGML_TYPE_Q4_1:
  11992. 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);
  11993. break;
  11994. case GGML_TYPE_Q5_0:
  11995. 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);
  11996. break;
  11997. case GGML_TYPE_Q5_1:
  11998. 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);
  11999. break;
  12000. case GGML_TYPE_Q8_0:
  12001. 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);
  12002. break;
  12003. case GGML_TYPE_Q2_K:
  12004. 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);
  12005. break;
  12006. case GGML_TYPE_Q3_K:
  12007. 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);
  12008. break;
  12009. case GGML_TYPE_Q4_K:
  12010. 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);
  12011. break;
  12012. case GGML_TYPE_Q5_K:
  12013. 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);
  12014. break;
  12015. case GGML_TYPE_Q6_K:
  12016. 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);
  12017. break;
  12018. default:
  12019. GGML_ASSERT(false);
  12020. break;
  12021. }
  12022. (void) src1;
  12023. (void) dst;
  12024. (void) src1_ddf_i;
  12025. }
  12026. catch (sycl::exception const &exc) {
  12027. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12028. << ", line:" << __LINE__ << std::endl;
  12029. std::exit(1);
  12030. }
  12031. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split) {
  12032. int64_t min_compute_capability = INT_MAX;
  12033. int64_t max_compute_capability = INT_MIN;
  12034. for (int i = 0; i < g_device_count; ++i) {
  12035. if (tensor_split[i] < (i + 1 < g_device_count ? tensor_split[i + 1] : 1.0f)) {
  12036. if (min_compute_capability > g_device_caps[i].cc) {
  12037. min_compute_capability = g_device_caps[i].cc;
  12038. }
  12039. if (max_compute_capability < g_device_caps[i].cc) {
  12040. max_compute_capability = g_device_caps[i].cc;
  12041. }
  12042. }
  12043. }
  12044. switch(type) {
  12045. case GGML_TYPE_Q4_0:
  12046. case GGML_TYPE_Q4_1:
  12047. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  12048. case GGML_TYPE_Q5_0:
  12049. case GGML_TYPE_Q5_1:
  12050. case GGML_TYPE_Q8_0:
  12051. return 64;
  12052. case GGML_TYPE_F16:
  12053. case GGML_TYPE_F32:
  12054. return 1;
  12055. case GGML_TYPE_Q2_K:
  12056. case GGML_TYPE_Q3_K:
  12057. case GGML_TYPE_Q4_K:
  12058. case GGML_TYPE_Q5_K:
  12059. case GGML_TYPE_IQ2_XXS:
  12060. case GGML_TYPE_IQ2_XS:
  12061. case GGML_TYPE_IQ3_XXS:
  12062. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  12063. case GGML_TYPE_Q6_K:
  12064. return 64;
  12065. default:
  12066. GGML_ASSERT(false);
  12067. }
  12068. }
  12069. inline void ggml_sycl_op_mul_mat_vec_q(
  12070. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  12071. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  12072. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  12073. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  12074. const dpct::queue_ptr &stream) {
  12075. GGML_ASSERT(ggml_nrows(src1) == 1);
  12076. const int64_t ne00 = src0->ne[0];
  12077. const int64_t row_diff = row_high - row_low;
  12078. switch (src0->type) {
  12079. case GGML_TYPE_Q4_0:
  12080. mul_mat_vec_q4_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12081. break;
  12082. case GGML_TYPE_Q4_1:
  12083. mul_mat_vec_q4_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12084. break;
  12085. case GGML_TYPE_Q5_0:
  12086. mul_mat_vec_q5_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12087. break;
  12088. case GGML_TYPE_Q5_1:
  12089. mul_mat_vec_q5_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12090. break;
  12091. case GGML_TYPE_Q8_0:
  12092. mul_mat_vec_q8_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12093. break;
  12094. case GGML_TYPE_Q2_K:
  12095. mul_mat_vec_q2_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12096. break;
  12097. case GGML_TYPE_Q3_K:
  12098. mul_mat_vec_q3_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12099. break;
  12100. case GGML_TYPE_Q4_K:
  12101. mul_mat_vec_q4_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12102. break;
  12103. case GGML_TYPE_Q5_K:
  12104. mul_mat_vec_q5_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12105. break;
  12106. case GGML_TYPE_Q6_K:
  12107. mul_mat_vec_q6_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12108. break;
  12109. case GGML_TYPE_IQ2_XXS:
  12110. mul_mat_vec_iq2_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12111. break;
  12112. case GGML_TYPE_IQ2_XS:
  12113. mul_mat_vec_iq2_xs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12114. break;
  12115. case GGML_TYPE_IQ3_XXS:
  12116. mul_mat_vec_iq3_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  12117. break;
  12118. default:
  12119. GGML_ASSERT(false);
  12120. break;
  12121. }
  12122. (void) src1;
  12123. (void) dst;
  12124. (void) src1_ddf_i;
  12125. (void) src1_ncols;
  12126. (void) src1_padded_row_size;
  12127. }
  12128. inline void ggml_sycl_op_dequantize_mul_mat_vec(
  12129. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  12130. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  12131. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  12132. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  12133. const dpct::queue_ptr &stream) {
  12134. const int64_t ne00 = src0->ne[0];
  12135. const int64_t row_diff = row_high - row_low;
  12136. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12137. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  12138. #ifdef GGML_SYCL_F16
  12139. sycl_pool_alloc<sycl::half> src1_dfloat_a;
  12140. sycl::half *src1_dfloat = nullptr; // dfloat == half
  12141. bool src1_convert_f16 =
  12142. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  12143. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  12144. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  12145. if (src1_convert_f16) {
  12146. src1_dfloat = src1_dfloat_a.alloc(ne00);
  12147. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  12148. GGML_ASSERT(to_fp16_sycl != nullptr);
  12149. to_fp16_sycl(src1_ddf_i, src1_dfloat, ne00, stream);
  12150. }
  12151. #else
  12152. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  12153. #endif // GGML_SYCL_F16
  12154. switch (src0->type) {
  12155. case GGML_TYPE_Q4_0:
  12156. dequantize_mul_mat_vec_q4_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12157. break;
  12158. case GGML_TYPE_Q4_1:
  12159. dequantize_mul_mat_vec_q4_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12160. break;
  12161. case GGML_TYPE_Q5_0:
  12162. dequantize_mul_mat_vec_q5_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12163. break;
  12164. case GGML_TYPE_Q5_1:
  12165. dequantize_mul_mat_vec_q5_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12166. break;
  12167. case GGML_TYPE_Q8_0:
  12168. dequantize_mul_mat_vec_q8_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12169. break;
  12170. case GGML_TYPE_Q2_K:
  12171. dequantize_mul_mat_vec_q2_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12172. break;
  12173. case GGML_TYPE_Q3_K:
  12174. dequantize_mul_mat_vec_q3_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12175. break;
  12176. case GGML_TYPE_Q4_K:
  12177. dequantize_mul_mat_vec_q4_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12178. break;
  12179. case GGML_TYPE_Q5_K:
  12180. dequantize_mul_mat_vec_q5_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12181. break;
  12182. case GGML_TYPE_Q6_K:
  12183. dequantize_mul_mat_vec_q6_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  12184. break;
  12185. case GGML_TYPE_F16:
  12186. convert_mul_mat_vec_f16_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  12187. break;
  12188. default:
  12189. GGML_ASSERT(false);
  12190. break;
  12191. }
  12192. (void) src1;
  12193. (void) dst;
  12194. (void) src1_ddq_i;
  12195. (void) src1_ncols;
  12196. (void) src1_padded_row_size;
  12197. }
  12198. inline void ggml_sycl_op_mul_mat_sycl(
  12199. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  12200. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  12201. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  12202. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  12203. const dpct::queue_ptr &stream) try {
  12204. GGML_ASSERT(src0_dd_i != nullptr);
  12205. GGML_ASSERT(src1_ddf_i != nullptr);
  12206. GGML_ASSERT(dst_dd_i != nullptr);
  12207. const int64_t ne00 = src0->ne[0];
  12208. const int64_t ne10 = src1->ne[0];
  12209. const int64_t ne0 = dst->ne[0];
  12210. const int64_t row_diff = row_high - row_low;
  12211. int id;
  12212. SYCL_CHECK(
  12213. CHECK_TRY_ERROR(id = get_current_device_id()));
  12214. // the main device has a larger memory buffer to hold the results from all GPUs
  12215. // ldc == nrows of the matrix that cuBLAS writes into
  12216. int ldc = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  12217. #ifdef GGML_SYCL_F16
  12218. bool use_fp16 = true; // TODO(Yu) SYCL capability check
  12219. #else
  12220. bool use_fp16 = false;
  12221. #endif
  12222. if ((src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  12223. use_fp16 && ggml_is_contiguous(src0) && row_diff == src0->ne[1] &&
  12224. dst->op_params[0] == GGML_PREC_DEFAULT) {
  12225. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp16 path\n");
  12226. sycl_pool_alloc<sycl::half> src0_as_f16;
  12227. if (src0->type != GGML_TYPE_F16) {
  12228. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src0->type);
  12229. GGML_ASSERT(to_fp16_sycl != nullptr);
  12230. size_t ne = row_diff*ne00;
  12231. src0_as_f16.alloc(ne);
  12232. to_fp16_sycl(src0_dd_i, src0_as_f16.get(), ne, stream);
  12233. }
  12234. const sycl::half *src0_ptr = src0->type == GGML_TYPE_F16
  12235. ? (const sycl::half *)src0_dd_i
  12236. : src0_as_f16.get();
  12237. sycl_pool_alloc<sycl::half> src1_as_f16;
  12238. if (src1->type != GGML_TYPE_F16) {
  12239. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  12240. GGML_ASSERT(to_fp16_sycl != nullptr);
  12241. size_t ne = src1_ncols*ne10;
  12242. src1_as_f16.alloc(ne);
  12243. to_fp16_sycl(src1_ddf_i, src1_as_f16.get(), ne, stream);
  12244. }
  12245. const sycl::half *src1_ptr = src1->type == GGML_TYPE_F16
  12246. ? (const sycl::half *)src1->data + src1_padded_row_size
  12247. : src1_as_f16.get();
  12248. sycl_pool_alloc<sycl::half> dst_f16(row_diff * src1_ncols);
  12249. const sycl::half alpha_f16 = 1.0f;
  12250. const sycl::half beta_f16 = 0.0f;
  12251. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  12252. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
  12253. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  12254. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  12255. &alpha_f16, src0_ptr, dpct::library_data_t::real_half, ne00,
  12256. src1_ptr, dpct::library_data_t::real_half, ne10, &beta_f16,
  12257. dst_f16.get(), dpct::library_data_t::real_half, ldc,
  12258. dpct::library_data_t::real_half)));
  12259. g_sycl_handles[id]->wait();
  12260. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  12261. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  12262. }
  12263. else {
  12264. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
  12265. sycl_pool_alloc<float> src0_ddq_as_f32;
  12266. sycl_pool_alloc<float> src1_ddq_as_f32;
  12267. if (src0->type != GGML_TYPE_F32) {
  12268. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src0->type);
  12269. GGML_ASSERT(to_fp32_sycl != nullptr);
  12270. src0_ddq_as_f32.alloc(row_diff*ne00);
  12271. to_fp32_sycl(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  12272. }
  12273. if (src1->type != GGML_TYPE_F32) {
  12274. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src1->type);
  12275. GGML_ASSERT(to_fp32_sycl != nullptr);
  12276. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  12277. to_fp32_sycl(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  12278. }
  12279. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  12280. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  12281. const float alpha = 1.0f;
  12282. const float beta = 0.0f;
  12283. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  12284. SYCL_CHECK(CHECK_TRY_ERROR(oneapi::mkl::blas::column_major::gemm(
  12285. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  12286. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  12287. dpct::get_value(&alpha, *g_sycl_handles[id]), src0_ddf_i, ne00,
  12288. src1_ddf1_i, ne10, dpct::get_value(&beta, *g_sycl_handles[id]),
  12289. dst_dd_i, ldc)));
  12290. g_sycl_handles[id]->wait();
  12291. }
  12292. (void) dst;
  12293. (void) src1_ddq_i;
  12294. (void) src1_padded_row_size;
  12295. }
  12296. catch (sycl::exception const &exc) {
  12297. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12298. << ", line:" << __LINE__ << std::endl;
  12299. std::exit(1);
  12300. }
  12301. inline void ggml_sycl_op_rope(const ggml_tensor *src0, const ggml_tensor *src1,
  12302. ggml_tensor *dst, const float *src0_dd,
  12303. const float *src1_dd, float *dst_dd,
  12304. const dpct::queue_ptr &main_stream) {
  12305. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  12306. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  12307. GGML_ASSERT(src0->type == dst->type);
  12308. const int64_t ne00 = src0->ne[0];
  12309. const int64_t ne01 = src0->ne[1];
  12310. const int64_t ne2 = dst->ne[2];
  12311. const int64_t nrows = ggml_nrows(src0);
  12312. //const int n_past = ((int32_t *) dst->op_params)[0];
  12313. const int n_dims = ((int32_t *) dst->op_params)[1];
  12314. const int mode = ((int32_t *) dst->op_params)[2];
  12315. const int n_ctx = ((int32_t *) dst->op_params)[3];
  12316. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  12317. // RoPE alteration for extended context
  12318. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  12319. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  12320. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  12321. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  12322. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  12323. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  12324. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  12325. const int32_t * pos = nullptr;
  12326. if ((mode & 1) == 0) {
  12327. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  12328. GGML_ASSERT(src1->ne[0] == ne2);
  12329. pos = (const int32_t *) src1_dd;
  12330. }
  12331. const bool is_neox = mode & 2;
  12332. const bool is_glm = mode & 4;
  12333. rope_corr_dims corr_dims;
  12334. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  12335. // compute
  12336. if (is_glm) {
  12337. GGML_ASSERT(false);
  12338. rope_glm_f32_sycl(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  12339. } else if (is_neox) {
  12340. if (src0->type == GGML_TYPE_F32) {
  12341. rope_neox_sycl(
  12342. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12343. attn_factor, corr_dims, main_stream
  12344. );
  12345. } else if (src0->type == GGML_TYPE_F16) {
  12346. rope_neox_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd,
  12347. ne00, n_dims, nrows, pos, freq_scale, ne01,
  12348. freq_base, ext_factor, attn_factor, corr_dims,
  12349. main_stream);
  12350. } else {
  12351. GGML_ASSERT(false);
  12352. }
  12353. } else {
  12354. if (src0->type == GGML_TYPE_F32) {
  12355. rope_sycl(
  12356. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12357. attn_factor, corr_dims, main_stream
  12358. );
  12359. } else if (src0->type == GGML_TYPE_F16) {
  12360. rope_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd, ne00,
  12361. nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12362. attn_factor, corr_dims, main_stream);
  12363. } else {
  12364. GGML_ASSERT(false);
  12365. }
  12366. }
  12367. (void) src1;
  12368. (void) dst;
  12369. (void) src1_dd;
  12370. }
  12371. inline void ggml_sycl_op_alibi(const ggml_tensor *src0, const ggml_tensor *src1,
  12372. ggml_tensor *dst, const float *src0_dd,
  12373. const float *src1_dd, float *dst_dd,
  12374. const dpct::queue_ptr &main_stream) {
  12375. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12376. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12377. GGML_TENSOR_LOCALS_3(int64_t, ne0, src0, ne);
  12378. const int64_t nrows = ggml_nrows(src0);
  12379. //const int n_past = ((int32_t *) dst->op_params)[0];
  12380. const int n_head = ((int32_t *) dst->op_params)[1];
  12381. float max_bias;
  12382. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  12383. //GGML_ASSERT(ne01 + n_past == ne00);
  12384. GGML_ASSERT(n_head == ne02);
  12385. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  12386. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  12387. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  12388. alibi_f32_sycl(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  12389. (void) src1;
  12390. (void) src1_dd;
  12391. }
  12392. static void ggml_sycl_op_pool2d(const ggml_tensor *src0,
  12393. const ggml_tensor *src1, ggml_tensor *dst,
  12394. const float *src0_dd, const float *src1_dd,
  12395. float *dst_dd, const dpct::queue_ptr &main_stream) {
  12396. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12397. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12398. const int32_t * opts = (const int32_t *)dst->op_params;
  12399. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  12400. const int k0 = opts[1];
  12401. const int k1 = opts[2];
  12402. const int s0 = opts[3];
  12403. const int s1 = opts[4];
  12404. const int p0 = opts[5];
  12405. const int p1 = opts[6];
  12406. const int64_t IH = src0->ne[1];
  12407. const int64_t IW = src0->ne[0];
  12408. const int64_t N = dst->ne[3];
  12409. const int64_t OC = dst->ne[2];
  12410. const int64_t OH = dst->ne[1];
  12411. const int64_t OW = dst->ne[0];
  12412. const int parallel_elements = N * OC * OH * OW;
  12413. const int num_blocks = (parallel_elements + SYCL_POOL2D_BLOCK_SIZE - 1) / SYCL_POOL2D_BLOCK_SIZE;
  12414. sycl::range<3> block_nums(1, 1, num_blocks);
  12415. main_stream->parallel_for(
  12416. sycl::nd_range<3>(block_nums *
  12417. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  12418. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  12419. [=](sycl::nd_item<3> item_ct1) {
  12420. pool2d_nchw_kernel(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0,
  12421. parallel_elements, src0_dd, dst_dd, op,
  12422. item_ct1);
  12423. });
  12424. (void) src1;
  12425. (void) src1_dd;
  12426. }
  12427. inline void ggml_sycl_op_im2col(const ggml_tensor *src0,
  12428. const ggml_tensor *src1, ggml_tensor *dst,
  12429. const float *src0_dd, const float *src1_dd,
  12430. float *dst_dd,
  12431. const dpct::queue_ptr &main_stream) {
  12432. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12433. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12434. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  12435. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  12436. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  12437. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  12438. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  12439. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  12440. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  12441. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  12442. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  12443. const int64_t IH = is_2D ? src1->ne[1] : 1;
  12444. const int64_t IW = src1->ne[0];
  12445. const int64_t KH = is_2D ? src0->ne[1] : 1;
  12446. const int64_t KW = src0->ne[0];
  12447. const int64_t OH = is_2D ? dst->ne[2] : 1;
  12448. const int64_t OW = dst->ne[1];
  12449. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  12450. if (dst->type == GGML_TYPE_F16) {
  12451. 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);
  12452. } else {
  12453. im2col_sycl(src1_dd, (float *)dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  12454. }
  12455. (void) src0;
  12456. (void) src0_dd;
  12457. }
  12458. inline void ggml_sycl_op_sum_rows(const ggml_tensor *src0,
  12459. const ggml_tensor *src1, ggml_tensor *dst,
  12460. const float *src0_dd, const float *src1_dd,
  12461. float *dst_dd,
  12462. const dpct::queue_ptr &main_stream) {
  12463. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12464. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12465. const int64_t ncols = src0->ne[0];
  12466. const int64_t nrows = ggml_nrows(src0);
  12467. sum_rows_f32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
  12468. (void) src1;
  12469. (void) dst;
  12470. (void) src1_dd;
  12471. }
  12472. inline void ggml_sycl_op_argsort(const ggml_tensor *src0,
  12473. const ggml_tensor *src1, ggml_tensor *dst,
  12474. const float *src0_dd, const float *src1_dd,
  12475. float *dst_dd,
  12476. const dpct::queue_ptr &main_stream) {
  12477. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12478. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  12479. const int64_t ncols = src0->ne[0];
  12480. const int64_t nrows = ggml_nrows(src0);
  12481. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  12482. argsort_f32_i32_sycl(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  12483. (void) src1;
  12484. (void) dst;
  12485. (void) src1_dd;
  12486. }
  12487. inline void ggml_sycl_op_diag_mask_inf(const ggml_tensor *src0,
  12488. const ggml_tensor *src1,
  12489. ggml_tensor *dst, const float *src0_dd,
  12490. const float *src1_dd, float *dst_dd,
  12491. const dpct::queue_ptr &main_stream) {
  12492. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12493. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12494. const int64_t ne00 = src0->ne[0];
  12495. const int64_t ne01 = src0->ne[1];
  12496. const int nrows0 = ggml_nrows(src0);
  12497. const int n_past = ((int32_t *) dst->op_params)[0];
  12498. diag_mask_inf_f32_sycl(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  12499. (void) src1;
  12500. (void) dst;
  12501. (void) src1_dd;
  12502. }
  12503. inline void ggml_sycl_op_soft_max(const ggml_tensor *src0,
  12504. const ggml_tensor *src1, ggml_tensor *dst,
  12505. const float *src0_dd, const float *src1_dd,
  12506. float *dst_dd,
  12507. const dpct::queue_ptr &main_stream) {
  12508. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12509. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12510. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  12511. const int64_t ne00 = src0->ne[0];
  12512. const int64_t nrows_x = ggml_nrows(src0);
  12513. const int64_t nrows_y = src0->ne[1];
  12514. float scale = 1.0f;
  12515. float max_bias = 0.0f;
  12516. memcpy(&scale, dst->op_params + 0, sizeof(float));
  12517. memcpy(&max_bias, dst->op_params + 1, sizeof(float));
  12518. // positions tensor
  12519. float * src2_dd = nullptr;
  12520. sycl_pool_alloc<float> src2_f;
  12521. ggml_tensor * src2 = dst->src[2];
  12522. const bool use_src2 = src2 != nullptr;
  12523. if (use_src2) {
  12524. const bool src2_on_device = src2->backend == GGML_BACKEND_TYPE_GPU;
  12525. if (src2_on_device) {
  12526. ggml_tensor_extra_gpu * src2_extra = (ggml_tensor_extra_gpu *) src2->extra;
  12527. src2_dd = (float *) src2_extra->data_device[g_main_device];
  12528. } else {
  12529. src2_dd = src2_f.alloc(ggml_nelements(src2));
  12530. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src2_dd, src2, 0, 0, 0, 1, main_stream));
  12531. }
  12532. }
  12533. soft_max_f32_sycl(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00,
  12534. nrows_x, nrows_y, scale, max_bias, main_stream);
  12535. }
  12536. inline void ggml_sycl_op_scale(const ggml_tensor *src0, const ggml_tensor *src1,
  12537. ggml_tensor *dst, const float *src0_dd,
  12538. const float *src1_dd, float *dst_dd,
  12539. const dpct::queue_ptr &main_stream) {
  12540. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12541. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12542. float scale;
  12543. memcpy(&scale, dst->op_params, sizeof(float));
  12544. scale_f32_sycl(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  12545. /*
  12546. DPCT1010:87: SYCL uses exceptions to report errors and does not use the
  12547. error codes. The call was replaced with 0. You need to rewrite this code.
  12548. */
  12549. SYCL_CHECK(0);
  12550. (void) src1;
  12551. (void) dst;
  12552. (void) src1_dd;
  12553. }
  12554. inline void ggml_sycl_op_clamp(const ggml_tensor *src0, const ggml_tensor *src1,
  12555. ggml_tensor *dst, const float *src0_dd,
  12556. const float *src1_dd, float *dst_dd,
  12557. const dpct::queue_ptr &main_stream) {
  12558. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12559. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12560. float min;
  12561. float max;
  12562. memcpy(&min, dst->op_params, sizeof(float));
  12563. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  12564. clamp_f32_sycl(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  12565. /*
  12566. DPCT1010:88: SYCL uses exceptions to report errors and does not use the
  12567. error codes. The call was replaced with 0. You need to rewrite this code.
  12568. */
  12569. SYCL_CHECK(0);
  12570. (void) src1;
  12571. (void) dst;
  12572. (void) src1_dd;
  12573. }
  12574. static void ggml_sycl_op_flatten(const ggml_tensor *src0,
  12575. const ggml_tensor *src1, ggml_tensor *dst,
  12576. const ggml_sycl_op_flatten_t op) try {
  12577. const int64_t nrows0 = ggml_nrows(src0);
  12578. const bool use_src1 = src1 != nullptr;
  12579. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  12580. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12581. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12582. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12583. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  12584. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12585. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12586. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_TYPE_GPU;
  12587. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  12588. // dd = data device
  12589. float * src0_ddf = nullptr;
  12590. float * src1_ddf = nullptr;
  12591. float * dst_ddf = nullptr;
  12592. sycl_pool_alloc<float> src0_f;
  12593. sycl_pool_alloc<float> src1_f;
  12594. sycl_pool_alloc<float> dst_f;
  12595. ggml_sycl_set_device(g_main_device);
  12596. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12597. // GGML_SYCL_DEBUG("g_main_device=%d, main_stream=%p src0_on_device=%d, src1_on_device=%d, dst_on_device=%d\n",
  12598. // g_main_device, main_stream, src0_on_device, src1_on_device, dst_on_device);
  12599. if (src0_on_device) {
  12600. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  12601. } else {
  12602. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  12603. // GGML_SYCL_DEBUG("before ggml_sycl_cpy_tensor_2d src0_ddf=%p, src0=%p\n", src0_ddf, src0);
  12604. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  12605. }
  12606. if (use_src1) {
  12607. if (src1_on_device) {
  12608. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12609. } else {
  12610. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  12611. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  12612. }
  12613. }
  12614. if (dst_on_device) {
  12615. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12616. } else {
  12617. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  12618. }
  12619. // GGML_SYCL_DEBUG("op src0=%p, src1=%p, dst=%p, src0_ddf=%p, src1_ddf=%p, dst_ddf=%p, main_stream=%p\n",
  12620. // src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  12621. // do the computation
  12622. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  12623. /*
  12624. DPCT1010:89: SYCL uses exceptions to report errors and does not use the
  12625. error codes. The call was replaced with 0. You need to rewrite this code.
  12626. */
  12627. SYCL_CHECK(0);
  12628. // copy dst to host if necessary
  12629. if (!dst_on_device) {
  12630. SYCL_CHECK(CHECK_TRY_ERROR(
  12631. main_stream->memcpy(dst->data, dst_ddf, ggml_nbytes(dst)).wait()));
  12632. }
  12633. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12634. SYCL_CHECK(CHECK_TRY_ERROR(
  12635. dpct::get_current_device().queues_wait_and_throw()));
  12636. }
  12637. // print_ggml_tensor("tensor", dst);
  12638. }
  12639. catch (sycl::exception const &exc) {
  12640. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12641. << ", line:" << __LINE__ << std::endl;
  12642. std::exit(1);
  12643. }
  12644. static void ggml_sycl_set_peer_access(const int n_tokens) {
  12645. static bool peer_access_enabled = false;
  12646. const bool enable_peer_access = n_tokens <= GGML_SYCL_PEER_MAX_BATCH_SIZE;
  12647. if (peer_access_enabled == enable_peer_access) {
  12648. return;
  12649. }
  12650. #ifdef NDEBUG
  12651. for (int i = 0; i < g_device_count; ++i) {
  12652. SYCL_CHECK(ggml_sycl_set_device(i));
  12653. // SYCL_CHECK(syclDeviceSynchronize());
  12654. }
  12655. for (int i = 0; i < g_device_count; ++i) {
  12656. SYCL_CHECK(ggml_sycl_set_device(i));
  12657. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  12658. if (i == id_other) {
  12659. continue;
  12660. }
  12661. if (i != g_main_device && id_other != g_main_device) {
  12662. continue;
  12663. }
  12664. // int can_access_peer;
  12665. // SYCL_CHECK(syclDeviceCanAccessPeer(&can_access_peer, id, id_other));
  12666. // if (can_access_peer) {
  12667. // if (enable_peer_access) {
  12668. // SYCL_CHECK(syclDeviceEnablePeerAccess(id_other, 0));
  12669. // } else {
  12670. // SYCL_CHECK(syclDeviceDisablePeerAccess(id_other));
  12671. // }
  12672. // }
  12673. }
  12674. }
  12675. #endif // NDEBUG
  12676. peer_access_enabled = enable_peer_access;
  12677. }
  12678. struct ggml_backend_sycl_split_buffer_type_context {
  12679. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  12680. };
  12681. static void ggml_sycl_op_mul_mat(const ggml_tensor *src0,
  12682. const ggml_tensor *src1, ggml_tensor *dst,
  12683. ggml_sycl_op_mul_mat_t op,
  12684. const bool convert_src1_to_q8_1) try {
  12685. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne);
  12686. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  12687. const int64_t nrows1 = ggml_nrows(src1);
  12688. GGML_ASSERT(ne03 == ne13);
  12689. const int64_t ne0 = dst->ne[0];
  12690. const int64_t ne1 = dst->ne[1];
  12691. const int nb2 = dst->nb[2];
  12692. const int nb3 = dst->nb[3];
  12693. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12694. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12695. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  12696. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  12697. const int64_t i02_divisor = ne12 / ne02;
  12698. const size_t src0_ts = ggml_type_size(src0->type);
  12699. const size_t src0_bs = ggml_blck_size(src0->type);
  12700. const size_t q8_1_ts = sizeof(block_q8_1);
  12701. const size_t q8_1_bs = QK8_1;
  12702. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12703. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12704. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12705. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12706. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  12707. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  12708. int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  12709. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12710. GGML_ASSERT(!(split && ne02 > 1));
  12711. GGML_ASSERT(!(split && ne03 > 1));
  12712. GGML_ASSERT(!(split && ne02 < ne12));
  12713. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  12714. if (split) {
  12715. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  12716. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  12717. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
  12718. tensor_split = buft_ctx->tensor_split;
  12719. }
  12720. struct dev_data {
  12721. sycl_pool_alloc<char> src0_dd_alloc;
  12722. sycl_pool_alloc<float> src1_ddf_alloc;
  12723. sycl_pool_alloc<char> src1_ddq_alloc;
  12724. sycl_pool_alloc<float> dst_dd_alloc;
  12725. char *src0_dd = nullptr;
  12726. float *src1_ddf = nullptr; // float
  12727. char *src1_ddq = nullptr; // q8_1
  12728. float *dst_dd = nullptr;
  12729. int64_t row_low;
  12730. int64_t row_high;
  12731. };
  12732. dev_data dev[GGML_SYCL_MAX_DEVICES];
  12733. int used_devices = 0;
  12734. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12735. for (int i = 0; i < g_device_count; ++i) {
  12736. // by default, use all rows
  12737. dev[i].row_low = 0;
  12738. dev[i].row_high = ne01;
  12739. // for multi GPU, get the row boundaries from tensor split
  12740. // and round to mul_mat_q tile sizes
  12741. if (split) {
  12742. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  12743. if (i != 0) {
  12744. dev[i].row_low = ne01*tensor_split[i];
  12745. if (dev[i].row_low < ne01) {
  12746. dev[i].row_low -= dev[i].row_low % rounding;
  12747. }
  12748. }
  12749. if (i != g_device_count - 1) {
  12750. dev[i].row_high = ne01*tensor_split[i + 1];
  12751. if (dev[i].row_high < ne01) {
  12752. dev[i].row_high -= dev[i].row_high % rounding;
  12753. }
  12754. }
  12755. }
  12756. }
  12757. for (int i = 0; i < g_device_count; ++i) {
  12758. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  12759. continue;
  12760. }
  12761. used_devices++;
  12762. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12763. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12764. ggml_sycl_set_device(i);
  12765. dpct::queue_ptr stream = g_syclStreams[i][0];
  12766. if (src0_on_device && src0_is_contiguous) {
  12767. dev[i].src0_dd = (char *) src0_extra->data_device[i];
  12768. } else {
  12769. dev[i].src0_dd = dev[i].src0_dd_alloc.alloc(ggml_nbytes(src0));
  12770. }
  12771. if (src1_on_device && src1_is_contiguous) {
  12772. dev[i].src1_ddf = (float *) src1_extra->data_device[i];
  12773. } else {
  12774. dev[i].src1_ddf = dev[i].src1_ddf_alloc.alloc(ggml_nelements(src1));
  12775. }
  12776. if (convert_src1_to_q8_1) {
  12777. dev[i].src1_ddq = dev[i].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  12778. if (src1_on_device && src1_is_contiguous) {
  12779. quantize_row_q8_1_sycl(dev[i].src1_ddf, dev[i].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  12780. /*
  12781. DPCT1010:90: SYCL uses exceptions to report errors and does not
  12782. use the error codes. The call was replaced with 0. You need to
  12783. rewrite this code.
  12784. */
  12785. SYCL_CHECK(0);
  12786. }
  12787. }
  12788. if (dst_on_device) {
  12789. dev[i].dst_dd = (float *) dst_extra->data_device[i];
  12790. } else {
  12791. const size_t size_dst_ddf = split ? (dev[i].row_high - dev[i].row_low)*ne1 : ggml_nelements(dst);
  12792. dev[i].dst_dd = dev[i].dst_dd_alloc.alloc(size_dst_ddf);
  12793. }
  12794. }
  12795. // if multiple devices are used they need to wait for the main device
  12796. // here an event is recorded that signals that the main device has finished calculating the input data
  12797. if (split && used_devices > 1) {
  12798. ggml_sycl_set_device(g_main_device);
  12799. /*
  12800. DPCT1024:91: The original code returned the error code that was further
  12801. consumed by the program logic. This original code was replaced with 0.
  12802. You may need to rewrite the program logic consuming the error code.
  12803. */
  12804. SYCL_CHECK(CHECK_TRY_ERROR(
  12805. *src0_extra->events[g_main_device][0] =
  12806. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier()));
  12807. }
  12808. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  12809. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  12810. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  12811. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  12812. for (int i = 0; i < g_device_count; ++i) {
  12813. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  12814. continue;
  12815. }
  12816. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12817. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12818. const int64_t row_diff = dev[i].row_high - dev[i].row_low;
  12819. ggml_sycl_set_device(i);
  12820. dpct::queue_ptr stream = g_syclStreams[i][is];
  12821. // wait for main GPU data if necessary
  12822. if (split && (i != g_main_device || is != 0)) {
  12823. /*
  12824. DPCT1009:163: SYCL uses exceptions to report errors and does not
  12825. use the error codes. The original code was commented out and a
  12826. warning string was inserted. You need to rewrite this code.
  12827. */
  12828. SYCL_CHECK(CHECK_TRY_ERROR(stream->ext_oneapi_submit_barrier(
  12829. {*src0_extra->events[g_main_device][0]})));
  12830. }
  12831. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  12832. const int64_t i03 = i0 / ne12;
  12833. const int64_t i02 = i0 % ne12;
  12834. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  12835. // for split tensors the data begins at i0 == i0_offset_low
  12836. char * src0_dd_i = dev[i].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  12837. float * src1_ddf_i = dev[i].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  12838. char * src1_ddq_i = dev[i].src1_ddq + src1_ddq_i_offset;
  12839. float * dst_dd_i = dev[i].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  12840. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  12841. // in that case an offset on dst_ddf_i is needed
  12842. if (dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device) {
  12843. dst_dd_i += dev[i].row_low; // offset is 0 if no tensor split
  12844. }
  12845. // copy src0, src1 to device if necessary
  12846. if (src1->backend == GGML_BACKEND_TYPE_GPU && src1_is_contiguous) {
  12847. if (i != g_main_device) {
  12848. if (convert_src1_to_q8_1) {
  12849. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  12850. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  12851. src1_ddq_i, src1_ddq_i_source,
  12852. src1_ncols * src1_padded_col_size * q8_1_ts /
  12853. q8_1_bs).wait()));
  12854. } else {
  12855. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  12856. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  12857. SYCL_CHECK(CHECK_TRY_ERROR(dev2dev_memcpy(*stream, *main_stream,
  12858. src1_ddf_i, src1_ddf_i_source,
  12859. src1_ncols * ne10 * sizeof(float))));
  12860. }
  12861. }
  12862. } else if (src1->backend == GGML_BACKEND_TYPE_CPU || (src1_on_device && !src1_is_contiguous)) {
  12863. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(
  12864. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  12865. } else {
  12866. GGML_ASSERT(false);
  12867. }
  12868. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_TYPE_CPU || !src1_is_contiguous)) {
  12869. quantize_row_q8_1_sycl(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  12870. /*
  12871. DPCT1010:92: SYCL uses exceptions to report errors and does
  12872. not use the error codes. The call was replaced with 0. You
  12873. need to rewrite this code.
  12874. */
  12875. SYCL_CHECK(0);
  12876. }
  12877. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  12878. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[i].row_low, dev[i].row_high, stream));
  12879. }
  12880. if (src1->type == GGML_TYPE_F16) {
  12881. src1_padded_col_size = (i0 * ne11 + src1_col_0) * ne10;
  12882. }
  12883. // do the computation
  12884. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  12885. dev[i].row_low, dev[i].row_high, src1_ncols, src1_padded_col_size, stream);
  12886. /*
  12887. DPCT1010:93: SYCL uses exceptions to report errors and does not
  12888. use the error codes. The call was replaced with 0. You need to
  12889. rewrite this code.
  12890. */
  12891. SYCL_CHECK(0);
  12892. // copy dst to host or other device if necessary
  12893. if (!dst_on_device) {
  12894. void * dst_off_device;
  12895. dpct::memcpy_direction kind;
  12896. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12897. dst_off_device = dst->data;
  12898. kind = dpct::device_to_host;
  12899. } else if (dst->backend == GGML_BACKEND_TYPE_GPU) {
  12900. dst_off_device = dst_extra->data_device[g_main_device];
  12901. kind = dpct::device_to_device;
  12902. } else {
  12903. GGML_ASSERT(false);
  12904. }
  12905. if (split) {
  12906. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  12907. // dst is NOT transposed.
  12908. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  12909. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  12910. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  12911. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  12912. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  12913. dhf_dst_i += src1_col_0*ne0 + dev[i].row_low;
  12914. //todo, dirty solution. Need be updated when device2device memcpy() is supported.
  12915. if (kind == dpct::device_to_device) {
  12916. size_t dst_size = ggml_nbytes_pad(dst);
  12917. float *host_buf = (float *)malloc(dst_size);
  12918. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12919. host_buf, ne0 * sizeof(float), dst_dd_i,
  12920. row_diff * sizeof(float), row_diff * sizeof(float),
  12921. src1_ncols, dpct::device_to_host, *stream)));
  12922. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[i]).queues_wait_and_throw();
  12923. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12924. dhf_dst_i, ne0 * sizeof(float), host_buf,
  12925. row_diff * sizeof(float), row_diff * sizeof(float),
  12926. src1_ncols, dpct::host_to_device, *main_stream)));
  12927. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[g_main_device]).queues_wait_and_throw();
  12928. free(host_buf);
  12929. } else {
  12930. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12931. dhf_dst_i, ne0 * sizeof(float), dst_dd_i,
  12932. row_diff * sizeof(float), row_diff * sizeof(float),
  12933. src1_ncols, kind, *stream)));
  12934. }
  12935. } else {
  12936. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  12937. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  12938. dhf_dst_i += src1_col_0*ne0;
  12939. SYCL_CHECK(CHECK_TRY_ERROR(
  12940. stream->memcpy(dhf_dst_i, dst_dd_i,
  12941. src1_ncols * ne0 * sizeof(float)).wait()));
  12942. }
  12943. }
  12944. // add event for the main device to wait on until other device is done
  12945. if (split && (i != g_main_device || is != 0)) {
  12946. /*
  12947. DPCT1024:94: The original code returned the error code that
  12948. was further consumed by the program logic. This original
  12949. code was replaced with 0. You may need to rewrite the
  12950. program logic consuming the error code.
  12951. */
  12952. SYCL_CHECK(CHECK_TRY_ERROR(
  12953. *src0_extra->events[i][is] =
  12954. stream->ext_oneapi_submit_barrier()));
  12955. }
  12956. }
  12957. }
  12958. }
  12959. // main device waits for all other devices to be finished
  12960. if (split && g_device_count > 1) {
  12961. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  12962. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  12963. ggml_sycl_set_device(g_main_device);
  12964. for (int i = 0; i < g_device_count; ++i) {
  12965. if (dev[i].row_low == dev[i].row_high) {
  12966. continue;
  12967. }
  12968. for (int64_t is = 0; is < is_max; ++is) {
  12969. SYCL_CHECK(CHECK_TRY_ERROR(
  12970. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier(
  12971. {*src0_extra->events[i][is]})));
  12972. }
  12973. }
  12974. }
  12975. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12976. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12977. SYCL_CHECK(CHECK_TRY_ERROR(
  12978. dpct::get_current_device().queues_wait_and_throw()));
  12979. }
  12980. }
  12981. catch (sycl::exception const &exc) {
  12982. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12983. << ", line:" << __LINE__ << std::endl;
  12984. std::exit(1);
  12985. }
  12986. static void ggml_sycl_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12987. GGML_SYCL_DEBUG("call %s\n", __func__);
  12988. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_repeat);
  12989. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12990. }
  12991. static void ggml_sycl_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12992. GGML_SYCL_DEBUG("call %s\n", __func__);
  12993. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_get_rows);
  12994. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12995. }
  12996. static void ggml_sycl_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12997. GGML_SYCL_DEBUG("call %s\n", __func__);
  12998. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_add);
  12999. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13000. }
  13001. static void ggml_sycl_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13002. GGML_SYCL_DEBUG("call %s\n", __func__);
  13003. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_acc);
  13004. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13005. }
  13006. static void ggml_sycl_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13007. GGML_SYCL_DEBUG("call %s\n", __func__);
  13008. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_mul);
  13009. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13010. }
  13011. static void ggml_sycl_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13012. GGML_SYCL_DEBUG("call %s\n", __func__);
  13013. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_div);
  13014. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13015. }
  13016. static void ggml_sycl_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13017. GGML_SYCL_DEBUG("call %s\n", __func__);
  13018. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu);
  13019. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13020. }
  13021. static void ggml_sycl_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13022. GGML_SYCL_DEBUG("call %s\n", __func__);
  13023. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_silu);
  13024. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13025. }
  13026. static void ggml_sycl_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13027. GGML_SYCL_DEBUG("call %s\n", __func__);
  13028. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu_quick);
  13029. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13030. }
  13031. static void ggml_sycl_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13032. GGML_SYCL_DEBUG("call %s\n", __func__);
  13033. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_tanh);
  13034. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13035. }
  13036. static void ggml_sycl_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13037. GGML_SYCL_DEBUG("call %s\n", __func__);
  13038. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_relu);
  13039. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13040. }
  13041. static void ggml_sycl_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13042. GGML_SYCL_DEBUG("call %s\n", __func__);
  13043. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardsigmoid);
  13044. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13045. }
  13046. static void ggml_sycl_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13047. GGML_SYCL_DEBUG("call %s\n", __func__);
  13048. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardswish);
  13049. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13050. }
  13051. static void ggml_sycl_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13052. GGML_SYCL_DEBUG("call %s\n", __func__);
  13053. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_leaky_relu);
  13054. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13055. }
  13056. static void ggml_sycl_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13057. GGML_SYCL_DEBUG("call %s\n", __func__);
  13058. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sqr);
  13059. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13060. }
  13061. static void ggml_sycl_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13062. GGML_SYCL_DEBUG("call %s\n", __func__);
  13063. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_norm);
  13064. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13065. }
  13066. static void ggml_sycl_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13067. GGML_SYCL_DEBUG("call %s\n", __func__);
  13068. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_group_norm);
  13069. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13070. }
  13071. static void ggml_sycl_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13072. GGML_SYCL_DEBUG("call %s\n", __func__);
  13073. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_concat);
  13074. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13075. }
  13076. static void ggml_sycl_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13077. GGML_SYCL_DEBUG("call %s\n", __func__);
  13078. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_upscale);
  13079. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13080. }
  13081. static void ggml_sycl_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13082. GGML_SYCL_DEBUG("call %s\n", __func__);
  13083. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pad);
  13084. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13085. }
  13086. static void ggml_sycl_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13087. GGML_SYCL_DEBUG("call %s\n", __func__);
  13088. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rms_norm);
  13089. GGML_SYCL_DEBUG("call %s done\n", __func__);
  13090. }
  13091. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  13092. if (!g_sycl_loaded) return false;
  13093. const int64_t ne10 = src1->ne[0];
  13094. const int64_t ne0 = dst->ne[0];
  13095. const int64_t ne1 = dst->ne[1];
  13096. // TODO: find the optimal values for these
  13097. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  13098. src1->type == GGML_TYPE_F32 &&
  13099. dst->type == GGML_TYPE_F32 &&
  13100. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  13101. }
  13102. static void ggml_sycl_mul_mat_vec_p021(const ggml_tensor *src0,
  13103. const ggml_tensor *src1,
  13104. ggml_tensor *dst) try {
  13105. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  13106. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13107. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  13108. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  13109. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  13110. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  13111. const int64_t ne00 = src0->ne[0];
  13112. const int64_t ne01 = src0->ne[1];
  13113. const int64_t ne02 = src0->ne[2];
  13114. const int64_t ne12 = src1->ne[2];
  13115. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13116. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13117. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13118. void * src0_ddq = src0_extra->data_device[g_main_device];
  13119. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13120. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13121. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13122. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13123. ggml_mul_mat_p021_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  13124. }
  13125. catch (sycl::exception const &exc) {
  13126. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13127. << ", line:" << __LINE__ << std::endl;
  13128. std::exit(1);
  13129. }
  13130. static void ggml_sycl_mul_mat_vec_nc(const ggml_tensor *src0,
  13131. const ggml_tensor *src1,
  13132. ggml_tensor *dst) try {
  13133. GGML_ASSERT(!ggml_is_transposed(src0));
  13134. GGML_ASSERT(!ggml_is_transposed(src1));
  13135. GGML_ASSERT(!ggml_is_permuted(src0));
  13136. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13137. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  13138. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  13139. const int64_t ne00 = src0->ne[0];
  13140. const int64_t ne01 = src0->ne[1];
  13141. const int64_t ne02 = src0->ne[2];
  13142. const int64_t nb01 = src0->nb[1];
  13143. const int64_t nb02 = src0->nb[2];
  13144. const int64_t ne12 = src1->ne[2];
  13145. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13146. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13147. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13148. void * src0_ddq = src0_extra->data_device[g_main_device];
  13149. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13150. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13151. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13152. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13153. const int64_t row_stride_x = nb01 / sizeof(sycl::half);
  13154. const int64_t channel_stride_x = nb02 / sizeof(sycl::half);
  13155. 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);
  13156. }
  13157. catch (sycl::exception const &exc) {
  13158. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13159. << ", line:" << __LINE__ << std::endl;
  13160. std::exit(1);
  13161. }
  13162. static void k_compute_batched_ptrs(const sycl::half *src0_as_f16,
  13163. const sycl::half *src1_as_f16, char *dst,
  13164. const void **ptrs_src, void **ptrs_dst,
  13165. int64_t ne12, int64_t ne13, int64_t ne23,
  13166. size_t nb02, size_t nb03, size_t nb12,
  13167. size_t nb13, size_t nbd2, size_t nbd3,
  13168. int64_t r2, int64_t r3,
  13169. const sycl::nd_item<3> &item_ct1) {
  13170. int64_t i13 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  13171. item_ct1.get_local_id(2);
  13172. int64_t i12 = item_ct1.get_group(1) * item_ct1.get_local_range(1) +
  13173. item_ct1.get_local_id(1);
  13174. if (i13 >= ne13 || i12 >= ne12) {
  13175. return;
  13176. }
  13177. int64_t i03 = i13 / r3;
  13178. int64_t i02 = i12 / r2;
  13179. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  13180. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  13181. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  13182. }
  13183. static void ggml_sycl_mul_mat_batched_sycl(const ggml_tensor *src0,
  13184. const ggml_tensor *src1,
  13185. ggml_tensor *dst) try {
  13186. GGML_ASSERT(!ggml_is_transposed(src0));
  13187. GGML_ASSERT(!ggml_is_transposed(src1));
  13188. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13189. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  13190. GGML_TENSOR_BINARY_OP_LOCALS
  13191. const int64_t ne_dst = ggml_nelements(dst);
  13192. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13193. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13194. SYCL_CHECK(
  13195. CHECK_TRY_ERROR(g_sycl_handles[g_main_device] = main_stream));
  13196. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13197. void * src0_ddq = src0_extra->data_device[g_main_device];
  13198. sycl::half *src0_as_f16 = (sycl::half *)src0_ddq;
  13199. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13200. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13201. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13202. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13203. // convert src1 to fp16
  13204. sycl_pool_alloc<sycl::half> src1_f16_alloc;
  13205. if (src1->type != GGML_TYPE_F16) {
  13206. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  13207. const int64_t ne_src1 = ggml_nelements(src1);
  13208. src1_f16_alloc.alloc(ne_src1);
  13209. GGML_ASSERT(to_fp16_sycl != nullptr);
  13210. to_fp16_sycl(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  13211. }
  13212. sycl::half *src1_f16 = src1->type == GGML_TYPE_F16 ? (sycl::half *)src1_ddf
  13213. : src1_f16_alloc.get();
  13214. sycl_pool_alloc<sycl::half> dst_f16;
  13215. char * dst_t;
  13216. dpct::library_data_t cu_compute_type = dpct::library_data_t::real_float;
  13217. dpct::library_data_t cu_data_type = dpct::library_data_t::real_float;
  13218. // dst strides
  13219. size_t nbd2 = dst->nb[2];
  13220. size_t nbd3 = dst->nb[3];
  13221. const sycl::half alpha_f16 = 1.0f;
  13222. const sycl::half beta_f16 = 0.0f;
  13223. const float alpha_f32 = 1.0f;
  13224. const float beta_f32 = 0.0f;
  13225. const void * alpha = &alpha_f32;
  13226. const void * beta = &beta_f32;
  13227. // TODO: Renable (dst->op_params[0] =! GGML_PREC_DEFAULT) pathway
  13228. // oneMKL open source supports half, half, float, float: datatypes
  13229. dst_t = (char *) dst_ddf;
  13230. GGML_ASSERT(ne12 % ne02 == 0);
  13231. GGML_ASSERT(ne13 % ne03 == 0);
  13232. // broadcast factors
  13233. const int64_t r2 = ne12/ne02;
  13234. const int64_t r3 = ne13/ne03;
  13235. #if 0
  13236. // use syclGemmEx
  13237. {
  13238. for (int i13 = 0; i13 < ne13; ++i13) {
  13239. for (int i12 = 0; i12 < ne12; ++i12) {
  13240. int i03 = i13 / r3;
  13241. int i02 = i12 / r2;
  13242. SYCL_CHECK(
  13243. syclGemmEx(g_sycl_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  13244. ne01, ne11, ne10,
  13245. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , SYCL_R_16F, nb01/sizeof(half),
  13246. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, SYCL_R_16F, nb11/sizeof(float),
  13247. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  13248. cu_compute_type,
  13249. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  13250. }
  13251. }
  13252. }
  13253. #else
  13254. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  13255. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  13256. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  13257. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  13258. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  13259. (const char *)src0_as_f16, dpct::library_data_t::real_half,
  13260. nb01 / nb00, nb02 / nb00,
  13261. (const char *)src1_f16, dpct::library_data_t::real_half,
  13262. nb11 / nb10, nb12 / nb10, beta,
  13263. (char *)dst_t, cu_data_type, ne01, nb2 / nb0,
  13264. ne12 * ne13, cu_compute_type)));
  13265. g_sycl_handles[g_main_device]->wait();
  13266. } else {
  13267. const int ne23 = ne12*ne13;
  13268. sycl_pool_alloc<const void *> ptrs_src(2*ne23);
  13269. sycl_pool_alloc< void *> ptrs_dst(1*ne23);
  13270. sycl::range<3> block_dims(1, ne12, ne13);
  13271. /*
  13272. DPCT1049:47: The work-group size passed to the SYCL kernel may exceed
  13273. the limit. To get the device limit, query
  13274. info::device::max_work_group_size. Adjust the work-group size if needed.
  13275. */
  13276. {
  13277. dpct::has_capability_or_fail(main_stream->get_device(),
  13278. {sycl::aspect::fp16});
  13279. main_stream->submit([&](sycl::handler &cgh) {
  13280. const void **ptrs_src_get = ptrs_src.get();
  13281. void **ptrs_dst_get = ptrs_dst.get();
  13282. size_t nb12_scaled = src1->type == GGML_TYPE_F16 ? nb12 : nb12 / 2;
  13283. size_t nb13_scaled = src1->type == GGML_TYPE_F16 ? nb13 : nb13 / 2;
  13284. cgh.parallel_for(sycl::nd_range<3>(block_dims, block_dims),
  13285. [=](sycl::nd_item<3> item_ct1) {
  13286. k_compute_batched_ptrs(
  13287. src0_as_f16, src1_f16,
  13288. dst_t, ptrs_src_get,
  13289. ptrs_dst_get, ne12, ne13, ne23,
  13290. nb02, nb03, nb12_scaled, nb13_scaled,
  13291. nbd2, nbd3, r2, r3, item_ct1);
  13292. });
  13293. }).wait();
  13294. }
  13295. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  13296. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  13297. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  13298. (const void **)(ptrs_src.get() + 0 * ne23),
  13299. dpct::library_data_t::real_half, nb01 / nb00,
  13300. (const void **)(ptrs_src.get() + 1 * ne23),
  13301. dpct::library_data_t::real_half, nb11 / nb10, beta,
  13302. (void **)(ptrs_dst.get() + 0 * ne23), cu_data_type, ne01, ne23,
  13303. cu_compute_type)));
  13304. g_sycl_handles[g_main_device]->wait();
  13305. }
  13306. #endif
  13307. }
  13308. catch (sycl::exception const &exc) {
  13309. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13310. << ", line:" << __LINE__ << std::endl;
  13311. std::exit(1);
  13312. }
  13313. static void ggml_sycl_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13314. const bool all_on_device =
  13315. (src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT) &&
  13316. (src1->backend == GGML_BACKEND_TYPE_GPU) &&
  13317. ( dst->backend == GGML_BACKEND_TYPE_GPU);
  13318. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  13319. int64_t min_compute_capability = INT_MAX;
  13320. for (int i = 0; i < g_device_count; ++i) {
  13321. 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)) {
  13322. min_compute_capability = g_device_caps[i].cc;
  13323. }
  13324. }
  13325. #ifdef SYCL_USE_XMX
  13326. const bool use_xmx = true;
  13327. #else
  13328. const bool use_xmx = false;
  13329. #endif
  13330. // debug helpers
  13331. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  13332. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  13333. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  13334. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  13335. //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);
  13336. //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);
  13337. if (!split && all_on_device && !use_xmx && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  13338. // KQ single-batch
  13339. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_p021\n");
  13340. ggml_sycl_mul_mat_vec_p021(src0, src1, dst);
  13341. } 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) {
  13342. // KQV single-batch
  13343. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_nc\n");
  13344. ggml_sycl_mul_mat_vec_nc(src0, src1, dst);
  13345. } else if (!split && all_on_device && use_xmx && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  13346. // KQ + KQV multi-batch
  13347. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_batched_sycl\n");
  13348. ggml_sycl_mul_mat_batched_sycl(src0, src1, dst);
  13349. } else if (src0->type == GGML_TYPE_F32) {
  13350. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat\n");
  13351. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  13352. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  13353. // GGML_SYCL_DEBUG("ggml_is_quantized or GGML_TYPE_F16\n");
  13354. if (src1->ne[1] == 1 && src0->ne[0] % GGML_SYCL_DMMV_X == 0) {
  13355. #ifdef GGML_SYCL_FORCE_DMMV
  13356. const bool use_mul_mat_vec_q = false;
  13357. #else
  13358. const bool use_mul_mat_vec_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  13359. #endif // GGML_SYCL_FORCE_DMMV
  13360. if (use_mul_mat_vec_q) {
  13361. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  13362. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_vec_q path\n");
  13363. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_vec_q, true);
  13364. } else {
  13365. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_dequantize_mul_mat_vec path\n");
  13366. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_dequantize_mul_mat_vec, false);
  13367. }
  13368. } else {
  13369. bool use_mul_mat_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type);
  13370. if (use_xmx && min_compute_capability >= VER_GEN9 && src1->ne[1] > XMX_MAX_BATCH_SIZE) {
  13371. use_mul_mat_q = false;
  13372. }
  13373. if (use_mul_mat_q) {
  13374. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_q path\n");
  13375. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_q, true);
  13376. } else {
  13377. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_sycl path\n");
  13378. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  13379. }
  13380. }
  13381. } else {
  13382. GGML_ASSERT(false);
  13383. }
  13384. }
  13385. #if 0
  13386. template<typename ... Srcs>
  13387. static __global__ void k_compute_batched_ptrs_id(
  13388. const void ** ptrs_src, void ** ptrs_dst,
  13389. int ne12, int ne13,
  13390. int ne23,
  13391. int nb02, int nb03,
  13392. int nb12, int nb13,
  13393. int nb2, int nb3,
  13394. int r2, int r3,
  13395. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  13396. const half * src1_f16, half * dst_f16,
  13397. const int32_t * ids, const int id,
  13398. Srcs... src0s) {
  13399. int i = ids[id];
  13400. half * src0_f16;
  13401. const void * srcs_ar[] = { (const half *) src0s... };
  13402. if (src0_type == GGML_TYPE_F16) {
  13403. src0_f16 = (half *) srcs_ar[i];
  13404. } else {
  13405. src0_f16 = src0_as_f16;
  13406. if (item_ct1.get_local_id(2) == 0 && threadIdx.y == 0) {
  13407. const to_fp16_sycl_t to_fp16 = ggml_get_to_fp16_sycl(src0_type);
  13408. to_fp16(srcs_ar[i], src0_f16, src0_ne, syclStreamFireAndForget);
  13409. }
  13410. }
  13411. int i13 = blockIdx.x * blockDim.x + item_ct1.get_local_id(2);
  13412. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  13413. if (i13 >= ne13 || i12 >= ne12) {
  13414. return;
  13415. }
  13416. int i03 = i13 / r3;
  13417. int i02 = i12 / r2;
  13418. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  13419. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  13420. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  13421. }
  13422. static void ggml_sycl_mul_mat_id_sycl(ggml_tensor * dst) {
  13423. const struct ggml_tensor * ids = dst->src[0];
  13424. const struct ggml_tensor * src1 = dst->src[1];
  13425. const struct ggml_tensor * src00 = dst->src[2];
  13426. const int id = dst->op_params[0];
  13427. GGML_ASSERT(!ggml_is_transposed(src00));
  13428. GGML_ASSERT(!ggml_is_transposed(src1));
  13429. GGML_ASSERT(src00->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13430. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  13431. GGML_TENSOR_LOCALS(int64_t, ne0, src00, ne);
  13432. //const int64_t nb01 = src00->nb[1];
  13433. GGML_TENSOR_LOCALS(int64_t, nb0, src00, nb);
  13434. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  13435. GGML_TENSOR_LOCALS(int64_t, nb1, src1, nb);
  13436. //const int64_t nb11 = src1->nb[1];
  13437. const int64_t ne1 = ggml_nelements(src1);
  13438. const int64_t ne = ggml_nelements(dst);
  13439. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13440. syclStream_t main_stream = g_syclStreams[g_main_device][0];
  13441. SYCL_CHECK(syclSetStream(g_sycl_handles[g_main_device], main_stream));
  13442. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13443. //void * src0_ddq = src0_extra->data_device[g_main_device];
  13444. //half * src0_as_f16 = (half *) src0_ddq;
  13445. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13446. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13447. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13448. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13449. // convert src1 to fp16
  13450. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  13451. GGML_ASSERT(to_fp16_sycl != nullptr);
  13452. size_t src1_as = 0;
  13453. half * src1_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne1 * sizeof(half), &src1_as);
  13454. to_fp16_sycl(src1_ddf, src1_as_f16, ne1, main_stream);
  13455. size_t dst_as = 0;
  13456. half * dst_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne * sizeof(half), &dst_as);
  13457. GGML_ASSERT(ne12 % ne02 == 0);
  13458. GGML_ASSERT(ne13 % ne03 == 0);
  13459. // broadcast factors
  13460. const int64_t r2 = ne12/ne02;
  13461. const int64_t r3 = ne13/ne03;
  13462. const half alpha_f16 = 1.0f;
  13463. const half beta_f16 = 0.0f;
  13464. // use syclGemmBatchedEx
  13465. const int ne23 = ne12*ne13;
  13466. const void ** ptrs_src = nullptr;
  13467. void ** ptrs_dst = nullptr;
  13468. size_t ptrs_src_s = 0;
  13469. size_t ptrs_dst_s = 0;
  13470. ptrs_src = (const void **) ggml_sycl_pool_malloc(g_main_device, 2*ne23*sizeof(void *), &ptrs_src_s);
  13471. ptrs_dst = ( void **) ggml_sycl_pool_malloc(g_main_device, 1*ne23*sizeof(void *), &ptrs_dst_s);
  13472. int64_t src0_ne = ggml_nelements(src00);
  13473. half * src0_as_f16 = nullptr;
  13474. size_t src0_as = 0;
  13475. if (src00->type != GGML_TYPE_F16) {
  13476. src0_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, src0_ne * sizeof(half), &src0_as);
  13477. }
  13478. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  13479. dim3 block_dims(ne13, ne12);
  13480. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  13481. ptrs_src, ptrs_dst,
  13482. ne12, ne13,
  13483. ne23,
  13484. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  13485. nb12, nb13,
  13486. dst->nb[2], dst->nb[3],
  13487. r2, r3,
  13488. src00->type, src0_as_f16, src0_ne,
  13489. src1_as_f16, dst_f16,
  13490. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  13491. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  13492. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  13493. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  13494. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  13495. );
  13496. SYCL_CHECK(syclGetLastError());
  13497. SYCL_CHECK(
  13498. syclGemmBatchedEx(g_sycl_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  13499. ne01, ne11, ne10,
  13500. &alpha_f16, (const void **) (ptrs_src + 0*ne23), SYCL_R_16F, ne00,
  13501. (const void **) (ptrs_src + 1*ne23), SYCL_R_16F, ne10,
  13502. &beta_f16, ( void **) (ptrs_dst + 0*ne23), SYCL_R_16F, ne01,
  13503. ne23,
  13504. CUBLAS_COMPUTE_16F,
  13505. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  13506. if (src0_as != 0) {
  13507. ggml_sycl_pool_free(g_main_device, src0_as_f16, src0_as);
  13508. }
  13509. if (ptrs_src_s != 0) {
  13510. ggml_sycl_pool_free(g_main_device, ptrs_src, ptrs_src_s);
  13511. }
  13512. if (ptrs_dst_s != 0) {
  13513. ggml_sycl_pool_free(g_main_device, ptrs_dst, ptrs_dst_s);
  13514. }
  13515. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  13516. to_fp32_sycl(dst_f16, dst_ddf, ne, main_stream);
  13517. ggml_sycl_pool_free(g_main_device, src1_as_f16, src1_as);
  13518. ggml_sycl_pool_free(g_main_device, dst_f16, dst_as);
  13519. }
  13520. #endif
  13521. static void ggml_sycl_mul_mat_id(const ggml_tensor *src0,
  13522. const ggml_tensor *src1,
  13523. ggml_tensor *dst) try {
  13524. #if 0
  13525. ggml_sycl_mul_mat_id_sycl(dst);
  13526. // TODO: mmq/mmv support
  13527. #endif
  13528. const int64_t nb11 = src1->nb[1];
  13529. const int64_t nb1 = dst->nb[1];
  13530. const struct ggml_tensor * ids = src0;
  13531. const int32_t id = ((int32_t *) dst->op_params)[0];
  13532. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  13533. std::vector<char> ids_host(ggml_nbytes(ids));
  13534. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13535. if (ids->backend == GGML_BACKEND_TYPE_GPU) {
  13536. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  13537. SYCL_CHECK(CHECK_TRY_ERROR(
  13538. stream->memcpy(ids_host.data(), ids_dev, ggml_nbytes(ids)).wait()));
  13539. // SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  13540. } else {
  13541. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  13542. }
  13543. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  13544. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  13545. ggml_tensor_extra_gpu src1_row_extra;
  13546. ggml_tensor_extra_gpu dst_row_extra;
  13547. ggml_tensor src1_row = *src1;
  13548. ggml_tensor dst_row = *dst;
  13549. src1_row.backend = GGML_BACKEND_TYPE_GPU;
  13550. dst_row.backend = GGML_BACKEND_TYPE_GPU;
  13551. src1_row.extra = &src1_row_extra;
  13552. dst_row.extra = &dst_row_extra;
  13553. char * src1_original = src1->backend == GGML_BACKEND_TYPE_CPU ?
  13554. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  13555. char * dst_original = dst->backend == GGML_BACKEND_TYPE_CPU ?
  13556. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  13557. if (src1->ne[1] == 1) {
  13558. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  13559. GGML_ASSERT(dst->backend == GGML_BACKEND_TYPE_GPU);
  13560. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13561. //int32_t row_id;
  13562. //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]));
  13563. //SYCL_CHECK(syclStreamSynchronize(g_syclStreams[g_main_device][0]));
  13564. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13565. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13566. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  13567. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  13568. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  13569. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  13570. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  13571. ggml_sycl_mul_mat(src0_row, &src1_row, &dst_row);
  13572. }
  13573. } else {
  13574. sycl_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  13575. sycl_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  13576. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  13577. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  13578. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  13579. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  13580. int64_t num_src1_rows = 0;
  13581. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13582. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13583. if (row_id_i != row_id) {
  13584. continue;
  13585. }
  13586. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13587. SYCL_CHECK(CHECK_TRY_ERROR(
  13588. stream->memcpy(src1_contiguous.get() + num_src1_rows * nb11,
  13589. src1_original + i01 * nb11, nb11).wait()));
  13590. num_src1_rows++;
  13591. }
  13592. if (num_src1_rows == 0) {
  13593. continue;
  13594. }
  13595. src1_row.ne[1] = num_src1_rows;
  13596. dst_row.ne[1] = num_src1_rows;
  13597. src1_row.nb[1] = nb11;
  13598. src1_row.nb[2] = num_src1_rows*nb11;
  13599. src1_row.nb[3] = num_src1_rows*nb11;
  13600. dst_row.nb[1] = nb1;
  13601. dst_row.nb[2] = num_src1_rows*nb1;
  13602. dst_row.nb[3] = num_src1_rows*nb1;
  13603. ggml_sycl_mul_mat(src0_row, &src1_row, &dst_row);
  13604. num_src1_rows = 0;
  13605. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13606. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13607. if (row_id_i != row_id) {
  13608. continue;
  13609. }
  13610. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13611. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  13612. dst_original + i01 * nb1,
  13613. dst_contiguous.get() + num_src1_rows * nb1, nb1).wait()));
  13614. num_src1_rows++;
  13615. }
  13616. }
  13617. }
  13618. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  13619. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  13620. }
  13621. }
  13622. catch (sycl::exception const &exc) {
  13623. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13624. << ", line:" << __LINE__ << std::endl;
  13625. std::exit(1);
  13626. }
  13627. static void ggml_sycl_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13628. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_scale);
  13629. }
  13630. static void ggml_sycl_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13631. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_clamp);
  13632. }
  13633. static void ggml_sycl_cpy(const ggml_tensor *src0, const ggml_tensor *src1,
  13634. ggml_tensor *dst) try {
  13635. const int64_t ne = ggml_nelements(src0);
  13636. GGML_ASSERT(ne == ggml_nelements(src1));
  13637. GGML_ASSERT(src0->backend == GGML_BACKEND_TYPE_GPU);
  13638. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  13639. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  13640. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  13641. GGML_TENSOR_BINARY_OP_LOCALS;
  13642. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13643. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13644. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13645. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13646. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  13647. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  13648. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  13649. 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);
  13650. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  13651. 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);
  13652. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  13653. 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);
  13654. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  13655. 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);
  13656. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  13657. 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);
  13658. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  13659. 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);
  13660. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  13661. 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);
  13662. } else if (src0->type == GGML_TYPE_I16 && src1->type == GGML_TYPE_I16) {
  13663. 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);
  13664. } else if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32) {
  13665. 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);
  13666. } else {
  13667. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  13668. ggml_type_name(src0->type), ggml_type_name(src1->type));
  13669. GGML_ASSERT(false);
  13670. }
  13671. (void) dst;
  13672. }
  13673. catch (sycl::exception const &exc) {
  13674. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13675. << ", line:" << __LINE__ << std::endl;
  13676. std::exit(1);
  13677. }
  13678. static void ggml_sycl_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13679. // TODO: why do we pass dst as src1 here?
  13680. ggml_sycl_cpy(src0, dst, nullptr);
  13681. (void) src1;
  13682. }
  13683. static void ggml_sycl_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13684. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_diag_mask_inf);
  13685. }
  13686. static void ggml_sycl_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13687. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_soft_max);
  13688. }
  13689. static void ggml_sycl_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13690. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  13691. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rope);
  13692. }
  13693. static void ggml_sycl_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13694. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_alibi);
  13695. }
  13696. static void ggml_sycl_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13697. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pool2d);
  13698. }
  13699. static void ggml_sycl_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13700. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_im2col);
  13701. }
  13702. static void ggml_sycl_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13703. GGML_ASSERT(ggml_is_contiguous(src0));
  13704. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sum_rows);
  13705. }
  13706. static void ggml_sycl_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13707. GGML_ASSERT(ggml_is_contiguous(src0));
  13708. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_argsort);
  13709. }
  13710. static void ggml_sycl_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13711. (void) src0;
  13712. (void) src1;
  13713. (void) dst;
  13714. }
  13715. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  13716. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  13717. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  13718. }
  13719. void ggml_sycl_free_data(struct ggml_tensor *tensor) try {
  13720. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_TYPE_GPU && tensor->backend != GGML_BACKEND_TYPE_GPU_SPLIT) ) {
  13721. return;
  13722. }
  13723. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  13724. for (int i = 0; i < g_device_count; ++i) {
  13725. const dpct::queue_ptr stream = g_syclStreams[i][0];
  13726. if (extra->data_device[i] != nullptr) {
  13727. SYCL_CHECK(ggml_sycl_set_device(i));
  13728. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(extra->data_device[i], *stream)));
  13729. }
  13730. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  13731. if (extra->events[i][is] != nullptr) {
  13732. SYCL_CHECK(ggml_sycl_set_device(i));
  13733. SYCL_CHECK(CHECK_TRY_ERROR(
  13734. dpct::destroy_event(extra->events[i][is])));
  13735. }
  13736. }
  13737. }
  13738. delete extra;
  13739. }
  13740. catch (sycl::exception const &exc) {
  13741. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13742. << ", line:" << __LINE__ << std::endl;
  13743. std::exit(1);
  13744. }
  13745. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  13746. static size_t g_temp_tensor_extra_index = 0;
  13747. static ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  13748. if (g_temp_tensor_extras == nullptr) {
  13749. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  13750. }
  13751. size_t alloc_index = g_temp_tensor_extra_index;
  13752. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  13753. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  13754. memset(extra, 0, sizeof(*extra));
  13755. return extra;
  13756. }
  13757. static void ggml_sycl_assign_buffers_impl(struct ggml_tensor *tensor,
  13758. bool scratch, bool force_inplace,
  13759. bool no_alloc) try {
  13760. if (scratch && g_scratch_size == 0) {
  13761. return;
  13762. }
  13763. tensor->backend = GGML_BACKEND_TYPE_GPU;
  13764. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_CPU) {
  13765. const ggml_op src0_op = tensor->src[0]->op;
  13766. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  13767. ggml_sycl_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  13768. }
  13769. }
  13770. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_TYPE_CPU) {
  13771. ggml_sycl_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  13772. }
  13773. if (scratch && no_alloc) {
  13774. return;
  13775. }
  13776. ggml_tensor_extra_gpu * extra;
  13777. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  13778. tensor->op == GGML_OP_VIEW ||
  13779. force_inplace;
  13780. const size_t size = ggml_nbytes(tensor);
  13781. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13782. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13783. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT)) {
  13784. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  13785. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  13786. size_t offset = 0;
  13787. if (tensor->op == GGML_OP_VIEW) {
  13788. memcpy(&offset, tensor->op_params, sizeof(size_t));
  13789. }
  13790. extra = ggml_sycl_alloc_temp_tensor_extra();
  13791. extra->data_device[g_main_device] = src0_ddc + offset;
  13792. } else if (tensor->op == GGML_OP_CPY) {
  13793. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  13794. void * src1_ddv = src1_extra->data_device[g_main_device];
  13795. extra = ggml_sycl_alloc_temp_tensor_extra();
  13796. extra->data_device[g_main_device] = src1_ddv;
  13797. } else if (scratch) {
  13798. GGML_ASSERT(size <= g_scratch_size);
  13799. if (g_scratch_offset + size > g_scratch_size) {
  13800. g_scratch_offset = 0;
  13801. }
  13802. char * data = (char *) g_scratch_buffer;
  13803. if (data == nullptr) {
  13804. SYCL_CHECK(CHECK_TRY_ERROR(
  13805. data = (char *)sycl::malloc_device(
  13806. g_scratch_size, *stream)));
  13807. g_scratch_buffer = data;
  13808. }
  13809. extra = ggml_sycl_alloc_temp_tensor_extra();
  13810. extra->data_device[g_main_device] = data + g_scratch_offset;
  13811. g_scratch_offset += size;
  13812. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  13813. } else { // allocate new buffers outside of scratch
  13814. void * data;
  13815. SYCL_CHECK(CHECK_TRY_ERROR(data = (void *)sycl::malloc_device(
  13816. size, *stream)));
  13817. SYCL_CHECK(CHECK_TRY_ERROR(
  13818. (*stream).memset(data, 0, size).wait()));
  13819. extra = new ggml_tensor_extra_gpu;
  13820. memset(extra, 0, sizeof(*extra));
  13821. extra->data_device[g_main_device] = data;
  13822. }
  13823. tensor->extra = extra;
  13824. }
  13825. catch (sycl::exception const &exc) {
  13826. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13827. << ", line:" << __LINE__ << std::endl;
  13828. std::exit(1);
  13829. }
  13830. void ggml_sycl_copy_to_device(struct ggml_tensor *tensor) try {
  13831. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  13832. GGML_ASSERT(ggml_is_contiguous(tensor));
  13833. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  13834. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13835. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13836. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  13837. .memcpy(extra->data_device[g_main_device],
  13838. tensor->data, ggml_nbytes(tensor))
  13839. .wait()));
  13840. }
  13841. catch (sycl::exception const &exc) {
  13842. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13843. << ", line:" << __LINE__ << std::endl;
  13844. std::exit(1);
  13845. }
  13846. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor) {
  13847. ggml_sycl_assign_buffers_impl(tensor, true, false, false);
  13848. }
  13849. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  13850. ggml_sycl_assign_buffers_impl(tensor, true, false, true);
  13851. }
  13852. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  13853. ggml_sycl_assign_buffers_impl(tensor, false, false, false);
  13854. }
  13855. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  13856. ggml_sycl_assign_buffers_impl(tensor, false, true, false);
  13857. }
  13858. void ggml_sycl_set_main_device(const int main_device) try {
  13859. if (g_main_device == main_device) return;
  13860. check_allow_gpu_index(main_device);
  13861. g_main_device = main_device;
  13862. g_main_device_id = g_sycl_gpu_mgr->gpus[main_device];
  13863. if (g_ggml_sycl_debug) {
  13864. dpct::device_info prop;
  13865. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  13866. prop, dpct::dev_mgr::instance().get_device(g_main_device_id))));
  13867. fprintf(stderr, "Using device %d (%s) as main device\n",
  13868. g_main_device_id, prop.get_name());
  13869. }
  13870. }
  13871. catch (sycl::exception const &exc) {
  13872. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13873. << ", line:" << __LINE__ << std::endl;
  13874. std::exit(1);
  13875. }
  13876. void ggml_sycl_set_scratch_size(const size_t scratch_size) {
  13877. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  13878. // it still won't always work as expected, but it's better than nothing
  13879. if (scratch_size > g_scratch_size) {
  13880. ggml_sycl_free_scratch();
  13881. }
  13882. g_scratch_size = std::max(g_scratch_size, scratch_size);
  13883. }
  13884. void ggml_sycl_free_scratch() try {
  13885. if (g_scratch_buffer == nullptr) {
  13886. return;
  13887. }
  13888. ggml_sycl_set_device(g_main_device);
  13889. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13890. SYCL_CHECK(CHECK_TRY_ERROR(
  13891. sycl::free(g_scratch_buffer, *stream)));
  13892. g_scratch_buffer = nullptr;
  13893. }
  13894. catch (sycl::exception const &exc) {
  13895. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13896. << ", line:" << __LINE__ << std::endl;
  13897. std::exit(1);
  13898. }
  13899. bool ggml_sycl_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  13900. if (!g_sycl_loaded) return false;
  13901. ggml_sycl_func_t func;
  13902. const bool any_on_device = tensor->backend == GGML_BACKEND_TYPE_GPU
  13903. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT))
  13904. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_TYPE_GPU);
  13905. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  13906. return false;
  13907. }
  13908. if (tensor->op == GGML_OP_MUL_MAT) {
  13909. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  13910. #ifndef NDEBUG
  13911. 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]);
  13912. #endif
  13913. return false;
  13914. }
  13915. }
  13916. switch (tensor->op) {
  13917. case GGML_OP_REPEAT:
  13918. func = ggml_sycl_repeat;
  13919. break;
  13920. case GGML_OP_GET_ROWS:
  13921. func = ggml_sycl_get_rows;
  13922. break;
  13923. case GGML_OP_DUP:
  13924. func = ggml_sycl_dup;
  13925. break;
  13926. case GGML_OP_ADD:
  13927. func = ggml_sycl_add;
  13928. break;
  13929. case GGML_OP_ACC:
  13930. func = ggml_sycl_acc;
  13931. break;
  13932. case GGML_OP_MUL:
  13933. func = ggml_sycl_mul;
  13934. break;
  13935. case GGML_OP_DIV:
  13936. func = ggml_sycl_div;
  13937. break;
  13938. case GGML_OP_UNARY:
  13939. switch (ggml_get_unary_op(tensor)) {
  13940. case GGML_UNARY_OP_GELU:
  13941. func = ggml_sycl_gelu;
  13942. break;
  13943. case GGML_UNARY_OP_SILU:
  13944. func = ggml_sycl_silu;
  13945. break;
  13946. case GGML_UNARY_OP_GELU_QUICK:
  13947. func = ggml_sycl_gelu_quick;
  13948. break;
  13949. case GGML_UNARY_OP_TANH:
  13950. func = ggml_sycl_tanh;
  13951. break;
  13952. case GGML_UNARY_OP_RELU:
  13953. func = ggml_sycl_relu;
  13954. break;
  13955. case GGML_UNARY_OP_HARDSIGMOID:
  13956. func = ggml_sycl_hardsigmoid;
  13957. break;
  13958. case GGML_UNARY_OP_HARDSWISH:
  13959. func = ggml_sycl_hardswish;
  13960. break;
  13961. default:
  13962. return false;
  13963. }
  13964. break;
  13965. case GGML_OP_NORM:
  13966. func = ggml_sycl_norm;
  13967. break;
  13968. case GGML_OP_GROUP_NORM:
  13969. func = ggml_sycl_group_norm;
  13970. break;
  13971. case GGML_OP_CONCAT:
  13972. func = ggml_sycl_concat;
  13973. break;
  13974. case GGML_OP_UPSCALE:
  13975. func = ggml_sycl_upscale;
  13976. break;
  13977. case GGML_OP_PAD:
  13978. func = ggml_sycl_pad;
  13979. break;
  13980. case GGML_OP_LEAKY_RELU:
  13981. func = ggml_sycl_leaky_relu;
  13982. break;
  13983. case GGML_OP_RMS_NORM:
  13984. func = ggml_sycl_rms_norm;
  13985. break;
  13986. case GGML_OP_MUL_MAT:
  13987. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  13988. return false;
  13989. }
  13990. func = ggml_sycl_mul_mat;
  13991. break;
  13992. case GGML_OP_MUL_MAT_ID:
  13993. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  13994. return false;
  13995. }
  13996. func = ggml_sycl_mul_mat_id;
  13997. break;
  13998. case GGML_OP_SCALE:
  13999. func = ggml_sycl_scale;
  14000. break;
  14001. case GGML_OP_SQR:
  14002. func = ggml_sycl_sqr;
  14003. break;
  14004. case GGML_OP_CLAMP:
  14005. func = ggml_sycl_clamp;
  14006. break;
  14007. case GGML_OP_CPY:
  14008. func = ggml_sycl_cpy;
  14009. break;
  14010. case GGML_OP_CONT:
  14011. func = ggml_sycl_dup;
  14012. break;
  14013. case GGML_OP_NONE:
  14014. case GGML_OP_RESHAPE:
  14015. case GGML_OP_VIEW:
  14016. case GGML_OP_PERMUTE:
  14017. case GGML_OP_TRANSPOSE:
  14018. func = ggml_sycl_nop;
  14019. break;
  14020. case GGML_OP_DIAG_MASK_INF:
  14021. func = ggml_sycl_diag_mask_inf;
  14022. break;
  14023. case GGML_OP_SOFT_MAX:
  14024. func = ggml_sycl_soft_max;
  14025. break;
  14026. case GGML_OP_ROPE:
  14027. func = ggml_sycl_rope;
  14028. break;
  14029. case GGML_OP_ALIBI:
  14030. func = ggml_sycl_alibi;
  14031. break;
  14032. case GGML_OP_IM2COL:
  14033. func = ggml_sycl_im2col;
  14034. break;
  14035. case GGML_OP_POOL_2D:
  14036. func = ggml_sycl_pool2d;
  14037. break;
  14038. case GGML_OP_SUM_ROWS:
  14039. func = ggml_sycl_sum_rows;
  14040. break;
  14041. case GGML_OP_ARGSORT:
  14042. func = ggml_sycl_argsort;
  14043. break;
  14044. default:
  14045. return false;
  14046. }
  14047. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  14048. ggml_sycl_set_peer_access(tensor->src[1]->ne[1]);
  14049. }
  14050. if (params->ith != 0) {
  14051. return true;
  14052. }
  14053. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  14054. return true;
  14055. }
  14056. func(tensor->src[0], tensor->src[1], tensor);
  14057. return true;
  14058. }
  14059. GGML_API GGML_CALL void ggml_sycl_get_gpu_list(int *id_list, int max_len) try {
  14060. for(int i=0;i<max_len;i++) id_list[i] = -1;
  14061. if (!g_sycl_gpu_mgr) {
  14062. g_sycl_gpu_mgr = new sycl_gpu_mgr();
  14063. }
  14064. for (int i=0;i< g_sycl_gpu_mgr->gpus.size();i++){
  14065. if (i>=max_len) break;
  14066. id_list[i] = g_sycl_gpu_mgr->gpus[i];
  14067. }
  14068. return;
  14069. }
  14070. catch (sycl::exception const &exc) {
  14071. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14072. << ", line:" << __LINE__ << std::endl;
  14073. std::exit(1);
  14074. }
  14075. int ggml_sycl_get_device_count() try {
  14076. int device_count;
  14077. if (CHECK_TRY_ERROR(device_count =
  14078. dpct::dev_mgr::instance().device_count()) != 0) {
  14079. return 0;
  14080. }
  14081. return device_count;
  14082. }
  14083. catch (sycl::exception const &exc) {
  14084. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14085. << ", line:" << __LINE__ << std::endl;
  14086. std::exit(1);
  14087. }
  14088. GGML_API GGML_CALL void ggml_sycl_get_device_description(int device, char *description,
  14089. size_t description_size) try {
  14090. dpct::device_info prop;
  14091. int device_id = g_sycl_gpu_mgr->gpus[device];
  14092. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  14093. prop, dpct::dev_mgr::instance().get_device(device_id))));
  14094. snprintf(description, description_size, "%s", prop.get_name());
  14095. }
  14096. catch (sycl::exception const &exc) {
  14097. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14098. << ", line:" << __LINE__ << std::endl;
  14099. std::exit(1);
  14100. }
  14101. GGML_CALL void ggml_backend_sycl_get_device_memory(int device, size_t *free,
  14102. size_t *total) try {
  14103. ggml_sycl_set_device(device);
  14104. /*
  14105. DPCT1009:218: SYCL uses exceptions to report errors and does not use the
  14106. error codes. The original code was commented out and a warning string was
  14107. inserted. You need to rewrite this code.
  14108. */
  14109. /*
  14110. DPCT1106:217: 'cudaMemGetInfo' was migrated with the Intel extensions for
  14111. device information which may not be supported by all compilers or runtimes.
  14112. You may need to adjust the code.
  14113. */
  14114. int device_id = g_sycl_gpu_mgr->gpus[device];
  14115. SYCL_CHECK(CHECK_TRY_ERROR(
  14116. dpct::dev_mgr::instance().get_device(device_id).get_memory_info(*free, *total)));
  14117. }
  14118. catch (sycl::exception const &exc) {
  14119. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14120. << ", line:" << __LINE__ << std::endl;
  14121. std::exit(1);
  14122. }
  14123. ////////////////////////////////////////////////////////////////////////////////
  14124. // backend interface
  14125. #define UNUSED GGML_UNUSED
  14126. // sycl buffer
  14127. struct ggml_backend_sycl_buffer_context {
  14128. int device;
  14129. void * dev_ptr = nullptr;
  14130. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  14131. size_t temp_tensor_extra_index = 0;
  14132. std::string name;
  14133. ggml_backend_sycl_buffer_context(int device, void * dev_ptr) :
  14134. device(device), dev_ptr(dev_ptr) {
  14135. check_allow_gpu_index(device);
  14136. int id = g_sycl_gpu_mgr->gpus[device];
  14137. name = (GGML_SYCL_NAME + std::to_string(id));
  14138. }
  14139. ~ ggml_backend_sycl_buffer_context() {
  14140. delete[] temp_tensor_extras;
  14141. }
  14142. ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  14143. if (temp_tensor_extras == nullptr) {
  14144. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  14145. }
  14146. size_t alloc_index = temp_tensor_extra_index;
  14147. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  14148. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  14149. memset(extra, 0, sizeof(*extra));
  14150. return extra;
  14151. }
  14152. };
  14153. GGML_CALL static const char * ggml_backend_sycl_buffer_get_name(ggml_backend_buffer_t buffer) {
  14154. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  14155. return ctx->name.c_str();
  14156. }
  14157. GGML_CALL static bool ggml_backend_buffer_is_sycl(ggml_backend_buffer_t buffer) {
  14158. return buffer->iface.get_name == ggml_backend_sycl_buffer_get_name;
  14159. }
  14160. static void
  14161. ggml_backend_sycl_buffer_free_buffer(ggml_backend_buffer_t buffer) try {
  14162. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14163. ggml_sycl_set_device(ctx->device);
  14164. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14165. SYCL_CHECK(
  14166. CHECK_TRY_ERROR(sycl::free(ctx->dev_ptr, *stream)));
  14167. delete ctx;
  14168. }
  14169. catch (sycl::exception const &exc) {
  14170. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14171. << ", line:" << __LINE__ << std::endl;
  14172. std::exit(1);
  14173. }
  14174. static void * ggml_backend_sycl_buffer_get_base(ggml_backend_buffer_t buffer) {
  14175. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14176. return ctx->dev_ptr;
  14177. }
  14178. GGML_CALL static void
  14179. ggml_backend_sycl_buffer_init_tensor(ggml_backend_buffer_t buffer,
  14180. ggml_tensor *tensor) try {
  14181. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  14182. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  14183. assert(tensor->view_src->buffer->buft == buffer->buft);
  14184. tensor->backend = tensor->view_src->backend;
  14185. tensor->extra = tensor->view_src->extra;
  14186. return;
  14187. }
  14188. ggml_tensor_extra_gpu * extra = ctx->ggml_sycl_alloc_temp_tensor_extra();
  14189. extra->data_device[ctx->device] = tensor->data;
  14190. tensor->backend = GGML_BACKEND_TYPE_GPU;
  14191. tensor->extra = extra;
  14192. if (ggml_is_quantized(tensor->type)) {
  14193. // initialize padding to 0 to avoid possible NaN values
  14194. size_t original_size = ggml_nbytes(tensor);
  14195. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  14196. if (padded_size > original_size && tensor->view_src == nullptr) {
  14197. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[ctx->device][0]->memset(
  14198. (char *)tensor->data + original_size, 0,
  14199. padded_size - original_size).wait()));
  14200. }
  14201. }
  14202. }
  14203. catch (sycl::exception const &exc) {
  14204. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14205. << ", line:" << __LINE__ << std::endl;
  14206. std::exit(1);
  14207. }
  14208. static void ggml_backend_sycl_buffer_set_tensor(ggml_backend_buffer_t buffer,
  14209. ggml_tensor *tensor,
  14210. const void *data, size_t offset,
  14211. size_t size) try {
  14212. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14213. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14214. ggml_sycl_set_device(ctx->device);
  14215. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14216. SYCL_CHECK(
  14217. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  14218. SYCL_CHECK(
  14219. CHECK_TRY_ERROR((*stream)
  14220. .memcpy((char *)tensor->data + offset, data, size)
  14221. .wait()));
  14222. }
  14223. catch (sycl::exception const &exc) {
  14224. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14225. << ", line:" << __LINE__ << std::endl;
  14226. std::exit(1);
  14227. }
  14228. static void ggml_backend_sycl_buffer_get_tensor(ggml_backend_buffer_t buffer,
  14229. const ggml_tensor *tensor,
  14230. void *data, size_t offset,
  14231. size_t size) try {
  14232. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14233. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14234. ggml_sycl_set_device(ctx->device);
  14235. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14236. SYCL_CHECK(
  14237. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  14238. SYCL_CHECK(CHECK_TRY_ERROR(
  14239. (*stream)
  14240. .memcpy(data, (const char *)tensor->data + offset, size)
  14241. .wait()));
  14242. }
  14243. catch (sycl::exception const &exc) {
  14244. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14245. << ", line:" << __LINE__ << std::endl;
  14246. std::exit(1);
  14247. }
  14248. GGML_CALL static bool
  14249. ggml_backend_sycl_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
  14250. const ggml_tensor *src,
  14251. ggml_tensor *dst) try {
  14252. if (ggml_backend_buffer_is_sycl(src->buffer)) {
  14253. ggml_backend_sycl_buffer_context * src_ctx = (ggml_backend_sycl_buffer_context *)src->buffer->context;
  14254. ggml_backend_sycl_buffer_context * dst_ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  14255. ggml_sycl_set_device(src_ctx->device);
  14256. /*
  14257. DPCT1009:198: SYCL uses exceptions to report errors and does not use the
  14258. error codes. The original code was commented out and a warning string
  14259. was inserted. You need to rewrite this code.
  14260. */
  14261. SYCL_CHECK(CHECK_TRY_ERROR(
  14262. dpct::dev_mgr::instance().get_device(src_ctx->device).queues_wait_and_throw()));
  14263. ggml_sycl_set_device(dst_ctx->device);
  14264. /*
  14265. DPCT1009:199: SYCL uses exceptions to report errors and does not use the
  14266. error codes. The original code was commented out and a warning string
  14267. was inserted. You need to rewrite this code.
  14268. */
  14269. SYCL_CHECK(CHECK_TRY_ERROR(
  14270. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  14271. /*
  14272. DPCT1009:200: SYCL uses exceptions to report errors and does not use the
  14273. error codes. The original code was commented out and a warning string
  14274. was inserted. You need to rewrite this code.
  14275. */
  14276. dpct::queue_ptr stream_dst = g_syclStreams[dst_ctx->device][0];
  14277. dpct::queue_ptr stream_src = g_syclStreams[src_ctx->device][0];
  14278. size_t size = ggml_nbytes(src);
  14279. //todo. it's dirty solutino to walkaroud known issue:device2device cross GPUs.
  14280. dev2dev_memcpy(*stream_dst, *stream_src, dst->data, src->data, size);
  14281. //todo, it's known issue:error in device2device cross GPUs. reused when the issue is fixed. DON"T remove
  14282. #if 0
  14283. SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy(
  14284. (char *)dst->data, (const char *)src->data, size).wait()));
  14285. /*
  14286. DPCT1009:201: SYCL uses exceptions to report errors and does not use the
  14287. error codes. The original code was commented out and a warning string
  14288. was inserted. You need to rewrite this code.
  14289. */
  14290. SYCL_CHECK(CHECK_TRY_ERROR(
  14291. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  14292. #endif
  14293. return true;
  14294. }
  14295. return false;
  14296. }
  14297. catch (sycl::exception const &exc) {
  14298. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14299. << ", line:" << __LINE__ << std::endl;
  14300. std::exit(1);
  14301. }
  14302. static void ggml_backend_sycl_buffer_clear(ggml_backend_buffer_t buffer,
  14303. uint8_t value) try {
  14304. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  14305. ggml_sycl_set_device(ctx->device);
  14306. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  14307. SYCL_CHECK(
  14308. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  14309. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  14310. .memset(ctx->dev_ptr, value, buffer->size)
  14311. .wait()));
  14312. }
  14313. catch (sycl::exception const &exc) {
  14314. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14315. << ", line:" << __LINE__ << std::endl;
  14316. std::exit(1);
  14317. }
  14318. static struct ggml_backend_buffer_i ggml_backend_sycl_buffer_interface = {
  14319. /* .get_name = */ ggml_backend_sycl_buffer_get_name,
  14320. /* .free_buffer = */ ggml_backend_sycl_buffer_free_buffer,
  14321. /* .get_base = */ ggml_backend_sycl_buffer_get_base,
  14322. /* .init_tensor = */ ggml_backend_sycl_buffer_init_tensor,
  14323. /* .set_tensor = */ ggml_backend_sycl_buffer_set_tensor,
  14324. /* .get_tensor = */ ggml_backend_sycl_buffer_get_tensor,
  14325. /* .cpy_tensor = */ ggml_backend_sycl_buffer_cpy_tensor,
  14326. /* .clear = */ ggml_backend_sycl_buffer_clear,
  14327. /* .reset = */ NULL,
  14328. };
  14329. // sycl buffer type
  14330. struct ggml_backend_sycl_buffer_type_context {
  14331. int device;
  14332. std::string name;
  14333. };
  14334. struct ggml_backend_sycl_context {
  14335. int device;
  14336. std::string name;
  14337. };
  14338. GGML_CALL static const char * ggml_backend_sycl_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14339. ggml_backend_sycl_buffer_type_context * ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14340. return ctx->name.c_str();
  14341. }
  14342. GGML_CALL static ggml_backend_buffer_t
  14343. ggml_backend_sycl_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
  14344. size_t size) try {
  14345. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14346. ggml_sycl_set_device(buft_ctx->device);
  14347. const dpct::queue_ptr stream = g_syclStreams[buft_ctx->device][0];
  14348. size = std::max(size, (size_t)1); // syclMalloc returns null for size 0
  14349. void * dev_ptr;
  14350. SYCL_CHECK(CHECK_TRY_ERROR(dev_ptr = (void *)sycl::malloc_device(
  14351. size, *stream)));
  14352. ggml_backend_sycl_buffer_context * ctx = new ggml_backend_sycl_buffer_context(buft_ctx->device, dev_ptr);
  14353. return ggml_backend_buffer_init(buft, ggml_backend_sycl_buffer_interface, ctx, size);
  14354. }
  14355. catch (sycl::exception const &exc) {
  14356. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14357. << ", line:" << __LINE__ << std::endl;
  14358. std::exit(1);
  14359. }
  14360. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  14361. return 128;
  14362. UNUSED(buft);
  14363. }
  14364. static size_t ggml_backend_sycl_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  14365. return dpct::get_current_device().get_max_mem_alloc_size();
  14366. UNUSED(buft);
  14367. }
  14368. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  14369. size_t size = ggml_nbytes(tensor);
  14370. int64_t ne0 = tensor->ne[0];
  14371. if (ggml_is_quantized(tensor->type)) {
  14372. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14373. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14374. }
  14375. }
  14376. return size;
  14377. UNUSED(buft);
  14378. }
  14379. GGML_CALL static bool ggml_backend_sycl_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  14380. if (!ggml_backend_is_sycl(backend)) {
  14381. return false;
  14382. }
  14383. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14384. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14385. return buft_ctx->device == sycl_ctx->device;
  14386. }
  14387. static ggml_backend_buffer_type_i ggml_backend_sycl_buffer_type_interface = {
  14388. /* .get_name = */ ggml_backend_sycl_buffer_type_name,
  14389. /* .alloc_buffer = */ ggml_backend_sycl_buffer_type_alloc_buffer,
  14390. /* .get_alignment = */ ggml_backend_sycl_buffer_type_get_alignment,
  14391. /* .get_max_size = */ ggml_backend_sycl_buffer_type_get_max_size,
  14392. /* .get_alloc_size = */ ggml_backend_sycl_buffer_type_get_alloc_size,
  14393. /* .supports_backend = */ ggml_backend_sycl_buffer_type_supports_backend,
  14394. /* .is_host = */ nullptr,
  14395. };
  14396. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(int device) {
  14397. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  14398. static bool ggml_backend_sycl_buffer_type_initialized = false;
  14399. if (!ggml_backend_sycl_buffer_type_initialized) {
  14400. for (int i = 0; i < g_device_count; i++) {
  14401. ggml_backend_sycl_buffer_types[i] = {
  14402. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  14403. /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(g_sycl_gpu_mgr->gpus[i])},
  14404. };
  14405. }
  14406. ggml_backend_sycl_buffer_type_initialized = true;
  14407. }
  14408. return &ggml_backend_sycl_buffer_types[device];
  14409. }
  14410. // sycl split buffer type
  14411. 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) {
  14412. const int64_t nrows = ggml_nrows(tensor);
  14413. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  14414. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  14415. *row_low -= *row_low % rounding;
  14416. if (id == g_device_count - 1) {
  14417. *row_high = nrows;
  14418. } else {
  14419. *row_high = nrows*tensor_split[id + 1];
  14420. *row_high -= *row_high % rounding;
  14421. }
  14422. }
  14423. struct ggml_backend_sycl_split_buffer_context {
  14424. ~ggml_backend_sycl_split_buffer_context() try {
  14425. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  14426. for (int i = 0; i < g_device_count; ++i) {
  14427. // int id = g_sycl_gpu_mgr->gpus[i];
  14428. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  14429. if (extra->events[i][is] != nullptr) {
  14430. /*
  14431. DPCT1009:206: SYCL uses exceptions to report errors and
  14432. does not use the error codes. The original code was
  14433. commented out and a warning string was inserted. You
  14434. need to rewrite this code.
  14435. */
  14436. SYCL_CHECK(CHECK_TRY_ERROR(
  14437. dpct::destroy_event(extra->events[i][is])));
  14438. }
  14439. }
  14440. if (extra->data_device[i] != nullptr) {
  14441. /*
  14442. DPCT1009:207: SYCL uses exceptions to report errors and does
  14443. not use the error codes. The original code was commented out
  14444. and a warning string was inserted. You need to rewrite this
  14445. code.
  14446. */
  14447. ggml_sycl_set_device(i);
  14448. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(
  14449. extra->data_device[i], *g_syclStreams[i][0])));
  14450. }
  14451. }
  14452. delete extra;
  14453. }
  14454. }
  14455. catch (sycl::exception const &exc) {
  14456. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14457. << ", line:" << __LINE__ << std::endl;
  14458. std::exit(1);
  14459. }
  14460. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  14461. };
  14462. GGML_CALL static const char * ggml_backend_sycl_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  14463. return GGML_SYCL_NAME "_Split";
  14464. UNUSED(buffer);
  14465. }
  14466. // unused at the moment
  14467. //static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer) {
  14468. // return buffer->iface.get_name == ggml_backend_sycl_split_buffer_get_name;
  14469. //}
  14470. GGML_CALL static void ggml_backend_sycl_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  14471. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  14472. delete ctx;
  14473. }
  14474. GGML_CALL static void * ggml_backend_sycl_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  14475. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  14476. return (void *)0x1000;
  14477. UNUSED(buffer);
  14478. }
  14479. GGML_CALL static void
  14480. ggml_backend_sycl_split_buffer_init_tensor(ggml_backend_buffer_t buffer,
  14481. ggml_tensor *tensor) try {
  14482. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  14483. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  14484. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14485. const int64_t ne0 = tensor->ne[0];
  14486. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  14487. ctx->tensor_extras.push_back(extra);
  14488. for (int i = 0; i < g_device_count; ++i) {
  14489. // int id = g_sycl_gpu_mgr->gpus[i];
  14490. int64_t row_low, row_high;
  14491. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14492. int64_t nrows_split = row_high - row_low;
  14493. if (nrows_split == 0) {
  14494. continue;
  14495. }
  14496. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14497. const size_t original_size = size;
  14498. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14499. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14500. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14501. }
  14502. // FIXME: do not crash if cudaMalloc fails
  14503. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  14504. ggml_sycl_set_device(i);
  14505. char * buf;
  14506. /*
  14507. DPCT1009:208: SYCL uses exceptions to report errors and does not use the
  14508. error codes. The original code was commented out and a warning string
  14509. was inserted. You need to rewrite this code.
  14510. */
  14511. SYCL_CHECK(CHECK_TRY_ERROR(buf = (char *)sycl::malloc_device(
  14512. size, *g_syclStreams[i][0])));
  14513. // set padding to 0 to avoid possible NaN values
  14514. if (size > original_size) {
  14515. /*
  14516. DPCT1009:209: SYCL uses exceptions to report errors and does not use
  14517. the error codes. The original code was commented out and a warning
  14518. string was inserted. You need to rewrite this code.
  14519. */
  14520. SYCL_CHECK(CHECK_TRY_ERROR(
  14521. (*g_syclStreams[i][0])
  14522. .memset(buf + original_size, 0, size - original_size)
  14523. .wait()));
  14524. }
  14525. extra->data_device[i] = buf;
  14526. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  14527. /*
  14528. DPCT1009:210: SYCL uses exceptions to report errors and does not use
  14529. the error codes. The original code was commented out and a warning
  14530. string was inserted. You need to rewrite this code.
  14531. */
  14532. SYCL_CHECK(
  14533. CHECK_TRY_ERROR(extra->events[i][is] = new sycl::event()));
  14534. }
  14535. }
  14536. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  14537. tensor->extra = extra;
  14538. }
  14539. catch (sycl::exception const &exc) {
  14540. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14541. << ", line:" << __LINE__ << std::endl;
  14542. std::exit(1);
  14543. }
  14544. GGML_CALL static void
  14545. ggml_backend_sycl_split_buffer_set_tensor(ggml_backend_buffer_t buffer,
  14546. ggml_tensor *tensor, const void *data,
  14547. size_t offset, size_t size) try {
  14548. // split tensors must always be set in their entirety at once
  14549. GGML_ASSERT(offset == 0);
  14550. GGML_ASSERT(size == ggml_nbytes(tensor));
  14551. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14552. const int64_t ne0 = tensor->ne[0];
  14553. const size_t nb1 = tensor->nb[1];
  14554. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  14555. for (int i = 0; i < g_device_count; ++i) {
  14556. // int id = g_sycl_gpu_mgr->gpus[i];
  14557. int64_t row_low, row_high;
  14558. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14559. int64_t nrows_split = row_high - row_low;
  14560. if (nrows_split == 0) {
  14561. continue;
  14562. }
  14563. const size_t offset_split = row_low*nb1;
  14564. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14565. const size_t original_size = size;
  14566. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14567. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14568. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14569. }
  14570. const char * buf_host = (const char *)data + offset_split;
  14571. /*
  14572. DPCT1009:211: SYCL uses exceptions to report errors and does not use the
  14573. error codes. The original code was commented out and a warning string
  14574. was inserted. You need to rewrite this code.
  14575. */
  14576. ggml_sycl_set_device(i);
  14577. SYCL_CHECK(CHECK_TRY_ERROR(
  14578. (*g_syclStreams[i][0])
  14579. .memcpy(extra->data_device[i], buf_host, original_size)
  14580. .wait()));
  14581. }
  14582. }
  14583. catch (sycl::exception const &exc) {
  14584. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14585. << ", line:" << __LINE__ << std::endl;
  14586. std::exit(1);
  14587. }
  14588. GGML_CALL static void
  14589. ggml_backend_sycl_split_buffer_get_tensor(ggml_backend_buffer_t buffer,
  14590. const ggml_tensor *tensor, void *data,
  14591. size_t offset, size_t size) try {
  14592. // split tensors must always be set in their entirety at once
  14593. GGML_ASSERT(offset == 0);
  14594. GGML_ASSERT(size == ggml_nbytes(tensor));
  14595. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14596. const int64_t ne0 = tensor->ne[0];
  14597. const size_t nb1 = tensor->nb[1];
  14598. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  14599. for (int i = 0; i < g_device_count; ++i) {
  14600. // int id = g_sycl_gpu_mgr->gpus[i];
  14601. int64_t row_low, row_high;
  14602. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14603. int64_t nrows_split = row_high - row_low;
  14604. if (nrows_split == 0) {
  14605. continue;
  14606. }
  14607. const size_t offset_split = row_low*nb1;
  14608. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14609. const size_t original_size = size;
  14610. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14611. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14612. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14613. }
  14614. char * buf_host = (char *)data + offset_split;
  14615. /*
  14616. DPCT1009:212: SYCL uses exceptions to report errors and does not use the
  14617. error codes. The original code was commented out and a warning string
  14618. was inserted. You need to rewrite this code.
  14619. */
  14620. ggml_sycl_set_device(i);
  14621. SYCL_CHECK(CHECK_TRY_ERROR(
  14622. (*g_syclStreams[i][0])
  14623. .memcpy(buf_host, extra->data_device[i], original_size)
  14624. .wait()));
  14625. }
  14626. }
  14627. catch (sycl::exception const &exc) {
  14628. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14629. << ", line:" << __LINE__ << std::endl;
  14630. std::exit(1);
  14631. }
  14632. GGML_CALL static void ggml_backend_sycl_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  14633. UNUSED(buffer);
  14634. UNUSED(value);
  14635. }
  14636. static struct ggml_backend_buffer_i ggml_backend_sycl_split_buffer_interface = {
  14637. /* .get_name = */ ggml_backend_sycl_split_buffer_get_name,
  14638. /* .free_buffer = */ ggml_backend_sycl_split_buffer_free_buffer,
  14639. /* .get_base = */ ggml_backend_sycl_split_buffer_get_base,
  14640. /* .init_tensor = */ ggml_backend_sycl_split_buffer_init_tensor,
  14641. /* .set_tensor = */ ggml_backend_sycl_split_buffer_set_tensor,
  14642. /* .get_tensor = */ ggml_backend_sycl_split_buffer_get_tensor,
  14643. /* .cpy_tensor = */ NULL,
  14644. /* .clear = */ ggml_backend_sycl_split_buffer_clear,
  14645. /* .reset = */ NULL,
  14646. };
  14647. GGML_CALL static const char * ggml_backend_sycl_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14648. return GGML_SYCL_NAME "_Split";
  14649. UNUSED(buft);
  14650. }
  14651. GGML_CALL static ggml_backend_buffer_t ggml_backend_sycl_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  14652. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  14653. // instead, we allocate them for each tensor separately in init_tensor
  14654. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  14655. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  14656. ggml_backend_sycl_split_buffer_context * ctx = new ggml_backend_sycl_split_buffer_context();
  14657. return ggml_backend_buffer_init(buft, ggml_backend_sycl_split_buffer_interface, ctx, size);
  14658. }
  14659. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  14660. return 128;
  14661. UNUSED(buft);
  14662. }
  14663. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  14664. ggml_backend_sycl_split_buffer_type_context * ctx = (ggml_backend_sycl_split_buffer_type_context *)buft->context;
  14665. size_t total_size = 0;
  14666. const int64_t ne0 = tensor->ne[0];
  14667. for (int i = 0; i < g_device_count; ++i) {
  14668. // int id = g_sycl_gpu_mgr->gpus[i];
  14669. int64_t row_low, row_high;
  14670. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, i);
  14671. int64_t nrows_split = row_high - row_low;
  14672. if (nrows_split == 0) {
  14673. continue;
  14674. }
  14675. total_size += ggml_nbytes_split(tensor, nrows_split);
  14676. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14677. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14678. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14679. }
  14680. }
  14681. return total_size;
  14682. }
  14683. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  14684. return ggml_backend_is_sycl(backend);
  14685. UNUSED(buft);
  14686. }
  14687. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  14688. return false;
  14689. UNUSED(buft);
  14690. }
  14691. static ggml_backend_buffer_type_i ggml_backend_sycl_split_buffer_type_interface = {
  14692. /* .get_name = */ ggml_backend_sycl_split_buffer_type_name,
  14693. /* .alloc_buffer = */ ggml_backend_sycl_split_buffer_type_alloc_buffer,
  14694. /* .get_alignment = */ ggml_backend_sycl_split_buffer_type_get_alignment,
  14695. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  14696. /* .get_alloc_size = */ ggml_backend_sycl_split_buffer_type_get_alloc_size,
  14697. /* .supports_backend = */ ggml_backend_sycl_split_buffer_type_supports_backend,
  14698. /* .is_host = */ ggml_backend_sycl_split_buffer_type_is_host,
  14699. };
  14700. GGML_CALL ggml_backend_buffer_type_t ggml_backend_sycl_split_buffer_type(const float * tensor_split) {
  14701. // FIXME: this is not thread safe
  14702. static std::map<std::array<float, GGML_SYCL_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  14703. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split_arr = {};
  14704. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_SYCL_MAX_DEVICES, [](float x) { return x == 0.0f; });
  14705. if (all_zero) {
  14706. tensor_split_arr = g_default_tensor_split;
  14707. } else {
  14708. float split_sum = 0.0f;
  14709. for (int i = 0; i < g_device_count; ++i) {
  14710. // int id = g_sycl_gpu_mgr->gpus[i];
  14711. tensor_split_arr[i] = split_sum;
  14712. split_sum += tensor_split[i];
  14713. }
  14714. for (int i = 0; i < g_device_count; ++i) {
  14715. // int id = g_sycl_gpu_mgr->gpus[i];
  14716. tensor_split_arr[i] /= split_sum;
  14717. }
  14718. }
  14719. auto it = buft_map.find(tensor_split_arr);
  14720. if (it != buft_map.end()) {
  14721. return &it->second;
  14722. }
  14723. struct ggml_backend_buffer_type buft {
  14724. /* .iface = */ ggml_backend_sycl_split_buffer_type_interface,
  14725. /* .context = */ new ggml_backend_sycl_split_buffer_type_context{tensor_split_arr},
  14726. };
  14727. auto result = buft_map.emplace(tensor_split_arr, buft);
  14728. return &result.first->second;
  14729. }
  14730. // host buffer type
  14731. GGML_CALL static const char * ggml_backend_sycl_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14732. return GGML_SYCL_NAME "_Host";
  14733. UNUSED(buft);
  14734. }
  14735. GGML_CALL static const char * ggml_backend_sycl_host_buffer_name(ggml_backend_buffer_t buffer) {
  14736. return GGML_SYCL_NAME "_Host";
  14737. UNUSED(buffer);
  14738. }
  14739. static void ggml_backend_sycl_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  14740. ggml_sycl_host_free(buffer->context);
  14741. }
  14742. static ggml_backend_buffer_t ggml_backend_sycl_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  14743. void * ptr = ggml_sycl_host_malloc(size);
  14744. if (ptr == nullptr) {
  14745. // fallback to cpu buffer
  14746. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  14747. }
  14748. // FIXME: this is a hack to avoid having to implement a new buffer type
  14749. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  14750. buffer->buft = buft;
  14751. buffer->iface.get_name = ggml_backend_sycl_host_buffer_name;
  14752. buffer->iface.free_buffer = ggml_backend_sycl_host_buffer_free_buffer;
  14753. return buffer;
  14754. }
  14755. ggml_backend_buffer_type_t ggml_backend_sycl_host_buffer_type() {
  14756. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_type_host = {
  14757. /* .iface = */ {
  14758. /* .get_name = */ ggml_backend_sycl_host_buffer_type_name,
  14759. /* .alloc_buffer = */ ggml_backend_sycl_host_buffer_type_alloc_buffer,
  14760. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  14761. /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
  14762. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  14763. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  14764. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  14765. },
  14766. /* .context = */ nullptr,
  14767. };
  14768. return &ggml_backend_sycl_buffer_type_host;
  14769. }
  14770. // backend
  14771. GGML_CALL static const char * ggml_backend_sycl_name(ggml_backend_t backend) {
  14772. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14773. return sycl_ctx->name.c_str();
  14774. }
  14775. GGML_CALL static void ggml_backend_sycl_free(ggml_backend_t backend) {
  14776. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14777. delete sycl_ctx;
  14778. delete backend;
  14779. }
  14780. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_sycl_get_default_buffer_type(ggml_backend_t backend) {
  14781. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14782. return ggml_backend_sycl_buffer_type(sycl_ctx->device);
  14783. }
  14784. GGML_CALL static void ggml_backend_sycl_set_tensor_async(ggml_backend_t backend,
  14785. ggml_tensor *tensor,
  14786. const void *data, size_t offset,
  14787. size_t size) try {
  14788. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14789. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  14790. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14791. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14792. (char *)tensor->data + offset, data, size).wait()));
  14793. }
  14794. catch (sycl::exception const &exc) {
  14795. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14796. << ", line:" << __LINE__ << std::endl;
  14797. std::exit(1);
  14798. }
  14799. GGML_CALL static void ggml_backend_sycl_get_tensor_async(ggml_backend_t backend,
  14800. const ggml_tensor *tensor,
  14801. void *data, size_t offset,
  14802. size_t size) try {
  14803. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14804. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  14805. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14806. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14807. data, (const char *)tensor->data + offset, size).wait()));
  14808. }
  14809. catch (sycl::exception const &exc) {
  14810. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14811. << ", line:" << __LINE__ << std::endl;
  14812. std::exit(1);
  14813. }
  14814. GGML_CALL static bool ggml_backend_sycl_cpy_tensor_async(ggml_backend_t backend,
  14815. const ggml_tensor *src,
  14816. ggml_tensor *dst) try {
  14817. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14818. if (dst->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && ggml_backend_buffer_is_sycl(src->buffer)) {
  14819. /*
  14820. DPCT1009:215: SYCL uses exceptions to report errors and does not use the
  14821. error codes. The original code was commented out and a warning string
  14822. was inserted. You need to rewrite this code.
  14823. */
  14824. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14825. dst->data, src->data, ggml_nbytes(dst)).wait()));
  14826. return true;
  14827. }
  14828. return false;
  14829. }
  14830. catch (sycl::exception const &exc) {
  14831. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14832. << ", line:" << __LINE__ << std::endl;
  14833. std::exit(1);
  14834. }
  14835. static void ggml_backend_sycl_synchronize(ggml_backend_t backend) try {
  14836. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14837. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->wait()));
  14838. UNUSED(backend);
  14839. }
  14840. catch (sycl::exception const &exc) {
  14841. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14842. << ", line:" << __LINE__ << std::endl;
  14843. std::exit(1);
  14844. }
  14845. GGML_CALL static ggml_status ggml_backend_sycl_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  14846. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14847. ggml_sycl_set_main_device(sycl_ctx->device);
  14848. ggml_compute_params params = {};
  14849. params.type = GGML_TASK_TYPE_COMPUTE;
  14850. params.ith = 0;
  14851. for (int i = 0; i < cgraph->n_nodes; i++) {
  14852. ggml_tensor * node = cgraph->nodes[i];
  14853. if (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) {
  14854. continue;
  14855. }
  14856. #ifndef NDEBUG
  14857. assert(node->backend == GGML_BACKEND_TYPE_GPU || node->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  14858. assert(node->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  14859. assert(node->extra != nullptr);
  14860. for (int j = 0; j < GGML_MAX_SRC; j++) {
  14861. if (node->src[j] != nullptr) {
  14862. assert(node->src[j]->backend == GGML_BACKEND_TYPE_GPU || node->src[j]->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  14863. assert(node->src[j]->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  14864. assert(node->src[j]->extra != nullptr);
  14865. }
  14866. }
  14867. #endif
  14868. bool ok = ggml_sycl_compute_forward(&params, node);
  14869. if (!ok) {
  14870. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  14871. }
  14872. GGML_ASSERT(ok);
  14873. }
  14874. return GGML_STATUS_SUCCESS;
  14875. }
  14876. GGML_CALL static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  14877. switch (op->op) {
  14878. case GGML_OP_UNARY:
  14879. switch (ggml_get_unary_op(op)) {
  14880. case GGML_UNARY_OP_GELU:
  14881. case GGML_UNARY_OP_SILU:
  14882. case GGML_UNARY_OP_RELU:
  14883. case GGML_UNARY_OP_HARDSIGMOID:
  14884. case GGML_UNARY_OP_HARDSWISH:
  14885. case GGML_UNARY_OP_GELU_QUICK:
  14886. case GGML_UNARY_OP_TANH:
  14887. return true;
  14888. default:
  14889. return false;
  14890. }
  14891. break;
  14892. case GGML_OP_MUL_MAT:
  14893. case GGML_OP_MUL_MAT_ID:
  14894. {
  14895. struct ggml_tensor * a;
  14896. struct ggml_tensor * b;
  14897. if (op->op == GGML_OP_MUL_MAT) {
  14898. a = op->src[0];
  14899. b = op->src[1];
  14900. } else {
  14901. a = op->src[2];
  14902. b = op->src[1];
  14903. }
  14904. if (a->ne[3] != b->ne[3]) {
  14905. return false;
  14906. }
  14907. ggml_type a_type = a->type;
  14908. if (a_type == GGML_TYPE_IQ2_XXS || a_type == GGML_TYPE_IQ2_XS || a_type == GGML_TYPE_IQ3_XXS ||
  14909. a_type == GGML_TYPE_IQ1_S || a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ3_S ||
  14910. a_type == GGML_TYPE_IQ2_S || a_type == GGML_TYPE_IQ4_XS) {
  14911. return false;
  14912. }
  14913. return true;
  14914. } break;
  14915. case GGML_OP_GET_ROWS:
  14916. {
  14917. switch (op->src[0]->type) {
  14918. case GGML_TYPE_F16:
  14919. case GGML_TYPE_F32:
  14920. case GGML_TYPE_Q4_0:
  14921. case GGML_TYPE_Q4_1:
  14922. case GGML_TYPE_Q5_0:
  14923. case GGML_TYPE_Q5_1:
  14924. case GGML_TYPE_Q8_0:
  14925. return true;
  14926. default:
  14927. return false;
  14928. }
  14929. } break;
  14930. case GGML_OP_CPY:
  14931. {
  14932. ggml_type src0_type = op->src[0]->type;
  14933. ggml_type src1_type = op->src[1]->type;
  14934. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  14935. return true;
  14936. }
  14937. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  14938. return true;
  14939. }
  14940. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  14941. return true;
  14942. }
  14943. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  14944. return true;
  14945. }
  14946. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  14947. return true;
  14948. }
  14949. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  14950. return true;
  14951. }
  14952. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  14953. return true;
  14954. }
  14955. return false;
  14956. } break;
  14957. case GGML_OP_CONCAT:
  14958. {
  14959. ggml_type src0_type = op->src[0]->type;
  14960. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  14961. } break;
  14962. case GGML_OP_DUP:
  14963. case GGML_OP_NONE:
  14964. case GGML_OP_RESHAPE:
  14965. case GGML_OP_REPEAT:
  14966. case GGML_OP_VIEW:
  14967. case GGML_OP_PERMUTE:
  14968. case GGML_OP_TRANSPOSE:
  14969. case GGML_OP_NORM:
  14970. case GGML_OP_ADD:
  14971. case GGML_OP_MUL:
  14972. case GGML_OP_DIV:
  14973. case GGML_OP_RMS_NORM:
  14974. case GGML_OP_SCALE:
  14975. case GGML_OP_SQR:
  14976. case GGML_OP_CLAMP:
  14977. case GGML_OP_CONT:
  14978. case GGML_OP_DIAG_MASK_INF:
  14979. case GGML_OP_SOFT_MAX:
  14980. case GGML_OP_ROPE:
  14981. case GGML_OP_ALIBI:
  14982. case GGML_OP_IM2COL:
  14983. case GGML_OP_POOL_2D:
  14984. case GGML_OP_SUM_ROWS:
  14985. case GGML_OP_ARGSORT:
  14986. case GGML_OP_ACC:
  14987. case GGML_OP_GROUP_NORM:
  14988. case GGML_OP_UPSCALE:
  14989. case GGML_OP_PAD:
  14990. case GGML_OP_LEAKY_RELU:
  14991. return true;
  14992. default:
  14993. return false;
  14994. }
  14995. UNUSED(backend);
  14996. }
  14997. static ggml_backend_i ggml_backend_sycl_interface = {
  14998. /* .get_name = */ ggml_backend_sycl_name,
  14999. /* .free = */ ggml_backend_sycl_free,
  15000. /* .get_default_buffer_type = */ ggml_backend_sycl_get_default_buffer_type,
  15001. /* .set_tensor_async = */ ggml_backend_sycl_set_tensor_async,
  15002. /* .get_tensor_async = */ ggml_backend_sycl_get_tensor_async,
  15003. /* .cpy_tensor_async = */ ggml_backend_sycl_cpy_tensor_async,
  15004. /* .synchronize = */ ggml_backend_sycl_synchronize,
  15005. /* .graph_plan_create = */ NULL,
  15006. /* .graph_plan_free = */ NULL,
  15007. /* .graph_plan_compute = */ NULL,
  15008. /* .graph_compute = */ ggml_backend_sycl_graph_compute,
  15009. /* .supports_op = */ ggml_backend_sycl_supports_op,
  15010. };
  15011. static ggml_guid_t ggml_backend_sycl_guid() {
  15012. static ggml_guid guid = { 0x58, 0x05, 0x13, 0x8f, 0xcd, 0x3a, 0x61, 0x9d, 0xe7, 0xcd, 0x98, 0xa9, 0x03, 0xfd, 0x7c, 0x53 };
  15013. return &guid;
  15014. }
  15015. GGML_CALL ggml_backend_t ggml_backend_sycl_init(int device) {
  15016. ggml_init_sycl(); // TODO: remove from ggml.c
  15017. check_allow_gpu_index(device);
  15018. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  15019. ggml_sycl_set_main_device(device);
  15020. int id = g_sycl_gpu_mgr->gpus[device];
  15021. ggml_backend_sycl_context * ctx = new ggml_backend_sycl_context {
  15022. /* .device = */ device,
  15023. /* .name = */ GGML_SYCL_NAME + std::to_string(id),
  15024. };
  15025. ggml_backend_t sycl_backend = new ggml_backend {
  15026. /* .guid = */ ggml_backend_sycl_guid(),
  15027. /* .interface = */ ggml_backend_sycl_interface,
  15028. /* .context = */ ctx
  15029. };
  15030. return sycl_backend;
  15031. }
  15032. bool ggml_backend_is_sycl(ggml_backend_t backend) {
  15033. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_sycl_guid());
  15034. }
  15035. GGML_CALL int ggml_backend_sycl_get_device_count() {
  15036. if (!g_sycl_gpu_mgr) g_sycl_gpu_mgr = new sycl_gpu_mgr();
  15037. return g_sycl_gpu_mgr->get_gpu_count();
  15038. }
  15039. GGML_CALL static ggml_backend_t ggml_backend_reg_sycl_init(const char * params, void * user_data) {
  15040. ggml_backend_t sycl_backend = ggml_backend_sycl_init((int) (intptr_t) user_data);
  15041. return sycl_backend;
  15042. UNUSED(params);
  15043. }
  15044. GGML_API GGML_CALL int ggml_backend_sycl_get_device_index(int device_id) {
  15045. return g_sycl_gpu_mgr->get_index(device_id);
  15046. }
  15047. extern "C" int ggml_backend_sycl_reg_devices();
  15048. int ggml_backend_sycl_reg_devices() {
  15049. if (!g_sycl_gpu_mgr) g_sycl_gpu_mgr = new sycl_gpu_mgr();
  15050. g_device_count = g_sycl_gpu_mgr->get_gpu_count();
  15051. assert(g_device_count>0);
  15052. for (int i = 0; i < g_device_count; i++) {
  15053. int id = g_sycl_gpu_mgr->gpus[i];
  15054. char name[128];
  15055. snprintf(name, sizeof(name), "%s%d", GGML_SYCL_NAME, id);
  15056. ggml_backend_register(name, ggml_backend_reg_sycl_init, ggml_backend_sycl_buffer_type(i), (void *) (intptr_t) i);
  15057. }
  15058. return g_device_count;
  15059. }