ggml-sycl.cpp 582 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254
  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. #if (defined(__SYCL_COMPILER_VERSION) && __SYCL_COMPILER_VERSION >= 20221105)
  603. if (!has(sycl::aspect::ext_intel_free_memory))
  604. {
  605. std::cerr << "get_memory_info: ext_intel_free_memory is not supported." << std::endl;
  606. free_memory = 0;
  607. }
  608. else
  609. {
  610. free_memory = get_info<sycl::ext::intel::info::device::free_memory>();
  611. }
  612. #else
  613. std::cerr << "get_memory_info: ext_intel_free_memory is not supported." << std::endl;
  614. free_memory = 0;
  615. #if defined(_MSC_VER) && !defined(__clang__)
  616. #pragma message("Querying the number of bytes of free memory is not supported")
  617. #else
  618. #warning "Querying the number of bytes of free memory is not supported"
  619. #endif
  620. #endif
  621. total_memory = get_device_info().get_global_mem_size();
  622. }
  623. void get_device_info(device_info &out) const
  624. {
  625. dpct::get_device_info(out, *this);
  626. }
  627. device_info get_device_info() const
  628. {
  629. device_info prop;
  630. dpct::get_device_info(prop, *this);
  631. return prop;
  632. }
  633. void reset()
  634. {
  635. std::lock_guard<mutex_type> lock(m_mutex);
  636. clear_queues();
  637. init_queues();
  638. }
  639. sycl::queue &in_order_queue() { return *_q_in_order; }
  640. sycl::queue &out_of_order_queue() { return *_q_out_of_order; }
  641. sycl::queue &default_queue()
  642. {
  643. #ifdef DPCT_USM_LEVEL_NONE
  644. return out_of_order_queue();
  645. #else
  646. return in_order_queue();
  647. #endif // DPCT_USM_LEVEL_NONE
  648. }
  649. void queues_wait_and_throw()
  650. {
  651. std::unique_lock<mutex_type> lock(m_mutex);
  652. std::vector<std::shared_ptr<sycl::queue>> current_queues(
  653. _queues);
  654. lock.unlock();
  655. for (const auto &q : current_queues)
  656. {
  657. q->wait_and_throw();
  658. }
  659. // Guard the destruct of current_queues to make sure the ref count is safe.
  660. lock.lock();
  661. }
  662. sycl::queue *create_queue(bool enable_exception_handler = false)
  663. {
  664. #ifdef DPCT_USM_LEVEL_NONE
  665. return create_out_of_order_queue(enable_exception_handler);
  666. #else
  667. return create_in_order_queue(enable_exception_handler);
  668. #endif // DPCT_USM_LEVEL_NONE
  669. }
  670. sycl::queue *create_in_order_queue(bool enable_exception_handler = false)
  671. {
  672. std::lock_guard<mutex_type> lock(m_mutex);
  673. return create_queue_impl(enable_exception_handler,
  674. sycl::property::queue::in_order());
  675. }
  676. sycl::queue *create_out_of_order_queue(bool enable_exception_handler = false)
  677. {
  678. std::lock_guard<mutex_type> lock(m_mutex);
  679. return create_queue_impl(enable_exception_handler);
  680. }
  681. void destroy_queue(sycl::queue *&queue)
  682. {
  683. std::lock_guard<mutex_type> lock(m_mutex);
  684. _queues.erase(std::remove_if(_queues.begin(), _queues.end(),
  685. [=](const std::shared_ptr<sycl::queue> &q) -> bool
  686. {
  687. return q.get() == queue;
  688. }),
  689. _queues.end());
  690. queue = nullptr;
  691. }
  692. void set_saved_queue(sycl::queue *q)
  693. {
  694. std::lock_guard<mutex_type> lock(m_mutex);
  695. _saved_queue = q;
  696. }
  697. sycl::queue *get_saved_queue() const
  698. {
  699. std::lock_guard<mutex_type> lock(m_mutex);
  700. return _saved_queue;
  701. }
  702. sycl::context get_context() const { return _ctx; }
  703. private:
  704. void clear_queues()
  705. {
  706. _queues.clear();
  707. _q_in_order = _q_out_of_order = _saved_queue = nullptr;
  708. }
  709. void init_queues()
  710. {
  711. _q_in_order = create_queue_impl(true, sycl::property::queue::in_order());
  712. _q_out_of_order = create_queue_impl(true);
  713. _saved_queue = &default_queue();
  714. }
  715. /// Caller should acquire resource \p m_mutex before calling this function.
  716. template <class... Properties>
  717. sycl::queue *create_queue_impl(bool enable_exception_handler,
  718. Properties... properties)
  719. {
  720. sycl::async_handler eh = {};
  721. if (enable_exception_handler)
  722. {
  723. eh = exception_handler;
  724. }
  725. _queues.push_back(std::make_shared<sycl::queue>(
  726. _ctx, *this, eh,
  727. sycl::property_list(
  728. #ifdef DPCT_PROFILING_ENABLED
  729. sycl::property::queue::enable_profiling(),
  730. #endif
  731. properties...)));
  732. return _queues.back().get();
  733. }
  734. void get_version(int &major, int &minor) const
  735. {
  736. detail::get_version(*this, major, minor);
  737. }
  738. sycl::queue *_q_in_order, *_q_out_of_order;
  739. sycl::queue *_saved_queue;
  740. sycl::context _ctx;
  741. std::vector<std::shared_ptr<sycl::queue>> _queues;
  742. mutable mutex_type m_mutex;
  743. };
  744. /// device manager
  745. class dev_mgr
  746. {
  747. public:
  748. device_ext &current_device()
  749. {
  750. unsigned int dev_id = current_device_id();
  751. check_id(dev_id);
  752. return *_devs[dev_id];
  753. }
  754. device_ext &cpu_device() const
  755. {
  756. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  757. if (_cpu_device == -1)
  758. {
  759. throw std::runtime_error("no valid cpu device");
  760. }
  761. else
  762. {
  763. return *_devs[_cpu_device];
  764. }
  765. }
  766. device_ext &get_device(unsigned int id) const
  767. {
  768. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  769. check_id(id);
  770. return *_devs[id];
  771. }
  772. unsigned int current_device_id() const
  773. {
  774. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  775. auto it = _thread2dev_map.find(get_tid());
  776. if (it != _thread2dev_map.end())
  777. return it->second;
  778. return DEFAULT_DEVICE_ID;
  779. }
  780. /// Select device with a device ID.
  781. /// \param [in] id The id of the device which can
  782. /// be obtained through get_device_id(const sycl::device).
  783. void select_device(unsigned int id)
  784. {
  785. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  786. check_id(id);
  787. _thread2dev_map[get_tid()] = id;
  788. }
  789. unsigned int device_count() { return _devs.size(); }
  790. unsigned int get_device_id(const sycl::device &dev)
  791. {
  792. unsigned int id = 0;
  793. for (auto dev_item : _devs)
  794. {
  795. if (*dev_item == dev)
  796. {
  797. break;
  798. }
  799. id++;
  800. }
  801. return id;
  802. }
  803. template <class DeviceSelector>
  804. std::enable_if_t<
  805. std::is_invocable_r_v<int, DeviceSelector, const sycl::device &>>
  806. select_device(const DeviceSelector &selector = sycl::gpu_selector_v)
  807. {
  808. sycl::device selected_device = sycl::device(selector);
  809. unsigned int selected_device_id = get_device_id(selected_device);
  810. select_device(selected_device_id);
  811. }
  812. /// Returns the instance of device manager singleton.
  813. static dev_mgr &instance()
  814. {
  815. static dev_mgr d_m;
  816. return d_m;
  817. }
  818. dev_mgr(const dev_mgr &) = delete;
  819. dev_mgr &operator=(const dev_mgr &) = delete;
  820. dev_mgr(dev_mgr &&) = delete;
  821. dev_mgr &operator=(dev_mgr &&) = delete;
  822. private:
  823. mutable std::recursive_mutex m_mutex;
  824. dev_mgr()
  825. {
  826. sycl::device default_device =
  827. sycl::device(sycl::default_selector_v);
  828. _devs.push_back(std::make_shared<device_ext>(default_device));
  829. std::vector<sycl::device> sycl_all_devs =
  830. sycl::device::get_devices(sycl::info::device_type::all);
  831. // Collect other devices except for the default device.
  832. if (default_device.is_cpu())
  833. _cpu_device = 0;
  834. for (auto &dev : sycl_all_devs)
  835. {
  836. if (dev == default_device)
  837. {
  838. continue;
  839. }
  840. _devs.push_back(std::make_shared<device_ext>(dev));
  841. if (_cpu_device == -1 && dev.is_cpu())
  842. {
  843. _cpu_device = _devs.size() - 1;
  844. }
  845. }
  846. }
  847. void check_id(unsigned int id) const
  848. {
  849. if (id >= _devs.size())
  850. {
  851. throw std::runtime_error("invalid device id");
  852. }
  853. }
  854. std::vector<std::shared_ptr<device_ext>> _devs;
  855. /// DEFAULT_DEVICE_ID is used, if current_device_id() can not find current
  856. /// thread id in _thread2dev_map, which means default device should be used
  857. /// for the current thread.
  858. const unsigned int DEFAULT_DEVICE_ID = 0;
  859. /// thread-id to device-id map.
  860. std::map<unsigned int, unsigned int> _thread2dev_map;
  861. int _cpu_device = -1;
  862. };
  863. static inline sycl::queue &get_default_queue()
  864. {
  865. return dev_mgr::instance().current_device().default_queue();
  866. }
  867. namespace detail
  868. {
  869. enum class pointer_access_attribute
  870. {
  871. host_only = 0,
  872. device_only,
  873. host_device,
  874. end
  875. };
  876. static pointer_access_attribute get_pointer_attribute(sycl::queue &q,
  877. const void *ptr)
  878. {
  879. #ifdef DPCT_USM_LEVEL_NONE
  880. return mem_mgr::instance().is_device_ptr(ptr)
  881. ? pointer_access_attribute::device_only
  882. : pointer_access_attribute::host_only;
  883. #else
  884. switch (sycl::get_pointer_type(ptr, q.get_context()))
  885. {
  886. case sycl::usm::alloc::unknown:
  887. return pointer_access_attribute::host_only;
  888. case sycl::usm::alloc::device:
  889. return pointer_access_attribute::device_only;
  890. case sycl::usm::alloc::shared:
  891. case sycl::usm::alloc::host:
  892. return pointer_access_attribute::host_device;
  893. }
  894. #endif
  895. }
  896. template <typename ArgT>
  897. inline constexpr std::uint64_t get_type_combination_id(ArgT Val)
  898. {
  899. static_assert((unsigned char)library_data_t::library_data_t_size <=
  900. std::numeric_limits<unsigned char>::max() &&
  901. "library_data_t size exceeds limit.");
  902. static_assert(std::is_same_v<ArgT, library_data_t>, "Unsupported ArgT");
  903. return (std::uint64_t)Val;
  904. }
  905. template <typename FirstT, typename... RestT>
  906. inline constexpr std::uint64_t get_type_combination_id(FirstT FirstVal,
  907. RestT... RestVal)
  908. {
  909. static_assert((std::uint8_t)library_data_t::library_data_t_size <=
  910. std::numeric_limits<unsigned char>::max() &&
  911. "library_data_t size exceeds limit.");
  912. static_assert(sizeof...(RestT) <= 8 && "Too many parameters");
  913. static_assert(std::is_same_v<FirstT, library_data_t>, "Unsupported FirstT");
  914. return get_type_combination_id(RestVal...) << 8 | ((std::uint64_t)FirstVal);
  915. }
  916. class mem_mgr
  917. {
  918. mem_mgr()
  919. {
  920. // Reserved address space, no real memory allocation happens here.
  921. #if defined(__linux__)
  922. mapped_address_space =
  923. (byte_t *)mmap(nullptr, mapped_region_size, PROT_NONE,
  924. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  925. #elif defined(_WIN64)
  926. mapped_address_space = (byte_t *)VirtualAlloc(
  927. NULL, // NULL specified as the base address parameter
  928. mapped_region_size, // Size of allocation
  929. MEM_RESERVE, // Allocate reserved pages
  930. PAGE_NOACCESS); // Protection = no access
  931. #else
  932. #error "Only support Windows and Linux."
  933. #endif
  934. next_free = mapped_address_space;
  935. };
  936. public:
  937. using buffer_id_t = int;
  938. struct allocation
  939. {
  940. buffer_t buffer;
  941. byte_t *alloc_ptr;
  942. size_t size;
  943. };
  944. ~mem_mgr()
  945. {
  946. #if defined(__linux__)
  947. munmap(mapped_address_space, mapped_region_size);
  948. #elif defined(_WIN64)
  949. VirtualFree(mapped_address_space, 0, MEM_RELEASE);
  950. #else
  951. #error "Only support Windows and Linux."
  952. #endif
  953. };
  954. mem_mgr(const mem_mgr &) = delete;
  955. mem_mgr &operator=(const mem_mgr &) = delete;
  956. mem_mgr(mem_mgr &&) = delete;
  957. mem_mgr &operator=(mem_mgr &&) = delete;
  958. /// Allocate
  959. void *mem_alloc(size_t size)
  960. {
  961. if (!size)
  962. return nullptr;
  963. std::lock_guard<std::mutex> lock(m_mutex);
  964. if (next_free + size > mapped_address_space + mapped_region_size)
  965. {
  966. throw std::runtime_error("dpct_malloc: out of memory for virtual memory pool");
  967. }
  968. // Allocation
  969. sycl::range<1> r(size);
  970. buffer_t buf(r);
  971. allocation A{buf, next_free, size};
  972. // Map allocation to device pointer
  973. void *result = next_free;
  974. m_map.emplace(next_free + size, A);
  975. // Update pointer to the next free space.
  976. next_free += (size + extra_padding + alignment - 1) & ~(alignment - 1);
  977. return result;
  978. }
  979. /// Deallocate
  980. void mem_free(const void *ptr)
  981. {
  982. if (!ptr)
  983. return;
  984. std::lock_guard<std::mutex> lock(m_mutex);
  985. auto it = get_map_iterator(ptr);
  986. m_map.erase(it);
  987. }
  988. /// map: device pointer -> allocation(buffer, alloc_ptr, size)
  989. allocation translate_ptr(const void *ptr)
  990. {
  991. std::lock_guard<std::mutex> lock(m_mutex);
  992. auto it = get_map_iterator(ptr);
  993. return it->second;
  994. }
  995. /// Check if the pointer represents device pointer or not.
  996. bool is_device_ptr(const void *ptr) const
  997. {
  998. std::lock_guard<std::mutex> lock(m_mutex);
  999. return (mapped_address_space <= ptr) &&
  1000. (ptr < mapped_address_space + mapped_region_size);
  1001. }
  1002. /// Returns the instance of memory manager singleton.
  1003. static mem_mgr &instance()
  1004. {
  1005. static mem_mgr m;
  1006. return m;
  1007. }
  1008. private:
  1009. std::map<byte_t *, allocation> m_map;
  1010. mutable std::mutex m_mutex;
  1011. byte_t *mapped_address_space;
  1012. byte_t *next_free;
  1013. const size_t mapped_region_size = 128ull * 1024 * 1024 * 1024;
  1014. const size_t alignment = 256;
  1015. /// This padding may be defined to some positive value to debug
  1016. /// out of bound accesses.
  1017. const size_t extra_padding = 0;
  1018. std::map<byte_t *, allocation>::iterator get_map_iterator(const void *ptr)
  1019. {
  1020. auto it = m_map.upper_bound((byte_t *)ptr);
  1021. if (it == m_map.end())
  1022. {
  1023. // Not a virtual pointer.
  1024. throw std::runtime_error("can not get buffer from non-virtual pointer");
  1025. }
  1026. const allocation &alloc = it->second;
  1027. if (ptr < alloc.alloc_ptr)
  1028. {
  1029. // Out of bound.
  1030. // This may happen if there's a gap between allocations due to alignment
  1031. // or extra padding and pointer points to this gap.
  1032. throw std::runtime_error("invalid virtual pointer");
  1033. }
  1034. return it;
  1035. }
  1036. };
  1037. template <class T, memory_region Memory, size_t Dimension>
  1038. class accessor;
  1039. template <memory_region Memory, class T = byte_t>
  1040. class memory_traits
  1041. {
  1042. public:
  1043. static constexpr sycl::access::target target =
  1044. sycl::access::target::device;
  1045. static constexpr sycl::access_mode mode =
  1046. (Memory == constant) ? sycl::access_mode::read
  1047. : sycl::access_mode::read_write;
  1048. static constexpr size_t type_size = sizeof(T);
  1049. using element_t =
  1050. typename std::conditional<Memory == constant, const T, T>::type;
  1051. using value_t = typename std::remove_cv<T>::type;
  1052. template <size_t Dimension = 1>
  1053. using accessor_t = typename std::conditional<
  1054. Memory == local, sycl::local_accessor<value_t, Dimension>,
  1055. sycl::accessor<T, Dimension, mode, target>>::type;
  1056. using pointer_t = T *;
  1057. };
  1058. static inline void *dpct_malloc(size_t size, sycl::queue &q)
  1059. {
  1060. #ifdef DPCT_USM_LEVEL_NONE
  1061. return mem_mgr::instance().mem_alloc(size * sizeof(byte_t));
  1062. #else
  1063. return sycl::malloc_device(size, q.get_device(), q.get_context());
  1064. #endif // DPCT_USM_LEVEL_NONE
  1065. }
  1066. #define PITCH_DEFAULT_ALIGN(x) (((x) + 31) & ~(0x1F))
  1067. static inline void *dpct_malloc(size_t &pitch, size_t x, size_t y, size_t z,
  1068. sycl::queue &q)
  1069. {
  1070. pitch = PITCH_DEFAULT_ALIGN(x);
  1071. return dpct_malloc(pitch * y * z, q);
  1072. }
  1073. /**
  1074. * @brief Sets \p value to the first \p size elements starting from \p dev_ptr in \p q.
  1075. * @tparam valueT The type of the element to be set.
  1076. * @param [in] q The queue in which the operation is done.
  1077. * @param [in] dev_ptr Pointer to the virtual device memory address.
  1078. * @param [in] value The value to be set.
  1079. * @param [in] size Number of elements to be set to the value.
  1080. * @return An event representing the memset operation.
  1081. */
  1082. template <typename valueT>
  1083. static inline sycl::event dpct_memset(sycl::queue &q, void *dev_ptr,
  1084. valueT value, size_t size)
  1085. {
  1086. #ifdef DPCT_USM_LEVEL_NONE
  1087. auto &mm = mem_mgr::instance();
  1088. assert(mm.is_device_ptr(dev_ptr));
  1089. auto alloc = mm.translate_ptr(dev_ptr);
  1090. size_t offset = (valueT *)dev_ptr - (valueT *)alloc.alloc_ptr;
  1091. return q.submit([&](sycl::handler &cgh)
  1092. {
  1093. auto r = sycl::range<1>(size);
  1094. auto o = sycl::id<1>(offset);
  1095. auto new_buffer = alloc.buffer.reinterpret<valueT>(
  1096. sycl::range<1>(alloc.size / sizeof(valueT)));
  1097. sycl::accessor<valueT, 1, sycl::access_mode::write,
  1098. sycl::access::target::device>
  1099. acc(new_buffer, cgh, r, o);
  1100. cgh.fill(acc, value); });
  1101. #else
  1102. return q.fill(dev_ptr, value, size);
  1103. #endif // DPCT_USM_LEVEL_NONE
  1104. }
  1105. /**
  1106. * @brief Sets \p value to the 3D memory region pointed by \p data in \p q.
  1107. * @tparam valueT The type of the element to be set.
  1108. * @param [in] q The queue in which the operation is done.
  1109. * @param [in] data Pointer to the pitched device memory region.
  1110. * @param [in] value The value to be set.
  1111. * @param [in] size 3D memory region by number of elements.
  1112. * @return An event list representing the memset operations.
  1113. */
  1114. template <typename valueT>
  1115. static inline std::vector<sycl::event>
  1116. dpct_memset(sycl::queue &q, pitched_data data, valueT value,
  1117. sycl::range<3> size)
  1118. {
  1119. std::vector<sycl::event> event_list;
  1120. size_t slice = data.get_pitch() * data.get_y();
  1121. unsigned char *data_surface = (unsigned char *)data.get_data_ptr();
  1122. for (size_t z = 0; z < size.get(2); ++z)
  1123. {
  1124. unsigned char *data_ptr = data_surface;
  1125. for (size_t y = 0; y < size.get(1); ++y)
  1126. {
  1127. event_list.push_back(dpct_memset(q, data_ptr, value, size.get(0)));
  1128. data_ptr += data.get_pitch();
  1129. }
  1130. data_surface += slice;
  1131. }
  1132. return event_list;
  1133. }
  1134. /**
  1135. * @brief Sets \p val to the pitched 2D memory region pointed by \p ptr in \p q.
  1136. * @tparam valueT The type of the element to be set.
  1137. * @param [in] q The queue in which the operation is done.
  1138. * @param [in] ptr Pointer to the virtual device memory.
  1139. * @param [in] pitch The pitch size by number of elements, including padding.
  1140. * @param [in] val The value to be set.
  1141. * @param [in] x The width of memory region by number of elements.
  1142. * @param [in] y The height of memory region by number of elements.
  1143. * @return An event list representing the memset operations.
  1144. */
  1145. template <typename valueT>
  1146. static inline std::vector<sycl::event>
  1147. dpct_memset(sycl::queue &q, void *ptr, size_t pitch, valueT val, size_t x,
  1148. size_t y)
  1149. {
  1150. return dpct_memset(q, pitched_data(ptr, pitch, x, 1), val,
  1151. sycl::range<3>(x, y, 1));
  1152. }
  1153. static memcpy_direction deduce_memcpy_direction(sycl::queue &q, void *to_ptr,
  1154. const void *from_ptr,
  1155. memcpy_direction dir)
  1156. {
  1157. switch (dir)
  1158. {
  1159. case memcpy_direction::host_to_host:
  1160. case memcpy_direction::host_to_device:
  1161. case memcpy_direction::device_to_host:
  1162. case memcpy_direction::device_to_device:
  1163. return dir;
  1164. case memcpy_direction::automatic:
  1165. {
  1166. // table[to_attribute][from_attribute]
  1167. static const memcpy_direction
  1168. direction_table[static_cast<unsigned>(pointer_access_attribute::end)]
  1169. [static_cast<unsigned>(pointer_access_attribute::end)] =
  1170. {{memcpy_direction::host_to_host,
  1171. memcpy_direction::device_to_host,
  1172. memcpy_direction::host_to_host},
  1173. {memcpy_direction::host_to_device,
  1174. memcpy_direction::device_to_device,
  1175. memcpy_direction::device_to_device},
  1176. {memcpy_direction::host_to_host,
  1177. memcpy_direction::device_to_device,
  1178. memcpy_direction::device_to_device}};
  1179. return direction_table[static_cast<unsigned>(get_pointer_attribute(
  1180. q, to_ptr))][static_cast<unsigned>(get_pointer_attribute(q, from_ptr))];
  1181. }
  1182. default:
  1183. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1184. }
  1185. }
  1186. static sycl::event
  1187. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1188. memcpy_direction direction,
  1189. const std::vector<sycl::event> &dep_events = {})
  1190. {
  1191. if (!size)
  1192. return sycl::event{};
  1193. #ifdef DPCT_USM_LEVEL_NONE
  1194. auto &mm = mem_mgr::instance();
  1195. auto real_direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1196. switch (real_direction)
  1197. {
  1198. case host_to_host:
  1199. return q.submit([&](sycl::handler &cgh)
  1200. {
  1201. cgh.depends_on(dep_events);
  1202. cgh.host_task([=] { std::memcpy(to_ptr, from_ptr, size); }); });
  1203. case host_to_device:
  1204. {
  1205. auto alloc = mm.translate_ptr(to_ptr);
  1206. size_t offset = (byte_t *)to_ptr - alloc.alloc_ptr;
  1207. return q.submit([&](sycl::handler &cgh)
  1208. {
  1209. cgh.depends_on(dep_events);
  1210. auto r = sycl::range<1>(size);
  1211. auto o = sycl::id<1>(offset);
  1212. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1213. sycl::access::target::device>
  1214. acc(alloc.buffer, cgh, r, o);
  1215. cgh.copy(from_ptr, acc); });
  1216. }
  1217. case device_to_host:
  1218. {
  1219. auto alloc = mm.translate_ptr(from_ptr);
  1220. size_t offset = (byte_t *)from_ptr - alloc.alloc_ptr;
  1221. return q.submit([&](sycl::handler &cgh)
  1222. {
  1223. cgh.depends_on(dep_events);
  1224. auto r = sycl::range<1>(size);
  1225. auto o = sycl::id<1>(offset);
  1226. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1227. sycl::access::target::device>
  1228. acc(alloc.buffer, cgh, r, o);
  1229. cgh.copy(acc, to_ptr); });
  1230. }
  1231. case device_to_device:
  1232. {
  1233. auto to_alloc = mm.translate_ptr(to_ptr);
  1234. auto from_alloc = mm.translate_ptr(from_ptr);
  1235. size_t to_offset = (byte_t *)to_ptr - to_alloc.alloc_ptr;
  1236. size_t from_offset = (byte_t *)from_ptr - from_alloc.alloc_ptr;
  1237. return q.submit([&](sycl::handler &cgh)
  1238. {
  1239. cgh.depends_on(dep_events);
  1240. auto r = sycl::range<1>(size);
  1241. auto to_o = sycl::id<1>(to_offset);
  1242. auto from_o = sycl::id<1>(from_offset);
  1243. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1244. sycl::access::target::device>
  1245. to_acc(to_alloc.buffer, cgh, r, to_o);
  1246. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1247. sycl::access::target::device>
  1248. from_acc(from_alloc.buffer, cgh, r, from_o);
  1249. cgh.copy(from_acc, to_acc); });
  1250. }
  1251. default:
  1252. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1253. }
  1254. #else
  1255. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  1256. #endif // DPCT_USM_LEVEL_NONE
  1257. }
  1258. // Get actual copy range and make sure it will not exceed range.
  1259. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  1260. size_t pitch)
  1261. {
  1262. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  1263. }
  1264. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  1265. size_t pitch)
  1266. {
  1267. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  1268. }
  1269. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  1270. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  1271. static inline std::vector<sycl::event>
  1272. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1273. sycl::range<3> to_range, sycl::range<3> from_range,
  1274. sycl::id<3> to_id, sycl::id<3> from_id,
  1275. sycl::range<3> size, memcpy_direction direction,
  1276. const std::vector<sycl::event> &dep_events = {})
  1277. {
  1278. // RAII for host pointer
  1279. class host_buffer
  1280. {
  1281. void *_buf;
  1282. size_t _size;
  1283. sycl::queue &_q;
  1284. const std::vector<sycl::event> &_deps; // free operation depends
  1285. public:
  1286. host_buffer(size_t size, sycl::queue &q,
  1287. const std::vector<sycl::event> &deps)
  1288. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  1289. void *get_ptr() const { return _buf; }
  1290. size_t get_size() const { return _size; }
  1291. ~host_buffer()
  1292. {
  1293. if (_buf)
  1294. {
  1295. _q.submit([&](sycl::handler &cgh)
  1296. {
  1297. cgh.depends_on(_deps);
  1298. cgh.host_task([buf = _buf] { std::free(buf); }); });
  1299. }
  1300. }
  1301. };
  1302. std::vector<sycl::event> event_list;
  1303. size_t to_slice = to_range.get(1) * to_range.get(0),
  1304. from_slice = from_range.get(1) * from_range.get(0);
  1305. unsigned char *to_surface =
  1306. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  1307. const unsigned char *from_surface =
  1308. (const unsigned char *)from_ptr +
  1309. get_offset(from_id, from_slice, from_range.get(0));
  1310. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  1311. {
  1312. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  1313. direction, dep_events)};
  1314. }
  1315. direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1316. size_t size_slice = size.get(1) * size.get(0);
  1317. switch (direction)
  1318. {
  1319. case host_to_host:
  1320. for (size_t z = 0; z < size.get(2); ++z)
  1321. {
  1322. unsigned char *to_ptr = to_surface;
  1323. const unsigned char *from_ptr = from_surface;
  1324. if (to_range.get(0) == from_range.get(0) &&
  1325. to_range.get(0) == size.get(0))
  1326. {
  1327. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  1328. direction, dep_events));
  1329. }
  1330. else
  1331. {
  1332. for (size_t y = 0; y < size.get(1); ++y)
  1333. {
  1334. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  1335. direction, dep_events));
  1336. to_ptr += to_range.get(0);
  1337. from_ptr += from_range.get(0);
  1338. }
  1339. }
  1340. to_surface += to_slice;
  1341. from_surface += from_slice;
  1342. }
  1343. break;
  1344. case host_to_device:
  1345. {
  1346. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  1347. event_list);
  1348. std::vector<sycl::event> host_events;
  1349. if (to_slice == size_slice)
  1350. {
  1351. // Copy host data to a temp host buffer with the shape of target.
  1352. host_events =
  1353. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  1354. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  1355. host_to_host, dep_events);
  1356. }
  1357. else
  1358. {
  1359. // Copy host data to a temp host buffer with the shape of target.
  1360. host_events = dpct_memcpy(
  1361. q, buf.get_ptr(), from_surface, to_range, from_range,
  1362. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  1363. // If has padding data, not sure whether it is useless. So fill temp
  1364. // buffer with it.
  1365. std::vector<sycl::event>{
  1366. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  1367. device_to_host, dep_events)});
  1368. }
  1369. // Copy from temp host buffer to device with only one submit.
  1370. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  1371. buf.get_size(), host_to_device,
  1372. host_events));
  1373. break;
  1374. }
  1375. case device_to_host:
  1376. {
  1377. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  1378. event_list);
  1379. // Copy from host temp buffer to host target with reshaping.
  1380. event_list = dpct_memcpy(
  1381. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  1382. sycl::id<3>(0, 0, 0), size, host_to_host,
  1383. // Copy from device to temp host buffer with only one submit.
  1384. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  1385. buf.get_size(),
  1386. device_to_host, dep_events)});
  1387. break;
  1388. }
  1389. case device_to_device:
  1390. #ifdef DPCT_USM_LEVEL_NONE
  1391. {
  1392. auto &mm = mem_mgr::instance();
  1393. auto to_alloc = mm.translate_ptr(to_surface);
  1394. auto from_alloc = mm.translate_ptr(from_surface);
  1395. size_t to_offset = (byte_t *)to_surface - to_alloc.alloc_ptr;
  1396. size_t from_offset = (byte_t *)from_surface - from_alloc.alloc_ptr;
  1397. event_list.push_back(q.submit([&](sycl::handler &cgh)
  1398. {
  1399. cgh.depends_on(dep_events);
  1400. auto to_o = sycl::id<1>(to_offset);
  1401. auto from_o = sycl::id<1>(from_offset);
  1402. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1403. sycl::access::target::device>
  1404. to_acc(to_alloc.buffer, cgh,
  1405. get_copy_range(size, to_slice, to_range.get(0)), to_o);
  1406. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1407. sycl::access::target::device>
  1408. from_acc(from_alloc.buffer, cgh,
  1409. get_copy_range(size, from_slice, from_range.get(0)), from_o);
  1410. cgh.parallel_for<class dpct_memcpy_3d_detail_usmnone>(
  1411. size,
  1412. [=](sycl::id<3> id) {
  1413. to_acc[get_offset(id, to_slice, to_range.get(0))] =
  1414. from_acc[get_offset(id, from_slice, from_range.get(0))];
  1415. }); }));
  1416. }
  1417. #else
  1418. event_list.push_back(q.submit([&](sycl::handler &cgh)
  1419. {
  1420. cgh.depends_on(dep_events);
  1421. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  1422. size,
  1423. [=](sycl::id<3> id) {
  1424. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  1425. from_surface[get_offset(id, from_slice, from_range.get(0))];
  1426. }); }));
  1427. #endif
  1428. break;
  1429. default:
  1430. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1431. }
  1432. return event_list;
  1433. }
  1434. /// memcpy 2D/3D matrix specified by pitched_data.
  1435. static inline std::vector<sycl::event>
  1436. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  1437. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  1438. memcpy_direction direction = automatic)
  1439. {
  1440. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  1441. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  1442. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  1443. size, direction);
  1444. }
  1445. /// memcpy 2D matrix with pitch.
  1446. static inline std::vector<sycl::event>
  1447. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1448. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  1449. memcpy_direction direction = automatic)
  1450. {
  1451. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  1452. sycl::range<3>(from_pitch, y, 1),
  1453. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  1454. sycl::range<3>(x, y, 1), direction);
  1455. }
  1456. namespace deprecated
  1457. {
  1458. template <typename T, sycl::usm::alloc AllocKind>
  1459. class usm_allocator
  1460. {
  1461. private:
  1462. using Alloc = sycl::usm_allocator<T, AllocKind>;
  1463. Alloc _impl;
  1464. public:
  1465. using value_type = typename std::allocator_traits<Alloc>::value_type;
  1466. using pointer = typename std::allocator_traits<Alloc>::pointer;
  1467. using const_pointer = typename std::allocator_traits<Alloc>::const_pointer;
  1468. using void_pointer = typename std::allocator_traits<Alloc>::void_pointer;
  1469. using const_void_pointer =
  1470. typename std::allocator_traits<Alloc>::const_void_pointer;
  1471. using reference = typename std::allocator_traits<Alloc>::value_type &;
  1472. using const_reference =
  1473. const typename std::allocator_traits<Alloc>::value_type &;
  1474. using difference_type =
  1475. typename std::allocator_traits<Alloc>::difference_type;
  1476. using size_type = typename std::allocator_traits<Alloc>::size_type;
  1477. using propagate_on_container_copy_assignment = typename std::allocator_traits<
  1478. Alloc>::propagate_on_container_copy_assignment;
  1479. using propagate_on_container_move_assignment = typename std::allocator_traits<
  1480. Alloc>::propagate_on_container_move_assignment;
  1481. using propagate_on_container_swap =
  1482. typename std::allocator_traits<Alloc>::propagate_on_container_swap;
  1483. using is_always_equal =
  1484. typename std::allocator_traits<Alloc>::is_always_equal;
  1485. template <typename U>
  1486. struct rebind
  1487. {
  1488. typedef usm_allocator<U, AllocKind> other;
  1489. };
  1490. usm_allocator() : _impl(dpct::get_default_queue()) {}
  1491. ~usm_allocator() {}
  1492. usm_allocator(const usm_allocator &other) : _impl(other._impl) {}
  1493. usm_allocator(usm_allocator &&other) : _impl(std::move(other._impl)) {}
  1494. pointer address(reference r) { return &r; }
  1495. const_pointer address(const_reference r) { return &r; }
  1496. pointer allocate(size_type cnt, const_void_pointer hint = nullptr)
  1497. {
  1498. return std::allocator_traits<Alloc>::allocate(_impl, cnt, hint);
  1499. }
  1500. void deallocate(pointer p, size_type cnt)
  1501. {
  1502. std::allocator_traits<Alloc>::deallocate(_impl, p, cnt);
  1503. }
  1504. size_type max_size() const
  1505. {
  1506. return std::allocator_traits<Alloc>::max_size(_impl);
  1507. }
  1508. bool operator==(const usm_allocator &other) const { return _impl == other._impl; }
  1509. bool operator!=(const usm_allocator &other) const { return _impl != other._impl; }
  1510. };
  1511. } // namespace deprecated
  1512. inline void dpct_free(void *ptr,
  1513. const sycl::queue &q)
  1514. {
  1515. if (ptr)
  1516. {
  1517. #ifdef DPCT_USM_LEVEL_NONE
  1518. detail::mem_mgr::instance().mem_free(ptr);
  1519. #else
  1520. sycl::free(ptr, q.get_context());
  1521. #endif // DPCT_USM_LEVEL_NONE
  1522. }
  1523. }
  1524. template <typename T>
  1525. inline auto get_memory(const void *x)
  1526. {
  1527. T *new_x = reinterpret_cast<T *>(const_cast<void *>(x));
  1528. #ifdef DPCT_USM_LEVEL_NONE
  1529. return dpct::get_buffer<std::remove_cv_t<T>>(new_x);
  1530. #else
  1531. return new_x;
  1532. #endif
  1533. }
  1534. template <typename T>
  1535. inline typename DataType<T>::T2 get_value(const T *s, sycl::queue &q)
  1536. {
  1537. using Ty = typename DataType<T>::T2;
  1538. Ty s_h;
  1539. if (get_pointer_attribute(q, s) == pointer_access_attribute::device_only)
  1540. detail::dpct_memcpy(q, (void *)&s_h, (void *)s, sizeof(T), device_to_host)
  1541. .wait();
  1542. else
  1543. s_h = *reinterpret_cast<const Ty *>(s);
  1544. return s_h;
  1545. }
  1546. } // namespace detail
  1547. template <typename T>
  1548. inline auto get_value(const T *s, sycl::queue &q)
  1549. {
  1550. return detail::get_value(s, q);
  1551. }
  1552. namespace detail
  1553. {
  1554. template <class Ta, class Tb, class Tc, class Ts>
  1555. inline void gemm_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1556. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1557. const void *alpha, const void *a, int lda, const void *b,
  1558. int ldb, const void *beta, void *c, int ldc)
  1559. {
  1560. #ifndef __INTEL_MKL__
  1561. throw std::runtime_error("The oneAPI Math Kernel Library (oneMKL) Interfaces "
  1562. "Project does not support this API.");
  1563. #else
  1564. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1565. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1566. auto data_a = get_memory<const Ta>(a);
  1567. auto data_b = get_memory<const Tb>(b);
  1568. auto data_c = get_memory<Tc>(c);
  1569. oneapi::mkl::blas::column_major::gemm(
  1570. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1571. data_b, ldb, beta_value, data_c, ldc);
  1572. #endif
  1573. }
  1574. template <typename VecT, class BinaryOperation, class = void>
  1575. class vectorized_binary
  1576. {
  1577. public:
  1578. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1579. {
  1580. VecT v4;
  1581. for (size_t i = 0; i < v4.size(); ++i)
  1582. {
  1583. v4[i] = binary_op(a[i], b[i]);
  1584. }
  1585. return v4;
  1586. }
  1587. };
  1588. template <typename VecT, class BinaryOperation>
  1589. class vectorized_binary<
  1590. VecT, BinaryOperation,
  1591. std::void_t<std::invoke_result_t<BinaryOperation, VecT, VecT>>>
  1592. {
  1593. public:
  1594. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1595. {
  1596. return binary_op(a, b).template as<VecT>();
  1597. }
  1598. };
  1599. template <class Ta, class Tb, class Tc, class Ts>
  1600. inline void gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1601. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1602. const void *alpha, const void **a, int lda,
  1603. const void **b, int ldb, const void *beta, void **c,
  1604. int ldc, int batch_size)
  1605. {
  1606. struct matrix_info_t
  1607. {
  1608. oneapi::mkl::transpose transpose_info[2];
  1609. Ts value_info[2];
  1610. std::int64_t size_info[3];
  1611. std::int64_t ld_info[3];
  1612. std::int64_t groupsize_info;
  1613. };
  1614. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1615. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1616. matrix_info_t *matrix_info =
  1617. (matrix_info_t *)std::malloc(sizeof(matrix_info_t));
  1618. matrix_info->transpose_info[0] = a_trans;
  1619. matrix_info->transpose_info[1] = b_trans;
  1620. matrix_info->value_info[0] = alpha_value;
  1621. matrix_info->value_info[1] = beta_value;
  1622. matrix_info->size_info[0] = m;
  1623. matrix_info->size_info[1] = n;
  1624. matrix_info->size_info[2] = k;
  1625. matrix_info->ld_info[0] = lda;
  1626. matrix_info->ld_info[1] = ldb;
  1627. matrix_info->ld_info[2] = ldc;
  1628. matrix_info->groupsize_info = batch_size;
  1629. sycl::event e = oneapi::mkl::blas::column_major::gemm_batch(
  1630. q, matrix_info->transpose_info, matrix_info->transpose_info + 1,
  1631. matrix_info->size_info, matrix_info->size_info + 1,
  1632. matrix_info->size_info + 2, matrix_info->value_info,
  1633. reinterpret_cast<const Ta **>(a), matrix_info->ld_info,
  1634. reinterpret_cast<const Tb **>(b), matrix_info->ld_info + 1,
  1635. matrix_info->value_info + 1, reinterpret_cast<Tc **>(c),
  1636. matrix_info->ld_info + 2, 1, &(matrix_info->groupsize_info));
  1637. q.submit([&](sycl::handler &cgh)
  1638. {
  1639. cgh.depends_on(e);
  1640. cgh.host_task([=] { std::free(matrix_info); }); });
  1641. }
  1642. template <class Ta, class Tb, class Tc, class Ts>
  1643. inline void
  1644. gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1645. oneapi::mkl::transpose b_trans, int m, int n,
  1646. int k, const void *alpha, const void *a, int lda,
  1647. long long int stride_a, const void *b, int ldb,
  1648. long long int stride_b, const void *beta, void *c,
  1649. int ldc, long long int stride_c, int batch_size)
  1650. {
  1651. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1652. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1653. auto data_a = get_memory<const Ta>(a);
  1654. auto data_b = get_memory<const Tb>(b);
  1655. auto data_c = get_memory<Tc>(c);
  1656. oneapi::mkl::blas::column_major::gemm_batch(
  1657. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1658. stride_a, data_b, ldb, stride_b, beta_value,
  1659. data_c, ldc, stride_c, batch_size);
  1660. }
  1661. } // namespace detail
  1662. template <typename VecT, class BinaryOperation>
  1663. inline unsigned vectorized_binary(unsigned a, unsigned b,
  1664. const BinaryOperation binary_op)
  1665. {
  1666. sycl::vec<unsigned, 1> v0{a}, v1{b};
  1667. auto v2 = v0.as<VecT>();
  1668. auto v3 = v1.as<VecT>();
  1669. auto v4 =
  1670. detail::vectorized_binary<VecT, BinaryOperation>()(v2, v3, binary_op);
  1671. v0 = v4.template as<sycl::vec<unsigned, 1>>();
  1672. return v0;
  1673. }
  1674. static void async_dpct_memcpy(void *to_ptr, const void *from_ptr, size_t size,
  1675. memcpy_direction direction = automatic,
  1676. sycl::queue &q = dpct::get_default_queue())
  1677. {
  1678. detail::dpct_memcpy(q, to_ptr, from_ptr, size, direction);
  1679. }
  1680. static inline unsigned int select_device(unsigned int id)
  1681. {
  1682. dev_mgr::instance().select_device(id);
  1683. return id;
  1684. }
  1685. template <typename T>
  1686. T permute_sub_group_by_xor(sycl::sub_group g, T x, unsigned int mask,
  1687. int logical_sub_group_size = 32)
  1688. {
  1689. unsigned int id = g.get_local_linear_id();
  1690. unsigned int start_index =
  1691. id / logical_sub_group_size * logical_sub_group_size;
  1692. unsigned int target_offset = (id % logical_sub_group_size) ^ mask;
  1693. return sycl::select_from_group(g, x,
  1694. target_offset < logical_sub_group_size
  1695. ? start_index + target_offset
  1696. : id);
  1697. }
  1698. template <typename T>
  1699. sycl::vec<T, 4> extract_and_sign_or_zero_extend4(T val)
  1700. {
  1701. return sycl::vec<T, 1>(val)
  1702. .template as<sycl::vec<
  1703. std::conditional_t<std::is_signed_v<T>, int8_t, uint8_t>, 4>>()
  1704. .template convert<T>();
  1705. }
  1706. template <typename T1, typename T2>
  1707. using dot_product_acc_t =
  1708. std::conditional_t<std::is_unsigned_v<T1> && std::is_unsigned_v<T2>,
  1709. uint32_t, int32_t>;
  1710. template <typename T1, typename T2, typename T3>
  1711. inline auto dp4a(T1 a, T2 b, T3 c)
  1712. {
  1713. dot_product_acc_t<T1, T2> res = c;
  1714. auto va = extract_and_sign_or_zero_extend4(a);
  1715. auto vb = extract_and_sign_or_zero_extend4(b);
  1716. res += va[0] * vb[0];
  1717. res += va[1] * vb[1];
  1718. res += va[2] * vb[2];
  1719. res += va[3] * vb[3];
  1720. return res;
  1721. }
  1722. struct sub_sat
  1723. {
  1724. template <typename T>
  1725. auto operator()(const T x, const T y) const
  1726. {
  1727. return sycl::sub_sat(x, y);
  1728. }
  1729. };
  1730. template <typename S, typename T>
  1731. inline T vectorized_min(T a, T b)
  1732. {
  1733. sycl::vec<T, 1> v0{a}, v1{b};
  1734. auto v2 = v0.template as<S>();
  1735. auto v3 = v1.template as<S>();
  1736. auto v4 = sycl::min(v2, v3);
  1737. v0 = v4.template as<sycl::vec<T, 1>>();
  1738. return v0;
  1739. }
  1740. inline float pow(const float a, const int b) { return sycl::pown(a, b); }
  1741. inline double pow(const double a, const int b) { return sycl::pown(a, b); }
  1742. inline float pow(const float a, const float b) { return sycl::pow(a, b); }
  1743. inline double pow(const double a, const double b) { return sycl::pow(a, b); }
  1744. template <typename T, typename U>
  1745. inline typename std::enable_if_t<std::is_floating_point_v<T>, T>
  1746. pow(const T a, const U b)
  1747. {
  1748. return sycl::pow(a, static_cast<T>(b));
  1749. }
  1750. template <typename T, typename U>
  1751. inline typename std::enable_if_t<!std::is_floating_point_v<T>, double>
  1752. pow(const T a, const U b)
  1753. {
  1754. return sycl::pow(static_cast<double>(a), static_cast<double>(b));
  1755. }
  1756. inline double min(const double a, const float b)
  1757. {
  1758. return sycl::fmin(a, static_cast<double>(b));
  1759. }
  1760. inline double min(const float a, const double b)
  1761. {
  1762. return sycl::fmin(static_cast<double>(a), b);
  1763. }
  1764. inline float min(const float a, const float b) { return sycl::fmin(a, b); }
  1765. inline double min(const double a, const double b) { return sycl::fmin(a, b); }
  1766. inline std::uint32_t min(const std::uint32_t a, const std::int32_t b)
  1767. {
  1768. return sycl::min(a, static_cast<std::uint32_t>(b));
  1769. }
  1770. inline std::uint32_t min(const std::int32_t a, const std::uint32_t b)
  1771. {
  1772. return sycl::min(static_cast<std::uint32_t>(a), b);
  1773. }
  1774. inline std::int32_t min(const std::int32_t a, const std::int32_t b)
  1775. {
  1776. return sycl::min(a, b);
  1777. }
  1778. inline std::uint32_t min(const std::uint32_t a, const std::uint32_t b)
  1779. {
  1780. return sycl::min(a, b);
  1781. }
  1782. inline std::uint64_t min(const std::uint64_t a, const std::int64_t b)
  1783. {
  1784. return sycl::min(a, static_cast<std::uint64_t>(b));
  1785. }
  1786. inline std::uint64_t min(const std::int64_t a, const std::uint64_t b)
  1787. {
  1788. return sycl::min(static_cast<std::uint64_t>(a), b);
  1789. }
  1790. inline std::int64_t min(const std::int64_t a, const std::int64_t b)
  1791. {
  1792. return sycl::min(a, b);
  1793. }
  1794. inline std::uint64_t min(const std::uint64_t a, const std::uint64_t b)
  1795. {
  1796. return sycl::min(a, b);
  1797. }
  1798. inline std::uint64_t min(const std::uint64_t a, const std::int32_t b)
  1799. {
  1800. return sycl::min(a, static_cast<std::uint64_t>(b));
  1801. }
  1802. inline std::uint64_t min(const std::int32_t a, const std::uint64_t b)
  1803. {
  1804. return sycl::min(static_cast<std::uint64_t>(a), b);
  1805. }
  1806. inline std::uint64_t min(const std::uint64_t a, const std::uint32_t b)
  1807. {
  1808. return sycl::min(a, static_cast<std::uint64_t>(b));
  1809. }
  1810. inline std::uint64_t min(const std::uint32_t a, const std::uint64_t b)
  1811. {
  1812. return sycl::min(static_cast<std::uint64_t>(a), b);
  1813. }
  1814. // max function overloads.
  1815. // For floating-point types, `float` or `double` arguments are acceptable.
  1816. // For integer types, `std::uint32_t`, `std::int32_t`, `std::uint64_t` or
  1817. // `std::int64_t` type arguments are acceptable.
  1818. inline double max(const double a, const float b)
  1819. {
  1820. return sycl::fmax(a, static_cast<double>(b));
  1821. }
  1822. inline double max(const float a, const double b)
  1823. {
  1824. return sycl::fmax(static_cast<double>(a), b);
  1825. }
  1826. inline float max(const float a, const float b) { return sycl::fmax(a, b); }
  1827. inline double max(const double a, const double b) { return sycl::fmax(a, b); }
  1828. inline std::uint32_t max(const std::uint32_t a, const std::int32_t b)
  1829. {
  1830. return sycl::max(a, static_cast<std::uint32_t>(b));
  1831. }
  1832. inline std::uint32_t max(const std::int32_t a, const std::uint32_t b)
  1833. {
  1834. return sycl::max(static_cast<std::uint32_t>(a), b);
  1835. }
  1836. inline std::int32_t max(const std::int32_t a, const std::int32_t b)
  1837. {
  1838. return sycl::max(a, b);
  1839. }
  1840. inline std::uint32_t max(const std::uint32_t a, const std::uint32_t b)
  1841. {
  1842. return sycl::max(a, b);
  1843. }
  1844. inline std::uint64_t max(const std::uint64_t a, const std::int64_t b)
  1845. {
  1846. return sycl::max(a, static_cast<std::uint64_t>(b));
  1847. }
  1848. inline std::uint64_t max(const std::int64_t a, const std::uint64_t b)
  1849. {
  1850. return sycl::max(static_cast<std::uint64_t>(a), b);
  1851. }
  1852. inline std::int64_t max(const std::int64_t a, const std::int64_t b)
  1853. {
  1854. return sycl::max(a, b);
  1855. }
  1856. inline std::uint64_t max(const std::uint64_t a, const std::uint64_t b)
  1857. {
  1858. return sycl::max(a, b);
  1859. }
  1860. inline std::uint64_t max(const std::uint64_t a, const std::int32_t b)
  1861. {
  1862. return sycl::max(a, static_cast<std::uint64_t>(b));
  1863. }
  1864. inline std::uint64_t max(const std::int32_t a, const std::uint64_t b)
  1865. {
  1866. return sycl::max(static_cast<std::uint64_t>(a), b);
  1867. }
  1868. inline std::uint64_t max(const std::uint64_t a, const std::uint32_t b)
  1869. {
  1870. return sycl::max(a, static_cast<std::uint64_t>(b));
  1871. }
  1872. inline std::uint64_t max(const std::uint32_t a, const std::uint64_t b)
  1873. {
  1874. return sycl::max(static_cast<std::uint64_t>(a), b);
  1875. }
  1876. inline void
  1877. has_capability_or_fail(const sycl::device &dev,
  1878. const std::initializer_list<sycl::aspect> &props)
  1879. {
  1880. for (const auto &it : props)
  1881. {
  1882. if (dev.has(it))
  1883. continue;
  1884. switch (it)
  1885. {
  1886. case sycl::aspect::fp64:
  1887. throw std::runtime_error("'double' is not supported in '" +
  1888. dev.get_info<sycl::info::device::name>() +
  1889. "' device");
  1890. break;
  1891. case sycl::aspect::fp16:
  1892. throw std::runtime_error("'half' is not supported in '" +
  1893. dev.get_info<sycl::info::device::name>() +
  1894. "' device");
  1895. break;
  1896. default:
  1897. #define __SYCL_ASPECT(ASPECT, ID) \
  1898. case sycl::aspect::ASPECT: \
  1899. return #ASPECT;
  1900. #define __SYCL_ASPECT_DEPRECATED(ASPECT, ID, MESSAGE) __SYCL_ASPECT(ASPECT, ID)
  1901. #define __SYCL_ASPECT_DEPRECATED_ALIAS(ASPECT, ID, MESSAGE)
  1902. auto getAspectNameStr = [](sycl::aspect AspectNum) -> std::string
  1903. {
  1904. switch (AspectNum)
  1905. {
  1906. #include <sycl/info/aspects.def>
  1907. #include <sycl/info/aspects_deprecated.def>
  1908. default:
  1909. return "unknown aspect";
  1910. }
  1911. };
  1912. #undef __SYCL_ASPECT_DEPRECATED_ALIAS
  1913. #undef __SYCL_ASPECT_DEPRECATED
  1914. #undef __SYCL_ASPECT
  1915. throw std::runtime_error(
  1916. "'" + getAspectNameStr(it) + "' is not supported in '" +
  1917. dev.get_info<sycl::info::device::name>() + "' device");
  1918. }
  1919. break;
  1920. }
  1921. }
  1922. static inline unsigned int get_current_device_id()
  1923. {
  1924. return dev_mgr::instance().current_device_id();
  1925. }
  1926. static inline device_ext &get_current_device()
  1927. {
  1928. return dev_mgr::instance().current_device();
  1929. }
  1930. static inline sycl::queue &get_in_order_queue()
  1931. {
  1932. return dev_mgr::instance().current_device().in_order_queue();
  1933. }
  1934. static sycl::event
  1935. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1936. memcpy_direction direction,
  1937. const std::vector<sycl::event> &dep_events = {})
  1938. {
  1939. if (!size)
  1940. return sycl::event{};
  1941. #ifdef DPCT_USM_LEVEL_NONE
  1942. auto &mm = mem_mgr::instance();
  1943. auto real_direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1944. switch (real_direction)
  1945. {
  1946. case host_to_host:
  1947. return q.submit([&](sycl::handler &cgh)
  1948. {
  1949. cgh.depends_on(dep_events);
  1950. cgh.host_task([=] { std::memcpy(to_ptr, from_ptr, size); }); });
  1951. case host_to_device:
  1952. {
  1953. auto alloc = mm.translate_ptr(to_ptr);
  1954. size_t offset = (byte_t *)to_ptr - alloc.alloc_ptr;
  1955. return q.submit([&](sycl::handler &cgh)
  1956. {
  1957. cgh.depends_on(dep_events);
  1958. auto r = sycl::range<1>(size);
  1959. auto o = sycl::id<1>(offset);
  1960. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1961. sycl::access::target::device>
  1962. acc(alloc.buffer, cgh, r, o);
  1963. cgh.copy(from_ptr, acc); });
  1964. }
  1965. case device_to_host:
  1966. {
  1967. auto alloc = mm.translate_ptr(from_ptr);
  1968. size_t offset = (byte_t *)from_ptr - alloc.alloc_ptr;
  1969. return q.submit([&](sycl::handler &cgh)
  1970. {
  1971. cgh.depends_on(dep_events);
  1972. auto r = sycl::range<1>(size);
  1973. auto o = sycl::id<1>(offset);
  1974. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1975. sycl::access::target::device>
  1976. acc(alloc.buffer, cgh, r, o);
  1977. cgh.copy(acc, to_ptr); });
  1978. }
  1979. case device_to_device:
  1980. {
  1981. auto to_alloc = mm.translate_ptr(to_ptr);
  1982. auto from_alloc = mm.translate_ptr(from_ptr);
  1983. size_t to_offset = (byte_t *)to_ptr - to_alloc.alloc_ptr;
  1984. size_t from_offset = (byte_t *)from_ptr - from_alloc.alloc_ptr;
  1985. return q.submit([&](sycl::handler &cgh)
  1986. {
  1987. cgh.depends_on(dep_events);
  1988. auto r = sycl::range<1>(size);
  1989. auto to_o = sycl::id<1>(to_offset);
  1990. auto from_o = sycl::id<1>(from_offset);
  1991. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1992. sycl::access::target::device>
  1993. to_acc(to_alloc.buffer, cgh, r, to_o);
  1994. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1995. sycl::access::target::device>
  1996. from_acc(from_alloc.buffer, cgh, r, from_o);
  1997. cgh.copy(from_acc, to_acc); });
  1998. }
  1999. default:
  2000. throw std::runtime_error("dpct_memcpy: invalid direction value");
  2001. }
  2002. #else
  2003. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  2004. #endif // DPCT_USM_LEVEL_NONE
  2005. }
  2006. // Get actual copy range and make sure it will not exceed range.
  2007. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  2008. size_t pitch)
  2009. {
  2010. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  2011. }
  2012. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  2013. size_t pitch)
  2014. {
  2015. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  2016. }
  2017. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  2018. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  2019. static inline std::vector<sycl::event>
  2020. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  2021. sycl::range<3> to_range, sycl::range<3> from_range,
  2022. sycl::id<3> to_id, sycl::id<3> from_id,
  2023. sycl::range<3> size, memcpy_direction direction,
  2024. const std::vector<sycl::event> &dep_events = {})
  2025. {
  2026. // RAII for host pointer
  2027. class host_buffer
  2028. {
  2029. void *_buf;
  2030. size_t _size;
  2031. sycl::queue &_q;
  2032. const std::vector<sycl::event> &_deps; // free operation depends
  2033. public:
  2034. host_buffer(size_t size, sycl::queue &q,
  2035. const std::vector<sycl::event> &deps)
  2036. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  2037. void *get_ptr() const { return _buf; }
  2038. size_t get_size() const { return _size; }
  2039. ~host_buffer()
  2040. {
  2041. if (_buf)
  2042. {
  2043. _q.submit([&](sycl::handler &cgh)
  2044. {
  2045. cgh.depends_on(_deps);
  2046. cgh.host_task([buf = _buf] { std::free(buf); }); });
  2047. }
  2048. }
  2049. };
  2050. std::vector<sycl::event> event_list;
  2051. size_t to_slice = to_range.get(1) * to_range.get(0),
  2052. from_slice = from_range.get(1) * from_range.get(0);
  2053. unsigned char *to_surface =
  2054. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  2055. const unsigned char *from_surface =
  2056. (const unsigned char *)from_ptr +
  2057. get_offset(from_id, from_slice, from_range.get(0));
  2058. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  2059. {
  2060. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  2061. direction, dep_events)};
  2062. }
  2063. direction = detail::deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  2064. size_t size_slice = size.get(1) * size.get(0);
  2065. switch (direction)
  2066. {
  2067. case host_to_host:
  2068. for (size_t z = 0; z < size.get(2); ++z)
  2069. {
  2070. unsigned char *to_ptr = to_surface;
  2071. const unsigned char *from_ptr = from_surface;
  2072. if (to_range.get(0) == from_range.get(0) &&
  2073. to_range.get(0) == size.get(0))
  2074. {
  2075. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  2076. direction, dep_events));
  2077. }
  2078. else
  2079. {
  2080. for (size_t y = 0; y < size.get(1); ++y)
  2081. {
  2082. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  2083. direction, dep_events));
  2084. to_ptr += to_range.get(0);
  2085. from_ptr += from_range.get(0);
  2086. }
  2087. }
  2088. to_surface += to_slice;
  2089. from_surface += from_slice;
  2090. }
  2091. break;
  2092. case host_to_device:
  2093. {
  2094. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  2095. event_list);
  2096. std::vector<sycl::event> host_events;
  2097. if (to_slice == size_slice)
  2098. {
  2099. // Copy host data to a temp host buffer with the shape of target.
  2100. host_events =
  2101. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  2102. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  2103. host_to_host, dep_events);
  2104. }
  2105. else
  2106. {
  2107. // Copy host data to a temp host buffer with the shape of target.
  2108. host_events = dpct_memcpy(
  2109. q, buf.get_ptr(), from_surface, to_range, from_range,
  2110. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  2111. // If has padding data, not sure whether it is useless. So fill temp
  2112. // buffer with it.
  2113. std::vector<sycl::event>{
  2114. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  2115. device_to_host, dep_events)});
  2116. }
  2117. // Copy from temp host buffer to device with only one submit.
  2118. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  2119. buf.get_size(), host_to_device,
  2120. host_events));
  2121. break;
  2122. }
  2123. case device_to_host:
  2124. {
  2125. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  2126. event_list);
  2127. // Copy from host temp buffer to host target with reshaping.
  2128. event_list = dpct_memcpy(
  2129. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  2130. sycl::id<3>(0, 0, 0), size, host_to_host,
  2131. // Copy from device to temp host buffer with only one submit.
  2132. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  2133. buf.get_size(),
  2134. device_to_host, dep_events)});
  2135. break;
  2136. }
  2137. case device_to_device:
  2138. #ifdef DPCT_USM_LEVEL_NONE
  2139. {
  2140. auto &mm = mem_mgr::instance();
  2141. auto to_alloc = mm.translate_ptr(to_surface);
  2142. auto from_alloc = mm.translate_ptr(from_surface);
  2143. size_t to_offset = (byte_t *)to_surface - to_alloc.alloc_ptr;
  2144. size_t from_offset = (byte_t *)from_surface - from_alloc.alloc_ptr;
  2145. event_list.push_back(q.submit([&](sycl::handler &cgh)
  2146. {
  2147. cgh.depends_on(dep_events);
  2148. auto to_o = sycl::id<1>(to_offset);
  2149. auto from_o = sycl::id<1>(from_offset);
  2150. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  2151. sycl::access::target::device>
  2152. to_acc(to_alloc.buffer, cgh,
  2153. get_copy_range(size, to_slice, to_range.get(0)), to_o);
  2154. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  2155. sycl::access::target::device>
  2156. from_acc(from_alloc.buffer, cgh,
  2157. get_copy_range(size, from_slice, from_range.get(0)), from_o);
  2158. cgh.parallel_for<class dpct_memcpy_3d_detail_usmnone>(
  2159. size,
  2160. [=](sycl::id<3> id) {
  2161. to_acc[get_offset(id, to_slice, to_range.get(0))] =
  2162. from_acc[get_offset(id, from_slice, from_range.get(0))];
  2163. }); }));
  2164. }
  2165. #else
  2166. event_list.push_back(q.submit([&](sycl::handler &cgh)
  2167. {
  2168. cgh.depends_on(dep_events);
  2169. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  2170. size,
  2171. [=](sycl::id<3> id) {
  2172. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  2173. from_surface[get_offset(id, from_slice, from_range.get(0))];
  2174. }); }));
  2175. #endif
  2176. break;
  2177. default:
  2178. throw std::runtime_error("dpct_memcpy: invalid direction value");
  2179. }
  2180. return event_list;
  2181. }
  2182. /// memcpy 2D/3D matrix specified by pitched_data.
  2183. static inline std::vector<sycl::event>
  2184. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  2185. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  2186. memcpy_direction direction = automatic)
  2187. {
  2188. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  2189. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  2190. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  2191. size, direction);
  2192. }
  2193. /// memcpy 2D matrix with pitch.
  2194. static inline std::vector<sycl::event>
  2195. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  2196. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  2197. memcpy_direction direction = automatic)
  2198. {
  2199. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  2200. sycl::range<3>(from_pitch, y, 1),
  2201. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  2202. sycl::range<3>(x, y, 1), direction);
  2203. }
  2204. inline void gemm(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2205. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2206. const void *alpha, const void *a, library_data_t a_type,
  2207. int lda, const void *b, library_data_t b_type, int ldb,
  2208. const void *beta, void *c, library_data_t c_type, int ldc,
  2209. library_data_t scaling_type)
  2210. {
  2211. if (scaling_type == library_data_t::real_float &&
  2212. c_type == library_data_t::complex_float)
  2213. {
  2214. scaling_type = library_data_t::complex_float;
  2215. }
  2216. else if (scaling_type == library_data_t::real_double &&
  2217. c_type == library_data_t::complex_double)
  2218. {
  2219. scaling_type = library_data_t::complex_double;
  2220. }
  2221. std::uint64_t key =
  2222. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2223. switch (key)
  2224. {
  2225. case detail::get_type_combination_id(
  2226. library_data_t::real_float, library_data_t::real_float,
  2227. library_data_t::real_float, library_data_t::real_float):
  2228. {
  2229. detail::gemm_impl<float, float, float, float>(
  2230. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2231. break;
  2232. }
  2233. case detail::get_type_combination_id(
  2234. library_data_t::real_double, library_data_t::real_double,
  2235. library_data_t::real_double, library_data_t::real_double):
  2236. {
  2237. detail::gemm_impl<double, double, double, double>(
  2238. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2239. break;
  2240. }
  2241. case detail::get_type_combination_id(
  2242. library_data_t::complex_float, library_data_t::complex_float,
  2243. library_data_t::complex_float, library_data_t::complex_float):
  2244. {
  2245. detail::gemm_impl<std::complex<float>, std::complex<float>,
  2246. std::complex<float>, std::complex<float>>(
  2247. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2248. break;
  2249. }
  2250. case detail::get_type_combination_id(
  2251. library_data_t::complex_double, library_data_t::complex_double,
  2252. library_data_t::complex_double, library_data_t::complex_double):
  2253. {
  2254. detail::gemm_impl<std::complex<double>, std::complex<double>,
  2255. std::complex<double>, std::complex<double>>(
  2256. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2257. break;
  2258. }
  2259. case detail::get_type_combination_id(
  2260. library_data_t::real_half, library_data_t::real_half,
  2261. library_data_t::real_half, library_data_t::real_half):
  2262. {
  2263. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2264. sycl::half>(q, a_trans, b_trans, m, n, k, alpha, a,
  2265. lda, b, ldb, beta, c, ldc);
  2266. break;
  2267. }
  2268. case detail::get_type_combination_id(
  2269. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2270. library_data_t::real_float, library_data_t::real_float):
  2271. {
  2272. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2273. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda, b,
  2274. ldb, beta, c, ldc);
  2275. break;
  2276. }
  2277. case detail::get_type_combination_id(
  2278. library_data_t::real_half, library_data_t::real_half,
  2279. library_data_t::real_float, library_data_t::real_float):
  2280. {
  2281. detail::gemm_impl<sycl::half, sycl::half, float, float>(
  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::real_half, library_data_t::real_half,
  2287. library_data_t::real_half, library_data_t::real_float):
  2288. {
  2289. float alpha_value =
  2290. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2291. float beta_value =
  2292. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2293. sycl::half alpha_half(alpha_value);
  2294. sycl::half beta_half(beta_value);
  2295. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2296. sycl::half>(q, a_trans, b_trans, m, n, k, &alpha_half,
  2297. a, lda, b, ldb, &beta_half, c, ldc);
  2298. break;
  2299. }
  2300. case detail::get_type_combination_id(
  2301. library_data_t::real_int8, library_data_t::real_int8,
  2302. library_data_t::real_float, library_data_t::real_float):
  2303. {
  2304. detail::gemm_impl<std::int8_t, std::int8_t, float, float>(
  2305. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2306. break;
  2307. }
  2308. case detail::get_type_combination_id(
  2309. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2310. library_data_t::real_bfloat16, library_data_t::real_float):
  2311. {
  2312. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2313. oneapi::mkl::bfloat16, float>(
  2314. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2315. break;
  2316. }
  2317. case detail::get_type_combination_id(
  2318. library_data_t::real_int8, library_data_t::real_int8,
  2319. library_data_t::real_int32, library_data_t::real_int32):
  2320. {
  2321. float alpha_float =
  2322. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2323. float beta_float =
  2324. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2325. detail::gemm_impl<std::int8_t, std::int8_t, std::int32_t, float>(
  2326. q, a_trans, b_trans, m, n, k, &alpha_float, a, lda, b, ldb, &beta_float, c, ldc);
  2327. break;
  2328. }
  2329. default:
  2330. throw std::runtime_error("the combination of data type is unsupported");
  2331. }
  2332. } // gemm()
  2333. /// Computes a batch of matrix-matrix product with general matrices.
  2334. /// \param [in] q The queue where the routine should be executed.
  2335. /// \param [in] a_trans Specifies the operation applied to A.
  2336. /// \param [in] b_trans Specifies the operation applied to B.
  2337. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2338. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2339. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2340. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2341. /// \param [in] a Input matrix A.
  2342. /// \param [in] a_type Data type of the matrix A.
  2343. /// \param [in] lda Leading dimension of A.
  2344. /// \param [in] b Input matrix B.
  2345. /// \param [in] b_type Data type of the matrix B.
  2346. /// \param [in] ldb Leading dimension of B.
  2347. /// \param [in] beta Scaling factor for matrix C.
  2348. /// \param [in, out] c Input/Output matrix C.
  2349. /// \param [in] c_type Data type of the matrix C.
  2350. /// \param [in] ldc Leading dimension of C.
  2351. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2352. /// \param [in] scaling_type Data type of the scaling factors.
  2353. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2354. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2355. const void *alpha, const void *a[],
  2356. library_data_t a_type, int lda, const void *b[],
  2357. library_data_t b_type, int ldb, const void *beta,
  2358. void *c[], library_data_t c_type, int ldc,
  2359. int batch_size, library_data_t scaling_type)
  2360. {
  2361. #ifdef DPCT_USM_LEVEL_NONE
  2362. throw std::runtime_error("this API is unsupported when USM level is none");
  2363. #else
  2364. if (scaling_type == library_data_t::real_float &&
  2365. c_type == library_data_t::complex_float)
  2366. {
  2367. scaling_type = library_data_t::complex_float;
  2368. }
  2369. else if (scaling_type == library_data_t::real_double &&
  2370. c_type == library_data_t::complex_double)
  2371. {
  2372. scaling_type = library_data_t::complex_double;
  2373. }
  2374. std::uint64_t key =
  2375. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2376. switch (key)
  2377. {
  2378. case detail::get_type_combination_id(
  2379. library_data_t::real_float, library_data_t::real_float,
  2380. library_data_t::real_float, library_data_t::real_float):
  2381. {
  2382. detail::gemm_batch_impl<float, float, float, float>(
  2383. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2384. batch_size);
  2385. break;
  2386. }
  2387. case detail::get_type_combination_id(
  2388. library_data_t::real_double, library_data_t::real_double,
  2389. library_data_t::real_double, library_data_t::real_double):
  2390. {
  2391. detail::gemm_batch_impl<double, double, double, double>(
  2392. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2393. batch_size);
  2394. break;
  2395. }
  2396. case detail::get_type_combination_id(
  2397. library_data_t::complex_float, library_data_t::complex_float,
  2398. library_data_t::complex_float, library_data_t::complex_float):
  2399. {
  2400. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2401. std::complex<float>, std::complex<float>>(
  2402. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2403. batch_size);
  2404. break;
  2405. }
  2406. case detail::get_type_combination_id(
  2407. library_data_t::complex_double, library_data_t::complex_double,
  2408. library_data_t::complex_double, library_data_t::complex_double):
  2409. {
  2410. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2411. std::complex<double>, std::complex<double>>(
  2412. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2413. batch_size);
  2414. break;
  2415. }
  2416. case detail::get_type_combination_id(
  2417. library_data_t::real_half, library_data_t::real_half,
  2418. library_data_t::real_half, library_data_t::real_half):
  2419. {
  2420. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2421. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2422. a, lda, b, ldb, beta, c, ldc,
  2423. batch_size);
  2424. break;
  2425. }
  2426. #ifdef __INTEL_MKL__
  2427. case detail::get_type_combination_id(
  2428. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2429. library_data_t::real_bfloat16, library_data_t::real_float):
  2430. {
  2431. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2432. oneapi::mkl::bfloat16, float>(
  2433. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2434. batch_size);
  2435. break;
  2436. }
  2437. case detail::get_type_combination_id(
  2438. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2439. library_data_t::real_float, library_data_t::real_float):
  2440. {
  2441. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2442. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2443. b, ldb, beta, c, ldc, batch_size);
  2444. break;
  2445. }
  2446. case detail::get_type_combination_id(
  2447. library_data_t::real_int8, library_data_t::real_int8,
  2448. library_data_t::real_int32, library_data_t::real_int32):
  2449. {
  2450. float alpha_float =
  2451. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2452. float beta_float =
  2453. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2454. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2455. float>(q, a_trans, b_trans, m, n, k, &alpha_float,
  2456. a, lda, b, ldb, &beta_float, c, ldc,
  2457. batch_size);
  2458. break;
  2459. }
  2460. case detail::get_type_combination_id(
  2461. library_data_t::real_int8, library_data_t::real_int8,
  2462. library_data_t::real_float, library_data_t::real_float):
  2463. {
  2464. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2465. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2466. batch_size);
  2467. break;
  2468. }
  2469. case detail::get_type_combination_id(
  2470. library_data_t::real_half, library_data_t::real_half,
  2471. library_data_t::real_float, library_data_t::real_float):
  2472. {
  2473. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2474. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2475. batch_size);
  2476. break;
  2477. }
  2478. #endif
  2479. case detail::get_type_combination_id(
  2480. library_data_t::real_half, library_data_t::real_half,
  2481. library_data_t::real_half, library_data_t::real_float):
  2482. {
  2483. float alpha_value =
  2484. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2485. float beta_value =
  2486. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2487. sycl::half alpha_half(alpha_value);
  2488. sycl::half beta_half(beta_value);
  2489. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2490. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, b, ldb, &beta_half, c, ldc,
  2491. batch_size);
  2492. break;
  2493. }
  2494. default:
  2495. throw std::runtime_error("the combination of data type is unsupported");
  2496. }
  2497. #endif
  2498. }
  2499. /// Computes a batch of matrix-matrix product with general matrices.
  2500. /// \param [in] q The queue where the routine should be executed.
  2501. /// \param [in] a_trans Specifies the operation applied to A.
  2502. /// \param [in] b_trans Specifies the operation applied to B.
  2503. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2504. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2505. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2506. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2507. /// \param [in] a Input matrix A.
  2508. /// \param [in] a_type Data type of the matrix A.
  2509. /// \param [in] lda Leading dimension of A.
  2510. /// \param [in] stride_a Stride between the different A matrices.
  2511. /// \param [in] b Input matrix B.
  2512. /// \param [in] b_type Data type of the matrix B.
  2513. /// \param [in] ldb Leading dimension of B.
  2514. /// \param [in] stride_b Stride between the different B matrices.
  2515. /// \param [in] beta Scaling factor for matrix C.
  2516. /// \param [in, out] c Input/Output matrix C.
  2517. /// \param [in] c_type Data type of the matrix C.
  2518. /// \param [in] ldc Leading dimension of C.
  2519. /// \param [in] stride_c Stride between the different C matrices.
  2520. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2521. /// \param [in] scaling_type Data type of the scaling factors.
  2522. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2523. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2524. const void *alpha, const void *a, library_data_t a_type,
  2525. int lda, long long int stride_a, const void *b,
  2526. library_data_t b_type, int ldb, long long int stride_b,
  2527. const void *beta, void *c, library_data_t c_type,
  2528. int ldc, long long int stride_c, int batch_size,
  2529. library_data_t scaling_type)
  2530. {
  2531. if (scaling_type == library_data_t::real_float &&
  2532. c_type == library_data_t::complex_float)
  2533. {
  2534. scaling_type = library_data_t::complex_float;
  2535. }
  2536. else if (scaling_type == library_data_t::real_double &&
  2537. c_type == library_data_t::complex_double)
  2538. {
  2539. scaling_type = library_data_t::complex_double;
  2540. }
  2541. std::uint64_t key =
  2542. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2543. switch (key)
  2544. {
  2545. case detail::get_type_combination_id(
  2546. library_data_t::real_float, library_data_t::real_float,
  2547. library_data_t::real_float, library_data_t::real_float):
  2548. {
  2549. detail::gemm_batch_impl<float, float, float, float>(
  2550. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2551. beta, c, ldc, stride_c, batch_size);
  2552. break;
  2553. }
  2554. case detail::get_type_combination_id(
  2555. library_data_t::real_double, library_data_t::real_double,
  2556. library_data_t::real_double, library_data_t::real_double):
  2557. {
  2558. detail::gemm_batch_impl<double, double, double, double>(
  2559. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2560. beta, c, ldc, stride_c, batch_size);
  2561. break;
  2562. }
  2563. case detail::get_type_combination_id(
  2564. library_data_t::complex_float, library_data_t::complex_float,
  2565. library_data_t::complex_float, library_data_t::complex_float):
  2566. {
  2567. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2568. std::complex<float>, std::complex<float>>(
  2569. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2570. beta, c, ldc, stride_c, batch_size);
  2571. break;
  2572. }
  2573. case detail::get_type_combination_id(
  2574. library_data_t::complex_double, library_data_t::complex_double,
  2575. library_data_t::complex_double, library_data_t::complex_double):
  2576. {
  2577. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2578. std::complex<double>, std::complex<double>>(
  2579. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2580. beta, c, ldc, stride_c, batch_size);
  2581. break;
  2582. }
  2583. case detail::get_type_combination_id(
  2584. library_data_t::real_half, library_data_t::real_half,
  2585. library_data_t::real_half, library_data_t::real_half):
  2586. {
  2587. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2588. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2589. a, lda, stride_a, b, ldb, stride_b,
  2590. beta, c, ldc, stride_c, batch_size);
  2591. break;
  2592. }
  2593. #ifdef __INTEL_MKL__
  2594. case detail::get_type_combination_id(
  2595. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2596. library_data_t::real_bfloat16, library_data_t::real_float):
  2597. {
  2598. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2599. oneapi::mkl::bfloat16, float>(
  2600. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2601. beta, c, ldc, stride_c, batch_size);
  2602. break;
  2603. }
  2604. case detail::get_type_combination_id(
  2605. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2606. library_data_t::real_float, library_data_t::real_float):
  2607. {
  2608. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2609. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2610. stride_a, b, ldb, stride_b, beta, c, ldc,
  2611. stride_c, batch_size);
  2612. break;
  2613. }
  2614. case detail::get_type_combination_id(
  2615. library_data_t::real_int8, library_data_t::real_int8,
  2616. library_data_t::real_int32, library_data_t::real_int32):
  2617. {
  2618. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2619. std::int32_t>(q, a_trans, b_trans, m, n, k, alpha,
  2620. a, lda, stride_a, b, ldb, stride_b,
  2621. beta, c, ldc, stride_c, batch_size);
  2622. break;
  2623. }
  2624. case detail::get_type_combination_id(
  2625. library_data_t::real_int8, library_data_t::real_int8,
  2626. library_data_t::real_float, library_data_t::real_float):
  2627. {
  2628. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2629. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2630. beta, c, ldc, stride_c, batch_size);
  2631. break;
  2632. }
  2633. case detail::get_type_combination_id(
  2634. library_data_t::real_half, library_data_t::real_half,
  2635. library_data_t::real_float, library_data_t::real_float):
  2636. {
  2637. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2638. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2639. beta, c, ldc, stride_c, batch_size);
  2640. break;
  2641. }
  2642. #endif
  2643. case detail::get_type_combination_id(
  2644. library_data_t::real_half, library_data_t::real_half,
  2645. library_data_t::real_half, library_data_t::real_float):
  2646. {
  2647. float alpha_value =
  2648. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2649. float beta_value =
  2650. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2651. sycl::half alpha_half(alpha_value);
  2652. sycl::half beta_half(beta_value);
  2653. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2654. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, stride_a, b, ldb, stride_b,
  2655. &beta_half, c, ldc, stride_c, batch_size);
  2656. break;
  2657. }
  2658. default:
  2659. throw std::runtime_error("the combination of data type is unsupported");
  2660. }
  2661. }
  2662. static inline void
  2663. async_dpct_memcpy(void *to_ptr, size_t to_pitch, const void *from_ptr,
  2664. size_t from_pitch, size_t x, size_t y,
  2665. memcpy_direction direction = automatic,
  2666. sycl::queue &q = get_default_queue())
  2667. {
  2668. detail::dpct_memcpy(q, to_ptr, from_ptr, to_pitch, from_pitch, x, y,
  2669. direction);
  2670. }
  2671. using err0 = detail::generic_error_type<struct err0_tag, int>;
  2672. using err1 = detail::generic_error_type<struct err1_tag, int>;
  2673. } // COPY from DPCT head files
  2674. static int g_ggml_sycl_debug=0;
  2675. #define GGML_SYCL_DEBUG(...) do{if(g_ggml_sycl_debug) printf(__VA_ARGS__);}while(0)
  2676. #define CHECK_TRY_ERROR(expr) \
  2677. [&]() { \
  2678. try { \
  2679. expr; \
  2680. return dpct::success; \
  2681. } catch (std::exception const &e) { \
  2682. std::cerr << e.what()<< "\nException caught at file:" << __FILE__ \
  2683. << ", line:" << __LINE__ <<", func:"<<__func__<< std::endl; \
  2684. return dpct::default_error; \
  2685. } \
  2686. }()
  2687. // #define DEBUG_SYCL_MALLOC
  2688. static int g_work_group_size = 0;
  2689. // typedef sycl::half ggml_fp16_t;
  2690. #define __SYCL_ARCH__ DPCT_COMPATIBILITY_TEMP
  2691. #define VER_4VEC 610 //todo for hardward optimize.
  2692. #define VER_GEN9 700 //todo for hardward optimize.
  2693. #define VER_GEN12 1000000 //todo for hardward optimize.
  2694. #define VER_GEN13 (VER_GEN12 + 1030) //todo for hardward optimize.
  2695. #define GGML_SYCL_MAX_NODES 8192 //TODO: adapt to hardwares
  2696. //define for XMX in Intel GPU
  2697. //TODO: currently, it's not used for XMX really.
  2698. #define SYCL_USE_XMX
  2699. // max batch size to use MMQ kernels when tensor cores are available
  2700. #define XMX_MAX_BATCH_SIZE 32
  2701. #if defined(_MSC_VER)
  2702. #pragma warning(disable: 4244 4267) // possible loss of data
  2703. #endif
  2704. static_assert(sizeof(sycl::half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  2705. static void crash(){
  2706. int *ptr = NULL;
  2707. *ptr = 0;
  2708. }
  2709. static void ggml_sycl_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  2710. fprintf(stderr, "SYCL error: %s: %s\n", stmt, msg);
  2711. fprintf(stderr, " in function %s at %s:%d\n", func, file, line);
  2712. GGML_ASSERT(!"SYCL error");
  2713. }
  2714. #define SYCL_CHECK(err) do { \
  2715. auto err_ = (err); if (err_ != 0) ggml_sycl_error( \
  2716. #err, __func__, __FILE__, __LINE__, \
  2717. "Meet error in this line code!"); \
  2718. } while (0)
  2719. #if DPCT_COMPAT_RT_VERSION >= 11100
  2720. #define GGML_SYCL_ASSUME(x) __builtin_assume(x)
  2721. #else
  2722. #define GGML_SYCL_ASSUME(x)
  2723. #endif // DPCT_COMPAT_RT_VERSION >= 11100
  2724. #ifdef GGML_SYCL_F16
  2725. typedef sycl::half dfloat; // dequantize float
  2726. typedef sycl::half2 dfloat2;
  2727. #else
  2728. typedef float dfloat; // dequantize float
  2729. typedef sycl::float2 dfloat2;
  2730. #endif //GGML_SYCL_F16
  2731. bool ggml_sycl_loaded(void);
  2732. void * ggml_sycl_host_malloc(size_t size);
  2733. void ggml_sycl_host_free(void * ptr);
  2734. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
  2735. void ggml_sycl_set_tensor_split(const float * tensor_split);
  2736. void ggml_sycl_transform_tensor(void * data, struct ggml_tensor * tensor);
  2737. void ggml_sycl_free_data(struct ggml_tensor * tensor);
  2738. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor);
  2739. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor);
  2740. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor);
  2741. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor);
  2742. void ggml_sycl_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset);
  2743. void ggml_sycl_copy_to_device(struct ggml_tensor * tensor);
  2744. void ggml_sycl_set_main_device(int main_device);
  2745. void ggml_sycl_set_mul_mat_q(bool mul_mat_q);
  2746. void ggml_sycl_set_scratch_size(size_t scratch_size);
  2747. void ggml_sycl_free_scratch(void);
  2748. void ggml_sycl_get_device_description(int device, char * description, size_t description_size);
  2749. bool ggml_backend_is_sycl(ggml_backend_t backend);
  2750. int ggml_backend_sycl_get_device(ggml_backend_t backend);
  2751. int get_main_device();
  2752. void print_ggml_tensor(const char*name, struct ggml_tensor *src);
  2753. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt);
  2754. static __dpct_inline__ int get_int_from_int8(const int8_t *x8, const int &i32) {
  2755. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2756. int x32 = 0;
  2757. x32 |= x16[0] << 0;
  2758. x32 |= x16[1] << 16;
  2759. return x32;
  2760. }
  2761. static __dpct_inline__ int get_int_from_uint8(const uint8_t *x8,
  2762. const int &i32) {
  2763. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2764. int x32 = 0;
  2765. x32 |= x16[0] << 0;
  2766. x32 |= x16[1] << 16;
  2767. return x32;
  2768. }
  2769. static __dpct_inline__ int get_int_from_int8_aligned(const int8_t *x8,
  2770. const int &i32) {
  2771. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2772. }
  2773. static __dpct_inline__ int get_int_from_uint8_aligned(const uint8_t *x8,
  2774. const int &i32) {
  2775. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2776. }
  2777. template <typename T>
  2778. using to_t_sycl_t = void (*)(const void *__restrict__ x, T *__restrict__ y,
  2779. int k, dpct::queue_ptr stream);
  2780. typedef to_t_sycl_t<float> to_fp32_sycl_t;
  2781. typedef to_t_sycl_t<sycl::half> to_fp16_sycl_t;
  2782. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  2783. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  2784. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  2785. typedef void (*ggml_sycl_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  2786. typedef void (*ggml_sycl_op_mul_mat_t)(
  2787. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  2788. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  2789. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  2790. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  2791. const dpct::queue_ptr &stream);
  2792. typedef void (*ggml_sycl_op_flatten_t)(const ggml_tensor *src0,
  2793. const ggml_tensor *src1,
  2794. ggml_tensor *dst, const float *src0_dd,
  2795. const float *src1_dd, float *dst_dd,
  2796. const dpct::queue_ptr &main_stream);
  2797. // QK = number of values after dequantization
  2798. // QR = QK / number of values before dequantization
  2799. // QI = number of 32 bit integers before dequantization
  2800. #define QK4_0 32
  2801. #define QR4_0 2
  2802. #define QI4_0 (QK4_0 / (4 * QR4_0))
  2803. typedef struct dpct_type_471834 {
  2804. sycl::half d; // delta
  2805. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  2806. } block_q4_0;
  2807. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  2808. #define QK4_1 32
  2809. #define QR4_1 2
  2810. #define QI4_1 (QK4_1 / (4 * QR4_1))
  2811. typedef struct dpct_type_143705 {
  2812. sycl::half2 dm; // dm.x = delta, dm.y = min
  2813. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  2814. } block_q4_1;
  2815. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  2816. #define QK5_0 32
  2817. #define QR5_0 2
  2818. #define QI5_0 (QK5_0 / (4 * QR5_0))
  2819. typedef struct dpct_type_673649 {
  2820. sycl::half d; // delta
  2821. uint8_t qh[4]; // 5-th bit of quants
  2822. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  2823. } block_q5_0;
  2824. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  2825. #define QK5_1 32
  2826. #define QR5_1 2
  2827. #define QI5_1 (QK5_1 / (4 * QR5_1))
  2828. typedef struct dpct_type_135589 {
  2829. sycl::half2 dm; // dm.x = delta, dm.y = min
  2830. uint8_t qh[4]; // 5-th bit of quants
  2831. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  2832. } block_q5_1;
  2833. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  2834. #define QK8_0 32
  2835. #define QR8_0 1
  2836. #define QI8_0 (QK8_0 / (4 * QR8_0))
  2837. typedef struct dpct_type_122878 {
  2838. sycl::half d; // delta
  2839. int8_t qs[QK8_0]; // quants
  2840. } block_q8_0;
  2841. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  2842. #define QK8_1 32
  2843. #define QR8_1 1
  2844. #define QI8_1 (QK8_1 / (4 * QR8_1))
  2845. typedef struct dpct_type_143721 {
  2846. sycl::half2 ds; // ds.x = delta, ds.y = sum
  2847. int8_t qs[QK8_0]; // quants
  2848. } block_q8_1;
  2849. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  2850. typedef float (*vec_dot_q_sycl_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  2851. typedef void (*allocate_tiles_sycl_t)(int **x_ql, sycl::half2 **x_dm,
  2852. int **x_qh, int **x_sc);
  2853. typedef void (*load_tiles_sycl_t)(const void *__restrict__ vx,
  2854. int *__restrict__ x_ql,
  2855. sycl::half2 *__restrict__ x_dm,
  2856. int *__restrict__ x_qh,
  2857. int *__restrict__ x_sc, const int &i_offset,
  2858. const int &i_max, const int &k,
  2859. const int &blocks_per_row);
  2860. typedef float (*vec_dot_q_mul_mat_sycl_t)(
  2861. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  2862. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  2863. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ms,
  2864. const int &i, const int &j, const int &k);
  2865. //================================= k-quants
  2866. #ifdef GGML_QKK_64
  2867. #define QK_K 64
  2868. #define K_SCALE_SIZE 4
  2869. #else
  2870. #define QK_K 256
  2871. #define K_SCALE_SIZE 12
  2872. #endif
  2873. #define QR2_K 4
  2874. #define QI2_K (QK_K / (4*QR2_K))
  2875. typedef struct dpct_type_619598 {
  2876. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  2877. uint8_t qs[QK_K/4]; // quants
  2878. sycl::half2 dm; // super-block scale for quantized scales/mins
  2879. } block_q2_K;
  2880. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  2881. #define QR3_K 4
  2882. #define QI3_K (QK_K / (4*QR3_K))
  2883. typedef struct dpct_type_138576 {
  2884. uint8_t hmask[QK_K/8]; // quants - high bit
  2885. uint8_t qs[QK_K/4]; // quants - low 2 bits
  2886. #ifdef GGML_QKK_64
  2887. uint8_t scales[2]; // scales, quantized with 8 bits
  2888. #else
  2889. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  2890. #endif
  2891. sycl::half d; // super-block scale
  2892. } block_q3_K;
  2893. //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");
  2894. #define QR4_K 2
  2895. #define QI4_K (QK_K / (4*QR4_K))
  2896. #ifdef GGML_QKK_64
  2897. typedef struct {
  2898. half dm[2]; // super-block scales/mins
  2899. uint8_t scales[2]; // 4-bit block scales/mins
  2900. uint8_t qs[QK_K/2]; // 4--bit quants
  2901. } block_q4_K;
  2902. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  2903. #else
  2904. typedef struct dpct_type_154943 {
  2905. sycl::half2 dm; // super-block scale for quantized scales/mins
  2906. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  2907. uint8_t qs[QK_K/2]; // 4--bit quants
  2908. } block_q4_K;
  2909. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  2910. #endif
  2911. #define QR5_K 2
  2912. #define QI5_K (QK_K / (4*QR5_K))
  2913. #ifdef GGML_QKK_64
  2914. typedef struct {
  2915. half d; // super-block scale
  2916. int8_t scales[QK_K/16]; // block scales
  2917. uint8_t qh[QK_K/8]; // quants, high bit
  2918. uint8_t qs[QK_K/2]; // quants, low 4 bits
  2919. } block_q5_K;
  2920. 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");
  2921. #else
  2922. typedef struct dpct_type_866817 {
  2923. sycl::half2 dm; // super-block scale for quantized scales/mins
  2924. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  2925. uint8_t qh[QK_K/8]; // quants, high bit
  2926. uint8_t qs[QK_K/2]; // quants, low 4 bits
  2927. } block_q5_K;
  2928. 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");
  2929. #endif
  2930. #define QR6_K 2
  2931. #define QI6_K (QK_K / (4*QR6_K))
  2932. typedef struct dpct_type_107281 {
  2933. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  2934. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  2935. int8_t scales[QK_K/16]; // scales
  2936. sycl::half d; // delta
  2937. } block_q6_K;
  2938. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  2939. #define WARP_SIZE 32
  2940. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  2941. #define SYCL_GELU_BLOCK_SIZE 256
  2942. #define SYCL_SILU_BLOCK_SIZE 256
  2943. #define SYCL_TANH_BLOCK_SIZE 256
  2944. #define SYCL_RELU_BLOCK_SIZE 256
  2945. #define SYCL_SQR_BLOCK_SIZE 256
  2946. #define SYCL_CPY_BLOCK_SIZE 32
  2947. #define SYCL_SCALE_BLOCK_SIZE 256
  2948. #define SYCL_CLAMP_BLOCK_SIZE 256
  2949. #define SYCL_ROPE_BLOCK_SIZE 256
  2950. #define SYCL_SOFT_MAX_BLOCK_SIZE 1024
  2951. #define SYCL_ALIBI_BLOCK_SIZE 32
  2952. #define SYCL_DIAG_MASK_INF_BLOCK_SIZE 32
  2953. #define SYCL_QUANTIZE_BLOCK_SIZE 256
  2954. #define SYCL_DEQUANTIZE_BLOCK_SIZE 256
  2955. #define SYCL_GET_ROWS_BLOCK_SIZE 256
  2956. #define SYCL_UPSCALE_BLOCK_SIZE 256
  2957. #define SYCL_CONCAT_BLOCK_SIZE 256
  2958. #define SYCL_PAD_BLOCK_SIZE 256
  2959. #define SYCL_ACC_BLOCK_SIZE 256
  2960. #define SYCL_IM2COL_BLOCK_SIZE 256
  2961. // dmmv = dequantize_mul_mat_vec
  2962. #ifndef GGML_SYCL_DMMV_X
  2963. #define GGML_SYCL_DMMV_X 32
  2964. #endif
  2965. #ifndef GGML_SYCL_MMV_Y
  2966. #define GGML_SYCL_MMV_Y 1
  2967. #endif
  2968. #ifndef K_QUANTS_PER_ITERATION
  2969. #define K_QUANTS_PER_ITERATION 2
  2970. #else
  2971. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  2972. #endif
  2973. #ifndef GGML_SYCL_PEER_MAX_BATCH_SIZE
  2974. #define GGML_SYCL_PEER_MAX_BATCH_SIZE 128
  2975. #endif // GGML_SYCL_PEER_MAX_BATCH_SIZE
  2976. #define MUL_MAT_SRC1_COL_STRIDE 128
  2977. #define MAX_STREAMS 8
  2978. static dpct::queue_ptr g_syclStreams[GGML_SYCL_MAX_DEVICES][MAX_STREAMS] = {
  2979. {0}};
  2980. struct ggml_tensor_extra_gpu {
  2981. void * data_device[GGML_SYCL_MAX_DEVICES]; // 1 pointer for each device for split tensors
  2982. dpct::event_ptr
  2983. events[GGML_SYCL_MAX_DEVICES]
  2984. [MAX_STREAMS]; // events for synchronizing multiple GPUs
  2985. };
  2986. inline dpct::err0 ggml_sycl_set_device(const int device) try {
  2987. int current_device;
  2988. SYCL_CHECK(CHECK_TRY_ERROR(
  2989. current_device = dpct::dev_mgr::instance().current_device_id()));
  2990. // GGML_SYCL_DEBUG("ggml_sycl_set_device device=%d, current_device=%d\n", device, current_device);
  2991. if (device == current_device) {
  2992. return 0;
  2993. }
  2994. return CHECK_TRY_ERROR(dpct::select_device(device));
  2995. }
  2996. catch (sycl::exception const &exc) {
  2997. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  2998. << ", line:" << __LINE__ << std::endl;
  2999. crash();
  3000. std::exit(1);
  3001. }
  3002. static int g_device_count = -1;
  3003. static int g_all_sycl_device_count = -1;
  3004. static int g_main_device = -1;
  3005. static int g_main_device_index = -1;
  3006. static float g_tensor_split[GGML_SYCL_MAX_DEVICES] = {0};
  3007. struct sycl_device_capabilities {
  3008. int cc; // compute capability
  3009. bool vmm; // virtual memory support
  3010. size_t vmm_granularity; // granularity of virtual memory
  3011. int device_id;
  3012. };
  3013. static sycl_device_capabilities g_device_caps[GGML_SYCL_MAX_DEVICES] = { {0, false, 0, -1} };
  3014. struct sycl_device_id2index {
  3015. int index;
  3016. };
  3017. static sycl_device_id2index g_sycl_device_id2index[GGML_SYCL_MAX_DEVICES] = { {-1} };
  3018. static void * g_scratch_buffer = nullptr;
  3019. static size_t g_scratch_size = 0; // disabled by default
  3020. static size_t g_scratch_offset = 0;
  3021. static dpct::queue_ptr g_sycl_handles[GGML_SYCL_MAX_DEVICES] = {nullptr};
  3022. int get_main_device(){
  3023. return g_main_device;
  3024. }
  3025. [[noreturn]]
  3026. static void bad_arch(const sycl::stream &stream_ct1) {
  3027. stream_ct1 << "ERROR: ggml-sycl was compiled without support for the "
  3028. "current GPU architecture.\n";
  3029. // __trap();
  3030. std::exit(1);
  3031. (void) bad_arch; // suppress unused function warning
  3032. }
  3033. void log_ggml_var_device(const char*name, float *src, size_t total_elements, bool src_on_device){
  3034. if(!g_ggml_sycl_debug) return;
  3035. if(!src){
  3036. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3037. return;
  3038. }
  3039. char filename[1024];
  3040. sprintf(filename, "%s.txt", name);
  3041. printf("GGML Tensor:%s save to %s\n", name, filename);
  3042. size_t total_size = total_elements*sizeof(float);
  3043. float *local_buf = NULL;
  3044. // printf("total_size %d2, src_on_device %d\n", total_size, src_on_device);
  3045. if(src_on_device) {
  3046. local_buf = (float *) ggml_sycl_host_malloc(total_size);
  3047. // printf("local buf %p size %d bytes\n", local_buf, total_size);
  3048. ggml_sycl_set_device(g_main_device);
  3049. dpct::queue_ptr main_stream = g_syclStreams[g_main_device_index][0];
  3050. main_stream->memcpy(local_buf, src, total_size);
  3051. }
  3052. else {
  3053. local_buf = (float *)src;
  3054. // printf("local buf from src-> data %p\n", local_buf);
  3055. }
  3056. std::ofstream logfile;
  3057. logfile.open(filename);
  3058. // printf("local buf element %d\n", total_elements);
  3059. for(int i=0; i<total_elements; i++){
  3060. if((i+1)%20 ==0) logfile <<std::endl;
  3061. else logfile << local_buf[i] <<" ";
  3062. }
  3063. logfile <<std::endl;
  3064. logfile.close();
  3065. if(src_on_device) ggml_sycl_host_free(local_buf);
  3066. }
  3067. //todo: debug for crash in some case
  3068. void print_ggml_tensor(const char*name, struct ggml_tensor *src){
  3069. if(!g_ggml_sycl_debug) return;
  3070. if(!src){
  3071. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3072. return;
  3073. }
  3074. size_t total_elements = ggml_nelements(src);
  3075. const bool src_on_device = src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT;
  3076. float *src_data =NULL;
  3077. if(src_on_device) {
  3078. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  3079. src_data = (float*)src_extra->data_device[g_main_device_index];
  3080. }
  3081. else {
  3082. src_data = (float *)src->data;
  3083. }
  3084. log_ggml_var_device(name, src_data, total_elements, src_on_device);
  3085. }
  3086. static int log_file_name_idx=0;
  3087. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt) {
  3088. stop_cnt = 4;
  3089. if(log_file_name_idx>=stop_cnt) return;
  3090. char filename[1280];
  3091. sprintf(filename, "%s_%07d", name, log_file_name_idx);
  3092. log_file_name_idx++;
  3093. print_ggml_tensor(filename, src);
  3094. // print_ggml_tensor("ggml_sycl_rms_norm_src0", (ggml_tensor *)src0);
  3095. // print_ggml_tensor("ggml_sycl_rms_norm_src1", (ggml_tensor *)src1);
  3096. // int *ptr = NULL;
  3097. // *ptr = 0;
  3098. }
  3099. static __dpct_inline__ float warp_reduce_sum(float x,
  3100. const sycl::nd_item<3> &item_ct1) {
  3101. #pragma unroll
  3102. for (int mask = 16; mask > 0; mask >>= 1) {
  3103. /*
  3104. DPCT1096:98: The right-most dimension of the work-group used in the SYCL
  3105. kernel that calls this function may be less than "32". The function
  3106. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3107. CPU device. Modify the size of the work-group to ensure that the value
  3108. of the right-most dimension is a multiple of "32".
  3109. */
  3110. x += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), x, mask);
  3111. }
  3112. return x;
  3113. }
  3114. static __dpct_inline__ sycl::float2
  3115. warp_reduce_sum(sycl::float2 a, const sycl::nd_item<3> &item_ct1) {
  3116. #pragma unroll
  3117. for (int mask = 16; mask > 0; mask >>= 1) {
  3118. a.x() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.x(),
  3119. mask);
  3120. a.y() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.y(),
  3121. mask);
  3122. }
  3123. return a;
  3124. }
  3125. static __dpct_inline__ float warp_reduce_max(float x,
  3126. const sycl::nd_item<3> &item_ct1) {
  3127. #pragma unroll
  3128. for (int mask = 16; mask > 0; mask >>= 1) {
  3129. /*
  3130. DPCT1096:97: The right-most dimension of the work-group used in the SYCL
  3131. kernel that calls this function may be less than "32". The function
  3132. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3133. CPU device. Modify the size of the work-group to ensure that the value
  3134. of the right-most dimension is a multiple of "32".
  3135. */
  3136. x = sycl::fmax(x, dpct::permute_sub_group_by_xor(
  3137. item_ct1.get_sub_group(), x, mask));
  3138. }
  3139. return x;
  3140. }
  3141. static __dpct_inline__ float op_repeat(const float a, const float b) {
  3142. return b;
  3143. }
  3144. static __dpct_inline__ float op_add(const float a, const float b) {
  3145. return a + b;
  3146. }
  3147. static __dpct_inline__ float op_mul(const float a, const float b) {
  3148. return a * b;
  3149. }
  3150. static __dpct_inline__ float op_div(const float a, const float b) {
  3151. return a / b;
  3152. }
  3153. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3154. static void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3155. int ne0, int ne1, int ne2, int ne3,
  3156. int ne10, int ne11, int ne12, int ne13,
  3157. /*int s0, */ int s1, int s2, int s3,
  3158. /*int s10,*/ int s11, int s12, int s13,
  3159. const sycl::nd_item<3> &item_ct1) {
  3160. const int i0s = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3161. item_ct1.get_local_id(2);
  3162. const int i1 = (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  3163. item_ct1.get_local_id(1));
  3164. const int i2 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3165. item_ct1.get_local_id(0)) /
  3166. ne3;
  3167. const int i3 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3168. item_ct1.get_local_id(0)) %
  3169. ne3;
  3170. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3171. return;
  3172. }
  3173. const int i11 = i1 % ne11;
  3174. const int i12 = i2 % ne12;
  3175. const int i13 = i3 % ne13;
  3176. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3177. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3178. const size_t i_dst = i_src0;
  3179. const src0_t * src0_row = src0 + i_src0;
  3180. const src1_t * src1_row = src1 + i_src1;
  3181. dst_t * dst_row = dst + i_dst;
  3182. for (int i0 = i0s; i0 < ne0;
  3183. i0 += item_ct1.get_local_range(2) * item_ct1.get_group_range(2)) {
  3184. const int i10 = i0 % ne10;
  3185. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3186. }
  3187. }
  3188. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3189. static void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3190. int ne0, int ne1, int ne2, int ne3,
  3191. int ne10, int ne11, int ne12, int ne13,
  3192. /*int s0, */ int s1, int s2, int s3,
  3193. /*int s10,*/ int s11, int s12, int s13,
  3194. const sycl::nd_item<3> &item_ct1) {
  3195. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3196. item_ct1.get_local_id(2);
  3197. const int i3 = i/(ne2*ne1*ne0);
  3198. const int i2 = (i/(ne1*ne0)) % ne2;
  3199. const int i1 = (i/ne0) % ne1;
  3200. const int i0 = i % ne0;
  3201. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3202. return;
  3203. }
  3204. const int i11 = i1 % ne11;
  3205. const int i12 = i2 % ne12;
  3206. const int i13 = i3 % ne13;
  3207. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3208. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3209. const size_t i_dst = i_src0;
  3210. const src0_t * src0_row = src0 + i_src0;
  3211. const src1_t * src1_row = src1 + i_src1;
  3212. dst_t * dst_row = dst + i_dst;
  3213. const int i10 = i0 % ne10;
  3214. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3215. }
  3216. static void acc_f32(const float * x, const float * y, float * dst, const int ne,
  3217. const int ne10, const int ne11, const int ne12,
  3218. const int nb1, const int nb2, int offset, const sycl::nd_item<3> &item_ct1) {
  3219. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3220. item_ct1.get_local_id(2);
  3221. if (i >= ne) {
  3222. return;
  3223. }
  3224. int src1_idx = i - offset;
  3225. int oz = src1_idx / nb2;
  3226. int oy = (src1_idx - (oz * nb2)) / nb1;
  3227. int ox = src1_idx % nb1;
  3228. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  3229. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  3230. } else {
  3231. dst[i] = x[i];
  3232. }
  3233. }
  3234. static void gelu_f32(const float * x, float * dst, const int k,
  3235. const sycl::nd_item<3> &item_ct1) {
  3236. const float GELU_COEF_A = 0.044715f;
  3237. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  3238. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3239. item_ct1.get_local_id(2);
  3240. if (i >= k) {
  3241. return;
  3242. }
  3243. float xi = x[i];
  3244. dst[i] = 0.5f * xi *
  3245. (1.0f +
  3246. sycl::tanh(SQRT_2_OVER_PI * xi * (1.0f + GELU_COEF_A * xi * xi)));
  3247. }
  3248. static void silu_f32(const float * x, float * dst, const int k,
  3249. const sycl::nd_item<3> &item_ct1) {
  3250. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3251. item_ct1.get_local_id(2);
  3252. if (i >= k) {
  3253. return;
  3254. }
  3255. dst[i] = x[i] / (1.0f + sycl::native::exp(-x[i]));
  3256. }
  3257. static void gelu_quick_f32(const float *x, float *dst, int k,
  3258. const sycl::nd_item<3> &item_ct1) {
  3259. const float GELU_QUICK_COEF = -1.702f;
  3260. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3261. item_ct1.get_local_id(2);
  3262. if (i >= k) {
  3263. return;
  3264. }
  3265. dst[i] = x[i] * (1.0f / (1.0f + sycl::native::exp(GELU_QUICK_COEF * x[i])));
  3266. }
  3267. static void tanh_f32(const float *x, float *dst, int k,
  3268. const sycl::nd_item<3> &item_ct1) {
  3269. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3270. item_ct1.get_local_id(2);
  3271. if (i >= k) {
  3272. return;
  3273. }
  3274. dst[i] = sycl::tanh((float)(x[i]));
  3275. }
  3276. static void relu_f32(const float * x, float * dst, const int k,
  3277. const sycl::nd_item<3> &item_ct1) {
  3278. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3279. item_ct1.get_local_id(2);
  3280. if (i >= k) {
  3281. return;
  3282. }
  3283. dst[i] = sycl::fmax((float)(x[i]), (float)0);
  3284. }
  3285. static void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope,
  3286. const sycl::nd_item<3> &item_ct1) {
  3287. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3288. item_ct1.get_local_id(2);
  3289. if (i >= k) {
  3290. return;
  3291. }
  3292. dst[i] = sycl::fmax((float)(x[i]), (float)0) +
  3293. sycl::fmin((float)(x[i]), 0.0f) * negative_slope;
  3294. }
  3295. static void sqr_f32(const float * x, float * dst, const int k,
  3296. const sycl::nd_item<3> &item_ct1) {
  3297. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3298. item_ct1.get_local_id(2);
  3299. if (i >= k) {
  3300. return;
  3301. }
  3302. dst[i] = x[i] * x[i];
  3303. }
  3304. static void norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3305. const sycl::nd_item<3> &item_ct1, sycl::float2 *s_sum, int block_size) {
  3306. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3307. item_ct1.get_local_id(1);
  3308. const int tid = item_ct1.get_local_id(2);
  3309. sycl::float2 mean_var = sycl::float2(0.f, 0.f);
  3310. for (int col = tid; col < ncols; col += block_size) {
  3311. const float xi = x[row*ncols + col];
  3312. mean_var.x() += xi;
  3313. mean_var.y() += xi * xi;
  3314. }
  3315. // sum up partial sums
  3316. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3317. if (block_size > WARP_SIZE) {
  3318. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3319. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3320. if (lane_id == 0) {
  3321. s_sum[warp_id] = mean_var;
  3322. }
  3323. /*
  3324. DPCT1118:0: SYCL group functions and algorithms must be encountered in
  3325. converged control flow. You may need to adjust the code.
  3326. */
  3327. item_ct1.barrier(sycl::access::fence_space::local_space);
  3328. mean_var = s_sum[lane_id];
  3329. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3330. }
  3331. const float mean = mean_var.x() / ncols;
  3332. const float var = mean_var.y() / ncols - mean * mean;
  3333. const float inv_std = sycl::rsqrt(var + eps);
  3334. for (int col = tid; col < ncols; col += block_size) {
  3335. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  3336. }
  3337. }
  3338. static void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02,
  3339. const sycl::nd_item<3> &item_ct1) {
  3340. int nidx = item_ct1.get_local_id(2) +
  3341. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3342. if (nidx >= ne0) {
  3343. return;
  3344. }
  3345. // operation
  3346. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3347. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3348. if (item_ct1.get_group(0) < ne02) { // src0
  3349. int offset_src =
  3350. nidx + item_ct1.get_group(1) * ne0 +
  3351. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3352. dst[offset_dst] = x[offset_src];
  3353. } else {
  3354. int offset_src =
  3355. nidx + item_ct1.get_group(1) * ne0 +
  3356. (item_ct1.get_group(0) - ne02) * ne0 * item_ct1.get_group_range(1);
  3357. dst[offset_dst] = y[offset_src];
  3358. }
  3359. }
  3360. static void upscale_f32(const float *x, float *dst, const int ne00, const int nb02, const int scale_factor,
  3361. const sycl::nd_item<3> &item_ct1) {
  3362. int ne0 = ne00 * scale_factor;
  3363. int nidx = item_ct1.get_local_id(2) +
  3364. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3365. if (nidx >= ne0) {
  3366. return;
  3367. }
  3368. // operation
  3369. int i00 = nidx / scale_factor;
  3370. int i01 = item_ct1.get_group(1) / scale_factor;
  3371. int offset_src = i00 + i01 * ne00 + item_ct1.get_group(0) * nb02;
  3372. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3373. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3374. dst[offset_dst] = x[offset_src];
  3375. }
  3376. static void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02,
  3377. const sycl::nd_item<3> &item_ct1) {
  3378. int nidx = item_ct1.get_local_id(2) +
  3379. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3380. if (nidx >= ne0) {
  3381. return;
  3382. }
  3383. // operation
  3384. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3385. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3386. if (nidx < ne00 && item_ct1.get_group(1) < ne01 &&
  3387. item_ct1.get_group(0) < ne02) {
  3388. int offset_src = nidx + item_ct1.get_group(1) * ne00 +
  3389. item_ct1.get_group(0) * ne00 * ne01;
  3390. dst[offset_dst] = x[offset_src];
  3391. } else {
  3392. dst[offset_dst] = 0.0f;
  3393. }
  3394. }
  3395. static void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps,
  3396. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3397. int start = item_ct1.get_group(2) * group_size;
  3398. int end = start + group_size;
  3399. start += item_ct1.get_local_id(2);
  3400. if (end >= ne_elements) {
  3401. end = ne_elements;
  3402. }
  3403. float tmp = 0.0f; // partial sum for thread in warp
  3404. for (int j = start; j < end; j += block_size) {
  3405. tmp += x[j];
  3406. }
  3407. tmp = warp_reduce_sum(tmp, item_ct1);
  3408. if (block_size > WARP_SIZE) {
  3409. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3410. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3411. if (lane_id == 0) {
  3412. s_sum[warp_id] = tmp;
  3413. }
  3414. /*
  3415. DPCT1118:1: SYCL group functions and algorithms must be encountered in
  3416. converged control flow. You may need to adjust the code.
  3417. */
  3418. /*
  3419. DPCT1065:54: Consider replacing sycl::nd_item::barrier() with
  3420. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3421. better performance if there is no access to global memory.
  3422. */
  3423. item_ct1.barrier();
  3424. tmp = s_sum[lane_id];
  3425. tmp = warp_reduce_sum(tmp, item_ct1);
  3426. }
  3427. float mean = tmp / group_size;
  3428. tmp = 0.0f;
  3429. for (int j = start; j < end; j += block_size) {
  3430. float xi = x[j] - mean;
  3431. dst[j] = xi;
  3432. tmp += xi * xi;
  3433. }
  3434. tmp = warp_reduce_sum(tmp, item_ct1);
  3435. if (block_size > WARP_SIZE) {
  3436. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3437. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3438. if (lane_id == 0) {
  3439. s_sum[warp_id] = tmp;
  3440. }
  3441. /*
  3442. DPCT1118:2: SYCL group functions and algorithms must be encountered in
  3443. converged control flow. You may need to adjust the code.
  3444. */
  3445. /*
  3446. DPCT1065:55: Consider replacing sycl::nd_item::barrier() with
  3447. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3448. better performance if there is no access to global memory.
  3449. */
  3450. item_ct1.barrier();
  3451. tmp = s_sum[lane_id];
  3452. tmp = warp_reduce_sum(tmp, item_ct1);
  3453. }
  3454. float variance = tmp / group_size;
  3455. float scale = sycl::rsqrt(variance + eps);
  3456. for (int j = start; j < end; j += block_size) {
  3457. dst[j] *= scale;
  3458. }
  3459. }
  3460. static void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3461. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3462. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3463. item_ct1.get_local_id(1);
  3464. const int tid = item_ct1.get_local_id(2);
  3465. float tmp = 0.0f; // partial sum for thread in warp
  3466. for (int col = tid; col < ncols; col += block_size) {
  3467. const float xi = x[row*ncols + col];
  3468. tmp += xi * xi;
  3469. }
  3470. // sum up partial sums
  3471. tmp = warp_reduce_sum(tmp, item_ct1);
  3472. if (block_size > WARP_SIZE) {
  3473. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3474. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3475. if (lane_id == 0) {
  3476. s_sum[warp_id] = tmp;
  3477. }
  3478. /*
  3479. DPCT1118:3: SYCL group functions and algorithms must be encountered in
  3480. converged control flow. You may need to adjust the code.
  3481. */
  3482. item_ct1.barrier(sycl::access::fence_space::local_space);
  3483. tmp = s_sum[lane_id];
  3484. tmp = warp_reduce_sum(tmp, item_ct1);
  3485. }
  3486. const float mean = tmp / ncols;
  3487. const float scale = sycl::rsqrt(mean + eps);
  3488. for (int col = tid; col < ncols; col += block_size) {
  3489. dst[row*ncols + col] = scale * x[row*ncols + col];
  3490. }
  3491. }
  3492. static __dpct_inline__ void dequantize_q4_0(const void *vx, const int ib,
  3493. const int iqs, dfloat2 &v) {
  3494. const block_q4_0 * x = (const block_q4_0 *) vx;
  3495. const dfloat d = x[ib].d;
  3496. const int vui = x[ib].qs[iqs];
  3497. v.x() = vui & 0xF;
  3498. v.y() = vui >> 4;
  3499. #ifdef GGML_SYCL_F16
  3500. // v = v - {8.0f, 8.0f};
  3501. // v = v * {d, d};
  3502. v.s0() = (v.s0() - 8.0f) * d;
  3503. v.s1() = (v.s1() - 8.0f) * d;
  3504. #else
  3505. v.x() = (v.x() - 8.0f) * d;
  3506. v.y() = (v.y() - 8.0f) * d;
  3507. #endif // GGML_SYCL_F16
  3508. }
  3509. static __dpct_inline__ void dequantize_q4_1(const void *vx, const int ib,
  3510. const int iqs, dfloat2 &v) {
  3511. const block_q4_1 * x = (const block_q4_1 *) vx;
  3512. const dfloat d = x[ib].dm[0];
  3513. const dfloat m = x[ib].dm[1];
  3514. const int vui = x[ib].qs[iqs];
  3515. v.x() = vui & 0xF;
  3516. v.y() = vui >> 4;
  3517. #ifdef GGML_SYCL_F16
  3518. // v = v * {d, d};
  3519. // v = v + {m, m};
  3520. v.s0() = (v.s0() * d) + m;
  3521. v.s1() = (v.s1() * d) + m;
  3522. #else
  3523. v.x() = (v.x() * d) + m;
  3524. v.y() = (v.y() * d) + m;
  3525. #endif // GGML_SYCL_F16
  3526. }
  3527. static __dpct_inline__ void dequantize_q5_0(const void *vx, const int ib,
  3528. const int iqs, dfloat2 &v) {
  3529. const block_q5_0 * x = (const block_q5_0 *) vx;
  3530. const dfloat d = x[ib].d;
  3531. uint32_t qh;
  3532. memcpy(&qh, x[ib].qh, sizeof(qh));
  3533. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3534. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3535. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3536. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3537. #ifdef GGML_SYCL_F16
  3538. // v = v - {16.0f, 16.0f};
  3539. // v = v * {d, d};
  3540. v.s0() = (v.s0() - 16.0f) * d;
  3541. v.s1() = (v.s1() - 16.0f) * d;
  3542. #else
  3543. v.x() = (v.x() - 16.0f) * d;
  3544. v.y() = (v.y() - 16.0f) * d;
  3545. #endif // GGML_SYCL_F16
  3546. }
  3547. static __dpct_inline__ void dequantize_q5_1(const void *vx, const int ib,
  3548. const int iqs, dfloat2 &v) {
  3549. const block_q5_1 * x = (const block_q5_1 *) vx;
  3550. const dfloat d = x[ib].dm[0];
  3551. const dfloat m = x[ib].dm[1];
  3552. uint32_t qh;
  3553. memcpy(&qh, x[ib].qh, sizeof(qh));
  3554. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3555. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3556. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3557. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3558. #ifdef GGML_SYCL_F16
  3559. // v = v * {d, d};
  3560. // v = v + {m, m};
  3561. v.s0() = (v.s0() * d) + m;
  3562. v.s1() = (v.s1() * d) + m;
  3563. #else
  3564. v.x() = (v.x() * d) + m;
  3565. v.y() = (v.y() * d) + m;
  3566. #endif // GGML_SYCL_F16
  3567. }
  3568. static __dpct_inline__ void dequantize_q8_0(const void *vx, const int ib,
  3569. const int iqs, dfloat2 &v) {
  3570. const block_q8_0 * x = (const block_q8_0 *) vx;
  3571. const dfloat d = x[ib].d;
  3572. v.x() = x[ib].qs[iqs + 0];
  3573. v.y() = x[ib].qs[iqs + 1];
  3574. #ifdef GGML_SYCL_F16
  3575. // v = v * {d, d};
  3576. v.s0() *= d;
  3577. v.s1() *= d;
  3578. #else
  3579. v.x() *= d;
  3580. v.y() *= d;
  3581. #endif // GGML_SYCL_F16
  3582. }
  3583. //================================== k-quants
  3584. template<typename dst_t>
  3585. static void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3586. const sycl::nd_item<3> &item_ct1) {
  3587. const int i = item_ct1.get_group(2);
  3588. const block_q2_K * x = (const block_q2_K *) vx;
  3589. const int tid = item_ct1.get_local_id(2);
  3590. #if QK_K == 256
  3591. const int n = tid/32;
  3592. const int l = tid - 32*n;
  3593. const int is = 8*n + l/16;
  3594. const uint8_t q = x[i].qs[32*n + l];
  3595. dst_t * y = yy + i*QK_K + 128*n;
  3596. float dall = x[i].dm[0];
  3597. float dmin = x[i].dm[1];
  3598. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3599. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3600. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  3601. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  3602. #else
  3603. const int is = tid/16; // 0 or 1
  3604. const int il = tid%16; // 0...15
  3605. const uint8_t q = x[i].qs[il] >> (2*is);
  3606. dst_t * y = yy + i*QK_K + 16*is + il;
  3607. float dall = __low2half(x[i].dm);
  3608. float dmin = __high2half(x[i].dm);
  3609. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3610. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3611. #endif
  3612. }
  3613. template<typename dst_t>
  3614. static void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3615. const sycl::nd_item<3> &item_ct1) {
  3616. const int i = item_ct1.get_group(2);
  3617. const block_q3_K * x = (const block_q3_K *) vx;
  3618. #if QK_K == 256
  3619. const int r = item_ct1.get_local_id(2) / 4;
  3620. const int tid = r/2;
  3621. const int is0 = r%2;
  3622. const int l0 = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4);
  3623. const int n = tid / 4;
  3624. const int j = tid - 4*n;
  3625. uint8_t m = 1 << (4*n + j);
  3626. int is = 8*n + 2*j + is0;
  3627. int shift = 2*j;
  3628. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  3629. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  3630. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  3631. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  3632. float d_all = x[i].d;
  3633. float dl = d_all * (us - 32);
  3634. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  3635. const uint8_t * q = x[i].qs + 32*n;
  3636. const uint8_t * hm = x[i].hmask;
  3637. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  3638. #else
  3639. const int tid = threadIdx.x;
  3640. const int is = tid/16; // 0 or 1
  3641. const int il = tid%16; // 0...15
  3642. const int im = il/8; // 0...1
  3643. const int in = il%8; // 0...7
  3644. dst_t * y = yy + i*QK_K + 16*is + il;
  3645. const uint8_t q = x[i].qs[il] >> (2*is);
  3646. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  3647. const float d = (float)x[i].d;
  3648. if (is == 0) {
  3649. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  3650. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  3651. } else {
  3652. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  3653. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  3654. }
  3655. #endif
  3656. }
  3657. #if QK_K == 256
  3658. static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  3659. if (j < 4) {
  3660. d = q[j] & 63; m = q[j + 4] & 63;
  3661. } else {
  3662. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  3663. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  3664. }
  3665. }
  3666. #endif
  3667. template<typename dst_t>
  3668. static void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3669. const sycl::nd_item<3> &item_ct1) {
  3670. const block_q4_K * x = (const block_q4_K *) vx;
  3671. const int i = item_ct1.get_group(2);
  3672. #if QK_K == 256
  3673. // assume 32 threads
  3674. const int tid = item_ct1.get_local_id(2);
  3675. const int il = tid/8;
  3676. const int ir = tid%8;
  3677. const int is = 2*il;
  3678. const int n = 4;
  3679. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  3680. const float dall = x[i].dm[0];
  3681. const float dmin = x[i].dm[1];
  3682. const uint8_t * q = x[i].qs + 32*il + n*ir;
  3683. uint8_t sc, m;
  3684. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3685. const float d1 = dall * sc; const float m1 = dmin * m;
  3686. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3687. const float d2 = dall * sc; const float m2 = dmin * m;
  3688. for (int l = 0; l < n; ++l) {
  3689. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  3690. y[l +32] = d2 * (q[l] >> 4) - m2;
  3691. }
  3692. #else
  3693. const int tid = threadIdx.x;
  3694. const uint8_t * q = x[i].qs;
  3695. dst_t * y = yy + i*QK_K;
  3696. const float d = (float)x[i].dm[0];
  3697. const float m = (float)x[i].dm[1];
  3698. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  3699. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  3700. #endif
  3701. }
  3702. template<typename dst_t>
  3703. static void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3704. const sycl::nd_item<3> &item_ct1) {
  3705. const block_q5_K * x = (const block_q5_K *) vx;
  3706. const int i = item_ct1.get_group(2);
  3707. #if QK_K == 256
  3708. // assume 64 threads - this is very slightly better than the one below
  3709. const int tid = item_ct1.get_local_id(2);
  3710. const int il = tid/16; // il is in 0...3
  3711. const int ir = tid%16; // ir is in 0...15
  3712. const int is = 2*il; // is is in 0...6
  3713. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  3714. const float dall = x[i].dm[0];
  3715. const float dmin = x[i].dm[1];
  3716. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  3717. const uint8_t * qh = x[i].qh + 2*ir;
  3718. uint8_t sc, m;
  3719. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3720. const float d1 = dall * sc; const float m1 = dmin * m;
  3721. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3722. const float d2 = dall * sc; const float m2 = dmin * m;
  3723. uint8_t hm = 1 << (2*il);
  3724. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  3725. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  3726. hm <<= 1;
  3727. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  3728. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  3729. #else
  3730. const int tid = threadIdx.x;
  3731. const uint8_t q = x[i].qs[tid];
  3732. const int im = tid/8; // 0...3
  3733. const int in = tid%8; // 0...7
  3734. const int is = tid/16; // 0 or 1
  3735. const uint8_t h = x[i].qh[in] >> im;
  3736. const float d = x[i].d;
  3737. dst_t * y = yy + i*QK_K + tid;
  3738. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  3739. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  3740. #endif
  3741. }
  3742. template<typename dst_t>
  3743. static void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3744. const sycl::nd_item<3> &item_ct1) {
  3745. const block_q6_K * x = (const block_q6_K *) vx;
  3746. const int i = item_ct1.get_group(2);
  3747. #if QK_K == 256
  3748. // assume 64 threads - this is very slightly better than the one below
  3749. const int tid = item_ct1.get_local_id(2);
  3750. const int ip = tid/32; // ip is 0 or 1
  3751. const int il = tid - 32*ip; // 0...32
  3752. const int is = 8*ip + il/16;
  3753. dst_t * y = yy + i*QK_K + 128*ip + il;
  3754. const float d = x[i].d;
  3755. const uint8_t * ql = x[i].ql + 64*ip + il;
  3756. const uint8_t qh = x[i].qh[32*ip + il];
  3757. const int8_t * sc = x[i].scales + is;
  3758. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3759. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  3760. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3761. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  3762. #else
  3763. // assume 32 threads
  3764. const int tid = threadIdx.x;
  3765. const int ip = tid/16; // 0 or 1
  3766. const int il = tid - 16*ip; // 0...15
  3767. dst_t * y = yy + i*QK_K + 16*ip + il;
  3768. const float d = x[i].d;
  3769. const uint8_t ql = x[i].ql[16*ip + il];
  3770. const uint8_t qh = x[i].qh[il] >> (2*ip);
  3771. const int8_t * sc = x[i].scales;
  3772. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3773. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3774. #endif
  3775. }
  3776. /*
  3777. DPCT1110:4: The total declared local variable size in device function
  3778. dequantize_mul_mat_vec_q2_k exceeds 128 bytes and may cause high register
  3779. pressure. Consult with your hardware vendor to find the total register size
  3780. available and adjust the code, or use smaller sub-group size to avoid high
  3781. register pressure.
  3782. */
  3783. static void dequantize_mul_mat_vec_q2_k(const void *__restrict__ vx,
  3784. const float *__restrict__ yy,
  3785. float *__restrict__ dst,
  3786. const int ncols, int nrows,
  3787. const sycl::nd_item<3> &item_ct1) {
  3788. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  3789. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3790. item_ct1.get_local_id(1);
  3791. if (row > nrows) return;
  3792. const int num_blocks_per_row = ncols / QK_K;
  3793. const int ib0 = row*num_blocks_per_row;
  3794. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  3795. float tmp = 0; // partial sum for thread in warp
  3796. #if QK_K == 256
  3797. const int tid =
  3798. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  3799. const int ix =
  3800. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  3801. const int step = 16/K_QUANTS_PER_ITERATION;
  3802. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  3803. const int in = tid - step*im; // 0...15 or 0...7
  3804. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  3805. const int q_offset = 32*im + l0;
  3806. const int s_offset = 8*im;
  3807. const int y_offset = 128*im + l0;
  3808. uint32_t aux[4];
  3809. const uint8_t * d = (const uint8_t *)aux;
  3810. const uint8_t * m = (const uint8_t *)(aux + 2);
  3811. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  3812. const float * y = yy + i * QK_K + y_offset;
  3813. const uint8_t * q = x[i].qs + q_offset;
  3814. const float dall = x[i].dm[0];
  3815. const float dmin = x[i].dm[1];
  3816. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  3817. aux[0] = a[0] & 0x0f0f0f0f;
  3818. aux[1] = a[1] & 0x0f0f0f0f;
  3819. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  3820. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  3821. float sum1 = 0, sum2 = 0;
  3822. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  3823. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  3824. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  3825. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  3826. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  3827. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  3828. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  3829. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  3830. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  3831. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  3832. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  3833. }
  3834. tmp += dall * sum1 - dmin * sum2;
  3835. }
  3836. #else
  3837. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  3838. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  3839. const int offset = tid * K_QUANTS_PER_ITERATION;
  3840. uint32_t uaux[2];
  3841. const uint8_t * d = (const uint8_t *)uaux;
  3842. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  3843. const float * y = yy + i * QK_K + offset;
  3844. const uint8_t * q = x[i].qs + offset;
  3845. const uint32_t * s = (const uint32_t *)x[i].scales;
  3846. uaux[0] = s[0] & 0x0f0f0f0f;
  3847. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  3848. const float2 dall = __half22float2(x[i].dm);
  3849. float sum1 = 0, sum2 = 0;
  3850. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  3851. const uint8_t ql = q[l];
  3852. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  3853. + y[l+16] * d[1] * ((ql >> 2) & 3)
  3854. + y[l+32] * d[2] * ((ql >> 4) & 3)
  3855. + y[l+48] * d[3] * ((ql >> 6) & 3);
  3856. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  3857. }
  3858. tmp += dall.x * sum1 - dall.y * sum2;
  3859. }
  3860. #endif
  3861. // sum up partial sums and write back result
  3862. #pragma unroll
  3863. for (int mask = 16; mask > 0; mask >>= 1) {
  3864. tmp +=
  3865. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  3866. }
  3867. if (item_ct1.get_local_id(2) == 0) {
  3868. dst[row] = tmp;
  3869. }
  3870. }
  3871. /*
  3872. DPCT1110:5: The total declared local variable size in device function
  3873. dequantize_mul_mat_vec_q3_k exceeds 128 bytes and may cause high register
  3874. pressure. Consult with your hardware vendor to find the total register size
  3875. available and adjust the code, or use smaller sub-group size to avoid high
  3876. register pressure.
  3877. */
  3878. static void dequantize_mul_mat_vec_q3_k(const void *__restrict__ vx,
  3879. const float *__restrict__ yy,
  3880. float *__restrict__ dst,
  3881. const int ncols, int nrows,
  3882. const sycl::nd_item<3> &item_ct1) {
  3883. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3884. item_ct1.get_local_id(1);
  3885. if (row > nrows) return;
  3886. const int num_blocks_per_row = ncols / QK_K;
  3887. const int ib0 = row*num_blocks_per_row;
  3888. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  3889. float tmp = 0; // partial sum for thread in warp
  3890. #if QK_K == 256
  3891. const uint16_t kmask1 = 0x0303;
  3892. const uint16_t kmask2 = 0x0f0f;
  3893. const int tid =
  3894. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  3895. const int ix =
  3896. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  3897. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  3898. const int step = 16/K_QUANTS_PER_ITERATION;
  3899. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  3900. const int in = tid - step*im; // 0....15 or 0...7
  3901. const uint8_t m = 1 << (4*im);
  3902. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  3903. const int q_offset = 32*im + l0;
  3904. const int y_offset = 128*im + l0;
  3905. uint16_t utmp[4];
  3906. const int8_t * s = (const int8_t *)utmp;
  3907. const uint16_t s_shift = 4*im;
  3908. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  3909. const float * y = yy + i * QK_K + y_offset;
  3910. const uint8_t * q = x[i].qs + q_offset;
  3911. const uint8_t * h = x[i].hmask + l0;
  3912. const uint16_t * a = (const uint16_t *)x[i].scales;
  3913. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  3914. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  3915. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  3916. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  3917. const float d = x[i].d;
  3918. float sum = 0;
  3919. for (int l = 0; l < n; ++l) {
  3920. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  3921. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  3922. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  3923. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  3924. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  3925. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  3926. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  3927. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  3928. }
  3929. tmp += d * sum;
  3930. }
  3931. #else
  3932. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  3933. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  3934. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  3935. const int in = offset/8; // 0 or 1
  3936. const int im = offset%8; // 0...7
  3937. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  3938. const float * y = yy + i * QK_K + offset;
  3939. const uint8_t * q = x[i].qs + offset;
  3940. const uint8_t * s = x[i].scales;
  3941. const float dall = (float)x[i].d;
  3942. float sum = 0;
  3943. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  3944. const uint8_t hl = x[i].hmask[im+l] >> in;
  3945. const uint8_t ql = q[l];
  3946. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  3947. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  3948. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  3949. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  3950. }
  3951. tmp += sum;
  3952. }
  3953. #endif
  3954. // sum up partial sums and write back result
  3955. #pragma unroll
  3956. for (int mask = 16; mask > 0; mask >>= 1) {
  3957. tmp +=
  3958. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  3959. }
  3960. if (item_ct1.get_local_id(2) == 0) {
  3961. dst[row] = tmp;
  3962. }
  3963. }
  3964. /*
  3965. DPCT1110:6: The total declared local variable size in device function
  3966. dequantize_mul_mat_vec_q4_k exceeds 128 bytes and may cause high register
  3967. pressure. Consult with your hardware vendor to find the total register size
  3968. available and adjust the code, or use smaller sub-group size to avoid high
  3969. register pressure.
  3970. */
  3971. static void dequantize_mul_mat_vec_q4_k(const void *__restrict__ vx,
  3972. const float *__restrict__ yy,
  3973. float *__restrict__ dst,
  3974. const int ncols, int nrows,
  3975. const sycl::nd_item<3> &item_ct1) {
  3976. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3977. item_ct1.get_local_id(1);
  3978. if (row > nrows) return;
  3979. const int num_blocks_per_row = ncols / QK_K;
  3980. const int ib0 = row*num_blocks_per_row;
  3981. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  3982. #if QK_K == 256
  3983. const uint16_t kmask1 = 0x3f3f;
  3984. const uint16_t kmask2 = 0x0f0f;
  3985. const uint16_t kmask3 = 0xc0c0;
  3986. const int tid =
  3987. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  3988. const int ix =
  3989. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  3990. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  3991. const int il = tid/step; // 0...3
  3992. const int ir = tid - step*il; // 0...7 or 0...3
  3993. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  3994. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  3995. const int in = il%2;
  3996. const int l0 = n*(2*ir + in);
  3997. const int q_offset = 32*im + l0;
  3998. const int y_offset = 64*im + l0;
  3999. uint16_t aux[4];
  4000. const uint8_t * sc = (const uint8_t *)aux;
  4001. #if K_QUANTS_PER_ITERATION == 2
  4002. uint32_t q32[4];
  4003. const uint8_t * q4 = (const uint8_t *)q32;
  4004. #else
  4005. uint16_t q16[4];
  4006. const uint8_t * q4 = (const uint8_t *)q16;
  4007. #endif
  4008. float tmp = 0; // partial sum for thread in warp
  4009. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4010. const float * y1 = yy + i*QK_K + y_offset;
  4011. const float * y2 = y1 + 128;
  4012. const float dall = x[i].dm[0];
  4013. const float dmin = x[i].dm[1];
  4014. const uint16_t * a = (const uint16_t *)x[i].scales;
  4015. aux[0] = a[im+0] & kmask1;
  4016. aux[1] = a[im+2] & kmask1;
  4017. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4018. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4019. #if K_QUANTS_PER_ITERATION == 2
  4020. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  4021. const uint32_t * q2 = q1 + 16;
  4022. q32[0] = q1[0] & 0x0f0f0f0f;
  4023. q32[1] = q1[0] & 0xf0f0f0f0;
  4024. q32[2] = q2[0] & 0x0f0f0f0f;
  4025. q32[3] = q2[0] & 0xf0f0f0f0;
  4026. sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
  4027. float smin = 0;
  4028. for (int l = 0; l < 4; ++l) {
  4029. s.x() += y1[l] * q4[l + 0]; s.y() += y1[l + 32] * q4[l + 4];
  4030. s.z() += y2[l] * q4[l + 8]; s.w() += y2[l + 32] * q4[l + 12];
  4031. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4032. }
  4033. tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f / 16.f +
  4034. s.z() * sc[4] + s.w() * sc[5] * 1.f / 16.f) -
  4035. dmin * smin;
  4036. #else
  4037. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  4038. const uint16_t * q2 = q1 + 32;
  4039. q16[0] = q1[0] & 0x0f0f;
  4040. q16[1] = q1[0] & 0xf0f0;
  4041. q16[2] = q2[0] & 0x0f0f;
  4042. q16[3] = q2[0] & 0xf0f0;
  4043. float4 s = {0.f, 0.f, 0.f, 0.f};
  4044. float smin = 0;
  4045. for (int l = 0; l < 2; ++l) {
  4046. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  4047. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  4048. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4049. }
  4050. 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;
  4051. #endif
  4052. }
  4053. #else
  4054. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  4055. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  4056. const int step = tid * K_QUANTS_PER_ITERATION;
  4057. uint16_t aux16[2];
  4058. const uint8_t * s = (const uint8_t *)aux16;
  4059. float tmp = 0;
  4060. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4061. const uint8_t * q = x[i].qs + step;
  4062. const float * y = yy + i*QK_K + step;
  4063. const uint16_t * a = (const uint16_t *)x[i].scales;
  4064. aux16[0] = a[0] & 0x0f0f;
  4065. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4066. const float d = (float)x[i].dm[0];
  4067. const float m = (float)x[i].dm[1];
  4068. float sum = 0.f;
  4069. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4070. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  4071. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  4072. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  4073. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  4074. }
  4075. tmp += sum;
  4076. }
  4077. #endif
  4078. // sum up partial sums and write back result
  4079. #pragma unroll
  4080. for (int mask = 16; mask > 0; mask >>= 1) {
  4081. tmp +=
  4082. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4083. }
  4084. if (tid == 0) {
  4085. dst[row] = tmp;
  4086. }
  4087. }
  4088. /*
  4089. DPCT1110:7: The total declared local variable size in device function
  4090. dequantize_mul_mat_vec_q5_k exceeds 128 bytes and may cause high register
  4091. pressure. Consult with your hardware vendor to find the total register size
  4092. available and adjust the code, or use smaller sub-group size to avoid high
  4093. register pressure.
  4094. */
  4095. static void dequantize_mul_mat_vec_q5_k(const void *__restrict__ vx,
  4096. const float *__restrict__ yy,
  4097. float *__restrict__ dst,
  4098. const int ncols,
  4099. const sycl::nd_item<3> &item_ct1) {
  4100. const int row = item_ct1.get_group(2);
  4101. const int num_blocks_per_row = ncols / QK_K;
  4102. const int ib0 = row*num_blocks_per_row;
  4103. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  4104. float tmp = 0; // partial sum for thread in warp
  4105. #if QK_K == 256
  4106. const uint16_t kmask1 = 0x3f3f;
  4107. const uint16_t kmask2 = 0x0f0f;
  4108. const uint16_t kmask3 = 0xc0c0;
  4109. const int tid = item_ct1.get_local_id(2) / 2; // 0...15
  4110. const int ix = item_ct1.get_local_id(2) % 2;
  4111. const int il = tid/4; // 0...3
  4112. const int ir = tid - 4*il;// 0...3
  4113. const int n = 2;
  4114. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4115. const int in = il%2;
  4116. const int l0 = n*(2*ir + in);
  4117. const int q_offset = 32*im + l0;
  4118. const int y_offset = 64*im + l0;
  4119. const uint8_t hm1 = 1 << (2*im);
  4120. const uint8_t hm2 = hm1 << 4;
  4121. uint16_t aux[4];
  4122. const uint8_t * sc = (const uint8_t *)aux;
  4123. uint16_t q16[8];
  4124. const uint8_t * q4 = (const uint8_t *)q16;
  4125. for (int i = ix; i < num_blocks_per_row; i += 2) {
  4126. const uint8_t * ql1 = x[i].qs + q_offset;
  4127. const uint8_t * qh = x[i].qh + l0;
  4128. const float * y1 = yy + i*QK_K + y_offset;
  4129. const float * y2 = y1 + 128;
  4130. const float dall = x[i].dm[0];
  4131. const float dmin = x[i].dm[1];
  4132. const uint16_t * a = (const uint16_t *)x[i].scales;
  4133. aux[0] = a[im+0] & kmask1;
  4134. aux[1] = a[im+2] & kmask1;
  4135. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4136. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4137. sycl::float4 sum = {0.f, 0.f, 0.f, 0.f};
  4138. float smin = 0;
  4139. const uint16_t * q1 = (const uint16_t *)ql1;
  4140. const uint16_t * q2 = q1 + 32;
  4141. q16[0] = q1[0] & 0x0f0f;
  4142. q16[1] = q1[8] & 0x0f0f;
  4143. q16[2] = (q1[0] >> 4) & 0x0f0f;
  4144. q16[3] = (q1[8] >> 4) & 0x0f0f;
  4145. q16[4] = q2[0] & 0x0f0f;
  4146. q16[5] = q2[8] & 0x0f0f;
  4147. q16[6] = (q2[0] >> 4) & 0x0f0f;
  4148. q16[7] = (q2[8] >> 4) & 0x0f0f;
  4149. for (int l = 0; l < n; ++l) {
  4150. sum.x() +=
  4151. y1[l + 0] * (q4[l + 0] + (qh[l + 0] & (hm1 << 0) ? 16 : 0)) +
  4152. y1[l + 16] * (q4[l + 2] + (qh[l + 16] & (hm1 << 0) ? 16 : 0));
  4153. sum.y() +=
  4154. y1[l + 32] * (q4[l + 4] + (qh[l + 0] & (hm1 << 1) ? 16 : 0)) +
  4155. y1[l + 48] * (q4[l + 6] + (qh[l + 16] & (hm1 << 1) ? 16 : 0));
  4156. sum.z() +=
  4157. y2[l + 0] * (q4[l + 8] + (qh[l + 0] & (hm2 << 0) ? 16 : 0)) +
  4158. y2[l + 16] * (q4[l + 10] + (qh[l + 16] & (hm2 << 0) ? 16 : 0));
  4159. sum.w() +=
  4160. y2[l + 32] * (q4[l + 12] + (qh[l + 0] & (hm2 << 1) ? 16 : 0)) +
  4161. y2[l + 48] * (q4[l + 14] + (qh[l + 16] & (hm2 << 1) ? 16 : 0));
  4162. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  4163. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  4164. }
  4165. tmp += dall * (sum.x() * sc[0] + sum.y() * sc[1] + sum.z() * sc[4] +
  4166. sum.w() * sc[5]) -
  4167. dmin * smin;
  4168. }
  4169. #else
  4170. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  4171. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  4172. const int step = tid * K_QUANTS_PER_ITERATION;
  4173. const int im = step/8;
  4174. const int in = step%8;
  4175. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4176. const uint8_t * q = x[i].qs + step;
  4177. const int8_t * s = x[i].scales;
  4178. const float * y = yy + i*QK_K + step;
  4179. const float d = x[i].d;
  4180. float sum = 0.f;
  4181. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4182. const uint8_t h = x[i].qh[in+j] >> im;
  4183. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  4184. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  4185. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  4186. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  4187. }
  4188. tmp += sum;
  4189. }
  4190. #endif
  4191. // sum up partial sums and write back result
  4192. #pragma unroll
  4193. for (int mask = 16; mask > 0; mask >>= 1) {
  4194. tmp +=
  4195. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4196. }
  4197. if (item_ct1.get_local_id(2) == 0) {
  4198. dst[row] = tmp;
  4199. }
  4200. }
  4201. static void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows,
  4202. const sycl::nd_item<3> &item_ct1) {
  4203. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4204. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4205. item_ct1.get_local_id(1);
  4206. if (row > nrows) return;
  4207. const int num_blocks_per_row = ncols / QK_K;
  4208. const int ib0 = row*num_blocks_per_row;
  4209. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  4210. #if QK_K == 256
  4211. const int tid =
  4212. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4213. const int ix =
  4214. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0, 1
  4215. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  4216. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4217. const int in = tid - step*im; // 0...15 or 0...7
  4218. #if K_QUANTS_PER_ITERATION == 1
  4219. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  4220. const int is = 0;
  4221. #else
  4222. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  4223. const int is = in / 4;
  4224. #endif
  4225. const int ql_offset = 64*im + l0;
  4226. const int qh_offset = 32*im + l0;
  4227. const int s_offset = 8*im + is;
  4228. const int y_offset = 128*im + l0;
  4229. float tmp = 0; // partial sum for thread in warp
  4230. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4231. const float * y = yy + i * QK_K + y_offset;
  4232. const uint8_t * ql = x[i].ql + ql_offset;
  4233. const uint8_t * qh = x[i].qh + qh_offset;
  4234. const int8_t * s = x[i].scales + s_offset;
  4235. const float d = x[i].d;
  4236. #if K_QUANTS_PER_ITERATION == 1
  4237. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  4238. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  4239. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  4240. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  4241. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  4242. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  4243. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  4244. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  4245. tmp += sum;
  4246. #else
  4247. float sum = 0;
  4248. for (int l = 0; l < 4; ++l) {
  4249. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  4250. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  4251. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  4252. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  4253. }
  4254. tmp += sum;
  4255. #endif
  4256. }
  4257. #else
  4258. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  4259. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  4260. const int step = tid * K_QUANTS_PER_ITERATION;
  4261. float tmp = 0; // partial sum for thread in warp
  4262. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4263. const float * y = yy + i * QK_K + step;
  4264. const uint8_t * ql = x[i].ql + step;
  4265. const uint8_t * qh = x[i].qh + step;
  4266. const int8_t * s = x[i].scales;
  4267. const float d = x[i+0].d;
  4268. float sum = 0;
  4269. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4270. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  4271. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  4272. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  4273. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  4274. }
  4275. tmp += sum;
  4276. }
  4277. #endif
  4278. // sum up partial sums and write back result
  4279. #pragma unroll
  4280. for (int mask = 16; mask > 0; mask >>= 1) {
  4281. tmp +=
  4282. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4283. }
  4284. if (tid == 0) {
  4285. dst[row] = tmp;
  4286. }
  4287. }
  4288. static void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4289. const sycl::half *x = (const sycl::half *)vx;
  4290. // automatic half -> float type cast if dfloat == float
  4291. v.x() = x[ib + iqs + 0];
  4292. v.y() = x[ib + iqs + 1];
  4293. }
  4294. static void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4295. const float * x = (const float *) vx;
  4296. // automatic half -> float type cast if dfloat == float
  4297. v.x() = x[ib + iqs + 0];
  4298. v.y() = x[ib + iqs + 1];
  4299. }
  4300. static void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded,
  4301. const sycl::nd_item<3> &item_ct1) {
  4302. const int ix = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4303. item_ct1.get_local_id(2);
  4304. if (ix >= kx_padded) {
  4305. return;
  4306. }
  4307. const int iy = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4308. item_ct1.get_local_id(1);
  4309. const int i_padded = iy*kx_padded + ix;
  4310. block_q8_1 * y = (block_q8_1 *) vy;
  4311. const int ib = i_padded / QK8_1; // block index
  4312. const int iqs = i_padded % QK8_1; // quant index
  4313. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  4314. float amax = sycl::fabs((float)xi);
  4315. float sum = xi;
  4316. #pragma unroll
  4317. for (int mask = 16; mask > 0; mask >>= 1) {
  4318. amax = sycl::fmax(amax, dpct::permute_sub_group_by_xor(
  4319. item_ct1.get_sub_group(), amax, mask));
  4320. sum +=
  4321. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), sum, mask);
  4322. }
  4323. const float d = amax / 127;
  4324. const int8_t q = amax == 0.0f ? 0 : sycl::round(xi / d);
  4325. y[ib].qs[iqs] = q;
  4326. if (iqs > 0) {
  4327. return;
  4328. }
  4329. reinterpret_cast<sycl::half &>(y[ib].ds.x()) = d;
  4330. reinterpret_cast<sycl::half &>(y[ib].ds.y()) = sum;
  4331. }
  4332. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4333. static void k_get_rows(
  4334. const void * src0, const int32_t * src1, dst_t * dst,
  4335. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4336. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4337. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4338. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4339. size_t s10, size_t s11, size_t s12,
  4340. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4341. const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4342. item_ct1.get_local_id(2)) *
  4343. 2;
  4344. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4345. item_ct1.get_local_id(1);
  4346. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4347. item_ct1.get_local_id(0)) /
  4348. ne12;
  4349. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4350. item_ct1.get_local_id(0)) %
  4351. ne12;
  4352. if (i00 >= ne00) {
  4353. return;
  4354. }
  4355. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4356. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4357. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  4358. const int ib = i00/qk; // block index
  4359. const int iqs = (i00%qk)/qr; // quant index
  4360. const int iybs = i00 - i00%qk; // dst block start index
  4361. const int y_offset = qr == 1 ? 1 : qk/2;
  4362. // dequantize
  4363. dfloat2 v;
  4364. dequantize_kernel(src0_row, ib, iqs, v);
  4365. dst_row[iybs + iqs + 0] = v.x();
  4366. dst_row[iybs + iqs + y_offset] = v.y();
  4367. }
  4368. template<typename src0_t, typename dst_t>
  4369. static void k_get_rows_float(
  4370. const src0_t * src0, const int32_t * src1, dst_t * dst,
  4371. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4372. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4373. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4374. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4375. size_t s10, size_t s11, size_t s12,
  4376. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4377. const int i00 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4378. item_ct1.get_local_id(2);
  4379. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4380. item_ct1.get_local_id(1);
  4381. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4382. item_ct1.get_local_id(0)) /
  4383. ne12;
  4384. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4385. item_ct1.get_local_id(0)) %
  4386. ne12;
  4387. if (i00 >= ne00) {
  4388. return;
  4389. }
  4390. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4391. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4392. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  4393. dst_row[i00] = src0_row[i00];
  4394. }
  4395. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4396. static void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4397. const sycl::nd_item<3> &item_ct1) {
  4398. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4399. 2 * item_ct1.get_local_id(2);
  4400. if (i >= k) {
  4401. return;
  4402. }
  4403. const int ib = i/qk; // block index
  4404. const int iqs = (i%qk)/qr; // quant index
  4405. const int iybs = i - i%qk; // y block start index
  4406. const int y_offset = qr == 1 ? 1 : qk/2;
  4407. // dequantize
  4408. dfloat2 v;
  4409. dequantize_kernel(vx, ib, iqs, v);
  4410. y[iybs + iqs + 0] = v.x();
  4411. y[iybs + iqs + y_offset] = v.y();
  4412. }
  4413. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  4414. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  4415. #define VDR_Q4_0_Q8_1_MMVQ 2
  4416. #define VDR_Q4_0_Q8_1_MMQ 4
  4417. template <int vdr>
  4418. static __dpct_inline__ float vec_dot_q4_0_q8_1_impl(const int *v, const int *u,
  4419. const float &d4,
  4420. const sycl::half2 &ds8) {
  4421. int sumi = 0;
  4422. #pragma unroll
  4423. for (int i = 0; i < vdr; ++i) {
  4424. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4425. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4426. // SIMD dot product of quantized values
  4427. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4428. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4429. }
  4430. const sycl::float2 ds8f =
  4431. ds8.convert<float, sycl::rounding_mode::automatic>();
  4432. // second part effectively subtracts 8 from each quant value
  4433. return d4 * (sumi * ds8f.x() - (8 * vdr / QI4_0) * ds8f.y());
  4434. }
  4435. #define VDR_Q4_1_Q8_1_MMVQ 2
  4436. #define VDR_Q4_1_Q8_1_MMQ 4
  4437. template <int vdr>
  4438. static __dpct_inline__ float vec_dot_q4_1_q8_1_impl(const int *v, const int *u,
  4439. const sycl::half2 &dm4,
  4440. const sycl::half2 &ds8) {
  4441. int sumi = 0;
  4442. #pragma unroll
  4443. for (int i = 0; i < vdr; ++i) {
  4444. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4445. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4446. // SIMD dot product of quantized values
  4447. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4448. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4449. }
  4450. #ifdef GGML_SYCL_F16
  4451. const sycl::float2 tmp =
  4452. (dm4 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4453. const float d4d8 = tmp.x();
  4454. const float m4s8 = tmp.y();
  4455. #else
  4456. const sycl::float2 dm4f =
  4457. dm4.convert<float, sycl::rounding_mode::automatic>();
  4458. const sycl::float2 ds8f =
  4459. ds8.convert<float, sycl::rounding_mode::automatic>();
  4460. const float d4d8 = dm4f.x() * ds8f.x();
  4461. const float m4s8 = dm4f.y() * ds8f.y();
  4462. #endif // GGML_SYCL_F16
  4463. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  4464. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  4465. }
  4466. #define VDR_Q5_0_Q8_1_MMVQ 2
  4467. #define VDR_Q5_0_Q8_1_MMQ 4
  4468. template <int vdr>
  4469. static __dpct_inline__ float
  4470. vec_dot_q5_0_q8_1_impl(const int *vl, const int *vh, const int *u,
  4471. const float &d5, const sycl::half2 &ds8) {
  4472. int sumi = 0;
  4473. #pragma unroll
  4474. for (int i = 0; i < vdr; ++i) {
  4475. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4476. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4477. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4478. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4479. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4480. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4481. sumi); // SIMD dot product of quantized values
  4482. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4483. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4484. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4485. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4486. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4487. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4488. sumi); // SIMD dot product of quantized values
  4489. }
  4490. const sycl::float2 ds8f =
  4491. ds8.convert<float, sycl::rounding_mode::automatic>();
  4492. // second part effectively subtracts 16 from each quant value
  4493. return d5 * (sumi * ds8f.x() - (16 * vdr / QI5_0) * ds8f.y());
  4494. }
  4495. #define VDR_Q5_1_Q8_1_MMVQ 2
  4496. #define VDR_Q5_1_Q8_1_MMQ 4
  4497. template <int vdr>
  4498. static __dpct_inline__ float
  4499. vec_dot_q5_1_q8_1_impl(const int *vl, const int *vh, const int *u,
  4500. const sycl::half2 &dm5, const sycl::half2 &ds8) {
  4501. int sumi = 0;
  4502. #pragma unroll
  4503. for (int i = 0; i < vdr; ++i) {
  4504. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4505. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4506. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4507. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4508. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4509. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4510. sumi); // SIMD dot product of quantized values
  4511. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4512. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4513. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4514. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4515. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4516. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4517. sumi); // SIMD dot product of quantized values
  4518. }
  4519. #ifdef GGML_SYCL_F16
  4520. const sycl::float2 tmp =
  4521. (dm5 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4522. const float d5d8 = tmp.x();
  4523. const float m5s8 = tmp.y();
  4524. #else
  4525. const sycl::float2 dm5f =
  4526. dm5.convert<float, sycl::rounding_mode::automatic>();
  4527. const sycl::float2 ds8f =
  4528. ds8.convert<float, sycl::rounding_mode::automatic>();
  4529. const float d5d8 = dm5f.x() * ds8f.x();
  4530. const float m5s8 = dm5f.y() * ds8f.y();
  4531. #endif // GGML_SYCL_F16
  4532. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  4533. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  4534. }
  4535. #define VDR_Q8_0_Q8_1_MMVQ 2
  4536. #define VDR_Q8_0_Q8_1_MMQ 8
  4537. template <int vdr>
  4538. static __dpct_inline__ float vec_dot_q8_0_q8_1_impl(const int *v, const int *u,
  4539. const float &d8_0,
  4540. const float &d8_1) {
  4541. int sumi = 0;
  4542. #pragma unroll
  4543. for (int i = 0; i < vdr; ++i) {
  4544. // SIMD dot product of quantized values
  4545. sumi = dpct::dp4a(v[i], u[i], sumi);
  4546. }
  4547. return d8_0*d8_1 * sumi;
  4548. }
  4549. template <int vdr>
  4550. static __dpct_inline__ float vec_dot_q8_1_q8_1_impl(const int *v, const int *u,
  4551. const sycl::half2 &dm8,
  4552. const sycl::half2 &ds8) {
  4553. int sumi = 0;
  4554. #pragma unroll
  4555. for (int i = 0; i < vdr; ++i) {
  4556. // SIMD dot product of quantized values
  4557. sumi = dpct::dp4a(v[i], u[i], sumi);
  4558. }
  4559. #ifdef GGML_SYCL_F16
  4560. const sycl::float2 tmp =
  4561. (dm8 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4562. const float d8d8 = tmp.x();
  4563. const float m8s8 = tmp.y();
  4564. #else
  4565. const sycl::float2 dm8f =
  4566. dm8.convert<float, sycl::rounding_mode::automatic>();
  4567. const sycl::float2 ds8f =
  4568. ds8.convert<float, sycl::rounding_mode::automatic>();
  4569. const float d8d8 = dm8f.x() * ds8f.x();
  4570. const float m8s8 = dm8f.y() * ds8f.y();
  4571. #endif // GGML_SYCL_F16
  4572. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  4573. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  4574. }
  4575. #define VDR_Q2_K_Q8_1_MMVQ 1
  4576. #define VDR_Q2_K_Q8_1_MMQ 2
  4577. // contiguous v/x values
  4578. static __dpct_inline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  4579. const int &v, const int *__restrict__ u, const uint8_t *__restrict__ scales,
  4580. const sycl::half2 &dm2, const float *__restrict__ d8) {
  4581. float sumf_d = 0.0f;
  4582. float sumf_m = 0.0f;
  4583. #pragma unroll
  4584. for (int i = 0; i < QR2_K; ++i) {
  4585. const int sc = scales[2*i];
  4586. const int vi = (v >> (2*i)) & 0x03030303;
  4587. sumf_d +=
  4588. d8[i] * (dpct::dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  4589. // fill int with 4x m
  4590. int m = sc >> 4;
  4591. m |= m << 8;
  4592. m |= m << 16;
  4593. sumf_m += d8[i] *
  4594. dpct::dp4a(
  4595. m, u[i],
  4596. 0); // multiply constant q2_K part with sum of q8_1 values
  4597. }
  4598. const sycl::float2 dm2f =
  4599. dm2.convert<float, sycl::rounding_mode::automatic>();
  4600. return dm2f.x() * sumf_d - dm2f.y() * sumf_m;
  4601. }
  4602. // contiguous u/y values
  4603. static __dpct_inline__ float
  4604. vec_dot_q2_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4605. const uint8_t *__restrict__ scales,
  4606. const sycl::half2 &dm2, const float &d8) {
  4607. int sumi_d = 0;
  4608. int sumi_m = 0;
  4609. #pragma unroll
  4610. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  4611. int sumi_d_sc = 0;
  4612. const int sc = scales[i0 / (QI8_1/2)];
  4613. // fill int with 4x m
  4614. int m = sc >> 4;
  4615. m |= m << 8;
  4616. m |= m << 16;
  4617. #pragma unroll
  4618. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  4619. sumi_d_sc = dpct::dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  4620. sumi_m = dpct::dp4a(m, u[i],
  4621. sumi_m); // multiply sum of q8_1 values with m
  4622. }
  4623. sumi_d += sumi_d_sc * (sc & 0xF);
  4624. }
  4625. const sycl::float2 dm2f =
  4626. dm2.convert<float, sycl::rounding_mode::automatic>();
  4627. return d8 * (dm2f.x() * sumi_d - dm2f.y() * sumi_m);
  4628. }
  4629. #define VDR_Q3_K_Q8_1_MMVQ 1
  4630. #define VDR_Q3_K_Q8_1_MMQ 2
  4631. // contiguous v/x values
  4632. static __dpct_inline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  4633. const int &vl, const int &vh, const int *__restrict__ u,
  4634. const uint8_t *__restrict__ scales, const int &scale_offset,
  4635. const float &d3, const float *__restrict__ d8) {
  4636. float sumf = 0.0f;
  4637. #pragma unroll
  4638. for (int i = 0; i < QR3_K; ++i) {
  4639. const int isc = scale_offset + 2*i;
  4640. const int isc_low = isc % (QK_K/32);
  4641. const int sc_shift_low = 4 * (isc / (QK_K/32));
  4642. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  4643. const int isc_high = isc % (QK_K/64);
  4644. const int sc_shift_high = 2 * (isc / (QK_K/64));
  4645. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  4646. const int sc = (sc_low | sc_high) - 32;
  4647. const int vil = (vl >> (2*i)) & 0x03030303;
  4648. const int vih = ((vh >> i) << 2) & 0x04040404;
  4649. const int vi =
  4650. dpct::vectorized_binary<sycl::char4>(vil, vih, dpct::sub_sat());
  4651. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  4652. }
  4653. return d3 * sumf;
  4654. }
  4655. // contiguous u/y values
  4656. static __dpct_inline__ float
  4657. vec_dot_q3_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4658. const int8_t *__restrict__ scales, const float &d3,
  4659. const float &d8) {
  4660. int sumi = 0;
  4661. #pragma unroll
  4662. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  4663. int sumi_sc = 0;
  4664. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  4665. sumi_sc = dpct::dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  4666. }
  4667. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  4668. }
  4669. return d3*d8 * sumi;
  4670. }
  4671. #define VDR_Q4_K_Q8_1_MMVQ 2
  4672. #define VDR_Q4_K_Q8_1_MMQ 8
  4673. // contiguous v/x values
  4674. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  4675. const int *__restrict__ v, const int *__restrict__ u,
  4676. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4677. const sycl::half2 &dm4, const float *__restrict__ d8) {
  4678. float sumf_d = 0.0f;
  4679. float sumf_m = 0.0f;
  4680. #pragma unroll
  4681. for (int i = 0; i < QR4_K; ++i) {
  4682. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  4683. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  4684. const int dot1 =
  4685. dpct::dp4a(v1i, u[2 * i + 1],
  4686. dpct::dp4a(v0i, u[2 * i + 0], 0)); // SIMD dot product
  4687. const int dot2 =
  4688. dpct::dp4a(0x01010101, u[2 * i + 1],
  4689. dpct::dp4a(0x01010101, u[2 * i + 0], 0)); // sum of u
  4690. sumf_d += d8[i] * (dot1 * sc[i]);
  4691. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  4692. }
  4693. const sycl::float2 dm4f =
  4694. dm4.convert<float, sycl::rounding_mode::automatic>();
  4695. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4696. }
  4697. // contiguous u/y values
  4698. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_mmq(
  4699. const int *__restrict__ v, const int *__restrict__ u,
  4700. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4701. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  4702. float sumf_d = 0.0f;
  4703. float sumf_m = 0.0f;
  4704. #pragma unroll
  4705. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  4706. int sumi_d = 0;
  4707. #pragma unroll
  4708. for (int j = 0; j < QI8_1; ++j) {
  4709. sumi_d = dpct::dp4a((v[j] >> (4 * i)) & 0x0F0F0F0F,
  4710. u[i * QI8_1 + j], sumi_d); // SIMD dot product
  4711. }
  4712. const sycl::float2 ds8f =
  4713. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  4714. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  4715. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  4716. }
  4717. const sycl::float2 dm4f =
  4718. dm4.convert<float, sycl::rounding_mode::automatic>();
  4719. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4720. }
  4721. #define VDR_Q5_K_Q8_1_MMVQ 2
  4722. #define VDR_Q5_K_Q8_1_MMQ 8
  4723. // contiguous v/x values
  4724. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  4725. const int *__restrict__ vl, const int *__restrict__ vh,
  4726. const int *__restrict__ u, const uint8_t *__restrict__ sc,
  4727. const uint8_t *__restrict__ m, const sycl::half2 &dm5,
  4728. const float *__restrict__ d8) {
  4729. float sumf_d = 0.0f;
  4730. float sumf_m = 0.0f;
  4731. #pragma unroll
  4732. for (int i = 0; i < QR5_K; ++i) {
  4733. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  4734. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  4735. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  4736. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  4737. const int v0i = vl0i | vh0i;
  4738. const int v1i = vl1i | vh1i;
  4739. const int dot1 =
  4740. dpct::dp4a(v0i, u[2 * i + 0],
  4741. dpct::dp4a(v1i, u[2 * i + 1], 0)); // SIMD dot product
  4742. const int dot2 =
  4743. dpct::dp4a(0x01010101, u[2 * i + 0],
  4744. dpct::dp4a(0x01010101, u[2 * i + 1], 0)); // sum of u
  4745. sumf_d += d8[i] * (dot1 * sc[i]);
  4746. sumf_m += d8[i] * (dot2 * m[i]);
  4747. }
  4748. const sycl::float2 dm5f =
  4749. dm5.convert<float, sycl::rounding_mode::automatic>();
  4750. return dm5f.x() * sumf_d - dm5f.y() * sumf_m;
  4751. }
  4752. // contiguous u/y values
  4753. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_mmq(
  4754. const int *__restrict__ v, const int *__restrict__ u,
  4755. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4756. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  4757. float sumf_d = 0.0f;
  4758. float sumf_m = 0.0f;
  4759. #pragma unroll
  4760. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  4761. int sumi_d = 0;
  4762. #pragma unroll
  4763. for (int j = 0; j < QI8_1; ++j) {
  4764. sumi_d = dpct::dp4a(v[i * QI8_1 + j], u[i * QI8_1 + j],
  4765. sumi_d); // SIMD dot product
  4766. }
  4767. const sycl::float2 ds8f =
  4768. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  4769. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  4770. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  4771. }
  4772. const sycl::float2 dm4f =
  4773. dm4.convert<float, sycl::rounding_mode::automatic>();
  4774. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4775. }
  4776. #define VDR_Q6_K_Q8_1_MMVQ 1
  4777. #define VDR_Q6_K_Q8_1_MMQ 8
  4778. // contiguous v/x values
  4779. static __dpct_inline__ float
  4780. vec_dot_q6_K_q8_1_impl_mmvq(const int &vl, const int &vh,
  4781. const int *__restrict__ u,
  4782. const int8_t *__restrict__ scales, const float &d,
  4783. const float *__restrict__ d8) {
  4784. float sumf = 0.0f;
  4785. #pragma unroll
  4786. for (int i = 0; i < QR6_K; ++i) {
  4787. const int sc = scales[4*i];
  4788. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  4789. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  4790. const int vi = dpct::vectorized_binary<sycl::char4>(
  4791. (vil | vih), 0x20202020, dpct::sub_sat()); // vi = (vil | vih) - 32
  4792. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  4793. }
  4794. return d*sumf;
  4795. }
  4796. // contiguous u/y values
  4797. static __dpct_inline__ float
  4798. vec_dot_q6_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4799. const int8_t *__restrict__ sc, const float &d6,
  4800. const float *__restrict__ d8) {
  4801. float sumf_d = 0.0f;
  4802. #pragma unroll
  4803. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  4804. sycl::int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  4805. #pragma unroll
  4806. for (int i = i0; i < i0 + 2; ++i) {
  4807. sumi_d.x() = dpct::dp4a(v[2 * i + 0], u[2 * i + 0],
  4808. sumi_d.x()); // SIMD dot product
  4809. sumi_d.x() = dpct::dp4a(v[2 * i + 1], u[2 * i + 1],
  4810. sumi_d.x()); // SIMD dot product
  4811. sumi_d.y() = dpct::dp4a(v[2 * i + 4], u[2 * i + 4],
  4812. sumi_d.y()); // SIMD dot product
  4813. sumi_d.y() = dpct::dp4a(v[2 * i + 5], u[2 * i + 5],
  4814. sumi_d.y()); // SIMD dot product
  4815. }
  4816. sumf_d += d8[i0 / 4] *
  4817. (sc[i0 / 2 + 0] * sumi_d.x() + sc[i0 / 2 + 1] * sumi_d.y());
  4818. }
  4819. return d6 * sumf_d;
  4820. }
  4821. static __dpct_inline__ float
  4822. vec_dot_q4_0_q8_1(const void *__restrict__ vbq,
  4823. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  4824. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  4825. int v[VDR_Q4_0_Q8_1_MMVQ];
  4826. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  4827. #pragma unroll
  4828. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  4829. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  4830. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  4831. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  4832. }
  4833. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  4834. }
  4835. template <int mmq_y>
  4836. static __dpct_inline__ void
  4837. allocate_tiles_q4_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  4838. int *tile_x_qs_q4_0, float *tile_x_d_q4_0) {
  4839. (void)x_qh; (void)x_sc;
  4840. *x_ql = tile_x_qs_q4_0;
  4841. *x_dm = (sycl::half2 *)tile_x_d_q4_0;
  4842. }
  4843. template <int mmq_y, int nwarps, bool need_check>
  4844. static __dpct_inline__ void
  4845. load_tiles_q4_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  4846. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  4847. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  4848. const int &k, const int &blocks_per_row) {
  4849. (void)x_qh; (void)x_sc;
  4850. GGML_SYCL_ASSUME(i_offset >= 0);
  4851. GGML_SYCL_ASSUME(i_offset < nwarps);
  4852. GGML_SYCL_ASSUME(k >= 0);
  4853. GGML_SYCL_ASSUME(k < WARP_SIZE);
  4854. const int kbx = k / QI4_0;
  4855. const int kqsx = k % QI4_0;
  4856. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  4857. float * x_dmf = (float *) x_dm;
  4858. #pragma unroll
  4859. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  4860. int i = i0 + i_offset;
  4861. if (need_check) {
  4862. i = sycl::min(i, i_max);
  4863. }
  4864. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  4865. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  4866. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  4867. }
  4868. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  4869. const int kbxd = k % blocks_per_tile_x_row;
  4870. #pragma unroll
  4871. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  4872. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  4873. if (need_check) {
  4874. i = sycl::min(i, i_max);
  4875. }
  4876. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  4877. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  4878. }
  4879. }
  4880. static __dpct_inline__ float vec_dot_q4_0_q8_1_mul_mat(
  4881. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  4882. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  4883. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  4884. const int &i, const int &j, const int &k) {
  4885. (void)x_qh; (void)x_sc;
  4886. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  4887. const float * x_dmf = (const float *) x_dm;
  4888. int u[2*VDR_Q4_0_Q8_1_MMQ];
  4889. #pragma unroll
  4890. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  4891. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  4892. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  4893. }
  4894. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  4895. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  4896. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  4897. }
  4898. static __dpct_inline__ float
  4899. vec_dot_q4_1_q8_1(const void *__restrict__ vbq,
  4900. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  4901. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  4902. int v[VDR_Q4_1_Q8_1_MMVQ];
  4903. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  4904. #pragma unroll
  4905. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  4906. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  4907. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  4908. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  4909. }
  4910. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  4911. }
  4912. template <int mmq_y>
  4913. static __dpct_inline__ void
  4914. allocate_tiles_q4_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  4915. int *tile_x_qs_q4_1, sycl::half2 *tile_x_dm_q4_1) {
  4916. (void)x_qh; (void)x_sc;
  4917. *x_ql = tile_x_qs_q4_1;
  4918. *x_dm = tile_x_dm_q4_1;
  4919. }
  4920. template <int mmq_y, int nwarps, bool need_check>
  4921. static __dpct_inline__ void
  4922. load_tiles_q4_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  4923. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  4924. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  4925. const int &k, const int &blocks_per_row) {
  4926. (void)x_qh; (void)x_sc;
  4927. GGML_SYCL_ASSUME(i_offset >= 0);
  4928. GGML_SYCL_ASSUME(i_offset < nwarps);
  4929. GGML_SYCL_ASSUME(k >= 0);
  4930. GGML_SYCL_ASSUME(k < WARP_SIZE);
  4931. const int kbx = k / QI4_1;
  4932. const int kqsx = k % QI4_1;
  4933. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  4934. #pragma unroll
  4935. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  4936. int i = i0 + i_offset;
  4937. if (need_check) {
  4938. i = sycl::min(i, i_max);
  4939. }
  4940. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  4941. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  4942. }
  4943. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  4944. const int kbxd = k % blocks_per_tile_x_row;
  4945. #pragma unroll
  4946. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  4947. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  4948. if (need_check) {
  4949. i = sycl::min(i, i_max);
  4950. }
  4951. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  4952. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  4953. }
  4954. }
  4955. static __dpct_inline__ float vec_dot_q4_1_q8_1_mul_mat(
  4956. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  4957. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  4958. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  4959. const int &i, const int &j, const int &k) {
  4960. (void)x_qh; (void)x_sc;
  4961. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  4962. int u[2*VDR_Q4_1_Q8_1_MMQ];
  4963. #pragma unroll
  4964. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  4965. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  4966. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  4967. }
  4968. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  4969. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  4970. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  4971. }
  4972. static __dpct_inline__ float
  4973. vec_dot_q5_0_q8_1(const void *__restrict__ vbq,
  4974. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  4975. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  4976. int vl[VDR_Q5_0_Q8_1_MMVQ];
  4977. int vh[VDR_Q5_0_Q8_1_MMVQ];
  4978. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  4979. #pragma unroll
  4980. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  4981. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  4982. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  4983. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  4984. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  4985. }
  4986. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  4987. }
  4988. template <int mmq_y>
  4989. static __dpct_inline__ void
  4990. allocate_tiles_q5_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  4991. int *tile_x_ql_q5_0, float *tile_x_d_q5_0) {
  4992. (void)x_qh; (void)x_sc;
  4993. *x_ql = tile_x_ql_q5_0;
  4994. *x_dm = (sycl::half2 *)tile_x_d_q5_0;
  4995. }
  4996. template <int mmq_y, int nwarps, bool need_check>
  4997. static __dpct_inline__ void
  4998. load_tiles_q5_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  4999. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5000. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5001. const int &k, const int &blocks_per_row) {
  5002. (void)x_qh; (void)x_sc;
  5003. GGML_SYCL_ASSUME(i_offset >= 0);
  5004. GGML_SYCL_ASSUME(i_offset < nwarps);
  5005. GGML_SYCL_ASSUME(k >= 0);
  5006. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5007. const int kbx = k / QI5_0;
  5008. const int kqsx = k % QI5_0;
  5009. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  5010. #pragma unroll
  5011. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5012. int i = i0 + i_offset;
  5013. if (need_check) {
  5014. i = sycl::min(i, i_max);
  5015. }
  5016. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5017. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  5018. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  5019. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5020. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5021. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5022. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5023. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5024. qs0 = dpct::vectorized_binary<sycl::char4>(
  5025. qs0, 0x10101010, dpct::sub_sat()); // subtract 16
  5026. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5027. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5028. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5029. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5030. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5031. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5032. qs1 = dpct::vectorized_binary<sycl::char4>(
  5033. qs1, 0x10101010, dpct::sub_sat()); // subtract 16
  5034. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5035. }
  5036. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  5037. const int kbxd = k % blocks_per_tile_x_row;
  5038. float * x_dmf = (float *) x_dm;
  5039. #pragma unroll
  5040. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  5041. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  5042. if (need_check) {
  5043. i = sycl::min(i, i_max);
  5044. }
  5045. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5046. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  5047. }
  5048. }
  5049. static __dpct_inline__ float vec_dot_q5_0_q8_1_mul_mat(
  5050. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5051. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5052. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5053. const int &i, const int &j, const int &k) {
  5054. (void)x_qh; (void)x_sc;
  5055. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5056. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  5057. const float * x_dmf = (const float *) x_dm;
  5058. const float * y_df = (const float *) y_ds;
  5059. int u[2*VDR_Q5_0_Q8_1_MMQ];
  5060. #pragma unroll
  5061. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  5062. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5063. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  5064. }
  5065. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  5066. (&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)]);
  5067. }
  5068. static __dpct_inline__ float
  5069. vec_dot_q5_1_q8_1(const void *__restrict__ vbq,
  5070. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5071. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  5072. int vl[VDR_Q5_1_Q8_1_MMVQ];
  5073. int vh[VDR_Q5_1_Q8_1_MMVQ];
  5074. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  5075. #pragma unroll
  5076. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  5077. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  5078. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  5079. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5080. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  5081. }
  5082. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  5083. }
  5084. template <int mmq_y>
  5085. static __dpct_inline__ void
  5086. allocate_tiles_q5_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5087. int *tile_x_ql_q5_1, sycl::half2 *tile_x_dm_q5_1) {
  5088. (void)x_qh; (void)x_sc;
  5089. *x_ql = tile_x_ql_q5_1;
  5090. *x_dm = tile_x_dm_q5_1;
  5091. }
  5092. template <int mmq_y, int nwarps, bool need_check>
  5093. static __dpct_inline__ void
  5094. load_tiles_q5_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5095. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5096. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5097. const int &k, const int &blocks_per_row) {
  5098. (void)x_qh; (void)x_sc;
  5099. GGML_SYCL_ASSUME(i_offset >= 0);
  5100. GGML_SYCL_ASSUME(i_offset < nwarps);
  5101. GGML_SYCL_ASSUME(k >= 0);
  5102. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5103. const int kbx = k / QI5_1;
  5104. const int kqsx = k % QI5_1;
  5105. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  5106. #pragma unroll
  5107. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5108. int i = i0 + i_offset;
  5109. if (need_check) {
  5110. i = sycl::min(i, i_max);
  5111. }
  5112. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5113. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5114. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  5115. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5116. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5117. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5118. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5119. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5120. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5121. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5122. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5123. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5124. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5125. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5126. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5127. }
  5128. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  5129. const int kbxd = k % blocks_per_tile_x_row;
  5130. #pragma unroll
  5131. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  5132. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  5133. if (need_check) {
  5134. i = sycl::min(i, i_max);
  5135. }
  5136. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5137. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  5138. }
  5139. }
  5140. static __dpct_inline__ float vec_dot_q5_1_q8_1_mul_mat(
  5141. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5142. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5143. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5144. const int &i, const int &j, const int &k) {
  5145. (void)x_qh; (void)x_sc;
  5146. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5147. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  5148. int u[2*VDR_Q5_1_Q8_1_MMQ];
  5149. #pragma unroll
  5150. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  5151. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5152. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  5153. }
  5154. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  5155. (&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)]);
  5156. }
  5157. static __dpct_inline__ float
  5158. vec_dot_q8_0_q8_1(const void *__restrict__ vbq,
  5159. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5160. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  5161. int v[VDR_Q8_0_Q8_1_MMVQ];
  5162. int u[VDR_Q8_0_Q8_1_MMVQ];
  5163. #pragma unroll
  5164. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  5165. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  5166. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5167. }
  5168. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d,
  5169. bq8_1->ds[0]);
  5170. }
  5171. template <int mmq_y>
  5172. static __dpct_inline__ void
  5173. allocate_tiles_q8_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5174. int *tile_x_qs_q8_0, float *tile_x_d_q8_0) {
  5175. (void)x_qh; (void)x_sc;
  5176. *x_ql = tile_x_qs_q8_0;
  5177. *x_dm = (sycl::half2 *)tile_x_d_q8_0;
  5178. }
  5179. template <int mmq_y, int nwarps, bool need_check>
  5180. static __dpct_inline__ void
  5181. load_tiles_q8_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5182. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5183. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5184. const int &k, const int &blocks_per_row) {
  5185. (void)x_qh; (void)x_sc;
  5186. GGML_SYCL_ASSUME(i_offset >= 0);
  5187. GGML_SYCL_ASSUME(i_offset < nwarps);
  5188. GGML_SYCL_ASSUME(k >= 0);
  5189. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5190. const int kbx = k / QI8_0;
  5191. const int kqsx = k % QI8_0;
  5192. float * x_dmf = (float *) x_dm;
  5193. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  5194. #pragma unroll
  5195. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5196. int i = i0 + i_offset;
  5197. if (need_check) {
  5198. i = sycl::min(i, i_max);
  5199. }
  5200. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5201. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  5202. }
  5203. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  5204. const int kbxd = k % blocks_per_tile_x_row;
  5205. #pragma unroll
  5206. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  5207. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  5208. if (need_check) {
  5209. i = sycl::min(i, i_max);
  5210. }
  5211. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5212. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  5213. }
  5214. }
  5215. static __dpct_inline__ float vec_dot_q8_0_q8_1_mul_mat(
  5216. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5217. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5218. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5219. const int &i, const int &j, const int &k) {
  5220. (void)x_qh; (void)x_sc;
  5221. const float * x_dmf = (const float *) x_dm;
  5222. const float * y_df = (const float *) y_ds;
  5223. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  5224. (&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],
  5225. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  5226. }
  5227. static __dpct_inline__ float
  5228. vec_dot_q2_K_q8_1(const void *__restrict__ vbq,
  5229. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5230. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  5231. const int bq8_offset = QR2_K * (iqs / QI8_1);
  5232. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5233. const uint8_t * scales = bq2_K->scales + scale_offset;
  5234. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  5235. int u[QR2_K];
  5236. float d8[QR2_K];
  5237. #pragma unroll
  5238. for (int i = 0; i < QR2_K; ++ i) {
  5239. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5240. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5241. }
  5242. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  5243. }
  5244. template <int mmq_y>
  5245. static __dpct_inline__ void
  5246. allocate_tiles_q2_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5247. int *tile_x_ql_q2_K, sycl::half2 *tile_x_dm_q2_K,
  5248. int *tile_x_sc_q2_K) {
  5249. (void)x_qh;
  5250. *x_ql = tile_x_ql_q2_K;
  5251. *x_dm = tile_x_dm_q2_K;
  5252. *x_sc = tile_x_sc_q2_K;
  5253. }
  5254. template <int mmq_y, int nwarps, bool need_check>
  5255. static __dpct_inline__ void
  5256. load_tiles_q2_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5257. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5258. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5259. const int &k, const int &blocks_per_row) {
  5260. (void)x_qh;
  5261. GGML_SYCL_ASSUME(i_offset >= 0);
  5262. GGML_SYCL_ASSUME(i_offset < nwarps);
  5263. GGML_SYCL_ASSUME(k >= 0);
  5264. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5265. const int kbx = k / QI2_K;
  5266. const int kqsx = k % QI2_K;
  5267. const block_q2_K * bx0 = (const block_q2_K *) vx;
  5268. #pragma unroll
  5269. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5270. int i = i0 + i_offset;
  5271. if (need_check) {
  5272. i = sycl::min(i, i_max);
  5273. }
  5274. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  5275. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5276. }
  5277. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  5278. const int kbxd = k % blocks_per_tile_x_row;
  5279. #pragma unroll
  5280. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  5281. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  5282. if (need_check) {
  5283. i = sycl::min(i, i_max);
  5284. }
  5285. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5286. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  5287. }
  5288. #pragma unroll
  5289. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5290. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5291. if (need_check) {
  5292. i = sycl::min(i, i_max);
  5293. }
  5294. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  5295. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  5296. }
  5297. }
  5298. static __dpct_inline__ float vec_dot_q2_K_q8_1_mul_mat(
  5299. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5300. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5301. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5302. const int &i, const int &j, const int &k) {
  5303. (void)x_qh;
  5304. const int kbx = k / QI2_K;
  5305. const int ky = (k % QI2_K) * QR2_K;
  5306. const float * y_df = (const float *) y_ds;
  5307. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  5308. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  5309. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  5310. #pragma unroll
  5311. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  5312. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5313. }
  5314. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  5315. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  5316. 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]);
  5317. }
  5318. static __dpct_inline__ float
  5319. vec_dot_q3_K_q8_1(const void *__restrict__ vbq,
  5320. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5321. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  5322. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  5323. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5324. const float d = bq3_K->d;
  5325. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  5326. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5327. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  5328. int u[QR3_K];
  5329. float d8[QR3_K];
  5330. #pragma unroll
  5331. for (int i = 0; i < QR3_K; ++i) {
  5332. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5333. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5334. }
  5335. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  5336. }
  5337. template <int mmq_y>
  5338. static __dpct_inline__ void
  5339. allocate_tiles_q3_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5340. int *tile_x_ql_q3_K, sycl::half2 *tile_x_dm_q3_K,
  5341. int *tile_x_qh_q3_K, int *tile_x_sc_q3_K) {
  5342. *x_ql = tile_x_ql_q3_K;
  5343. *x_dm = tile_x_dm_q3_K;
  5344. *x_qh = tile_x_qh_q3_K;
  5345. *x_sc = tile_x_sc_q3_K;
  5346. }
  5347. template <int mmq_y, int nwarps, bool need_check>
  5348. static __dpct_inline__ void
  5349. load_tiles_q3_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5350. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5351. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5352. const int &k, const int &blocks_per_row) {
  5353. GGML_SYCL_ASSUME(i_offset >= 0);
  5354. GGML_SYCL_ASSUME(i_offset < nwarps);
  5355. GGML_SYCL_ASSUME(k >= 0);
  5356. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5357. const int kbx = k / QI3_K;
  5358. const int kqsx = k % QI3_K;
  5359. const block_q3_K * bx0 = (const block_q3_K *) vx;
  5360. #pragma unroll
  5361. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5362. int i = i0 + i_offset;
  5363. if (need_check) {
  5364. i = sycl::min(i, i_max);
  5365. }
  5366. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  5367. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5368. }
  5369. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  5370. const int kbxd = k % blocks_per_tile_x_row;
  5371. float * x_dmf = (float *) x_dm;
  5372. #pragma unroll
  5373. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  5374. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  5375. if (need_check) {
  5376. i = sycl::min(i, i_max);
  5377. }
  5378. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5379. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  5380. }
  5381. #pragma unroll
  5382. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  5383. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  5384. if (need_check) {
  5385. i = sycl::min(i, i_max);
  5386. }
  5387. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  5388. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5389. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  5390. }
  5391. #pragma unroll
  5392. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5393. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5394. if (need_check) {
  5395. i = sycl::min(i, i_max);
  5396. }
  5397. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  5398. const int ksc = k % (QI3_K/4);
  5399. const int ksc_low = ksc % (QI3_K/8);
  5400. const int shift_low = 4 * (ksc / (QI3_K/8));
  5401. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  5402. const int ksc_high = QI3_K/8;
  5403. const int shift_high = 2 * ksc;
  5404. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  5405. const int sc = dpct::vectorized_binary<sycl::char4>(
  5406. sc_low | sc_high, 0x20202020, dpct::sub_sat());
  5407. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  5408. }
  5409. }
  5410. static __dpct_inline__ float vec_dot_q3_K_q8_1_mul_mat(
  5411. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5412. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5413. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5414. const int &i, const int &j, const int &k) {
  5415. const int kbx = k / QI3_K;
  5416. const int ky = (k % QI3_K) * QR3_K;
  5417. const float * x_dmf = (const float *) x_dm;
  5418. const float * y_df = (const float *) y_ds;
  5419. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  5420. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  5421. #pragma unroll
  5422. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  5423. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  5424. const int shift = 2 * ((ky % 32) / 8);
  5425. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5426. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  5427. const int vlh = (vh << 2) & 0x04040404;
  5428. v[l] = dpct::vectorized_binary<sycl::char4>(vll, vlh, dpct::sub_sat());
  5429. }
  5430. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  5431. 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]);
  5432. }
  5433. static __dpct_inline__ float
  5434. vec_dot_q4_K_q8_1(const void *__restrict__ vbq,
  5435. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5436. #ifndef GGML_QKK_64
  5437. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5438. int v[2];
  5439. int u[2*QR4_K];
  5440. float d8[QR4_K];
  5441. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  5442. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  5443. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  5444. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  5445. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  5446. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  5447. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5448. v[0] = q4[0];
  5449. v[1] = q4[4];
  5450. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  5451. uint16_t aux[2];
  5452. const int j = bq8_offset/2;
  5453. if (j < 2) {
  5454. aux[0] = scales[j+0] & 0x3f3f;
  5455. aux[1] = scales[j+2] & 0x3f3f;
  5456. } else {
  5457. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5458. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5459. }
  5460. const uint8_t * sc = (const uint8_t *)aux;
  5461. const uint8_t * m = sc + 2;
  5462. for (int i = 0; i < QR4_K; ++i) {
  5463. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5464. d8[i] = bq8i->ds[0];
  5465. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5466. u[2*i+0] = q8[0];
  5467. u[2*i+1] = q8[4];
  5468. }
  5469. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  5470. #else
  5471. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  5472. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5473. float sumf_d = 0.0f;
  5474. float sumf_m = 0.0f;
  5475. uint16_t aux16[2];
  5476. const uint8_t * s = (const uint8_t *)aux16;
  5477. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  5478. aux16[0] = a[0] & 0x0f0f;
  5479. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5480. const float dall = bq4_K->dm[0];
  5481. const float dmin = bq4_K->dm[1];
  5482. const float d8_1 = __low2float(bq8_1[0].ds);
  5483. const float d8_2 = __low2float(bq8_1[1].ds);
  5484. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  5485. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  5486. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  5487. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  5488. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  5489. const int v1 = q4[0];
  5490. const int v2 = q4[4];
  5491. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  5492. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  5493. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  5494. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  5495. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  5496. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  5497. return dall * sumf_d - dmin * sumf_m;
  5498. #else
  5499. bad_arch();
  5500. #endif // __SYCL_ARCH__ >= VER_4VEC
  5501. #endif
  5502. }
  5503. template <int mmq_y>
  5504. static __dpct_inline__ void
  5505. allocate_tiles_q4_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5506. int *tile_x_ql_q4_K, sycl::half2 *tile_x_dm_q4_K,
  5507. int *tile_x_sc_q4_K) {
  5508. (void)x_qh;
  5509. *x_ql = tile_x_ql_q4_K;
  5510. *x_dm = tile_x_dm_q4_K;
  5511. *x_sc = tile_x_sc_q4_K;
  5512. }
  5513. template <int mmq_y, int nwarps, bool need_check>
  5514. static __dpct_inline__ void
  5515. load_tiles_q4_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5516. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5517. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5518. const int &k, const int &blocks_per_row) {
  5519. (void)x_qh;
  5520. GGML_SYCL_ASSUME(i_offset >= 0);
  5521. GGML_SYCL_ASSUME(i_offset < nwarps);
  5522. GGML_SYCL_ASSUME(k >= 0);
  5523. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5524. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  5525. const int kqsx = k % QI4_K; // == k if QK_K == 256
  5526. const block_q4_K * bx0 = (const block_q4_K *) vx;
  5527. #pragma unroll
  5528. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5529. int i = i0 + i_offset;
  5530. if (need_check) {
  5531. i = sycl::min(i, i_max);
  5532. }
  5533. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  5534. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5535. }
  5536. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  5537. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5538. #pragma unroll
  5539. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  5540. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  5541. if (need_check) {
  5542. i = sycl::min(i, i_max);
  5543. }
  5544. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5545. #if QK_K == 256
  5546. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  5547. #else
  5548. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  5549. #endif
  5550. }
  5551. #pragma unroll
  5552. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5553. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5554. if (need_check) {
  5555. i = sycl::min(i, i_max);
  5556. }
  5557. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  5558. const int * scales = (const int *) bxi->scales;
  5559. const int ksc = k % (WARP_SIZE/8);
  5560. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5561. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5562. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5563. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5564. }
  5565. }
  5566. static __dpct_inline__ float vec_dot_q4_K_q8_1_mul_mat(
  5567. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5568. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5569. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5570. const int &i, const int &j, const int &k) {
  5571. (void)x_qh;
  5572. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  5573. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  5574. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  5575. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  5576. }
  5577. static __dpct_inline__ float
  5578. vec_dot_q5_K_q8_1(const void *__restrict__ vbq,
  5579. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5580. #ifndef GGML_QKK_64
  5581. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  5582. int vl[2];
  5583. int vh[2];
  5584. int u[2*QR5_K];
  5585. float d8[QR5_K];
  5586. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  5587. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5588. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  5589. vl[0] = ql[0];
  5590. vl[1] = ql[4];
  5591. vh[0] = qh[0] >> bq8_offset;
  5592. vh[1] = qh[4] >> bq8_offset;
  5593. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  5594. uint16_t aux[2];
  5595. const int j = bq8_offset/2;
  5596. if (j < 2) {
  5597. aux[0] = scales[j+0] & 0x3f3f;
  5598. aux[1] = scales[j+2] & 0x3f3f;
  5599. } else {
  5600. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5601. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5602. }
  5603. const uint8_t * sc = (const uint8_t *)aux;
  5604. const uint8_t * m = sc + 2;
  5605. #pragma unroll
  5606. for (int i = 0; i < QR5_K; ++i) {
  5607. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5608. d8[i] = bq8i->ds[0];
  5609. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5610. u[2*i+0] = q8[0];
  5611. u[2*i+1] = q8[4];
  5612. }
  5613. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  5614. #else
  5615. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  5616. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  5617. const int8_t * s = bq5_K->scales;
  5618. const float d = bq5_K->d;
  5619. const float d8_1 = __low2half(bq8_1[0].ds);
  5620. const float d8_2 = __low2half(bq8_1[1].ds);
  5621. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  5622. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  5623. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  5624. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  5625. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  5626. const int vl1 = ql[0];
  5627. const int vl2 = ql[4];
  5628. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  5629. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  5630. const int in = step%8; // 0, 4, 0, 4
  5631. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  5632. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  5633. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  5634. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  5635. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  5636. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  5637. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  5638. return d * sumf_d;
  5639. #else
  5640. bad_arch();
  5641. #endif // __SYCL_ARCH__ >= VER_4VEC
  5642. #endif
  5643. }
  5644. template <int mmq_y>
  5645. static __dpct_inline__ void
  5646. allocate_tiles_q5_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5647. int *tile_x_ql_q5_K, sycl::half2 *tile_x_dm_q5_K,
  5648. int *tile_x_sc_q5_K) {
  5649. (void)x_qh;
  5650. *x_ql = tile_x_ql_q5_K;
  5651. *x_dm = tile_x_dm_q5_K;
  5652. *x_sc = tile_x_sc_q5_K;
  5653. }
  5654. template <int mmq_y, int nwarps, bool need_check>
  5655. static __dpct_inline__ void
  5656. load_tiles_q5_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5657. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5658. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5659. const int &k, const int &blocks_per_row) {
  5660. (void)x_qh;
  5661. GGML_SYCL_ASSUME(i_offset >= 0);
  5662. GGML_SYCL_ASSUME(i_offset < nwarps);
  5663. GGML_SYCL_ASSUME(k >= 0);
  5664. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5665. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  5666. const int kqsx = k % QI5_K; // == k if QK_K == 256
  5667. const block_q5_K * bx0 = (const block_q5_K *) vx;
  5668. #pragma unroll
  5669. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5670. int i = i0 + i_offset;
  5671. if (need_check) {
  5672. i = sycl::min(i, i_max);
  5673. }
  5674. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  5675. const int ky = QR5_K*kqsx;
  5676. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5677. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  5678. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  5679. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  5680. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  5681. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  5682. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  5683. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  5684. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  5685. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  5686. }
  5687. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  5688. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5689. #pragma unroll
  5690. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  5691. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  5692. if (need_check) {
  5693. i = sycl::min(i, i_max);
  5694. }
  5695. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5696. #if QK_K == 256
  5697. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  5698. #endif
  5699. }
  5700. #pragma unroll
  5701. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5702. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5703. if (need_check) {
  5704. i = sycl::min(i, i_max);
  5705. }
  5706. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  5707. const int * scales = (const int *) bxi->scales;
  5708. const int ksc = k % (WARP_SIZE/8);
  5709. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5710. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5711. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5712. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5713. }
  5714. }
  5715. static __dpct_inline__ float vec_dot_q5_K_q8_1_mul_mat(
  5716. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5717. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5718. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5719. const int &i, const int &j, const int &k) {
  5720. (void)x_qh;
  5721. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  5722. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  5723. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  5724. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  5725. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  5726. }
  5727. static __dpct_inline__ float
  5728. vec_dot_q6_K_q8_1(const void *__restrict__ vbq,
  5729. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5730. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  5731. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  5732. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  5733. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  5734. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  5735. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  5736. const int8_t * scales = bq6_K->scales + scale_offset;
  5737. int u[QR6_K];
  5738. float d8[QR6_K];
  5739. #pragma unroll
  5740. for (int i = 0; i < QR6_K; ++i) {
  5741. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  5742. d8[i] = bq8_1[bq8_offset + 2 * i].ds[0];
  5743. }
  5744. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  5745. }
  5746. template <int mmq_y>
  5747. static __dpct_inline__ void
  5748. allocate_tiles_q6_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5749. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_sc) {
  5750. (void)x_qh;
  5751. *x_ql = tile_x_ql;
  5752. *x_dm = tile_x_dm;
  5753. *x_sc = tile_x_sc;
  5754. }
  5755. template <int mmq_y, int nwarps, bool need_check>
  5756. static __dpct_inline__ void
  5757. load_tiles_q6_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5758. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5759. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5760. const int &k, const int &blocks_per_row) {
  5761. (void)x_qh;
  5762. GGML_SYCL_ASSUME(i_offset >= 0);
  5763. GGML_SYCL_ASSUME(i_offset < nwarps);
  5764. GGML_SYCL_ASSUME(k >= 0);
  5765. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5766. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  5767. const int kqsx = k % QI6_K; // == k if QK_K == 256
  5768. const block_q6_K * bx0 = (const block_q6_K *) vx;
  5769. #pragma unroll
  5770. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5771. int i = i0 + i_offset;
  5772. if (need_check) {
  5773. i = sycl::min(i, i_max);
  5774. }
  5775. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  5776. const int ky = QR6_K*kqsx;
  5777. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  5778. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  5779. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  5780. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  5781. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  5782. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  5783. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  5784. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  5785. x_ql[i * (2 * WARP_SIZE + 1) + kq0] =
  5786. dpct::vectorized_binary<sycl::char4>(ql0 | qh0, 0x20202020,
  5787. dpct::sub_sat());
  5788. x_ql[i * (2 * WARP_SIZE + 1) + kq1] =
  5789. dpct::vectorized_binary<sycl::char4>(ql1 | qh1, 0x20202020,
  5790. dpct::sub_sat());
  5791. }
  5792. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  5793. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5794. float * x_dmf = (float *) x_dm;
  5795. #pragma unroll
  5796. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  5797. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  5798. if (need_check) {
  5799. i = sycl::min(i, i_max);
  5800. }
  5801. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5802. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  5803. }
  5804. #pragma unroll
  5805. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5806. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5807. if (need_check) {
  5808. i = sycl::min(i, i_max);
  5809. }
  5810. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  5811. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  5812. }
  5813. }
  5814. static __dpct_inline__ float vec_dot_q6_K_q8_1_mul_mat(
  5815. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5816. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5817. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5818. const int &i, const int &j, const int &k) {
  5819. (void)x_qh;
  5820. const float * x_dmf = (const float *) x_dm;
  5821. const float * y_df = (const float *) y_ds;
  5822. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  5823. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  5824. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  5825. 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]);
  5826. }
  5827. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x,
  5828. int mmq_y, int nwarps, load_tiles_sycl_t load_tiles, int vdr,
  5829. vec_dot_q_mul_mat_sycl_t vec_dot>
  5830. /*
  5831. DPCT1110:8: The total declared local variable size in device function mul_mat_q
  5832. exceeds 128 bytes and may cause high register pressure. Consult with your
  5833. hardware vendor to find the total register size available and adjust the code,
  5834. or use smaller sub-group size to avoid high register pressure.
  5835. */
  5836. static __dpct_inline__ void
  5837. mul_mat_q(const void *__restrict__ vx, const void *__restrict__ vy,
  5838. float *__restrict__ dst, const int ncols_x, const int nrows_x,
  5839. const int ncols_y, const int nrows_y, const int nrows_dst,
  5840. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_qh,
  5841. int *tile_x_sc, const sycl::nd_item<3> &item_ct1, int *tile_y_qs,
  5842. sycl::half2 *tile_y_ds) {
  5843. const block_q_t * x = (const block_q_t *) vx;
  5844. const block_q8_1 * y = (const block_q8_1 *) vy;
  5845. const int blocks_per_row_x = ncols_x / qk;
  5846. const int blocks_per_col_y = nrows_y / QK8_1;
  5847. const int blocks_per_warp = WARP_SIZE / qi;
  5848. const int & ncols_dst = ncols_y;
  5849. const int row_dst_0 = item_ct1.get_group(2) * mmq_y;
  5850. const int & row_x_0 = row_dst_0;
  5851. const int col_dst_0 = item_ct1.get_group(1) * mmq_x;
  5852. const int & col_y_0 = col_dst_0;
  5853. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  5854. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  5855. load_tiles(x + row_x_0 * blocks_per_row_x + ib0, tile_x_ql, tile_x_dm,
  5856. tile_x_qh, tile_x_sc, item_ct1.get_local_id(1),
  5857. nrows_x - row_x_0 - 1, item_ct1.get_local_id(2),
  5858. blocks_per_row_x);
  5859. #pragma unroll
  5860. for (int ir = 0; ir < qr; ++ir) {
  5861. const int kqs = ir * WARP_SIZE + item_ct1.get_local_id(2);
  5862. const int kbxd = kqs / QI8_1;
  5863. #pragma unroll
  5864. for (int i = 0; i < mmq_x; i += nwarps) {
  5865. const int col_y_eff = dpct::min(
  5866. (unsigned int)(col_y_0 + item_ct1.get_local_id(1) + i),
  5867. ncols_y - 1); // to prevent out-of-bounds memory accesses
  5868. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  5869. const int index_y = (item_ct1.get_local_id(1) + i) * WARP_SIZE +
  5870. kqs % WARP_SIZE;
  5871. tile_y_qs[index_y] = get_int_from_int8_aligned(
  5872. by0->qs, item_ct1.get_local_id(2) % QI8_1);
  5873. }
  5874. #pragma unroll
  5875. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  5876. const int ids =
  5877. (ids0 + item_ct1.get_local_id(1) * QI8_1 +
  5878. item_ct1.get_local_id(2) / (WARP_SIZE / QI8_1)) %
  5879. mmq_x;
  5880. const int kby = item_ct1.get_local_id(2) % (WARP_SIZE / QI8_1);
  5881. const int col_y_eff = sycl::min(col_y_0 + ids, ncols_y - 1);
  5882. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  5883. const sycl::half2 *dsi_src =
  5884. &y[col_y_eff * blocks_per_col_y + ib0 * (qk / QK8_1) +
  5885. ir * (WARP_SIZE / QI8_1) + kby]
  5886. .ds;
  5887. sycl::half2 *dsi_dst =
  5888. &tile_y_ds[ids * (WARP_SIZE / QI8_1) + kby];
  5889. if (need_sum) {
  5890. *dsi_dst = *dsi_src;
  5891. } else {
  5892. float * dfi_dst = (float *) dsi_dst;
  5893. *dfi_dst = (*dsi_src)[0];
  5894. }
  5895. }
  5896. /*
  5897. DPCT1118:9: SYCL group functions and algorithms must be encountered
  5898. in converged control flow. You may need to adjust the code.
  5899. */
  5900. /*
  5901. DPCT1065:56: Consider replacing sycl::nd_item::barrier() with
  5902. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  5903. better performance if there is no access to global memory.
  5904. */
  5905. item_ct1.barrier();
  5906. // #pragma unroll // unrolling this loop causes too much register pressure
  5907. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  5908. #pragma unroll
  5909. for (int j = 0; j < mmq_x; j += nwarps) {
  5910. #pragma unroll
  5911. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  5912. sum[i / WARP_SIZE][j / nwarps] += vec_dot(
  5913. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  5914. tile_y_qs, tile_y_ds, item_ct1.get_local_id(2) + i,
  5915. item_ct1.get_local_id(1) + j, k);
  5916. }
  5917. }
  5918. }
  5919. /*
  5920. DPCT1118:10: SYCL group functions and algorithms must be encountered
  5921. in converged control flow. You may need to adjust the code.
  5922. */
  5923. /*
  5924. DPCT1065:57: Consider replacing sycl::nd_item::barrier() with
  5925. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  5926. better performance if there is no access to global memory.
  5927. */
  5928. item_ct1.barrier();
  5929. }
  5930. }
  5931. #pragma unroll
  5932. for (int j = 0; j < mmq_x; j += nwarps) {
  5933. const int col_dst = col_dst_0 + j + item_ct1.get_local_id(1);
  5934. if (col_dst >= ncols_dst) {
  5935. return;
  5936. }
  5937. #pragma unroll
  5938. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  5939. const int row_dst = row_dst_0 + item_ct1.get_local_id(2) + i;
  5940. if (row_dst >= nrows_dst) {
  5941. continue;
  5942. }
  5943. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  5944. }
  5945. }
  5946. }
  5947. #define MMQ_X_Q4_0_RDNA2 64
  5948. #define MMQ_Y_Q4_0_RDNA2 128
  5949. #define NWARPS_Q4_0_RDNA2 8
  5950. #define MMQ_X_Q4_0_RDNA1 64
  5951. #define MMQ_Y_Q4_0_RDNA1 64
  5952. #define NWARPS_Q4_0_RDNA1 8
  5953. #if defined(SYCL_USE_XMX)
  5954. #define MMQ_X_Q4_0_AMPERE 4
  5955. #define MMQ_Y_Q4_0_AMPERE 32
  5956. #define NWARPS_Q4_0_AMPERE 4
  5957. #else
  5958. #define MMQ_X_Q4_0_AMPERE 64
  5959. #define MMQ_Y_Q4_0_AMPERE 128
  5960. #define NWARPS_Q4_0_AMPERE 4
  5961. #endif
  5962. #define MMQ_X_Q4_0_PASCAL 64
  5963. #define MMQ_Y_Q4_0_PASCAL 64
  5964. #define NWARPS_Q4_0_PASCAL 8
  5965. template <bool need_check> static void
  5966. mul_mat_q4_0(
  5967. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  5968. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  5969. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_0, float *tile_x_d_q4_0,
  5970. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  5971. int * tile_x_ql = nullptr;
  5972. sycl::half2 *tile_x_dm = nullptr;
  5973. int * tile_x_qh = nullptr;
  5974. int * tile_x_sc = nullptr;
  5975. //sycl_todo: change according to hardware
  5976. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  5977. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  5978. const int nwarps = NWARPS_Q4_0_AMPERE;
  5979. allocate_tiles_q4_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  5980. tile_x_qs_q4_0, tile_x_d_q4_0);
  5981. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps,
  5982. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ,
  5983. vec_dot_q4_0_q8_1_mul_mat>(
  5984. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  5985. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  5986. }
  5987. #define MMQ_X_Q4_1_RDNA2 64
  5988. #define MMQ_Y_Q4_1_RDNA2 128
  5989. #define NWARPS_Q4_1_RDNA2 8
  5990. #define MMQ_X_Q4_1_RDNA1 64
  5991. #define MMQ_Y_Q4_1_RDNA1 64
  5992. #define NWARPS_Q4_1_RDNA1 8
  5993. #if defined(SYCL_USE_XMX)
  5994. #define MMQ_X_Q4_1_AMPERE 4
  5995. #define MMQ_Y_Q4_1_AMPERE 32
  5996. #define NWARPS_Q4_1_AMPERE 4
  5997. #else
  5998. #define MMQ_X_Q4_1_AMPERE 64
  5999. #define MMQ_Y_Q4_1_AMPERE 128
  6000. #define NWARPS_Q4_1_AMPERE 4
  6001. #endif
  6002. #define MMQ_X_Q4_1_PASCAL 64
  6003. #define MMQ_Y_Q4_1_PASCAL 64
  6004. #define NWARPS_Q4_1_PASCAL 8
  6005. template <bool need_check> static void
  6006. mul_mat_q4_1(
  6007. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6008. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6009. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_1,
  6010. sycl::half2 *tile_x_dm_q4_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6011. int * tile_x_ql = nullptr;
  6012. sycl::half2 *tile_x_dm = nullptr;
  6013. int * tile_x_qh = nullptr;
  6014. int * tile_x_sc = nullptr;
  6015. //sycl_todo: change according to hardware
  6016. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  6017. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  6018. const int nwarps = NWARPS_Q4_1_AMPERE;
  6019. allocate_tiles_q4_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6020. tile_x_qs_q4_1, tile_x_dm_q4_1);
  6021. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps,
  6022. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ,
  6023. vec_dot_q4_1_q8_1_mul_mat>(
  6024. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6025. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6026. }
  6027. #define MMQ_X_Q5_0_RDNA2 64
  6028. #define MMQ_Y_Q5_0_RDNA2 128
  6029. #define NWARPS_Q5_0_RDNA2 8
  6030. #define MMQ_X_Q5_0_RDNA1 64
  6031. #define MMQ_Y_Q5_0_RDNA1 64
  6032. #define NWARPS_Q5_0_RDNA1 8
  6033. #if defined(SYCL_USE_XMX)
  6034. #define MMQ_X_Q5_0_AMPERE 4
  6035. #define MMQ_Y_Q5_0_AMPERE 32
  6036. #define NWARPS_Q5_0_AMPERE 4
  6037. #else
  6038. #define MMQ_X_Q5_0_AMPERE 128
  6039. #define MMQ_Y_Q5_0_AMPERE 64
  6040. #define NWARPS_Q5_0_AMPERE 4
  6041. #endif
  6042. #define MMQ_X_Q5_0_PASCAL 64
  6043. #define MMQ_Y_Q5_0_PASCAL 64
  6044. #define NWARPS_Q5_0_PASCAL 8
  6045. template <bool need_check> static void
  6046. mul_mat_q5_0(
  6047. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6048. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6049. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_0, float *tile_x_d_q5_0,
  6050. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6051. int * tile_x_ql = nullptr;
  6052. sycl::half2 *tile_x_dm = nullptr;
  6053. int * tile_x_qh = nullptr;
  6054. int * tile_x_sc = nullptr;
  6055. //sycl_todo: change according to hardware
  6056. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  6057. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  6058. const int nwarps = NWARPS_Q5_0_AMPERE;
  6059. allocate_tiles_q5_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6060. tile_x_ql_q5_0, tile_x_d_q5_0);
  6061. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps,
  6062. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ,
  6063. vec_dot_q5_0_q8_1_mul_mat>(
  6064. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6065. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6066. }
  6067. #define MMQ_X_Q5_1_RDNA2 64
  6068. #define MMQ_Y_Q5_1_RDNA2 128
  6069. #define NWARPS_Q5_1_RDNA2 8
  6070. #define MMQ_X_Q5_1_RDNA1 64
  6071. #define MMQ_Y_Q5_1_RDNA1 64
  6072. #define NWARPS_Q5_1_RDNA1 8
  6073. #if defined(SYCL_USE_XMX)
  6074. #define MMQ_X_Q5_1_AMPERE 4
  6075. #define MMQ_Y_Q5_1_AMPERE 32
  6076. #define NWARPS_Q5_1_AMPERE 4
  6077. #else
  6078. #define MMQ_X_Q5_1_AMPERE 128
  6079. #define MMQ_Y_Q5_1_AMPERE 64
  6080. #define NWARPS_Q5_1_AMPERE 4
  6081. #endif
  6082. #define MMQ_X_Q5_1_PASCAL 64
  6083. #define MMQ_Y_Q5_1_PASCAL 64
  6084. #define NWARPS_Q5_1_PASCAL 8
  6085. template <bool need_check> static void
  6086. mul_mat_q5_1(
  6087. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6088. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6089. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_1,
  6090. sycl::half2 *tile_x_dm_q5_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6091. int * tile_x_ql = nullptr;
  6092. sycl::half2 *tile_x_dm = nullptr;
  6093. int * tile_x_qh = nullptr;
  6094. int * tile_x_sc = nullptr;
  6095. //sycl_todo: change according to hardware
  6096. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  6097. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  6098. const int nwarps = NWARPS_Q5_1_AMPERE;
  6099. allocate_tiles_q5_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6100. tile_x_ql_q5_1, tile_x_dm_q5_1);
  6101. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps,
  6102. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ,
  6103. vec_dot_q5_1_q8_1_mul_mat>(
  6104. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6105. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6106. }
  6107. #define MMQ_X_Q8_0_RDNA2 64
  6108. #define MMQ_Y_Q8_0_RDNA2 128
  6109. #define NWARPS_Q8_0_RDNA2 8
  6110. #define MMQ_X_Q8_0_RDNA1 64
  6111. #define MMQ_Y_Q8_0_RDNA1 64
  6112. #define NWARPS_Q8_0_RDNA1 8
  6113. #if defined(SYCL_USE_XMX)
  6114. #define MMQ_X_Q8_0_AMPERE 4
  6115. #define MMQ_Y_Q8_0_AMPERE 32
  6116. #define NWARPS_Q8_0_AMPERE 4
  6117. #else
  6118. #define MMQ_X_Q8_0_AMPERE 128
  6119. #define MMQ_Y_Q8_0_AMPERE 64
  6120. #define NWARPS_Q8_0_AMPERE 4
  6121. #endif
  6122. #define MMQ_X_Q8_0_PASCAL 64
  6123. #define MMQ_Y_Q8_0_PASCAL 64
  6124. #define NWARPS_Q8_0_PASCAL 8
  6125. template <bool need_check> static void
  6126. mul_mat_q8_0(
  6127. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6128. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6129. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q8_0, float *tile_x_d_q8_0,
  6130. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6131. int * tile_x_ql = nullptr;
  6132. sycl::half2 *tile_x_dm = nullptr;
  6133. int * tile_x_qh = nullptr;
  6134. int * tile_x_sc = nullptr;
  6135. //sycl_todo: change according to hardware
  6136. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  6137. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  6138. const int nwarps = NWARPS_Q8_0_AMPERE;
  6139. allocate_tiles_q8_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6140. tile_x_qs_q8_0, tile_x_d_q8_0);
  6141. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps,
  6142. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ,
  6143. vec_dot_q8_0_q8_1_mul_mat>(
  6144. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6145. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6146. }
  6147. #define MMQ_X_Q2_K_RDNA2 64
  6148. #define MMQ_Y_Q2_K_RDNA2 128
  6149. #define NWARPS_Q2_K_RDNA2 8
  6150. #define MMQ_X_Q2_K_RDNA1 128
  6151. #define MMQ_Y_Q2_K_RDNA1 32
  6152. #define NWARPS_Q2_K_RDNA1 8
  6153. #if defined(SYCL_USE_XMX)
  6154. #define MMQ_X_Q2_K_AMPERE 4
  6155. #define MMQ_Y_Q2_K_AMPERE 32
  6156. #define NWARPS_Q2_K_AMPERE 4
  6157. #else
  6158. #define MMQ_X_Q2_K_AMPERE 64
  6159. #define MMQ_Y_Q2_K_AMPERE 128
  6160. #define NWARPS_Q2_K_AMPERE 4
  6161. #endif
  6162. #define MMQ_X_Q2_K_PASCAL 64
  6163. #define MMQ_Y_Q2_K_PASCAL 64
  6164. #define NWARPS_Q2_K_PASCAL 8
  6165. template <bool need_check> static void
  6166. mul_mat_q2_K(
  6167. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6168. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6169. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q2_K,
  6170. sycl::half2 *tile_x_dm_q2_K, int *tile_x_sc_q2_K, int *tile_y_qs,
  6171. sycl::half2 *tile_y_ds) {
  6172. int * tile_x_ql = nullptr;
  6173. sycl::half2 *tile_x_dm = nullptr;
  6174. int * tile_x_qh = nullptr;
  6175. int * tile_x_sc = nullptr;
  6176. //sycl_todo: change according to hardware
  6177. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  6178. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  6179. const int nwarps = NWARPS_Q2_K_AMPERE;
  6180. allocate_tiles_q2_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6181. tile_x_ql_q2_K, tile_x_dm_q2_K, tile_x_sc_q2_K);
  6182. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps,
  6183. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ,
  6184. vec_dot_q2_K_q8_1_mul_mat>(
  6185. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6186. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6187. }
  6188. #define MMQ_X_Q3_K_RDNA2 128
  6189. #define MMQ_Y_Q3_K_RDNA2 64
  6190. #define NWARPS_Q3_K_RDNA2 8
  6191. #define MMQ_X_Q3_K_RDNA1 32
  6192. #define MMQ_Y_Q3_K_RDNA1 128
  6193. #define NWARPS_Q3_K_RDNA1 8
  6194. #if defined(SYCL_USE_XMX)
  6195. #define MMQ_X_Q3_K_AMPERE 4
  6196. #define MMQ_Y_Q3_K_AMPERE 32
  6197. #define NWARPS_Q3_K_AMPERE 4
  6198. #else
  6199. #define MMQ_X_Q3_K_AMPERE 128
  6200. #define MMQ_Y_Q3_K_AMPERE 128
  6201. #define NWARPS_Q3_K_AMPERE 4
  6202. #endif
  6203. #define MMQ_X_Q3_K_PASCAL 64
  6204. #define MMQ_Y_Q3_K_PASCAL 64
  6205. #define NWARPS_Q3_K_PASCAL 8
  6206. template <bool need_check> static void
  6207. mul_mat_q3_K(
  6208. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6209. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6210. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q3_K,
  6211. sycl::half2 *tile_x_dm_q3_K, int *tile_x_qh_q3_K, int *tile_x_sc_q3_K,
  6212. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6213. int * tile_x_ql = nullptr;
  6214. sycl::half2 *tile_x_dm = nullptr;
  6215. int * tile_x_qh = nullptr;
  6216. int * tile_x_sc = nullptr;
  6217. //sycl_todo: change according to hardware
  6218. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  6219. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  6220. const int nwarps = NWARPS_Q3_K_AMPERE;
  6221. allocate_tiles_q3_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6222. tile_x_ql_q3_K, tile_x_dm_q3_K, tile_x_qh_q3_K,
  6223. tile_x_sc_q3_K);
  6224. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps,
  6225. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ,
  6226. vec_dot_q3_K_q8_1_mul_mat>(
  6227. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6228. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6229. }
  6230. #define MMQ_X_Q4_K_RDNA2 64
  6231. #define MMQ_Y_Q4_K_RDNA2 128
  6232. #define NWARPS_Q4_K_RDNA2 8
  6233. #define MMQ_X_Q4_K_RDNA1 32
  6234. #define MMQ_Y_Q4_K_RDNA1 64
  6235. #define NWARPS_Q4_K_RDNA1 8
  6236. #if defined(SYCL_USE_XMX)
  6237. #define MMQ_X_Q4_K_AMPERE 4
  6238. #define MMQ_Y_Q4_K_AMPERE 32
  6239. #define NWARPS_Q4_K_AMPERE 4
  6240. #else
  6241. #define MMQ_X_Q4_K_AMPERE 64
  6242. #define MMQ_Y_Q4_K_AMPERE 128
  6243. #define NWARPS_Q4_K_AMPERE 4
  6244. #endif
  6245. #define MMQ_X_Q4_K_PASCAL 64
  6246. #define MMQ_Y_Q4_K_PASCAL 64
  6247. #define NWARPS_Q4_K_PASCAL 8
  6248. template <bool need_check> static void
  6249. mul_mat_q4_K(
  6250. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6251. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6252. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q4_K,
  6253. sycl::half2 *tile_x_dm_q4_K, int *tile_x_sc_q4_K, int *tile_y_qs,
  6254. sycl::half2 *tile_y_ds) {
  6255. int * tile_x_ql = nullptr;
  6256. sycl::half2 *tile_x_dm = nullptr;
  6257. int * tile_x_qh = nullptr;
  6258. int * tile_x_sc = nullptr;
  6259. //sycl_todo: change according to hardware
  6260. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  6261. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  6262. const int nwarps = NWARPS_Q4_K_AMPERE;
  6263. allocate_tiles_q4_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6264. tile_x_ql_q4_K, tile_x_dm_q4_K, tile_x_sc_q4_K);
  6265. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps,
  6266. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ,
  6267. vec_dot_q4_K_q8_1_mul_mat>(
  6268. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6269. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6270. }
  6271. #define MMQ_X_Q5_K_RDNA2 64
  6272. #define MMQ_Y_Q5_K_RDNA2 128
  6273. #define NWARPS_Q5_K_RDNA2 8
  6274. #define MMQ_X_Q5_K_RDNA1 32
  6275. #define MMQ_Y_Q5_K_RDNA1 64
  6276. #define NWARPS_Q5_K_RDNA1 8
  6277. #if defined(SYCL_USE_XMX)
  6278. #define MMQ_X_Q5_K_AMPERE 4
  6279. #define MMQ_Y_Q5_K_AMPERE 32
  6280. #define NWARPS_Q5_K_AMPERE 4
  6281. #else
  6282. #define MMQ_X_Q5_K_AMPERE 64
  6283. #define MMQ_Y_Q5_K_AMPERE 128
  6284. #define NWARPS_Q5_K_AMPERE 4
  6285. #endif
  6286. #define MMQ_X_Q5_K_PASCAL 64
  6287. #define MMQ_Y_Q5_K_PASCAL 64
  6288. #define NWARPS_Q5_K_PASCAL 8
  6289. template <bool need_check> static void
  6290. mul_mat_q5_K(
  6291. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6292. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6293. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_K,
  6294. sycl::half2 *tile_x_dm_q5_K, int *tile_x_sc_q5_K, int *tile_y_qs,
  6295. sycl::half2 *tile_y_ds) {
  6296. int * tile_x_ql = nullptr;
  6297. sycl::half2 *tile_x_dm = nullptr;
  6298. int * tile_x_qh = nullptr;
  6299. int * tile_x_sc = nullptr;
  6300. //sycl_todo: change according to hardware
  6301. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  6302. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  6303. const int nwarps = NWARPS_Q5_K_AMPERE;
  6304. allocate_tiles_q5_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6305. tile_x_ql_q5_K, tile_x_dm_q5_K, tile_x_sc_q5_K);
  6306. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps,
  6307. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ,
  6308. vec_dot_q5_K_q8_1_mul_mat>(
  6309. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6310. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6311. }
  6312. #define MMQ_X_Q6_K_RDNA2 64
  6313. #define MMQ_Y_Q6_K_RDNA2 128
  6314. #define NWARPS_Q6_K_RDNA2 8
  6315. #define MMQ_X_Q6_K_RDNA1 32
  6316. #define MMQ_Y_Q6_K_RDNA1 64
  6317. #define NWARPS_Q6_K_RDNA1 8
  6318. #if defined(SYCL_USE_XMX)
  6319. #define MMQ_X_Q6_K_AMPERE 4
  6320. #define MMQ_Y_Q6_K_AMPERE 32
  6321. #define NWARPS_Q6_K_AMPERE 4
  6322. #else
  6323. #define MMQ_X_Q6_K_AMPERE 64
  6324. #define MMQ_Y_Q6_K_AMPERE 64
  6325. #define NWARPS_Q6_K_AMPERE 4
  6326. #endif
  6327. #define MMQ_X_Q6_K_PASCAL 64
  6328. #define MMQ_Y_Q6_K_PASCAL 64
  6329. #define NWARPS_Q6_K_PASCAL 8
  6330. template <bool need_check> static void
  6331. mul_mat_q6_K(
  6332. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6333. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6334. const sycl::nd_item<3> &item_ct1, int *tile_x_ql, sycl::half2 *tile_x_dm,
  6335. int *tile_x_sc, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6336. // int * tile_x_ql = nullptr;
  6337. // sycl::half2 *tile_x_dm = nullptr;
  6338. int * tile_x_qh = nullptr;
  6339. // int * tile_x_sc = nullptr;
  6340. //sycl_todo: change according to hardware
  6341. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  6342. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  6343. const int nwarps = NWARPS_Q6_K_AMPERE;
  6344. allocate_tiles_q6_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6345. tile_x_ql, tile_x_dm, tile_x_sc);
  6346. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps,
  6347. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ,
  6348. vec_dot_q6_K_q8_1_mul_mat>(
  6349. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6350. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6351. }
  6352. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl>
  6353. static void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  6354. const sycl::nd_item<3> &item_ct1) {
  6355. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6356. item_ct1.get_local_id(1);
  6357. if (row >= nrows) {
  6358. return;
  6359. }
  6360. const int blocks_per_row = ncols / qk;
  6361. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6362. // partial sum for each thread
  6363. float tmp = 0.0f;
  6364. const block_q_t * x = (const block_q_t *) vx;
  6365. const block_q8_1 * y = (const block_q8_1 *) vy;
  6366. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  6367. const int ibx = row * blocks_per_row + i +
  6368. item_ct1.get_local_id(2) / (qi / vdr); // x block index
  6369. const int iby = (i + item_ct1.get_local_id(2) / (qi / vdr)) *
  6370. (qk / QK8_1); // y block index that aligns with ibx
  6371. const int iqs =
  6372. vdr *
  6373. (item_ct1.get_local_id(2) %
  6374. (qi / vdr)); // x block quant index when casting the quants to int
  6375. tmp += vec_dot_q_sycl(&x[ibx], &y[iby], iqs);
  6376. }
  6377. // sum up partial sums and write back result
  6378. #pragma unroll
  6379. for (int mask = 16; mask > 0; mask >>= 1) {
  6380. tmp +=
  6381. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6382. }
  6383. if (item_ct1.get_local_id(2) == 0) {
  6384. dst[row] = tmp;
  6385. }
  6386. }
  6387. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  6388. static void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows,
  6389. const sycl::nd_item<3> &item_ct1) {
  6390. // qk = quantized weights per x block
  6391. // qr = number of quantized weights per data value in x block
  6392. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6393. item_ct1.get_local_id(1);
  6394. if (row >= nrows) {
  6395. return;
  6396. }
  6397. const int tid = item_ct1.get_local_id(2);
  6398. const int iter_stride = 2*GGML_SYCL_DMMV_X;
  6399. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  6400. const int y_offset = qr == 1 ? 1 : qk/2;
  6401. // partial sum for each thread
  6402. #ifdef GGML_SYCL_F16
  6403. sycl::half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  6404. #else
  6405. float tmp = 0.0f;
  6406. #endif // GGML_SYCL_F16
  6407. for (int i = 0; i < ncols; i += iter_stride) {
  6408. const int col = i + vals_per_iter*tid;
  6409. const int ib = (row*ncols + col)/qk; // x block index
  6410. const int iqs = (col%qk)/qr; // x quant index
  6411. const int iybs = col - col%qk; // y block start index
  6412. // processing >2 values per i iter is faster for fast GPUs
  6413. #pragma unroll
  6414. for (int j = 0; j < vals_per_iter; j += 2) {
  6415. // process 2 vals per j iter
  6416. // dequantize
  6417. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  6418. dfloat2 v;
  6419. dequantize_kernel(vx, ib, iqs + j/qr, v);
  6420. // matrix multiplication
  6421. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  6422. #ifdef GGML_SYCL_F16
  6423. dfloat2 t1{y[iybs + iqs + j / qr + 0],
  6424. y[iybs + iqs + j / qr + y_offset]};
  6425. tmp += v * t1;
  6426. #else
  6427. tmp += v.x() * y[iybs + iqs + j / qr + 0];
  6428. tmp += v.y() * y[iybs + iqs + j / qr + y_offset];
  6429. #endif // GGML_SYCL_F16
  6430. }
  6431. }
  6432. // sum up partial sums and write back result
  6433. #pragma unroll
  6434. for (int mask = 16; mask > 0; mask >>= 1) {
  6435. tmp +=
  6436. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6437. }
  6438. if (tid == 0) {
  6439. #ifdef GGML_SYCL_F16
  6440. dst[row] = tmp.x() + tmp.y();
  6441. #else
  6442. dst[row] = tmp;
  6443. #endif // GGML_SYCL_F16
  6444. }
  6445. }
  6446. static void mul_mat_p021_f16_f32(
  6447. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  6448. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y,
  6449. const sycl::nd_item<3> &item_ct1) {
  6450. const sycl::half *x = (const sycl::half *)vx;
  6451. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6452. item_ct1.get_local_id(1);
  6453. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  6454. item_ct1.get_local_id(0);
  6455. const int channel_x = channel / (nchannels_y / nchannels_x);
  6456. const int nrows_y = ncols_x;
  6457. const int nrows_dst = nrows_x;
  6458. const int row_dst = row_x;
  6459. float tmp = 0.0f;
  6460. for (int col_x0 = 0; col_x0 < ncols_x;
  6461. col_x0 += item_ct1.get_local_range(2)) {
  6462. const int col_x = col_x0 + item_ct1.get_local_id(2);
  6463. if (col_x >= ncols_x) {
  6464. break;
  6465. }
  6466. // x is transposed and permuted
  6467. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  6468. const float xi =
  6469. sycl::vec<sycl::half, 1>(x[ix])
  6470. .convert<float, sycl::rounding_mode::automatic>()[0];
  6471. const int row_y = col_x;
  6472. // y is not transposed but permuted
  6473. const int iy = channel*nrows_y + row_y;
  6474. tmp += xi * y[iy];
  6475. }
  6476. // dst is not transposed and not permuted
  6477. const int idst = channel*nrows_dst + row_dst;
  6478. // sum up partial sums and write back result
  6479. #pragma unroll
  6480. for (int mask = 16; mask > 0; mask >>= 1) {
  6481. tmp +=
  6482. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6483. }
  6484. if (item_ct1.get_local_id(2) == 0) {
  6485. dst[idst] = tmp;
  6486. }
  6487. }
  6488. static void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  6489. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  6490. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor,
  6491. const sycl::nd_item<3> &item_ct1) {
  6492. const sycl::half *x = (const sycl::half *)vx;
  6493. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6494. item_ct1.get_local_id(1);
  6495. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  6496. item_ct1.get_local_id(0);
  6497. const int channel_x = channel / channel_x_divisor;
  6498. const int nrows_y = ncols_x;
  6499. const int nrows_dst = nrows_x;
  6500. const int row_dst = row_x;
  6501. const int idst = channel*nrows_dst + row_dst;
  6502. float tmp = 0.0f;
  6503. for (int col_x0 = 0; col_x0 < ncols_x;
  6504. col_x0 += item_ct1.get_local_range(2)) {
  6505. const int col_x = col_x0 + item_ct1.get_local_id(2);
  6506. if (col_x >= ncols_x) {
  6507. break;
  6508. }
  6509. const int row_y = col_x;
  6510. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  6511. const int iy = channel*nrows_y + row_y;
  6512. const float xi =
  6513. sycl::vec<sycl::half, 1>(x[ix])
  6514. .convert<float, sycl::rounding_mode::automatic>()[0];
  6515. tmp += xi * y[iy];
  6516. }
  6517. // sum up partial sums and write back result
  6518. #pragma unroll
  6519. for (int mask = 16; mask > 0; mask >>= 1) {
  6520. tmp +=
  6521. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6522. }
  6523. if (item_ct1.get_local_id(2) == 0) {
  6524. dst[idst] = tmp;
  6525. }
  6526. }
  6527. static void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  6528. const float * xi = (const float *) cxi;
  6529. float * dsti = (float *) cdsti;
  6530. *dsti = *xi;
  6531. }
  6532. static void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  6533. const float * xi = (const float *) cxi;
  6534. sycl::half *dsti = (sycl::half *)cdsti;
  6535. *dsti = sycl::vec<float, 1>(*xi)
  6536. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  6537. }
  6538. static void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  6539. const sycl::half *xi = (const sycl::half *)cxi;
  6540. sycl::half *dsti = (sycl::half *)cdsti;
  6541. *dsti = *xi;
  6542. }
  6543. static void cpy_1_i16_i16(const char * cxi, char * cdsti) {
  6544. const int16_t *xi = (const int16_t *)cxi;
  6545. int16_t *dsti = (int16_t *)cdsti;
  6546. *dsti = *xi;
  6547. }
  6548. static void cpy_1_i32_i32(const char * cxi, char * cdsti) {
  6549. const int32_t *xi = (const int32_t *)cxi;
  6550. int32_t *dsti = (int32_t *)cdsti;
  6551. *dsti = *xi;
  6552. }
  6553. template <cpy_kernel_t cpy_1>
  6554. static void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  6555. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  6556. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12,
  6557. const sycl::nd_item<3> &item_ct1) {
  6558. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6559. item_ct1.get_local_id(2);
  6560. if (i >= ne) {
  6561. return;
  6562. }
  6563. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  6564. // then combine those indices with the corresponding byte offsets to get the total offsets
  6565. const int i02 = i / (ne00*ne01);
  6566. const int i01 = (i - i02*ne01*ne00) / ne00;
  6567. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  6568. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  6569. const int i12 = i / (ne10*ne11);
  6570. const int i11 = (i - i12*ne10*ne11) / ne10;
  6571. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  6572. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  6573. cpy_1(cx + x_offset, cdst + dst_offset);
  6574. }
  6575. static void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  6576. const float * xi = (const float *) cxi;
  6577. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  6578. float amax = 0.0f; // absolute max
  6579. for (int j = 0; j < QK8_0; j++) {
  6580. const float v = xi[j];
  6581. amax = sycl::fmax(amax, sycl::fabs((float)v));
  6582. }
  6583. const float d = amax / ((1 << 7) - 1);
  6584. const float id = d ? 1.0f/d : 0.0f;
  6585. dsti->d = d;
  6586. for (int j = 0; j < QK8_0; ++j) {
  6587. const float x0 = xi[j]*id;
  6588. dsti->qs[j] = sycl::round((float)x0);
  6589. }
  6590. }
  6591. static void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  6592. const float * xi = (const float *) cxi;
  6593. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  6594. float amax = 0.0f;
  6595. float vmax = 0.0f;
  6596. for (int j = 0; j < QK4_0; ++j) {
  6597. const float v = xi[j];
  6598. if (amax < sycl::fabs((float)v)) {
  6599. amax = sycl::fabs((float)v);
  6600. vmax = v;
  6601. }
  6602. }
  6603. const float d = vmax / -8;
  6604. const float id = d ? 1.0f/d : 0.0f;
  6605. dsti->d = d;
  6606. for (int j = 0; j < QK4_0/2; ++j) {
  6607. const float x0 = xi[0 + j]*id;
  6608. const float x1 = xi[QK4_0/2 + j]*id;
  6609. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 8.5f));
  6610. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 8.5f));
  6611. dsti->qs[j] = xi0;
  6612. dsti->qs[j] |= xi1 << 4;
  6613. }
  6614. }
  6615. static void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  6616. const float * xi = (const float *) cxi;
  6617. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  6618. float vmin = FLT_MAX;
  6619. float vmax = -FLT_MAX;
  6620. for (int j = 0; j < QK4_1; ++j) {
  6621. const float v = xi[j];
  6622. if (v < vmin) vmin = v;
  6623. if (v > vmax) vmax = v;
  6624. }
  6625. const float d = (vmax - vmin) / ((1 << 4) - 1);
  6626. const float id = d ? 1.0f/d : 0.0f;
  6627. dsti->dm.x() = d;
  6628. dsti->dm.y() = vmin;
  6629. for (int j = 0; j < QK4_1/2; ++j) {
  6630. const float x0 = (xi[0 + j] - vmin)*id;
  6631. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  6632. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 0.5f));
  6633. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 0.5f));
  6634. dsti->qs[j] = xi0;
  6635. dsti->qs[j] |= xi1 << 4;
  6636. }
  6637. }
  6638. template <cpy_kernel_t cpy_blck, int qk>
  6639. static void cpy_f32_q(const char * cx, char * cdst, const int ne,
  6640. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  6641. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12,
  6642. const sycl::nd_item<3> &item_ct1) {
  6643. const int i = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6644. item_ct1.get_local_id(2)) *
  6645. qk;
  6646. if (i >= ne) {
  6647. return;
  6648. }
  6649. const int i02 = i / (ne00*ne01);
  6650. const int i01 = (i - i02*ne01*ne00) / ne00;
  6651. const int i00 = (i - i02*ne01*ne00 - i01*ne00);
  6652. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  6653. const int i12 = i / (ne10*ne11);
  6654. const int i11 = (i - i12*ne10*ne11) / ne10;
  6655. const int i10 = (i - i12*ne10*ne11 - i11*ne10)/qk;
  6656. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  6657. cpy_blck(cx + x_offset, cdst + dst_offset);
  6658. }
  6659. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  6660. const float y = (i0 / 2 - low) / sycl::max(0.001f, high - low);
  6661. return 1.0f - sycl::min(1.0f, sycl::max(0.0f, y));
  6662. }
  6663. struct rope_corr_dims {
  6664. float v[4];
  6665. };
  6666. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  6667. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  6668. static void rope_yarn(
  6669. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  6670. float * cos_theta, float * sin_theta
  6671. ) {
  6672. // Get n-d rotational scaling corrected for extrapolation
  6673. float theta_interp = freq_scale * theta_extrap;
  6674. float theta = theta_interp;
  6675. if (ext_factor != 0.0f) {
  6676. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  6677. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  6678. // Get n-d magnitude scaling corrected for interpolation
  6679. mscale *= 1.0f + 0.1f * sycl::log(1.0f / freq_scale);
  6680. }
  6681. *cos_theta = sycl::cos(theta) * mscale;
  6682. *sin_theta = sycl::sin(theta) * mscale;
  6683. }
  6684. // rope == RoPE == rotary positional embedding
  6685. template<typename T, bool has_pos>
  6686. static void rope(
  6687. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  6688. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  6689. ,
  6690. const sycl::nd_item<3> &item_ct1) {
  6691. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6692. item_ct1.get_local_id(1));
  6693. if (col >= ncols) {
  6694. return;
  6695. }
  6696. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6697. item_ct1.get_local_id(2);
  6698. const int i = row*ncols + col;
  6699. const int i2 = row/p_delta_rows;
  6700. const int p = has_pos ? pos[i2] : 0;
  6701. const float theta_base = p * dpct::pow(freq_base, -float(col) / ncols);
  6702. float cos_theta, sin_theta;
  6703. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  6704. const float x0 = x[i + 0];
  6705. const float x1 = x[i + 1];
  6706. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  6707. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  6708. }
  6709. template<typename T, bool has_pos>
  6710. static void rope_neox(
  6711. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  6712. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  6713. ,
  6714. const sycl::nd_item<3> &item_ct1) {
  6715. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6716. item_ct1.get_local_id(1));
  6717. if (col >= ncols) {
  6718. return;
  6719. }
  6720. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6721. item_ct1.get_local_id(2);
  6722. const int ib = col / n_dims;
  6723. const int ic = col % n_dims;
  6724. if (ib > 0) {
  6725. const int i = row*ncols + ib*n_dims + ic;
  6726. dst[i + 0] = x[i + 0];
  6727. dst[i + 1] = x[i + 1];
  6728. return;
  6729. }
  6730. const int i = row*ncols + ib*n_dims + ic/2;
  6731. const int i2 = row/p_delta_rows;
  6732. float cur_rot = inv_ndims * ic - ib;
  6733. const int p = has_pos ? pos[i2] : 0;
  6734. const float theta_base =
  6735. p * freq_scale * dpct::pow(theta_scale, col / 2.0f);
  6736. float cos_theta, sin_theta;
  6737. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  6738. const float x0 = x[i + 0];
  6739. const float x1 = x[i + n_dims/2];
  6740. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  6741. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  6742. }
  6743. static void rope_glm_f32(
  6744. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  6745. int n_ctx
  6746. , const sycl::nd_item<3> &item_ct1) {
  6747. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6748. item_ct1.get_local_id(2);
  6749. const int half_n_dims = ncols/4;
  6750. if (col >= half_n_dims) {
  6751. return;
  6752. }
  6753. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6754. item_ct1.get_local_id(1);
  6755. const int i = row*ncols + col;
  6756. const int i2 = row/p_delta_rows;
  6757. const float col_theta_scale = dpct::pow(freq_base, -2.0f * col / ncols);
  6758. // FIXME: this is likely wrong
  6759. const int p = pos != nullptr ? pos[i2] : 0;
  6760. const float theta = sycl::min(p, n_ctx - 2) * freq_scale * col_theta_scale;
  6761. const float sin_theta = sycl::sin((float)theta);
  6762. const float cos_theta = sycl::cos((float)theta);
  6763. const float x0 = x[i + 0];
  6764. const float x1 = x[i + half_n_dims];
  6765. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  6766. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  6767. const float block_theta =
  6768. ((float)sycl::max(p - n_ctx - 2, 0)) * col_theta_scale;
  6769. const float sin_block_theta = sycl::sin((float)block_theta);
  6770. const float cos_block_theta = sycl::cos((float)block_theta);
  6771. const float x2 = x[i + half_n_dims * 2];
  6772. const float x3 = x[i + half_n_dims * 3];
  6773. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  6774. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  6775. }
  6776. static void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  6777. const int n_heads_log2_floor, const float m0, const float m1,
  6778. const sycl::nd_item<3> &item_ct1) {
  6779. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6780. item_ct1.get_local_id(2);
  6781. if (col >= ncols) {
  6782. return;
  6783. }
  6784. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6785. item_ct1.get_local_id(1);
  6786. const int i = row*ncols + col;
  6787. const int k = row/k_rows;
  6788. float m_k;
  6789. if (k < n_heads_log2_floor) {
  6790. m_k = dpct::pow(m0, k + 1);
  6791. } else {
  6792. m_k = dpct::pow(m1, 2 * (k - n_heads_log2_floor) + 1);
  6793. }
  6794. dst[i] = col * m_k + x[i];
  6795. }
  6796. static void k_sum_rows_f32(const float * x, float * dst, const int ncols,
  6797. const sycl::nd_item<3> &item_ct1) {
  6798. const int row = item_ct1.get_group(1);
  6799. const int col = item_ct1.get_local_id(2);
  6800. float sum = 0.0f;
  6801. for (int i = col; i < ncols; i += item_ct1.get_local_range(2)) {
  6802. sum += x[row * ncols + i];
  6803. }
  6804. sum = warp_reduce_sum(sum, item_ct1);
  6805. if (col == 0) {
  6806. dst[row] = sum;
  6807. }
  6808. }
  6809. template<typename T>
  6810. static inline void swap(T & a, T & b) {
  6811. T tmp = a;
  6812. a = b;
  6813. b = tmp;
  6814. }
  6815. template<ggml_sort_order order>
  6816. static void k_argsort_f32_i32(const float * x, int * dst, const int ncols,
  6817. const sycl::nd_item<3> &item_ct1) {
  6818. // bitonic sort
  6819. int col = item_ct1.get_local_id(2);
  6820. int row = item_ct1.get_group(1);
  6821. if (col >= ncols) return;
  6822. const float * x_row = x + row * ncols;
  6823. int * dst_row = dst + row * ncols;
  6824. // initialize indices
  6825. if (col < ncols) {
  6826. dst_row[col] = col;
  6827. }
  6828. /*
  6829. DPCT1065:58: Consider replacing sycl::nd_item::barrier() with
  6830. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better
  6831. performance if there is no access to global memory.
  6832. */
  6833. item_ct1.barrier();
  6834. for (int k = 2; k <= ncols; k *= 2) {
  6835. for (int j = k / 2; j > 0; j /= 2) {
  6836. int ixj = col ^ j;
  6837. if (ixj > col) {
  6838. if ((col & k) == 0) {
  6839. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  6840. swap(dst_row[col], dst_row[ixj]);
  6841. }
  6842. } else {
  6843. if (order == GGML_SORT_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  6844. swap(dst_row[col], dst_row[ixj]);
  6845. }
  6846. }
  6847. }
  6848. /*
  6849. DPCT1118:11: SYCL group functions and algorithms must be encountered
  6850. in converged control flow. You may need to adjust the code.
  6851. */
  6852. /*
  6853. DPCT1065:59: Consider replacing sycl::nd_item::barrier() with
  6854. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6855. better performance if there is no access to global memory.
  6856. */
  6857. item_ct1.barrier();
  6858. }
  6859. }
  6860. }
  6861. static void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past,
  6862. const sycl::nd_item<3> &item_ct1) {
  6863. const int col = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6864. item_ct1.get_local_id(1);
  6865. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6866. item_ct1.get_local_id(2);
  6867. if (col >= ncols) {
  6868. return;
  6869. }
  6870. const int i = row*ncols + col;
  6871. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  6872. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  6873. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  6874. }
  6875. static void soft_max_f32(const float * x, const float * y, float * dst, const int ncols, const int nrows_y, const float scale,
  6876. const sycl::nd_item<3> &item_ct1, float *buf) {
  6877. const int tid = item_ct1.get_local_id(2);
  6878. const int rowx = item_ct1.get_group(2);
  6879. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  6880. const int block_size = item_ct1.get_local_range(2);
  6881. const int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  6882. const int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  6883. float max_val = -INFINITY;
  6884. for (int col = tid; col < ncols; col += block_size) {
  6885. const int ix = rowx*ncols + col;
  6886. const int iy = rowy*ncols + col;
  6887. max_val = sycl::max(max_val, x[ix] * scale + (y ? y[iy] : 0.0f));
  6888. }
  6889. // find the max value in the block
  6890. max_val = warp_reduce_max(max_val, item_ct1);
  6891. if (block_size > WARP_SIZE) {
  6892. if (warp_id == 0) {
  6893. buf[lane_id] = -INFINITY;
  6894. }
  6895. /*
  6896. DPCT1118:12: SYCL group functions and algorithms must be encountered in
  6897. converged control flow. You may need to adjust the code.
  6898. */
  6899. /*
  6900. DPCT1065:60: Consider replacing sycl::nd_item::barrier() with
  6901. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6902. better performance if there is no access to global memory.
  6903. */
  6904. item_ct1.barrier();
  6905. if (lane_id == 0) {
  6906. buf[warp_id] = max_val;
  6907. }
  6908. /*
  6909. DPCT1118:13: SYCL group functions and algorithms must be encountered in
  6910. converged control flow. You may need to adjust the code.
  6911. */
  6912. /*
  6913. DPCT1065:61: Consider replacing sycl::nd_item::barrier() with
  6914. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6915. better performance if there is no access to global memory.
  6916. */
  6917. item_ct1.barrier();
  6918. max_val = buf[lane_id];
  6919. max_val = warp_reduce_max(max_val, item_ct1);
  6920. }
  6921. float tmp = 0.f;
  6922. for (int col = tid; col < ncols; col += block_size) {
  6923. const int ix = rowx*ncols + col;
  6924. const int iy = rowy*ncols + col;
  6925. const float val =
  6926. sycl::native::exp((x[ix] * scale + (y ? y[iy] : 0.0f)) - max_val);
  6927. tmp += val;
  6928. dst[ix] = val;
  6929. }
  6930. // find the sum of exps in the block
  6931. tmp = warp_reduce_sum(tmp, item_ct1);
  6932. if (block_size > WARP_SIZE) {
  6933. if (warp_id == 0) {
  6934. buf[lane_id] = 0.f;
  6935. }
  6936. /*
  6937. DPCT1118:14: SYCL group functions and algorithms must be encountered in
  6938. converged control flow. You may need to adjust the code.
  6939. */
  6940. /*
  6941. DPCT1065:62: Consider replacing sycl::nd_item::barrier() with
  6942. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6943. better performance if there is no access to global memory.
  6944. */
  6945. item_ct1.barrier();
  6946. if (lane_id == 0) {
  6947. buf[warp_id] = tmp;
  6948. }
  6949. /*
  6950. DPCT1118:15: SYCL group functions and algorithms must be encountered in
  6951. converged control flow. You may need to adjust the code.
  6952. */
  6953. /*
  6954. DPCT1065:63: Consider replacing sycl::nd_item::barrier() with
  6955. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6956. better performance if there is no access to global memory.
  6957. */
  6958. item_ct1.barrier();
  6959. tmp = buf[lane_id];
  6960. tmp = warp_reduce_sum(tmp, item_ct1);
  6961. }
  6962. const float inv_tmp = 1.f / tmp;
  6963. for (int col = tid; col < ncols; col += block_size) {
  6964. const int i = rowx*ncols + col;
  6965. dst[i] *= inv_tmp;
  6966. }
  6967. }
  6968. static void scale_f32(const float * x, float * dst, const float scale, const int k,
  6969. const sycl::nd_item<3> &item_ct1) {
  6970. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6971. item_ct1.get_local_id(2);
  6972. if (i >= k) {
  6973. return;
  6974. }
  6975. dst[i] = scale * x[i];
  6976. }
  6977. static void clamp_f32(const float * x, float * dst, const float min, const float max, const int k,
  6978. const sycl::nd_item<3> &item_ct1) {
  6979. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6980. item_ct1.get_local_id(2);
  6981. if (i >= k) {
  6982. return;
  6983. }
  6984. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  6985. }
  6986. static void im2col_f32_f16(const float *x, sycl::half *dst, int offset_delta,
  6987. int IW, int IH, int OW, int KW, int KH,
  6988. int pelements, int CHW, int s0, int s1, int p0,
  6989. int p1, int d0, int d1,
  6990. const sycl::nd_item<3> &item_ct1) {
  6991. const int i = item_ct1.get_local_id(2) +
  6992. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  6993. if (i >= pelements) {
  6994. return;
  6995. }
  6996. const int ksize = OW * (KH > 1 ? KW : 1);
  6997. const int kx = i / ksize;
  6998. const int kd = kx * ksize;
  6999. const int ky = (i - kd) / OW;
  7000. const int ix = i % OW;
  7001. const int64_t iiw = ix * s0 + kx * d0 - p0;
  7002. const int64_t iih = item_ct1.get_group(1) * s1 + ky * d1 - p1;
  7003. const int64_t offset_dst =
  7004. (item_ct1.get_group(1) * OW + ix) * CHW +
  7005. (item_ct1.get_group(0) * (KW * KH) + ky * KW + kx);
  7006. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  7007. dst[offset_dst] =
  7008. sycl::vec<float, 1>(0.0f)
  7009. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7010. } else {
  7011. const int64_t offset_src = item_ct1.get_group(0) * offset_delta;
  7012. dst[offset_dst] =
  7013. sycl::vec<float, 1>(x[offset_src + iih * IW + iiw])
  7014. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7015. }
  7016. }
  7017. template <int qk, int qr, dequantize_kernel_t dq>
  7018. static void get_rows_sycl(const ggml_tensor *src0, const ggml_tensor *src1,
  7019. ggml_tensor *dst, const void *src0_dd,
  7020. const int32_t *src1_dd, float *dst_dd,
  7021. dpct::queue_ptr stream) {
  7022. GGML_TENSOR_BINARY_OP_LOCALS
  7023. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  7024. const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
  7025. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  7026. // strides in elements
  7027. //const size_t s0 = nb0 / ggml_element_size(dst);
  7028. const size_t s1 = nb1 / ggml_element_size(dst);
  7029. const size_t s2 = nb2 / ggml_element_size(dst);
  7030. const size_t s3 = nb3 / ggml_element_size(dst);
  7031. const size_t s10 = nb10 / ggml_element_size(src1);
  7032. const size_t s11 = nb11 / ggml_element_size(src1);
  7033. const size_t s12 = nb12 / ggml_element_size(src1);
  7034. //const size_t s13 = nb13 / ggml_element_size(src1);
  7035. GGML_ASSERT(ne00 % 2 == 0);
  7036. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7037. [=](sycl::nd_item<3> item_ct1) {
  7038. k_get_rows<qk, qr, dq>(
  7039. src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  7040. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  7041. });
  7042. (void) dst;
  7043. }
  7044. template <typename src0_t>
  7045. static void get_rows_sycl_float(const ggml_tensor *src0,
  7046. const ggml_tensor *src1, ggml_tensor *dst,
  7047. const src0_t *src0_dd, const int32_t *src1_dd,
  7048. float *dst_dd, dpct::queue_ptr stream) {
  7049. GGML_TENSOR_BINARY_OP_LOCALS
  7050. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  7051. const int block_num_x = (ne00 + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
  7052. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  7053. // strides in elements
  7054. //const size_t s0 = nb0 / ggml_element_size(dst);
  7055. const size_t s1 = nb1 / ggml_element_size(dst);
  7056. const size_t s2 = nb2 / ggml_element_size(dst);
  7057. const size_t s3 = nb3 / ggml_element_size(dst);
  7058. const size_t s10 = nb10 / ggml_element_size(src1);
  7059. const size_t s11 = nb11 / ggml_element_size(src1);
  7060. const size_t s12 = nb12 / ggml_element_size(src1);
  7061. //const size_t s13 = nb13 / ggml_element_size(src1);
  7062. {
  7063. dpct::has_capability_or_fail(stream->get_device(),
  7064. {sycl::aspect::fp16});
  7065. stream->parallel_for(
  7066. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7067. [=](sycl::nd_item<3> item_ct1) {
  7068. k_get_rows_float(src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  7069. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  7070. });
  7071. }
  7072. (void) dst;
  7073. }
  7074. template<float (*bin_op)(const float, const float)>
  7075. struct bin_bcast_sycl {
  7076. template <typename src0_t, typename src1_t, typename dst_t>
  7077. void operator()(const struct ggml_tensor *src0,
  7078. const struct ggml_tensor *src1, struct ggml_tensor *dst,
  7079. const src0_t *src0_dd, const src1_t *src1_dd, dst_t *dst_dd,
  7080. dpct::queue_ptr stream) {
  7081. GGML_TENSOR_BINARY_OP_LOCALS
  7082. int nr0 = ne10/ne0;
  7083. int nr1 = ne11/ne1;
  7084. int nr2 = ne12/ne2;
  7085. int nr3 = ne13/ne3;
  7086. int nr[4] = { nr0, nr1, nr2, nr3 };
  7087. // collapse dimensions until first broadcast dimension
  7088. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  7089. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  7090. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  7091. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  7092. auto collapse = [](int64_t cne[]) {
  7093. cne[0] *= cne[1];
  7094. cne[1] = cne[2];
  7095. cne[2] = cne[3];
  7096. cne[3] = 1;
  7097. };
  7098. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  7099. cnb[1] *= cne[1];
  7100. cnb[2] *= cne[2];
  7101. cnb[3] *= cne[3];
  7102. };
  7103. for (int i = 0; i < 4; i++) {
  7104. if (nr[i] != 1) {
  7105. break;
  7106. }
  7107. if (i > 0) {
  7108. collapse_nb(cnb0, cne0);
  7109. collapse_nb(cnb1, cne1);
  7110. collapse(cne0);
  7111. collapse(cne1);
  7112. }
  7113. }
  7114. {
  7115. int64_t ne0 = cne0[0];
  7116. int64_t ne1 = cne0[1];
  7117. int64_t ne2 = cne0[2];
  7118. int64_t ne3 = cne0[3];
  7119. int64_t ne10 = cne1[0];
  7120. int64_t ne11 = cne1[1];
  7121. int64_t ne12 = cne1[2];
  7122. int64_t ne13 = cne1[3];
  7123. size_t nb0 = cnb0[0];
  7124. size_t nb1 = cnb0[1];
  7125. size_t nb2 = cnb0[2];
  7126. size_t nb3 = cnb0[3];
  7127. size_t nb10 = cnb1[0];
  7128. size_t nb11 = cnb1[1];
  7129. size_t nb12 = cnb1[2];
  7130. size_t nb13 = cnb1[3];
  7131. size_t s0 = nb0 / sizeof(dst_t);
  7132. size_t s1 = nb1 / sizeof(dst_t);
  7133. size_t s2 = nb2 / sizeof(dst_t);
  7134. size_t s3 = nb3 / sizeof(dst_t);
  7135. size_t s10 = nb10 / sizeof(src1_t);
  7136. size_t s11 = nb11 / sizeof(src1_t);
  7137. size_t s12 = nb12 / sizeof(src1_t);
  7138. size_t s13 = nb13 / sizeof(src1_t);
  7139. GGML_ASSERT(s0 == 1);
  7140. GGML_ASSERT(s10 == 1);
  7141. const int block_size = 128;
  7142. int64_t hne0 = std::max(ne0/2LL, 1LL);
  7143. sycl::range<3> block_dims(1, 1, 1);
  7144. block_dims[2] = std::min<unsigned int>(hne0, block_size);
  7145. block_dims[1] = std::min<unsigned int>(
  7146. ne1, block_size / (unsigned int)block_dims[2]);
  7147. block_dims[0] = std::min(
  7148. std::min<unsigned int>(
  7149. ne2 * ne3, block_size / (unsigned int)block_dims[2] /
  7150. (unsigned int)block_dims[1]),
  7151. 64U);
  7152. sycl::range<3> block_nums(
  7153. (ne2 * ne3 + block_dims[0] - 1) / block_dims[0],
  7154. (ne1 + block_dims[1] - 1) / block_dims[1],
  7155. (hne0 + block_dims[2] - 1) / block_dims[2]);
  7156. if (block_nums[0] > 65535) {
  7157. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  7158. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  7159. {
  7160. dpct::has_capability_or_fail(stream->get_device(),
  7161. {sycl::aspect::fp16});
  7162. stream->parallel_for(
  7163. sycl::nd_range<3>(sycl::range<3>(1, 1, block_num) *
  7164. sycl::range<3>(1, 1, block_size),
  7165. sycl::range<3>(1, 1, block_size)),
  7166. [=](sycl::nd_item<3> item_ct1) {
  7167. k_bin_bcast_unravel<bin_op>(
  7168. src0_dd, src1_dd, dst_dd, ne0, ne1, ne2, ne3,
  7169. ne10, ne11, ne12, ne13, s1, s2, s3, s11, s12,
  7170. s13, item_ct1);
  7171. });
  7172. }
  7173. } else {
  7174. /*
  7175. DPCT1049:16: The work-group size passed to the SYCL kernel may
  7176. exceed the limit. To get the device limit, query
  7177. info::device::max_work_group_size. Adjust the work-group size if
  7178. needed.
  7179. */
  7180. dpct::has_capability_or_fail(stream->get_device(),
  7181. {sycl::aspect::fp16});
  7182. stream->parallel_for(
  7183. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7184. [=](sycl::nd_item<3> item_ct1) {
  7185. k_bin_bcast<bin_op>(src0_dd, src1_dd, dst_dd, ne0, ne1,
  7186. ne2, ne3, ne10, ne11, ne12, ne13,
  7187. s1, s2, s3, s11, s12, s13,
  7188. item_ct1);
  7189. });
  7190. }
  7191. }
  7192. }
  7193. };
  7194. static void acc_f32_sycl(const float *x, const float *y, float *dst,
  7195. const int n_elements, const int ne10, const int ne11,
  7196. const int ne12, const int nb1, const int nb2,
  7197. const int offset, dpct::queue_ptr stream) {
  7198. int num_blocks = (n_elements + SYCL_ACC_BLOCK_SIZE - 1) / SYCL_ACC_BLOCK_SIZE;
  7199. stream->parallel_for(
  7200. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7201. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE),
  7202. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE)),
  7203. [=](sycl::nd_item<3> item_ct1) {
  7204. acc_f32(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset,
  7205. item_ct1);
  7206. });
  7207. }
  7208. static void gelu_f32_sycl(const float *x, float *dst, const int k,
  7209. dpct::queue_ptr stream) {
  7210. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  7211. stream->parallel_for(
  7212. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7213. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  7214. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  7215. [=](sycl::nd_item<3> item_ct1) {
  7216. gelu_f32(x, dst, k, item_ct1);
  7217. });
  7218. }
  7219. static void silu_f32_sycl(const float *x, float *dst, const int k,
  7220. dpct::queue_ptr stream) {
  7221. const int num_blocks = (k + SYCL_SILU_BLOCK_SIZE - 1) / SYCL_SILU_BLOCK_SIZE;
  7222. stream->parallel_for(
  7223. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7224. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE),
  7225. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE)),
  7226. [=](sycl::nd_item<3> item_ct1) {
  7227. silu_f32(x, dst, k, item_ct1);
  7228. });
  7229. }
  7230. static void gelu_quick_f32_sycl(const float *x, float *dst, const int k,
  7231. dpct::queue_ptr stream) {
  7232. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  7233. stream->parallel_for(
  7234. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7235. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  7236. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  7237. [=](sycl::nd_item<3> item_ct1) {
  7238. gelu_quick_f32(x, dst, k, item_ct1);
  7239. });
  7240. }
  7241. static void tanh_f32_sycl(const float *x, float *dst, const int k,
  7242. dpct::queue_ptr stream) {
  7243. const int num_blocks = (k + SYCL_TANH_BLOCK_SIZE - 1) / SYCL_TANH_BLOCK_SIZE;
  7244. stream->parallel_for(
  7245. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7246. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE),
  7247. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE)),
  7248. [=](sycl::nd_item<3> item_ct1) {
  7249. tanh_f32(x, dst, k, item_ct1);
  7250. });
  7251. }
  7252. static void relu_f32_sycl(const float *x, float *dst, const int k,
  7253. dpct::queue_ptr stream) {
  7254. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  7255. stream->parallel_for(
  7256. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7257. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  7258. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  7259. [=](sycl::nd_item<3> item_ct1) {
  7260. relu_f32(x, dst, k, item_ct1);
  7261. });
  7262. }
  7263. static void leaky_relu_f32_sycl(const float *x, float *dst, const int k,
  7264. const float negative_slope,
  7265. dpct::queue_ptr stream) {
  7266. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  7267. stream->parallel_for(
  7268. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7269. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  7270. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  7271. [=](sycl::nd_item<3> item_ct1) {
  7272. leaky_relu_f32(x, dst, k, negative_slope, item_ct1);
  7273. });
  7274. }
  7275. static void sqr_f32_sycl(const float *x, float *dst, const int k,
  7276. dpct::queue_ptr stream) {
  7277. const int num_blocks = (k + SYCL_SQR_BLOCK_SIZE - 1) / SYCL_SQR_BLOCK_SIZE;
  7278. stream->parallel_for(
  7279. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7280. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE),
  7281. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE)),
  7282. [=](sycl::nd_item<3> item_ct1) {
  7283. sqr_f32(x, dst, k, item_ct1);
  7284. });
  7285. }
  7286. static void norm_f32_sycl(const float *x, float *dst, const int ncols,
  7287. const int nrows, const float eps,
  7288. dpct::queue_ptr stream) {
  7289. GGML_ASSERT(ncols % WARP_SIZE == 0);
  7290. if (ncols < 1024) {
  7291. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  7292. stream->submit([&](sycl::handler &cgh) {
  7293. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  7294. sycl::range<1>(32), cgh);
  7295. cgh.parallel_for(
  7296. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  7297. block_dims),
  7298. [=](sycl::nd_item<3> item_ct1)
  7299. [[intel::reqd_sub_group_size(32)]] {
  7300. norm_f32(x, dst, ncols, eps, item_ct1,
  7301. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  7302. });
  7303. });
  7304. } else {
  7305. const int work_group_size = g_work_group_size;
  7306. const sycl::range<3> block_dims(1, 1, work_group_size);
  7307. /*
  7308. DPCT1049:17: The work-group size passed to the SYCL kernel may exceed
  7309. the limit. To get the device limit, query
  7310. info::device::max_work_group_size. Adjust the work-group size if needed.
  7311. */
  7312. stream->submit([&](sycl::handler &cgh) {
  7313. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  7314. sycl::range<1>(32), cgh);
  7315. cgh.parallel_for(
  7316. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  7317. block_dims),
  7318. [=](sycl::nd_item<3> item_ct1)
  7319. [[intel::reqd_sub_group_size(32)]] {
  7320. norm_f32(x, dst, ncols, eps, item_ct1,
  7321. s_sum_acc_ct1.get_pointer(), work_group_size);
  7322. });
  7323. });
  7324. }
  7325. }
  7326. static void group_norm_f32_sycl(const float *x, float *dst,
  7327. const int num_groups, const int group_size,
  7328. const int ne_elements, dpct::queue_ptr stream) {
  7329. static const float eps = 1e-6f;
  7330. if (group_size < 1024) {
  7331. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  7332. stream->submit([&](sycl::handler &cgh) {
  7333. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  7334. cgh);
  7335. const float eps_ct4 = eps;
  7336. cgh.parallel_for(
  7337. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  7338. block_dims),
  7339. [=](sycl::nd_item<3> item_ct1)
  7340. [[intel::reqd_sub_group_size(32)]] {
  7341. group_norm_f32(
  7342. x, dst, group_size, ne_elements, eps_ct4, item_ct1,
  7343. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  7344. });
  7345. });
  7346. } else {
  7347. const int work_group_size = g_work_group_size;
  7348. const sycl::range<3> block_dims(1, 1, work_group_size);
  7349. /*
  7350. DPCT1049:18: The work-group size passed to the SYCL kernel may exceed
  7351. the limit. To get the device limit, query
  7352. info::device::max_work_group_size. Adjust the work-group size if needed.
  7353. */
  7354. stream->submit([&](sycl::handler &cgh) {
  7355. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  7356. cgh);
  7357. const float eps_ct4 = eps;
  7358. cgh.parallel_for(
  7359. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  7360. block_dims),
  7361. [=](sycl::nd_item<3> item_ct1)
  7362. [[intel::reqd_sub_group_size(32)]] {
  7363. group_norm_f32(x, dst, group_size, ne_elements,
  7364. eps_ct4, item_ct1,
  7365. s_sum_acc_ct1.get_pointer(), work_group_size);
  7366. });
  7367. });
  7368. }
  7369. }
  7370. static void concat_f32_sycl(const float *x, const float *y, float *dst,
  7371. const int ne0, int ne1, int ne2, int ne02,
  7372. dpct::queue_ptr stream) {
  7373. int num_blocks = (ne0 + SYCL_CONCAT_BLOCK_SIZE - 1) / SYCL_CONCAT_BLOCK_SIZE;
  7374. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  7375. stream->parallel_for(
  7376. sycl::nd_range<3>(gridDim *
  7377. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE),
  7378. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE)),
  7379. [=](sycl::nd_item<3> item_ct1) {
  7380. concat_f32(x, y, dst, ne0, ne02, item_ct1);
  7381. });
  7382. }
  7383. static void upscale_f32_sycl(const float *x, float *dst, const int ne00,
  7384. const int ne01, const int ne02,
  7385. const int scale_factor, dpct::queue_ptr stream) {
  7386. int ne0 = (ne00 * scale_factor);
  7387. int num_blocks = (ne0 + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE;
  7388. sycl::range<3> gridDim(ne02, (ne01 * scale_factor), num_blocks);
  7389. stream->parallel_for(
  7390. sycl::nd_range<3>(gridDim *
  7391. sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE),
  7392. sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE)),
  7393. [=](sycl::nd_item<3> item_ct1) {
  7394. upscale_f32(x, dst, ne00, ne00 * ne01, scale_factor, item_ct1);
  7395. });
  7396. }
  7397. static void pad_f32_sycl(const float *x, float *dst, const int ne00,
  7398. const int ne01, const int ne02, const int ne0,
  7399. const int ne1, const int ne2, dpct::queue_ptr stream) {
  7400. int num_blocks = (ne0 + SYCL_PAD_BLOCK_SIZE - 1) / SYCL_PAD_BLOCK_SIZE;
  7401. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  7402. stream->parallel_for(
  7403. sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE),
  7404. sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE)),
  7405. [=](sycl::nd_item<3> item_ct1) {
  7406. pad_f32(x, dst, ne0, ne00, ne01, ne02, item_ct1);
  7407. });
  7408. }
  7409. static void rms_norm_f32_sycl(const float *x, float *dst, const int ncols,
  7410. const int nrows, const float eps,
  7411. dpct::queue_ptr stream) {
  7412. GGML_ASSERT(ncols % WARP_SIZE == 0);
  7413. // printf("%s ncols=%d, nrows=%d, WARP_SIZE=%d\n", __func__, ncols, nrows, WARP_SIZE);
  7414. if (ncols < 1024) {
  7415. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  7416. stream->submit([&](sycl::handler &cgh) {
  7417. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  7418. cgh);
  7419. cgh.parallel_for(
  7420. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  7421. block_dims),
  7422. [=](sycl::nd_item<3> item_ct1)
  7423. [[intel::reqd_sub_group_size(32)]] {
  7424. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  7425. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  7426. });
  7427. });
  7428. } else {
  7429. const int work_group_size = g_work_group_size;
  7430. const sycl::range<3> block_dims(1, 1, work_group_size);
  7431. /*
  7432. DPCT1049:19: The work-group size passed to the SYCL kernel may exceed
  7433. the limit. To get the device limit, query
  7434. info::device::max_work_group_size. Adjust the work-group size if needed.
  7435. */
  7436. stream->submit([&](sycl::handler &cgh) {
  7437. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  7438. cgh);
  7439. cgh.parallel_for(
  7440. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  7441. block_dims),
  7442. [=](sycl::nd_item<3> item_ct1)
  7443. [[intel::reqd_sub_group_size(32)]] {
  7444. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  7445. s_sum_acc_ct1.get_pointer(), work_group_size);
  7446. });
  7447. });
  7448. }
  7449. }
  7450. static void quantize_row_q8_1_sycl(const float *x, void *vy, const int kx,
  7451. const int ky, const int kx_padded,
  7452. dpct::queue_ptr stream) {
  7453. const int block_num_x = (kx_padded + SYCL_QUANTIZE_BLOCK_SIZE - 1) / SYCL_QUANTIZE_BLOCK_SIZE;
  7454. const sycl::range<3> num_blocks(1, ky, block_num_x);
  7455. const sycl::range<3> block_size(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE);
  7456. {
  7457. dpct::has_capability_or_fail(stream->get_device(),
  7458. {sycl::aspect::fp16});
  7459. stream->parallel_for(
  7460. sycl::nd_range<3>(num_blocks * block_size, block_size),
  7461. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7462. quantize_q8_1(x, vy, kx, kx_padded, item_ct1);
  7463. });
  7464. }
  7465. }
  7466. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  7467. static void dequantize_block_sycl(const void *__restrict__ vx,
  7468. dst_t *__restrict__ y, const int k,
  7469. dpct::queue_ptr stream) {
  7470. const int num_blocks = (k + SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / SYCL_DEQUANTIZE_BLOCK_SIZE;
  7471. {
  7472. dpct::has_capability_or_fail(stream->get_device(),
  7473. {sycl::aspect::fp16});
  7474. stream->parallel_for(
  7475. sycl::nd_range<3>(
  7476. sycl::range<3>(1, 1, num_blocks) *
  7477. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  7478. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  7479. [=](sycl::nd_item<3> item_ct1) {
  7480. dequantize_block<qk, qr, dequantize_kernel>(vx, y, k, item_ct1);
  7481. });
  7482. }
  7483. }
  7484. template <typename dst_t>
  7485. static void dequantize_row_q2_K_sycl(const void *vx, dst_t *y, const int k,
  7486. dpct::queue_ptr stream) {
  7487. const int nb = k / QK_K;
  7488. #if QK_K == 256
  7489. {
  7490. dpct::has_capability_or_fail(stream->get_device(),
  7491. {sycl::aspect::fp16});
  7492. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7493. sycl::range<3>(1, 1, 64),
  7494. sycl::range<3>(1, 1, 64)),
  7495. [=](sycl::nd_item<3> item_ct1) {
  7496. dequantize_block_q2_K(vx, y, item_ct1);
  7497. });
  7498. }
  7499. #else
  7500. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  7501. #endif
  7502. }
  7503. template <typename dst_t>
  7504. static void dequantize_row_q3_K_sycl(const void *vx, dst_t *y, const int k,
  7505. dpct::queue_ptr stream) {
  7506. const int nb = k / QK_K;
  7507. #if QK_K == 256
  7508. {
  7509. dpct::has_capability_or_fail(stream->get_device(),
  7510. {sycl::aspect::fp16});
  7511. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7512. sycl::range<3>(1, 1, 64),
  7513. sycl::range<3>(1, 1, 64)),
  7514. [=](sycl::nd_item<3> item_ct1) {
  7515. dequantize_block_q3_K(vx, y, item_ct1);
  7516. });
  7517. }
  7518. #else
  7519. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  7520. #endif
  7521. }
  7522. template <typename dst_t>
  7523. static void dequantize_row_q4_K_sycl(const void *vx, dst_t *y, const int k,
  7524. dpct::queue_ptr stream) {
  7525. const int nb = k / QK_K;
  7526. {
  7527. dpct::has_capability_or_fail(stream->get_device(),
  7528. {sycl::aspect::fp16});
  7529. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7530. sycl::range<3>(1, 1, 32),
  7531. sycl::range<3>(1, 1, 32)),
  7532. [=](sycl::nd_item<3> item_ct1) {
  7533. dequantize_block_q4_K(vx, y, item_ct1);
  7534. });
  7535. }
  7536. }
  7537. template <typename dst_t>
  7538. static void dequantize_row_q5_K_sycl(const void *vx, dst_t *y, const int k,
  7539. dpct::queue_ptr stream) {
  7540. const int nb = k / QK_K;
  7541. #if QK_K == 256
  7542. {
  7543. dpct::has_capability_or_fail(stream->get_device(),
  7544. {sycl::aspect::fp16});
  7545. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7546. sycl::range<3>(1, 1, 64),
  7547. sycl::range<3>(1, 1, 64)),
  7548. [=](sycl::nd_item<3> item_ct1) {
  7549. dequantize_block_q5_K(vx, y, item_ct1);
  7550. });
  7551. }
  7552. #else
  7553. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  7554. #endif
  7555. }
  7556. template <typename dst_t>
  7557. static void dequantize_row_q6_K_sycl(const void *vx, dst_t *y, const int k,
  7558. dpct::queue_ptr stream) {
  7559. const int nb = k / QK_K;
  7560. #if QK_K == 256
  7561. {
  7562. dpct::has_capability_or_fail(stream->get_device(),
  7563. {sycl::aspect::fp16});
  7564. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7565. sycl::range<3>(1, 1, 64),
  7566. sycl::range<3>(1, 1, 64)),
  7567. [=](sycl::nd_item<3> item_ct1) {
  7568. dequantize_block_q6_K(vx, y, item_ct1);
  7569. });
  7570. }
  7571. #else
  7572. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  7573. #endif
  7574. }
  7575. static to_fp16_sycl_t ggml_get_to_fp16_sycl(ggml_type type) {
  7576. switch (type) {
  7577. case GGML_TYPE_Q4_0:
  7578. return dequantize_block_sycl<QK4_0, QR4_0, dequantize_q4_0>;
  7579. case GGML_TYPE_Q4_1:
  7580. return dequantize_block_sycl<QK4_1, QR4_1, dequantize_q4_1>;
  7581. case GGML_TYPE_Q5_0:
  7582. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  7583. case GGML_TYPE_Q5_1:
  7584. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  7585. case GGML_TYPE_Q8_0:
  7586. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  7587. case GGML_TYPE_Q2_K:
  7588. return dequantize_row_q2_K_sycl;
  7589. case GGML_TYPE_Q3_K:
  7590. return dequantize_row_q3_K_sycl;
  7591. case GGML_TYPE_Q4_K:
  7592. return dequantize_row_q4_K_sycl;
  7593. case GGML_TYPE_Q5_K:
  7594. return dequantize_row_q5_K_sycl;
  7595. case GGML_TYPE_Q6_K:
  7596. return dequantize_row_q6_K_sycl;
  7597. case GGML_TYPE_F32:
  7598. return dequantize_block_sycl<1, 1, convert_f32>;
  7599. default:
  7600. return nullptr;
  7601. }
  7602. }
  7603. static to_fp32_sycl_t ggml_get_to_fp32_sycl(ggml_type type) {
  7604. switch (type) {
  7605. case GGML_TYPE_Q4_0:
  7606. return dequantize_block_sycl<QK4_0, QR4_0, dequantize_q4_0>;
  7607. case GGML_TYPE_Q4_1:
  7608. return dequantize_block_sycl<QK4_1, QR4_1, dequantize_q4_1>;
  7609. case GGML_TYPE_Q5_0:
  7610. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  7611. case GGML_TYPE_Q5_1:
  7612. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  7613. case GGML_TYPE_Q8_0:
  7614. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  7615. case GGML_TYPE_Q2_K:
  7616. return dequantize_row_q2_K_sycl;
  7617. case GGML_TYPE_Q3_K:
  7618. return dequantize_row_q3_K_sycl;
  7619. case GGML_TYPE_Q4_K:
  7620. return dequantize_row_q4_K_sycl;
  7621. case GGML_TYPE_Q5_K:
  7622. return dequantize_row_q5_K_sycl;
  7623. case GGML_TYPE_Q6_K:
  7624. return dequantize_row_q6_K_sycl;
  7625. case GGML_TYPE_F16:
  7626. return dequantize_block_sycl<1, 1, convert_f16>;
  7627. default:
  7628. return nullptr;
  7629. }
  7630. }
  7631. static void dequantize_mul_mat_vec_q4_0_sycl(const void *vx, const dfloat *y,
  7632. float *dst, const int ncols,
  7633. const int nrows,
  7634. dpct::queue_ptr stream) {
  7635. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7636. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7637. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  7638. const sycl::range<3> block_nums(1, 1, block_num_y);
  7639. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7640. {
  7641. dpct::has_capability_or_fail(stream->get_device(),
  7642. {sycl::aspect::fp16});
  7643. stream->parallel_for(
  7644. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7645. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7646. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>(
  7647. vx, y, dst, ncols, nrows, item_ct1);
  7648. });
  7649. }
  7650. }
  7651. static void dequantize_mul_mat_vec_q4_1_sycl(const void *vx, const dfloat *y,
  7652. float *dst, const int ncols,
  7653. const int nrows,
  7654. dpct::queue_ptr stream) {
  7655. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7656. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7657. const sycl::range<3> block_nums(1, 1, block_num_y);
  7658. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7659. {
  7660. dpct::has_capability_or_fail(stream->get_device(),
  7661. {sycl::aspect::fp16});
  7662. stream->parallel_for(
  7663. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7664. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7665. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>(
  7666. vx, y, dst, ncols, nrows, item_ct1);
  7667. });
  7668. }
  7669. }
  7670. static void dequantize_mul_mat_vec_q5_0_sycl(const void *vx, const dfloat *y,
  7671. float *dst, const int ncols,
  7672. const int nrows,
  7673. dpct::queue_ptr stream) {
  7674. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7675. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7676. const sycl::range<3> block_nums(1, 1, block_num_y);
  7677. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7678. {
  7679. dpct::has_capability_or_fail(stream->get_device(),
  7680. {sycl::aspect::fp16});
  7681. stream->parallel_for(
  7682. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7683. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7684. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>(
  7685. vx, y, dst, ncols, nrows, item_ct1);
  7686. });
  7687. }
  7688. }
  7689. static void dequantize_mul_mat_vec_q5_1_sycl(const void *vx, const dfloat *y,
  7690. float *dst, const int ncols,
  7691. const int nrows,
  7692. dpct::queue_ptr stream) {
  7693. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7694. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7695. const sycl::range<3> block_nums(1, 1, block_num_y);
  7696. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7697. {
  7698. dpct::has_capability_or_fail(stream->get_device(),
  7699. {sycl::aspect::fp16});
  7700. stream->parallel_for(
  7701. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7702. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7703. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>(
  7704. vx, y, dst, ncols, nrows, item_ct1);
  7705. });
  7706. }
  7707. }
  7708. static void dequantize_mul_mat_vec_q8_0_sycl(const void *vx, const dfloat *y,
  7709. float *dst, const int ncols,
  7710. const int nrows,
  7711. dpct::queue_ptr stream) {
  7712. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7713. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7714. const sycl::range<3> block_nums(1, 1, block_num_y);
  7715. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7716. {
  7717. dpct::has_capability_or_fail(stream->get_device(),
  7718. {sycl::aspect::fp16});
  7719. stream->parallel_for(
  7720. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7721. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7722. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>(
  7723. vx, y, dst, ncols, nrows, item_ct1);
  7724. });
  7725. }
  7726. }
  7727. static void dequantize_mul_mat_vec_q2_K_sycl(const void *vx, const float *y,
  7728. float *dst, const int ncols,
  7729. const int nrows,
  7730. dpct::queue_ptr stream) {
  7731. GGML_ASSERT(ncols % QK_K == 0);
  7732. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  7733. const int block_num_y = (nrows + ny - 1) / ny;
  7734. const sycl::range<3> block_nums(1, 1, block_num_y);
  7735. const sycl::range<3> block_dims(1, ny, 32);
  7736. stream->parallel_for(
  7737. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7738. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7739. dequantize_mul_mat_vec_q2_k(vx, y, dst, ncols, nrows, item_ct1);
  7740. });
  7741. }
  7742. static void dequantize_mul_mat_vec_q3_K_sycl(const void *vx, const float *y,
  7743. float *dst, const int ncols,
  7744. const int nrows,
  7745. dpct::queue_ptr stream) {
  7746. GGML_ASSERT(ncols % QK_K == 0);
  7747. const int ny = 2 / K_QUANTS_PER_ITERATION;
  7748. const int block_num_y = (nrows + ny - 1) / ny;
  7749. const sycl::range<3> block_nums(1, 1, block_num_y);
  7750. const sycl::range<3> block_dims(1, ny, 32);
  7751. stream->parallel_for(
  7752. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7753. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7754. dequantize_mul_mat_vec_q3_k(vx, y, dst, ncols, nrows, item_ct1);
  7755. });
  7756. }
  7757. static void dequantize_mul_mat_vec_q4_K_sycl(const void *vx, const float *y,
  7758. float *dst, const int ncols,
  7759. const int nrows,
  7760. dpct::queue_ptr stream) {
  7761. GGML_ASSERT(ncols % QK_K == 0);
  7762. const int ny = 2 / K_QUANTS_PER_ITERATION;
  7763. const int block_num_y = (nrows + ny - 1) / ny;
  7764. const sycl::range<3> block_nums(1, 1, block_num_y);
  7765. const sycl::range<3> block_dims(1, ny, 32);
  7766. stream->parallel_for(
  7767. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7768. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7769. dequantize_mul_mat_vec_q4_k(vx, y, dst, ncols, nrows, item_ct1);
  7770. });
  7771. }
  7772. static void dequantize_mul_mat_vec_q5_K_sycl(const void *vx, const float *y,
  7773. float *dst, const int ncols,
  7774. const int nrows,
  7775. dpct::queue_ptr stream) {
  7776. GGML_ASSERT(ncols % QK_K == 0);
  7777. const sycl::range<3> block_dims(1, 1, 32);
  7778. stream->parallel_for(
  7779. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims, block_dims),
  7780. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7781. dequantize_mul_mat_vec_q5_k(vx, y, dst, ncols, item_ct1);
  7782. });
  7783. }
  7784. static void dequantize_mul_mat_vec_q6_K_sycl(const void *vx, const float *y,
  7785. float *dst, const int ncols,
  7786. const int nrows,
  7787. dpct::queue_ptr stream) {
  7788. GGML_ASSERT(ncols % QK_K == 0);
  7789. const int ny = 2 / K_QUANTS_PER_ITERATION;
  7790. const int block_num_y = (nrows + ny - 1) / ny;
  7791. const sycl::range<3> block_nums(1, 1, block_num_y);
  7792. const sycl::range<3> block_dims(1, ny, 32);
  7793. stream->parallel_for(
  7794. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7795. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7796. dequantize_mul_mat_vec_q6_k(vx, y, dst, ncols, nrows, item_ct1);
  7797. });
  7798. }
  7799. static void convert_mul_mat_vec_f16_sycl(const void *vx, const dfloat *y,
  7800. float *dst, const int ncols,
  7801. const int nrows,
  7802. dpct::queue_ptr stream) {
  7803. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7804. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7805. const sycl::range<3> block_nums(1, 1, block_num_y);
  7806. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7807. {
  7808. dpct::has_capability_or_fail(stream->get_device(),
  7809. {sycl::aspect::fp16});
  7810. stream->parallel_for(
  7811. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7812. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7813. dequantize_mul_mat_vec<1, 1, convert_f16>(vx, y, dst, ncols,
  7814. nrows, item_ct1);
  7815. });
  7816. }
  7817. }
  7818. static void mul_mat_vec_q4_0_q8_1_sycl(const void *vx, const void *vy,
  7819. float *dst, const int ncols,
  7820. const int nrows,
  7821. dpct::queue_ptr stream) {
  7822. GGML_ASSERT(ncols % QK4_0 == 0);
  7823. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7824. const sycl::range<3> block_nums(1, 1, block_num_y);
  7825. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7826. stream->parallel_for(
  7827. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7828. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7829. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ,
  7830. vec_dot_q4_0_q8_1>(vx, vy, dst, ncols, nrows,
  7831. item_ct1);
  7832. });
  7833. }
  7834. static void mul_mat_vec_q4_1_q8_1_sycl(const void *vx, const void *vy,
  7835. float *dst, const int ncols,
  7836. const int nrows,
  7837. dpct::queue_ptr stream) {
  7838. GGML_ASSERT(ncols % QK4_1 == 0);
  7839. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7840. const sycl::range<3> block_nums(1, 1, block_num_y);
  7841. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7842. stream->parallel_for(
  7843. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7844. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7845. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ,
  7846. vec_dot_q4_1_q8_1>(vx, vy, dst, ncols, nrows,
  7847. item_ct1);
  7848. });
  7849. }
  7850. static void mul_mat_vec_q5_0_q8_1_sycl(const void *vx, const void *vy,
  7851. float *dst, const int ncols,
  7852. const int nrows,
  7853. dpct::queue_ptr stream) {
  7854. GGML_ASSERT(ncols % QK5_0 == 0);
  7855. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7856. const sycl::range<3> block_nums(1, 1, block_num_y);
  7857. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7858. stream->parallel_for(
  7859. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7860. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7861. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ,
  7862. vec_dot_q5_0_q8_1>(vx, vy, dst, ncols, nrows,
  7863. item_ct1);
  7864. });
  7865. }
  7866. static void mul_mat_vec_q5_1_q8_1_sycl(const void *vx, const void *vy,
  7867. float *dst, const int ncols,
  7868. const int nrows,
  7869. dpct::queue_ptr stream) {
  7870. GGML_ASSERT(ncols % QK5_1 == 0);
  7871. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7872. const sycl::range<3> block_nums(1, 1, block_num_y);
  7873. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7874. stream->parallel_for(
  7875. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7876. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7877. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ,
  7878. vec_dot_q5_1_q8_1>(vx, vy, dst, ncols, nrows,
  7879. item_ct1);
  7880. });
  7881. }
  7882. static void mul_mat_vec_q8_0_q8_1_sycl(const void *vx, const void *vy,
  7883. float *dst, const int ncols,
  7884. const int nrows,
  7885. dpct::queue_ptr stream) {
  7886. GGML_ASSERT(ncols % QK8_0 == 0);
  7887. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7888. const sycl::range<3> block_nums(1, 1, block_num_y);
  7889. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7890. stream->parallel_for(
  7891. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7892. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7893. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ,
  7894. vec_dot_q8_0_q8_1>(vx, vy, dst, ncols, nrows,
  7895. item_ct1);
  7896. });
  7897. }
  7898. static void mul_mat_vec_q2_K_q8_1_sycl(const void *vx, const void *vy,
  7899. float *dst, const int ncols,
  7900. const int nrows,
  7901. dpct::queue_ptr stream) {
  7902. GGML_ASSERT(ncols % QK_K == 0);
  7903. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7904. const sycl::range<3> block_nums(1, 1, block_num_y);
  7905. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7906. stream->parallel_for(
  7907. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7908. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7909. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ,
  7910. vec_dot_q2_K_q8_1>(vx, vy, dst, ncols, nrows,
  7911. item_ct1);
  7912. });
  7913. }
  7914. static void mul_mat_vec_q3_K_q8_1_sycl(const void *vx, const void *vy,
  7915. float *dst, const int ncols,
  7916. const int nrows,
  7917. dpct::queue_ptr stream) {
  7918. GGML_ASSERT(ncols % QK_K == 0);
  7919. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7920. const sycl::range<3> block_nums(1, 1, block_num_y);
  7921. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7922. stream->parallel_for(
  7923. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7924. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7925. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ,
  7926. vec_dot_q3_K_q8_1>(vx, vy, dst, ncols, nrows,
  7927. item_ct1);
  7928. });
  7929. }
  7930. static void mul_mat_vec_q4_K_q8_1_sycl(const void *vx, const void *vy,
  7931. float *dst, const int ncols,
  7932. const int nrows,
  7933. dpct::queue_ptr stream) {
  7934. GGML_ASSERT(ncols % QK_K == 0);
  7935. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7936. const sycl::range<3> block_nums(1, 1, block_num_y);
  7937. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7938. stream->parallel_for(
  7939. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7940. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7941. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ,
  7942. vec_dot_q4_K_q8_1>(vx, vy, dst, ncols, nrows,
  7943. item_ct1);
  7944. });
  7945. }
  7946. static void mul_mat_vec_q5_K_q8_1_sycl(const void *vx, const void *vy,
  7947. float *dst, const int ncols,
  7948. const int nrows,
  7949. dpct::queue_ptr stream) {
  7950. GGML_ASSERT(ncols % QK_K == 0);
  7951. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7952. const sycl::range<3> block_nums(1, 1, block_num_y);
  7953. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7954. stream->parallel_for(
  7955. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7956. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7957. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ,
  7958. vec_dot_q5_K_q8_1>(vx, vy, dst, ncols, nrows,
  7959. item_ct1);
  7960. });
  7961. }
  7962. static void mul_mat_vec_q6_K_q8_1_sycl(const void *vx, const void *vy,
  7963. float *dst, const int ncols,
  7964. const int nrows,
  7965. dpct::queue_ptr stream) {
  7966. GGML_ASSERT(ncols % QK_K == 0);
  7967. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7968. const sycl::range<3> block_nums(1, 1, block_num_y);
  7969. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7970. stream->parallel_for(
  7971. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7972. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7973. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ,
  7974. vec_dot_q6_K_q8_1>(vx, vy, dst, ncols, nrows,
  7975. item_ct1);
  7976. });
  7977. }
  7978. int get_device_index_by_id(int id){
  7979. int res = g_sycl_device_id2index[id].index;
  7980. // GGML_SYCL_DEBUG("get_device_index_by_id id=%d device_index=%d\n", id, res);
  7981. GGML_ASSERT(res>=0);
  7982. return res;
  7983. }
  7984. int get_device_id_by_index(int index){
  7985. int res = g_device_caps[index].device_id;
  7986. GGML_ASSERT(res>=0);
  7987. return res;
  7988. }
  7989. int get_current_device_index(){
  7990. return get_device_index_by_id(dpct::dev_mgr::instance().current_device_id());
  7991. }
  7992. static void ggml_mul_mat_q4_0_q8_1_sycl(const void *vx, const void *vy,
  7993. float *dst, const int ncols_x,
  7994. const int nrows_x, const int ncols_y,
  7995. const int nrows_y, const int nrows_dst,
  7996. dpct::queue_ptr stream) try {
  7997. int id;
  7998. SYCL_CHECK(
  7999. CHECK_TRY_ERROR(id = get_current_device_index()));
  8000. const int compute_capability = g_device_caps[id].cc;
  8001. int mmq_x, mmq_y, nwarps;
  8002. if (compute_capability >= VER_GEN13) {
  8003. mmq_x = MMQ_X_Q4_0_RDNA2;
  8004. mmq_y = MMQ_Y_Q4_0_RDNA2;
  8005. nwarps = NWARPS_Q4_0_RDNA2;
  8006. } else if (compute_capability >= VER_GEN12) {
  8007. mmq_x = MMQ_X_Q4_0_RDNA1;
  8008. mmq_y = MMQ_Y_Q4_0_RDNA1;
  8009. nwarps = NWARPS_Q4_0_RDNA1;
  8010. } else if (compute_capability >= VER_GEN9) {
  8011. mmq_x = MMQ_X_Q4_0_AMPERE;
  8012. mmq_y = MMQ_Y_Q4_0_AMPERE;
  8013. nwarps = NWARPS_Q4_0_AMPERE;
  8014. } else if (compute_capability >= VER_4VEC) {
  8015. mmq_x = MMQ_X_Q4_0_PASCAL;
  8016. mmq_y = MMQ_Y_Q4_0_PASCAL;
  8017. nwarps = NWARPS_Q4_0_PASCAL;
  8018. } else {
  8019. GGML_ASSERT(false);
  8020. }
  8021. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8022. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8023. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8024. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8025. if (nrows_x % mmq_y == 0) {
  8026. const bool need_check = false;
  8027. /*
  8028. DPCT1049:20: The work-group size passed to the SYCL kernel may exceed
  8029. the limit. To get the device limit, query
  8030. info::device::max_work_group_size. Adjust the work-group size if needed.
  8031. */
  8032. {
  8033. dpct::has_capability_or_fail(stream->get_device(),
  8034. {sycl::aspect::fp16});
  8035. stream->submit([&](sycl::handler &cgh) {
  8036. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  8037. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8038. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  8039. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  8040. cgh);
  8041. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8042. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8043. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8044. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8045. cgh.parallel_for(
  8046. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8047. [=](sycl::nd_item<3> item_ct1) {
  8048. mul_mat_q4_0<need_check>(
  8049. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8050. nrows_dst, item_ct1,
  8051. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  8052. tile_x_d_q4_0_acc_ct1.get_pointer(),
  8053. tile_y_qs_acc_ct1.get_pointer(),
  8054. tile_y_ds_acc_ct1.get_pointer());
  8055. });
  8056. });
  8057. }
  8058. } else {
  8059. const bool need_check = true;
  8060. /*
  8061. DPCT1049:21: The work-group size passed to the SYCL kernel may exceed
  8062. the limit. To get the device limit, query
  8063. info::device::max_work_group_size. Adjust the work-group size if needed.
  8064. */
  8065. {
  8066. dpct::has_capability_or_fail(stream->get_device(),
  8067. {sycl::aspect::fp16});
  8068. stream->submit([&](sycl::handler &cgh) {
  8069. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  8070. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8071. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  8072. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  8073. cgh);
  8074. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8075. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8076. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8077. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8078. cgh.parallel_for(
  8079. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8080. [=](sycl::nd_item<3> item_ct1) {
  8081. mul_mat_q4_0<need_check>(
  8082. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8083. nrows_dst, item_ct1,
  8084. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  8085. tile_x_d_q4_0_acc_ct1.get_pointer(),
  8086. tile_y_qs_acc_ct1.get_pointer(),
  8087. tile_y_ds_acc_ct1.get_pointer());
  8088. });
  8089. });
  8090. }
  8091. }
  8092. }
  8093. catch (sycl::exception const &exc) {
  8094. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8095. << ", line:" << __LINE__ << std::endl;
  8096. std::exit(1);
  8097. }
  8098. static void ggml_mul_mat_q4_1_q8_1_sycl(const void *vx, const void *vy,
  8099. float *dst, const int ncols_x,
  8100. const int nrows_x, const int ncols_y,
  8101. const int nrows_y, const int nrows_dst,
  8102. dpct::queue_ptr stream) try {
  8103. int id;
  8104. SYCL_CHECK(
  8105. CHECK_TRY_ERROR(id = get_current_device_index()));
  8106. const int compute_capability = g_device_caps[id].cc;
  8107. int mmq_x, mmq_y, nwarps;
  8108. if (compute_capability >= VER_GEN13) {
  8109. mmq_x = MMQ_X_Q4_1_RDNA2;
  8110. mmq_y = MMQ_Y_Q4_1_RDNA2;
  8111. nwarps = NWARPS_Q4_1_RDNA2;
  8112. } else if (compute_capability >= VER_GEN12) {
  8113. mmq_x = MMQ_X_Q4_1_RDNA1;
  8114. mmq_y = MMQ_Y_Q4_1_RDNA1;
  8115. nwarps = NWARPS_Q4_1_RDNA1;
  8116. } else if (compute_capability >= VER_GEN9) {
  8117. mmq_x = MMQ_X_Q4_1_AMPERE;
  8118. mmq_y = MMQ_Y_Q4_1_AMPERE;
  8119. nwarps = NWARPS_Q4_1_AMPERE;
  8120. } else if (compute_capability >= VER_4VEC) {
  8121. mmq_x = MMQ_X_Q4_1_PASCAL;
  8122. mmq_y = MMQ_Y_Q4_1_PASCAL;
  8123. nwarps = NWARPS_Q4_1_PASCAL;
  8124. } else {
  8125. GGML_ASSERT(false);
  8126. }
  8127. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8128. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8129. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8130. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8131. if (nrows_x % mmq_y == 0) {
  8132. const bool need_check = false;
  8133. /*
  8134. DPCT1049:22: The work-group size passed to the SYCL kernel may exceed
  8135. the limit. To get the device limit, query
  8136. info::device::max_work_group_size. Adjust the work-group size if needed.
  8137. */
  8138. {
  8139. dpct::has_capability_or_fail(stream->get_device(),
  8140. {sycl::aspect::fp16});
  8141. stream->submit([&](sycl::handler &cgh) {
  8142. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  8143. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  8144. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  8145. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  8146. cgh);
  8147. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8148. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8149. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8150. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8151. cgh.parallel_for(
  8152. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8153. [=](sycl::nd_item<3> item_ct1) {
  8154. mul_mat_q4_1<need_check>(
  8155. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8156. nrows_dst, item_ct1,
  8157. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  8158. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  8159. tile_y_qs_acc_ct1.get_pointer(),
  8160. tile_y_ds_acc_ct1.get_pointer());
  8161. });
  8162. });
  8163. }
  8164. } else {
  8165. const bool need_check = true;
  8166. /*
  8167. DPCT1049:23: The work-group size passed to the SYCL kernel may exceed
  8168. the limit. To get the device limit, query
  8169. info::device::max_work_group_size. Adjust the work-group size if needed.
  8170. */
  8171. {
  8172. dpct::has_capability_or_fail(stream->get_device(),
  8173. {sycl::aspect::fp16});
  8174. stream->submit([&](sycl::handler &cgh) {
  8175. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  8176. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  8177. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  8178. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  8179. cgh);
  8180. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8181. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8182. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8183. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8184. cgh.parallel_for(
  8185. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8186. [=](sycl::nd_item<3> item_ct1) {
  8187. mul_mat_q4_1<need_check>(
  8188. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8189. nrows_dst, item_ct1,
  8190. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  8191. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  8192. tile_y_qs_acc_ct1.get_pointer(),
  8193. tile_y_ds_acc_ct1.get_pointer());
  8194. });
  8195. });
  8196. }
  8197. }
  8198. }
  8199. catch (sycl::exception const &exc) {
  8200. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8201. << ", line:" << __LINE__ << std::endl;
  8202. std::exit(1);
  8203. }
  8204. static void ggml_mul_mat_q5_0_q8_1_sycl(const void *vx, const void *vy,
  8205. float *dst, const int ncols_x,
  8206. const int nrows_x, const int ncols_y,
  8207. const int nrows_y, const int nrows_dst,
  8208. dpct::queue_ptr stream) try {
  8209. int id;
  8210. SYCL_CHECK(
  8211. CHECK_TRY_ERROR(id = get_current_device_index()));
  8212. const int compute_capability = g_device_caps[id].cc;
  8213. int mmq_x, mmq_y, nwarps;
  8214. if (compute_capability >= VER_GEN13) {
  8215. mmq_x = MMQ_X_Q5_0_RDNA2;
  8216. mmq_y = MMQ_Y_Q5_0_RDNA2;
  8217. nwarps = NWARPS_Q5_0_RDNA2;
  8218. } else if (compute_capability >= VER_GEN12) {
  8219. mmq_x = MMQ_X_Q5_0_RDNA1;
  8220. mmq_y = MMQ_Y_Q5_0_RDNA1;
  8221. nwarps = NWARPS_Q5_0_RDNA1;
  8222. } else if (compute_capability >= VER_GEN9) {
  8223. mmq_x = MMQ_X_Q5_0_AMPERE;
  8224. mmq_y = MMQ_Y_Q5_0_AMPERE;
  8225. nwarps = NWARPS_Q5_0_AMPERE;
  8226. } else if (compute_capability >= VER_4VEC) {
  8227. mmq_x = MMQ_X_Q5_0_PASCAL;
  8228. mmq_y = MMQ_Y_Q5_0_PASCAL;
  8229. nwarps = NWARPS_Q5_0_PASCAL;
  8230. } else {
  8231. GGML_ASSERT(false);
  8232. }
  8233. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8234. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8235. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8236. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8237. if (nrows_x % mmq_y == 0) {
  8238. const bool need_check = false;
  8239. /*
  8240. DPCT1049:24: The work-group size passed to the SYCL kernel may exceed
  8241. the limit. To get the device limit, query
  8242. info::device::max_work_group_size. Adjust the work-group size if needed.
  8243. */
  8244. {
  8245. dpct::has_capability_or_fail(stream->get_device(),
  8246. {sycl::aspect::fp16});
  8247. stream->submit([&](sycl::handler &cgh) {
  8248. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  8249. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8250. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  8251. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  8252. cgh);
  8253. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8254. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8255. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8256. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8257. cgh.parallel_for(
  8258. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8259. [=](sycl::nd_item<3> item_ct1) {
  8260. mul_mat_q5_0<need_check>(
  8261. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8262. nrows_dst, item_ct1,
  8263. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  8264. tile_x_d_q5_0_acc_ct1.get_pointer(),
  8265. tile_y_qs_acc_ct1.get_pointer(),
  8266. tile_y_ds_acc_ct1.get_pointer());
  8267. });
  8268. });
  8269. }
  8270. } else {
  8271. const bool need_check = true;
  8272. /*
  8273. DPCT1049:25: The work-group size passed to the SYCL kernel may exceed
  8274. the limit. To get the device limit, query
  8275. info::device::max_work_group_size. Adjust the work-group size if needed.
  8276. */
  8277. {
  8278. dpct::has_capability_or_fail(stream->get_device(),
  8279. {sycl::aspect::fp16});
  8280. stream->submit([&](sycl::handler &cgh) {
  8281. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  8282. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8283. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  8284. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  8285. cgh);
  8286. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8287. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8288. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8289. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8290. cgh.parallel_for(
  8291. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8292. [=](sycl::nd_item<3> item_ct1) {
  8293. mul_mat_q5_0<need_check>(
  8294. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8295. nrows_dst, item_ct1,
  8296. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  8297. tile_x_d_q5_0_acc_ct1.get_pointer(),
  8298. tile_y_qs_acc_ct1.get_pointer(),
  8299. tile_y_ds_acc_ct1.get_pointer());
  8300. });
  8301. });
  8302. }
  8303. }
  8304. }
  8305. catch (sycl::exception const &exc) {
  8306. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8307. << ", line:" << __LINE__ << std::endl;
  8308. std::exit(1);
  8309. }
  8310. static void ggml_mul_mat_q5_1_q8_1_sycl(const void *vx, const void *vy,
  8311. float *dst, const int ncols_x,
  8312. const int nrows_x, const int ncols_y,
  8313. const int nrows_y, const int nrows_dst,
  8314. dpct::queue_ptr stream) try {
  8315. int id;
  8316. SYCL_CHECK(
  8317. CHECK_TRY_ERROR(id = get_current_device_index()));
  8318. const int compute_capability = g_device_caps[id].cc;
  8319. int mmq_x, mmq_y, nwarps;
  8320. if (compute_capability >= VER_GEN13) {
  8321. mmq_x = MMQ_X_Q5_1_RDNA2;
  8322. mmq_y = MMQ_Y_Q5_1_RDNA2;
  8323. nwarps = NWARPS_Q5_1_RDNA2;
  8324. } else if (compute_capability >= VER_GEN12) {
  8325. mmq_x = MMQ_X_Q5_1_RDNA1;
  8326. mmq_y = MMQ_Y_Q5_1_RDNA1;
  8327. nwarps = NWARPS_Q5_1_RDNA1;
  8328. } else if (compute_capability >= VER_GEN9) {
  8329. mmq_x = MMQ_X_Q5_1_AMPERE;
  8330. mmq_y = MMQ_Y_Q5_1_AMPERE;
  8331. nwarps = NWARPS_Q5_1_AMPERE;
  8332. } else if (compute_capability >= VER_4VEC) {
  8333. mmq_x = MMQ_X_Q5_1_PASCAL;
  8334. mmq_y = MMQ_Y_Q5_1_PASCAL;
  8335. nwarps = NWARPS_Q5_1_PASCAL;
  8336. } else {
  8337. GGML_ASSERT(false);
  8338. }
  8339. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8340. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8341. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8342. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8343. if (nrows_x % mmq_y == 0) {
  8344. const bool need_check = false;
  8345. /*
  8346. DPCT1049:26: The work-group size passed to the SYCL kernel may exceed
  8347. the limit. To get the device limit, query
  8348. info::device::max_work_group_size. Adjust the work-group size if needed.
  8349. */
  8350. {
  8351. dpct::has_capability_or_fail(stream->get_device(),
  8352. {sycl::aspect::fp16});
  8353. stream->submit([&](sycl::handler &cgh) {
  8354. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  8355. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8356. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  8357. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  8358. cgh);
  8359. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8360. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8361. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8362. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8363. cgh.parallel_for(
  8364. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8365. [=](sycl::nd_item<3> item_ct1) {
  8366. mul_mat_q5_1<need_check>(
  8367. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8368. nrows_dst, item_ct1,
  8369. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  8370. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  8371. tile_y_qs_acc_ct1.get_pointer(),
  8372. tile_y_ds_acc_ct1.get_pointer());
  8373. });
  8374. });
  8375. }
  8376. } else {
  8377. const bool need_check = true;
  8378. /*
  8379. DPCT1049:27: The work-group size passed to the SYCL kernel may exceed
  8380. the limit. To get the device limit, query
  8381. info::device::max_work_group_size. Adjust the work-group size if needed.
  8382. */
  8383. {
  8384. dpct::has_capability_or_fail(stream->get_device(),
  8385. {sycl::aspect::fp16});
  8386. stream->submit([&](sycl::handler &cgh) {
  8387. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  8388. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8389. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  8390. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  8391. cgh);
  8392. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8393. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8394. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8395. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8396. cgh.parallel_for(
  8397. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8398. [=](sycl::nd_item<3> item_ct1) {
  8399. mul_mat_q5_1<need_check>(
  8400. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8401. nrows_dst, item_ct1,
  8402. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  8403. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  8404. tile_y_qs_acc_ct1.get_pointer(),
  8405. tile_y_ds_acc_ct1.get_pointer());
  8406. });
  8407. });
  8408. }
  8409. }
  8410. }
  8411. catch (sycl::exception const &exc) {
  8412. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8413. << ", line:" << __LINE__ << std::endl;
  8414. std::exit(1);
  8415. }
  8416. static void ggml_mul_mat_q8_0_q8_1_sycl(const void *vx, const void *vy,
  8417. float *dst, const int ncols_x,
  8418. const int nrows_x, const int ncols_y,
  8419. const int nrows_y, const int nrows_dst,
  8420. dpct::queue_ptr stream) try {
  8421. int id;
  8422. SYCL_CHECK(
  8423. CHECK_TRY_ERROR(id = get_current_device_index()));
  8424. const int compute_capability = g_device_caps[id].cc;
  8425. int mmq_x, mmq_y, nwarps;
  8426. if (compute_capability >= VER_GEN13) {
  8427. mmq_x = MMQ_X_Q8_0_RDNA2;
  8428. mmq_y = MMQ_Y_Q8_0_RDNA2;
  8429. nwarps = NWARPS_Q8_0_RDNA2;
  8430. } else if (compute_capability >= VER_GEN12) {
  8431. mmq_x = MMQ_X_Q8_0_RDNA1;
  8432. mmq_y = MMQ_Y_Q8_0_RDNA1;
  8433. nwarps = NWARPS_Q8_0_RDNA1;
  8434. } else if (compute_capability >= VER_GEN9) {
  8435. mmq_x = MMQ_X_Q8_0_AMPERE;
  8436. mmq_y = MMQ_Y_Q8_0_AMPERE;
  8437. nwarps = NWARPS_Q8_0_AMPERE;
  8438. } else if (compute_capability >= VER_4VEC) {
  8439. mmq_x = MMQ_X_Q8_0_PASCAL;
  8440. mmq_y = MMQ_Y_Q8_0_PASCAL;
  8441. nwarps = NWARPS_Q8_0_PASCAL;
  8442. } else {
  8443. GGML_ASSERT(false);
  8444. }
  8445. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8446. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8447. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8448. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8449. if (nrows_x % mmq_y == 0) {
  8450. const bool need_check = false;
  8451. /*
  8452. DPCT1049:28: The work-group size passed to the SYCL kernel may exceed
  8453. the limit. To get the device limit, query
  8454. info::device::max_work_group_size. Adjust the work-group size if needed.
  8455. */
  8456. {
  8457. dpct::has_capability_or_fail(stream->get_device(),
  8458. {sycl::aspect::fp16});
  8459. stream->submit([&](sycl::handler &cgh) {
  8460. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  8461. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8462. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  8463. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  8464. cgh);
  8465. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8466. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8467. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8468. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8469. cgh.parallel_for(
  8470. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8471. [=](sycl::nd_item<3> item_ct1) {
  8472. mul_mat_q8_0<need_check>(
  8473. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8474. nrows_dst, item_ct1,
  8475. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  8476. tile_x_d_q8_0_acc_ct1.get_pointer(),
  8477. tile_y_qs_acc_ct1.get_pointer(),
  8478. tile_y_ds_acc_ct1.get_pointer());
  8479. });
  8480. });
  8481. }
  8482. } else {
  8483. const bool need_check = true;
  8484. /*
  8485. DPCT1049:29: The work-group size passed to the SYCL kernel may exceed
  8486. the limit. To get the device limit, query
  8487. info::device::max_work_group_size. Adjust the work-group size if needed.
  8488. */
  8489. {
  8490. dpct::has_capability_or_fail(stream->get_device(),
  8491. {sycl::aspect::fp16});
  8492. stream->submit([&](sycl::handler &cgh) {
  8493. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  8494. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8495. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  8496. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  8497. cgh);
  8498. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8499. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8500. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8501. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8502. cgh.parallel_for(
  8503. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8504. [=](sycl::nd_item<3> item_ct1) {
  8505. mul_mat_q8_0<need_check>(
  8506. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8507. nrows_dst, item_ct1,
  8508. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  8509. tile_x_d_q8_0_acc_ct1.get_pointer(),
  8510. tile_y_qs_acc_ct1.get_pointer(),
  8511. tile_y_ds_acc_ct1.get_pointer());
  8512. });
  8513. });
  8514. }
  8515. }
  8516. }
  8517. catch (sycl::exception const &exc) {
  8518. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8519. << ", line:" << __LINE__ << std::endl;
  8520. std::exit(1);
  8521. }
  8522. static void ggml_mul_mat_q2_K_q8_1_sycl(const void *vx, const void *vy,
  8523. float *dst, const int ncols_x,
  8524. const int nrows_x, const int ncols_y,
  8525. const int nrows_y, const int nrows_dst,
  8526. dpct::queue_ptr stream) try {
  8527. int id;
  8528. SYCL_CHECK(
  8529. CHECK_TRY_ERROR(id = get_current_device_index()));
  8530. const int compute_capability = g_device_caps[id].cc;
  8531. int mmq_x, mmq_y, nwarps;
  8532. if (compute_capability >= VER_GEN13) {
  8533. mmq_x = MMQ_X_Q2_K_RDNA2;
  8534. mmq_y = MMQ_Y_Q2_K_RDNA2;
  8535. nwarps = NWARPS_Q2_K_RDNA2;
  8536. } else if (compute_capability >= VER_GEN12) {
  8537. mmq_x = MMQ_X_Q2_K_RDNA1;
  8538. mmq_y = MMQ_Y_Q2_K_RDNA1;
  8539. nwarps = NWARPS_Q2_K_RDNA1;
  8540. } else if (compute_capability >= VER_GEN9) {
  8541. mmq_x = MMQ_X_Q2_K_AMPERE;
  8542. mmq_y = MMQ_Y_Q2_K_AMPERE;
  8543. nwarps = NWARPS_Q2_K_AMPERE;
  8544. } else if (compute_capability >= VER_4VEC) {
  8545. mmq_x = MMQ_X_Q2_K_PASCAL;
  8546. mmq_y = MMQ_Y_Q2_K_PASCAL;
  8547. nwarps = NWARPS_Q2_K_PASCAL;
  8548. } else {
  8549. GGML_ASSERT(false);
  8550. }
  8551. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8552. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8553. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8554. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8555. if (nrows_x % mmq_y == 0) {
  8556. const bool need_check = false;
  8557. /*
  8558. DPCT1049:30: The work-group size passed to the SYCL kernel may exceed
  8559. the limit. To get the device limit, query
  8560. info::device::max_work_group_size. Adjust the work-group size if needed.
  8561. */
  8562. {
  8563. dpct::has_capability_or_fail(stream->get_device(),
  8564. {sycl::aspect::fp16});
  8565. stream->submit([&](sycl::handler &cgh) {
  8566. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  8567. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8568. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  8569. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  8570. cgh);
  8571. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  8572. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  8573. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8574. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8575. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8576. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8577. cgh.parallel_for(
  8578. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8579. [=](sycl::nd_item<3> item_ct1) {
  8580. mul_mat_q2_K<need_check>(
  8581. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8582. nrows_dst, item_ct1,
  8583. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  8584. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  8585. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  8586. tile_y_qs_acc_ct1.get_pointer(),
  8587. tile_y_ds_acc_ct1.get_pointer());
  8588. });
  8589. });
  8590. }
  8591. } else {
  8592. const bool need_check = true;
  8593. /*
  8594. DPCT1049:31: The work-group size passed to the SYCL kernel may exceed
  8595. the limit. To get the device limit, query
  8596. info::device::max_work_group_size. Adjust the work-group size if needed.
  8597. */
  8598. {
  8599. dpct::has_capability_or_fail(stream->get_device(),
  8600. {sycl::aspect::fp16});
  8601. stream->submit([&](sycl::handler &cgh) {
  8602. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  8603. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8604. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  8605. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  8606. cgh);
  8607. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  8608. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  8609. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8610. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8611. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8612. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8613. cgh.parallel_for(
  8614. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8615. [=](sycl::nd_item<3> item_ct1) {
  8616. mul_mat_q2_K<need_check>(
  8617. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8618. nrows_dst, item_ct1,
  8619. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  8620. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  8621. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  8622. tile_y_qs_acc_ct1.get_pointer(),
  8623. tile_y_ds_acc_ct1.get_pointer());
  8624. });
  8625. });
  8626. }
  8627. }
  8628. }
  8629. catch (sycl::exception const &exc) {
  8630. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8631. << ", line:" << __LINE__ << std::endl;
  8632. std::exit(1);
  8633. }
  8634. static void ggml_mul_mat_q3_K_q8_1_sycl(const void *vx, const void *vy,
  8635. float *dst, const int ncols_x,
  8636. const int nrows_x, const int ncols_y,
  8637. const int nrows_y, const int nrows_dst,
  8638. dpct::queue_ptr stream) try {
  8639. #if QK_K == 256
  8640. int id;
  8641. SYCL_CHECK(
  8642. CHECK_TRY_ERROR(id = get_current_device_index()));
  8643. const int compute_capability = g_device_caps[id].cc;
  8644. int mmq_x, mmq_y, nwarps;
  8645. if (compute_capability >= VER_GEN13) {
  8646. mmq_x = MMQ_X_Q3_K_RDNA2;
  8647. mmq_y = MMQ_Y_Q3_K_RDNA2;
  8648. nwarps = NWARPS_Q3_K_RDNA2;
  8649. } else if (compute_capability >= VER_GEN12) {
  8650. mmq_x = MMQ_X_Q3_K_RDNA1;
  8651. mmq_y = MMQ_Y_Q3_K_RDNA1;
  8652. nwarps = NWARPS_Q3_K_RDNA1;
  8653. } else if (compute_capability >= VER_GEN9) {
  8654. mmq_x = MMQ_X_Q3_K_AMPERE;
  8655. mmq_y = MMQ_Y_Q3_K_AMPERE;
  8656. nwarps = NWARPS_Q3_K_AMPERE;
  8657. } else if (compute_capability >= VER_4VEC) {
  8658. mmq_x = MMQ_X_Q3_K_PASCAL;
  8659. mmq_y = MMQ_Y_Q3_K_PASCAL;
  8660. nwarps = NWARPS_Q3_K_PASCAL;
  8661. } else {
  8662. GGML_ASSERT(false);
  8663. }
  8664. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8665. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8666. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8667. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8668. if (nrows_x % mmq_y == 0) {
  8669. const bool need_check = false;
  8670. /*
  8671. DPCT1049:32: The work-group size passed to the SYCL kernel may exceed
  8672. the limit. To get the device limit, query
  8673. info::device::max_work_group_size. Adjust the work-group size if needed.
  8674. */
  8675. {
  8676. dpct::has_capability_or_fail(stream->get_device(),
  8677. {sycl::aspect::fp16});
  8678. stream->submit([&](sycl::handler &cgh) {
  8679. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  8680. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8681. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  8682. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  8683. cgh);
  8684. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  8685. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  8686. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  8687. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  8688. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8689. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8690. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8691. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8692. cgh.parallel_for(
  8693. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8694. [=](sycl::nd_item<3> item_ct1) {
  8695. mul_mat_q3_K<need_check>(
  8696. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8697. nrows_dst, item_ct1,
  8698. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  8699. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  8700. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  8701. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  8702. tile_y_qs_acc_ct1.get_pointer(),
  8703. tile_y_ds_acc_ct1.get_pointer());
  8704. });
  8705. });
  8706. }
  8707. } else {
  8708. const bool need_check = true;
  8709. /*
  8710. DPCT1049:33: The work-group size passed to the SYCL kernel may exceed
  8711. the limit. To get the device limit, query
  8712. info::device::max_work_group_size. Adjust the work-group size if needed.
  8713. */
  8714. {
  8715. dpct::has_capability_or_fail(stream->get_device(),
  8716. {sycl::aspect::fp16});
  8717. stream->submit([&](sycl::handler &cgh) {
  8718. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  8719. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8720. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  8721. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  8722. cgh);
  8723. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  8724. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  8725. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  8726. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  8727. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8728. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8729. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8730. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8731. cgh.parallel_for(
  8732. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8733. [=](sycl::nd_item<3> item_ct1) {
  8734. mul_mat_q3_K<need_check>(
  8735. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8736. nrows_dst, item_ct1,
  8737. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  8738. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  8739. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  8740. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  8741. tile_y_qs_acc_ct1.get_pointer(),
  8742. tile_y_ds_acc_ct1.get_pointer());
  8743. });
  8744. });
  8745. }
  8746. }
  8747. #endif
  8748. }
  8749. catch (sycl::exception const &exc) {
  8750. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8751. << ", line:" << __LINE__ << std::endl;
  8752. std::exit(1);
  8753. }
  8754. static void ggml_mul_mat_q4_K_q8_1_sycl(const void *vx, const void *vy,
  8755. float *dst, const int ncols_x,
  8756. const int nrows_x, const int ncols_y,
  8757. const int nrows_y, const int nrows_dst,
  8758. dpct::queue_ptr stream) try {
  8759. int id;
  8760. SYCL_CHECK(
  8761. CHECK_TRY_ERROR(id = get_current_device_index()));
  8762. const int compute_capability = g_device_caps[id].cc;
  8763. int mmq_x, mmq_y, nwarps;
  8764. if (compute_capability >= VER_GEN13) {
  8765. mmq_x = MMQ_X_Q4_K_RDNA2;
  8766. mmq_y = MMQ_Y_Q4_K_RDNA2;
  8767. nwarps = NWARPS_Q4_K_RDNA2;
  8768. } else if (compute_capability >= VER_GEN12) {
  8769. mmq_x = MMQ_X_Q4_K_RDNA1;
  8770. mmq_y = MMQ_Y_Q4_K_RDNA1;
  8771. nwarps = NWARPS_Q4_K_RDNA1;
  8772. } else if (compute_capability >= VER_GEN9) {
  8773. mmq_x = MMQ_X_Q4_K_AMPERE;
  8774. mmq_y = MMQ_Y_Q4_K_AMPERE;
  8775. nwarps = NWARPS_Q4_K_AMPERE;
  8776. } else if (compute_capability >= VER_4VEC) {
  8777. mmq_x = MMQ_X_Q4_K_PASCAL;
  8778. mmq_y = MMQ_Y_Q4_K_PASCAL;
  8779. nwarps = NWARPS_Q4_K_PASCAL;
  8780. } else {
  8781. GGML_ASSERT(false);
  8782. }
  8783. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8784. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8785. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8786. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8787. if (nrows_x % mmq_y == 0) {
  8788. const bool need_check = false;
  8789. /*
  8790. DPCT1049:34: The work-group size passed to the SYCL kernel may exceed
  8791. the limit. To get the device limit, query
  8792. info::device::max_work_group_size. Adjust the work-group size if needed.
  8793. */
  8794. {
  8795. dpct::has_capability_or_fail(stream->get_device(),
  8796. {sycl::aspect::fp16});
  8797. stream->submit([&](sycl::handler &cgh) {
  8798. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  8799. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8800. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  8801. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  8802. cgh);
  8803. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  8804. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  8805. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8806. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8807. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8808. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8809. cgh.parallel_for(
  8810. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8811. [=](sycl::nd_item<3> item_ct1) {
  8812. mul_mat_q4_K<need_check>(
  8813. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8814. nrows_dst, item_ct1,
  8815. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  8816. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  8817. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  8818. tile_y_qs_acc_ct1.get_pointer(),
  8819. tile_y_ds_acc_ct1.get_pointer());
  8820. });
  8821. });
  8822. }
  8823. } else {
  8824. const bool need_check = true;
  8825. /*
  8826. DPCT1049:35: The work-group size passed to the SYCL kernel may exceed
  8827. the limit. To get the device limit, query
  8828. info::device::max_work_group_size. Adjust the work-group size if needed.
  8829. */
  8830. {
  8831. dpct::has_capability_or_fail(stream->get_device(),
  8832. {sycl::aspect::fp16});
  8833. stream->submit([&](sycl::handler &cgh) {
  8834. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  8835. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8836. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  8837. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  8838. cgh);
  8839. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  8840. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  8841. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8842. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8843. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8844. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8845. cgh.parallel_for(
  8846. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8847. [=](sycl::nd_item<3> item_ct1) {
  8848. mul_mat_q4_K<need_check>(
  8849. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8850. nrows_dst, item_ct1,
  8851. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  8852. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  8853. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  8854. tile_y_qs_acc_ct1.get_pointer(),
  8855. tile_y_ds_acc_ct1.get_pointer());
  8856. });
  8857. });
  8858. }
  8859. }
  8860. }
  8861. catch (sycl::exception const &exc) {
  8862. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8863. << ", line:" << __LINE__ << std::endl;
  8864. std::exit(1);
  8865. }
  8866. static void ggml_mul_mat_q5_K_q8_1_sycl(const void *vx, const void *vy,
  8867. float *dst, const int ncols_x,
  8868. const int nrows_x, const int ncols_y,
  8869. const int nrows_y, const int nrows_dst,
  8870. dpct::queue_ptr stream) try {
  8871. int id;
  8872. SYCL_CHECK(
  8873. CHECK_TRY_ERROR(id = get_current_device_index()));
  8874. const int compute_capability = g_device_caps[id].cc;
  8875. int mmq_x, mmq_y, nwarps;
  8876. if (compute_capability >= VER_GEN13) {
  8877. mmq_x = MMQ_X_Q5_K_RDNA2;
  8878. mmq_y = MMQ_Y_Q5_K_RDNA2;
  8879. nwarps = NWARPS_Q5_K_RDNA2;
  8880. } else if (compute_capability >= VER_GEN12) {
  8881. mmq_x = MMQ_X_Q5_K_RDNA1;
  8882. mmq_y = MMQ_Y_Q5_K_RDNA1;
  8883. nwarps = NWARPS_Q5_K_RDNA1;
  8884. } else if (compute_capability >= VER_GEN9) {
  8885. mmq_x = MMQ_X_Q5_K_AMPERE;
  8886. mmq_y = MMQ_Y_Q5_K_AMPERE;
  8887. nwarps = NWARPS_Q5_K_AMPERE;
  8888. } else if (compute_capability >= VER_4VEC) {
  8889. mmq_x = MMQ_X_Q5_K_PASCAL;
  8890. mmq_y = MMQ_Y_Q5_K_PASCAL;
  8891. nwarps = NWARPS_Q5_K_PASCAL;
  8892. } else {
  8893. GGML_ASSERT(false);
  8894. }
  8895. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8896. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8897. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8898. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8899. if (nrows_x % mmq_y == 0) {
  8900. const bool need_check = false;
  8901. /*
  8902. DPCT1049:36: The work-group size passed to the SYCL kernel may exceed
  8903. the limit. To get the device limit, query
  8904. info::device::max_work_group_size. Adjust the work-group size if needed.
  8905. */
  8906. {
  8907. dpct::has_capability_or_fail(stream->get_device(),
  8908. {sycl::aspect::fp16});
  8909. stream->submit([&](sycl::handler &cgh) {
  8910. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  8911. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8912. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  8913. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  8914. cgh);
  8915. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  8916. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  8917. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8918. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8919. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8920. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8921. cgh.parallel_for(
  8922. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8923. [=](sycl::nd_item<3> item_ct1) {
  8924. mul_mat_q5_K<need_check>(
  8925. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8926. nrows_dst, item_ct1,
  8927. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  8928. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  8929. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  8930. tile_y_qs_acc_ct1.get_pointer(),
  8931. tile_y_ds_acc_ct1.get_pointer());
  8932. });
  8933. });
  8934. }
  8935. } else {
  8936. const bool need_check = true;
  8937. /*
  8938. DPCT1049:37: The work-group size passed to the SYCL kernel may exceed
  8939. the limit. To get the device limit, query
  8940. info::device::max_work_group_size. Adjust the work-group size if needed.
  8941. */
  8942. {
  8943. dpct::has_capability_or_fail(stream->get_device(),
  8944. {sycl::aspect::fp16});
  8945. stream->submit([&](sycl::handler &cgh) {
  8946. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  8947. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8948. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  8949. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  8950. cgh);
  8951. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  8952. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  8953. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8954. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8955. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8956. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8957. cgh.parallel_for(
  8958. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8959. [=](sycl::nd_item<3> item_ct1) {
  8960. mul_mat_q5_K<need_check>(
  8961. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8962. nrows_dst, item_ct1,
  8963. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  8964. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  8965. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  8966. tile_y_qs_acc_ct1.get_pointer(),
  8967. tile_y_ds_acc_ct1.get_pointer());
  8968. });
  8969. });
  8970. }
  8971. }
  8972. }
  8973. catch (sycl::exception const &exc) {
  8974. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8975. << ", line:" << __LINE__ << std::endl;
  8976. std::exit(1);
  8977. }
  8978. static void ggml_mul_mat_q6_K_q8_1_sycl(const void *vx, const void *vy,
  8979. float *dst, const int ncols_x,
  8980. const int nrows_x, const int ncols_y,
  8981. const int nrows_y, const int nrows_dst,
  8982. dpct::queue_ptr stream) try {
  8983. int id;
  8984. SYCL_CHECK(
  8985. CHECK_TRY_ERROR(id = get_current_device_index()));
  8986. const int compute_capability = g_device_caps[id].cc;
  8987. int mmq_x, mmq_y, nwarps;
  8988. if (compute_capability >= VER_GEN13) {
  8989. mmq_x = MMQ_X_Q6_K_RDNA2;
  8990. mmq_y = MMQ_Y_Q6_K_RDNA2;
  8991. nwarps = NWARPS_Q6_K_RDNA2;
  8992. } else if (compute_capability >= VER_GEN12) {
  8993. mmq_x = MMQ_X_Q6_K_RDNA1;
  8994. mmq_y = MMQ_Y_Q6_K_RDNA1;
  8995. nwarps = NWARPS_Q6_K_RDNA1;
  8996. } else if (compute_capability >= VER_GEN9) {
  8997. mmq_x = MMQ_X_Q6_K_AMPERE;
  8998. mmq_y = MMQ_Y_Q6_K_AMPERE;
  8999. nwarps = NWARPS_Q6_K_AMPERE;
  9000. } else if (compute_capability >= VER_4VEC) {
  9001. mmq_x = MMQ_X_Q6_K_PASCAL;
  9002. mmq_y = MMQ_Y_Q6_K_PASCAL;
  9003. nwarps = NWARPS_Q6_K_PASCAL;
  9004. } else {
  9005. GGML_ASSERT(false);
  9006. }
  9007. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9008. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9009. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9010. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9011. if (nrows_x % mmq_y == 0) {
  9012. const bool need_check = false;
  9013. /*
  9014. DPCT1049:38: The work-group size passed to the SYCL kernel may exceed
  9015. the limit. To get the device limit, query
  9016. info::device::max_work_group_size. Adjust the work-group size if needed.
  9017. */
  9018. {
  9019. dpct::has_capability_or_fail(stream->get_device(),
  9020. {sycl::aspect::fp16});
  9021. stream->submit([&](sycl::handler &cgh) {
  9022. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  9023. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9024. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  9025. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  9026. cgh);
  9027. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  9028. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  9029. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9030. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9031. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9032. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9033. cgh.parallel_for(
  9034. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9035. [=](sycl::nd_item<3> item_ct1) {
  9036. mul_mat_q6_K<need_check>(
  9037. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9038. nrows_dst, item_ct1,
  9039. tile_x_ql_acc_ct1.get_pointer(),
  9040. tile_x_dm_acc_ct1.get_pointer(),
  9041. tile_x_sc_acc_ct1.get_pointer(),
  9042. tile_y_qs_acc_ct1.get_pointer(),
  9043. tile_y_ds_acc_ct1.get_pointer());
  9044. });
  9045. });
  9046. }
  9047. } else {
  9048. const bool need_check = true;
  9049. /*
  9050. DPCT1049:39: The work-group size passed to the SYCL kernel may exceed
  9051. the limit. To get the device limit, query
  9052. info::device::max_work_group_size. Adjust the work-group size if needed.
  9053. */
  9054. {
  9055. dpct::has_capability_or_fail(stream->get_device(),
  9056. {sycl::aspect::fp16});
  9057. stream->submit([&](sycl::handler &cgh) {
  9058. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  9059. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9060. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  9061. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  9062. cgh);
  9063. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  9064. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  9065. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9066. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9067. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9068. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9069. cgh.parallel_for(
  9070. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9071. [=](sycl::nd_item<3> item_ct1) {
  9072. mul_mat_q6_K<need_check>(
  9073. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9074. nrows_dst, item_ct1,
  9075. tile_x_ql_acc_ct1.get_pointer(),
  9076. tile_x_dm_acc_ct1.get_pointer(),
  9077. tile_x_sc_acc_ct1.get_pointer(),
  9078. tile_y_qs_acc_ct1.get_pointer(),
  9079. tile_y_ds_acc_ct1.get_pointer());
  9080. });
  9081. });
  9082. }
  9083. }
  9084. }
  9085. catch (sycl::exception const &exc) {
  9086. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9087. << ", line:" << __LINE__ << std::endl;
  9088. std::exit(1);
  9089. }
  9090. static void ggml_mul_mat_p021_f16_f32_sycl(const void *vx, const float *y,
  9091. float *dst, const int ncols_x,
  9092. const int nrows_x,
  9093. const int nchannels_x,
  9094. const int nchannels_y,
  9095. dpct::queue_ptr stream) {
  9096. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  9097. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  9098. {
  9099. dpct::has_capability_or_fail(stream->get_device(),
  9100. {sycl::aspect::fp16});
  9101. stream->parallel_for(
  9102. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9103. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9104. mul_mat_p021_f16_f32(vx, y, dst, ncols_x, nrows_x, nchannels_x,
  9105. nchannels_y, item_ct1);
  9106. });
  9107. }
  9108. }
  9109. static void ggml_mul_mat_vec_nc_f16_f32_sycl(
  9110. const void *vx, const float *y, float *dst, const int ncols_x,
  9111. const int nrows_x, const int row_stride_x, const int nchannels_x,
  9112. const int nchannels_y, const int channel_stride_x, dpct::queue_ptr stream) {
  9113. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  9114. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  9115. {
  9116. dpct::has_capability_or_fail(stream->get_device(),
  9117. {sycl::aspect::fp16});
  9118. stream->parallel_for(
  9119. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9120. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9121. mul_mat_vec_nc_f16_f32(vx, y, dst, ncols_x, nrows_x,
  9122. row_stride_x, channel_stride_x,
  9123. nchannels_y / nchannels_x, item_ct1);
  9124. });
  9125. }
  9126. }
  9127. static void ggml_cpy_f32_f32_sycl(const char *cx, char *cdst, const int ne,
  9128. const int ne00, const int ne01,
  9129. const int nb00, const int nb01,
  9130. const int nb02, const int ne10,
  9131. const int ne11, const int nb10,
  9132. const int nb11, const int nb12,
  9133. dpct::queue_ptr stream) {
  9134. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9135. {
  9136. dpct::has_capability_or_fail(stream->get_device(),
  9137. {sycl::aspect::fp16});
  9138. stream->parallel_for(
  9139. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9140. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9141. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9142. [=](sycl::nd_item<3> item_ct1) {
  9143. cpy_f32_f16<cpy_1_f32_f32>(cx, cdst, ne, ne00, ne01, nb00, nb01,
  9144. nb02, ne10, ne11, nb10, nb11, nb12,
  9145. item_ct1);
  9146. });
  9147. }
  9148. }
  9149. static void ggml_cpy_f32_f16_sycl(const char *cx, char *cdst, const int ne,
  9150. const int ne00, const int ne01,
  9151. const int nb00, const int nb01,
  9152. const int nb02, const int ne10,
  9153. const int ne11, const int nb10,
  9154. const int nb11, const int nb12,
  9155. dpct::queue_ptr stream) {
  9156. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9157. {
  9158. dpct::has_capability_or_fail(stream->get_device(),
  9159. {sycl::aspect::fp16});
  9160. stream->parallel_for(
  9161. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9162. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9163. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9164. [=](sycl::nd_item<3> item_ct1) {
  9165. cpy_f32_f16<cpy_1_f32_f16>(cx, cdst, ne, ne00, ne01, nb00, nb01,
  9166. nb02, ne10, ne11, nb10, nb11, nb12,
  9167. item_ct1);
  9168. });
  9169. }
  9170. }
  9171. static void ggml_cpy_f32_q8_0_sycl(const char *cx, char *cdst, const int ne,
  9172. const int ne00, const int ne01,
  9173. const int nb00, const int nb01,
  9174. const int nb02, const int ne10,
  9175. const int ne11, const int nb10,
  9176. const int nb11, const int nb12,
  9177. dpct::queue_ptr stream) {
  9178. GGML_ASSERT(ne % QK8_0 == 0);
  9179. const int num_blocks = ne / QK8_0;
  9180. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  9181. sycl::range<3>(1, 1, 1)),
  9182. [=](sycl::nd_item<3> item_ct1) {
  9183. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0>(
  9184. cx, cdst, ne, ne00, ne01, nb00, nb01, nb02,
  9185. ne10, ne11, nb10, nb11, nb12, item_ct1);
  9186. });
  9187. }
  9188. static void ggml_cpy_f32_q4_0_sycl(const char *cx, char *cdst, const int ne,
  9189. const int ne00, const int ne01,
  9190. const int nb00, const int nb01,
  9191. const int nb02, const int ne10,
  9192. const int ne11, const int nb10,
  9193. const int nb11, const int nb12,
  9194. dpct::queue_ptr stream) {
  9195. GGML_ASSERT(ne % QK4_0 == 0);
  9196. const int num_blocks = ne / QK4_0;
  9197. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  9198. sycl::range<3>(1, 1, 1)),
  9199. [=](sycl::nd_item<3> item_ct1) {
  9200. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0>(
  9201. cx, cdst, ne, ne00, ne01, nb00, nb01, nb02,
  9202. ne10, ne11, nb10, nb11, nb12, item_ct1);
  9203. });
  9204. }
  9205. static void ggml_cpy_f32_q4_1_sycl(const char *cx, char *cdst, const int ne,
  9206. const int ne00, const int ne01,
  9207. const int nb00, const int nb01,
  9208. const int nb02, const int ne10,
  9209. const int ne11, const int nb10,
  9210. const int nb11, const int nb12,
  9211. dpct::queue_ptr stream) {
  9212. GGML_ASSERT(ne % QK4_1 == 0);
  9213. const int num_blocks = ne / QK4_1;
  9214. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  9215. sycl::range<3>(1, 1, 1)),
  9216. [=](sycl::nd_item<3> item_ct1) {
  9217. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1>(
  9218. cx, cdst, ne, ne00, ne01, nb00, nb01, nb02,
  9219. ne10, ne11, nb10, nb11, nb12, item_ct1);
  9220. });
  9221. }
  9222. static void ggml_cpy_f16_f16_sycl(const char *cx, char *cdst, const int ne,
  9223. const int ne00, const int ne01,
  9224. const int nb00, const int nb01,
  9225. const int nb02, const int ne10,
  9226. const int ne11, const int nb10,
  9227. const int nb11, const int nb12,
  9228. dpct::queue_ptr stream) {
  9229. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9230. {
  9231. dpct::has_capability_or_fail(stream->get_device(),
  9232. {sycl::aspect::fp16});
  9233. stream->parallel_for(
  9234. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9235. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9236. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9237. [=](sycl::nd_item<3> item_ct1) {
  9238. cpy_f32_f16<cpy_1_f16_f16>(cx, cdst, ne, ne00, ne01, nb00, nb01,
  9239. nb02, ne10, ne11, nb10, nb11, nb12,
  9240. item_ct1);
  9241. });
  9242. }
  9243. }
  9244. static void ggml_cpy_i16_i16_sycl(const char *cx, char *cdst, const int ne,
  9245. const int ne00, const int ne01,
  9246. const int nb00, const int nb01,
  9247. const int nb02, const int ne10,
  9248. const int ne11, const int nb10,
  9249. const int nb11, const int nb12,
  9250. dpct::queue_ptr stream) {
  9251. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9252. {
  9253. // dpct::has_capability_or_fail(stream->get_device(),
  9254. // {sycl::aspect::fp16});
  9255. stream->parallel_for(
  9256. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9257. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9258. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9259. [=](sycl::nd_item<3> item_ct1) {
  9260. cpy_f32_f16<cpy_1_i16_i16>(cx, cdst, ne, ne00, ne01, nb00, nb01,
  9261. nb02, ne10, ne11, nb10, nb11, nb12,
  9262. item_ct1);
  9263. });
  9264. }
  9265. }
  9266. static void ggml_cpy_i32_i32_sycl(const char *cx, char *cdst, const int ne,
  9267. const int ne00, const int ne01,
  9268. const int nb00, const int nb01,
  9269. const int nb02, const int ne10,
  9270. const int ne11, const int nb10,
  9271. const int nb11, const int nb12,
  9272. dpct::queue_ptr stream) {
  9273. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9274. {
  9275. // dpct::has_capability_or_fail(stream->get_device(),
  9276. // {sycl::aspect::fp16});
  9277. stream->parallel_for(
  9278. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9279. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9280. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9281. [=](sycl::nd_item<3> item_ct1) {
  9282. cpy_f32_f16<cpy_1_i32_i32>(cx, cdst, ne, ne00, ne01, nb00, nb01,
  9283. nb02, ne10, ne11, nb10, nb11, nb12,
  9284. item_ct1);
  9285. });
  9286. }
  9287. }
  9288. static void scale_f32_sycl(const float *x, float *dst, const float scale,
  9289. const int k, dpct::queue_ptr stream) {
  9290. const int num_blocks = (k + SYCL_SCALE_BLOCK_SIZE - 1) / SYCL_SCALE_BLOCK_SIZE;
  9291. stream->parallel_for(
  9292. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9293. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE),
  9294. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE)),
  9295. [=](sycl::nd_item<3> item_ct1) {
  9296. scale_f32(x, dst, scale, k, item_ct1);
  9297. });
  9298. }
  9299. static void clamp_f32_sycl(const float *x, float *dst, const float min,
  9300. const float max, const int k,
  9301. dpct::queue_ptr stream) {
  9302. const int num_blocks = (k + SYCL_CLAMP_BLOCK_SIZE - 1) / SYCL_CLAMP_BLOCK_SIZE;
  9303. stream->parallel_for(
  9304. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9305. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE),
  9306. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE)),
  9307. [=](sycl::nd_item<3> item_ct1) {
  9308. clamp_f32(x, dst, min, max, k, item_ct1);
  9309. });
  9310. }
  9311. template <typename T>
  9312. static void rope_sycl(const T *x, T *dst, int ncols, int nrows,
  9313. const int32_t *pos, float freq_scale, int p_delta_rows,
  9314. float freq_base, float ext_factor, float attn_factor,
  9315. rope_corr_dims corr_dims, dpct::queue_ptr stream) {
  9316. GGML_ASSERT(ncols % 2 == 0);
  9317. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  9318. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  9319. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  9320. if (pos == nullptr) {
  9321. /*
  9322. DPCT1049:40: The work-group size passed to the SYCL kernel may exceed
  9323. the limit. To get the device limit, query
  9324. info::device::max_work_group_size. Adjust the work-group size if needed.
  9325. */
  9326. dpct::has_capability_or_fail(stream->get_device(),
  9327. {sycl::aspect::fp16});
  9328. stream->parallel_for(
  9329. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9330. [=](sycl::nd_item<3> item_ct1) {
  9331. rope<T, false>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  9332. freq_base, ext_factor, attn_factor, corr_dims,
  9333. item_ct1);
  9334. });
  9335. } else {
  9336. /*
  9337. DPCT1049:41: The work-group size passed to the SYCL kernel may exceed
  9338. the limit. To get the device limit, query
  9339. info::device::max_work_group_size. Adjust the work-group size if needed.
  9340. */
  9341. dpct::has_capability_or_fail(stream->get_device(),
  9342. {sycl::aspect::fp16});
  9343. stream->parallel_for(
  9344. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9345. [=](sycl::nd_item<3> item_ct1) {
  9346. rope<T, true>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  9347. freq_base, ext_factor, attn_factor, corr_dims,
  9348. item_ct1);
  9349. });
  9350. }
  9351. }
  9352. template <typename T>
  9353. static void rope_neox_sycl(const T *x, T *dst, int ncols, int n_dims, int nrows,
  9354. const int32_t *pos, float freq_scale,
  9355. int p_delta_rows, float freq_base, float ext_factor,
  9356. float attn_factor, rope_corr_dims corr_dims,
  9357. dpct::queue_ptr stream) {
  9358. GGML_ASSERT(ncols % 2 == 0);
  9359. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  9360. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  9361. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  9362. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  9363. const float inv_ndims = -1.0f / n_dims;
  9364. if (pos == nullptr) {
  9365. /*
  9366. DPCT1049:42: The work-group size passed to the SYCL kernel may exceed
  9367. the limit. To get the device limit, query
  9368. info::device::max_work_group_size. Adjust the work-group size if needed.
  9369. */
  9370. dpct::has_capability_or_fail(stream->get_device(),
  9371. {sycl::aspect::fp16});
  9372. stream->parallel_for(
  9373. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9374. [=](sycl::nd_item<3> item_ct1) {
  9375. rope_neox<T, false>(x, dst, ncols, n_dims, pos, freq_scale,
  9376. p_delta_rows, ext_factor, attn_factor,
  9377. corr_dims, theta_scale, inv_ndims,
  9378. item_ct1);
  9379. });
  9380. } else {
  9381. /*
  9382. DPCT1049:43: The work-group size passed to the SYCL kernel may exceed
  9383. the limit. To get the device limit, query
  9384. info::device::max_work_group_size. Adjust the work-group size if needed.
  9385. */
  9386. dpct::has_capability_or_fail(stream->get_device(),
  9387. {sycl::aspect::fp16});
  9388. stream->parallel_for(
  9389. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9390. [=](sycl::nd_item<3> item_ct1) {
  9391. rope_neox<T, true>(x, dst, ncols, n_dims, pos, freq_scale,
  9392. p_delta_rows, ext_factor, attn_factor,
  9393. corr_dims, theta_scale, inv_ndims, item_ct1);
  9394. });
  9395. }
  9396. }
  9397. static void rope_glm_f32_sycl(const float *x, float *dst, int ncols, int nrows,
  9398. const int32_t *pos, float freq_scale,
  9399. int p_delta_rows, float freq_base, int n_ctx,
  9400. dpct::queue_ptr stream) {
  9401. GGML_ASSERT(ncols % 4 == 0);
  9402. const sycl::range<3> block_dims(1, 1, SYCL_ROPE_BLOCK_SIZE / 4);
  9403. const int num_blocks_x = (ncols + SYCL_ROPE_BLOCK_SIZE - 1) / SYCL_ROPE_BLOCK_SIZE;
  9404. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  9405. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9406. [=](sycl::nd_item<3> item_ct1) {
  9407. rope_glm_f32(x, dst, ncols, pos, freq_scale,
  9408. p_delta_rows, freq_base, n_ctx,
  9409. item_ct1);
  9410. });
  9411. }
  9412. static void alibi_f32_sycl(const float *x, float *dst, const int ncols,
  9413. const int nrows, const int k_rows,
  9414. const int n_heads_log2_floor, const float m0,
  9415. const float m1, dpct::queue_ptr stream) {
  9416. const sycl::range<3> block_dims(1, 1, SYCL_ALIBI_BLOCK_SIZE);
  9417. const int num_blocks_x = (ncols + SYCL_ALIBI_BLOCK_SIZE - 1) / (SYCL_ALIBI_BLOCK_SIZE);
  9418. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  9419. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9420. [=](sycl::nd_item<3> item_ct1) {
  9421. alibi_f32(x, dst, ncols, k_rows,
  9422. n_heads_log2_floor, m0, m1, item_ct1);
  9423. });
  9424. }
  9425. static void sum_rows_f32_sycl(const float *x, float *dst, const int ncols,
  9426. const int nrows, dpct::queue_ptr stream) {
  9427. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  9428. const sycl::range<3> block_nums(1, nrows, 1);
  9429. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9430. [=](sycl::nd_item<3> item_ct1)
  9431. [[intel::reqd_sub_group_size(32)]] {
  9432. k_sum_rows_f32(x, dst, ncols, item_ct1);
  9433. });
  9434. }
  9435. static void argsort_f32_i32_sycl(const float *x, int *dst, const int ncols,
  9436. const int nrows, ggml_sort_order order,
  9437. dpct::queue_ptr stream) {
  9438. // bitonic sort requires ncols to be power of 2
  9439. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  9440. const sycl::range<3> block_dims(1, 1, ncols);
  9441. const sycl::range<3> block_nums(1, nrows, 1);
  9442. if (order == GGML_SORT_ASC) {
  9443. /*
  9444. DPCT1049:44: The work-group size passed to the SYCL kernel may exceed
  9445. the limit. To get the device limit, query
  9446. info::device::max_work_group_size. Adjust the work-group size if needed.
  9447. */
  9448. stream->parallel_for(
  9449. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9450. [=](sycl::nd_item<3> item_ct1) {
  9451. k_argsort_f32_i32<GGML_SORT_ASC>(x, dst, ncols, item_ct1);
  9452. });
  9453. } else if (order == GGML_SORT_DESC) {
  9454. /*
  9455. DPCT1049:45: The work-group size passed to the SYCL kernel may exceed
  9456. the limit. To get the device limit, query
  9457. info::device::max_work_group_size. Adjust the work-group size if needed.
  9458. */
  9459. stream->parallel_for(
  9460. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9461. [=](sycl::nd_item<3> item_ct1) {
  9462. k_argsort_f32_i32<GGML_SORT_DESC>(x, dst, ncols, item_ct1);
  9463. });
  9464. } else {
  9465. GGML_ASSERT(false);
  9466. }
  9467. }
  9468. static void diag_mask_inf_f32_sycl(const float *x, float *dst,
  9469. const int ncols_x, const int nrows_x,
  9470. const int rows_per_channel, const int n_past,
  9471. dpct::queue_ptr stream) {
  9472. const sycl::range<3> block_dims(1, SYCL_DIAG_MASK_INF_BLOCK_SIZE, 1);
  9473. const int block_num_x = (ncols_x + SYCL_DIAG_MASK_INF_BLOCK_SIZE - 1) / SYCL_DIAG_MASK_INF_BLOCK_SIZE;
  9474. const sycl::range<3> block_nums(1, block_num_x, nrows_x);
  9475. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9476. [=](sycl::nd_item<3> item_ct1) {
  9477. diag_mask_inf_f32(x, dst, ncols_x,
  9478. rows_per_channel, n_past,
  9479. item_ct1);
  9480. });
  9481. }
  9482. static void soft_max_f32_sycl(const float *x, const float *y, float *dst,
  9483. const int ncols_x, const int nrows_x,
  9484. const int nrows_y, const float scale,
  9485. dpct::queue_ptr stream) {
  9486. int nth = WARP_SIZE;
  9487. while (nth < ncols_x && nth < SYCL_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  9488. const sycl::range<3> block_dims(1, 1, nth);
  9489. const sycl::range<3> block_nums(1, 1, nrows_x);
  9490. /*
  9491. DPCT1049:46: The work-group size passed to the SYCL kernel may exceed the
  9492. limit. To get the device limit, query info::device::max_work_group_size.
  9493. Adjust the work-group size if needed.
  9494. */
  9495. stream->submit([&](sycl::handler &cgh) {
  9496. /*
  9497. DPCT1101:96: 'SYCL_SOFT_MAX_BLOCK_SIZE/WARP_SIZE' expression was
  9498. replaced with a value. Modify the code to use the original expression,
  9499. provided in comments, if it is correct.
  9500. */
  9501. sycl::local_accessor<float, 1> buf_acc_ct1(
  9502. sycl::range<1>(32 /*SYCL_SOFT_MAX_BLOCK_SIZE/WARP_SIZE*/), cgh);
  9503. cgh.parallel_for(
  9504. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9505. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9506. soft_max_f32(x, y, dst, ncols_x, nrows_y, scale, item_ct1,
  9507. buf_acc_ct1.get_pointer());
  9508. });
  9509. });
  9510. }
  9511. static void im2col_f32_f16_sycl(const float *x, sycl::half *dst, int IW, int IH,
  9512. int OW, int OH, int KW, int KH, int IC,
  9513. int offset_delta, int s0, int s1, int p0,
  9514. int p1, int d0, int d1,
  9515. dpct::queue_ptr stream) {
  9516. const int parallel_elements = OW * KW * KH;
  9517. const int num_blocks = (parallel_elements + SYCL_IM2COL_BLOCK_SIZE - 1) / SYCL_IM2COL_BLOCK_SIZE;
  9518. sycl::range<3> block_nums(IC, OH, num_blocks);
  9519. {
  9520. dpct::has_capability_or_fail(stream->get_device(),
  9521. {sycl::aspect::fp16});
  9522. stream->parallel_for(
  9523. sycl::nd_range<3>(block_nums *
  9524. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  9525. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  9526. [=](sycl::nd_item<3> item_ct1) {
  9527. im2col_f32_f16(x, dst, offset_delta, IW, IH, OW, KW, KH,
  9528. parallel_elements, (IC * KH * KW), s0, s1, p0,
  9529. p1, d0, d1, item_ct1);
  9530. });
  9531. }
  9532. }
  9533. // buffer pool for sycl
  9534. #define MAX_SYCL_BUFFERS 256
  9535. struct scoped_spin_lock {
  9536. std::atomic_flag& lock;
  9537. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  9538. while (lock.test_and_set(std::memory_order_acquire)) {
  9539. ; // spin
  9540. }
  9541. }
  9542. ~scoped_spin_lock() {
  9543. lock.clear(std::memory_order_release);
  9544. }
  9545. scoped_spin_lock(const scoped_spin_lock&) = delete;
  9546. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  9547. };
  9548. static std::atomic_flag g_sycl_pool_lock = ATOMIC_FLAG_INIT;
  9549. // #define DEBUG_SYCL_MALLOC
  9550. struct sycl_buffer {
  9551. void * ptr = nullptr;
  9552. size_t size = 0;
  9553. };
  9554. static sycl_buffer g_sycl_buffer_pool[GGML_SYCL_MAX_DEVICES][MAX_SYCL_BUFFERS];
  9555. static size_t g_sycl_pool_size[GGML_SYCL_MAX_DEVICES] = {0};
  9556. static void *ggml_sycl_pool_malloc_leg(size_t size, size_t *actual_size) try {
  9557. scoped_spin_lock lock(g_sycl_pool_lock);
  9558. int id;
  9559. SYCL_CHECK(
  9560. CHECK_TRY_ERROR(id = get_current_device_index()));
  9561. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg index %d\n", id);
  9562. #ifdef DEBUG_SYCL_MALLOC
  9563. int nnz = 0;
  9564. size_t max_size = 0;
  9565. #endif
  9566. size_t best_diff = 1ull << 36;
  9567. int ibest = -1;
  9568. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  9569. sycl_buffer& b = g_sycl_buffer_pool[id][i];
  9570. if (b.ptr != nullptr) {
  9571. #ifdef DEBUG_SYCL_MALLOC
  9572. ++nnz;
  9573. if (b.size > max_size) max_size = b.size;
  9574. #endif
  9575. if (b.size >= size) {
  9576. size_t diff = b.size - size;
  9577. if (diff < best_diff) {
  9578. best_diff = diff;
  9579. ibest = i;
  9580. if (!best_diff) {
  9581. void * ptr = b.ptr;
  9582. *actual_size = b.size;
  9583. b.ptr = nullptr;
  9584. b.size = 0;
  9585. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 1 %p\n", ptr);
  9586. return ptr;
  9587. }
  9588. }
  9589. }
  9590. }
  9591. }
  9592. if (ibest >= 0) {
  9593. sycl_buffer& b = g_sycl_buffer_pool[id][ibest];
  9594. void * ptr = b.ptr;
  9595. *actual_size = b.size;
  9596. b.ptr = nullptr;
  9597. b.size = 0;
  9598. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 2 %p\n", ptr);
  9599. return ptr;
  9600. }
  9601. void * ptr;
  9602. size_t look_ahead_size = (size_t) (1.05 * size);
  9603. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  9604. const dpct::queue_ptr stream = g_syclStreams[id][0];
  9605. SYCL_CHECK(
  9606. CHECK_TRY_ERROR(ptr = (void *)sycl::malloc_device(
  9607. look_ahead_size, *stream)));
  9608. *actual_size = look_ahead_size;
  9609. g_sycl_pool_size[id] += look_ahead_size;
  9610. #ifdef DEBUG_SYCL_MALLOC
  9611. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  9612. (uint32_t)(max_size/1024/1024), (uint32_t)(g_sycl_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  9613. #endif
  9614. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return %p\n", ptr);
  9615. return ptr;
  9616. }
  9617. catch (sycl::exception const &exc) {
  9618. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9619. << ", line:" << __LINE__ << std::endl;
  9620. std::exit(1);
  9621. }
  9622. static void ggml_sycl_pool_free_leg(void *ptr, size_t size) try {
  9623. scoped_spin_lock lock(g_sycl_pool_lock);
  9624. int id;
  9625. SYCL_CHECK(
  9626. CHECK_TRY_ERROR(id = get_current_device_index()));
  9627. const dpct::queue_ptr stream = g_syclStreams[id][0];
  9628. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  9629. sycl_buffer& b = g_sycl_buffer_pool[id][i];
  9630. if (b.ptr == nullptr) {
  9631. b.ptr = ptr;
  9632. b.size = size;
  9633. return;
  9634. }
  9635. }
  9636. fprintf(stderr, "WARNING: sycl buffer pool full, increase MAX_SYCL_BUFFERS\n");
  9637. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *stream)));
  9638. g_sycl_pool_size[id] -= size;
  9639. }
  9640. catch (sycl::exception const &exc) {
  9641. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9642. << ", line:" << __LINE__ << std::endl;
  9643. std::exit(1);
  9644. }
  9645. // pool with virtual memory
  9646. /*
  9647. DPCT1082:64: Migration of CUmemGenericAllocationHandle type is not supported.
  9648. */
  9649. // static std::vector<CUmemGenericAllocationHandle>
  9650. // g_sycl_pool_handles[GGML_SYCL_MAX_DEVICES];
  9651. static dpct::device_ptr g_sycl_pool_addr[GGML_SYCL_MAX_DEVICES] = {0};
  9652. static size_t g_sycl_pool_used[GGML_SYCL_MAX_DEVICES] = {0};
  9653. static const size_t SYCL_POOL_VMM_MAX_SIZE = 1ull << 36; // 64 GB
  9654. static void *ggml_sycl_pool_malloc_vmm(size_t size, size_t *actual_size) try {
  9655. return NULL;
  9656. }
  9657. catch (sycl::exception const &exc) {
  9658. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9659. << ", line:" << __LINE__ << std::endl;
  9660. std::exit(1);
  9661. }
  9662. static void ggml_sycl_pool_free_vmm(void *ptr, size_t size) try {
  9663. scoped_spin_lock lock(g_sycl_pool_lock);
  9664. int id;
  9665. SYCL_CHECK(
  9666. CHECK_TRY_ERROR(id = dpct::dev_mgr::instance().current_device_id()));
  9667. #ifdef DEBUG_SYCL_MALLOC
  9668. printf("sycl pool[%d]: freed %llu bytes at %llx\n", id, (unsigned long long) size, ptr);
  9669. #endif
  9670. g_sycl_pool_used[id] -= size;
  9671. // all deallocations must be in reverse order of the allocations
  9672. GGML_ASSERT(ptr == (void *) (g_sycl_pool_addr[id] + g_sycl_pool_used[id]));
  9673. }
  9674. catch (sycl::exception const &exc) {
  9675. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9676. << ", line:" << __LINE__ << std::endl;
  9677. std::exit(1);
  9678. }
  9679. static void *ggml_sycl_pool_malloc(size_t size, size_t *actual_size) try {
  9680. int id;
  9681. SYCL_CHECK(
  9682. CHECK_TRY_ERROR(id = get_current_device_index()));
  9683. if (g_device_caps[id].vmm) {
  9684. return ggml_sycl_pool_malloc_vmm(size, actual_size);
  9685. } else {
  9686. return ggml_sycl_pool_malloc_leg(size, actual_size);
  9687. }
  9688. }
  9689. catch (sycl::exception const &exc) {
  9690. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9691. << ", line:" << __LINE__ << std::endl;
  9692. std::exit(1);
  9693. }
  9694. static void ggml_sycl_pool_free(void *ptr, size_t size) try {
  9695. int id;
  9696. SYCL_CHECK(
  9697. CHECK_TRY_ERROR(id = get_current_device_index()));
  9698. if (g_device_caps[id].vmm) {
  9699. ggml_sycl_pool_free_vmm(ptr, size);
  9700. } else {
  9701. ggml_sycl_pool_free_leg(ptr, size);
  9702. }
  9703. }
  9704. catch (sycl::exception const &exc) {
  9705. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9706. << ", line:" << __LINE__ << std::endl;
  9707. std::exit(1);
  9708. }
  9709. template<typename T>
  9710. struct sycl_pool_alloc {
  9711. T * ptr = nullptr;
  9712. size_t actual_size = 0;
  9713. // size is in number of elements
  9714. T * alloc(size_t size) {
  9715. GGML_ASSERT(ptr == nullptr);
  9716. ptr = (T *) ggml_sycl_pool_malloc(size * sizeof(T), &this->actual_size);
  9717. // GGML_SYCL_DEBUG("alloc %lu return %p actual size=%lu\n", size * sizeof(T), ptr, this->actual_size);
  9718. return ptr;
  9719. }
  9720. sycl_pool_alloc(size_t size) {
  9721. alloc(size);
  9722. }
  9723. ~sycl_pool_alloc() {
  9724. if (ptr != nullptr) {
  9725. ggml_sycl_pool_free(ptr, actual_size);
  9726. }
  9727. }
  9728. T * get() {
  9729. return ptr;
  9730. }
  9731. sycl_pool_alloc() = default;
  9732. sycl_pool_alloc(const sycl_pool_alloc &) = delete;
  9733. sycl_pool_alloc(sycl_pool_alloc &&) = delete;
  9734. sycl_pool_alloc& operator=(const sycl_pool_alloc &) = delete;
  9735. sycl_pool_alloc& operator=(sycl_pool_alloc &&) = delete;
  9736. };
  9737. static bool g_sycl_loaded = false;
  9738. bool ggml_sycl_loaded(void) {
  9739. return g_sycl_loaded;
  9740. }
  9741. void ggml_backend_sycl_print_sycl_devices(){
  9742. int device_count = dpct::dev_mgr::instance().device_count();
  9743. fprintf(stderr, "found %d SYCL devices:\n", device_count);
  9744. for (int id = 0; id < device_count; ++id) {
  9745. dpct::device_info prop;
  9746. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  9747. prop, dpct::dev_mgr::instance().get_device(id))));
  9748. sycl::device cur_device = dpct::dev_mgr::instance().get_device(id);
  9749. fprintf(stderr, " Device %d: %s,\tcompute capability %d.%d,\n\tmax compute_units %d,\tmax work group size %d,\tmax sub group size %d,\tglobal mem size %lu\n", id,
  9750. prop.get_name(), prop.get_major_version(),
  9751. prop.get_minor_version(),
  9752. prop.get_max_compute_units(),
  9753. prop.get_max_work_group_size(),
  9754. prop.get_max_sub_group_size(),
  9755. prop.get_global_mem_size()
  9756. );
  9757. }
  9758. // fprintf(stderr, "\n");
  9759. }
  9760. int get_sycl_env(const char* env_name, int default_val){
  9761. char * user_device_string = getenv(env_name);
  9762. int user_number = default_val;
  9763. unsigned n;
  9764. if (user_device_string != NULL && sscanf(user_device_string, " %u", &n) == 1) {
  9765. user_number = (int)n;
  9766. } else {
  9767. user_number=default_val;
  9768. }
  9769. return user_number;
  9770. }
  9771. int get_work_group_size(int user_device_id){
  9772. dpct::device_info prop;
  9773. dpct::get_device_info(
  9774. prop,
  9775. dpct::dev_mgr::instance().get_device(user_device_id));
  9776. return prop.get_max_work_group_size();
  9777. }
  9778. void ggml_init_sycl() try {
  9779. static bool initialized = false;
  9780. if (!initialized) {
  9781. g_ggml_sycl_debug = get_sycl_env("GGML_SYCL_DEBUG", 0);
  9782. printf("GGML_SYCL_DEBUG=%d\n", g_ggml_sycl_debug);
  9783. int user_device_id = get_sycl_env("GGML_SYCL_DEVICE", 0);
  9784. if (CHECK_TRY_ERROR(g_all_sycl_device_count =
  9785. dpct::dev_mgr::instance().device_count()) !=
  9786. 0) {
  9787. initialized = true;
  9788. g_sycl_loaded = false;
  9789. return;
  9790. }
  9791. GGML_ASSERT(g_all_sycl_device_count <= GGML_SYCL_MAX_DEVICES);
  9792. int64_t total_vram = 0;
  9793. #if defined(GGML_SYCL_F16)
  9794. fprintf(stderr, "%s: GGML_SYCL_F16: yes\n", __func__);
  9795. #else
  9796. fprintf(stderr, "%s: GGML_SYCL_F16: no\n", __func__);
  9797. #endif
  9798. #if defined(SYCL_USE_XMX)
  9799. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  9800. #else
  9801. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  9802. #endif
  9803. ggml_backend_sycl_print_sycl_devices();
  9804. for (int id = 0; id < GGML_SYCL_MAX_DEVICES; ++id) {
  9805. g_sycl_device_id2index[id].index = -1;
  9806. g_device_caps[id].vmm = 0;
  9807. g_device_caps[id].device_id = -1;
  9808. g_device_caps[id].cc = 0;
  9809. g_tensor_split[id] = 0;
  9810. }
  9811. int device_inx = -1;
  9812. for (int id = 0; id < g_all_sycl_device_count; ++id) {
  9813. if(id!=user_device_id) continue;
  9814. device_inx++;
  9815. int device_vmm = 0;
  9816. g_device_caps[device_inx].vmm = !!device_vmm;
  9817. g_device_caps[device_inx].device_id = id;
  9818. g_sycl_device_id2index[id].index = device_inx;
  9819. dpct::device_info prop;
  9820. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  9821. prop, dpct::dev_mgr::instance().get_device(id))));
  9822. // fprintf(stderr,
  9823. // " Device %d: %s, compute capability %d.%d, VMM: %s\n", id,
  9824. // prop.get_name(), prop.get_major_version(),
  9825. // prop.get_minor_version(), device_vmm ? "yes" : "no");
  9826. g_tensor_split[device_inx] = total_vram;
  9827. total_vram += prop.get_global_mem_size();
  9828. g_device_caps[device_inx].cc =
  9829. 100 * prop.get_major_version() + 10 * prop.get_minor_version();
  9830. // printf("g_device_caps[%d].cc=%d\n", device_inx, g_device_caps[device_inx].cc);
  9831. }
  9832. device_inx = -1;
  9833. for (int id = 0; id < g_all_sycl_device_count; ++id) {
  9834. if(id!=user_device_id) continue;
  9835. device_inx++;
  9836. g_tensor_split[device_inx] /= total_vram;
  9837. }
  9838. device_inx = -1;
  9839. for (int id = 0; id < g_all_sycl_device_count; ++id) {
  9840. if(id!=user_device_id) continue;
  9841. device_inx++;
  9842. SYCL_CHECK(ggml_sycl_set_device(id));
  9843. // create sycl streams
  9844. for (int is = 0; is < MAX_STREAMS; ++is) {
  9845. /*
  9846. DPCT1025:88: The SYCL queue is created ignoring the flag and
  9847. priority options.
  9848. */
  9849. SYCL_CHECK(CHECK_TRY_ERROR(
  9850. g_syclStreams[device_inx][is] =
  9851. dpct::get_current_device().create_queue()));
  9852. }
  9853. const dpct::queue_ptr stream = g_syclStreams[device_inx][0];
  9854. // create sycl handle
  9855. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[device_inx] =
  9856. stream));
  9857. /*
  9858. DPCT1027:89: The call to syclSetMathMode was replaced with 0
  9859. because this functionality is redundant in SYCL.
  9860. */
  9861. SYCL_CHECK(0);
  9862. }
  9863. // configure logging to stdout
  9864. // SYCL_CHECK(syclLoggerConfigure(1, 1, 0, nullptr));
  9865. //hardcode, force set to 1 device
  9866. g_device_count = 1;
  9867. ggml_sycl_set_main_device(user_device_id);
  9868. ggml_sycl_set_device(user_device_id);
  9869. g_work_group_size = get_work_group_size(user_device_id);
  9870. // fprintf(stderr, "Using Device %d\n", user_device_id);
  9871. // for (int id = 0; id < g_all_sycl_device_count; ++id) {
  9872. // GGML_SYCL_DEBUG("id=%d g_device_caps[%d].device_id=%d g_sycl_device_id2index[%d].index=%d ", id, id,
  9873. // g_device_caps[id].device_id, id, g_sycl_device_id2index[id].index);
  9874. // }
  9875. initialized = true;
  9876. g_sycl_loaded = true;
  9877. }
  9878. }
  9879. catch (sycl::exception const &exc) {
  9880. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9881. << ", line:" << __LINE__ << std::endl;
  9882. std::exit(1);
  9883. }
  9884. void ggml_sycl_set_tensor_split(const float * tensor_split) {
  9885. if (tensor_split == nullptr) {
  9886. return;
  9887. }
  9888. bool all_zero = true;
  9889. for (int i = 0; i < g_device_count; ++i) {
  9890. if (tensor_split[i] != 0.0f) {
  9891. all_zero = false;
  9892. break;
  9893. }
  9894. }
  9895. if (all_zero) {
  9896. return;
  9897. }
  9898. float split_sum = 0.0f;
  9899. for (int i = 0; i < g_device_count; ++i) {
  9900. g_tensor_split[i] = split_sum;
  9901. split_sum += tensor_split[i];
  9902. }
  9903. for (int i = 0; i < g_device_count; ++i) {
  9904. g_tensor_split[i] /= split_sum;
  9905. }
  9906. }
  9907. void *ggml_sycl_host_malloc(size_t size) try {
  9908. if (getenv("GGML_SYCL_NO_PINNED") != nullptr) {
  9909. return nullptr;
  9910. }
  9911. void * ptr = nullptr;
  9912. //allow to use dpct::get_in_order_queue() for host malloc
  9913. dpct::err0 err = CHECK_TRY_ERROR(
  9914. ptr = (void *)sycl::malloc_host(size, dpct::get_in_order_queue()));
  9915. /*
  9916. DPCT1000:82: Error handling if-stmt was detected but could not be rewritten.
  9917. */
  9918. if (err != 0) {
  9919. // clear the error
  9920. /*
  9921. DPCT1026:83: The call to syclGetLastError was removed because this
  9922. functionality is redundant in SYCL.
  9923. */
  9924. /*
  9925. DPCT1001:81: The statement could not be removed.
  9926. */
  9927. fprintf(
  9928. stderr,
  9929. "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  9930. /*
  9931. DPCT1009:84: SYCL uses exceptions to report errors and does not use
  9932. the error codes. The original code was commented out and a warning
  9933. string was inserted. You need to rewrite this code.
  9934. */
  9935. size / 1024.0 / 1024.0,
  9936. "syclGetErrorString is not supported" /*syclGetErrorString(err)*/);
  9937. return nullptr;
  9938. }
  9939. return ptr;
  9940. }
  9941. catch (sycl::exception const &exc) {
  9942. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9943. << ", line:" << __LINE__ << std::endl;
  9944. std::exit(1);
  9945. }
  9946. void ggml_sycl_host_free(void *ptr) try {
  9947. //allow to use dpct::get_in_order_queue() for host malloc
  9948. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, dpct::get_in_order_queue())));
  9949. }
  9950. catch (sycl::exception const &exc) {
  9951. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9952. << ", line:" << __LINE__ << std::endl;
  9953. std::exit(1);
  9954. }
  9955. static dpct::err0 ggml_sycl_cpy_tensor_2d(void *dst,
  9956. const struct ggml_tensor *src,
  9957. int64_t i3, int64_t i2,
  9958. int64_t i1_low, int64_t i1_high,
  9959. dpct::queue_ptr stream) try {
  9960. dpct::memcpy_direction kind;
  9961. char * src_ptr;
  9962. if (src->backend == GGML_BACKEND_CPU) {
  9963. kind = dpct::host_to_device;
  9964. src_ptr = (char *) src->data;
  9965. // GGML_SYCL_DEBUG("ggml_sycl_cpy_tensor_2d GGML_BACKEND_CPU src_ptr %p\n", src_ptr);
  9966. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  9967. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  9968. kind = dpct::device_to_device;
  9969. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  9970. int id;
  9971. SYCL_CHECK(CHECK_TRY_ERROR(
  9972. id = get_current_device_index()));
  9973. // GGML_SYCL_DEBUG("current device index %d\n", id);
  9974. src_ptr = (char *) extra->data_device[id];
  9975. } else {
  9976. // GGML_SYCL_DEBUG("GGML_ASSERT(false)\n");
  9977. GGML_ASSERT(false);
  9978. }
  9979. char * dst_ptr = (char *) dst;
  9980. const int64_t ne0 = src->ne[0];
  9981. const int64_t nb0 = src->nb[0];
  9982. const int64_t nb1 = src->nb[1];
  9983. const int64_t nb2 = src->nb[2];
  9984. const int64_t nb3 = src->nb[3];
  9985. const enum ggml_type type = src->type;
  9986. const int64_t ts = ggml_type_size(type);
  9987. const int64_t bs = ggml_blck_size(type);
  9988. int64_t i1_diff = i1_high - i1_low;
  9989. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  9990. if (nb0 == ts && nb1 == ts*ne0/bs) {
  9991. // GGML_SYCL_DEBUG("stream->memcpy: dst_ptr=%p, x=%p, size=%lu\n", dst_ptr, x, i1_diff * nb1);
  9992. // return CHECK_TRY_ERROR(stream->memcpy(dst_ptr, x, i1_diff * nb1));
  9993. return CHECK_TRY_ERROR(dpct::async_dpct_memcpy(dst_ptr, x, i1_diff * nb1,
  9994. kind, *stream));
  9995. } else if (nb0 == ts) {
  9996. return CHECK_TRY_ERROR(
  9997. dpct::async_dpct_memcpy(dst_ptr, ts * ne0 / bs, x, nb1,
  9998. ts * ne0 / bs, i1_diff, kind, *stream));
  9999. } else {
  10000. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  10001. const void * rx = (const void *) ((const char *) x + i1*nb1);
  10002. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  10003. // pretend the row is a matrix with cols=1
  10004. dpct::err0 r = CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  10005. rd, ts / bs, rx, nb0, ts / bs, ne0, kind, *stream));
  10006. /*
  10007. DPCT1001:85: The statement could not be removed.
  10008. */
  10009. /*
  10010. DPCT1000:86: Error handling if-stmt was detected but could not be
  10011. rewritten.
  10012. */
  10013. if (r != 0) return r;
  10014. }
  10015. return 0;
  10016. }
  10017. }
  10018. catch (sycl::exception const &exc) {
  10019. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10020. << ", line:" << __LINE__ << std::endl;
  10021. std::exit(1);
  10022. }
  10023. static void ggml_sycl_op_get_rows(const ggml_tensor *src0,
  10024. const ggml_tensor *src1, ggml_tensor *dst,
  10025. const float *src0_d, const float *src1_d,
  10026. float *dst_d, const dpct::queue_ptr &stream) {
  10027. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  10028. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10029. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  10030. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  10031. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  10032. const int32_t * src1_i32 = (const int32_t *) src1_d;
  10033. switch (src0->type) {
  10034. case GGML_TYPE_F16:
  10035. get_rows_sycl_float(src0, src1, dst, (const sycl::half *)src0_d,
  10036. src1_i32, dst_d, stream);
  10037. break;
  10038. case GGML_TYPE_F32:
  10039. get_rows_sycl_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10040. break;
  10041. case GGML_TYPE_Q4_0:
  10042. get_rows_sycl<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10043. break;
  10044. case GGML_TYPE_Q4_1:
  10045. get_rows_sycl<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10046. break;
  10047. case GGML_TYPE_Q5_0:
  10048. get_rows_sycl<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10049. break;
  10050. case GGML_TYPE_Q5_1:
  10051. get_rows_sycl<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10052. break;
  10053. case GGML_TYPE_Q8_0:
  10054. get_rows_sycl<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10055. break;
  10056. default:
  10057. // TODO: k-quants
  10058. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  10059. GGML_ASSERT(false);
  10060. break;
  10061. }
  10062. }
  10063. template <class op>
  10064. inline void ggml_sycl_op_bin_bcast(const ggml_tensor *src0,
  10065. const ggml_tensor *src1, ggml_tensor *dst,
  10066. const float *src0_dd, const float *src1_dd,
  10067. float *dst_dd,
  10068. const dpct::queue_ptr &main_stream) {
  10069. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  10070. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  10071. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  10072. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd,
  10073. (sycl::half *)dst_dd, main_stream);
  10074. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  10075. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd, dst_dd,
  10076. main_stream);
  10077. } else if (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  10078. op()(src0, src1, dst, (const int32_t *)src0_dd, (const int32_t *)src1_dd, (int32_t *)dst_dd,
  10079. main_stream);
  10080. } else if (src0->type == GGML_TYPE_I16 && dst->type == GGML_TYPE_I16) {
  10081. op()(src0, src1, dst, (const int16_t *)src0_dd, (const int16_t *)src1_dd, (int16_t *)dst_dd,
  10082. main_stream);
  10083. } else {
  10084. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  10085. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  10086. GGML_ASSERT(false);
  10087. }
  10088. }
  10089. static void ggml_sycl_op_repeat(const ggml_tensor *src0,
  10090. const ggml_tensor *src1, ggml_tensor *dst,
  10091. const float *src0_d, const float *src1_d,
  10092. float *dst_d,
  10093. const dpct::queue_ptr &main_stream) {
  10094. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  10095. (void) src1;
  10096. (void) src1_d;
  10097. }
  10098. inline void ggml_sycl_op_add(const ggml_tensor *src0, const ggml_tensor *src1,
  10099. ggml_tensor *dst, const float *src0_dd,
  10100. const float *src1_dd, float *dst_dd,
  10101. const dpct::queue_ptr &main_stream) {
  10102. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  10103. }
  10104. inline void ggml_sycl_op_acc(const ggml_tensor *src0, const ggml_tensor *src1,
  10105. ggml_tensor *dst, const float *src0_dd,
  10106. const float *src1_dd, float *dst_dd,
  10107. const dpct::queue_ptr &main_stream) {
  10108. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10109. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10110. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10111. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  10112. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  10113. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  10114. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  10115. int offset = dst->op_params[3] / 4; // offset in bytes
  10116. 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);
  10117. (void) dst;
  10118. }
  10119. inline void ggml_sycl_op_mul(const ggml_tensor *src0, const ggml_tensor *src1,
  10120. ggml_tensor *dst, const float *src0_dd,
  10121. const float *src1_dd, float *dst_dd,
  10122. const dpct::queue_ptr &main_stream) {
  10123. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  10124. }
  10125. inline void ggml_sycl_op_div(const ggml_tensor *src0, const ggml_tensor *src1,
  10126. ggml_tensor *dst, const float *src0_dd,
  10127. const float *src1_dd, float *dst_dd,
  10128. const dpct::queue_ptr &main_stream) {
  10129. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  10130. }
  10131. inline void ggml_sycl_op_gelu(const ggml_tensor *src0, const ggml_tensor *src1,
  10132. ggml_tensor *dst, const float *src0_dd,
  10133. const float *src1_dd, float *dst_dd,
  10134. const dpct::queue_ptr &main_stream) {
  10135. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10136. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10137. gelu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10138. (void) src1;
  10139. (void) dst;
  10140. (void) src1_dd;
  10141. }
  10142. inline void ggml_sycl_op_silu(const ggml_tensor *src0, const ggml_tensor *src1,
  10143. ggml_tensor *dst, const float *src0_dd,
  10144. const float *src1_dd, float *dst_dd,
  10145. const dpct::queue_ptr &main_stream) {
  10146. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10147. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10148. silu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10149. (void) src1;
  10150. (void) dst;
  10151. (void) src1_dd;
  10152. }
  10153. inline void ggml_sycl_op_gelu_quick(const ggml_tensor *src0,
  10154. const ggml_tensor *src1, ggml_tensor *dst,
  10155. const float *src0_dd, const float *src1_dd,
  10156. float *dst_dd,
  10157. const dpct::queue_ptr &main_stream) {
  10158. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10159. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10160. gelu_quick_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10161. (void) src1;
  10162. (void) dst;
  10163. (void) src1_dd;
  10164. }
  10165. inline void ggml_sycl_op_tanh(const ggml_tensor *src0, const ggml_tensor *src1,
  10166. ggml_tensor *dst, const float *src0_dd,
  10167. const float *src1_dd, float *dst_dd,
  10168. const dpct::queue_ptr &main_stream) {
  10169. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10170. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10171. tanh_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10172. (void) src1;
  10173. (void) dst;
  10174. (void) src1_dd;
  10175. }
  10176. inline void ggml_sycl_op_relu(const ggml_tensor *src0, const ggml_tensor *src1,
  10177. ggml_tensor *dst, const float *src0_dd,
  10178. const float *src1_dd, float *dst_dd,
  10179. const dpct::queue_ptr &main_stream) {
  10180. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10181. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10182. relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10183. (void) src1;
  10184. (void) dst;
  10185. (void) src1_dd;
  10186. }
  10187. inline void ggml_sycl_op_leaky_relu(const ggml_tensor *src0,
  10188. const ggml_tensor *src1, ggml_tensor *dst,
  10189. const float *src0_dd, const float *src1_dd,
  10190. float *dst_dd,
  10191. const dpct::queue_ptr &main_stream) {
  10192. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10193. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10194. float negative_slope;
  10195. memcpy(&negative_slope, dst->op_params, sizeof(float));
  10196. leaky_relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  10197. (void) src1;
  10198. (void) dst;
  10199. (void) src1_dd;
  10200. }
  10201. inline void ggml_sycl_op_sqr(const ggml_tensor *src0, const ggml_tensor *src1,
  10202. ggml_tensor *dst, const float *src0_dd,
  10203. const float *src1_dd, float *dst_dd,
  10204. const dpct::queue_ptr &main_stream) {
  10205. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10206. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10207. sqr_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10208. (void) src1;
  10209. (void) dst;
  10210. (void) src1_dd;
  10211. }
  10212. inline void ggml_sycl_op_norm(const ggml_tensor *src0, const ggml_tensor *src1,
  10213. ggml_tensor *dst, const float *src0_dd,
  10214. const float *src1_dd, float *dst_dd,
  10215. const dpct::queue_ptr &main_stream) {
  10216. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10217. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10218. const int64_t ne00 = src0->ne[0];
  10219. const int64_t nrows = ggml_nrows(src0);
  10220. float eps;
  10221. memcpy(&eps, dst->op_params, sizeof(float));
  10222. norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  10223. (void) src1;
  10224. (void) dst;
  10225. (void) src1_dd;
  10226. }
  10227. inline void ggml_sycl_op_group_norm(const ggml_tensor *src0,
  10228. const ggml_tensor *src1, ggml_tensor *dst,
  10229. const float *src0_dd, const float *src1_dd,
  10230. float *dst_dd,
  10231. const dpct::queue_ptr &main_stream) {
  10232. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10233. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10234. int num_groups = dst->op_params[0];
  10235. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  10236. group_norm_f32_sycl(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  10237. (void) src1;
  10238. (void) dst;
  10239. (void) src1_dd;
  10240. }
  10241. inline void ggml_sycl_op_concat(const ggml_tensor *src0,
  10242. const ggml_tensor *src1, ggml_tensor *dst,
  10243. const float *src0_dd, const float *src1_dd,
  10244. float *dst_dd,
  10245. const dpct::queue_ptr &main_stream) {
  10246. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10247. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10248. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10249. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  10250. 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);
  10251. }
  10252. (void) src1;
  10253. (void) dst;
  10254. }
  10255. inline void ggml_sycl_op_upscale(const ggml_tensor *src0,
  10256. const ggml_tensor *src1, ggml_tensor *dst,
  10257. const float *src0_dd, const float *src1_dd,
  10258. float *dst_dd,
  10259. const dpct::queue_ptr &main_stream) {
  10260. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10261. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10262. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  10263. const int scale_factor = dst->op_params[0];
  10264. upscale_f32_sycl(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  10265. (void) src1;
  10266. (void) dst;
  10267. (void) src1_dd;
  10268. }
  10269. inline void ggml_sycl_op_pad(const ggml_tensor *src0, const ggml_tensor *src1,
  10270. ggml_tensor *dst, const float *src0_dd,
  10271. const float *src1_dd, float *dst_dd,
  10272. const dpct::queue_ptr &main_stream) {
  10273. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10274. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10275. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  10276. pad_f32_sycl(src0_dd, dst_dd,
  10277. src0->ne[0], src0->ne[1], src0->ne[2],
  10278. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  10279. (void) src1;
  10280. (void) dst;
  10281. (void) src1_dd;
  10282. }
  10283. inline void ggml_sycl_op_rms_norm(const ggml_tensor *src0,
  10284. const ggml_tensor *src1, ggml_tensor *dst,
  10285. const float *src0_dd, const float *src1_dd,
  10286. float *dst_dd,
  10287. const dpct::queue_ptr &main_stream) {
  10288. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10289. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10290. const int64_t ne00 = src0->ne[0];
  10291. const int64_t nrows = ggml_nrows(src0);
  10292. float eps;
  10293. memcpy(&eps, dst->op_params, sizeof(float));
  10294. rms_norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  10295. (void) src1;
  10296. (void) dst;
  10297. (void) src1_dd;
  10298. }
  10299. inline void ggml_sycl_op_mul_mat_q(
  10300. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  10301. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  10302. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  10303. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  10304. const dpct::queue_ptr &stream) try {
  10305. const int64_t ne00 = src0->ne[0];
  10306. const int64_t ne10 = src1->ne[0];
  10307. GGML_ASSERT(ne10 % QK8_1 == 0);
  10308. const int64_t ne0 = dst->ne[0];
  10309. const int64_t row_diff = row_high - row_low;
  10310. int device_id;
  10311. SYCL_CHECK(
  10312. CHECK_TRY_ERROR(device_id = dpct::dev_mgr::instance().current_device_id()));
  10313. // the main device has a larger memory buffer to hold the results from all GPUs
  10314. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  10315. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && device_id == g_main_device ? ne0 : row_diff;
  10316. switch (src0->type) {
  10317. case GGML_TYPE_Q4_0:
  10318. 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);
  10319. break;
  10320. case GGML_TYPE_Q4_1:
  10321. 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);
  10322. break;
  10323. case GGML_TYPE_Q5_0:
  10324. 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);
  10325. break;
  10326. case GGML_TYPE_Q5_1:
  10327. 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);
  10328. break;
  10329. case GGML_TYPE_Q8_0:
  10330. 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);
  10331. break;
  10332. case GGML_TYPE_Q2_K:
  10333. 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);
  10334. break;
  10335. case GGML_TYPE_Q3_K:
  10336. 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);
  10337. break;
  10338. case GGML_TYPE_Q4_K:
  10339. 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);
  10340. break;
  10341. case GGML_TYPE_Q5_K:
  10342. 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);
  10343. break;
  10344. case GGML_TYPE_Q6_K:
  10345. 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);
  10346. break;
  10347. default:
  10348. GGML_ASSERT(false);
  10349. break;
  10350. }
  10351. (void) src1;
  10352. (void) dst;
  10353. (void) src1_ddf_i;
  10354. }
  10355. catch (sycl::exception const &exc) {
  10356. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10357. << ", line:" << __LINE__ << std::endl;
  10358. std::exit(1);
  10359. }
  10360. static int64_t get_row_rounding(ggml_type type) {
  10361. int64_t min_compute_capability = INT_MAX;
  10362. int64_t max_compute_capability = INT_MIN;
  10363. for (int64_t id = 0; id < g_device_count; ++id) {
  10364. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  10365. if (min_compute_capability > g_device_caps[id].cc) {
  10366. min_compute_capability = g_device_caps[id].cc;
  10367. }
  10368. if (max_compute_capability < g_device_caps[id].cc) {
  10369. max_compute_capability = g_device_caps[id].cc;
  10370. }
  10371. }
  10372. }
  10373. switch(type) {
  10374. case GGML_TYPE_Q4_0:
  10375. case GGML_TYPE_Q4_1:
  10376. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  10377. case GGML_TYPE_Q5_0:
  10378. case GGML_TYPE_Q5_1:
  10379. case GGML_TYPE_Q8_0:
  10380. return 64;
  10381. case GGML_TYPE_F16:
  10382. case GGML_TYPE_F32:
  10383. return 1;
  10384. case GGML_TYPE_Q2_K:
  10385. case GGML_TYPE_Q3_K:
  10386. case GGML_TYPE_Q4_K:
  10387. case GGML_TYPE_Q5_K:
  10388. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  10389. case GGML_TYPE_Q6_K:
  10390. return 64;
  10391. default:
  10392. GGML_ASSERT(false);
  10393. }
  10394. }
  10395. inline void ggml_sycl_op_mul_mat_vec_q(
  10396. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  10397. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  10398. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  10399. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  10400. const dpct::queue_ptr &stream) {
  10401. GGML_ASSERT(ggml_nrows(src1) == 1);
  10402. const int64_t ne00 = src0->ne[0];
  10403. const int64_t row_diff = row_high - row_low;
  10404. switch (src0->type) {
  10405. case GGML_TYPE_Q4_0:
  10406. mul_mat_vec_q4_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10407. break;
  10408. case GGML_TYPE_Q4_1:
  10409. mul_mat_vec_q4_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10410. break;
  10411. case GGML_TYPE_Q5_0:
  10412. mul_mat_vec_q5_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10413. break;
  10414. case GGML_TYPE_Q5_1:
  10415. mul_mat_vec_q5_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10416. break;
  10417. case GGML_TYPE_Q8_0:
  10418. mul_mat_vec_q8_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10419. break;
  10420. case GGML_TYPE_Q2_K:
  10421. mul_mat_vec_q2_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10422. break;
  10423. case GGML_TYPE_Q3_K:
  10424. mul_mat_vec_q3_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10425. break;
  10426. case GGML_TYPE_Q4_K:
  10427. mul_mat_vec_q4_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10428. break;
  10429. case GGML_TYPE_Q5_K:
  10430. mul_mat_vec_q5_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10431. break;
  10432. case GGML_TYPE_Q6_K:
  10433. mul_mat_vec_q6_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10434. break;
  10435. default:
  10436. GGML_ASSERT(false);
  10437. break;
  10438. }
  10439. (void) src1;
  10440. (void) dst;
  10441. (void) src1_ddf_i;
  10442. (void) src1_ncols;
  10443. (void) src1_padded_row_size;
  10444. }
  10445. inline void ggml_sycl_op_dequantize_mul_mat_vec(
  10446. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  10447. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  10448. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  10449. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  10450. const dpct::queue_ptr &stream) {
  10451. const int64_t ne00 = src0->ne[0];
  10452. const int64_t row_diff = row_high - row_low;
  10453. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  10454. #ifdef GGML_SYCL_F16
  10455. sycl_pool_alloc<sycl::half> src1_dfloat_a;
  10456. sycl::half *src1_dfloat = nullptr; // dfloat == half
  10457. bool src1_convert_f16 =
  10458. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  10459. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  10460. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  10461. if (src1_convert_f16) {
  10462. if (src1->type == GGML_TYPE_F16) {
  10463. src1_dfloat = (sycl::half *)src1->data + src1_padded_row_size;
  10464. } else {
  10465. src1_dfloat = src1_dfloat_a.alloc(ne00);
  10466. ggml_cpy_f32_f16_sycl((const char *)src1_ddf_i, (char *)src1_dfloat,
  10467. ne00, ne00, 1, sizeof(float), 0, 0, ne00, 1,
  10468. sizeof(sycl::half), 0, 0, stream);
  10469. }
  10470. }
  10471. #else
  10472. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  10473. #endif // GGML_SYCL_F16
  10474. switch (src0->type) {
  10475. case GGML_TYPE_Q4_0:
  10476. dequantize_mul_mat_vec_q4_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10477. break;
  10478. case GGML_TYPE_Q4_1:
  10479. dequantize_mul_mat_vec_q4_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10480. break;
  10481. case GGML_TYPE_Q5_0:
  10482. dequantize_mul_mat_vec_q5_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10483. break;
  10484. case GGML_TYPE_Q5_1:
  10485. dequantize_mul_mat_vec_q5_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10486. break;
  10487. case GGML_TYPE_Q8_0:
  10488. dequantize_mul_mat_vec_q8_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10489. break;
  10490. case GGML_TYPE_Q2_K:
  10491. dequantize_mul_mat_vec_q2_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10492. break;
  10493. case GGML_TYPE_Q3_K:
  10494. dequantize_mul_mat_vec_q3_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10495. break;
  10496. case GGML_TYPE_Q4_K:
  10497. dequantize_mul_mat_vec_q4_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10498. break;
  10499. case GGML_TYPE_Q5_K:
  10500. dequantize_mul_mat_vec_q5_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10501. break;
  10502. case GGML_TYPE_Q6_K:
  10503. dequantize_mul_mat_vec_q6_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10504. break;
  10505. case GGML_TYPE_F16:
  10506. convert_mul_mat_vec_f16_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10507. break;
  10508. default:
  10509. GGML_ASSERT(false);
  10510. break;
  10511. }
  10512. (void) src1;
  10513. (void) dst;
  10514. (void) src1_ddq_i;
  10515. (void) src1_ncols;
  10516. (void) src1_padded_row_size;
  10517. }
  10518. inline void ggml_sycl_op_mul_mat_sycl(
  10519. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  10520. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  10521. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  10522. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  10523. const dpct::queue_ptr &stream) try {
  10524. GGML_ASSERT(src0_dd_i != nullptr);
  10525. GGML_ASSERT(src1_ddf_i != nullptr);
  10526. GGML_ASSERT(dst_dd_i != nullptr);
  10527. const int64_t ne00 = src0->ne[0];
  10528. const int64_t ne10 = src1->ne[0];
  10529. const int64_t ne0 = dst->ne[0];
  10530. const int64_t row_diff = row_high - row_low;
  10531. int id;
  10532. int device_id = dpct::dev_mgr::instance().current_device_id();
  10533. SYCL_CHECK(
  10534. CHECK_TRY_ERROR(id = get_current_device_index()));
  10535. // the main device has a larger memory buffer to hold the results from all GPUs
  10536. // ldc == nrows of the matrix that cuBLAS writes into
  10537. int ldc = dst->backend == GGML_BACKEND_GPU && device_id == g_main_device ? ne0 : row_diff;
  10538. const int compute_capability = g_device_caps[id].cc;
  10539. #ifdef GGML_SYCL_F16
  10540. bool use_fp16 = true; // TODO(Yu) SYCL capability check
  10541. #else
  10542. bool use_fp16 = false;
  10543. #endif
  10544. // if (compute_capability >= VER_GEN9 && (src0->type == GGML_TYPE_F16 ||
  10545. // ggml_is_quantized(src0->type)) && ggml_is_contiguous(src0) && row_diff ==
  10546. // src0->ne[1] && dst->op_params[0] == GGML_PREC_DEFAULT) {
  10547. if ((src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  10548. use_fp16 && ggml_is_contiguous(src0) && row_diff == src0->ne[1] &&
  10549. dst->op_params[0] == GGML_PREC_DEFAULT) {
  10550. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  10551. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp16 path\n");
  10552. sycl_pool_alloc<sycl::half> src0_as_f16;
  10553. if (src0->type != GGML_TYPE_F16) {
  10554. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src0->type);
  10555. GGML_ASSERT(to_fp16_sycl != nullptr);
  10556. size_t ne = row_diff*ne00;
  10557. src0_as_f16.alloc(ne);
  10558. to_fp16_sycl(src0_dd_i, src0_as_f16.get(), ne, stream);
  10559. }
  10560. const sycl::half *src0_ptr = src0->type == GGML_TYPE_F16
  10561. ? (const sycl::half *)src0_dd_i
  10562. : src0_as_f16.get();
  10563. sycl_pool_alloc<sycl::half> src1_as_f16;
  10564. if (src1->type != GGML_TYPE_F16) {
  10565. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  10566. GGML_ASSERT(to_fp16_sycl != nullptr);
  10567. size_t ne = src1_ncols*ne10;
  10568. src1_as_f16.alloc(ne);
  10569. to_fp16_sycl(src1_ddf_i, src1_as_f16.get(), ne, stream);
  10570. }
  10571. const sycl::half *src1_ptr = src1->type == GGML_TYPE_F16
  10572. ? (const sycl::half *)src1->data + src1_padded_row_size
  10573. : src1_as_f16.get();
  10574. sycl_pool_alloc<sycl::half> dst_f16(row_diff * src1_ncols);
  10575. const sycl::half alpha_f16 = 1.0f;
  10576. const sycl::half beta_f16 = 0.0f;
  10577. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  10578. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
  10579. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  10580. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  10581. &alpha_f16, src0_ptr, dpct::library_data_t::real_half, ne00,
  10582. src1_ptr, dpct::library_data_t::real_half, ne10, &beta_f16,
  10583. dst_f16.get(), dpct::library_data_t::real_half, ldc,
  10584. dpct::library_data_t::real_half)));
  10585. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  10586. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  10587. }
  10588. else {
  10589. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
  10590. sycl_pool_alloc<float> src0_ddq_as_f32;
  10591. if (src0->type != GGML_TYPE_F32) {
  10592. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src0->type);
  10593. GGML_ASSERT(to_fp32_sycl != nullptr);
  10594. src0_ddq_as_f32.alloc(row_diff*ne00);
  10595. to_fp32_sycl(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  10596. }
  10597. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  10598. const float alpha = 1.0f;
  10599. const float beta = 0.0f;
  10600. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  10601. SYCL_CHECK(CHECK_TRY_ERROR(oneapi::mkl::blas::column_major::gemm(
  10602. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  10603. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  10604. dpct::get_value(&alpha, *g_sycl_handles[id]), src0_ddf_i, ne00,
  10605. src1_ddf_i, ne10, dpct::get_value(&beta, *g_sycl_handles[id]),
  10606. dst_dd_i, ldc)));
  10607. }
  10608. (void) dst;
  10609. (void) src1_ddq_i;
  10610. (void) src1_padded_row_size;
  10611. }
  10612. catch (sycl::exception const &exc) {
  10613. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10614. << ", line:" << __LINE__ << std::endl;
  10615. std::exit(1);
  10616. }
  10617. inline void ggml_sycl_op_rope(const ggml_tensor *src0, const ggml_tensor *src1,
  10618. ggml_tensor *dst, const float *src0_dd,
  10619. const float *src1_dd, float *dst_dd,
  10620. const dpct::queue_ptr &main_stream) {
  10621. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  10622. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  10623. GGML_ASSERT(src0->type == dst->type);
  10624. const int64_t ne00 = src0->ne[0];
  10625. const int64_t ne01 = src0->ne[1];
  10626. const int64_t ne2 = dst->ne[2];
  10627. const int64_t nrows = ggml_nrows(src0);
  10628. //const int n_past = ((int32_t *) dst->op_params)[0];
  10629. const int n_dims = ((int32_t *) dst->op_params)[1];
  10630. const int mode = ((int32_t *) dst->op_params)[2];
  10631. const int n_ctx = ((int32_t *) dst->op_params)[3];
  10632. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  10633. // RoPE alteration for extended context
  10634. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  10635. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  10636. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  10637. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  10638. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  10639. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  10640. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  10641. const int32_t * pos = nullptr;
  10642. if ((mode & 1) == 0) {
  10643. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  10644. GGML_ASSERT(src1->ne[0] == ne2);
  10645. pos = (const int32_t *) src1_dd;
  10646. }
  10647. const bool is_neox = mode & 2;
  10648. const bool is_glm = mode & 4;
  10649. rope_corr_dims corr_dims;
  10650. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  10651. // compute
  10652. if (is_glm) {
  10653. GGML_ASSERT(false);
  10654. rope_glm_f32_sycl(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  10655. } else if (is_neox) {
  10656. if (src0->type == GGML_TYPE_F32) {
  10657. rope_neox_sycl(
  10658. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  10659. attn_factor, corr_dims, main_stream
  10660. );
  10661. } else if (src0->type == GGML_TYPE_F16) {
  10662. rope_neox_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd,
  10663. ne00, n_dims, nrows, pos, freq_scale, ne01,
  10664. freq_base, ext_factor, attn_factor, corr_dims,
  10665. main_stream);
  10666. } else {
  10667. GGML_ASSERT(false);
  10668. }
  10669. } else {
  10670. if (src0->type == GGML_TYPE_F32) {
  10671. rope_sycl(
  10672. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  10673. attn_factor, corr_dims, main_stream
  10674. );
  10675. } else if (src0->type == GGML_TYPE_F16) {
  10676. rope_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd, ne00,
  10677. nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  10678. attn_factor, corr_dims, main_stream);
  10679. } else {
  10680. GGML_ASSERT(false);
  10681. }
  10682. }
  10683. (void) src1;
  10684. (void) dst;
  10685. (void) src1_dd;
  10686. }
  10687. inline void ggml_sycl_op_alibi(const ggml_tensor *src0, const ggml_tensor *src1,
  10688. ggml_tensor *dst, const float *src0_dd,
  10689. const float *src1_dd, float *dst_dd,
  10690. const dpct::queue_ptr &main_stream) {
  10691. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10692. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10693. const int64_t ne00 = src0->ne[0];
  10694. const int64_t ne01 = src0->ne[1];
  10695. const int64_t ne02 = src0->ne[2];
  10696. const int64_t nrows = ggml_nrows(src0);
  10697. //const int n_past = ((int32_t *) dst->op_params)[0];
  10698. const int n_head = ((int32_t *) dst->op_params)[1];
  10699. float max_bias;
  10700. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  10701. //GGML_ASSERT(ne01 + n_past == ne00);
  10702. GGML_ASSERT(n_head == ne02);
  10703. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  10704. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  10705. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  10706. alibi_f32_sycl(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  10707. (void) src1;
  10708. (void) src1_dd;
  10709. }
  10710. inline void ggml_sycl_op_im2col(const ggml_tensor *src0,
  10711. const ggml_tensor *src1, ggml_tensor *dst,
  10712. const float *src0_dd, const float *src1_dd,
  10713. float *dst_dd,
  10714. const dpct::queue_ptr &main_stream) {
  10715. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  10716. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10717. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  10718. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  10719. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  10720. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  10721. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  10722. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  10723. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  10724. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  10725. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  10726. const int64_t IH = is_2D ? src1->ne[1] : 1;
  10727. const int64_t IW = src1->ne[0];
  10728. const int64_t KH = is_2D ? src0->ne[1] : 1;
  10729. const int64_t KW = src0->ne[0];
  10730. const int64_t OH = is_2D ? dst->ne[2] : 1;
  10731. const int64_t OW = dst->ne[1];
  10732. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  10733. im2col_f32_f16_sycl(src1_dd, (sycl::half *)dst_dd, IW, IH, OW, OH, KW, KH,
  10734. IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  10735. (void) src0;
  10736. (void) src0_dd;
  10737. }
  10738. inline void ggml_sycl_op_sum_rows(const ggml_tensor *src0,
  10739. const ggml_tensor *src1, ggml_tensor *dst,
  10740. const float *src0_dd, const float *src1_dd,
  10741. float *dst_dd,
  10742. const dpct::queue_ptr &main_stream) {
  10743. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10744. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10745. const int64_t ncols = src0->ne[0];
  10746. const int64_t nrows = ggml_nrows(src0);
  10747. sum_rows_f32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
  10748. (void) src1;
  10749. (void) dst;
  10750. (void) src1_dd;
  10751. }
  10752. inline void ggml_sycl_op_argsort(const ggml_tensor *src0,
  10753. const ggml_tensor *src1, ggml_tensor *dst,
  10754. const float *src0_dd, const float *src1_dd,
  10755. float *dst_dd,
  10756. const dpct::queue_ptr &main_stream) {
  10757. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10758. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  10759. const int64_t ncols = src0->ne[0];
  10760. const int64_t nrows = ggml_nrows(src0);
  10761. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  10762. argsort_f32_i32_sycl(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  10763. (void) src1;
  10764. (void) dst;
  10765. (void) src1_dd;
  10766. }
  10767. inline void ggml_sycl_op_diag_mask_inf(const ggml_tensor *src0,
  10768. const ggml_tensor *src1,
  10769. ggml_tensor *dst, const float *src0_dd,
  10770. const float *src1_dd, float *dst_dd,
  10771. const dpct::queue_ptr &main_stream) {
  10772. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10773. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10774. const int64_t ne00 = src0->ne[0];
  10775. const int64_t ne01 = src0->ne[1];
  10776. const int nrows0 = ggml_nrows(src0);
  10777. const int n_past = ((int32_t *) dst->op_params)[0];
  10778. diag_mask_inf_f32_sycl(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  10779. (void) src1;
  10780. (void) dst;
  10781. (void) src1_dd;
  10782. }
  10783. inline void ggml_sycl_op_soft_max(const ggml_tensor *src0,
  10784. const ggml_tensor *src1, ggml_tensor *dst,
  10785. const float *src0_dd, const float *src1_dd,
  10786. float *dst_dd,
  10787. const dpct::queue_ptr &main_stream) {
  10788. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10789. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10790. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  10791. const int64_t ne00 = src0->ne[0];
  10792. const int64_t nrows_x = ggml_nrows(src0);
  10793. const int64_t nrows_y = src1 ? ggml_nrows(src1) : 1;
  10794. float scale = 1.0f;
  10795. memcpy(&scale, dst->op_params, sizeof(float));
  10796. soft_max_f32_sycl(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  10797. (void) dst;
  10798. }
  10799. inline void ggml_sycl_op_scale(const ggml_tensor *src0, const ggml_tensor *src1,
  10800. ggml_tensor *dst, const float *src0_dd,
  10801. const float *src1_dd, float *dst_dd,
  10802. const dpct::queue_ptr &main_stream) {
  10803. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10804. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10805. float scale;
  10806. memcpy(&scale, dst->op_params, sizeof(float));
  10807. scale_f32_sycl(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  10808. /*
  10809. DPCT1010:87: SYCL uses exceptions to report errors and does not use the
  10810. error codes. The call was replaced with 0. You need to rewrite this code.
  10811. */
  10812. SYCL_CHECK(0);
  10813. (void) src1;
  10814. (void) dst;
  10815. (void) src1_dd;
  10816. }
  10817. inline void ggml_sycl_op_clamp(const ggml_tensor *src0, const ggml_tensor *src1,
  10818. ggml_tensor *dst, const float *src0_dd,
  10819. const float *src1_dd, float *dst_dd,
  10820. const dpct::queue_ptr &main_stream) {
  10821. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10822. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10823. float min;
  10824. float max;
  10825. memcpy(&min, dst->op_params, sizeof(float));
  10826. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  10827. clamp_f32_sycl(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  10828. /*
  10829. DPCT1010:88: SYCL uses exceptions to report errors and does not use the
  10830. error codes. The call was replaced with 0. You need to rewrite this code.
  10831. */
  10832. SYCL_CHECK(0);
  10833. (void) src1;
  10834. (void) dst;
  10835. (void) src1_dd;
  10836. }
  10837. static void ggml_sycl_op_flatten(const ggml_tensor *src0,
  10838. const ggml_tensor *src1, ggml_tensor *dst,
  10839. const ggml_sycl_op_flatten_t op) try {
  10840. const int64_t nrows0 = ggml_nrows(src0);
  10841. const bool use_src1 = src1 != nullptr;
  10842. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  10843. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  10844. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  10845. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  10846. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  10847. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  10848. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  10849. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  10850. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  10851. // dd = data device
  10852. float * src0_ddf = nullptr;
  10853. float * src1_ddf = nullptr;
  10854. float * dst_ddf = nullptr;
  10855. sycl_pool_alloc<float> src0_f;
  10856. sycl_pool_alloc<float> src1_f;
  10857. sycl_pool_alloc<float> dst_f;
  10858. ggml_sycl_set_device(g_main_device);
  10859. dpct::queue_ptr main_stream = g_syclStreams[g_main_device_index][0];
  10860. // GGML_SYCL_DEBUG("g_main_device_index=%d, main_stream=%p src0_on_device=%d, src1_on_device=%d, dst_on_device=%d\n",
  10861. // g_main_device_index, main_stream, src0_on_device, src1_on_device, dst_on_device);
  10862. if (src0_on_device) {
  10863. src0_ddf = (float *) src0_extra->data_device[g_main_device_index];
  10864. } else {
  10865. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  10866. // GGML_SYCL_DEBUG("before ggml_sycl_cpy_tensor_2d src0_ddf=%p, src0=%p\n", src0_ddf, src0);
  10867. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  10868. }
  10869. if (use_src1) {
  10870. if (src1_on_device) {
  10871. src1_ddf = (float *) src1_extra->data_device[g_main_device_index];
  10872. } else {
  10873. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  10874. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  10875. }
  10876. }
  10877. if (dst_on_device) {
  10878. dst_ddf = (float *) dst_extra->data_device[g_main_device_index];
  10879. // printf("zjy dst_ddf=%p main_stream=%p g_main_device_index=%d\n", dst_ddf, main_stream, g_main_device_index);
  10880. } else {
  10881. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  10882. }
  10883. // GGML_SYCL_DEBUG("op src0=%p, src1=%p, dst=%p, src0_ddf=%p, src1_ddf=%p, dst_ddf=%p, main_stream=%p\n",
  10884. // src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  10885. // do the computation
  10886. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  10887. /*
  10888. DPCT1010:89: SYCL uses exceptions to report errors and does not use the
  10889. error codes. The call was replaced with 0. You need to rewrite this code.
  10890. */
  10891. SYCL_CHECK(0);
  10892. // copy dst to host if necessary
  10893. if (!dst_on_device) {
  10894. SYCL_CHECK(CHECK_TRY_ERROR(
  10895. main_stream->memcpy(dst->data, dst_ddf, ggml_nbytes(dst))));
  10896. }
  10897. if (dst->backend == GGML_BACKEND_CPU) {
  10898. SYCL_CHECK(CHECK_TRY_ERROR(
  10899. dpct::get_current_device().queues_wait_and_throw()));
  10900. }
  10901. // print_ggml_tensor("tensor", dst);
  10902. }
  10903. catch (sycl::exception const &exc) {
  10904. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10905. << ", line:" << __LINE__ << std::endl;
  10906. std::exit(1);
  10907. }
  10908. static void ggml_sycl_set_peer_access(const int n_tokens) {
  10909. static bool peer_access_enabled = false;
  10910. const bool enable_peer_access = n_tokens <= GGML_SYCL_PEER_MAX_BATCH_SIZE;
  10911. if (peer_access_enabled == enable_peer_access) {
  10912. return;
  10913. }
  10914. #ifdef NDEBUG
  10915. for (int id = 0; id < g_device_count; ++id) {
  10916. SYCL_CHECK(ggml_sycl_set_device(get_device_id_by_index(id)));
  10917. // SYCL_CHECK(syclDeviceSynchronize());
  10918. }
  10919. for (int id = 0; id < g_device_count; ++id) {
  10920. SYCL_CHECK(ggml_sycl_set_device(get_device_id_by_index(id)));
  10921. int device_id = g_device_caps[id].device_id;
  10922. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  10923. int device_id_other = g_device_caps[id_other].device_id;
  10924. if (device_id == id_other) {
  10925. continue;
  10926. }
  10927. if (device_id != g_main_device && device_id_other != g_main_device) {
  10928. continue;
  10929. }
  10930. int can_access_peer;
  10931. // SYCL_CHECK(syclDeviceCanAccessPeer(&can_access_peer, id, id_other));
  10932. // if (can_access_peer) {
  10933. // if (enable_peer_access) {
  10934. // SYCL_CHECK(syclDeviceEnablePeerAccess(id_other, 0));
  10935. // } else {
  10936. // SYCL_CHECK(syclDeviceDisablePeerAccess(id_other));
  10937. // }
  10938. // }
  10939. }
  10940. }
  10941. #endif // NDEBUG
  10942. peer_access_enabled = enable_peer_access;
  10943. }
  10944. static void ggml_sycl_op_mul_mat(const ggml_tensor *src0,
  10945. const ggml_tensor *src1, ggml_tensor *dst,
  10946. ggml_sycl_op_mul_mat_t op,
  10947. const bool convert_src1_to_q8_1) try {
  10948. const int64_t ne00 = src0->ne[0];
  10949. const int64_t ne01 = src0->ne[1];
  10950. const int64_t ne02 = src0->ne[2];
  10951. const int64_t ne03 = src0->ne[3];
  10952. const int64_t nrows0 = ggml_nrows(src0);
  10953. const int64_t ne10 = src1->ne[0];
  10954. const int64_t ne11 = src1->ne[1];
  10955. const int64_t ne12 = src1->ne[2];
  10956. const int64_t ne13 = src1->ne[3];
  10957. const int64_t nrows1 = ggml_nrows(src1);
  10958. GGML_ASSERT(ne03 == ne13);
  10959. const int64_t ne0 = dst->ne[0];
  10960. const int64_t ne1 = dst->ne[1];
  10961. const int nb2 = dst->nb[2];
  10962. const int nb3 = dst->nb[3];
  10963. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  10964. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  10965. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  10966. const int64_t i02_divisor = ne12 / ne02;
  10967. const size_t src0_ts = ggml_type_size(src0->type);
  10968. const size_t src0_bs = ggml_blck_size(src0->type);
  10969. const size_t q8_1_ts = sizeof(block_q8_1);
  10970. const size_t q8_1_bs = QK8_1;
  10971. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  10972. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  10973. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  10974. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  10975. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  10976. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  10977. int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  10978. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  10979. GGML_ASSERT(!(split && ne02 > 1));
  10980. GGML_ASSERT(!(split && ne03 > 1));
  10981. GGML_ASSERT(!(split && ne02 < ne12));
  10982. // dd = data device
  10983. char * src0_dd[GGML_SYCL_MAX_DEVICES] = {nullptr};
  10984. float * src1_ddf[GGML_SYCL_MAX_DEVICES] = {nullptr}; // float
  10985. char * src1_ddq[GGML_SYCL_MAX_DEVICES] = {nullptr}; // q8_1
  10986. float * dst_dd[GGML_SYCL_MAX_DEVICES] = {nullptr};
  10987. // as = actual size
  10988. size_t src0_as[GGML_SYCL_MAX_DEVICES] = {0};
  10989. size_t src1_asf[GGML_SYCL_MAX_DEVICES] = {0};
  10990. size_t src1_asq[GGML_SYCL_MAX_DEVICES] = {0};
  10991. size_t dst_as[GGML_SYCL_MAX_DEVICES] = {0};
  10992. int64_t row_low[GGML_SYCL_MAX_DEVICES];
  10993. int64_t row_high[GGML_SYCL_MAX_DEVICES];
  10994. int used_devices = 0;
  10995. for (int64_t id = 0; id < g_device_count; ++id) {
  10996. // by default, use all rows
  10997. row_low[id] = 0;
  10998. row_high[id] = ne01;
  10999. // for multi GPU, get the row boundaries from tensor split
  11000. // and round to mul_mat_q tile sizes
  11001. if (split) {
  11002. const int64_t rounding = get_row_rounding(src0->type);
  11003. if (id != 0) {
  11004. row_low[id] = ne01*g_tensor_split[id];
  11005. if (row_low[id] < ne01) {
  11006. row_low[id] -= row_low[id] % rounding;
  11007. }
  11008. }
  11009. if (id != g_device_count - 1) {
  11010. row_high[id] = ne01*g_tensor_split[id + 1];
  11011. if (row_high[id] < ne01) {
  11012. row_high[id] -= row_high[id] % rounding;
  11013. }
  11014. }
  11015. }
  11016. }
  11017. for (int64_t id = 0; id < g_device_count; ++id) {
  11018. if ((!split && id != g_main_device_index) || row_low[id] == row_high[id]) {
  11019. continue;
  11020. }
  11021. used_devices++;
  11022. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device_index;
  11023. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device_index;
  11024. ggml_sycl_set_device(get_device_id_by_index(id));
  11025. const dpct::queue_ptr stream = g_syclStreams[id][0];
  11026. if (src0_on_device && src0_is_contiguous) {
  11027. src0_dd[id] = (char *) src0_extra->data_device[id];
  11028. } else {
  11029. // const size_t size_src0_ddq = split ? (row_high[id]-row_low[id])*ne00 * src0_ts/src0_bs : ggml_nbytes(src0);
  11030. src0_dd[id] = (char *) ggml_sycl_pool_malloc(ggml_nbytes(src0), &src0_as[id]);
  11031. }
  11032. if (src1_on_device && src1_is_contiguous) {
  11033. src1_ddf[id] = (float *) src1_extra->data_device[id];
  11034. } else {
  11035. src1_ddf[id] = (float *) ggml_sycl_pool_malloc(ggml_nbytes(src1), &src1_asf[id]);
  11036. }
  11037. if (convert_src1_to_q8_1) {
  11038. src1_ddq[id] = (char *) ggml_sycl_pool_malloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs, &src1_asq[id]);
  11039. if (src1_on_device && src1_is_contiguous) {
  11040. quantize_row_q8_1_sycl(src1_ddf[id], src1_ddq[id], ne10, nrows1, src1_padded_col_size, stream);
  11041. /*
  11042. DPCT1010:90: SYCL uses exceptions to report errors and does not
  11043. use the error codes. The call was replaced with 0. You need to
  11044. rewrite this code.
  11045. */
  11046. SYCL_CHECK(0);
  11047. }
  11048. }
  11049. if (dst_on_device) {
  11050. dst_dd[id] = (float *) dst_extra->data_device[id];
  11051. } else {
  11052. const size_t size_dst_ddf = split ? (row_high[id]-row_low[id])*ne1*sizeof(float) : ggml_nbytes(dst);
  11053. dst_dd[id] = (float *) ggml_sycl_pool_malloc(size_dst_ddf, &dst_as[id]);
  11054. }
  11055. }
  11056. // if multiple devices are used they need to wait for the main device
  11057. // here an event is recorded that signals that the main device has finished calculating the input data
  11058. if (split && used_devices > 1) {
  11059. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11060. /*
  11061. DPCT1024:91: The original code returned the error code that was further
  11062. consumed by the program logic. This original code was replaced with 0.
  11063. You may need to rewrite the program logic consuming the error code.
  11064. */
  11065. SYCL_CHECK(CHECK_TRY_ERROR(
  11066. *src0_extra->events[g_main_device_index][0] =
  11067. g_syclStreams[g_main_device_index][0]->ext_oneapi_submit_barrier()));
  11068. }
  11069. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  11070. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  11071. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  11072. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  11073. for (int64_t id = 0; id < g_device_count; ++id) {
  11074. if ((!split && id != g_main_device_index) || row_low[id] == row_high[id]) {
  11075. continue;
  11076. }
  11077. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device_index;
  11078. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device_index;
  11079. const int64_t row_diff = row_high[id] - row_low[id];
  11080. ggml_sycl_set_device(get_device_id_by_index(id));
  11081. const dpct::queue_ptr stream = g_syclStreams[id][is];
  11082. // wait for main GPU data if necessary
  11083. if (split && (id != g_main_device_index || is != 0)) {
  11084. SYCL_CHECK(CHECK_TRY_ERROR(stream->ext_oneapi_submit_barrier(
  11085. {*src0_extra->events[g_main_device_index][0]})));
  11086. }
  11087. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  11088. const int64_t i03 = i0 / ne12;
  11089. const int64_t i02 = i0 % ne12;
  11090. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  11091. // for split tensors the data begins at i0 == i0_offset_low
  11092. char * src0_dd_i = src0_dd[id] + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  11093. float * src1_ddf_i = src1_ddf[id] + (i0*ne11 + src1_col_0) * ne10;
  11094. char * src1_ddq_i = src1_ddq[id] + src1_ddq_i_offset;
  11095. float * dst_dd_i = dst_dd[id] + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  11096. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  11097. // in that case an offset on dst_ddf_i is needed
  11098. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device_index) {
  11099. dst_dd_i += row_low[id]; // offset is 0 if no tensor split
  11100. }
  11101. // copy src0, src1 to device if necessary
  11102. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  11103. if (id != g_main_device_index) {
  11104. if (convert_src1_to_q8_1) {
  11105. char * src1_ddq_i_source = src1_ddq[g_main_device_index] + src1_ddq_i_offset;
  11106. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  11107. src1_ddq_i, src1_ddq_i_source,
  11108. src1_ncols * src1_padded_col_size * q8_1_ts /
  11109. q8_1_bs)));
  11110. } else {
  11111. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device_index];
  11112. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  11113. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  11114. src1_ddf_i, src1_ddf_i_source,
  11115. src1_ncols * ne10 * sizeof(float))));
  11116. }
  11117. }
  11118. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  11119. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(
  11120. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  11121. } else {
  11122. GGML_ASSERT(false);
  11123. }
  11124. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  11125. quantize_row_q8_1_sycl(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  11126. /*
  11127. DPCT1010:92: SYCL uses exceptions to report errors and does
  11128. not use the error codes. The call was replaced with 0. You
  11129. need to rewrite this code.
  11130. */
  11131. SYCL_CHECK(0);
  11132. }
  11133. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  11134. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, row_low[id], row_high[id], stream));
  11135. }
  11136. if (src1->type == GGML_TYPE_F16) {
  11137. src1_padded_col_size = (i0 * ne11 + src1_col_0) * ne10;
  11138. }
  11139. // do the computation
  11140. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  11141. row_low[id], row_high[id], src1_ncols, src1_padded_col_size, stream);
  11142. /*
  11143. DPCT1010:93: SYCL uses exceptions to report errors and does not
  11144. use the error codes. The call was replaced with 0. You need to
  11145. rewrite this code.
  11146. */
  11147. SYCL_CHECK(0);
  11148. // copy dst to host or other device if necessary
  11149. if (!dst_on_device) {
  11150. void * dst_off_device;
  11151. dpct::memcpy_direction kind;
  11152. if (dst->backend == GGML_BACKEND_CPU) {
  11153. dst_off_device = dst->data;
  11154. kind = dpct::device_to_host;
  11155. } else if (dst->backend == GGML_BACKEND_GPU) {
  11156. dst_off_device = dst_extra->data_device[g_main_device_index];
  11157. kind = dpct::device_to_device;
  11158. } else {
  11159. GGML_ASSERT(false);
  11160. }
  11161. if (split) {
  11162. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  11163. // dst is NOT transposed.
  11164. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  11165. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  11166. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  11167. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  11168. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  11169. dhf_dst_i += src1_col_0*ne0 + row_low[id];
  11170. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  11171. dhf_dst_i, ne0 * sizeof(float), dst_dd_i,
  11172. row_diff * sizeof(float), row_diff * sizeof(float),
  11173. src1_ncols, kind, *stream)));
  11174. } else {
  11175. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  11176. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  11177. dhf_dst_i += src1_col_0*ne0;
  11178. SYCL_CHECK(CHECK_TRY_ERROR(
  11179. stream->memcpy(dhf_dst_i, dst_dd_i,
  11180. src1_ncols * ne0 * sizeof(float))));
  11181. }
  11182. }
  11183. // add event for the main device to wait on until other device is done
  11184. if (split && (id != g_main_device_index || is != 0)) {
  11185. /*
  11186. DPCT1024:94: The original code returned the error code that
  11187. was further consumed by the program logic. This original
  11188. code was replaced with 0. You may need to rewrite the
  11189. program logic consuming the error code.
  11190. */
  11191. SYCL_CHECK(CHECK_TRY_ERROR(
  11192. *src0_extra->events[id][is] =
  11193. stream->ext_oneapi_submit_barrier()));
  11194. }
  11195. }
  11196. }
  11197. }
  11198. for (int64_t id = 0; id < g_device_count; ++id) {
  11199. if ((!split && id != g_main_device_index) || row_low[id] == row_high[id]) {
  11200. continue;
  11201. }
  11202. SYCL_CHECK(ggml_sycl_set_device(get_device_id_by_index(id)));
  11203. // free buffers again when done
  11204. if (dst_as[id] > 0) {
  11205. ggml_sycl_pool_free(dst_dd[id], dst_as[id]);
  11206. }
  11207. if (src1_asq[id] > 0) {
  11208. ggml_sycl_pool_free(src1_ddq[id], src1_asq[id]);
  11209. }
  11210. if (src1_asf[id] > 0) {
  11211. ggml_sycl_pool_free(src1_ddf[id], src1_asf[id]);
  11212. }
  11213. if (src0_as[id] > 0) {
  11214. ggml_sycl_pool_free(src0_dd[id], src0_as[id]);
  11215. }
  11216. }
  11217. // main device waits for all other devices to be finished
  11218. if (split && g_device_count > 1) {
  11219. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  11220. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  11221. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11222. for (int64_t id = 0; id < g_device_count; ++id) {
  11223. if (row_low[id] == row_high[id]) {
  11224. continue;
  11225. }
  11226. for (int64_t is = 0; is < is_max; ++is) {
  11227. SYCL_CHECK(CHECK_TRY_ERROR(
  11228. g_syclStreams[g_main_device_index][0]->ext_oneapi_submit_barrier(
  11229. {*src0_extra->events[id][is]})));
  11230. }
  11231. }
  11232. }
  11233. if (dst->backend == GGML_BACKEND_CPU) {
  11234. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11235. SYCL_CHECK(CHECK_TRY_ERROR(
  11236. dpct::get_current_device().queues_wait_and_throw()));
  11237. }
  11238. }
  11239. catch (sycl::exception const &exc) {
  11240. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11241. << ", line:" << __LINE__ << std::endl;
  11242. std::exit(1);
  11243. }
  11244. static void ggml_sycl_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11245. GGML_SYCL_DEBUG("call %s\n", __func__);
  11246. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_repeat);
  11247. }
  11248. static void ggml_sycl_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11249. GGML_SYCL_DEBUG("call %s\n", __func__);
  11250. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_get_rows);
  11251. }
  11252. static void ggml_sycl_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11253. GGML_SYCL_DEBUG("call %s\n", __func__);
  11254. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_add);
  11255. // log_tensor_with_cnt("log_ggml_sycl_add_src0", (struct ggml_tensor *) src0, 6);
  11256. // log_tensor_with_cnt("log_ggml_sycl_add_src1", (struct ggml_tensor *)src1, 6);
  11257. // log_tensor_with_cnt("log_ggml_sycl_add_dst", dst, 6);
  11258. }
  11259. static void ggml_sycl_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11260. GGML_SYCL_DEBUG("call %s\n", __func__);
  11261. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_acc);
  11262. }
  11263. static void ggml_sycl_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11264. GGML_SYCL_DEBUG("call %s\n", __func__);
  11265. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_mul);
  11266. // log_tensor_with_cnt("log_ggml_sycl_mul_src0", (struct ggml_tensor *)src0, 6);
  11267. // log_tensor_with_cnt("log_ggml_sycl_mul_src1", (struct ggml_tensor *)src1, 6);
  11268. // log_tensor_with_cnt("log_ggml_sycl_mul_dst", dst, 6);
  11269. }
  11270. static void ggml_sycl_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11271. GGML_SYCL_DEBUG("call %s\n", __func__);
  11272. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_div);
  11273. }
  11274. static void ggml_sycl_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11275. GGML_SYCL_DEBUG("call %s\n", __func__);
  11276. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu);
  11277. }
  11278. static void ggml_sycl_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11279. GGML_SYCL_DEBUG("call %s\n", __func__);
  11280. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_silu);
  11281. }
  11282. static void ggml_sycl_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11283. GGML_SYCL_DEBUG("call %s\n", __func__);
  11284. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu_quick);
  11285. }
  11286. static void ggml_sycl_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11287. GGML_SYCL_DEBUG("call %s\n", __func__);
  11288. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_tanh);
  11289. }
  11290. static void ggml_sycl_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11291. GGML_SYCL_DEBUG("call %s\n", __func__);
  11292. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_relu);
  11293. }
  11294. static void ggml_sycl_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11295. GGML_SYCL_DEBUG("call %s\n", __func__);
  11296. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_leaky_relu);
  11297. }
  11298. static void ggml_sycl_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11299. GGML_SYCL_DEBUG("call %s\n", __func__);
  11300. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sqr);
  11301. }
  11302. static void ggml_sycl_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11303. GGML_SYCL_DEBUG("call %s\n", __func__);
  11304. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_norm);
  11305. }
  11306. static void ggml_sycl_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11307. GGML_SYCL_DEBUG("call %s\n", __func__);
  11308. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_group_norm);
  11309. }
  11310. static void ggml_sycl_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11311. GGML_SYCL_DEBUG("call %s\n", __func__);
  11312. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_concat);
  11313. }
  11314. static void ggml_sycl_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11315. GGML_SYCL_DEBUG("call %s\n", __func__);
  11316. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_upscale);
  11317. }
  11318. static void ggml_sycl_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11319. GGML_SYCL_DEBUG("call %s\n", __func__);
  11320. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pad);
  11321. }
  11322. static void ggml_sycl_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11323. GGML_SYCL_DEBUG("call %s\n", __func__);
  11324. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rms_norm);
  11325. // log_tensor_with_cnt("log_ggml_sycl_rms_norm_src0", (struct ggml_tensor *)src0, 6);
  11326. // log_tensor_with_cnt("log_ggml_sycl_rms_norm_src1", (struct ggml_tensor *)src1, 6);
  11327. // log_tensor_with_cnt("log_ggml_sycl_rms_norm_dst", dst, 6);
  11328. }
  11329. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  11330. if (!g_sycl_loaded) return false;
  11331. const int64_t ne10 = src1->ne[0];
  11332. const int64_t ne0 = dst->ne[0];
  11333. const int64_t ne1 = dst->ne[1];
  11334. // TODO: find the optimal values for these
  11335. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  11336. src1->type == GGML_TYPE_F32 &&
  11337. dst->type == GGML_TYPE_F32 &&
  11338. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  11339. }
  11340. static void ggml_sycl_mul_mat_vec_p021(const ggml_tensor *src0,
  11341. const ggml_tensor *src1,
  11342. ggml_tensor *dst) try {
  11343. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  11344. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  11345. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  11346. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  11347. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  11348. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11349. const int64_t ne00 = src0->ne[0];
  11350. const int64_t ne01 = src0->ne[1];
  11351. const int64_t ne02 = src0->ne[2];
  11352. const int64_t ne12 = src1->ne[2];
  11353. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11354. dpct::queue_ptr main_stream = g_syclStreams[g_main_device_index][0];
  11355. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11356. void * src0_ddq = src0_extra->data_device[g_main_device_index];
  11357. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11358. float * src1_ddf = (float *) src1_extra->data_device[g_main_device_index];
  11359. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11360. float * dst_ddf = (float *) dst_extra->data_device[g_main_device_index];
  11361. ggml_mul_mat_p021_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  11362. }
  11363. catch (sycl::exception const &exc) {
  11364. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11365. << ", line:" << __LINE__ << std::endl;
  11366. std::exit(1);
  11367. }
  11368. static void ggml_sycl_mul_mat_vec_nc(const ggml_tensor *src0,
  11369. const ggml_tensor *src1,
  11370. ggml_tensor *dst) try {
  11371. GGML_ASSERT(!ggml_is_transposed(src0));
  11372. GGML_ASSERT(!ggml_is_transposed(src1));
  11373. GGML_ASSERT(!ggml_is_permuted(src0));
  11374. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  11375. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  11376. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11377. const int64_t ne00 = src0->ne[0];
  11378. const int64_t ne01 = src0->ne[1];
  11379. const int64_t ne02 = src0->ne[2];
  11380. const int64_t nb01 = src0->nb[1];
  11381. const int64_t nb02 = src0->nb[2];
  11382. const int64_t ne12 = src1->ne[2];
  11383. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11384. dpct::queue_ptr main_stream = g_syclStreams[g_main_device_index][0];
  11385. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11386. void * src0_ddq = src0_extra->data_device[g_main_device_index];
  11387. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11388. float * src1_ddf = (float *) src1_extra->data_device[g_main_device_index];
  11389. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11390. float * dst_ddf = (float *) dst_extra->data_device[g_main_device_index];
  11391. const int64_t row_stride_x = nb01 / sizeof(sycl::half);
  11392. const int64_t channel_stride_x = nb02 / sizeof(sycl::half);
  11393. 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);
  11394. }
  11395. catch (sycl::exception const &exc) {
  11396. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11397. << ", line:" << __LINE__ << std::endl;
  11398. std::exit(1);
  11399. }
  11400. static void k_compute_batched_ptrs(const sycl::half *src0_as_f16,
  11401. const sycl::half *src1_as_f16, char *dst,
  11402. const void **ptrs_src, void **ptrs_dst,
  11403. int64_t ne12, int64_t ne13, int64_t ne23,
  11404. size_t nb02, size_t nb03, size_t nb12,
  11405. size_t nb13, size_t nbd2, size_t nbd3,
  11406. int64_t r2, int64_t r3,
  11407. const sycl::nd_item<3> &item_ct1) {
  11408. int64_t i13 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  11409. item_ct1.get_local_id(2);
  11410. int64_t i12 = item_ct1.get_group(1) * item_ct1.get_local_range(1) +
  11411. item_ct1.get_local_id(1);
  11412. if (i13 >= ne13 || i12 >= ne12) {
  11413. return;
  11414. }
  11415. int64_t i03 = i13 / r3;
  11416. int64_t i02 = i12 / r2;
  11417. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  11418. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12/2 + i13*nb13/2;
  11419. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  11420. }
  11421. static void ggml_sycl_mul_mat_mat_batched_sycl(const ggml_tensor *src0,
  11422. const ggml_tensor *src1,
  11423. ggml_tensor *dst) try {
  11424. GGML_ASSERT(!ggml_is_transposed(src0));
  11425. GGML_ASSERT(!ggml_is_transposed(src1));
  11426. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  11427. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  11428. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11429. const int64_t ne00 = src0->ne[0]; GGML_UNUSED(ne00);
  11430. const int64_t ne01 = src0->ne[1];
  11431. const int64_t ne02 = src0->ne[2];
  11432. const int64_t ne03 = src0->ne[3];
  11433. const int64_t nb01 = src0->nb[1];
  11434. const int64_t nb02 = src0->nb[2]; GGML_UNUSED(nb02);
  11435. const int64_t nb03 = src0->nb[3]; GGML_UNUSED(nb03);
  11436. const int64_t ne10 = src1->ne[0];
  11437. const int64_t ne11 = src1->ne[1];
  11438. const int64_t ne12 = src1->ne[2];
  11439. const int64_t ne13 = src1->ne[3];
  11440. const int64_t nb11 = src1->nb[1];
  11441. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  11442. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  11443. const int64_t ne1 = ggml_nelements(src1);
  11444. const int64_t ne = ggml_nelements(dst);
  11445. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11446. dpct::queue_ptr main_stream = g_syclStreams[g_main_device_index][0];
  11447. SYCL_CHECK(
  11448. CHECK_TRY_ERROR(g_sycl_handles[g_main_device_index] = main_stream));
  11449. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11450. void * src0_ddq = src0_extra->data_device[g_main_device_index];
  11451. sycl::half *src0_as_f16 = (sycl::half *)src0_ddq;
  11452. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11453. float * src1_ddf = (float *) src1_extra->data_device[g_main_device_index];
  11454. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11455. float * dst_ddf = (float *) dst_extra->data_device[g_main_device_index];
  11456. // convert src1 to fp16
  11457. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  11458. GGML_ASSERT(to_fp16_sycl != nullptr);
  11459. sycl_pool_alloc<sycl::half> src1_as_f16(ne1);
  11460. to_fp16_sycl(src1_ddf, src1_as_f16.get(), ne1, main_stream);
  11461. sycl_pool_alloc<sycl::half> dst_f16;
  11462. char * dst_t;
  11463. dpct::library_data_t cu_compute_type = dpct::library_data_t::real_half;
  11464. dpct::library_data_t cu_data_type = dpct::library_data_t::real_half;
  11465. // dst strides
  11466. size_t nbd2 = dst->nb[2];
  11467. size_t nbd3 = dst->nb[3];
  11468. const sycl::half alpha_f16 = 1.0f;
  11469. const sycl::half beta_f16 = 0.0f;
  11470. const float alpha_f32 = 1.0f;
  11471. const float beta_f32 = 0.0f;
  11472. const void * alpha = &alpha_f16;
  11473. const void * beta = &beta_f16;
  11474. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  11475. dst_t = (char *) dst_f16.alloc(ne);
  11476. nbd2 /= sizeof(float) / sizeof(sycl::half);
  11477. nbd3 /= sizeof(float) / sizeof(sycl::half);
  11478. } else {
  11479. dst_t = (char *) dst_ddf;
  11480. cu_compute_type = dpct::library_data_t::real_float;
  11481. cu_data_type = dpct::library_data_t::real_float;
  11482. alpha = &alpha_f32;
  11483. beta = &beta_f32;
  11484. }
  11485. GGML_ASSERT(ne12 % ne02 == 0);
  11486. GGML_ASSERT(ne13 % ne03 == 0);
  11487. // broadcast factors
  11488. const int64_t r2 = ne12/ne02;
  11489. const int64_t r3 = ne13/ne03;
  11490. #if 0
  11491. // use syclGemmEx
  11492. {
  11493. for (int i13 = 0; i13 < ne13; ++i13) {
  11494. for (int i12 = 0; i12 < ne12; ++i12) {
  11495. int i03 = i13 / r3;
  11496. int i02 = i12 / r2;
  11497. SYCL_CHECK(
  11498. syclGemmEx(g_sycl_handles[g_main_device_index], CUBLAS_OP_T, CUBLAS_OP_N,
  11499. ne01, ne11, ne10,
  11500. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , SYCL_R_16F, nb01/sizeof(half),
  11501. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, SYCL_R_16F, nb11/sizeof(float),
  11502. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  11503. cu_compute_type,
  11504. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  11505. }
  11506. }
  11507. }
  11508. #else
  11509. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  11510. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  11511. // use syclGemmStridedBatchedEx
  11512. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  11513. *g_sycl_handles[g_main_device_index], oneapi::mkl::transpose::trans,
  11514. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  11515. (const char *)src0_as_f16, dpct::library_data_t::real_half,
  11516. nb01 / sizeof(sycl::half), src0->nb[2] / sizeof(sycl::half),
  11517. (const char *)src1_as_f16.get(), dpct::library_data_t::real_half,
  11518. nb11 / sizeof(float), src1->nb[2] / sizeof(float), beta,
  11519. (char *)dst_t, cu_data_type, ne01, dst->nb[2] / sizeof(float),
  11520. ne12 * ne13, cu_compute_type)));
  11521. } else {
  11522. // use syclGemmBatchedEx
  11523. const int ne23 = ne12*ne13;
  11524. sycl_pool_alloc<const void *> ptrs_src(2*ne23);
  11525. sycl_pool_alloc< void *> ptrs_dst(1*ne23);
  11526. sycl::range<3> block_dims(1, ne12, ne13);
  11527. /*
  11528. DPCT1049:47: The work-group size passed to the SYCL kernel may exceed
  11529. the limit. To get the device limit, query
  11530. info::device::max_work_group_size. Adjust the work-group size if needed.
  11531. */
  11532. {
  11533. dpct::has_capability_or_fail(main_stream->get_device(),
  11534. {sycl::aspect::fp16});
  11535. main_stream->submit([&](sycl::handler &cgh) {
  11536. const sycl::half *src1_as_f16_get_ct1 = src1_as_f16.get();
  11537. const void **ptrs_src_get_ct3 = ptrs_src.get();
  11538. void **ptrs_dst_get_ct4 = ptrs_dst.get();
  11539. cgh.parallel_for(sycl::nd_range<3>(block_dims, block_dims),
  11540. [=](sycl::nd_item<3> item_ct1) {
  11541. k_compute_batched_ptrs(
  11542. src0_as_f16, src1_as_f16_get_ct1,
  11543. dst_t, ptrs_src_get_ct3,
  11544. ptrs_dst_get_ct4, ne12, ne13, ne23,
  11545. nb02, nb03, nb12, nb13, nbd2, nbd3, r2,
  11546. r3, item_ct1);
  11547. });
  11548. });
  11549. }
  11550. /*
  11551. DPCT1010:95: SYCL uses exceptions to report errors and does not use the
  11552. error codes. The call was replaced with 0. You need to rewrite this
  11553. code.
  11554. */
  11555. SYCL_CHECK(0);
  11556. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  11557. *g_sycl_handles[g_main_device_index], oneapi::mkl::transpose::trans,
  11558. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  11559. (const void **)(ptrs_src.get() + 0 * ne23),
  11560. dpct::library_data_t::real_half, nb01 / sizeof(sycl::half),
  11561. (const void **)(ptrs_src.get() + 1 * ne23),
  11562. dpct::library_data_t::real_half, nb11 / sizeof(float), beta,
  11563. (void **)(ptrs_dst.get() + 0 * ne23), cu_data_type, ne01, ne23,
  11564. cu_compute_type)));
  11565. }
  11566. #endif
  11567. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  11568. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  11569. to_fp32_sycl(dst_f16.get(), dst_ddf, ne, main_stream);
  11570. }
  11571. }
  11572. catch (sycl::exception const &exc) {
  11573. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11574. << ", line:" << __LINE__ << std::endl;
  11575. std::exit(1);
  11576. }
  11577. static void ggml_sycl_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11578. const bool all_on_device =
  11579. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  11580. (src1->backend == GGML_BACKEND_GPU) &&
  11581. ( dst->backend == GGML_BACKEND_GPU);
  11582. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  11583. int64_t min_compute_capability = INT_MAX;
  11584. for (int64_t id = 0; id < g_device_count; ++id) {
  11585. if (min_compute_capability > g_device_caps[id].cc && g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  11586. min_compute_capability = g_device_caps[id].cc;
  11587. }
  11588. }
  11589. #ifdef SYCL_USE_XMX
  11590. const bool use_xmx = true;
  11591. #else
  11592. const bool use_xmx = false;
  11593. #endif
  11594. // debug helpers
  11595. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  11596. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  11597. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  11598. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  11599. //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);
  11600. //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);
  11601. if (!split && all_on_device && !use_xmx && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  11602. // KQ single-batch
  11603. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_p021\n");
  11604. ggml_sycl_mul_mat_vec_p021(src0, src1, dst);
  11605. } 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) {
  11606. // KQV single-batch
  11607. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_nc\n");
  11608. ggml_sycl_mul_mat_vec_nc(src0, src1, dst);
  11609. } else if (!split && all_on_device && use_xmx && src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  11610. // KQ + KQV multi-batch
  11611. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_mat_batched_sycl\n");
  11612. ggml_sycl_mul_mat_mat_batched_sycl(src0, src1, dst);
  11613. } else if (src0->type == GGML_TYPE_F32) {
  11614. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat\n");
  11615. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  11616. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  11617. // GGML_SYCL_DEBUG("ggml_is_quantized or GGML_TYPE_F16\n");
  11618. if (src1->ne[1] == 1 && src0->ne[0] % GGML_SYCL_DMMV_X == 0) {
  11619. #ifdef GGML_SYCL_FORCE_DMMV
  11620. const bool use_mul_mat_vec_q = false;
  11621. #else
  11622. const bool use_mul_mat_vec_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  11623. #endif // GGML_SYCL_FORCE_DMMV
  11624. if (use_mul_mat_vec_q) {
  11625. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  11626. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_vec_q path\n");
  11627. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_vec_q, true);
  11628. } else {
  11629. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_dequantize_mul_mat_vec path\n");
  11630. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_dequantize_mul_mat_vec, false);
  11631. }
  11632. } else {
  11633. bool use_mul_mat_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type);
  11634. if (use_xmx && min_compute_capability >= VER_GEN9 && src1->ne[1] > XMX_MAX_BATCH_SIZE) {
  11635. use_mul_mat_q = false;
  11636. }
  11637. if (use_mul_mat_q) {
  11638. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_q path\n");
  11639. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_q, true);
  11640. } else {
  11641. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_sycl path\n");
  11642. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  11643. }
  11644. }
  11645. } else {
  11646. GGML_ASSERT(false);
  11647. }
  11648. }
  11649. #if 0
  11650. template<typename ... Srcs>
  11651. static __global__ void k_compute_batched_ptrs_id(
  11652. const void ** ptrs_src, void ** ptrs_dst,
  11653. int ne12, int ne13,
  11654. int ne23,
  11655. int nb02, int nb03,
  11656. int nb12, int nb13,
  11657. int nb2, int nb3,
  11658. int r2, int r3,
  11659. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  11660. const half * src1_f16, half * dst_f16,
  11661. const int32_t * ids, const int id,
  11662. Srcs... src0s) {
  11663. int i = ids[id];
  11664. half * src0_f16;
  11665. const void * srcs_ar[] = { (const half *) src0s... };
  11666. if (src0_type == GGML_TYPE_F16) {
  11667. src0_f16 = (half *) srcs_ar[i];
  11668. } else {
  11669. src0_f16 = src0_as_f16;
  11670. if (threadIdx.x == 0 && threadIdx.y == 0) {
  11671. const to_fp16_sycl_t to_fp16 = ggml_get_to_fp16_sycl(src0_type);
  11672. to_fp16(srcs_ar[i], src0_f16, src0_ne, syclStreamFireAndForget);
  11673. }
  11674. }
  11675. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  11676. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  11677. if (i13 >= ne13 || i12 >= ne12) {
  11678. return;
  11679. }
  11680. int i03 = i13 / r3;
  11681. int i02 = i12 / r2;
  11682. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  11683. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  11684. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  11685. }
  11686. static void ggml_sycl_mul_mat_id_sycl(ggml_tensor * dst) {
  11687. const struct ggml_tensor * ids = dst->src[0];
  11688. const struct ggml_tensor * src1 = dst->src[1];
  11689. const struct ggml_tensor * src00 = dst->src[2];
  11690. const int id = dst->op_params[0];
  11691. GGML_ASSERT(!ggml_is_transposed(src00));
  11692. GGML_ASSERT(!ggml_is_transposed(src1));
  11693. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  11694. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11695. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  11696. const int64_t ne01 = src00->ne[1];
  11697. const int64_t ne02 = src00->ne[2];
  11698. const int64_t ne03 = src00->ne[3];
  11699. //const int64_t nb01 = src00->nb[1];
  11700. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  11701. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  11702. const int64_t ne10 = src1->ne[0];
  11703. const int64_t ne11 = src1->ne[1];
  11704. const int64_t ne12 = src1->ne[2];
  11705. const int64_t ne13 = src1->ne[3];
  11706. //const int64_t nb11 = src1->nb[1];
  11707. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  11708. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  11709. const int64_t ne1 = ggml_nelements(src1);
  11710. const int64_t ne = ggml_nelements(dst);
  11711. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11712. syclStream_t main_stream = g_syclStreams[g_main_device_index][0];
  11713. SYCL_CHECK(syclSetStream(g_sycl_handles[g_main_device_index], main_stream));
  11714. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11715. //void * src0_ddq = src0_extra->data_device[g_main_device_index];
  11716. //half * src0_as_f16 = (half *) src0_ddq;
  11717. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11718. float * src1_ddf = (float *) src1_extra->data_device[g_main_device_index];
  11719. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11720. float * dst_ddf = (float *) dst_extra->data_device[g_main_device_index];
  11721. // convert src1 to fp16
  11722. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  11723. GGML_ASSERT(to_fp16_sycl != nullptr);
  11724. size_t src1_as = 0;
  11725. half * src1_as_f16 = (half *) ggml_sycl_pool_malloc(ne1 * sizeof(half), &src1_as);
  11726. to_fp16_sycl(src1_ddf, src1_as_f16, ne1, main_stream);
  11727. size_t dst_as = 0;
  11728. half * dst_f16 = (half *) ggml_sycl_pool_malloc(ne * sizeof(half), &dst_as);
  11729. GGML_ASSERT(ne12 % ne02 == 0);
  11730. GGML_ASSERT(ne13 % ne03 == 0);
  11731. // broadcast factors
  11732. const int64_t r2 = ne12/ne02;
  11733. const int64_t r3 = ne13/ne03;
  11734. const half alpha_f16 = 1.0f;
  11735. const half beta_f16 = 0.0f;
  11736. // use syclGemmBatchedEx
  11737. const int ne23 = ne12*ne13;
  11738. const void ** ptrs_src = nullptr;
  11739. void ** ptrs_dst = nullptr;
  11740. size_t ptrs_src_s = 0;
  11741. size_t ptrs_dst_s = 0;
  11742. ptrs_src = (const void **) ggml_sycl_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  11743. ptrs_dst = ( void **) ggml_sycl_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  11744. int64_t src0_ne = ggml_nelements(src00);
  11745. half * src0_as_f16 = nullptr;
  11746. size_t src0_as = 0;
  11747. if (src00->type != GGML_TYPE_F16) {
  11748. src0_as_f16 = (half *) ggml_sycl_pool_malloc(src0_ne * sizeof(half), &src0_as);
  11749. }
  11750. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  11751. dim3 block_dims(ne13, ne12);
  11752. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  11753. ptrs_src, ptrs_dst,
  11754. ne12, ne13,
  11755. ne23,
  11756. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  11757. nb12, nb13,
  11758. dst->nb[2], dst->nb[3],
  11759. r2, r3,
  11760. src00->type, src0_as_f16, src0_ne,
  11761. src1_as_f16, dst_f16,
  11762. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device_index], id,
  11763. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device_index] : nullptr,
  11764. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device_index] : nullptr,
  11765. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device_index] : nullptr,
  11766. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device_index] : nullptr
  11767. );
  11768. SYCL_CHECK(syclGetLastError());
  11769. SYCL_CHECK(
  11770. syclGemmBatchedEx(g_sycl_handles[g_main_device_index], CUBLAS_OP_T, CUBLAS_OP_N,
  11771. ne01, ne11, ne10,
  11772. &alpha_f16, (const void **) (ptrs_src + 0*ne23), SYCL_R_16F, ne00,
  11773. (const void **) (ptrs_src + 1*ne23), SYCL_R_16F, ne10,
  11774. &beta_f16, ( void **) (ptrs_dst + 0*ne23), SYCL_R_16F, ne01,
  11775. ne23,
  11776. CUBLAS_COMPUTE_16F,
  11777. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  11778. if (src0_as != 0) {
  11779. ggml_sycl_pool_free(src0_as_f16, src0_as);
  11780. }
  11781. if (ptrs_src_s != 0) {
  11782. ggml_sycl_pool_free(ptrs_src, ptrs_src_s);
  11783. }
  11784. if (ptrs_dst_s != 0) {
  11785. ggml_sycl_pool_free(ptrs_dst, ptrs_dst_s);
  11786. }
  11787. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  11788. to_fp32_sycl(dst_f16, dst_ddf, ne, main_stream);
  11789. ggml_sycl_pool_free(src1_as_f16, src1_as);
  11790. ggml_sycl_pool_free(dst_f16, dst_as);
  11791. }
  11792. #endif
  11793. static void ggml_sycl_mul_mat_id(const ggml_tensor *src0,
  11794. const ggml_tensor *src1,
  11795. ggml_tensor *dst) try {
  11796. #if 0
  11797. ggml_sycl_mul_mat_id_sycl(dst);
  11798. // TODO: mmq/mmv support
  11799. #endif
  11800. const int64_t nb11 = src1->nb[1];
  11801. const int64_t nb1 = dst->nb[1];
  11802. const struct ggml_tensor * ids = src0;
  11803. const int32_t id = ((int32_t *) dst->op_params)[0];
  11804. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  11805. std::vector<char> ids_host(ggml_nbytes(ids));
  11806. const dpct::queue_ptr stream = g_syclStreams[g_main_device_index][0];
  11807. if (ids->backend == GGML_BACKEND_GPU) {
  11808. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device_index];
  11809. SYCL_CHECK(CHECK_TRY_ERROR(
  11810. stream->memcpy(ids_host.data(), ids_dev, ggml_nbytes(ids))));
  11811. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  11812. } else {
  11813. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  11814. }
  11815. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  11816. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  11817. ggml_tensor_extra_gpu src1_row_extra;
  11818. ggml_tensor_extra_gpu dst_row_extra;
  11819. ggml_tensor src1_row = *src1;
  11820. ggml_tensor dst_row = *dst;
  11821. src1_row.backend = GGML_BACKEND_GPU;
  11822. dst_row.backend = GGML_BACKEND_GPU;
  11823. src1_row.extra = &src1_row_extra;
  11824. dst_row.extra = &dst_row_extra;
  11825. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  11826. (char *) src1->data : (char *) src1_extra->data_device[g_main_device_index];
  11827. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  11828. (char *) dst->data : (char *) dst_extra->data_device[g_main_device_index];
  11829. if (src1->ne[1] == 1) {
  11830. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  11831. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  11832. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  11833. //int32_t row_id;
  11834. //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]));
  11835. //SYCL_CHECK(syclStreamSynchronize(g_syclStreams[g_main_device][0]));
  11836. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  11837. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  11838. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  11839. src1_row_extra.data_device[g_main_device_index] = src1_original + i01*src1->nb[1];
  11840. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  11841. dst_row_extra.data_device[g_main_device_index] = dst_original + i01*dst->nb[1];
  11842. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  11843. ggml_sycl_mul_mat(src0_row, &src1_row, &dst_row);
  11844. }
  11845. } else {
  11846. sycl_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  11847. sycl_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  11848. src1_row_extra.data_device[g_main_device_index] = src1_contiguous.get();
  11849. dst_row_extra.data_device[g_main_device_index] = dst_contiguous.get();
  11850. const dpct::memcpy_direction src1_kind =
  11851. src1->backend == GGML_BACKEND_CPU ? dpct::host_to_device
  11852. : dpct::device_to_device;
  11853. const dpct::memcpy_direction dst_kind = dst->backend == GGML_BACKEND_CPU
  11854. ? dpct::device_to_host
  11855. : dpct::device_to_device;
  11856. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  11857. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  11858. int64_t num_src1_rows = 0;
  11859. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  11860. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  11861. if (row_id_i != row_id) {
  11862. continue;
  11863. }
  11864. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  11865. SYCL_CHECK(CHECK_TRY_ERROR(
  11866. stream->memcpy(src1_contiguous.get() + num_src1_rows * nb11,
  11867. src1_original + i01 * nb11, nb11)));
  11868. num_src1_rows++;
  11869. }
  11870. if (num_src1_rows == 0) {
  11871. continue;
  11872. }
  11873. src1_row.ne[1] = num_src1_rows;
  11874. dst_row.ne[1] = num_src1_rows;
  11875. src1_row.nb[1] = nb11;
  11876. src1_row.nb[2] = num_src1_rows*nb11;
  11877. src1_row.nb[3] = num_src1_rows*nb11;
  11878. dst_row.nb[1] = nb1;
  11879. dst_row.nb[2] = num_src1_rows*nb1;
  11880. dst_row.nb[3] = num_src1_rows*nb1;
  11881. ggml_sycl_mul_mat(src0_row, &src1_row, &dst_row);
  11882. num_src1_rows = 0;
  11883. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  11884. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  11885. if (row_id_i != row_id) {
  11886. continue;
  11887. }
  11888. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  11889. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  11890. dst_original + i01 * nb1,
  11891. dst_contiguous.get() + num_src1_rows * nb1, nb1)));
  11892. num_src1_rows++;
  11893. }
  11894. }
  11895. }
  11896. if (dst->backend == GGML_BACKEND_CPU) {
  11897. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  11898. }
  11899. }
  11900. catch (sycl::exception const &exc) {
  11901. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11902. << ", line:" << __LINE__ << std::endl;
  11903. std::exit(1);
  11904. }
  11905. static void ggml_sycl_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11906. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_scale);
  11907. }
  11908. static void ggml_sycl_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11909. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_clamp);
  11910. }
  11911. static void ggml_sycl_cpy(const ggml_tensor *src0, const ggml_tensor *src1,
  11912. ggml_tensor *dst) try {
  11913. const int64_t ne = ggml_nelements(src0);
  11914. GGML_ASSERT(ne == ggml_nelements(src1));
  11915. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  11916. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  11917. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  11918. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  11919. const int64_t ne00 = src0->ne[0];
  11920. const int64_t ne01 = src0->ne[1];
  11921. GGML_ASSERT(src0->ne[3] == 1);
  11922. const int64_t nb00 = src0->nb[0];
  11923. const int64_t nb01 = src0->nb[1];
  11924. const int64_t nb02 = src0->nb[2];
  11925. const int64_t ne10 = src1->ne[0];
  11926. const int64_t ne11 = src1->ne[1];
  11927. GGML_ASSERT(src1->ne[3] == 1);
  11928. const int64_t nb10 = src1->nb[0];
  11929. const int64_t nb11 = src1->nb[1];
  11930. const int64_t nb12 = src1->nb[2];
  11931. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11932. dpct::queue_ptr main_stream = g_syclStreams[g_main_device_index][0];
  11933. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11934. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11935. char * src0_ddc = (char *) src0_extra->data_device[g_main_device_index];
  11936. char * src1_ddc = (char *) src1_extra->data_device[g_main_device_index];
  11937. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  11938. ggml_cpy_f32_f32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11939. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  11940. ggml_cpy_f32_f16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11941. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  11942. ggml_cpy_f32_q8_0_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11943. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  11944. ggml_cpy_f32_q4_0_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11945. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  11946. ggml_cpy_f32_q4_1_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11947. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  11948. ggml_cpy_f16_f16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11949. } else if (src0->type == GGML_TYPE_I16 && src1->type == GGML_TYPE_I16) {
  11950. ggml_cpy_i16_i16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11951. } else if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32) {
  11952. ggml_cpy_i32_i32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11953. } else {
  11954. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  11955. ggml_type_name(src0->type), ggml_type_name(src1->type));
  11956. GGML_ASSERT(false);
  11957. }
  11958. (void) dst;
  11959. }
  11960. catch (sycl::exception const &exc) {
  11961. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11962. << ", line:" << __LINE__ << std::endl;
  11963. std::exit(1);
  11964. }
  11965. static void ggml_sycl_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11966. // TODO: why do we pass dst as src1 here?
  11967. ggml_sycl_cpy(src0, dst, nullptr);
  11968. (void) src1;
  11969. }
  11970. static void ggml_sycl_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11971. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_diag_mask_inf);
  11972. }
  11973. static void ggml_sycl_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11974. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_soft_max);
  11975. }
  11976. static void ggml_sycl_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11977. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  11978. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rope);
  11979. }
  11980. static void ggml_sycl_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11981. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_alibi);
  11982. }
  11983. static void ggml_sycl_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11984. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_im2col);
  11985. }
  11986. static void ggml_sycl_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11987. GGML_ASSERT(ggml_is_contiguous(src0));
  11988. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sum_rows);
  11989. }
  11990. static void ggml_sycl_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11991. GGML_ASSERT(ggml_is_contiguous(src0));
  11992. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_argsort);
  11993. }
  11994. static void ggml_sycl_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11995. (void) src0;
  11996. (void) src1;
  11997. (void) dst;
  11998. }
  11999. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  12000. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  12001. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  12002. }
  12003. void ggml_sycl_transform_tensor(void *data, struct ggml_tensor *tensor) try {
  12004. const int64_t nrows = ggml_nrows(tensor);
  12005. const int64_t ne0 = tensor->ne[0];
  12006. const size_t nb1 = tensor->nb[1];
  12007. ggml_backend_type backend = tensor->backend;
  12008. ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  12009. memset(extra, 0, sizeof(*extra));
  12010. for (int64_t id = 0; id < g_device_count; ++id) {
  12011. if (backend == GGML_BACKEND_GPU && id != g_main_device_index) {
  12012. continue;
  12013. }
  12014. ggml_sycl_set_device(get_device_id_by_index(id));
  12015. const dpct::queue_ptr stream = g_syclStreams[id][0];
  12016. int64_t row_low, row_high;
  12017. if (backend == GGML_BACKEND_GPU) {
  12018. row_low = 0;
  12019. row_high = nrows;
  12020. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  12021. const int64_t rounding = get_row_rounding(tensor->type);
  12022. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  12023. row_low -= row_low % rounding;
  12024. if (id == g_device_count - 1) {
  12025. row_high = nrows;
  12026. } else {
  12027. row_high = nrows*g_tensor_split[id + 1];
  12028. row_high -= row_high % rounding;
  12029. }
  12030. } else {
  12031. GGML_ASSERT(false);
  12032. }
  12033. if (row_low == row_high) {
  12034. continue;
  12035. }
  12036. int64_t nrows_split = row_high - row_low;
  12037. const size_t offset_split = row_low*nb1;
  12038. size_t size = ggml_nbytes_split(tensor, nrows_split);
  12039. const size_t original_size = size;
  12040. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  12041. if (ne0 % MATRIX_ROW_PADDING != 0) {
  12042. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  12043. }
  12044. char * buf;
  12045. SYCL_CHECK(CHECK_TRY_ERROR(buf = (char *)sycl::malloc_device(
  12046. size, *stream)));
  12047. char * buf_host = (char *)data + offset_split;
  12048. // set padding to 0 to avoid possible NaN values
  12049. if (size > original_size) {
  12050. SYCL_CHECK(CHECK_TRY_ERROR(
  12051. (*stream)
  12052. .memset(buf + original_size, 0, size - original_size)
  12053. .wait()));
  12054. }
  12055. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  12056. .memcpy(buf, buf_host, original_size)
  12057. .wait()));
  12058. extra->data_device[id] = buf;
  12059. if (backend == GGML_BACKEND_GPU_SPLIT) {
  12060. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  12061. SYCL_CHECK(CHECK_TRY_ERROR(extra->events[id][is] =
  12062. new sycl::event()));
  12063. }
  12064. }
  12065. }
  12066. tensor->extra = extra;
  12067. }
  12068. catch (sycl::exception const &exc) {
  12069. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12070. << ", line:" << __LINE__ << std::endl;
  12071. std::exit(1);
  12072. }
  12073. void ggml_sycl_free_data(struct ggml_tensor *tensor) try {
  12074. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  12075. return;
  12076. }
  12077. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  12078. for (int64_t id = 0; id < g_device_count; ++id) {
  12079. const dpct::queue_ptr stream = g_syclStreams[id][0];
  12080. if (extra->data_device[id] != nullptr) {
  12081. SYCL_CHECK(ggml_sycl_set_device(get_device_id_by_index(id)));
  12082. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(extra->data_device[id], *stream)));
  12083. }
  12084. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  12085. if (extra->events[id][is] != nullptr) {
  12086. SYCL_CHECK(ggml_sycl_set_device(get_device_id_by_index(id)));
  12087. SYCL_CHECK(CHECK_TRY_ERROR(
  12088. dpct::destroy_event(extra->events[id][is])));
  12089. }
  12090. }
  12091. }
  12092. delete extra;
  12093. }
  12094. catch (sycl::exception const &exc) {
  12095. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12096. << ", line:" << __LINE__ << std::endl;
  12097. std::exit(1);
  12098. }
  12099. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  12100. static size_t g_temp_tensor_extra_index = 0;
  12101. static ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  12102. if (g_temp_tensor_extras == nullptr) {
  12103. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  12104. }
  12105. size_t alloc_index = g_temp_tensor_extra_index;
  12106. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  12107. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  12108. memset(extra, 0, sizeof(*extra));
  12109. return extra;
  12110. }
  12111. static void ggml_sycl_assign_buffers_impl(struct ggml_tensor *tensor,
  12112. bool scratch, bool force_inplace,
  12113. bool no_alloc) try {
  12114. if (scratch && g_scratch_size == 0) {
  12115. return;
  12116. }
  12117. tensor->backend = GGML_BACKEND_GPU;
  12118. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  12119. const ggml_op src0_op = tensor->src[0]->op;
  12120. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  12121. ggml_sycl_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  12122. }
  12123. }
  12124. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  12125. ggml_sycl_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  12126. }
  12127. if (scratch && no_alloc) {
  12128. return;
  12129. }
  12130. ggml_tensor_extra_gpu * extra;
  12131. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  12132. tensor->op == GGML_OP_VIEW ||
  12133. force_inplace;
  12134. const size_t size = ggml_nbytes(tensor);
  12135. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12136. const dpct::queue_ptr stream = g_syclStreams[g_main_device_index][0];
  12137. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  12138. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  12139. char * src0_ddc = (char *) src0_extra->data_device[g_main_device_index];
  12140. size_t offset = 0;
  12141. if (tensor->op == GGML_OP_VIEW) {
  12142. memcpy(&offset, tensor->op_params, sizeof(size_t));
  12143. }
  12144. extra = ggml_sycl_alloc_temp_tensor_extra();
  12145. extra->data_device[g_main_device_index] = src0_ddc + offset;
  12146. } else if (tensor->op == GGML_OP_CPY) {
  12147. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  12148. void * src1_ddv = src1_extra->data_device[g_main_device_index];
  12149. extra = ggml_sycl_alloc_temp_tensor_extra();
  12150. extra->data_device[g_main_device_index] = src1_ddv;
  12151. } else if (scratch) {
  12152. GGML_ASSERT(size <= g_scratch_size);
  12153. if (g_scratch_offset + size > g_scratch_size) {
  12154. g_scratch_offset = 0;
  12155. }
  12156. char * data = (char *) g_scratch_buffer;
  12157. if (data == nullptr) {
  12158. SYCL_CHECK(CHECK_TRY_ERROR(
  12159. data = (char *)sycl::malloc_device(
  12160. g_scratch_size, *stream)));
  12161. g_scratch_buffer = data;
  12162. }
  12163. extra = ggml_sycl_alloc_temp_tensor_extra();
  12164. extra->data_device[g_main_device_index] = data + g_scratch_offset;
  12165. g_scratch_offset += size;
  12166. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  12167. } else { // allocate new buffers outside of scratch
  12168. void * data;
  12169. SYCL_CHECK(CHECK_TRY_ERROR(data = (void *)sycl::malloc_device(
  12170. size, *stream)));
  12171. SYCL_CHECK(CHECK_TRY_ERROR(
  12172. (*stream).memset(data, 0, size).wait()));
  12173. extra = new ggml_tensor_extra_gpu;
  12174. memset(extra, 0, sizeof(*extra));
  12175. extra->data_device[g_main_device_index] = data;
  12176. }
  12177. tensor->extra = extra;
  12178. }
  12179. catch (sycl::exception const &exc) {
  12180. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12181. << ", line:" << __LINE__ << std::endl;
  12182. std::exit(1);
  12183. }
  12184. void ggml_sycl_assign_scratch_offset(struct ggml_tensor *tensor,
  12185. size_t offset) try {
  12186. if (g_scratch_size == 0) {
  12187. return;
  12188. }
  12189. if (g_scratch_buffer == nullptr) {
  12190. ggml_sycl_set_device(g_main_device);
  12191. const dpct::queue_ptr stream = g_syclStreams[g_main_device_index][0];
  12192. SYCL_CHECK(
  12193. CHECK_TRY_ERROR(g_scratch_buffer = (void *)sycl::malloc_device(
  12194. g_scratch_size, *stream)));
  12195. }
  12196. ggml_tensor_extra_gpu * extra = ggml_sycl_alloc_temp_tensor_extra();
  12197. const bool inplace = tensor->view_src != nullptr;
  12198. if (inplace && (tensor->view_src->backend == GGML_BACKEND_GPU || tensor->view_src->backend == GGML_BACKEND_GPU_SPLIT)) {
  12199. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->view_src->extra;
  12200. char * src0_ddc = (char *) src0_extra->data_device[g_main_device_index];
  12201. size_t view_offset = 0;
  12202. if (tensor->op == GGML_OP_VIEW) {
  12203. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  12204. }
  12205. extra->data_device[g_main_device_index] = src0_ddc + view_offset;
  12206. } else {
  12207. extra->data_device[g_main_device_index] = (char *) g_scratch_buffer + offset;
  12208. }
  12209. tensor->extra = extra;
  12210. }
  12211. catch (sycl::exception const &exc) {
  12212. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12213. << ", line:" << __LINE__ << std::endl;
  12214. std::exit(1);
  12215. }
  12216. void ggml_sycl_copy_to_device(struct ggml_tensor *tensor) try {
  12217. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  12218. GGML_ASSERT(ggml_is_contiguous(tensor));
  12219. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  12220. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12221. const dpct::queue_ptr stream = g_syclStreams[g_main_device_index][0];
  12222. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  12223. .memcpy(extra->data_device[g_main_device_index],
  12224. tensor->data, ggml_nbytes(tensor))
  12225. .wait()));
  12226. }
  12227. catch (sycl::exception const &exc) {
  12228. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12229. << ", line:" << __LINE__ << std::endl;
  12230. std::exit(1);
  12231. }
  12232. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor) {
  12233. ggml_sycl_assign_buffers_impl(tensor, true, false, false);
  12234. }
  12235. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  12236. ggml_sycl_assign_buffers_impl(tensor, true, false, true);
  12237. }
  12238. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  12239. ggml_sycl_assign_buffers_impl(tensor, false, false, false);
  12240. }
  12241. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  12242. ggml_sycl_assign_buffers_impl(tensor, false, true, false);
  12243. }
  12244. void ggml_sycl_set_main_device(const int main_device) try {
  12245. if (main_device >= g_all_sycl_device_count) {
  12246. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  12247. main_device, g_all_sycl_device_count, g_main_device);
  12248. return;
  12249. }
  12250. if (g_main_device != main_device && g_device_count >= 1) {
  12251. g_main_device = main_device;
  12252. g_main_device_index = get_device_index_by_id(g_main_device);
  12253. dpct::device_info prop;
  12254. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  12255. prop, dpct::dev_mgr::instance().get_device(g_main_device))));
  12256. fprintf(stderr, "Using device %d (%s) as main device\n",
  12257. g_main_device, prop.get_name());
  12258. }
  12259. }
  12260. catch (sycl::exception const &exc) {
  12261. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12262. << ", line:" << __LINE__ << std::endl;
  12263. std::exit(1);
  12264. }
  12265. void ggml_sycl_set_scratch_size(const size_t scratch_size) {
  12266. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  12267. // it still won't always work as expected, but it's better than nothing
  12268. if (scratch_size > g_scratch_size) {
  12269. ggml_sycl_free_scratch();
  12270. }
  12271. g_scratch_size = std::max(g_scratch_size, scratch_size);
  12272. }
  12273. void ggml_sycl_free_scratch() try {
  12274. if (g_scratch_buffer == nullptr) {
  12275. return;
  12276. }
  12277. ggml_sycl_set_device(g_main_device);
  12278. const dpct::queue_ptr stream = g_syclStreams[g_main_device_index][0];
  12279. SYCL_CHECK(CHECK_TRY_ERROR(
  12280. sycl::free(g_scratch_buffer, *stream)));
  12281. g_scratch_buffer = nullptr;
  12282. }
  12283. catch (sycl::exception const &exc) {
  12284. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12285. << ", line:" << __LINE__ << std::endl;
  12286. std::exit(1);
  12287. }
  12288. bool ggml_sycl_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  12289. if (!g_sycl_loaded) return false;
  12290. ggml_sycl_func_t func;
  12291. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  12292. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  12293. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  12294. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  12295. return false;
  12296. }
  12297. if (tensor->op == GGML_OP_MUL_MAT) {
  12298. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  12299. #ifndef NDEBUG
  12300. 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]);
  12301. #endif
  12302. return false;
  12303. }
  12304. }
  12305. switch (tensor->op) {
  12306. case GGML_OP_REPEAT:
  12307. func = ggml_sycl_repeat;
  12308. break;
  12309. case GGML_OP_GET_ROWS:
  12310. func = ggml_sycl_get_rows;
  12311. break;
  12312. case GGML_OP_DUP:
  12313. func = ggml_sycl_dup;
  12314. break;
  12315. case GGML_OP_ADD:
  12316. func = ggml_sycl_add;
  12317. break;
  12318. case GGML_OP_ACC:
  12319. func = ggml_sycl_acc;
  12320. break;
  12321. case GGML_OP_MUL:
  12322. func = ggml_sycl_mul;
  12323. break;
  12324. case GGML_OP_DIV:
  12325. func = ggml_sycl_div;
  12326. break;
  12327. case GGML_OP_UNARY:
  12328. switch (ggml_get_unary_op(tensor)) {
  12329. case GGML_UNARY_OP_GELU:
  12330. func = ggml_sycl_gelu;
  12331. break;
  12332. case GGML_UNARY_OP_SILU:
  12333. func = ggml_sycl_silu;
  12334. break;
  12335. case GGML_UNARY_OP_GELU_QUICK:
  12336. func = ggml_sycl_gelu_quick;
  12337. break;
  12338. case GGML_UNARY_OP_TANH:
  12339. func = ggml_sycl_tanh;
  12340. break;
  12341. case GGML_UNARY_OP_RELU:
  12342. func = ggml_sycl_relu;
  12343. break;
  12344. default:
  12345. return false;
  12346. }
  12347. break;
  12348. case GGML_OP_NORM:
  12349. func = ggml_sycl_norm;
  12350. break;
  12351. case GGML_OP_GROUP_NORM:
  12352. func = ggml_sycl_group_norm;
  12353. break;
  12354. case GGML_OP_CONCAT:
  12355. func = ggml_sycl_concat;
  12356. break;
  12357. case GGML_OP_UPSCALE:
  12358. func = ggml_sycl_upscale;
  12359. break;
  12360. case GGML_OP_PAD:
  12361. func = ggml_sycl_pad;
  12362. break;
  12363. case GGML_OP_LEAKY_RELU:
  12364. func = ggml_sycl_leaky_relu;
  12365. break;
  12366. case GGML_OP_RMS_NORM:
  12367. func = ggml_sycl_rms_norm;
  12368. break;
  12369. case GGML_OP_MUL_MAT:
  12370. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  12371. return false;
  12372. }
  12373. func = ggml_sycl_mul_mat;
  12374. break;
  12375. case GGML_OP_MUL_MAT_ID:
  12376. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  12377. return false;
  12378. }
  12379. func = ggml_sycl_mul_mat_id;
  12380. break;
  12381. case GGML_OP_SCALE:
  12382. func = ggml_sycl_scale;
  12383. break;
  12384. case GGML_OP_SQR:
  12385. func = ggml_sycl_sqr;
  12386. break;
  12387. case GGML_OP_CLAMP:
  12388. func = ggml_sycl_clamp;
  12389. break;
  12390. case GGML_OP_CPY:
  12391. func = ggml_sycl_cpy;
  12392. break;
  12393. case GGML_OP_CONT:
  12394. func = ggml_sycl_dup;
  12395. break;
  12396. case GGML_OP_NONE:
  12397. case GGML_OP_RESHAPE:
  12398. case GGML_OP_VIEW:
  12399. case GGML_OP_PERMUTE:
  12400. case GGML_OP_TRANSPOSE:
  12401. func = ggml_sycl_nop;
  12402. break;
  12403. case GGML_OP_DIAG_MASK_INF:
  12404. func = ggml_sycl_diag_mask_inf;
  12405. break;
  12406. case GGML_OP_SOFT_MAX:
  12407. func = ggml_sycl_soft_max;
  12408. break;
  12409. case GGML_OP_ROPE:
  12410. func = ggml_sycl_rope;
  12411. break;
  12412. case GGML_OP_ALIBI:
  12413. func = ggml_sycl_alibi;
  12414. break;
  12415. case GGML_OP_IM2COL:
  12416. func = ggml_sycl_im2col;
  12417. break;
  12418. case GGML_OP_SUM_ROWS:
  12419. func = ggml_sycl_sum_rows;
  12420. break;
  12421. case GGML_OP_ARGSORT:
  12422. func = ggml_sycl_argsort;
  12423. break;
  12424. default:
  12425. return false;
  12426. }
  12427. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  12428. ggml_sycl_set_peer_access(tensor->src[1]->ne[1]);
  12429. }
  12430. if (params->ith != 0) {
  12431. return true;
  12432. }
  12433. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  12434. return true;
  12435. }
  12436. func(tensor->src[0], tensor->src[1], tensor);
  12437. return true;
  12438. }
  12439. GGML_API GGML_CALL void ggml_sycl_get_gpu_list(int *id_list, int max_len) try {
  12440. int max_compute_units = -1;
  12441. for(int i=0;i<max_len;i++) id_list[i] = 0;
  12442. int device_count = dpct::dev_mgr::instance().device_count();
  12443. for(int id=0; id< device_count; id++){
  12444. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  12445. if (!device.is_gpu()) continue;
  12446. dpct::device_info prop;
  12447. dpct::get_device_info(prop, device);
  12448. if(max_compute_units < prop.get_max_compute_units()) max_compute_units = prop.get_max_compute_units();
  12449. }
  12450. for(int id=0;id< device_count;id++){
  12451. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  12452. if (!device.is_gpu()) continue;
  12453. dpct::device_info prop;
  12454. dpct::get_device_info(prop, device);
  12455. if(max_compute_units == prop.get_max_compute_units() && prop.get_major_version() == 1 ){
  12456. id_list[id] = 1;
  12457. }
  12458. }
  12459. return;
  12460. }
  12461. catch (sycl::exception const &exc) {
  12462. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12463. << ", line:" << __LINE__ << std::endl;
  12464. std::exit(1);
  12465. }
  12466. int ggml_sycl_get_device_count() try {
  12467. int device_count;
  12468. if (CHECK_TRY_ERROR(device_count =
  12469. dpct::dev_mgr::instance().device_count()) != 0) {
  12470. return 0;
  12471. }
  12472. return device_count;
  12473. }
  12474. catch (sycl::exception const &exc) {
  12475. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12476. << ", line:" << __LINE__ << std::endl;
  12477. std::exit(1);
  12478. }
  12479. GGML_API GGML_CALL void ggml_sycl_get_device_description(int device, char *description,
  12480. size_t description_size) try {
  12481. dpct::device_info prop;
  12482. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  12483. prop, dpct::dev_mgr::instance().get_device(device))));
  12484. snprintf(description, description_size, "%s", prop.get_name());
  12485. }
  12486. catch (sycl::exception const &exc) {
  12487. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12488. << ", line:" << __LINE__ << std::endl;
  12489. std::exit(1);
  12490. }
  12491. ////////////////////////////////////////////////////////////////////////////////
  12492. // backend interface
  12493. #define UNUSED GGML_UNUSED
  12494. struct ggml_backend_sycl_context {
  12495. int device;
  12496. std::string name;
  12497. };
  12498. // sycl buffer
  12499. struct ggml_backend_sycl_buffer_context {
  12500. int device;
  12501. void * dev_ptr = nullptr;
  12502. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  12503. size_t temp_tensor_extra_index = 0;
  12504. std::string name;
  12505. ggml_backend_sycl_buffer_context(int device, void * dev_ptr) : device(device), dev_ptr(dev_ptr) {}
  12506. ~ ggml_backend_sycl_buffer_context() {
  12507. delete[] temp_tensor_extras;
  12508. }
  12509. ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  12510. if (temp_tensor_extras == nullptr) {
  12511. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  12512. }
  12513. size_t alloc_index = temp_tensor_extra_index;
  12514. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  12515. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  12516. memset(extra, 0, sizeof(*extra));
  12517. return extra;
  12518. }
  12519. };
  12520. GGML_CALL static const char * ggml_backend_sycl_buffer_get_name(ggml_backend_buffer_t buffer) {
  12521. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  12522. return ctx->name.c_str();
  12523. }
  12524. GGML_CALL static bool ggml_backend_buffer_is_sycl(ggml_backend_buffer_t buffer) {
  12525. return buffer->iface.get_name == ggml_backend_sycl_buffer_get_name;
  12526. }
  12527. static void
  12528. ggml_backend_sycl_buffer_free_buffer(ggml_backend_buffer_t buffer) try {
  12529. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12530. ggml_sycl_set_device(ctx->device);
  12531. int device_index = get_device_index_by_id(ctx->device);
  12532. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  12533. SYCL_CHECK(
  12534. CHECK_TRY_ERROR(sycl::free(ctx->dev_ptr, *stream)));
  12535. delete ctx;
  12536. }
  12537. catch (sycl::exception const &exc) {
  12538. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12539. << ", line:" << __LINE__ << std::endl;
  12540. std::exit(1);
  12541. }
  12542. static void * ggml_backend_sycl_buffer_get_base(ggml_backend_buffer_t buffer) {
  12543. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12544. return ctx->dev_ptr;
  12545. }
  12546. static void ggml_backend_sycl_buffer_init_tensor(ggml_backend_buffer_t buffer,
  12547. ggml_tensor *tensor) try {
  12548. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12549. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  12550. assert(tensor->view_src->buffer->buft == buffer->buft);
  12551. tensor->backend = tensor->view_src->backend;
  12552. tensor->extra = tensor->view_src->extra;
  12553. return;
  12554. }
  12555. ggml_tensor_extra_gpu * extra = ctx->ggml_sycl_alloc_temp_tensor_extra();
  12556. extra->data_device[ctx->device] = tensor->data;
  12557. tensor->backend = GGML_BACKEND_GPU;
  12558. tensor->extra = extra;
  12559. if (ggml_is_quantized(tensor->type)) {
  12560. // initialize padding to 0 to avoid possible NaN values
  12561. int64_t row_low = 0;
  12562. int64_t row_high = ggml_nrows(tensor);
  12563. int64_t nrows_split = row_high - row_low;
  12564. size_t original_size = ggml_nbytes_split(tensor, nrows_split);
  12565. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  12566. if (padded_size > original_size && tensor->view_src == nullptr) {
  12567. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[ctx->device][0]->memset(
  12568. (char *)tensor->data + original_size, 0,
  12569. padded_size - original_size)));
  12570. }
  12571. }
  12572. UNUSED(buffer);
  12573. }
  12574. catch (sycl::exception const &exc) {
  12575. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12576. << ", line:" << __LINE__ << std::endl;
  12577. std::exit(1);
  12578. }
  12579. static void ggml_backend_sycl_buffer_set_tensor(ggml_backend_buffer_t buffer,
  12580. ggml_tensor *tensor,
  12581. const void *data, size_t offset,
  12582. size_t size) try {
  12583. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  12584. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12585. ggml_sycl_set_device(ctx->device);
  12586. int device_index = get_device_index_by_id(ctx->device);
  12587. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  12588. SYCL_CHECK(
  12589. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  12590. SYCL_CHECK(
  12591. CHECK_TRY_ERROR((*stream)
  12592. .memcpy((char *)tensor->data + offset, data, size)
  12593. .wait()));
  12594. }
  12595. catch (sycl::exception const &exc) {
  12596. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12597. << ", line:" << __LINE__ << std::endl;
  12598. std::exit(1);
  12599. }
  12600. static void ggml_backend_sycl_buffer_get_tensor(ggml_backend_buffer_t buffer,
  12601. const ggml_tensor *tensor,
  12602. void *data, size_t offset,
  12603. size_t size) try {
  12604. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  12605. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12606. ggml_sycl_set_device(ctx->device);
  12607. int device_index = get_device_index_by_id(ctx->device);
  12608. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  12609. SYCL_CHECK(
  12610. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  12611. SYCL_CHECK(CHECK_TRY_ERROR(
  12612. (*stream)
  12613. .memcpy(data, (const char *)tensor->data + offset, size)
  12614. .wait()));
  12615. }
  12616. catch (sycl::exception const &exc) {
  12617. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12618. << ", line:" << __LINE__ << std::endl;
  12619. std::exit(1);
  12620. }
  12621. static void ggml_backend_sycl_buffer_clear(ggml_backend_buffer_t buffer,
  12622. uint8_t value) try {
  12623. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12624. ggml_sycl_set_device(ctx->device);
  12625. int device_index = get_device_index_by_id(ctx->device);
  12626. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  12627. SYCL_CHECK(
  12628. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  12629. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  12630. .memset(ctx->dev_ptr, value, buffer->size)
  12631. .wait()));
  12632. }
  12633. catch (sycl::exception const &exc) {
  12634. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12635. << ", line:" << __LINE__ << std::endl;
  12636. std::exit(1);
  12637. }
  12638. static struct ggml_backend_buffer_i ggml_backend_sycl_buffer_interface = {
  12639. /* .get_name = */ ggml_backend_sycl_buffer_get_name,
  12640. /* .free_buffer = */ ggml_backend_sycl_buffer_free_buffer,
  12641. /* .get_base = */ ggml_backend_sycl_buffer_get_base,
  12642. /* .init_tensor = */ ggml_backend_sycl_buffer_init_tensor,
  12643. /* .set_tensor = */ ggml_backend_sycl_buffer_set_tensor,
  12644. /* .get_tensor = */ ggml_backend_sycl_buffer_get_tensor,
  12645. /* .cpy_tensor = */ NULL,
  12646. /* .clear = */ ggml_backend_sycl_buffer_clear,
  12647. /* .reset = */ NULL,
  12648. };
  12649. // sycl buffer type
  12650. struct ggml_backend_sycl_buffer_type_context {
  12651. int device;
  12652. std::string name;
  12653. };
  12654. GGML_CALL static const char * ggml_backend_sycl_buffer_type_name(ggml_backend_buffer_type_t buft) {
  12655. ggml_backend_sycl_buffer_type_context * ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  12656. return ctx->name.c_str();
  12657. }
  12658. static ggml_backend_buffer_t
  12659. ggml_backend_sycl_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
  12660. size_t size) try {
  12661. int device = (int) (intptr_t) buft->context;
  12662. ggml_sycl_set_device(device);
  12663. int device_index = get_device_index_by_id(device);
  12664. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  12665. size = std::max(size, (size_t)1); // syclMalloc returns null for size 0
  12666. void * dev_ptr;
  12667. SYCL_CHECK(CHECK_TRY_ERROR(dev_ptr = (void *)sycl::malloc_device(
  12668. size, *stream)));
  12669. ggml_backend_sycl_buffer_context * ctx = new ggml_backend_sycl_buffer_context(device, dev_ptr);
  12670. return ggml_backend_buffer_init(buft, ggml_backend_sycl_buffer_interface, ctx, size);
  12671. }
  12672. catch (sycl::exception const &exc) {
  12673. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12674. << ", line:" << __LINE__ << std::endl;
  12675. std::exit(1);
  12676. }
  12677. static size_t ggml_backend_sycl_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  12678. return 128;
  12679. UNUSED(buft);
  12680. }
  12681. static size_t ggml_backend_sycl_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  12682. return dpct::get_current_device().get_max_mem_alloc_size();
  12683. UNUSED(buft);
  12684. }
  12685. static size_t ggml_backend_sycl_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  12686. int64_t row_low = 0;
  12687. int64_t row_high = ggml_nrows(tensor);
  12688. int64_t nrows_split = row_high - row_low;
  12689. size_t size = ggml_nbytes_split(tensor, nrows_split);
  12690. int64_t ne0 = tensor->ne[0];
  12691. if (ggml_is_quantized(tensor->type)) {
  12692. if (ne0 % MATRIX_ROW_PADDING != 0) {
  12693. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  12694. }
  12695. }
  12696. return size;
  12697. UNUSED(buft);
  12698. }
  12699. static bool ggml_backend_sycl_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  12700. return ggml_backend_is_sycl(backend);
  12701. UNUSED(buft);
  12702. }
  12703. static ggml_backend_buffer_type_i ggml_backend_sycl_buffer_type_interface = {
  12704. /* .get_name = */ ggml_backend_sycl_buffer_type_name,
  12705. /* .alloc_buffer = */ ggml_backend_sycl_buffer_type_alloc_buffer,
  12706. /* .get_alignment = */ ggml_backend_sycl_buffer_type_get_alignment,
  12707. /* .get_max_size = */ ggml_backend_sycl_buffer_type_get_max_size,
  12708. /* .get_alloc_size = */ ggml_backend_sycl_buffer_type_get_alloc_size,
  12709. /* .supports_backend = */ ggml_backend_sycl_buffer_type_supports_backend,
  12710. /* .is_host = */ nullptr,
  12711. };
  12712. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(int device) {
  12713. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  12714. static bool ggml_backend_sycl_buffer_type_initialized = false;
  12715. if (!ggml_backend_sycl_buffer_type_initialized) {
  12716. for (int i = 0; i < GGML_SYCL_MAX_DEVICES; i++) {
  12717. ggml_backend_sycl_buffer_types[i] = {
  12718. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  12719. /* .context = */ (ggml_backend_buffer_type_context_t) (intptr_t) i,
  12720. };
  12721. }
  12722. ggml_backend_sycl_buffer_type_initialized = true;
  12723. }
  12724. return &ggml_backend_sycl_buffer_types[device];
  12725. }
  12726. // host buffer type
  12727. GGML_CALL static const char * ggml_backend_sycl_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  12728. return GGML_SYCL_NAME "_Host";
  12729. UNUSED(buft);
  12730. }
  12731. GGML_CALL static const char * ggml_backend_sycl_host_buffer_name(ggml_backend_buffer_t buffer) {
  12732. return GGML_SYCL_NAME "_Host";
  12733. UNUSED(buffer);
  12734. }
  12735. static void ggml_backend_sycl_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  12736. ggml_sycl_host_free(buffer->context);
  12737. }
  12738. static ggml_backend_buffer_t ggml_backend_sycl_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  12739. void * ptr = ggml_sycl_host_malloc(size);
  12740. if (ptr == nullptr) {
  12741. // fallback to cpu buffer
  12742. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  12743. }
  12744. // FIXME: this is a hack to avoid having to implement a new buffer type
  12745. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  12746. buffer->buft = buft;
  12747. buffer->iface.free_buffer = ggml_backend_sycl_host_buffer_free_buffer;
  12748. return buffer;
  12749. }
  12750. ggml_backend_buffer_type_t ggml_backend_sycl_host_buffer_type() {
  12751. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_type_host = {
  12752. /* .iface = */ {
  12753. /* .get_name = */ ggml_backend_sycl_host_buffer_type_name,
  12754. /* .alloc_buffer = */ ggml_backend_sycl_host_buffer_type_alloc_buffer,
  12755. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  12756. /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
  12757. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  12758. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  12759. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  12760. },
  12761. /* .context = */ nullptr,
  12762. };
  12763. return &ggml_backend_sycl_buffer_type_host;
  12764. }
  12765. // backend
  12766. struct ggml_backend_context_sycl {
  12767. int device;
  12768. };
  12769. static const char * ggml_backend_sycl_name(ggml_backend_t backend) {
  12770. return GGML_SYCL_NAME;
  12771. UNUSED(backend);
  12772. }
  12773. static void ggml_backend_sycl_free(ggml_backend_t backend) {
  12774. ggml_backend_context_sycl * sycl_ctx = (ggml_backend_context_sycl *)backend->context;
  12775. delete sycl_ctx;
  12776. delete backend;
  12777. }
  12778. static ggml_backend_buffer_type_t ggml_backend_sycl_get_default_buffer_type(ggml_backend_t backend) {
  12779. ggml_backend_context_sycl * sycl_ctx = (ggml_backend_context_sycl *)backend->context;
  12780. return ggml_backend_sycl_buffer_type(sycl_ctx->device);
  12781. }
  12782. static void ggml_backend_sycl_set_tensor_async(ggml_backend_t backend,
  12783. ggml_tensor *tensor,
  12784. const void *data, size_t offset,
  12785. size_t size) try {
  12786. ggml_backend_context_sycl * sycl_ctx = (ggml_backend_context_sycl *)backend->context;
  12787. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  12788. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  12789. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  12790. (char *)tensor->data + offset, data, size)));
  12791. }
  12792. catch (sycl::exception const &exc) {
  12793. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12794. << ", line:" << __LINE__ << std::endl;
  12795. std::exit(1);
  12796. }
  12797. static void ggml_backend_sycl_get_tensor_async(ggml_backend_t backend,
  12798. const ggml_tensor *tensor,
  12799. void *data, size_t offset,
  12800. size_t size) try {
  12801. ggml_backend_context_sycl * sycl_ctx = (ggml_backend_context_sycl *)backend->context;
  12802. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  12803. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  12804. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  12805. data, (const char *)tensor->data + offset, size)));
  12806. }
  12807. catch (sycl::exception const &exc) {
  12808. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12809. << ", line:" << __LINE__ << std::endl;
  12810. std::exit(1);
  12811. }
  12812. static void ggml_backend_sycl_synchronize(ggml_backend_t backend) try {
  12813. ggml_backend_context_sycl * sycl_ctx = (ggml_backend_context_sycl *)backend->context;
  12814. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->wait()));
  12815. UNUSED(backend);
  12816. }
  12817. catch (sycl::exception const &exc) {
  12818. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12819. << ", line:" << __LINE__ << std::endl;
  12820. std::exit(1);
  12821. }
  12822. static ggml_backend_graph_plan_t ggml_backend_sycl_graph_plan_create(ggml_backend_t backend, const ggml_cgraph * cgraph) {
  12823. GGML_ASSERT(!"not implemented");
  12824. return nullptr;
  12825. UNUSED(backend);
  12826. UNUSED(cgraph);
  12827. }
  12828. static void ggml_backend_sycl_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  12829. GGML_ASSERT(!"not implemented");
  12830. UNUSED(backend);
  12831. UNUSED(plan);
  12832. }
  12833. static void ggml_backend_sycl_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  12834. GGML_ASSERT(!"not implemented");
  12835. UNUSED(backend);
  12836. UNUSED(plan);
  12837. }
  12838. static bool ggml_backend_sycl_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  12839. ggml_backend_context_sycl * sycl_ctx = (ggml_backend_context_sycl *)backend->context;
  12840. ggml_sycl_set_main_device(sycl_ctx->device);
  12841. ggml_compute_params params = {};
  12842. params.type = GGML_TASK_COMPUTE;
  12843. params.ith = 0;
  12844. for (int i = 0; i < cgraph->n_nodes; i++) {
  12845. ggml_tensor * node = cgraph->nodes[i];
  12846. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE)
  12847. continue;
  12848. assert(node->backend == GGML_BACKEND_GPU);
  12849. assert(node->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  12850. assert(node->extra != nullptr);
  12851. for (int j = 0; j < GGML_MAX_SRC; j++) {
  12852. if (node->src[j] != nullptr) {
  12853. assert(node->src[j]->backend == GGML_BACKEND_GPU);
  12854. assert(node->src[j]->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  12855. assert(node->src[j]->extra != nullptr);
  12856. }
  12857. }
  12858. bool ok = ggml_sycl_compute_forward(&params, node);
  12859. if (!ok) {
  12860. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  12861. }
  12862. GGML_ASSERT(ok);
  12863. #if 0
  12864. if (node->type == GGML_TYPE_F32) {
  12865. syclDeviceSynchronize();
  12866. std::vector<float> tmp(ggml_nelements(node), 0.0f);
  12867. syclMemcpy(tmp.data(), node->data, ggml_nelements(node)*sizeof(float), syclMemcpyDeviceToHost);
  12868. printf("\n%s (%s) (%s %s) (%s %s): ", node->name, ggml_op_name(node->op),
  12869. ggml_type_name(node->src[0]->type),
  12870. node->src[1] ? ggml_type_name(node->src[1]->type) : "none",
  12871. node->src[0]->name,
  12872. node->src[1] ? node->src[1]->name : "none");
  12873. double sum = 0.0;
  12874. double sq_sum = 0.0;
  12875. for (int i = 0; i < ggml_nelements(node); i++) {
  12876. printf("%f ", tmp[i]);
  12877. sum += tmp[i];
  12878. sq_sum += tmp[i]*tmp[i];
  12879. }
  12880. printf("\n");
  12881. printf("sum: %f, ", sum);
  12882. printf("sq_sum: %f\n", sq_sum);
  12883. }
  12884. #endif
  12885. }
  12886. UNUSED(backend);
  12887. return true;
  12888. }
  12889. static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  12890. switch (op->op) {
  12891. case GGML_OP_UNARY:
  12892. switch (ggml_get_unary_op(op)) {
  12893. case GGML_UNARY_OP_GELU:
  12894. case GGML_UNARY_OP_SILU:
  12895. case GGML_UNARY_OP_RELU:
  12896. case GGML_UNARY_OP_GELU_QUICK:
  12897. case GGML_UNARY_OP_TANH:
  12898. return true;
  12899. default:
  12900. return false;
  12901. }
  12902. break;
  12903. case GGML_OP_MUL_MAT:
  12904. case GGML_OP_MUL_MAT_ID:
  12905. {
  12906. struct ggml_tensor * a;
  12907. struct ggml_tensor * b;
  12908. if (op->op == GGML_OP_MUL_MAT) {
  12909. a = op->src[0];
  12910. b = op->src[1];
  12911. } else {
  12912. a = op->src[2];
  12913. b = op->src[1];
  12914. }
  12915. if (a->ne[3] != b->ne[3]) {
  12916. return false;
  12917. }
  12918. if (a->type == GGML_TYPE_IQ2_XXS) {
  12919. return false;
  12920. }
  12921. if (a->type == GGML_TYPE_IQ2_XS) {
  12922. return false;
  12923. }
  12924. return true;
  12925. } break;
  12926. case GGML_OP_GET_ROWS:
  12927. {
  12928. switch (op->src[0]->type) {
  12929. case GGML_TYPE_F16:
  12930. case GGML_TYPE_F32:
  12931. case GGML_TYPE_Q4_0:
  12932. case GGML_TYPE_Q4_1:
  12933. case GGML_TYPE_Q5_0:
  12934. case GGML_TYPE_Q5_1:
  12935. case GGML_TYPE_Q8_0:
  12936. return true;
  12937. default:
  12938. return false;
  12939. }
  12940. } break;
  12941. case GGML_OP_CPY:
  12942. {
  12943. ggml_type src0_type = op->src[0]->type;
  12944. ggml_type src1_type = op->src[1]->type;
  12945. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  12946. return true;
  12947. }
  12948. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  12949. return true;
  12950. }
  12951. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  12952. return true;
  12953. }
  12954. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  12955. return true;
  12956. }
  12957. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  12958. return true;
  12959. }
  12960. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  12961. return true;
  12962. }
  12963. return false;
  12964. } break;
  12965. case GGML_OP_CONCAT:
  12966. {
  12967. ggml_type src0_type = op->src[0]->type;
  12968. if (src0_type == GGML_TYPE_F32) {
  12969. return true;
  12970. } else {
  12971. return false;
  12972. }
  12973. } break;
  12974. case GGML_OP_NONE:
  12975. case GGML_OP_RESHAPE:
  12976. case GGML_OP_VIEW:
  12977. case GGML_OP_PERMUTE:
  12978. case GGML_OP_TRANSPOSE:
  12979. case GGML_OP_NORM:
  12980. case GGML_OP_REPEAT:
  12981. case GGML_OP_DUP:
  12982. case GGML_OP_ADD:
  12983. case GGML_OP_MUL:
  12984. case GGML_OP_DIV:
  12985. case GGML_OP_RMS_NORM:
  12986. case GGML_OP_SCALE:
  12987. case GGML_OP_SQR:
  12988. case GGML_OP_CLAMP:
  12989. case GGML_OP_CONT:
  12990. case GGML_OP_DIAG_MASK_INF:
  12991. case GGML_OP_SOFT_MAX:
  12992. case GGML_OP_ROPE:
  12993. case GGML_OP_ALIBI:
  12994. case GGML_OP_IM2COL:
  12995. case GGML_OP_SUM_ROWS:
  12996. case GGML_OP_ARGSORT:
  12997. case GGML_OP_ACC:
  12998. case GGML_OP_GROUP_NORM:
  12999. case GGML_OP_UPSCALE:
  13000. case GGML_OP_PAD:
  13001. case GGML_OP_LEAKY_RELU:
  13002. return true;
  13003. default:
  13004. return false;
  13005. }
  13006. UNUSED(backend);
  13007. }
  13008. static ggml_backend_i ggml_backend_sycl_interface = {
  13009. /* .get_name = */ ggml_backend_sycl_name,
  13010. /* .free = */ ggml_backend_sycl_free,
  13011. /* .get_default_buffer_type = */ ggml_backend_sycl_get_default_buffer_type,
  13012. /* .set_tensor_async = */ ggml_backend_sycl_set_tensor_async,
  13013. /* .get_tensor_async = */ ggml_backend_sycl_get_tensor_async,
  13014. /* .cpy_tensor_async = */ NULL,
  13015. /* .synchronize = */ ggml_backend_sycl_synchronize,
  13016. /* .graph_plan_create = */ ggml_backend_sycl_graph_plan_create,
  13017. /* .graph_plan_free = */ ggml_backend_sycl_graph_plan_free,
  13018. /* .graph_plan_compute = */ ggml_backend_sycl_graph_plan_compute,
  13019. /* .graph_compute = */ ggml_backend_sycl_graph_compute,
  13020. /* .supports_op = */ ggml_backend_sycl_supports_op,
  13021. };
  13022. ggml_backend_t ggml_backend_sycl_init(int device) {
  13023. ggml_init_sycl(); // TODO: remove from ggml.c
  13024. if (device < 0 || device >= ggml_sycl_get_device_count()) {
  13025. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  13026. return nullptr;
  13027. }
  13028. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  13029. ggml_sycl_set_main_device(device);
  13030. ggml_backend_context_sycl * ctx = new ggml_backend_context_sycl {
  13031. /* .device = */ device
  13032. };
  13033. ggml_backend_t sycl_backend = new ggml_backend {
  13034. /* .interface = */ ggml_backend_sycl_interface,
  13035. /* .context = */ ctx
  13036. };
  13037. return sycl_backend;
  13038. }
  13039. bool ggml_backend_is_sycl(ggml_backend_t backend) {
  13040. return backend->iface.get_name == ggml_backend_sycl_name;
  13041. }
  13042. static ggml_backend_t ggml_backend_reg_sycl_init(const char * params, void * user_data) {
  13043. ggml_backend_t sycl_backend = ggml_backend_sycl_init((int) (intptr_t) user_data);
  13044. return sycl_backend;
  13045. UNUSED(params);
  13046. }
  13047. extern "C" int ggml_backend_sycl_reg_devices();
  13048. int ggml_backend_sycl_reg_devices() {
  13049. int device_count = ggml_sycl_get_device_count();
  13050. for (int i = 0; i < device_count; i++) {
  13051. char name[128];
  13052. snprintf(name, sizeof(name), "%s%d", GGML_SYCL_NAME, i);
  13053. ggml_backend_register(name, ggml_backend_reg_sycl_init, ggml_backend_sycl_buffer_type(i), (void *) (intptr_t) i);
  13054. }
  13055. return device_count;
  13056. }