ggml-sycl.cpp 580 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206
  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. /// Returns the maximum clock rate of device's global memory in kHz. If
  309. /// compiler does not support this API then returns default value 3200000 kHz.
  310. unsigned int get_memory_clock_rate() const { return _memory_clock_rate; }
  311. /// Returns the maximum bus width between device and memory in bits. If
  312. /// compiler does not support this API then returns default value 64 bits.
  313. unsigned int get_memory_bus_width() const { return _memory_bus_width; }
  314. uint32_t get_device_id() const { return _device_id; }
  315. std::array<unsigned char, 16> get_uuid() const { return _uuid; }
  316. /// Returns global memory cache size in bytes.
  317. unsigned int get_global_mem_cache_size() const
  318. {
  319. return _global_mem_cache_size;
  320. }
  321. // set interface
  322. void set_name(const char *name)
  323. {
  324. size_t length = strlen(name);
  325. if (length < 256)
  326. {
  327. std::memcpy(_name, name, length + 1);
  328. }
  329. else
  330. {
  331. std::memcpy(_name, name, 255);
  332. _name[255] = '\0';
  333. }
  334. }
  335. void set_max_work_item_sizes(const sycl::range<3> max_work_item_sizes)
  336. {
  337. for (int i = 0; i < 3; ++i)
  338. _max_work_item_sizes_i[i] = max_work_item_sizes[i];
  339. }
  340. [[deprecated]] void
  341. set_max_work_item_sizes(const sycl::id<3> max_work_item_sizes)
  342. {
  343. for (int i = 0; i < 3; ++i)
  344. {
  345. _max_work_item_sizes_i[i] = max_work_item_sizes[i];
  346. }
  347. }
  348. void set_host_unified_memory(bool host_unified_memory)
  349. {
  350. _host_unified_memory = host_unified_memory;
  351. }
  352. void set_major_version(int major) { _major = major; }
  353. void set_minor_version(int minor) { _minor = minor; }
  354. void set_integrated(int integrated) { _integrated = integrated; }
  355. void set_max_clock_frequency(int frequency) { _frequency = frequency; }
  356. void set_max_compute_units(int max_compute_units)
  357. {
  358. _max_compute_units = max_compute_units;
  359. }
  360. void set_global_mem_size(size_t global_mem_size)
  361. {
  362. _global_mem_size = global_mem_size;
  363. }
  364. void set_local_mem_size(size_t local_mem_size)
  365. {
  366. _local_mem_size = local_mem_size;
  367. }
  368. void set_max_work_group_size(int max_work_group_size)
  369. {
  370. _max_work_group_size = max_work_group_size;
  371. }
  372. void set_max_sub_group_size(int max_sub_group_size)
  373. {
  374. _max_sub_group_size = max_sub_group_size;
  375. }
  376. void
  377. set_max_work_items_per_compute_unit(int max_work_items_per_compute_unit)
  378. {
  379. _max_work_items_per_compute_unit = max_work_items_per_compute_unit;
  380. }
  381. void set_max_nd_range_size(int max_nd_range_size[])
  382. {
  383. for (int i = 0; i < 3; i++)
  384. {
  385. _max_nd_range_size[i] = max_nd_range_size[i];
  386. _max_nd_range_size_i[i] = max_nd_range_size[i];
  387. }
  388. }
  389. void set_memory_clock_rate(unsigned int memory_clock_rate)
  390. {
  391. _memory_clock_rate = memory_clock_rate;
  392. }
  393. void set_memory_bus_width(unsigned int memory_bus_width)
  394. {
  395. _memory_bus_width = memory_bus_width;
  396. }
  397. void
  398. set_max_register_size_per_work_group(int max_register_size_per_work_group)
  399. {
  400. _max_register_size_per_work_group = max_register_size_per_work_group;
  401. }
  402. void set_device_id(uint32_t device_id)
  403. {
  404. _device_id = device_id;
  405. }
  406. void set_uuid(std::array<unsigned char, 16> uuid)
  407. {
  408. _uuid = std::move(uuid);
  409. }
  410. void set_global_mem_cache_size(unsigned int global_mem_cache_size)
  411. {
  412. _global_mem_cache_size = global_mem_cache_size;
  413. }
  414. private:
  415. char _name[256];
  416. int _max_work_item_sizes_i[3];
  417. bool _host_unified_memory = false;
  418. int _major;
  419. int _minor;
  420. int _integrated = 0;
  421. int _frequency;
  422. // Set estimated value 3200000 kHz as default value.
  423. unsigned int _memory_clock_rate = 3200000;
  424. // Set estimated value 64 bits as default value.
  425. unsigned int _memory_bus_width = 64;
  426. unsigned int _global_mem_cache_size;
  427. int _max_compute_units;
  428. int _max_work_group_size;
  429. int _max_sub_group_size;
  430. int _max_work_items_per_compute_unit;
  431. int _max_register_size_per_work_group;
  432. size_t _global_mem_size;
  433. size_t _local_mem_size;
  434. size_t _max_nd_range_size[3];
  435. int _max_nd_range_size_i[3];
  436. uint32_t _device_id;
  437. std::array<unsigned char, 16> _uuid;
  438. };
  439. static int get_major_version(const sycl::device &dev)
  440. {
  441. int major, minor;
  442. detail::get_version(dev, major, minor);
  443. return major;
  444. }
  445. static int get_minor_version(const sycl::device &dev)
  446. {
  447. int major, minor;
  448. detail::get_version(dev, major, minor);
  449. return minor;
  450. }
  451. static void get_device_info(device_info &out, const sycl::device &dev)
  452. {
  453. device_info prop;
  454. prop.set_name(dev.get_info<sycl::info::device::name>().c_str());
  455. int major, minor;
  456. detail::get_version(dev, major, minor);
  457. prop.set_major_version(major);
  458. prop.set_minor_version(minor);
  459. prop.set_max_work_item_sizes(
  460. #if (__SYCL_COMPILER_VERSION && __SYCL_COMPILER_VERSION < 20220902)
  461. // oneAPI DPC++ compiler older than 2022/09/02, where max_work_item_sizes
  462. // is an enum class element
  463. dev.get_info<sycl::info::device::max_work_item_sizes>());
  464. #else
  465. // SYCL 2020-conformant code, max_work_item_sizes is a struct templated by
  466. // an int
  467. dev.get_info<sycl::info::device::max_work_item_sizes<3>>());
  468. #endif
  469. prop.set_host_unified_memory(dev.has(sycl::aspect::usm_host_allocations));
  470. prop.set_max_clock_frequency(
  471. dev.get_info<sycl::info::device::max_clock_frequency>() * 1000);
  472. prop.set_max_compute_units(
  473. dev.get_info<sycl::info::device::max_compute_units>());
  474. prop.set_max_work_group_size(
  475. dev.get_info<sycl::info::device::max_work_group_size>());
  476. prop.set_global_mem_size(dev.get_info<sycl::info::device::global_mem_size>());
  477. prop.set_local_mem_size(dev.get_info<sycl::info::device::local_mem_size>());
  478. #if (defined(SYCL_EXT_INTEL_DEVICE_INFO) && SYCL_EXT_INTEL_DEVICE_INFO >= 6)
  479. if (dev.has(sycl::aspect::ext_intel_memory_clock_rate))
  480. {
  481. unsigned int tmp =
  482. dev.get_info<sycl::ext::intel::info::device::memory_clock_rate>();
  483. if (tmp != 0)
  484. prop.set_memory_clock_rate(1000 * tmp);
  485. }
  486. if (dev.has(sycl::aspect::ext_intel_memory_bus_width))
  487. {
  488. prop.set_memory_bus_width(
  489. dev.get_info<sycl::ext::intel::info::device::memory_bus_width>());
  490. }
  491. if (dev.has(sycl::aspect::ext_intel_device_id))
  492. {
  493. prop.set_device_id(
  494. dev.get_info<sycl::ext::intel::info::device::device_id>());
  495. }
  496. if (dev.has(sycl::aspect::ext_intel_device_info_uuid))
  497. {
  498. prop.set_uuid(dev.get_info<sycl::ext::intel::info::device::uuid>());
  499. }
  500. #elif defined(_MSC_VER) && !defined(__clang__)
  501. #pragma message("get_device_info: querying memory_clock_rate and \
  502. memory_bus_width are not supported by the compiler used. \
  503. Use 3200000 kHz as memory_clock_rate default value. \
  504. Use 64 bits as memory_bus_width default value.")
  505. #else
  506. #warning "get_device_info: querying memory_clock_rate and \
  507. memory_bus_width are not supported by the compiler used. \
  508. Use 3200000 kHz as memory_clock_rate default value. \
  509. Use 64 bits as memory_bus_width default value."
  510. #endif
  511. size_t max_sub_group_size = 1;
  512. std::vector<size_t> sub_group_sizes =
  513. dev.get_info<sycl::info::device::sub_group_sizes>();
  514. for (const auto &sub_group_size : sub_group_sizes)
  515. {
  516. if (max_sub_group_size < sub_group_size)
  517. max_sub_group_size = sub_group_size;
  518. }
  519. prop.set_max_sub_group_size(max_sub_group_size);
  520. prop.set_max_work_items_per_compute_unit(
  521. dev.get_info<sycl::info::device::max_work_group_size>());
  522. int max_nd_range_size[] = {0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF};
  523. prop.set_max_nd_range_size(max_nd_range_size);
  524. // Estimates max register size per work group, feel free to update the value
  525. // according to device properties.
  526. prop.set_max_register_size_per_work_group(65536);
  527. prop.set_global_mem_cache_size(
  528. dev.get_info<sycl::info::device::global_mem_cache_size>());
  529. out = prop;
  530. }
  531. /// dpct device extension
  532. class device_ext : public sycl::device
  533. {
  534. typedef std::mutex mutex_type;
  535. public:
  536. device_ext() : sycl::device(), _ctx(*this) {}
  537. ~device_ext()
  538. {
  539. std::lock_guard<mutex_type> lock(m_mutex);
  540. clear_queues();
  541. }
  542. device_ext(const sycl::device &base) : sycl::device(base), _ctx(*this)
  543. {
  544. std::lock_guard<mutex_type> lock(m_mutex);
  545. init_queues();
  546. }
  547. int is_native_atomic_supported() { return 0; }
  548. int get_major_version() const
  549. {
  550. return dpct::get_major_version(*this);
  551. }
  552. int get_minor_version() const
  553. {
  554. return dpct::get_minor_version(*this);
  555. }
  556. int get_max_compute_units() const
  557. {
  558. return get_device_info().get_max_compute_units();
  559. }
  560. /// Return the maximum clock frequency of this device in KHz.
  561. int get_max_clock_frequency() const
  562. {
  563. return get_device_info().get_max_clock_frequency();
  564. }
  565. int get_integrated() const { return get_device_info().get_integrated(); }
  566. int get_max_sub_group_size() const
  567. {
  568. return get_device_info().get_max_sub_group_size();
  569. }
  570. int get_max_register_size_per_work_group() const
  571. {
  572. return get_device_info().get_max_register_size_per_work_group();
  573. }
  574. int get_max_work_group_size() const
  575. {
  576. return get_device_info().get_max_work_group_size();
  577. }
  578. int get_mem_base_addr_align() const
  579. {
  580. return get_info<sycl::info::device::mem_base_addr_align>();
  581. }
  582. size_t get_global_mem_size() const
  583. {
  584. return get_device_info().get_global_mem_size();
  585. }
  586. /// Get the number of bytes of free and total memory on the SYCL device.
  587. /// \param [out] free_memory The number of bytes of free memory on the SYCL device.
  588. /// \param [out] total_memory The number of bytes of total memory on the SYCL device.
  589. void get_memory_info(size_t &free_memory, size_t &total_memory)
  590. {
  591. #if (defined(__SYCL_COMPILER_VERSION) && __SYCL_COMPILER_VERSION >= 20221105)
  592. if (!has(sycl::aspect::ext_intel_free_memory))
  593. {
  594. std::cerr << "get_memory_info: ext_intel_free_memory is not supported." << std::endl;
  595. free_memory = 0;
  596. }
  597. else
  598. {
  599. free_memory = get_info<sycl::ext::intel::info::device::free_memory>();
  600. }
  601. #else
  602. std::cerr << "get_memory_info: ext_intel_free_memory is not supported." << std::endl;
  603. free_memory = 0;
  604. #if defined(_MSC_VER) && !defined(__clang__)
  605. #pragma message("Querying the number of bytes of free memory is not supported")
  606. #else
  607. #warning "Querying the number of bytes of free memory is not supported"
  608. #endif
  609. #endif
  610. total_memory = get_device_info().get_global_mem_size();
  611. }
  612. void get_device_info(device_info &out) const
  613. {
  614. dpct::get_device_info(out, *this);
  615. }
  616. device_info get_device_info() const
  617. {
  618. device_info prop;
  619. dpct::get_device_info(prop, *this);
  620. return prop;
  621. }
  622. void reset()
  623. {
  624. std::lock_guard<mutex_type> lock(m_mutex);
  625. clear_queues();
  626. init_queues();
  627. }
  628. sycl::queue &in_order_queue() { return *_q_in_order; }
  629. sycl::queue &out_of_order_queue() { return *_q_out_of_order; }
  630. sycl::queue &default_queue()
  631. {
  632. #ifdef DPCT_USM_LEVEL_NONE
  633. return out_of_order_queue();
  634. #else
  635. return in_order_queue();
  636. #endif // DPCT_USM_LEVEL_NONE
  637. }
  638. void queues_wait_and_throw()
  639. {
  640. std::unique_lock<mutex_type> lock(m_mutex);
  641. std::vector<std::shared_ptr<sycl::queue>> current_queues(
  642. _queues);
  643. lock.unlock();
  644. for (const auto &q : current_queues)
  645. {
  646. q->wait_and_throw();
  647. }
  648. // Guard the destruct of current_queues to make sure the ref count is safe.
  649. lock.lock();
  650. }
  651. sycl::queue *create_queue(bool enable_exception_handler = false)
  652. {
  653. #ifdef DPCT_USM_LEVEL_NONE
  654. return create_out_of_order_queue(enable_exception_handler);
  655. #else
  656. return create_in_order_queue(enable_exception_handler);
  657. #endif // DPCT_USM_LEVEL_NONE
  658. }
  659. sycl::queue *create_in_order_queue(bool enable_exception_handler = false)
  660. {
  661. std::lock_guard<mutex_type> lock(m_mutex);
  662. return create_queue_impl(enable_exception_handler,
  663. sycl::property::queue::in_order());
  664. }
  665. sycl::queue *create_out_of_order_queue(bool enable_exception_handler = false)
  666. {
  667. std::lock_guard<mutex_type> lock(m_mutex);
  668. return create_queue_impl(enable_exception_handler);
  669. }
  670. void destroy_queue(sycl::queue *&queue)
  671. {
  672. std::lock_guard<mutex_type> lock(m_mutex);
  673. _queues.erase(std::remove_if(_queues.begin(), _queues.end(),
  674. [=](const std::shared_ptr<sycl::queue> &q) -> bool
  675. {
  676. return q.get() == queue;
  677. }),
  678. _queues.end());
  679. queue = nullptr;
  680. }
  681. void set_saved_queue(sycl::queue *q)
  682. {
  683. std::lock_guard<mutex_type> lock(m_mutex);
  684. _saved_queue = q;
  685. }
  686. sycl::queue *get_saved_queue() const
  687. {
  688. std::lock_guard<mutex_type> lock(m_mutex);
  689. return _saved_queue;
  690. }
  691. sycl::context get_context() const { return _ctx; }
  692. private:
  693. void clear_queues()
  694. {
  695. _queues.clear();
  696. _q_in_order = _q_out_of_order = _saved_queue = nullptr;
  697. }
  698. void init_queues()
  699. {
  700. _q_in_order = create_queue_impl(true, sycl::property::queue::in_order());
  701. _q_out_of_order = create_queue_impl(true);
  702. _saved_queue = &default_queue();
  703. }
  704. /// Caller should acquire resource \p m_mutex before calling this function.
  705. template <class... Properties>
  706. sycl::queue *create_queue_impl(bool enable_exception_handler,
  707. Properties... properties)
  708. {
  709. sycl::async_handler eh = {};
  710. if (enable_exception_handler)
  711. {
  712. eh = exception_handler;
  713. }
  714. _queues.push_back(std::make_shared<sycl::queue>(
  715. _ctx, *this, eh,
  716. sycl::property_list(
  717. #ifdef DPCT_PROFILING_ENABLED
  718. sycl::property::queue::enable_profiling(),
  719. #endif
  720. properties...)));
  721. return _queues.back().get();
  722. }
  723. void get_version(int &major, int &minor) const
  724. {
  725. detail::get_version(*this, major, minor);
  726. }
  727. sycl::queue *_q_in_order, *_q_out_of_order;
  728. sycl::queue *_saved_queue;
  729. sycl::context _ctx;
  730. std::vector<std::shared_ptr<sycl::queue>> _queues;
  731. mutable mutex_type m_mutex;
  732. };
  733. /// device manager
  734. class dev_mgr
  735. {
  736. public:
  737. device_ext &current_device()
  738. {
  739. unsigned int dev_id = current_device_id();
  740. check_id(dev_id);
  741. return *_devs[dev_id];
  742. }
  743. device_ext &cpu_device() const
  744. {
  745. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  746. if (_cpu_device == -1)
  747. {
  748. throw std::runtime_error("no valid cpu device");
  749. }
  750. else
  751. {
  752. return *_devs[_cpu_device];
  753. }
  754. }
  755. device_ext &get_device(unsigned int id) const
  756. {
  757. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  758. check_id(id);
  759. return *_devs[id];
  760. }
  761. unsigned int current_device_id() const
  762. {
  763. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  764. auto it = _thread2dev_map.find(get_tid());
  765. if (it != _thread2dev_map.end())
  766. return it->second;
  767. return DEFAULT_DEVICE_ID;
  768. }
  769. /// Select device with a device ID.
  770. /// \param [in] id The id of the device which can
  771. /// be obtained through get_device_id(const sycl::device).
  772. void select_device(unsigned int id)
  773. {
  774. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  775. check_id(id);
  776. _thread2dev_map[get_tid()] = id;
  777. }
  778. unsigned int device_count() { return _devs.size(); }
  779. unsigned int get_device_id(const sycl::device &dev)
  780. {
  781. unsigned int id = 0;
  782. for (auto dev_item : _devs)
  783. {
  784. if (*dev_item == dev)
  785. {
  786. break;
  787. }
  788. id++;
  789. }
  790. return id;
  791. }
  792. template <class DeviceSelector>
  793. std::enable_if_t<
  794. std::is_invocable_r_v<int, DeviceSelector, const sycl::device &>>
  795. select_device(const DeviceSelector &selector = sycl::gpu_selector_v)
  796. {
  797. sycl::device selected_device = sycl::device(selector);
  798. unsigned int selected_device_id = get_device_id(selected_device);
  799. select_device(selected_device_id);
  800. }
  801. /// Returns the instance of device manager singleton.
  802. static dev_mgr &instance()
  803. {
  804. static dev_mgr d_m;
  805. return d_m;
  806. }
  807. dev_mgr(const dev_mgr &) = delete;
  808. dev_mgr &operator=(const dev_mgr &) = delete;
  809. dev_mgr(dev_mgr &&) = delete;
  810. dev_mgr &operator=(dev_mgr &&) = delete;
  811. private:
  812. mutable std::recursive_mutex m_mutex;
  813. dev_mgr()
  814. {
  815. sycl::device default_device =
  816. sycl::device(sycl::default_selector_v);
  817. _devs.push_back(std::make_shared<device_ext>(default_device));
  818. std::vector<sycl::device> sycl_all_devs =
  819. sycl::device::get_devices(sycl::info::device_type::all);
  820. // Collect other devices except for the default device.
  821. if (default_device.is_cpu())
  822. _cpu_device = 0;
  823. for (auto &dev : sycl_all_devs)
  824. {
  825. if (dev == default_device)
  826. {
  827. continue;
  828. }
  829. _devs.push_back(std::make_shared<device_ext>(dev));
  830. if (_cpu_device == -1 && dev.is_cpu())
  831. {
  832. _cpu_device = _devs.size() - 1;
  833. }
  834. }
  835. }
  836. void check_id(unsigned int id) const
  837. {
  838. if (id >= _devs.size())
  839. {
  840. throw std::runtime_error("invalid device id");
  841. }
  842. }
  843. std::vector<std::shared_ptr<device_ext>> _devs;
  844. /// DEFAULT_DEVICE_ID is used, if current_device_id() can not find current
  845. /// thread id in _thread2dev_map, which means default device should be used
  846. /// for the current thread.
  847. const unsigned int DEFAULT_DEVICE_ID = 0;
  848. /// thread-id to device-id map.
  849. std::map<unsigned int, unsigned int> _thread2dev_map;
  850. int _cpu_device = -1;
  851. };
  852. static inline sycl::queue &get_default_queue()
  853. {
  854. return dev_mgr::instance().current_device().default_queue();
  855. }
  856. namespace detail
  857. {
  858. enum class pointer_access_attribute
  859. {
  860. host_only = 0,
  861. device_only,
  862. host_device,
  863. end
  864. };
  865. static pointer_access_attribute get_pointer_attribute(sycl::queue &q,
  866. const void *ptr)
  867. {
  868. #ifdef DPCT_USM_LEVEL_NONE
  869. return mem_mgr::instance().is_device_ptr(ptr)
  870. ? pointer_access_attribute::device_only
  871. : pointer_access_attribute::host_only;
  872. #else
  873. switch (sycl::get_pointer_type(ptr, q.get_context()))
  874. {
  875. case sycl::usm::alloc::unknown:
  876. return pointer_access_attribute::host_only;
  877. case sycl::usm::alloc::device:
  878. return pointer_access_attribute::device_only;
  879. case sycl::usm::alloc::shared:
  880. case sycl::usm::alloc::host:
  881. return pointer_access_attribute::host_device;
  882. }
  883. #endif
  884. }
  885. template <typename ArgT>
  886. inline constexpr std::uint64_t get_type_combination_id(ArgT Val)
  887. {
  888. static_assert((unsigned char)library_data_t::library_data_t_size <=
  889. std::numeric_limits<unsigned char>::max() &&
  890. "library_data_t size exceeds limit.");
  891. static_assert(std::is_same_v<ArgT, library_data_t>, "Unsupported ArgT");
  892. return (std::uint64_t)Val;
  893. }
  894. template <typename FirstT, typename... RestT>
  895. inline constexpr std::uint64_t get_type_combination_id(FirstT FirstVal,
  896. RestT... RestVal)
  897. {
  898. static_assert((std::uint8_t)library_data_t::library_data_t_size <=
  899. std::numeric_limits<unsigned char>::max() &&
  900. "library_data_t size exceeds limit.");
  901. static_assert(sizeof...(RestT) <= 8 && "Too many parameters");
  902. static_assert(std::is_same_v<FirstT, library_data_t>, "Unsupported FirstT");
  903. return get_type_combination_id(RestVal...) << 8 | ((std::uint64_t)FirstVal);
  904. }
  905. class mem_mgr
  906. {
  907. mem_mgr()
  908. {
  909. // Reserved address space, no real memory allocation happens here.
  910. #if defined(__linux__)
  911. mapped_address_space =
  912. (byte_t *)mmap(nullptr, mapped_region_size, PROT_NONE,
  913. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  914. #elif defined(_WIN64)
  915. mapped_address_space = (byte_t *)VirtualAlloc(
  916. NULL, // NULL specified as the base address parameter
  917. mapped_region_size, // Size of allocation
  918. MEM_RESERVE, // Allocate reserved pages
  919. PAGE_NOACCESS); // Protection = no access
  920. #else
  921. #error "Only support Windows and Linux."
  922. #endif
  923. next_free = mapped_address_space;
  924. };
  925. public:
  926. using buffer_id_t = int;
  927. struct allocation
  928. {
  929. buffer_t buffer;
  930. byte_t *alloc_ptr;
  931. size_t size;
  932. };
  933. ~mem_mgr()
  934. {
  935. #if defined(__linux__)
  936. munmap(mapped_address_space, mapped_region_size);
  937. #elif defined(_WIN64)
  938. VirtualFree(mapped_address_space, 0, MEM_RELEASE);
  939. #else
  940. #error "Only support Windows and Linux."
  941. #endif
  942. };
  943. mem_mgr(const mem_mgr &) = delete;
  944. mem_mgr &operator=(const mem_mgr &) = delete;
  945. mem_mgr(mem_mgr &&) = delete;
  946. mem_mgr &operator=(mem_mgr &&) = delete;
  947. /// Allocate
  948. void *mem_alloc(size_t size)
  949. {
  950. if (!size)
  951. return nullptr;
  952. std::lock_guard<std::mutex> lock(m_mutex);
  953. if (next_free + size > mapped_address_space + mapped_region_size)
  954. {
  955. throw std::runtime_error("dpct_malloc: out of memory for virtual memory pool");
  956. }
  957. // Allocation
  958. sycl::range<1> r(size);
  959. buffer_t buf(r);
  960. allocation A{buf, next_free, size};
  961. // Map allocation to device pointer
  962. void *result = next_free;
  963. m_map.emplace(next_free + size, A);
  964. // Update pointer to the next free space.
  965. next_free += (size + extra_padding + alignment - 1) & ~(alignment - 1);
  966. return result;
  967. }
  968. /// Deallocate
  969. void mem_free(const void *ptr)
  970. {
  971. if (!ptr)
  972. return;
  973. std::lock_guard<std::mutex> lock(m_mutex);
  974. auto it = get_map_iterator(ptr);
  975. m_map.erase(it);
  976. }
  977. /// map: device pointer -> allocation(buffer, alloc_ptr, size)
  978. allocation translate_ptr(const void *ptr)
  979. {
  980. std::lock_guard<std::mutex> lock(m_mutex);
  981. auto it = get_map_iterator(ptr);
  982. return it->second;
  983. }
  984. /// Check if the pointer represents device pointer or not.
  985. bool is_device_ptr(const void *ptr) const
  986. {
  987. std::lock_guard<std::mutex> lock(m_mutex);
  988. return (mapped_address_space <= ptr) &&
  989. (ptr < mapped_address_space + mapped_region_size);
  990. }
  991. /// Returns the instance of memory manager singleton.
  992. static mem_mgr &instance()
  993. {
  994. static mem_mgr m;
  995. return m;
  996. }
  997. private:
  998. std::map<byte_t *, allocation> m_map;
  999. mutable std::mutex m_mutex;
  1000. byte_t *mapped_address_space;
  1001. byte_t *next_free;
  1002. const size_t mapped_region_size = 128ull * 1024 * 1024 * 1024;
  1003. const size_t alignment = 256;
  1004. /// This padding may be defined to some positive value to debug
  1005. /// out of bound accesses.
  1006. const size_t extra_padding = 0;
  1007. std::map<byte_t *, allocation>::iterator get_map_iterator(const void *ptr)
  1008. {
  1009. auto it = m_map.upper_bound((byte_t *)ptr);
  1010. if (it == m_map.end())
  1011. {
  1012. // Not a virtual pointer.
  1013. throw std::runtime_error("can not get buffer from non-virtual pointer");
  1014. }
  1015. const allocation &alloc = it->second;
  1016. if (ptr < alloc.alloc_ptr)
  1017. {
  1018. // Out of bound.
  1019. // This may happen if there's a gap between allocations due to alignment
  1020. // or extra padding and pointer points to this gap.
  1021. throw std::runtime_error("invalid virtual pointer");
  1022. }
  1023. return it;
  1024. }
  1025. };
  1026. template <class T, memory_region Memory, size_t Dimension>
  1027. class accessor;
  1028. template <memory_region Memory, class T = byte_t>
  1029. class memory_traits
  1030. {
  1031. public:
  1032. static constexpr sycl::access::target target =
  1033. sycl::access::target::device;
  1034. static constexpr sycl::access_mode mode =
  1035. (Memory == constant) ? sycl::access_mode::read
  1036. : sycl::access_mode::read_write;
  1037. static constexpr size_t type_size = sizeof(T);
  1038. using element_t =
  1039. typename std::conditional<Memory == constant, const T, T>::type;
  1040. using value_t = typename std::remove_cv<T>::type;
  1041. template <size_t Dimension = 1>
  1042. using accessor_t = typename std::conditional<
  1043. Memory == local, sycl::local_accessor<value_t, Dimension>,
  1044. sycl::accessor<T, Dimension, mode, target>>::type;
  1045. using pointer_t = T *;
  1046. };
  1047. static inline void *dpct_malloc(size_t size, sycl::queue &q)
  1048. {
  1049. #ifdef DPCT_USM_LEVEL_NONE
  1050. return mem_mgr::instance().mem_alloc(size * sizeof(byte_t));
  1051. #else
  1052. return sycl::malloc_device(size, q.get_device(), q.get_context());
  1053. #endif // DPCT_USM_LEVEL_NONE
  1054. }
  1055. #define PITCH_DEFAULT_ALIGN(x) (((x) + 31) & ~(0x1F))
  1056. static inline void *dpct_malloc(size_t &pitch, size_t x, size_t y, size_t z,
  1057. sycl::queue &q)
  1058. {
  1059. pitch = PITCH_DEFAULT_ALIGN(x);
  1060. return dpct_malloc(pitch * y * z, q);
  1061. }
  1062. /**
  1063. * @brief Sets \p value to the first \p size elements starting from \p dev_ptr in \p q.
  1064. * @tparam valueT The type of the element to be set.
  1065. * @param [in] q The queue in which the operation is done.
  1066. * @param [in] dev_ptr Pointer to the virtual device memory address.
  1067. * @param [in] value The value to be set.
  1068. * @param [in] size Number of elements to be set to the value.
  1069. * @return An event representing the memset operation.
  1070. */
  1071. template <typename valueT>
  1072. static inline sycl::event dpct_memset(sycl::queue &q, void *dev_ptr,
  1073. valueT value, size_t size)
  1074. {
  1075. #ifdef DPCT_USM_LEVEL_NONE
  1076. auto &mm = mem_mgr::instance();
  1077. assert(mm.is_device_ptr(dev_ptr));
  1078. auto alloc = mm.translate_ptr(dev_ptr);
  1079. size_t offset = (valueT *)dev_ptr - (valueT *)alloc.alloc_ptr;
  1080. return q.submit([&](sycl::handler &cgh)
  1081. {
  1082. auto r = sycl::range<1>(size);
  1083. auto o = sycl::id<1>(offset);
  1084. auto new_buffer = alloc.buffer.reinterpret<valueT>(
  1085. sycl::range<1>(alloc.size / sizeof(valueT)));
  1086. sycl::accessor<valueT, 1, sycl::access_mode::write,
  1087. sycl::access::target::device>
  1088. acc(new_buffer, cgh, r, o);
  1089. cgh.fill(acc, value); });
  1090. #else
  1091. return q.fill(dev_ptr, value, size);
  1092. #endif // DPCT_USM_LEVEL_NONE
  1093. }
  1094. /**
  1095. * @brief Sets \p value to the 3D memory region pointed by \p data in \p q.
  1096. * @tparam valueT The type of the element to be set.
  1097. * @param [in] q The queue in which the operation is done.
  1098. * @param [in] data Pointer to the pitched device memory region.
  1099. * @param [in] value The value to be set.
  1100. * @param [in] size 3D memory region by number of elements.
  1101. * @return An event list representing the memset operations.
  1102. */
  1103. template <typename valueT>
  1104. static inline std::vector<sycl::event>
  1105. dpct_memset(sycl::queue &q, pitched_data data, valueT value,
  1106. sycl::range<3> size)
  1107. {
  1108. std::vector<sycl::event> event_list;
  1109. size_t slice = data.get_pitch() * data.get_y();
  1110. unsigned char *data_surface = (unsigned char *)data.get_data_ptr();
  1111. for (size_t z = 0; z < size.get(2); ++z)
  1112. {
  1113. unsigned char *data_ptr = data_surface;
  1114. for (size_t y = 0; y < size.get(1); ++y)
  1115. {
  1116. event_list.push_back(dpct_memset(q, data_ptr, value, size.get(0)));
  1117. data_ptr += data.get_pitch();
  1118. }
  1119. data_surface += slice;
  1120. }
  1121. return event_list;
  1122. }
  1123. /**
  1124. * @brief Sets \p val to the pitched 2D memory region pointed by \p ptr in \p q.
  1125. * @tparam valueT The type of the element to be set.
  1126. * @param [in] q The queue in which the operation is done.
  1127. * @param [in] ptr Pointer to the virtual device memory.
  1128. * @param [in] pitch The pitch size by number of elements, including padding.
  1129. * @param [in] val The value to be set.
  1130. * @param [in] x The width of memory region by number of elements.
  1131. * @param [in] y The height of memory region by number of elements.
  1132. * @return An event list representing the memset operations.
  1133. */
  1134. template <typename valueT>
  1135. static inline std::vector<sycl::event>
  1136. dpct_memset(sycl::queue &q, void *ptr, size_t pitch, valueT val, size_t x,
  1137. size_t y)
  1138. {
  1139. return dpct_memset(q, pitched_data(ptr, pitch, x, 1), val,
  1140. sycl::range<3>(x, y, 1));
  1141. }
  1142. static memcpy_direction deduce_memcpy_direction(sycl::queue &q, void *to_ptr,
  1143. const void *from_ptr,
  1144. memcpy_direction dir)
  1145. {
  1146. switch (dir)
  1147. {
  1148. case memcpy_direction::host_to_host:
  1149. case memcpy_direction::host_to_device:
  1150. case memcpy_direction::device_to_host:
  1151. case memcpy_direction::device_to_device:
  1152. return dir;
  1153. case memcpy_direction::automatic:
  1154. {
  1155. // table[to_attribute][from_attribute]
  1156. static const memcpy_direction
  1157. direction_table[static_cast<unsigned>(pointer_access_attribute::end)]
  1158. [static_cast<unsigned>(pointer_access_attribute::end)] =
  1159. {{memcpy_direction::host_to_host,
  1160. memcpy_direction::device_to_host,
  1161. memcpy_direction::host_to_host},
  1162. {memcpy_direction::host_to_device,
  1163. memcpy_direction::device_to_device,
  1164. memcpy_direction::device_to_device},
  1165. {memcpy_direction::host_to_host,
  1166. memcpy_direction::device_to_device,
  1167. memcpy_direction::device_to_device}};
  1168. return direction_table[static_cast<unsigned>(get_pointer_attribute(
  1169. q, to_ptr))][static_cast<unsigned>(get_pointer_attribute(q, from_ptr))];
  1170. }
  1171. default:
  1172. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1173. }
  1174. }
  1175. static sycl::event
  1176. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1177. memcpy_direction direction,
  1178. const std::vector<sycl::event> &dep_events = {})
  1179. {
  1180. if (!size)
  1181. return sycl::event{};
  1182. #ifdef DPCT_USM_LEVEL_NONE
  1183. auto &mm = mem_mgr::instance();
  1184. auto real_direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1185. switch (real_direction)
  1186. {
  1187. case host_to_host:
  1188. return q.submit([&](sycl::handler &cgh)
  1189. {
  1190. cgh.depends_on(dep_events);
  1191. cgh.host_task([=] { std::memcpy(to_ptr, from_ptr, size); }); });
  1192. case host_to_device:
  1193. {
  1194. auto alloc = mm.translate_ptr(to_ptr);
  1195. size_t offset = (byte_t *)to_ptr - alloc.alloc_ptr;
  1196. return q.submit([&](sycl::handler &cgh)
  1197. {
  1198. cgh.depends_on(dep_events);
  1199. auto r = sycl::range<1>(size);
  1200. auto o = sycl::id<1>(offset);
  1201. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1202. sycl::access::target::device>
  1203. acc(alloc.buffer, cgh, r, o);
  1204. cgh.copy(from_ptr, acc); });
  1205. }
  1206. case device_to_host:
  1207. {
  1208. auto alloc = mm.translate_ptr(from_ptr);
  1209. size_t offset = (byte_t *)from_ptr - alloc.alloc_ptr;
  1210. return q.submit([&](sycl::handler &cgh)
  1211. {
  1212. cgh.depends_on(dep_events);
  1213. auto r = sycl::range<1>(size);
  1214. auto o = sycl::id<1>(offset);
  1215. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1216. sycl::access::target::device>
  1217. acc(alloc.buffer, cgh, r, o);
  1218. cgh.copy(acc, to_ptr); });
  1219. }
  1220. case device_to_device:
  1221. {
  1222. auto to_alloc = mm.translate_ptr(to_ptr);
  1223. auto from_alloc = mm.translate_ptr(from_ptr);
  1224. size_t to_offset = (byte_t *)to_ptr - to_alloc.alloc_ptr;
  1225. size_t from_offset = (byte_t *)from_ptr - from_alloc.alloc_ptr;
  1226. return q.submit([&](sycl::handler &cgh)
  1227. {
  1228. cgh.depends_on(dep_events);
  1229. auto r = sycl::range<1>(size);
  1230. auto to_o = sycl::id<1>(to_offset);
  1231. auto from_o = sycl::id<1>(from_offset);
  1232. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1233. sycl::access::target::device>
  1234. to_acc(to_alloc.buffer, cgh, r, to_o);
  1235. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1236. sycl::access::target::device>
  1237. from_acc(from_alloc.buffer, cgh, r, from_o);
  1238. cgh.copy(from_acc, to_acc); });
  1239. }
  1240. default:
  1241. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1242. }
  1243. #else
  1244. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  1245. #endif // DPCT_USM_LEVEL_NONE
  1246. }
  1247. // Get actual copy range and make sure it will not exceed range.
  1248. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  1249. size_t pitch)
  1250. {
  1251. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  1252. }
  1253. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  1254. size_t pitch)
  1255. {
  1256. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  1257. }
  1258. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  1259. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  1260. static inline std::vector<sycl::event>
  1261. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1262. sycl::range<3> to_range, sycl::range<3> from_range,
  1263. sycl::id<3> to_id, sycl::id<3> from_id,
  1264. sycl::range<3> size, memcpy_direction direction,
  1265. const std::vector<sycl::event> &dep_events = {})
  1266. {
  1267. // RAII for host pointer
  1268. class host_buffer
  1269. {
  1270. void *_buf;
  1271. size_t _size;
  1272. sycl::queue &_q;
  1273. const std::vector<sycl::event> &_deps; // free operation depends
  1274. public:
  1275. host_buffer(size_t size, sycl::queue &q,
  1276. const std::vector<sycl::event> &deps)
  1277. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  1278. void *get_ptr() const { return _buf; }
  1279. size_t get_size() const { return _size; }
  1280. ~host_buffer()
  1281. {
  1282. if (_buf)
  1283. {
  1284. _q.submit([&](sycl::handler &cgh)
  1285. {
  1286. cgh.depends_on(_deps);
  1287. cgh.host_task([buf = _buf] { std::free(buf); }); });
  1288. }
  1289. }
  1290. };
  1291. std::vector<sycl::event> event_list;
  1292. size_t to_slice = to_range.get(1) * to_range.get(0),
  1293. from_slice = from_range.get(1) * from_range.get(0);
  1294. unsigned char *to_surface =
  1295. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  1296. const unsigned char *from_surface =
  1297. (const unsigned char *)from_ptr +
  1298. get_offset(from_id, from_slice, from_range.get(0));
  1299. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  1300. {
  1301. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  1302. direction, dep_events)};
  1303. }
  1304. direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1305. size_t size_slice = size.get(1) * size.get(0);
  1306. switch (direction)
  1307. {
  1308. case host_to_host:
  1309. for (size_t z = 0; z < size.get(2); ++z)
  1310. {
  1311. unsigned char *to_ptr = to_surface;
  1312. const unsigned char *from_ptr = from_surface;
  1313. if (to_range.get(0) == from_range.get(0) &&
  1314. to_range.get(0) == size.get(0))
  1315. {
  1316. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  1317. direction, dep_events));
  1318. }
  1319. else
  1320. {
  1321. for (size_t y = 0; y < size.get(1); ++y)
  1322. {
  1323. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  1324. direction, dep_events));
  1325. to_ptr += to_range.get(0);
  1326. from_ptr += from_range.get(0);
  1327. }
  1328. }
  1329. to_surface += to_slice;
  1330. from_surface += from_slice;
  1331. }
  1332. break;
  1333. case host_to_device:
  1334. {
  1335. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  1336. event_list);
  1337. std::vector<sycl::event> host_events;
  1338. if (to_slice == size_slice)
  1339. {
  1340. // Copy host data to a temp host buffer with the shape of target.
  1341. host_events =
  1342. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  1343. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  1344. host_to_host, dep_events);
  1345. }
  1346. else
  1347. {
  1348. // Copy host data to a temp host buffer with the shape of target.
  1349. host_events = dpct_memcpy(
  1350. q, buf.get_ptr(), from_surface, to_range, from_range,
  1351. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  1352. // If has padding data, not sure whether it is useless. So fill temp
  1353. // buffer with it.
  1354. std::vector<sycl::event>{
  1355. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  1356. device_to_host, dep_events)});
  1357. }
  1358. // Copy from temp host buffer to device with only one submit.
  1359. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  1360. buf.get_size(), host_to_device,
  1361. host_events));
  1362. break;
  1363. }
  1364. case device_to_host:
  1365. {
  1366. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  1367. event_list);
  1368. // Copy from host temp buffer to host target with reshaping.
  1369. event_list = dpct_memcpy(
  1370. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  1371. sycl::id<3>(0, 0, 0), size, host_to_host,
  1372. // Copy from device to temp host buffer with only one submit.
  1373. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  1374. buf.get_size(),
  1375. device_to_host, dep_events)});
  1376. break;
  1377. }
  1378. case device_to_device:
  1379. #ifdef DPCT_USM_LEVEL_NONE
  1380. {
  1381. auto &mm = mem_mgr::instance();
  1382. auto to_alloc = mm.translate_ptr(to_surface);
  1383. auto from_alloc = mm.translate_ptr(from_surface);
  1384. size_t to_offset = (byte_t *)to_surface - to_alloc.alloc_ptr;
  1385. size_t from_offset = (byte_t *)from_surface - from_alloc.alloc_ptr;
  1386. event_list.push_back(q.submit([&](sycl::handler &cgh)
  1387. {
  1388. cgh.depends_on(dep_events);
  1389. auto to_o = sycl::id<1>(to_offset);
  1390. auto from_o = sycl::id<1>(from_offset);
  1391. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1392. sycl::access::target::device>
  1393. to_acc(to_alloc.buffer, cgh,
  1394. get_copy_range(size, to_slice, to_range.get(0)), to_o);
  1395. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1396. sycl::access::target::device>
  1397. from_acc(from_alloc.buffer, cgh,
  1398. get_copy_range(size, from_slice, from_range.get(0)), from_o);
  1399. cgh.parallel_for<class dpct_memcpy_3d_detail_usmnone>(
  1400. size,
  1401. [=](sycl::id<3> id) {
  1402. to_acc[get_offset(id, to_slice, to_range.get(0))] =
  1403. from_acc[get_offset(id, from_slice, from_range.get(0))];
  1404. }); }));
  1405. }
  1406. #else
  1407. event_list.push_back(q.submit([&](sycl::handler &cgh)
  1408. {
  1409. cgh.depends_on(dep_events);
  1410. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  1411. size,
  1412. [=](sycl::id<3> id) {
  1413. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  1414. from_surface[get_offset(id, from_slice, from_range.get(0))];
  1415. }); }));
  1416. #endif
  1417. break;
  1418. default:
  1419. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1420. }
  1421. return event_list;
  1422. }
  1423. /// memcpy 2D/3D matrix specified by pitched_data.
  1424. static inline std::vector<sycl::event>
  1425. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  1426. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  1427. memcpy_direction direction = automatic)
  1428. {
  1429. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  1430. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  1431. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  1432. size, direction);
  1433. }
  1434. /// memcpy 2D matrix with pitch.
  1435. static inline std::vector<sycl::event>
  1436. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1437. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  1438. memcpy_direction direction = automatic)
  1439. {
  1440. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  1441. sycl::range<3>(from_pitch, y, 1),
  1442. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  1443. sycl::range<3>(x, y, 1), direction);
  1444. }
  1445. namespace deprecated
  1446. {
  1447. template <typename T, sycl::usm::alloc AllocKind>
  1448. class usm_allocator
  1449. {
  1450. private:
  1451. using Alloc = sycl::usm_allocator<T, AllocKind>;
  1452. Alloc _impl;
  1453. public:
  1454. using value_type = typename std::allocator_traits<Alloc>::value_type;
  1455. using pointer = typename std::allocator_traits<Alloc>::pointer;
  1456. using const_pointer = typename std::allocator_traits<Alloc>::const_pointer;
  1457. using void_pointer = typename std::allocator_traits<Alloc>::void_pointer;
  1458. using const_void_pointer =
  1459. typename std::allocator_traits<Alloc>::const_void_pointer;
  1460. using reference = typename std::allocator_traits<Alloc>::value_type &;
  1461. using const_reference =
  1462. const typename std::allocator_traits<Alloc>::value_type &;
  1463. using difference_type =
  1464. typename std::allocator_traits<Alloc>::difference_type;
  1465. using size_type = typename std::allocator_traits<Alloc>::size_type;
  1466. using propagate_on_container_copy_assignment = typename std::allocator_traits<
  1467. Alloc>::propagate_on_container_copy_assignment;
  1468. using propagate_on_container_move_assignment = typename std::allocator_traits<
  1469. Alloc>::propagate_on_container_move_assignment;
  1470. using propagate_on_container_swap =
  1471. typename std::allocator_traits<Alloc>::propagate_on_container_swap;
  1472. using is_always_equal =
  1473. typename std::allocator_traits<Alloc>::is_always_equal;
  1474. template <typename U>
  1475. struct rebind
  1476. {
  1477. typedef usm_allocator<U, AllocKind> other;
  1478. };
  1479. usm_allocator() : _impl(dpct::get_default_queue()) {}
  1480. ~usm_allocator() {}
  1481. usm_allocator(const usm_allocator &other) : _impl(other._impl) {}
  1482. usm_allocator(usm_allocator &&other) : _impl(std::move(other._impl)) {}
  1483. pointer address(reference r) { return &r; }
  1484. const_pointer address(const_reference r) { return &r; }
  1485. pointer allocate(size_type cnt, const_void_pointer hint = nullptr)
  1486. {
  1487. return std::allocator_traits<Alloc>::allocate(_impl, cnt, hint);
  1488. }
  1489. void deallocate(pointer p, size_type cnt)
  1490. {
  1491. std::allocator_traits<Alloc>::deallocate(_impl, p, cnt);
  1492. }
  1493. size_type max_size() const
  1494. {
  1495. return std::allocator_traits<Alloc>::max_size(_impl);
  1496. }
  1497. bool operator==(const usm_allocator &other) const { return _impl == other._impl; }
  1498. bool operator!=(const usm_allocator &other) const { return _impl != other._impl; }
  1499. };
  1500. } // namespace deprecated
  1501. inline void dpct_free(void *ptr,
  1502. const sycl::queue &q)
  1503. {
  1504. if (ptr)
  1505. {
  1506. #ifdef DPCT_USM_LEVEL_NONE
  1507. detail::mem_mgr::instance().mem_free(ptr);
  1508. #else
  1509. sycl::free(ptr, q.get_context());
  1510. #endif // DPCT_USM_LEVEL_NONE
  1511. }
  1512. }
  1513. template <typename T>
  1514. inline auto get_memory(const void *x)
  1515. {
  1516. T *new_x = reinterpret_cast<T *>(const_cast<void *>(x));
  1517. #ifdef DPCT_USM_LEVEL_NONE
  1518. return dpct::get_buffer<std::remove_cv_t<T>>(new_x);
  1519. #else
  1520. return new_x;
  1521. #endif
  1522. }
  1523. template <typename T>
  1524. inline typename DataType<T>::T2 get_value(const T *s, sycl::queue &q)
  1525. {
  1526. using Ty = typename DataType<T>::T2;
  1527. Ty s_h;
  1528. if (get_pointer_attribute(q, s) == pointer_access_attribute::device_only)
  1529. detail::dpct_memcpy(q, (void *)&s_h, (void *)s, sizeof(T), device_to_host)
  1530. .wait();
  1531. else
  1532. s_h = *reinterpret_cast<const Ty *>(s);
  1533. return s_h;
  1534. }
  1535. } // namespace detail
  1536. template <typename T>
  1537. inline auto get_value(const T *s, sycl::queue &q)
  1538. {
  1539. return detail::get_value(s, q);
  1540. }
  1541. namespace detail
  1542. {
  1543. template <class Ta, class Tb, class Tc, class Ts>
  1544. inline void gemm_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1545. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1546. const void *alpha, const void *a, int lda, const void *b,
  1547. int ldb, const void *beta, void *c, int ldc)
  1548. {
  1549. #ifndef __INTEL_MKL__
  1550. throw std::runtime_error("The oneAPI Math Kernel Library (oneMKL) Interfaces "
  1551. "Project does not support this API.");
  1552. #else
  1553. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1554. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1555. auto data_a = get_memory<const Ta>(a);
  1556. auto data_b = get_memory<const Tb>(b);
  1557. auto data_c = get_memory<Tc>(c);
  1558. oneapi::mkl::blas::column_major::gemm(
  1559. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1560. data_b, ldb, beta_value, data_c, ldc);
  1561. #endif
  1562. }
  1563. template <typename VecT, class BinaryOperation, class = void>
  1564. class vectorized_binary
  1565. {
  1566. public:
  1567. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1568. {
  1569. VecT v4;
  1570. for (size_t i = 0; i < v4.size(); ++i)
  1571. {
  1572. v4[i] = binary_op(a[i], b[i]);
  1573. }
  1574. return v4;
  1575. }
  1576. };
  1577. template <typename VecT, class BinaryOperation>
  1578. class vectorized_binary<
  1579. VecT, BinaryOperation,
  1580. std::void_t<std::invoke_result_t<BinaryOperation, VecT, VecT>>>
  1581. {
  1582. public:
  1583. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1584. {
  1585. return binary_op(a, b).template as<VecT>();
  1586. }
  1587. };
  1588. template <class Ta, class Tb, class Tc, class Ts>
  1589. inline void gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1590. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1591. const void *alpha, const void **a, int lda,
  1592. const void **b, int ldb, const void *beta, void **c,
  1593. int ldc, int batch_size)
  1594. {
  1595. struct matrix_info_t
  1596. {
  1597. oneapi::mkl::transpose transpose_info[2];
  1598. Ts value_info[2];
  1599. std::int64_t size_info[3];
  1600. std::int64_t ld_info[3];
  1601. std::int64_t groupsize_info;
  1602. };
  1603. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1604. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1605. matrix_info_t *matrix_info =
  1606. (matrix_info_t *)std::malloc(sizeof(matrix_info_t));
  1607. matrix_info->transpose_info[0] = a_trans;
  1608. matrix_info->transpose_info[1] = b_trans;
  1609. matrix_info->value_info[0] = alpha_value;
  1610. matrix_info->value_info[1] = beta_value;
  1611. matrix_info->size_info[0] = m;
  1612. matrix_info->size_info[1] = n;
  1613. matrix_info->size_info[2] = k;
  1614. matrix_info->ld_info[0] = lda;
  1615. matrix_info->ld_info[1] = ldb;
  1616. matrix_info->ld_info[2] = ldc;
  1617. matrix_info->groupsize_info = batch_size;
  1618. sycl::event e = oneapi::mkl::blas::column_major::gemm_batch(
  1619. q, matrix_info->transpose_info, matrix_info->transpose_info + 1,
  1620. matrix_info->size_info, matrix_info->size_info + 1,
  1621. matrix_info->size_info + 2, matrix_info->value_info,
  1622. reinterpret_cast<const Ta **>(a), matrix_info->ld_info,
  1623. reinterpret_cast<const Tb **>(b), matrix_info->ld_info + 1,
  1624. matrix_info->value_info + 1, reinterpret_cast<Tc **>(c),
  1625. matrix_info->ld_info + 2, 1, &(matrix_info->groupsize_info));
  1626. q.submit([&](sycl::handler &cgh)
  1627. {
  1628. cgh.depends_on(e);
  1629. cgh.host_task([=] { std::free(matrix_info); }); });
  1630. }
  1631. template <class Ta, class Tb, class Tc, class Ts>
  1632. inline void
  1633. gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1634. oneapi::mkl::transpose b_trans, int m, int n,
  1635. int k, const void *alpha, const void *a, int lda,
  1636. long long int stride_a, const void *b, int ldb,
  1637. long long int stride_b, const void *beta, void *c,
  1638. int ldc, long long int stride_c, int batch_size)
  1639. {
  1640. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1641. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1642. auto data_a = get_memory<const Ta>(a);
  1643. auto data_b = get_memory<const Tb>(b);
  1644. auto data_c = get_memory<Tc>(c);
  1645. oneapi::mkl::blas::column_major::gemm_batch(
  1646. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1647. stride_a, data_b, ldb, stride_b, beta_value,
  1648. data_c, ldc, stride_c, batch_size);
  1649. }
  1650. } // namespace detail
  1651. template <typename VecT, class BinaryOperation>
  1652. inline unsigned vectorized_binary(unsigned a, unsigned b,
  1653. const BinaryOperation binary_op)
  1654. {
  1655. sycl::vec<unsigned, 1> v0{a}, v1{b};
  1656. auto v2 = v0.as<VecT>();
  1657. auto v3 = v1.as<VecT>();
  1658. auto v4 =
  1659. detail::vectorized_binary<VecT, BinaryOperation>()(v2, v3, binary_op);
  1660. v0 = v4.template as<sycl::vec<unsigned, 1>>();
  1661. return v0;
  1662. }
  1663. static void async_dpct_memcpy(void *to_ptr, const void *from_ptr, size_t size,
  1664. memcpy_direction direction = automatic,
  1665. sycl::queue &q = dpct::get_default_queue())
  1666. {
  1667. detail::dpct_memcpy(q, to_ptr, from_ptr, size, direction);
  1668. }
  1669. static inline unsigned int select_device(unsigned int id)
  1670. {
  1671. dev_mgr::instance().select_device(id);
  1672. return id;
  1673. }
  1674. template <typename T>
  1675. T permute_sub_group_by_xor(sycl::sub_group g, T x, unsigned int mask,
  1676. int logical_sub_group_size = 32)
  1677. {
  1678. unsigned int id = g.get_local_linear_id();
  1679. unsigned int start_index =
  1680. id / logical_sub_group_size * logical_sub_group_size;
  1681. unsigned int target_offset = (id % logical_sub_group_size) ^ mask;
  1682. return sycl::select_from_group(g, x,
  1683. target_offset < logical_sub_group_size
  1684. ? start_index + target_offset
  1685. : id);
  1686. }
  1687. template <typename T>
  1688. sycl::vec<T, 4> extract_and_sign_or_zero_extend4(T val)
  1689. {
  1690. return sycl::vec<T, 1>(val)
  1691. .template as<sycl::vec<
  1692. std::conditional_t<std::is_signed_v<T>, int8_t, uint8_t>, 4>>()
  1693. .template convert<T>();
  1694. }
  1695. template <typename T1, typename T2>
  1696. using dot_product_acc_t =
  1697. std::conditional_t<std::is_unsigned_v<T1> && std::is_unsigned_v<T2>,
  1698. uint32_t, int32_t>;
  1699. template <typename T1, typename T2, typename T3>
  1700. inline auto dp4a(T1 a, T2 b, T3 c)
  1701. {
  1702. dot_product_acc_t<T1, T2> res = c;
  1703. auto va = extract_and_sign_or_zero_extend4(a);
  1704. auto vb = extract_and_sign_or_zero_extend4(b);
  1705. res += va[0] * vb[0];
  1706. res += va[1] * vb[1];
  1707. res += va[2] * vb[2];
  1708. res += va[3] * vb[3];
  1709. return res;
  1710. }
  1711. struct sub_sat
  1712. {
  1713. template <typename T>
  1714. auto operator()(const T x, const T y) const
  1715. {
  1716. return sycl::sub_sat(x, y);
  1717. }
  1718. };
  1719. template <typename S, typename T>
  1720. inline T vectorized_min(T a, T b)
  1721. {
  1722. sycl::vec<T, 1> v0{a}, v1{b};
  1723. auto v2 = v0.template as<S>();
  1724. auto v3 = v1.template as<S>();
  1725. auto v4 = sycl::min(v2, v3);
  1726. v0 = v4.template as<sycl::vec<T, 1>>();
  1727. return v0;
  1728. }
  1729. inline float pow(const float a, const int b) { return sycl::pown(a, b); }
  1730. inline double pow(const double a, const int b) { return sycl::pown(a, b); }
  1731. inline float pow(const float a, const float b) { return sycl::pow(a, b); }
  1732. inline double pow(const double a, const double b) { return sycl::pow(a, b); }
  1733. template <typename T, typename U>
  1734. inline typename std::enable_if_t<std::is_floating_point_v<T>, T>
  1735. pow(const T a, const U b)
  1736. {
  1737. return sycl::pow(a, static_cast<T>(b));
  1738. }
  1739. template <typename T, typename U>
  1740. inline typename std::enable_if_t<!std::is_floating_point_v<T>, double>
  1741. pow(const T a, const U b)
  1742. {
  1743. return sycl::pow(static_cast<double>(a), static_cast<double>(b));
  1744. }
  1745. inline double min(const double a, const float b)
  1746. {
  1747. return sycl::fmin(a, static_cast<double>(b));
  1748. }
  1749. inline double min(const float a, const double b)
  1750. {
  1751. return sycl::fmin(static_cast<double>(a), b);
  1752. }
  1753. inline float min(const float a, const float b) { return sycl::fmin(a, b); }
  1754. inline double min(const double a, const double b) { return sycl::fmin(a, b); }
  1755. inline std::uint32_t min(const std::uint32_t a, const std::int32_t b)
  1756. {
  1757. return sycl::min(a, static_cast<std::uint32_t>(b));
  1758. }
  1759. inline std::uint32_t min(const std::int32_t a, const std::uint32_t b)
  1760. {
  1761. return sycl::min(static_cast<std::uint32_t>(a), b);
  1762. }
  1763. inline std::int32_t min(const std::int32_t a, const std::int32_t b)
  1764. {
  1765. return sycl::min(a, b);
  1766. }
  1767. inline std::uint32_t min(const std::uint32_t a, const std::uint32_t b)
  1768. {
  1769. return sycl::min(a, b);
  1770. }
  1771. inline std::uint64_t min(const std::uint64_t a, const std::int64_t b)
  1772. {
  1773. return sycl::min(a, static_cast<std::uint64_t>(b));
  1774. }
  1775. inline std::uint64_t min(const std::int64_t a, const std::uint64_t b)
  1776. {
  1777. return sycl::min(static_cast<std::uint64_t>(a), b);
  1778. }
  1779. inline std::int64_t min(const std::int64_t a, const std::int64_t b)
  1780. {
  1781. return sycl::min(a, b);
  1782. }
  1783. inline std::uint64_t min(const std::uint64_t a, const std::uint64_t b)
  1784. {
  1785. return sycl::min(a, b);
  1786. }
  1787. inline std::uint64_t min(const std::uint64_t a, const std::int32_t b)
  1788. {
  1789. return sycl::min(a, static_cast<std::uint64_t>(b));
  1790. }
  1791. inline std::uint64_t min(const std::int32_t a, const std::uint64_t b)
  1792. {
  1793. return sycl::min(static_cast<std::uint64_t>(a), b);
  1794. }
  1795. inline std::uint64_t min(const std::uint64_t a, const std::uint32_t b)
  1796. {
  1797. return sycl::min(a, static_cast<std::uint64_t>(b));
  1798. }
  1799. inline std::uint64_t min(const std::uint32_t a, const std::uint64_t b)
  1800. {
  1801. return sycl::min(static_cast<std::uint64_t>(a), b);
  1802. }
  1803. // max function overloads.
  1804. // For floating-point types, `float` or `double` arguments are acceptable.
  1805. // For integer types, `std::uint32_t`, `std::int32_t`, `std::uint64_t` or
  1806. // `std::int64_t` type arguments are acceptable.
  1807. inline double max(const double a, const float b)
  1808. {
  1809. return sycl::fmax(a, static_cast<double>(b));
  1810. }
  1811. inline double max(const float a, const double b)
  1812. {
  1813. return sycl::fmax(static_cast<double>(a), b);
  1814. }
  1815. inline float max(const float a, const float b) { return sycl::fmax(a, b); }
  1816. inline double max(const double a, const double b) { return sycl::fmax(a, b); }
  1817. inline std::uint32_t max(const std::uint32_t a, const std::int32_t b)
  1818. {
  1819. return sycl::max(a, static_cast<std::uint32_t>(b));
  1820. }
  1821. inline std::uint32_t max(const std::int32_t a, const std::uint32_t b)
  1822. {
  1823. return sycl::max(static_cast<std::uint32_t>(a), b);
  1824. }
  1825. inline std::int32_t max(const std::int32_t a, const std::int32_t b)
  1826. {
  1827. return sycl::max(a, b);
  1828. }
  1829. inline std::uint32_t max(const std::uint32_t a, const std::uint32_t b)
  1830. {
  1831. return sycl::max(a, b);
  1832. }
  1833. inline std::uint64_t max(const std::uint64_t a, const std::int64_t b)
  1834. {
  1835. return sycl::max(a, static_cast<std::uint64_t>(b));
  1836. }
  1837. inline std::uint64_t max(const std::int64_t a, const std::uint64_t b)
  1838. {
  1839. return sycl::max(static_cast<std::uint64_t>(a), b);
  1840. }
  1841. inline std::int64_t max(const std::int64_t a, const std::int64_t b)
  1842. {
  1843. return sycl::max(a, b);
  1844. }
  1845. inline std::uint64_t max(const std::uint64_t a, const std::uint64_t b)
  1846. {
  1847. return sycl::max(a, b);
  1848. }
  1849. inline std::uint64_t max(const std::uint64_t a, const std::int32_t b)
  1850. {
  1851. return sycl::max(a, static_cast<std::uint64_t>(b));
  1852. }
  1853. inline std::uint64_t max(const std::int32_t a, const std::uint64_t b)
  1854. {
  1855. return sycl::max(static_cast<std::uint64_t>(a), b);
  1856. }
  1857. inline std::uint64_t max(const std::uint64_t a, const std::uint32_t b)
  1858. {
  1859. return sycl::max(a, static_cast<std::uint64_t>(b));
  1860. }
  1861. inline std::uint64_t max(const std::uint32_t a, const std::uint64_t b)
  1862. {
  1863. return sycl::max(static_cast<std::uint64_t>(a), b);
  1864. }
  1865. inline void
  1866. has_capability_or_fail(const sycl::device &dev,
  1867. const std::initializer_list<sycl::aspect> &props)
  1868. {
  1869. for (const auto &it : props)
  1870. {
  1871. if (dev.has(it))
  1872. continue;
  1873. switch (it)
  1874. {
  1875. case sycl::aspect::fp64:
  1876. throw std::runtime_error("'double' is not supported in '" +
  1877. dev.get_info<sycl::info::device::name>() +
  1878. "' device");
  1879. break;
  1880. case sycl::aspect::fp16:
  1881. throw std::runtime_error("'half' is not supported in '" +
  1882. dev.get_info<sycl::info::device::name>() +
  1883. "' device");
  1884. break;
  1885. default:
  1886. #define __SYCL_ASPECT(ASPECT, ID) \
  1887. case sycl::aspect::ASPECT: \
  1888. return #ASPECT;
  1889. #define __SYCL_ASPECT_DEPRECATED(ASPECT, ID, MESSAGE) __SYCL_ASPECT(ASPECT, ID)
  1890. #define __SYCL_ASPECT_DEPRECATED_ALIAS(ASPECT, ID, MESSAGE)
  1891. auto getAspectNameStr = [](sycl::aspect AspectNum) -> std::string
  1892. {
  1893. switch (AspectNum)
  1894. {
  1895. #include <sycl/info/aspects.def>
  1896. #include <sycl/info/aspects_deprecated.def>
  1897. default:
  1898. return "unknown aspect";
  1899. }
  1900. };
  1901. #undef __SYCL_ASPECT_DEPRECATED_ALIAS
  1902. #undef __SYCL_ASPECT_DEPRECATED
  1903. #undef __SYCL_ASPECT
  1904. throw std::runtime_error(
  1905. "'" + getAspectNameStr(it) + "' is not supported in '" +
  1906. dev.get_info<sycl::info::device::name>() + "' device");
  1907. }
  1908. break;
  1909. }
  1910. }
  1911. static inline unsigned int get_current_device_id()
  1912. {
  1913. return dev_mgr::instance().current_device_id();
  1914. }
  1915. static inline device_ext &get_current_device()
  1916. {
  1917. return dev_mgr::instance().current_device();
  1918. }
  1919. static inline sycl::queue &get_in_order_queue()
  1920. {
  1921. return dev_mgr::instance().current_device().in_order_queue();
  1922. }
  1923. static sycl::event
  1924. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1925. memcpy_direction direction,
  1926. const std::vector<sycl::event> &dep_events = {})
  1927. {
  1928. if (!size)
  1929. return sycl::event{};
  1930. #ifdef DPCT_USM_LEVEL_NONE
  1931. auto &mm = mem_mgr::instance();
  1932. auto real_direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1933. switch (real_direction)
  1934. {
  1935. case host_to_host:
  1936. return q.submit([&](sycl::handler &cgh)
  1937. {
  1938. cgh.depends_on(dep_events);
  1939. cgh.host_task([=] { std::memcpy(to_ptr, from_ptr, size); }); });
  1940. case host_to_device:
  1941. {
  1942. auto alloc = mm.translate_ptr(to_ptr);
  1943. size_t offset = (byte_t *)to_ptr - alloc.alloc_ptr;
  1944. return q.submit([&](sycl::handler &cgh)
  1945. {
  1946. cgh.depends_on(dep_events);
  1947. auto r = sycl::range<1>(size);
  1948. auto o = sycl::id<1>(offset);
  1949. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1950. sycl::access::target::device>
  1951. acc(alloc.buffer, cgh, r, o);
  1952. cgh.copy(from_ptr, acc); });
  1953. }
  1954. case device_to_host:
  1955. {
  1956. auto alloc = mm.translate_ptr(from_ptr);
  1957. size_t offset = (byte_t *)from_ptr - alloc.alloc_ptr;
  1958. return q.submit([&](sycl::handler &cgh)
  1959. {
  1960. cgh.depends_on(dep_events);
  1961. auto r = sycl::range<1>(size);
  1962. auto o = sycl::id<1>(offset);
  1963. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1964. sycl::access::target::device>
  1965. acc(alloc.buffer, cgh, r, o);
  1966. cgh.copy(acc, to_ptr); });
  1967. }
  1968. case device_to_device:
  1969. {
  1970. auto to_alloc = mm.translate_ptr(to_ptr);
  1971. auto from_alloc = mm.translate_ptr(from_ptr);
  1972. size_t to_offset = (byte_t *)to_ptr - to_alloc.alloc_ptr;
  1973. size_t from_offset = (byte_t *)from_ptr - from_alloc.alloc_ptr;
  1974. return q.submit([&](sycl::handler &cgh)
  1975. {
  1976. cgh.depends_on(dep_events);
  1977. auto r = sycl::range<1>(size);
  1978. auto to_o = sycl::id<1>(to_offset);
  1979. auto from_o = sycl::id<1>(from_offset);
  1980. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1981. sycl::access::target::device>
  1982. to_acc(to_alloc.buffer, cgh, r, to_o);
  1983. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1984. sycl::access::target::device>
  1985. from_acc(from_alloc.buffer, cgh, r, from_o);
  1986. cgh.copy(from_acc, to_acc); });
  1987. }
  1988. default:
  1989. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1990. }
  1991. #else
  1992. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  1993. #endif // DPCT_USM_LEVEL_NONE
  1994. }
  1995. // Get actual copy range and make sure it will not exceed range.
  1996. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  1997. size_t pitch)
  1998. {
  1999. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  2000. }
  2001. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  2002. size_t pitch)
  2003. {
  2004. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  2005. }
  2006. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  2007. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  2008. static inline std::vector<sycl::event>
  2009. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  2010. sycl::range<3> to_range, sycl::range<3> from_range,
  2011. sycl::id<3> to_id, sycl::id<3> from_id,
  2012. sycl::range<3> size, memcpy_direction direction,
  2013. const std::vector<sycl::event> &dep_events = {})
  2014. {
  2015. // RAII for host pointer
  2016. class host_buffer
  2017. {
  2018. void *_buf;
  2019. size_t _size;
  2020. sycl::queue &_q;
  2021. const std::vector<sycl::event> &_deps; // free operation depends
  2022. public:
  2023. host_buffer(size_t size, sycl::queue &q,
  2024. const std::vector<sycl::event> &deps)
  2025. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  2026. void *get_ptr() const { return _buf; }
  2027. size_t get_size() const { return _size; }
  2028. ~host_buffer()
  2029. {
  2030. if (_buf)
  2031. {
  2032. _q.submit([&](sycl::handler &cgh)
  2033. {
  2034. cgh.depends_on(_deps);
  2035. cgh.host_task([buf = _buf] { std::free(buf); }); });
  2036. }
  2037. }
  2038. };
  2039. std::vector<sycl::event> event_list;
  2040. size_t to_slice = to_range.get(1) * to_range.get(0),
  2041. from_slice = from_range.get(1) * from_range.get(0);
  2042. unsigned char *to_surface =
  2043. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  2044. const unsigned char *from_surface =
  2045. (const unsigned char *)from_ptr +
  2046. get_offset(from_id, from_slice, from_range.get(0));
  2047. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  2048. {
  2049. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  2050. direction, dep_events)};
  2051. }
  2052. direction = detail::deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  2053. size_t size_slice = size.get(1) * size.get(0);
  2054. switch (direction)
  2055. {
  2056. case host_to_host:
  2057. for (size_t z = 0; z < size.get(2); ++z)
  2058. {
  2059. unsigned char *to_ptr = to_surface;
  2060. const unsigned char *from_ptr = from_surface;
  2061. if (to_range.get(0) == from_range.get(0) &&
  2062. to_range.get(0) == size.get(0))
  2063. {
  2064. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  2065. direction, dep_events));
  2066. }
  2067. else
  2068. {
  2069. for (size_t y = 0; y < size.get(1); ++y)
  2070. {
  2071. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  2072. direction, dep_events));
  2073. to_ptr += to_range.get(0);
  2074. from_ptr += from_range.get(0);
  2075. }
  2076. }
  2077. to_surface += to_slice;
  2078. from_surface += from_slice;
  2079. }
  2080. break;
  2081. case host_to_device:
  2082. {
  2083. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  2084. event_list);
  2085. std::vector<sycl::event> host_events;
  2086. if (to_slice == size_slice)
  2087. {
  2088. // Copy host data to a temp host buffer with the shape of target.
  2089. host_events =
  2090. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  2091. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  2092. host_to_host, dep_events);
  2093. }
  2094. else
  2095. {
  2096. // Copy host data to a temp host buffer with the shape of target.
  2097. host_events = dpct_memcpy(
  2098. q, buf.get_ptr(), from_surface, to_range, from_range,
  2099. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  2100. // If has padding data, not sure whether it is useless. So fill temp
  2101. // buffer with it.
  2102. std::vector<sycl::event>{
  2103. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  2104. device_to_host, dep_events)});
  2105. }
  2106. // Copy from temp host buffer to device with only one submit.
  2107. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  2108. buf.get_size(), host_to_device,
  2109. host_events));
  2110. break;
  2111. }
  2112. case device_to_host:
  2113. {
  2114. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  2115. event_list);
  2116. // Copy from host temp buffer to host target with reshaping.
  2117. event_list = dpct_memcpy(
  2118. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  2119. sycl::id<3>(0, 0, 0), size, host_to_host,
  2120. // Copy from device to temp host buffer with only one submit.
  2121. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  2122. buf.get_size(),
  2123. device_to_host, dep_events)});
  2124. break;
  2125. }
  2126. case device_to_device:
  2127. #ifdef DPCT_USM_LEVEL_NONE
  2128. {
  2129. auto &mm = mem_mgr::instance();
  2130. auto to_alloc = mm.translate_ptr(to_surface);
  2131. auto from_alloc = mm.translate_ptr(from_surface);
  2132. size_t to_offset = (byte_t *)to_surface - to_alloc.alloc_ptr;
  2133. size_t from_offset = (byte_t *)from_surface - from_alloc.alloc_ptr;
  2134. event_list.push_back(q.submit([&](sycl::handler &cgh)
  2135. {
  2136. cgh.depends_on(dep_events);
  2137. auto to_o = sycl::id<1>(to_offset);
  2138. auto from_o = sycl::id<1>(from_offset);
  2139. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  2140. sycl::access::target::device>
  2141. to_acc(to_alloc.buffer, cgh,
  2142. get_copy_range(size, to_slice, to_range.get(0)), to_o);
  2143. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  2144. sycl::access::target::device>
  2145. from_acc(from_alloc.buffer, cgh,
  2146. get_copy_range(size, from_slice, from_range.get(0)), from_o);
  2147. cgh.parallel_for<class dpct_memcpy_3d_detail_usmnone>(
  2148. size,
  2149. [=](sycl::id<3> id) {
  2150. to_acc[get_offset(id, to_slice, to_range.get(0))] =
  2151. from_acc[get_offset(id, from_slice, from_range.get(0))];
  2152. }); }));
  2153. }
  2154. #else
  2155. event_list.push_back(q.submit([&](sycl::handler &cgh)
  2156. {
  2157. cgh.depends_on(dep_events);
  2158. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  2159. size,
  2160. [=](sycl::id<3> id) {
  2161. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  2162. from_surface[get_offset(id, from_slice, from_range.get(0))];
  2163. }); }));
  2164. #endif
  2165. break;
  2166. default:
  2167. throw std::runtime_error("dpct_memcpy: invalid direction value");
  2168. }
  2169. return event_list;
  2170. }
  2171. /// memcpy 2D/3D matrix specified by pitched_data.
  2172. static inline std::vector<sycl::event>
  2173. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  2174. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  2175. memcpy_direction direction = automatic)
  2176. {
  2177. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  2178. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  2179. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  2180. size, direction);
  2181. }
  2182. /// memcpy 2D matrix with pitch.
  2183. static inline std::vector<sycl::event>
  2184. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  2185. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  2186. memcpy_direction direction = automatic)
  2187. {
  2188. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  2189. sycl::range<3>(from_pitch, y, 1),
  2190. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  2191. sycl::range<3>(x, y, 1), direction);
  2192. }
  2193. inline void gemm(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2194. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2195. const void *alpha, const void *a, library_data_t a_type,
  2196. int lda, const void *b, library_data_t b_type, int ldb,
  2197. const void *beta, void *c, library_data_t c_type, int ldc,
  2198. library_data_t scaling_type)
  2199. {
  2200. if (scaling_type == library_data_t::real_float &&
  2201. c_type == library_data_t::complex_float)
  2202. {
  2203. scaling_type = library_data_t::complex_float;
  2204. }
  2205. else if (scaling_type == library_data_t::real_double &&
  2206. c_type == library_data_t::complex_double)
  2207. {
  2208. scaling_type = library_data_t::complex_double;
  2209. }
  2210. std::uint64_t key =
  2211. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2212. switch (key)
  2213. {
  2214. case detail::get_type_combination_id(
  2215. library_data_t::real_float, library_data_t::real_float,
  2216. library_data_t::real_float, library_data_t::real_float):
  2217. {
  2218. detail::gemm_impl<float, float, float, float>(
  2219. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2220. break;
  2221. }
  2222. case detail::get_type_combination_id(
  2223. library_data_t::real_double, library_data_t::real_double,
  2224. library_data_t::real_double, library_data_t::real_double):
  2225. {
  2226. detail::gemm_impl<double, double, double, double>(
  2227. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2228. break;
  2229. }
  2230. case detail::get_type_combination_id(
  2231. library_data_t::complex_float, library_data_t::complex_float,
  2232. library_data_t::complex_float, library_data_t::complex_float):
  2233. {
  2234. detail::gemm_impl<std::complex<float>, std::complex<float>,
  2235. std::complex<float>, std::complex<float>>(
  2236. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2237. break;
  2238. }
  2239. case detail::get_type_combination_id(
  2240. library_data_t::complex_double, library_data_t::complex_double,
  2241. library_data_t::complex_double, library_data_t::complex_double):
  2242. {
  2243. detail::gemm_impl<std::complex<double>, std::complex<double>,
  2244. std::complex<double>, std::complex<double>>(
  2245. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2246. break;
  2247. }
  2248. case detail::get_type_combination_id(
  2249. library_data_t::real_half, library_data_t::real_half,
  2250. library_data_t::real_half, library_data_t::real_half):
  2251. {
  2252. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2253. sycl::half>(q, a_trans, b_trans, m, n, k, alpha, a,
  2254. lda, b, ldb, beta, c, ldc);
  2255. break;
  2256. }
  2257. case detail::get_type_combination_id(
  2258. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2259. library_data_t::real_float, library_data_t::real_float):
  2260. {
  2261. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2262. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda, b,
  2263. ldb, beta, c, ldc);
  2264. break;
  2265. }
  2266. case detail::get_type_combination_id(
  2267. library_data_t::real_half, library_data_t::real_half,
  2268. library_data_t::real_float, library_data_t::real_float):
  2269. {
  2270. detail::gemm_impl<sycl::half, sycl::half, float, float>(
  2271. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2272. break;
  2273. }
  2274. case detail::get_type_combination_id(
  2275. library_data_t::real_half, library_data_t::real_half,
  2276. library_data_t::real_half, library_data_t::real_float):
  2277. {
  2278. float alpha_value =
  2279. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2280. float beta_value =
  2281. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2282. sycl::half alpha_half(alpha_value);
  2283. sycl::half beta_half(beta_value);
  2284. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2285. sycl::half>(q, a_trans, b_trans, m, n, k, &alpha_half,
  2286. a, lda, b, ldb, &beta_half, c, ldc);
  2287. break;
  2288. }
  2289. case detail::get_type_combination_id(
  2290. library_data_t::real_int8, library_data_t::real_int8,
  2291. library_data_t::real_float, library_data_t::real_float):
  2292. {
  2293. detail::gemm_impl<std::int8_t, std::int8_t, float, float>(
  2294. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2295. break;
  2296. }
  2297. case detail::get_type_combination_id(
  2298. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2299. library_data_t::real_bfloat16, library_data_t::real_float):
  2300. {
  2301. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2302. oneapi::mkl::bfloat16, float>(
  2303. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2304. break;
  2305. }
  2306. case detail::get_type_combination_id(
  2307. library_data_t::real_int8, library_data_t::real_int8,
  2308. library_data_t::real_int32, library_data_t::real_int32):
  2309. {
  2310. float alpha_float =
  2311. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2312. float beta_float =
  2313. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2314. detail::gemm_impl<std::int8_t, std::int8_t, std::int32_t, float>(
  2315. q, a_trans, b_trans, m, n, k, &alpha_float, a, lda, b, ldb, &beta_float, c, ldc);
  2316. break;
  2317. }
  2318. default:
  2319. throw std::runtime_error("the combination of data type is unsupported");
  2320. }
  2321. } // gemm()
  2322. /// Computes a batch of matrix-matrix product with general matrices.
  2323. /// \param [in] q The queue where the routine should be executed.
  2324. /// \param [in] a_trans Specifies the operation applied to A.
  2325. /// \param [in] b_trans Specifies the operation applied to B.
  2326. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2327. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2328. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2329. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2330. /// \param [in] a Input matrix A.
  2331. /// \param [in] a_type Data type of the matrix A.
  2332. /// \param [in] lda Leading dimension of A.
  2333. /// \param [in] b Input matrix B.
  2334. /// \param [in] b_type Data type of the matrix B.
  2335. /// \param [in] ldb Leading dimension of B.
  2336. /// \param [in] beta Scaling factor for matrix C.
  2337. /// \param [in, out] c Input/Output matrix C.
  2338. /// \param [in] c_type Data type of the matrix C.
  2339. /// \param [in] ldc Leading dimension of C.
  2340. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2341. /// \param [in] scaling_type Data type of the scaling factors.
  2342. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2343. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2344. const void *alpha, const void *a[],
  2345. library_data_t a_type, int lda, const void *b[],
  2346. library_data_t b_type, int ldb, const void *beta,
  2347. void *c[], library_data_t c_type, int ldc,
  2348. int batch_size, library_data_t scaling_type)
  2349. {
  2350. #ifdef DPCT_USM_LEVEL_NONE
  2351. throw std::runtime_error("this API is unsupported when USM level is none");
  2352. #else
  2353. if (scaling_type == library_data_t::real_float &&
  2354. c_type == library_data_t::complex_float)
  2355. {
  2356. scaling_type = library_data_t::complex_float;
  2357. }
  2358. else if (scaling_type == library_data_t::real_double &&
  2359. c_type == library_data_t::complex_double)
  2360. {
  2361. scaling_type = library_data_t::complex_double;
  2362. }
  2363. std::uint64_t key =
  2364. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2365. switch (key)
  2366. {
  2367. case detail::get_type_combination_id(
  2368. library_data_t::real_float, library_data_t::real_float,
  2369. library_data_t::real_float, library_data_t::real_float):
  2370. {
  2371. detail::gemm_batch_impl<float, float, float, float>(
  2372. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2373. batch_size);
  2374. break;
  2375. }
  2376. case detail::get_type_combination_id(
  2377. library_data_t::real_double, library_data_t::real_double,
  2378. library_data_t::real_double, library_data_t::real_double):
  2379. {
  2380. detail::gemm_batch_impl<double, double, double, double>(
  2381. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2382. batch_size);
  2383. break;
  2384. }
  2385. case detail::get_type_combination_id(
  2386. library_data_t::complex_float, library_data_t::complex_float,
  2387. library_data_t::complex_float, library_data_t::complex_float):
  2388. {
  2389. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2390. std::complex<float>, std::complex<float>>(
  2391. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2392. batch_size);
  2393. break;
  2394. }
  2395. case detail::get_type_combination_id(
  2396. library_data_t::complex_double, library_data_t::complex_double,
  2397. library_data_t::complex_double, library_data_t::complex_double):
  2398. {
  2399. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2400. std::complex<double>, std::complex<double>>(
  2401. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2402. batch_size);
  2403. break;
  2404. }
  2405. case detail::get_type_combination_id(
  2406. library_data_t::real_half, library_data_t::real_half,
  2407. library_data_t::real_half, library_data_t::real_half):
  2408. {
  2409. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2410. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2411. a, lda, b, ldb, beta, c, ldc,
  2412. batch_size);
  2413. break;
  2414. }
  2415. #ifdef __INTEL_MKL__
  2416. case detail::get_type_combination_id(
  2417. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2418. library_data_t::real_bfloat16, library_data_t::real_float):
  2419. {
  2420. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2421. oneapi::mkl::bfloat16, float>(
  2422. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2423. batch_size);
  2424. break;
  2425. }
  2426. case detail::get_type_combination_id(
  2427. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2428. library_data_t::real_float, library_data_t::real_float):
  2429. {
  2430. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2431. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2432. b, ldb, beta, c, ldc, batch_size);
  2433. break;
  2434. }
  2435. case detail::get_type_combination_id(
  2436. library_data_t::real_int8, library_data_t::real_int8,
  2437. library_data_t::real_int32, library_data_t::real_int32):
  2438. {
  2439. float alpha_float =
  2440. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2441. float beta_float =
  2442. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2443. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2444. float>(q, a_trans, b_trans, m, n, k, &alpha_float,
  2445. a, lda, b, ldb, &beta_float, c, ldc,
  2446. batch_size);
  2447. break;
  2448. }
  2449. case detail::get_type_combination_id(
  2450. library_data_t::real_int8, library_data_t::real_int8,
  2451. library_data_t::real_float, library_data_t::real_float):
  2452. {
  2453. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2454. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2455. batch_size);
  2456. break;
  2457. }
  2458. case detail::get_type_combination_id(
  2459. library_data_t::real_half, library_data_t::real_half,
  2460. library_data_t::real_float, library_data_t::real_float):
  2461. {
  2462. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2463. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2464. batch_size);
  2465. break;
  2466. }
  2467. #endif
  2468. case detail::get_type_combination_id(
  2469. library_data_t::real_half, library_data_t::real_half,
  2470. library_data_t::real_half, library_data_t::real_float):
  2471. {
  2472. float alpha_value =
  2473. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2474. float beta_value =
  2475. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2476. sycl::half alpha_half(alpha_value);
  2477. sycl::half beta_half(beta_value);
  2478. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2479. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, b, ldb, &beta_half, c, ldc,
  2480. batch_size);
  2481. break;
  2482. }
  2483. default:
  2484. throw std::runtime_error("the combination of data type is unsupported");
  2485. }
  2486. #endif
  2487. }
  2488. /// Computes a batch of matrix-matrix product with general matrices.
  2489. /// \param [in] q The queue where the routine should be executed.
  2490. /// \param [in] a_trans Specifies the operation applied to A.
  2491. /// \param [in] b_trans Specifies the operation applied to B.
  2492. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2493. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2494. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2495. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2496. /// \param [in] a Input matrix A.
  2497. /// \param [in] a_type Data type of the matrix A.
  2498. /// \param [in] lda Leading dimension of A.
  2499. /// \param [in] stride_a Stride between the different A matrices.
  2500. /// \param [in] b Input matrix B.
  2501. /// \param [in] b_type Data type of the matrix B.
  2502. /// \param [in] ldb Leading dimension of B.
  2503. /// \param [in] stride_b Stride between the different B matrices.
  2504. /// \param [in] beta Scaling factor for matrix C.
  2505. /// \param [in, out] c Input/Output matrix C.
  2506. /// \param [in] c_type Data type of the matrix C.
  2507. /// \param [in] ldc Leading dimension of C.
  2508. /// \param [in] stride_c Stride between the different C matrices.
  2509. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2510. /// \param [in] scaling_type Data type of the scaling factors.
  2511. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2512. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2513. const void *alpha, const void *a, library_data_t a_type,
  2514. int lda, long long int stride_a, const void *b,
  2515. library_data_t b_type, int ldb, long long int stride_b,
  2516. const void *beta, void *c, library_data_t c_type,
  2517. int ldc, long long int stride_c, int batch_size,
  2518. library_data_t scaling_type)
  2519. {
  2520. if (scaling_type == library_data_t::real_float &&
  2521. c_type == library_data_t::complex_float)
  2522. {
  2523. scaling_type = library_data_t::complex_float;
  2524. }
  2525. else if (scaling_type == library_data_t::real_double &&
  2526. c_type == library_data_t::complex_double)
  2527. {
  2528. scaling_type = library_data_t::complex_double;
  2529. }
  2530. std::uint64_t key =
  2531. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2532. switch (key)
  2533. {
  2534. case detail::get_type_combination_id(
  2535. library_data_t::real_float, library_data_t::real_float,
  2536. library_data_t::real_float, library_data_t::real_float):
  2537. {
  2538. detail::gemm_batch_impl<float, float, float, float>(
  2539. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2540. beta, c, ldc, stride_c, batch_size);
  2541. break;
  2542. }
  2543. case detail::get_type_combination_id(
  2544. library_data_t::real_double, library_data_t::real_double,
  2545. library_data_t::real_double, library_data_t::real_double):
  2546. {
  2547. detail::gemm_batch_impl<double, double, double, double>(
  2548. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2549. beta, c, ldc, stride_c, batch_size);
  2550. break;
  2551. }
  2552. case detail::get_type_combination_id(
  2553. library_data_t::complex_float, library_data_t::complex_float,
  2554. library_data_t::complex_float, library_data_t::complex_float):
  2555. {
  2556. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2557. std::complex<float>, std::complex<float>>(
  2558. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2559. beta, c, ldc, stride_c, batch_size);
  2560. break;
  2561. }
  2562. case detail::get_type_combination_id(
  2563. library_data_t::complex_double, library_data_t::complex_double,
  2564. library_data_t::complex_double, library_data_t::complex_double):
  2565. {
  2566. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2567. std::complex<double>, std::complex<double>>(
  2568. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2569. beta, c, ldc, stride_c, batch_size);
  2570. break;
  2571. }
  2572. case detail::get_type_combination_id(
  2573. library_data_t::real_half, library_data_t::real_half,
  2574. library_data_t::real_half, library_data_t::real_half):
  2575. {
  2576. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2577. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2578. a, lda, stride_a, b, ldb, stride_b,
  2579. beta, c, ldc, stride_c, batch_size);
  2580. break;
  2581. }
  2582. #ifdef __INTEL_MKL__
  2583. case detail::get_type_combination_id(
  2584. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2585. library_data_t::real_bfloat16, library_data_t::real_float):
  2586. {
  2587. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2588. oneapi::mkl::bfloat16, float>(
  2589. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2590. beta, c, ldc, stride_c, batch_size);
  2591. break;
  2592. }
  2593. case detail::get_type_combination_id(
  2594. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2595. library_data_t::real_float, library_data_t::real_float):
  2596. {
  2597. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2598. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2599. stride_a, b, ldb, stride_b, beta, c, ldc,
  2600. stride_c, batch_size);
  2601. break;
  2602. }
  2603. case detail::get_type_combination_id(
  2604. library_data_t::real_int8, library_data_t::real_int8,
  2605. library_data_t::real_int32, library_data_t::real_int32):
  2606. {
  2607. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2608. std::int32_t>(q, a_trans, b_trans, m, n, k, alpha,
  2609. a, lda, stride_a, b, ldb, stride_b,
  2610. beta, c, ldc, stride_c, batch_size);
  2611. break;
  2612. }
  2613. case detail::get_type_combination_id(
  2614. library_data_t::real_int8, library_data_t::real_int8,
  2615. library_data_t::real_float, library_data_t::real_float):
  2616. {
  2617. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2618. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2619. beta, c, ldc, stride_c, batch_size);
  2620. break;
  2621. }
  2622. case detail::get_type_combination_id(
  2623. library_data_t::real_half, library_data_t::real_half,
  2624. library_data_t::real_float, library_data_t::real_float):
  2625. {
  2626. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2627. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2628. beta, c, ldc, stride_c, batch_size);
  2629. break;
  2630. }
  2631. #endif
  2632. case detail::get_type_combination_id(
  2633. library_data_t::real_half, library_data_t::real_half,
  2634. library_data_t::real_half, library_data_t::real_float):
  2635. {
  2636. float alpha_value =
  2637. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2638. float beta_value =
  2639. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2640. sycl::half alpha_half(alpha_value);
  2641. sycl::half beta_half(beta_value);
  2642. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2643. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, stride_a, b, ldb, stride_b,
  2644. &beta_half, c, ldc, stride_c, batch_size);
  2645. break;
  2646. }
  2647. default:
  2648. throw std::runtime_error("the combination of data type is unsupported");
  2649. }
  2650. }
  2651. static inline void
  2652. async_dpct_memcpy(void *to_ptr, size_t to_pitch, const void *from_ptr,
  2653. size_t from_pitch, size_t x, size_t y,
  2654. memcpy_direction direction = automatic,
  2655. sycl::queue &q = get_default_queue())
  2656. {
  2657. detail::dpct_memcpy(q, to_ptr, from_ptr, to_pitch, from_pitch, x, y,
  2658. direction);
  2659. }
  2660. using err0 = detail::generic_error_type<struct err0_tag, int>;
  2661. using err1 = detail::generic_error_type<struct err1_tag, int>;
  2662. } // COPY from DPCT head files
  2663. static int g_ggml_sycl_debug=0;
  2664. #define GGML_SYCL_DEBUG(...) do{if(g_ggml_sycl_debug) printf(__VA_ARGS__);}while(0)
  2665. #define CHECK_TRY_ERROR(expr) \
  2666. [&]() { \
  2667. try { \
  2668. expr; \
  2669. return dpct::success; \
  2670. } catch (std::exception const &e) { \
  2671. std::cerr << e.what()<< "\nException caught at file:" << __FILE__ \
  2672. << ", line:" << __LINE__ <<", func:"<<__func__<< std::endl; \
  2673. return dpct::default_error; \
  2674. } \
  2675. }()
  2676. // #define DEBUG_SYCL_MALLOC
  2677. static int g_work_group_size = 0;
  2678. // typedef sycl::half ggml_fp16_t;
  2679. #define __SYCL_ARCH__ DPCT_COMPATIBILITY_TEMP
  2680. #define VER_4VEC 610 //todo for hardward optimize.
  2681. #define VER_GEN9 700 //todo for hardward optimize.
  2682. #define VER_GEN12 1000000 //todo for hardward optimize.
  2683. #define VER_GEN13 (VER_GEN12 + 1030) //todo for hardward optimize.
  2684. #define GGML_SYCL_MAX_NODES 8192 //TODO: adapt to hardwares
  2685. //define for XMX in Intel GPU
  2686. //TODO: currently, it's not used for XMX really.
  2687. #define SYCL_USE_XMX
  2688. // max batch size to use MMQ kernels when tensor cores are available
  2689. #define XMX_MAX_BATCH_SIZE 32
  2690. #if defined(_MSC_VER)
  2691. #pragma warning(disable: 4244 4267) // possible loss of data
  2692. #endif
  2693. static_assert(sizeof(sycl::half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  2694. static void crash(){
  2695. int *ptr = NULL;
  2696. *ptr = 0;
  2697. }
  2698. static void ggml_sycl_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  2699. fprintf(stderr, "SYCL error: %s: %s\n", stmt, msg);
  2700. fprintf(stderr, " in function %s at %s:%d\n", func, file, line);
  2701. GGML_ASSERT(!"SYCL error");
  2702. }
  2703. #define SYCL_CHECK(err) do { \
  2704. auto err_ = (err); if (err_ != 0) ggml_sycl_error( \
  2705. #err, __func__, __FILE__, __LINE__, \
  2706. "Meet error in this line code!"); \
  2707. } while (0)
  2708. #if DPCT_COMPAT_RT_VERSION >= 11100
  2709. #define GGML_SYCL_ASSUME(x) __builtin_assume(x)
  2710. #else
  2711. #define GGML_SYCL_ASSUME(x)
  2712. #endif // DPCT_COMPAT_RT_VERSION >= 11100
  2713. #ifdef GGML_SYCL_F16
  2714. typedef sycl::half dfloat; // dequantize float
  2715. typedef sycl::half2 dfloat2;
  2716. #else
  2717. typedef float dfloat; // dequantize float
  2718. typedef sycl::float2 dfloat2;
  2719. #endif //GGML_SYCL_F16
  2720. bool ggml_sycl_loaded(void);
  2721. void * ggml_sycl_host_malloc(size_t size);
  2722. void ggml_sycl_host_free(void * ptr);
  2723. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
  2724. void ggml_sycl_set_tensor_split(const float * tensor_split);
  2725. void ggml_sycl_transform_tensor(void * data, struct ggml_tensor * tensor);
  2726. void ggml_sycl_free_data(struct ggml_tensor * tensor);
  2727. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor);
  2728. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor);
  2729. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor);
  2730. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor);
  2731. void ggml_sycl_assign_scratch_offset(struct ggml_tensor * tensor, size_t offset);
  2732. void ggml_sycl_copy_to_device(struct ggml_tensor * tensor);
  2733. void ggml_sycl_set_main_device(int main_device);
  2734. void ggml_sycl_set_mul_mat_q(bool mul_mat_q);
  2735. void ggml_sycl_set_scratch_size(size_t scratch_size);
  2736. void ggml_sycl_free_scratch(void);
  2737. int ggml_sycl_get_device_count(void);
  2738. void ggml_sycl_get_device_description(int device, char * description, size_t description_size);
  2739. bool ggml_backend_is_sycl(ggml_backend_t backend);
  2740. int ggml_backend_sycl_get_device(ggml_backend_t backend);
  2741. int get_main_device();
  2742. void print_ggml_tensor(const char*name, struct ggml_tensor *src);
  2743. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt);
  2744. static __dpct_inline__ int get_int_from_int8(const int8_t *x8, const int &i32) {
  2745. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2746. int x32 = 0;
  2747. x32 |= x16[0] << 0;
  2748. x32 |= x16[1] << 16;
  2749. return x32;
  2750. }
  2751. static __dpct_inline__ int get_int_from_uint8(const uint8_t *x8,
  2752. const int &i32) {
  2753. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2754. int x32 = 0;
  2755. x32 |= x16[0] << 0;
  2756. x32 |= x16[1] << 16;
  2757. return x32;
  2758. }
  2759. static __dpct_inline__ int get_int_from_int8_aligned(const int8_t *x8,
  2760. const int &i32) {
  2761. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2762. }
  2763. static __dpct_inline__ int get_int_from_uint8_aligned(const uint8_t *x8,
  2764. const int &i32) {
  2765. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2766. }
  2767. template <typename T>
  2768. using to_t_sycl_t = void (*)(const void *__restrict__ x, T *__restrict__ y,
  2769. int k, dpct::queue_ptr stream);
  2770. typedef to_t_sycl_t<float> to_fp32_sycl_t;
  2771. typedef to_t_sycl_t<sycl::half> to_fp16_sycl_t;
  2772. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  2773. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  2774. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  2775. typedef void (*ggml_sycl_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  2776. typedef void (*ggml_sycl_op_mul_mat_t)(
  2777. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  2778. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  2779. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  2780. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  2781. const dpct::queue_ptr &stream);
  2782. typedef void (*ggml_sycl_op_flatten_t)(const ggml_tensor *src0,
  2783. const ggml_tensor *src1,
  2784. ggml_tensor *dst, const float *src0_dd,
  2785. const float *src1_dd, float *dst_dd,
  2786. const dpct::queue_ptr &main_stream);
  2787. // QK = number of values after dequantization
  2788. // QR = QK / number of values before dequantization
  2789. // QI = number of 32 bit integers before dequantization
  2790. #define QK4_0 32
  2791. #define QR4_0 2
  2792. #define QI4_0 (QK4_0 / (4 * QR4_0))
  2793. typedef struct dpct_type_471834 {
  2794. sycl::half d; // delta
  2795. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  2796. } block_q4_0;
  2797. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  2798. #define QK4_1 32
  2799. #define QR4_1 2
  2800. #define QI4_1 (QK4_1 / (4 * QR4_1))
  2801. typedef struct dpct_type_143705 {
  2802. sycl::half2 dm; // dm.x = delta, dm.y = min
  2803. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  2804. } block_q4_1;
  2805. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  2806. #define QK5_0 32
  2807. #define QR5_0 2
  2808. #define QI5_0 (QK5_0 / (4 * QR5_0))
  2809. typedef struct dpct_type_673649 {
  2810. sycl::half d; // delta
  2811. uint8_t qh[4]; // 5-th bit of quants
  2812. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  2813. } block_q5_0;
  2814. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  2815. #define QK5_1 32
  2816. #define QR5_1 2
  2817. #define QI5_1 (QK5_1 / (4 * QR5_1))
  2818. typedef struct dpct_type_135589 {
  2819. sycl::half2 dm; // dm.x = delta, dm.y = min
  2820. uint8_t qh[4]; // 5-th bit of quants
  2821. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  2822. } block_q5_1;
  2823. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  2824. #define QK8_0 32
  2825. #define QR8_0 1
  2826. #define QI8_0 (QK8_0 / (4 * QR8_0))
  2827. typedef struct dpct_type_122878 {
  2828. sycl::half d; // delta
  2829. int8_t qs[QK8_0]; // quants
  2830. } block_q8_0;
  2831. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  2832. #define QK8_1 32
  2833. #define QR8_1 1
  2834. #define QI8_1 (QK8_1 / (4 * QR8_1))
  2835. typedef struct dpct_type_143721 {
  2836. sycl::half2 ds; // ds.x = delta, ds.y = sum
  2837. int8_t qs[QK8_0]; // quants
  2838. } block_q8_1;
  2839. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  2840. typedef float (*vec_dot_q_sycl_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  2841. typedef void (*allocate_tiles_sycl_t)(int **x_ql, sycl::half2 **x_dm,
  2842. int **x_qh, int **x_sc);
  2843. typedef void (*load_tiles_sycl_t)(const void *__restrict__ vx,
  2844. int *__restrict__ x_ql,
  2845. sycl::half2 *__restrict__ x_dm,
  2846. int *__restrict__ x_qh,
  2847. int *__restrict__ x_sc, const int &i_offset,
  2848. const int &i_max, const int &k,
  2849. const int &blocks_per_row);
  2850. typedef float (*vec_dot_q_mul_mat_sycl_t)(
  2851. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  2852. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  2853. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ms,
  2854. const int &i, const int &j, const int &k);
  2855. //================================= k-quants
  2856. #ifdef GGML_QKK_64
  2857. #define QK_K 64
  2858. #define K_SCALE_SIZE 4
  2859. #else
  2860. #define QK_K 256
  2861. #define K_SCALE_SIZE 12
  2862. #endif
  2863. #define QR2_K 4
  2864. #define QI2_K (QK_K / (4*QR2_K))
  2865. typedef struct dpct_type_619598 {
  2866. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  2867. uint8_t qs[QK_K/4]; // quants
  2868. sycl::half2 dm; // super-block scale for quantized scales/mins
  2869. } block_q2_K;
  2870. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  2871. #define QR3_K 4
  2872. #define QI3_K (QK_K / (4*QR3_K))
  2873. typedef struct dpct_type_138576 {
  2874. uint8_t hmask[QK_K/8]; // quants - high bit
  2875. uint8_t qs[QK_K/4]; // quants - low 2 bits
  2876. #ifdef GGML_QKK_64
  2877. uint8_t scales[2]; // scales, quantized with 8 bits
  2878. #else
  2879. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  2880. #endif
  2881. sycl::half d; // super-block scale
  2882. } block_q3_K;
  2883. //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");
  2884. #define QR4_K 2
  2885. #define QI4_K (QK_K / (4*QR4_K))
  2886. #ifdef GGML_QKK_64
  2887. typedef struct {
  2888. half dm[2]; // super-block scales/mins
  2889. uint8_t scales[2]; // 4-bit block scales/mins
  2890. uint8_t qs[QK_K/2]; // 4--bit quants
  2891. } block_q4_K;
  2892. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  2893. #else
  2894. typedef struct dpct_type_154943 {
  2895. sycl::half2 dm; // super-block scale for quantized scales/mins
  2896. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  2897. uint8_t qs[QK_K/2]; // 4--bit quants
  2898. } block_q4_K;
  2899. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  2900. #endif
  2901. #define QR5_K 2
  2902. #define QI5_K (QK_K / (4*QR5_K))
  2903. #ifdef GGML_QKK_64
  2904. typedef struct {
  2905. half d; // super-block scale
  2906. int8_t scales[QK_K/16]; // block scales
  2907. uint8_t qh[QK_K/8]; // quants, high bit
  2908. uint8_t qs[QK_K/2]; // quants, low 4 bits
  2909. } block_q5_K;
  2910. 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");
  2911. #else
  2912. typedef struct dpct_type_866817 {
  2913. sycl::half2 dm; // super-block scale for quantized scales/mins
  2914. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  2915. uint8_t qh[QK_K/8]; // quants, high bit
  2916. uint8_t qs[QK_K/2]; // quants, low 4 bits
  2917. } block_q5_K;
  2918. 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");
  2919. #endif
  2920. #define QR6_K 2
  2921. #define QI6_K (QK_K / (4*QR6_K))
  2922. typedef struct dpct_type_107281 {
  2923. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  2924. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  2925. int8_t scales[QK_K/16]; // scales
  2926. sycl::half d; // delta
  2927. } block_q6_K;
  2928. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  2929. #define WARP_SIZE 32
  2930. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  2931. #define SYCL_GELU_BLOCK_SIZE 256
  2932. #define SYCL_SILU_BLOCK_SIZE 256
  2933. #define SYCL_TANH_BLOCK_SIZE 256
  2934. #define SYCL_RELU_BLOCK_SIZE 256
  2935. #define SYCL_SQR_BLOCK_SIZE 256
  2936. #define SYCL_CPY_BLOCK_SIZE 32
  2937. #define SYCL_SCALE_BLOCK_SIZE 256
  2938. #define SYCL_CLAMP_BLOCK_SIZE 256
  2939. #define SYCL_ROPE_BLOCK_SIZE 256
  2940. #define SYCL_SOFT_MAX_BLOCK_SIZE 1024
  2941. #define SYCL_ALIBI_BLOCK_SIZE 32
  2942. #define SYCL_DIAG_MASK_INF_BLOCK_SIZE 32
  2943. #define SYCL_QUANTIZE_BLOCK_SIZE 256
  2944. #define SYCL_DEQUANTIZE_BLOCK_SIZE 256
  2945. #define SYCL_GET_ROWS_BLOCK_SIZE 256
  2946. #define SYCL_UPSCALE_BLOCK_SIZE 256
  2947. #define SYCL_CONCAT_BLOCK_SIZE 256
  2948. #define SYCL_PAD_BLOCK_SIZE 256
  2949. #define SYCL_ACC_BLOCK_SIZE 256
  2950. #define SYCL_IM2COL_BLOCK_SIZE 256
  2951. // dmmv = dequantize_mul_mat_vec
  2952. #ifndef GGML_SYCL_DMMV_X
  2953. #define GGML_SYCL_DMMV_X 32
  2954. #endif
  2955. #ifndef GGML_SYCL_MMV_Y
  2956. #define GGML_SYCL_MMV_Y 1
  2957. #endif
  2958. #ifndef K_QUANTS_PER_ITERATION
  2959. #define K_QUANTS_PER_ITERATION 2
  2960. #else
  2961. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  2962. #endif
  2963. #ifndef GGML_SYCL_PEER_MAX_BATCH_SIZE
  2964. #define GGML_SYCL_PEER_MAX_BATCH_SIZE 128
  2965. #endif // GGML_SYCL_PEER_MAX_BATCH_SIZE
  2966. #define MUL_MAT_SRC1_COL_STRIDE 128
  2967. #define MAX_STREAMS 8
  2968. static dpct::queue_ptr g_syclStreams[GGML_SYCL_MAX_DEVICES][MAX_STREAMS] = {
  2969. {0}};
  2970. struct ggml_tensor_extra_gpu {
  2971. void * data_device[GGML_SYCL_MAX_DEVICES]; // 1 pointer for each device for split tensors
  2972. dpct::event_ptr
  2973. events[GGML_SYCL_MAX_DEVICES]
  2974. [MAX_STREAMS]; // events for synchronizing multiple GPUs
  2975. };
  2976. inline dpct::err0 ggml_sycl_set_device(const int device) try {
  2977. int current_device;
  2978. SYCL_CHECK(CHECK_TRY_ERROR(
  2979. current_device = dpct::dev_mgr::instance().current_device_id()));
  2980. // GGML_SYCL_DEBUG("ggml_sycl_set_device device=%d, current_device=%d\n", device, current_device);
  2981. if (device == current_device) {
  2982. return 0;
  2983. }
  2984. return CHECK_TRY_ERROR(dpct::select_device(device));
  2985. }
  2986. catch (sycl::exception const &exc) {
  2987. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  2988. << ", line:" << __LINE__ << std::endl;
  2989. crash();
  2990. std::exit(1);
  2991. }
  2992. static int g_device_count = -1;
  2993. static int g_all_sycl_device_count = -1;
  2994. static int g_main_device = -1;
  2995. static int g_main_device_index = -1;
  2996. static float g_tensor_split[GGML_SYCL_MAX_DEVICES] = {0};
  2997. struct sycl_device_capabilities {
  2998. int cc; // compute capability
  2999. bool vmm; // virtual memory support
  3000. size_t vmm_granularity; // granularity of virtual memory
  3001. int device_id;
  3002. };
  3003. static sycl_device_capabilities g_device_caps[GGML_SYCL_MAX_DEVICES] = { {0, false, 0, -1} };
  3004. struct sycl_device_id2index {
  3005. int index;
  3006. };
  3007. static sycl_device_id2index g_sycl_device_id2index[GGML_SYCL_MAX_DEVICES] = { {-1} };
  3008. static void * g_scratch_buffer = nullptr;
  3009. static size_t g_scratch_size = 0; // disabled by default
  3010. static size_t g_scratch_offset = 0;
  3011. static dpct::queue_ptr g_sycl_handles[GGML_SYCL_MAX_DEVICES] = {nullptr};
  3012. int get_main_device(){
  3013. return g_main_device;
  3014. }
  3015. [[noreturn]]
  3016. static void bad_arch(const sycl::stream &stream_ct1) {
  3017. stream_ct1 << "ERROR: ggml-sycl was compiled without support for the "
  3018. "current GPU architecture.\n";
  3019. // __trap();
  3020. std::exit(1);
  3021. (void) bad_arch; // suppress unused function warning
  3022. }
  3023. void log_ggml_var_device(const char*name, float *src, size_t total_elements, bool src_on_device){
  3024. if(!g_ggml_sycl_debug) return;
  3025. if(!src){
  3026. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3027. return;
  3028. }
  3029. char filename[1024];
  3030. sprintf(filename, "%s.txt", name);
  3031. printf("GGML Tensor:%s save to %s\n", name, filename);
  3032. size_t total_size = total_elements*sizeof(float);
  3033. float *local_buf = NULL;
  3034. // printf("total_size %d2, src_on_device %d\n", total_size, src_on_device);
  3035. if(src_on_device) {
  3036. local_buf = (float *) ggml_sycl_host_malloc(total_size);
  3037. // printf("local buf %p size %d bytes\n", local_buf, total_size);
  3038. ggml_sycl_set_device(g_main_device);
  3039. dpct::queue_ptr main_stream = g_syclStreams[g_main_device_index][0];
  3040. main_stream->memcpy(local_buf, src, total_size);
  3041. }
  3042. else {
  3043. local_buf = (float *)src;
  3044. // printf("local buf from src-> data %p\n", local_buf);
  3045. }
  3046. std::ofstream logfile;
  3047. logfile.open(filename);
  3048. // printf("local buf element %d\n", total_elements);
  3049. for(int i=0; i<total_elements; i++){
  3050. if((i+1)%20 ==0) logfile <<std::endl;
  3051. else logfile << local_buf[i] <<" ";
  3052. }
  3053. logfile <<std::endl;
  3054. logfile.close();
  3055. if(src_on_device) ggml_sycl_host_free(local_buf);
  3056. }
  3057. //todo: debug for crash in some case
  3058. void print_ggml_tensor(const char*name, struct ggml_tensor *src){
  3059. if(!g_ggml_sycl_debug) return;
  3060. if(!src){
  3061. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3062. return;
  3063. }
  3064. size_t total_elements = ggml_nelements(src);
  3065. const bool src_on_device = src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT;
  3066. float *src_data =NULL;
  3067. if(src_on_device) {
  3068. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  3069. src_data = (float*)src_extra->data_device[g_main_device_index];
  3070. }
  3071. else {
  3072. src_data = (float *)src->data;
  3073. }
  3074. log_ggml_var_device(name, src_data, total_elements, src_on_device);
  3075. }
  3076. static int log_file_name_idx=0;
  3077. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt) {
  3078. stop_cnt = 4;
  3079. if(log_file_name_idx>=stop_cnt) return;
  3080. char filename[1280];
  3081. sprintf(filename, "%s_%07d", name, log_file_name_idx);
  3082. log_file_name_idx++;
  3083. print_ggml_tensor(filename, src);
  3084. // print_ggml_tensor("ggml_sycl_rms_norm_src0", (ggml_tensor *)src0);
  3085. // print_ggml_tensor("ggml_sycl_rms_norm_src1", (ggml_tensor *)src1);
  3086. // int *ptr = NULL;
  3087. // *ptr = 0;
  3088. }
  3089. static __dpct_inline__ float warp_reduce_sum(float x,
  3090. const sycl::nd_item<3> &item_ct1) {
  3091. #pragma unroll
  3092. for (int mask = 16; mask > 0; mask >>= 1) {
  3093. /*
  3094. DPCT1096:98: The right-most dimension of the work-group used in the SYCL
  3095. kernel that calls this function may be less than "32". The function
  3096. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3097. CPU device. Modify the size of the work-group to ensure that the value
  3098. of the right-most dimension is a multiple of "32".
  3099. */
  3100. x += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), x, mask);
  3101. }
  3102. return x;
  3103. }
  3104. static __dpct_inline__ sycl::float2
  3105. warp_reduce_sum(sycl::float2 a, const sycl::nd_item<3> &item_ct1) {
  3106. #pragma unroll
  3107. for (int mask = 16; mask > 0; mask >>= 1) {
  3108. a.x() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.x(),
  3109. mask);
  3110. a.y() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.y(),
  3111. mask);
  3112. }
  3113. return a;
  3114. }
  3115. static __dpct_inline__ float warp_reduce_max(float x,
  3116. const sycl::nd_item<3> &item_ct1) {
  3117. #pragma unroll
  3118. for (int mask = 16; mask > 0; mask >>= 1) {
  3119. /*
  3120. DPCT1096:97: The right-most dimension of the work-group used in the SYCL
  3121. kernel that calls this function may be less than "32". The function
  3122. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3123. CPU device. Modify the size of the work-group to ensure that the value
  3124. of the right-most dimension is a multiple of "32".
  3125. */
  3126. x = sycl::fmax(x, dpct::permute_sub_group_by_xor(
  3127. item_ct1.get_sub_group(), x, mask));
  3128. }
  3129. return x;
  3130. }
  3131. static __dpct_inline__ float op_repeat(const float a, const float b) {
  3132. return b;
  3133. }
  3134. static __dpct_inline__ float op_add(const float a, const float b) {
  3135. return a + b;
  3136. }
  3137. static __dpct_inline__ float op_mul(const float a, const float b) {
  3138. return a * b;
  3139. }
  3140. static __dpct_inline__ float op_div(const float a, const float b) {
  3141. return a / b;
  3142. }
  3143. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3144. static void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3145. int ne0, int ne1, int ne2, int ne3,
  3146. int ne10, int ne11, int ne12, int ne13,
  3147. /*int s0, */ int s1, int s2, int s3,
  3148. /*int s10,*/ int s11, int s12, int s13,
  3149. const sycl::nd_item<3> &item_ct1) {
  3150. const int i0s = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3151. item_ct1.get_local_id(2);
  3152. const int i1 = (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  3153. item_ct1.get_local_id(1));
  3154. const int i2 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3155. item_ct1.get_local_id(0)) /
  3156. ne3;
  3157. const int i3 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3158. item_ct1.get_local_id(0)) %
  3159. ne3;
  3160. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3161. return;
  3162. }
  3163. const int i11 = i1 % ne11;
  3164. const int i12 = i2 % ne12;
  3165. const int i13 = i3 % ne13;
  3166. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3167. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3168. const size_t i_dst = i_src0;
  3169. const src0_t * src0_row = src0 + i_src0;
  3170. const src1_t * src1_row = src1 + i_src1;
  3171. dst_t * dst_row = dst + i_dst;
  3172. for (int i0 = i0s; i0 < ne0;
  3173. i0 += item_ct1.get_local_range(2) * item_ct1.get_group_range(2)) {
  3174. const int i10 = i0 % ne10;
  3175. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3176. }
  3177. }
  3178. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3179. static void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3180. int ne0, int ne1, int ne2, int ne3,
  3181. int ne10, int ne11, int ne12, int ne13,
  3182. /*int s0, */ int s1, int s2, int s3,
  3183. /*int s10,*/ int s11, int s12, int s13,
  3184. const sycl::nd_item<3> &item_ct1) {
  3185. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3186. item_ct1.get_local_id(2);
  3187. const int i3 = i/(ne2*ne1*ne0);
  3188. const int i2 = (i/(ne1*ne0)) % ne2;
  3189. const int i1 = (i/ne0) % ne1;
  3190. const int i0 = i % ne0;
  3191. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3192. return;
  3193. }
  3194. const int i11 = i1 % ne11;
  3195. const int i12 = i2 % ne12;
  3196. const int i13 = i3 % ne13;
  3197. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3198. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3199. const size_t i_dst = i_src0;
  3200. const src0_t * src0_row = src0 + i_src0;
  3201. const src1_t * src1_row = src1 + i_src1;
  3202. dst_t * dst_row = dst + i_dst;
  3203. const int i10 = i0 % ne10;
  3204. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3205. }
  3206. static void acc_f32(const float * x, const float * y, float * dst, const int ne,
  3207. const int ne10, const int ne11, const int ne12,
  3208. const int nb1, const int nb2, int offset, const sycl::nd_item<3> &item_ct1) {
  3209. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3210. item_ct1.get_local_id(2);
  3211. if (i >= ne) {
  3212. return;
  3213. }
  3214. int src1_idx = i - offset;
  3215. int oz = src1_idx / nb2;
  3216. int oy = (src1_idx - (oz * nb2)) / nb1;
  3217. int ox = src1_idx % nb1;
  3218. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  3219. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  3220. } else {
  3221. dst[i] = x[i];
  3222. }
  3223. }
  3224. static void gelu_f32(const float * x, float * dst, const int k,
  3225. const sycl::nd_item<3> &item_ct1) {
  3226. const float GELU_COEF_A = 0.044715f;
  3227. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  3228. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3229. item_ct1.get_local_id(2);
  3230. if (i >= k) {
  3231. return;
  3232. }
  3233. float xi = x[i];
  3234. dst[i] = 0.5f * xi *
  3235. (1.0f +
  3236. sycl::tanh(SQRT_2_OVER_PI * xi * (1.0f + GELU_COEF_A * xi * xi)));
  3237. }
  3238. static void silu_f32(const float * x, float * dst, const int k,
  3239. const sycl::nd_item<3> &item_ct1) {
  3240. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3241. item_ct1.get_local_id(2);
  3242. if (i >= k) {
  3243. return;
  3244. }
  3245. dst[i] = x[i] / (1.0f + sycl::native::exp(-x[i]));
  3246. }
  3247. static void gelu_quick_f32(const float *x, float *dst, int k,
  3248. const sycl::nd_item<3> &item_ct1) {
  3249. const float GELU_QUICK_COEF = -1.702f;
  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 / (1.0f + sycl::native::exp(GELU_QUICK_COEF * x[i])));
  3256. }
  3257. static void tanh_f32(const float *x, float *dst, int k,
  3258. const sycl::nd_item<3> &item_ct1) {
  3259. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3260. item_ct1.get_local_id(2);
  3261. if (i >= k) {
  3262. return;
  3263. }
  3264. dst[i] = sycl::tanh((float)(x[i]));
  3265. }
  3266. static void relu_f32(const float * x, float * dst, const int k,
  3267. const sycl::nd_item<3> &item_ct1) {
  3268. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3269. item_ct1.get_local_id(2);
  3270. if (i >= k) {
  3271. return;
  3272. }
  3273. dst[i] = sycl::fmax((float)(x[i]), (float)0);
  3274. }
  3275. static void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope,
  3276. const sycl::nd_item<3> &item_ct1) {
  3277. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3278. item_ct1.get_local_id(2);
  3279. if (i >= k) {
  3280. return;
  3281. }
  3282. dst[i] = sycl::fmax((float)(x[i]), (float)0) +
  3283. sycl::fmin((float)(x[i]), 0.0f) * negative_slope;
  3284. }
  3285. static void sqr_f32(const float * x, float * dst, const int k,
  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] = x[i] * x[i];
  3293. }
  3294. static void norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3295. const sycl::nd_item<3> &item_ct1, sycl::float2 *s_sum, int block_size) {
  3296. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3297. item_ct1.get_local_id(1);
  3298. const int tid = item_ct1.get_local_id(2);
  3299. sycl::float2 mean_var = sycl::float2(0.f, 0.f);
  3300. for (int col = tid; col < ncols; col += block_size) {
  3301. const float xi = x[row*ncols + col];
  3302. mean_var.x() += xi;
  3303. mean_var.y() += xi * xi;
  3304. }
  3305. // sum up partial sums
  3306. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3307. if (block_size > WARP_SIZE) {
  3308. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3309. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3310. if (lane_id == 0) {
  3311. s_sum[warp_id] = mean_var;
  3312. }
  3313. /*
  3314. DPCT1118:0: SYCL group functions and algorithms must be encountered in
  3315. converged control flow. You may need to adjust the code.
  3316. */
  3317. item_ct1.barrier(sycl::access::fence_space::local_space);
  3318. mean_var = s_sum[lane_id];
  3319. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3320. }
  3321. const float mean = mean_var.x() / ncols;
  3322. const float var = mean_var.y() / ncols - mean * mean;
  3323. const float inv_std = sycl::rsqrt(var + eps);
  3324. for (int col = tid; col < ncols; col += block_size) {
  3325. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  3326. }
  3327. }
  3328. static void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02,
  3329. const sycl::nd_item<3> &item_ct1) {
  3330. int nidx = item_ct1.get_local_id(2) +
  3331. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3332. if (nidx >= ne0) {
  3333. return;
  3334. }
  3335. // operation
  3336. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3337. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3338. if (item_ct1.get_group(0) < ne02) { // src0
  3339. int offset_src =
  3340. nidx + item_ct1.get_group(1) * ne0 +
  3341. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3342. dst[offset_dst] = x[offset_src];
  3343. } else {
  3344. int offset_src =
  3345. nidx + item_ct1.get_group(1) * ne0 +
  3346. (item_ct1.get_group(0) - ne02) * ne0 * item_ct1.get_group_range(1);
  3347. dst[offset_dst] = y[offset_src];
  3348. }
  3349. }
  3350. static void upscale_f32(const float *x, float *dst, const int ne00, const int nb02, const int scale_factor,
  3351. const sycl::nd_item<3> &item_ct1) {
  3352. int ne0 = ne00 * scale_factor;
  3353. int nidx = item_ct1.get_local_id(2) +
  3354. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3355. if (nidx >= ne0) {
  3356. return;
  3357. }
  3358. // operation
  3359. int i00 = nidx / scale_factor;
  3360. int i01 = item_ct1.get_group(1) / scale_factor;
  3361. int offset_src = i00 + i01 * ne00 + item_ct1.get_group(0) * nb02;
  3362. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3363. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3364. dst[offset_dst] = x[offset_src];
  3365. }
  3366. static void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02,
  3367. const sycl::nd_item<3> &item_ct1) {
  3368. int nidx = item_ct1.get_local_id(2) +
  3369. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3370. if (nidx >= ne0) {
  3371. return;
  3372. }
  3373. // operation
  3374. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3375. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3376. if (nidx < ne00 && item_ct1.get_group(1) < ne01 &&
  3377. item_ct1.get_group(0) < ne02) {
  3378. int offset_src = nidx + item_ct1.get_group(1) * ne00 +
  3379. item_ct1.get_group(0) * ne00 * ne01;
  3380. dst[offset_dst] = x[offset_src];
  3381. } else {
  3382. dst[offset_dst] = 0.0f;
  3383. }
  3384. }
  3385. static void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps,
  3386. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3387. int start = item_ct1.get_group(2) * group_size;
  3388. int end = start + group_size;
  3389. start += item_ct1.get_local_id(2);
  3390. if (end >= ne_elements) {
  3391. end = ne_elements;
  3392. }
  3393. float tmp = 0.0f; // partial sum for thread in warp
  3394. for (int j = start; j < end; j += block_size) {
  3395. tmp += x[j];
  3396. }
  3397. tmp = warp_reduce_sum(tmp, item_ct1);
  3398. if (block_size > WARP_SIZE) {
  3399. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3400. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3401. if (lane_id == 0) {
  3402. s_sum[warp_id] = tmp;
  3403. }
  3404. /*
  3405. DPCT1118:1: SYCL group functions and algorithms must be encountered in
  3406. converged control flow. You may need to adjust the code.
  3407. */
  3408. /*
  3409. DPCT1065:54: Consider replacing sycl::nd_item::barrier() with
  3410. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3411. better performance if there is no access to global memory.
  3412. */
  3413. item_ct1.barrier();
  3414. tmp = s_sum[lane_id];
  3415. tmp = warp_reduce_sum(tmp, item_ct1);
  3416. }
  3417. float mean = tmp / group_size;
  3418. tmp = 0.0f;
  3419. for (int j = start; j < end; j += block_size) {
  3420. float xi = x[j] - mean;
  3421. dst[j] = xi;
  3422. tmp += xi * xi;
  3423. }
  3424. tmp = warp_reduce_sum(tmp, item_ct1);
  3425. if (block_size > WARP_SIZE) {
  3426. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3427. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3428. if (lane_id == 0) {
  3429. s_sum[warp_id] = tmp;
  3430. }
  3431. /*
  3432. DPCT1118:2: SYCL group functions and algorithms must be encountered in
  3433. converged control flow. You may need to adjust the code.
  3434. */
  3435. /*
  3436. DPCT1065:55: Consider replacing sycl::nd_item::barrier() with
  3437. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3438. better performance if there is no access to global memory.
  3439. */
  3440. item_ct1.barrier();
  3441. tmp = s_sum[lane_id];
  3442. tmp = warp_reduce_sum(tmp, item_ct1);
  3443. }
  3444. float variance = tmp / group_size;
  3445. float scale = sycl::rsqrt(variance + eps);
  3446. for (int j = start; j < end; j += block_size) {
  3447. dst[j] *= scale;
  3448. }
  3449. }
  3450. static void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3451. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3452. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3453. item_ct1.get_local_id(1);
  3454. const int tid = item_ct1.get_local_id(2);
  3455. float tmp = 0.0f; // partial sum for thread in warp
  3456. for (int col = tid; col < ncols; col += block_size) {
  3457. const float xi = x[row*ncols + col];
  3458. tmp += xi * xi;
  3459. }
  3460. // sum up partial sums
  3461. tmp = warp_reduce_sum(tmp, item_ct1);
  3462. if (block_size > WARP_SIZE) {
  3463. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3464. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3465. if (lane_id == 0) {
  3466. s_sum[warp_id] = tmp;
  3467. }
  3468. /*
  3469. DPCT1118:3: SYCL group functions and algorithms must be encountered in
  3470. converged control flow. You may need to adjust the code.
  3471. */
  3472. item_ct1.barrier(sycl::access::fence_space::local_space);
  3473. tmp = s_sum[lane_id];
  3474. tmp = warp_reduce_sum(tmp, item_ct1);
  3475. }
  3476. const float mean = tmp / ncols;
  3477. const float scale = sycl::rsqrt(mean + eps);
  3478. for (int col = tid; col < ncols; col += block_size) {
  3479. dst[row*ncols + col] = scale * x[row*ncols + col];
  3480. }
  3481. }
  3482. static __dpct_inline__ void dequantize_q4_0(const void *vx, const int ib,
  3483. const int iqs, dfloat2 &v) {
  3484. const block_q4_0 * x = (const block_q4_0 *) vx;
  3485. const dfloat d = x[ib].d;
  3486. const int vui = x[ib].qs[iqs];
  3487. v.x() = vui & 0xF;
  3488. v.y() = vui >> 4;
  3489. #ifdef GGML_SYCL_F16
  3490. // v = v - {8.0f, 8.0f};
  3491. // v = v * {d, d};
  3492. v.s0() = (v.s0() - 8.0f) * d;
  3493. v.s1() = (v.s1() - 8.0f) * d;
  3494. #else
  3495. v.x() = (v.x() - 8.0f) * d;
  3496. v.y() = (v.y() - 8.0f) * d;
  3497. #endif // GGML_SYCL_F16
  3498. }
  3499. static __dpct_inline__ void dequantize_q4_1(const void *vx, const int ib,
  3500. const int iqs, dfloat2 &v) {
  3501. const block_q4_1 * x = (const block_q4_1 *) vx;
  3502. const dfloat d = x[ib].dm[0];
  3503. const dfloat m = x[ib].dm[1];
  3504. const int vui = x[ib].qs[iqs];
  3505. v.x() = vui & 0xF;
  3506. v.y() = vui >> 4;
  3507. #ifdef GGML_SYCL_F16
  3508. // v = v * {d, d};
  3509. // v = v + {m, m};
  3510. v.s0() = (v.s0() * d) + m;
  3511. v.s1() = (v.s1() * d) + m;
  3512. #else
  3513. v.x() = (v.x() * d) + m;
  3514. v.y() = (v.y() * d) + m;
  3515. #endif // GGML_SYCL_F16
  3516. }
  3517. static __dpct_inline__ void dequantize_q5_0(const void *vx, const int ib,
  3518. const int iqs, dfloat2 &v) {
  3519. const block_q5_0 * x = (const block_q5_0 *) vx;
  3520. const dfloat d = x[ib].d;
  3521. uint32_t qh;
  3522. memcpy(&qh, x[ib].qh, sizeof(qh));
  3523. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3524. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3525. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3526. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3527. #ifdef GGML_SYCL_F16
  3528. // v = v - {16.0f, 16.0f};
  3529. // v = v * {d, d};
  3530. v.s0() = (v.s0() - 16.0f) * d;
  3531. v.s1() = (v.s1() - 16.0f) * d;
  3532. #else
  3533. v.x() = (v.x() - 16.0f) * d;
  3534. v.y() = (v.y() - 16.0f) * d;
  3535. #endif // GGML_SYCL_F16
  3536. }
  3537. static __dpct_inline__ void dequantize_q5_1(const void *vx, const int ib,
  3538. const int iqs, dfloat2 &v) {
  3539. const block_q5_1 * x = (const block_q5_1 *) vx;
  3540. const dfloat d = x[ib].dm[0];
  3541. const dfloat m = x[ib].dm[1];
  3542. uint32_t qh;
  3543. memcpy(&qh, x[ib].qh, sizeof(qh));
  3544. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3545. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3546. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3547. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3548. #ifdef GGML_SYCL_F16
  3549. // v = v * {d, d};
  3550. // v = v + {m, m};
  3551. v.s0() = (v.s0() * d) + m;
  3552. v.s1() = (v.s1() * d) + m;
  3553. #else
  3554. v.x() = (v.x() * d) + m;
  3555. v.y() = (v.y() * d) + m;
  3556. #endif // GGML_SYCL_F16
  3557. }
  3558. static __dpct_inline__ void dequantize_q8_0(const void *vx, const int ib,
  3559. const int iqs, dfloat2 &v) {
  3560. const block_q8_0 * x = (const block_q8_0 *) vx;
  3561. const dfloat d = x[ib].d;
  3562. v.x() = x[ib].qs[iqs + 0];
  3563. v.y() = x[ib].qs[iqs + 1];
  3564. #ifdef GGML_SYCL_F16
  3565. // v = v * {d, d};
  3566. v.s0() *= d;
  3567. v.s1() *= d;
  3568. #else
  3569. v.x() *= d;
  3570. v.y() *= d;
  3571. #endif // GGML_SYCL_F16
  3572. }
  3573. //================================== k-quants
  3574. template<typename dst_t>
  3575. static void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3576. const sycl::nd_item<3> &item_ct1) {
  3577. const int i = item_ct1.get_group(2);
  3578. const block_q2_K * x = (const block_q2_K *) vx;
  3579. const int tid = item_ct1.get_local_id(2);
  3580. #if QK_K == 256
  3581. const int n = tid/32;
  3582. const int l = tid - 32*n;
  3583. const int is = 8*n + l/16;
  3584. const uint8_t q = x[i].qs[32*n + l];
  3585. dst_t * y = yy + i*QK_K + 128*n;
  3586. float dall = x[i].dm[0];
  3587. float dmin = x[i].dm[1];
  3588. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3589. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3590. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  3591. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  3592. #else
  3593. const int is = tid/16; // 0 or 1
  3594. const int il = tid%16; // 0...15
  3595. const uint8_t q = x[i].qs[il] >> (2*is);
  3596. dst_t * y = yy + i*QK_K + 16*is + il;
  3597. float dall = __low2half(x[i].dm);
  3598. float dmin = __high2half(x[i].dm);
  3599. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3600. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3601. #endif
  3602. }
  3603. template<typename dst_t>
  3604. static void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3605. const sycl::nd_item<3> &item_ct1) {
  3606. const int i = item_ct1.get_group(2);
  3607. const block_q3_K * x = (const block_q3_K *) vx;
  3608. #if QK_K == 256
  3609. const int r = item_ct1.get_local_id(2) / 4;
  3610. const int tid = r/2;
  3611. const int is0 = r%2;
  3612. const int l0 = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4);
  3613. const int n = tid / 4;
  3614. const int j = tid - 4*n;
  3615. uint8_t m = 1 << (4*n + j);
  3616. int is = 8*n + 2*j + is0;
  3617. int shift = 2*j;
  3618. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  3619. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  3620. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  3621. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  3622. float d_all = x[i].d;
  3623. float dl = d_all * (us - 32);
  3624. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  3625. const uint8_t * q = x[i].qs + 32*n;
  3626. const uint8_t * hm = x[i].hmask;
  3627. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  3628. #else
  3629. const int tid = threadIdx.x;
  3630. const int is = tid/16; // 0 or 1
  3631. const int il = tid%16; // 0...15
  3632. const int im = il/8; // 0...1
  3633. const int in = il%8; // 0...7
  3634. dst_t * y = yy + i*QK_K + 16*is + il;
  3635. const uint8_t q = x[i].qs[il] >> (2*is);
  3636. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  3637. const float d = (float)x[i].d;
  3638. if (is == 0) {
  3639. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  3640. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  3641. } else {
  3642. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  3643. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  3644. }
  3645. #endif
  3646. }
  3647. #if QK_K == 256
  3648. static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  3649. if (j < 4) {
  3650. d = q[j] & 63; m = q[j + 4] & 63;
  3651. } else {
  3652. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  3653. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  3654. }
  3655. }
  3656. #endif
  3657. template<typename dst_t>
  3658. static void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3659. const sycl::nd_item<3> &item_ct1) {
  3660. const block_q4_K * x = (const block_q4_K *) vx;
  3661. const int i = item_ct1.get_group(2);
  3662. #if QK_K == 256
  3663. // assume 32 threads
  3664. const int tid = item_ct1.get_local_id(2);
  3665. const int il = tid/8;
  3666. const int ir = tid%8;
  3667. const int is = 2*il;
  3668. const int n = 4;
  3669. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  3670. const float dall = x[i].dm[0];
  3671. const float dmin = x[i].dm[1];
  3672. const uint8_t * q = x[i].qs + 32*il + n*ir;
  3673. uint8_t sc, m;
  3674. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3675. const float d1 = dall * sc; const float m1 = dmin * m;
  3676. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3677. const float d2 = dall * sc; const float m2 = dmin * m;
  3678. for (int l = 0; l < n; ++l) {
  3679. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  3680. y[l +32] = d2 * (q[l] >> 4) - m2;
  3681. }
  3682. #else
  3683. const int tid = threadIdx.x;
  3684. const uint8_t * q = x[i].qs;
  3685. dst_t * y = yy + i*QK_K;
  3686. const float d = (float)x[i].dm[0];
  3687. const float m = (float)x[i].dm[1];
  3688. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  3689. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  3690. #endif
  3691. }
  3692. template<typename dst_t>
  3693. static void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3694. const sycl::nd_item<3> &item_ct1) {
  3695. const block_q5_K * x = (const block_q5_K *) vx;
  3696. const int i = item_ct1.get_group(2);
  3697. #if QK_K == 256
  3698. // assume 64 threads - this is very slightly better than the one below
  3699. const int tid = item_ct1.get_local_id(2);
  3700. const int il = tid/16; // il is in 0...3
  3701. const int ir = tid%16; // ir is in 0...15
  3702. const int is = 2*il; // is is in 0...6
  3703. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  3704. const float dall = x[i].dm[0];
  3705. const float dmin = x[i].dm[1];
  3706. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  3707. const uint8_t * qh = x[i].qh + 2*ir;
  3708. uint8_t sc, m;
  3709. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3710. const float d1 = dall * sc; const float m1 = dmin * m;
  3711. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3712. const float d2 = dall * sc; const float m2 = dmin * m;
  3713. uint8_t hm = 1 << (2*il);
  3714. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  3715. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  3716. hm <<= 1;
  3717. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  3718. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  3719. #else
  3720. const int tid = threadIdx.x;
  3721. const uint8_t q = x[i].qs[tid];
  3722. const int im = tid/8; // 0...3
  3723. const int in = tid%8; // 0...7
  3724. const int is = tid/16; // 0 or 1
  3725. const uint8_t h = x[i].qh[in] >> im;
  3726. const float d = x[i].d;
  3727. dst_t * y = yy + i*QK_K + tid;
  3728. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  3729. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  3730. #endif
  3731. }
  3732. template<typename dst_t>
  3733. static void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3734. const sycl::nd_item<3> &item_ct1) {
  3735. const block_q6_K * x = (const block_q6_K *) vx;
  3736. const int i = item_ct1.get_group(2);
  3737. #if QK_K == 256
  3738. // assume 64 threads - this is very slightly better than the one below
  3739. const int tid = item_ct1.get_local_id(2);
  3740. const int ip = tid/32; // ip is 0 or 1
  3741. const int il = tid - 32*ip; // 0...32
  3742. const int is = 8*ip + il/16;
  3743. dst_t * y = yy + i*QK_K + 128*ip + il;
  3744. const float d = x[i].d;
  3745. const uint8_t * ql = x[i].ql + 64*ip + il;
  3746. const uint8_t qh = x[i].qh[32*ip + il];
  3747. const int8_t * sc = x[i].scales + is;
  3748. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3749. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  3750. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3751. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  3752. #else
  3753. // assume 32 threads
  3754. const int tid = threadIdx.x;
  3755. const int ip = tid/16; // 0 or 1
  3756. const int il = tid - 16*ip; // 0...15
  3757. dst_t * y = yy + i*QK_K + 16*ip + il;
  3758. const float d = x[i].d;
  3759. const uint8_t ql = x[i].ql[16*ip + il];
  3760. const uint8_t qh = x[i].qh[il] >> (2*ip);
  3761. const int8_t * sc = x[i].scales;
  3762. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3763. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3764. #endif
  3765. }
  3766. /*
  3767. DPCT1110:4: The total declared local variable size in device function
  3768. dequantize_mul_mat_vec_q2_k exceeds 128 bytes and may cause high register
  3769. pressure. Consult with your hardware vendor to find the total register size
  3770. available and adjust the code, or use smaller sub-group size to avoid high
  3771. register pressure.
  3772. */
  3773. static void dequantize_mul_mat_vec_q2_k(const void *__restrict__ vx,
  3774. const float *__restrict__ yy,
  3775. float *__restrict__ dst,
  3776. const int ncols, int nrows,
  3777. const sycl::nd_item<3> &item_ct1) {
  3778. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  3779. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3780. item_ct1.get_local_id(1);
  3781. if (row > nrows) return;
  3782. const int num_blocks_per_row = ncols / QK_K;
  3783. const int ib0 = row*num_blocks_per_row;
  3784. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  3785. float tmp = 0; // partial sum for thread in warp
  3786. #if QK_K == 256
  3787. const int tid =
  3788. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  3789. const int ix =
  3790. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  3791. const int step = 16/K_QUANTS_PER_ITERATION;
  3792. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  3793. const int in = tid - step*im; // 0...15 or 0...7
  3794. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  3795. const int q_offset = 32*im + l0;
  3796. const int s_offset = 8*im;
  3797. const int y_offset = 128*im + l0;
  3798. uint32_t aux[4];
  3799. const uint8_t * d = (const uint8_t *)aux;
  3800. const uint8_t * m = (const uint8_t *)(aux + 2);
  3801. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  3802. const float * y = yy + i * QK_K + y_offset;
  3803. const uint8_t * q = x[i].qs + q_offset;
  3804. const float dall = x[i].dm[0];
  3805. const float dmin = x[i].dm[1];
  3806. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  3807. aux[0] = a[0] & 0x0f0f0f0f;
  3808. aux[1] = a[1] & 0x0f0f0f0f;
  3809. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  3810. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  3811. float sum1 = 0, sum2 = 0;
  3812. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  3813. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  3814. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  3815. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  3816. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  3817. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  3818. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  3819. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  3820. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  3821. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  3822. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  3823. }
  3824. tmp += dall * sum1 - dmin * sum2;
  3825. }
  3826. #else
  3827. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  3828. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  3829. const int offset = tid * K_QUANTS_PER_ITERATION;
  3830. uint32_t uaux[2];
  3831. const uint8_t * d = (const uint8_t *)uaux;
  3832. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  3833. const float * y = yy + i * QK_K + offset;
  3834. const uint8_t * q = x[i].qs + offset;
  3835. const uint32_t * s = (const uint32_t *)x[i].scales;
  3836. uaux[0] = s[0] & 0x0f0f0f0f;
  3837. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  3838. const float2 dall = __half22float2(x[i].dm);
  3839. float sum1 = 0, sum2 = 0;
  3840. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  3841. const uint8_t ql = q[l];
  3842. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  3843. + y[l+16] * d[1] * ((ql >> 2) & 3)
  3844. + y[l+32] * d[2] * ((ql >> 4) & 3)
  3845. + y[l+48] * d[3] * ((ql >> 6) & 3);
  3846. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  3847. }
  3848. tmp += dall.x * sum1 - dall.y * sum2;
  3849. }
  3850. #endif
  3851. // sum up partial sums and write back result
  3852. #pragma unroll
  3853. for (int mask = 16; mask > 0; mask >>= 1) {
  3854. tmp +=
  3855. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  3856. }
  3857. if (item_ct1.get_local_id(2) == 0) {
  3858. dst[row] = tmp;
  3859. }
  3860. }
  3861. /*
  3862. DPCT1110:5: The total declared local variable size in device function
  3863. dequantize_mul_mat_vec_q3_k exceeds 128 bytes and may cause high register
  3864. pressure. Consult with your hardware vendor to find the total register size
  3865. available and adjust the code, or use smaller sub-group size to avoid high
  3866. register pressure.
  3867. */
  3868. static void dequantize_mul_mat_vec_q3_k(const void *__restrict__ vx,
  3869. const float *__restrict__ yy,
  3870. float *__restrict__ dst,
  3871. const int ncols, int nrows,
  3872. const sycl::nd_item<3> &item_ct1) {
  3873. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3874. item_ct1.get_local_id(1);
  3875. if (row > nrows) return;
  3876. const int num_blocks_per_row = ncols / QK_K;
  3877. const int ib0 = row*num_blocks_per_row;
  3878. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  3879. float tmp = 0; // partial sum for thread in warp
  3880. #if QK_K == 256
  3881. const uint16_t kmask1 = 0x0303;
  3882. const uint16_t kmask2 = 0x0f0f;
  3883. const int tid =
  3884. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  3885. const int ix =
  3886. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  3887. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  3888. const int step = 16/K_QUANTS_PER_ITERATION;
  3889. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  3890. const int in = tid - step*im; // 0....15 or 0...7
  3891. const uint8_t m = 1 << (4*im);
  3892. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  3893. const int q_offset = 32*im + l0;
  3894. const int y_offset = 128*im + l0;
  3895. uint16_t utmp[4];
  3896. const int8_t * s = (const int8_t *)utmp;
  3897. const uint16_t s_shift = 4*im;
  3898. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  3899. const float * y = yy + i * QK_K + y_offset;
  3900. const uint8_t * q = x[i].qs + q_offset;
  3901. const uint8_t * h = x[i].hmask + l0;
  3902. const uint16_t * a = (const uint16_t *)x[i].scales;
  3903. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  3904. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  3905. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  3906. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  3907. const float d = x[i].d;
  3908. float sum = 0;
  3909. for (int l = 0; l < n; ++l) {
  3910. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  3911. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  3912. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  3913. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  3914. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  3915. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  3916. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  3917. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  3918. }
  3919. tmp += d * sum;
  3920. }
  3921. #else
  3922. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  3923. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  3924. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  3925. const int in = offset/8; // 0 or 1
  3926. const int im = offset%8; // 0...7
  3927. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  3928. const float * y = yy + i * QK_K + offset;
  3929. const uint8_t * q = x[i].qs + offset;
  3930. const uint8_t * s = x[i].scales;
  3931. const float dall = (float)x[i].d;
  3932. float sum = 0;
  3933. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  3934. const uint8_t hl = x[i].hmask[im+l] >> in;
  3935. const uint8_t ql = q[l];
  3936. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  3937. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  3938. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  3939. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  3940. }
  3941. tmp += sum;
  3942. }
  3943. #endif
  3944. // sum up partial sums and write back result
  3945. #pragma unroll
  3946. for (int mask = 16; mask > 0; mask >>= 1) {
  3947. tmp +=
  3948. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  3949. }
  3950. if (item_ct1.get_local_id(2) == 0) {
  3951. dst[row] = tmp;
  3952. }
  3953. }
  3954. /*
  3955. DPCT1110:6: The total declared local variable size in device function
  3956. dequantize_mul_mat_vec_q4_k exceeds 128 bytes and may cause high register
  3957. pressure. Consult with your hardware vendor to find the total register size
  3958. available and adjust the code, or use smaller sub-group size to avoid high
  3959. register pressure.
  3960. */
  3961. static void dequantize_mul_mat_vec_q4_k(const void *__restrict__ vx,
  3962. const float *__restrict__ yy,
  3963. float *__restrict__ dst,
  3964. const int ncols, int nrows,
  3965. const sycl::nd_item<3> &item_ct1) {
  3966. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3967. item_ct1.get_local_id(1);
  3968. if (row > nrows) return;
  3969. const int num_blocks_per_row = ncols / QK_K;
  3970. const int ib0 = row*num_blocks_per_row;
  3971. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  3972. #if QK_K == 256
  3973. const uint16_t kmask1 = 0x3f3f;
  3974. const uint16_t kmask2 = 0x0f0f;
  3975. const uint16_t kmask3 = 0xc0c0;
  3976. const int tid =
  3977. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  3978. const int ix =
  3979. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  3980. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  3981. const int il = tid/step; // 0...3
  3982. const int ir = tid - step*il; // 0...7 or 0...3
  3983. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  3984. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  3985. const int in = il%2;
  3986. const int l0 = n*(2*ir + in);
  3987. const int q_offset = 32*im + l0;
  3988. const int y_offset = 64*im + l0;
  3989. uint16_t aux[4];
  3990. const uint8_t * sc = (const uint8_t *)aux;
  3991. #if K_QUANTS_PER_ITERATION == 2
  3992. uint32_t q32[4];
  3993. const uint8_t * q4 = (const uint8_t *)q32;
  3994. #else
  3995. uint16_t q16[4];
  3996. const uint8_t * q4 = (const uint8_t *)q16;
  3997. #endif
  3998. float tmp = 0; // partial sum for thread in warp
  3999. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4000. const float * y1 = yy + i*QK_K + y_offset;
  4001. const float * y2 = y1 + 128;
  4002. const float dall = x[i].dm[0];
  4003. const float dmin = x[i].dm[1];
  4004. const uint16_t * a = (const uint16_t *)x[i].scales;
  4005. aux[0] = a[im+0] & kmask1;
  4006. aux[1] = a[im+2] & kmask1;
  4007. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4008. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4009. #if K_QUANTS_PER_ITERATION == 2
  4010. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  4011. const uint32_t * q2 = q1 + 16;
  4012. q32[0] = q1[0] & 0x0f0f0f0f;
  4013. q32[1] = q1[0] & 0xf0f0f0f0;
  4014. q32[2] = q2[0] & 0x0f0f0f0f;
  4015. q32[3] = q2[0] & 0xf0f0f0f0;
  4016. sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
  4017. float smin = 0;
  4018. for (int l = 0; l < 4; ++l) {
  4019. s.x() += y1[l] * q4[l + 0]; s.y() += y1[l + 32] * q4[l + 4];
  4020. s.z() += y2[l] * q4[l + 8]; s.w() += y2[l + 32] * q4[l + 12];
  4021. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4022. }
  4023. tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f / 16.f +
  4024. s.z() * sc[4] + s.w() * sc[5] * 1.f / 16.f) -
  4025. dmin * smin;
  4026. #else
  4027. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  4028. const uint16_t * q2 = q1 + 32;
  4029. q16[0] = q1[0] & 0x0f0f;
  4030. q16[1] = q1[0] & 0xf0f0;
  4031. q16[2] = q2[0] & 0x0f0f;
  4032. q16[3] = q2[0] & 0xf0f0;
  4033. float4 s = {0.f, 0.f, 0.f, 0.f};
  4034. float smin = 0;
  4035. for (int l = 0; l < 2; ++l) {
  4036. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  4037. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  4038. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4039. }
  4040. 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;
  4041. #endif
  4042. }
  4043. #else
  4044. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  4045. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  4046. const int step = tid * K_QUANTS_PER_ITERATION;
  4047. uint16_t aux16[2];
  4048. const uint8_t * s = (const uint8_t *)aux16;
  4049. float tmp = 0;
  4050. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4051. const uint8_t * q = x[i].qs + step;
  4052. const float * y = yy + i*QK_K + step;
  4053. const uint16_t * a = (const uint16_t *)x[i].scales;
  4054. aux16[0] = a[0] & 0x0f0f;
  4055. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4056. const float d = (float)x[i].dm[0];
  4057. const float m = (float)x[i].dm[1];
  4058. float sum = 0.f;
  4059. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4060. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  4061. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  4062. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  4063. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  4064. }
  4065. tmp += sum;
  4066. }
  4067. #endif
  4068. // sum up partial sums and write back result
  4069. #pragma unroll
  4070. for (int mask = 16; mask > 0; mask >>= 1) {
  4071. tmp +=
  4072. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4073. }
  4074. if (tid == 0) {
  4075. dst[row] = tmp;
  4076. }
  4077. }
  4078. /*
  4079. DPCT1110:7: The total declared local variable size in device function
  4080. dequantize_mul_mat_vec_q5_k exceeds 128 bytes and may cause high register
  4081. pressure. Consult with your hardware vendor to find the total register size
  4082. available and adjust the code, or use smaller sub-group size to avoid high
  4083. register pressure.
  4084. */
  4085. static void dequantize_mul_mat_vec_q5_k(const void *__restrict__ vx,
  4086. const float *__restrict__ yy,
  4087. float *__restrict__ dst,
  4088. const int ncols,
  4089. const sycl::nd_item<3> &item_ct1) {
  4090. const int row = item_ct1.get_group(2);
  4091. const int num_blocks_per_row = ncols / QK_K;
  4092. const int ib0 = row*num_blocks_per_row;
  4093. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  4094. float tmp = 0; // partial sum for thread in warp
  4095. #if QK_K == 256
  4096. const uint16_t kmask1 = 0x3f3f;
  4097. const uint16_t kmask2 = 0x0f0f;
  4098. const uint16_t kmask3 = 0xc0c0;
  4099. const int tid = item_ct1.get_local_id(2) / 2; // 0...15
  4100. const int ix = item_ct1.get_local_id(2) % 2;
  4101. const int il = tid/4; // 0...3
  4102. const int ir = tid - 4*il;// 0...3
  4103. const int n = 2;
  4104. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4105. const int in = il%2;
  4106. const int l0 = n*(2*ir + in);
  4107. const int q_offset = 32*im + l0;
  4108. const int y_offset = 64*im + l0;
  4109. const uint8_t hm1 = 1 << (2*im);
  4110. const uint8_t hm2 = hm1 << 4;
  4111. uint16_t aux[4];
  4112. const uint8_t * sc = (const uint8_t *)aux;
  4113. uint16_t q16[8];
  4114. const uint8_t * q4 = (const uint8_t *)q16;
  4115. for (int i = ix; i < num_blocks_per_row; i += 2) {
  4116. const uint8_t * ql1 = x[i].qs + q_offset;
  4117. const uint8_t * qh = x[i].qh + l0;
  4118. const float * y1 = yy + i*QK_K + y_offset;
  4119. const float * y2 = y1 + 128;
  4120. const float dall = x[i].dm[0];
  4121. const float dmin = x[i].dm[1];
  4122. const uint16_t * a = (const uint16_t *)x[i].scales;
  4123. aux[0] = a[im+0] & kmask1;
  4124. aux[1] = a[im+2] & kmask1;
  4125. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4126. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4127. sycl::float4 sum = {0.f, 0.f, 0.f, 0.f};
  4128. float smin = 0;
  4129. const uint16_t * q1 = (const uint16_t *)ql1;
  4130. const uint16_t * q2 = q1 + 32;
  4131. q16[0] = q1[0] & 0x0f0f;
  4132. q16[1] = q1[8] & 0x0f0f;
  4133. q16[2] = (q1[0] >> 4) & 0x0f0f;
  4134. q16[3] = (q1[8] >> 4) & 0x0f0f;
  4135. q16[4] = q2[0] & 0x0f0f;
  4136. q16[5] = q2[8] & 0x0f0f;
  4137. q16[6] = (q2[0] >> 4) & 0x0f0f;
  4138. q16[7] = (q2[8] >> 4) & 0x0f0f;
  4139. for (int l = 0; l < n; ++l) {
  4140. sum.x() +=
  4141. y1[l + 0] * (q4[l + 0] + (qh[l + 0] & (hm1 << 0) ? 16 : 0)) +
  4142. y1[l + 16] * (q4[l + 2] + (qh[l + 16] & (hm1 << 0) ? 16 : 0));
  4143. sum.y() +=
  4144. y1[l + 32] * (q4[l + 4] + (qh[l + 0] & (hm1 << 1) ? 16 : 0)) +
  4145. y1[l + 48] * (q4[l + 6] + (qh[l + 16] & (hm1 << 1) ? 16 : 0));
  4146. sum.z() +=
  4147. y2[l + 0] * (q4[l + 8] + (qh[l + 0] & (hm2 << 0) ? 16 : 0)) +
  4148. y2[l + 16] * (q4[l + 10] + (qh[l + 16] & (hm2 << 0) ? 16 : 0));
  4149. sum.w() +=
  4150. y2[l + 32] * (q4[l + 12] + (qh[l + 0] & (hm2 << 1) ? 16 : 0)) +
  4151. y2[l + 48] * (q4[l + 14] + (qh[l + 16] & (hm2 << 1) ? 16 : 0));
  4152. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  4153. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  4154. }
  4155. tmp += dall * (sum.x() * sc[0] + sum.y() * sc[1] + sum.z() * sc[4] +
  4156. sum.w() * sc[5]) -
  4157. dmin * smin;
  4158. }
  4159. #else
  4160. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  4161. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  4162. const int step = tid * K_QUANTS_PER_ITERATION;
  4163. const int im = step/8;
  4164. const int in = step%8;
  4165. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4166. const uint8_t * q = x[i].qs + step;
  4167. const int8_t * s = x[i].scales;
  4168. const float * y = yy + i*QK_K + step;
  4169. const float d = x[i].d;
  4170. float sum = 0.f;
  4171. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4172. const uint8_t h = x[i].qh[in+j] >> im;
  4173. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  4174. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  4175. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  4176. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  4177. }
  4178. tmp += sum;
  4179. }
  4180. #endif
  4181. // sum up partial sums and write back result
  4182. #pragma unroll
  4183. for (int mask = 16; mask > 0; mask >>= 1) {
  4184. tmp +=
  4185. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4186. }
  4187. if (item_ct1.get_local_id(2) == 0) {
  4188. dst[row] = tmp;
  4189. }
  4190. }
  4191. static void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows,
  4192. const sycl::nd_item<3> &item_ct1) {
  4193. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4194. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4195. item_ct1.get_local_id(1);
  4196. if (row > nrows) return;
  4197. const int num_blocks_per_row = ncols / QK_K;
  4198. const int ib0 = row*num_blocks_per_row;
  4199. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  4200. #if QK_K == 256
  4201. const int tid =
  4202. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4203. const int ix =
  4204. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0, 1
  4205. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  4206. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4207. const int in = tid - step*im; // 0...15 or 0...7
  4208. #if K_QUANTS_PER_ITERATION == 1
  4209. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  4210. const int is = 0;
  4211. #else
  4212. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  4213. const int is = in / 4;
  4214. #endif
  4215. const int ql_offset = 64*im + l0;
  4216. const int qh_offset = 32*im + l0;
  4217. const int s_offset = 8*im + is;
  4218. const int y_offset = 128*im + l0;
  4219. float tmp = 0; // partial sum for thread in warp
  4220. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4221. const float * y = yy + i * QK_K + y_offset;
  4222. const uint8_t * ql = x[i].ql + ql_offset;
  4223. const uint8_t * qh = x[i].qh + qh_offset;
  4224. const int8_t * s = x[i].scales + s_offset;
  4225. const float d = x[i].d;
  4226. #if K_QUANTS_PER_ITERATION == 1
  4227. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  4228. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  4229. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  4230. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  4231. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  4232. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  4233. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  4234. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  4235. tmp += sum;
  4236. #else
  4237. float sum = 0;
  4238. for (int l = 0; l < 4; ++l) {
  4239. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  4240. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  4241. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  4242. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  4243. }
  4244. tmp += sum;
  4245. #endif
  4246. }
  4247. #else
  4248. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  4249. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  4250. const int step = tid * K_QUANTS_PER_ITERATION;
  4251. float tmp = 0; // partial sum for thread in warp
  4252. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4253. const float * y = yy + i * QK_K + step;
  4254. const uint8_t * ql = x[i].ql + step;
  4255. const uint8_t * qh = x[i].qh + step;
  4256. const int8_t * s = x[i].scales;
  4257. const float d = x[i+0].d;
  4258. float sum = 0;
  4259. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4260. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  4261. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  4262. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  4263. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  4264. }
  4265. tmp += sum;
  4266. }
  4267. #endif
  4268. // sum up partial sums and write back result
  4269. #pragma unroll
  4270. for (int mask = 16; mask > 0; mask >>= 1) {
  4271. tmp +=
  4272. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4273. }
  4274. if (tid == 0) {
  4275. dst[row] = tmp;
  4276. }
  4277. }
  4278. static void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4279. const sycl::half *x = (const sycl::half *)vx;
  4280. // automatic half -> float type cast if dfloat == float
  4281. v.x() = x[ib + iqs + 0];
  4282. v.y() = x[ib + iqs + 1];
  4283. }
  4284. static void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4285. const float * x = (const float *) vx;
  4286. // automatic half -> float type cast if dfloat == float
  4287. v.x() = x[ib + iqs + 0];
  4288. v.y() = x[ib + iqs + 1];
  4289. }
  4290. static void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded,
  4291. const sycl::nd_item<3> &item_ct1) {
  4292. const int ix = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4293. item_ct1.get_local_id(2);
  4294. if (ix >= kx_padded) {
  4295. return;
  4296. }
  4297. const int iy = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4298. item_ct1.get_local_id(1);
  4299. const int i_padded = iy*kx_padded + ix;
  4300. block_q8_1 * y = (block_q8_1 *) vy;
  4301. const int ib = i_padded / QK8_1; // block index
  4302. const int iqs = i_padded % QK8_1; // quant index
  4303. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  4304. float amax = sycl::fabs((float)xi);
  4305. float sum = xi;
  4306. #pragma unroll
  4307. for (int mask = 16; mask > 0; mask >>= 1) {
  4308. amax = sycl::fmax(amax, dpct::permute_sub_group_by_xor(
  4309. item_ct1.get_sub_group(), amax, mask));
  4310. sum +=
  4311. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), sum, mask);
  4312. }
  4313. const float d = amax / 127;
  4314. const int8_t q = amax == 0.0f ? 0 : sycl::round(xi / d);
  4315. y[ib].qs[iqs] = q;
  4316. if (iqs > 0) {
  4317. return;
  4318. }
  4319. reinterpret_cast<sycl::half &>(y[ib].ds.x()) = d;
  4320. reinterpret_cast<sycl::half &>(y[ib].ds.y()) = sum;
  4321. }
  4322. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4323. static void k_get_rows(
  4324. const void * src0, const int32_t * src1, dst_t * dst,
  4325. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4326. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4327. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4328. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4329. size_t s10, size_t s11, size_t s12,
  4330. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4331. const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4332. item_ct1.get_local_id(2)) *
  4333. 2;
  4334. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4335. item_ct1.get_local_id(1);
  4336. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4337. item_ct1.get_local_id(0)) /
  4338. ne12;
  4339. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4340. item_ct1.get_local_id(0)) %
  4341. ne12;
  4342. if (i00 >= ne00) {
  4343. return;
  4344. }
  4345. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4346. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4347. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  4348. const int ib = i00/qk; // block index
  4349. const int iqs = (i00%qk)/qr; // quant index
  4350. const int iybs = i00 - i00%qk; // dst block start index
  4351. const int y_offset = qr == 1 ? 1 : qk/2;
  4352. // dequantize
  4353. dfloat2 v;
  4354. dequantize_kernel(src0_row, ib, iqs, v);
  4355. dst_row[iybs + iqs + 0] = v.x();
  4356. dst_row[iybs + iqs + y_offset] = v.y();
  4357. }
  4358. template<typename src0_t, typename dst_t>
  4359. static void k_get_rows_float(
  4360. const src0_t * src0, const int32_t * src1, dst_t * dst,
  4361. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4362. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4363. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4364. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4365. size_t s10, size_t s11, size_t s12,
  4366. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4367. const int i00 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4368. item_ct1.get_local_id(2);
  4369. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4370. item_ct1.get_local_id(1);
  4371. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4372. item_ct1.get_local_id(0)) /
  4373. ne12;
  4374. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4375. item_ct1.get_local_id(0)) %
  4376. ne12;
  4377. if (i00 >= ne00) {
  4378. return;
  4379. }
  4380. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4381. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4382. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  4383. dst_row[i00] = src0_row[i00];
  4384. }
  4385. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4386. static void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4387. const sycl::nd_item<3> &item_ct1) {
  4388. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4389. 2 * item_ct1.get_local_id(2);
  4390. if (i >= k) {
  4391. return;
  4392. }
  4393. const int ib = i/qk; // block index
  4394. const int iqs = (i%qk)/qr; // quant index
  4395. const int iybs = i - i%qk; // y block start index
  4396. const int y_offset = qr == 1 ? 1 : qk/2;
  4397. // dequantize
  4398. dfloat2 v;
  4399. dequantize_kernel(vx, ib, iqs, v);
  4400. y[iybs + iqs + 0] = v.x();
  4401. y[iybs + iqs + y_offset] = v.y();
  4402. }
  4403. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  4404. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  4405. #define VDR_Q4_0_Q8_1_MMVQ 2
  4406. #define VDR_Q4_0_Q8_1_MMQ 4
  4407. template <int vdr>
  4408. static __dpct_inline__ float vec_dot_q4_0_q8_1_impl(const int *v, const int *u,
  4409. const float &d4,
  4410. const sycl::half2 &ds8) {
  4411. int sumi = 0;
  4412. #pragma unroll
  4413. for (int i = 0; i < vdr; ++i) {
  4414. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4415. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4416. // SIMD dot product of quantized values
  4417. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4418. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4419. }
  4420. const sycl::float2 ds8f =
  4421. ds8.convert<float, sycl::rounding_mode::automatic>();
  4422. // second part effectively subtracts 8 from each quant value
  4423. return d4 * (sumi * ds8f.x() - (8 * vdr / QI4_0) * ds8f.y());
  4424. }
  4425. #define VDR_Q4_1_Q8_1_MMVQ 2
  4426. #define VDR_Q4_1_Q8_1_MMQ 4
  4427. template <int vdr>
  4428. static __dpct_inline__ float vec_dot_q4_1_q8_1_impl(const int *v, const int *u,
  4429. const sycl::half2 &dm4,
  4430. const sycl::half2 &ds8) {
  4431. int sumi = 0;
  4432. #pragma unroll
  4433. for (int i = 0; i < vdr; ++i) {
  4434. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4435. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4436. // SIMD dot product of quantized values
  4437. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4438. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4439. }
  4440. #ifdef GGML_SYCL_F16
  4441. const sycl::float2 tmp =
  4442. (dm4 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4443. const float d4d8 = tmp.x();
  4444. const float m4s8 = tmp.y();
  4445. #else
  4446. const sycl::float2 dm4f =
  4447. dm4.convert<float, sycl::rounding_mode::automatic>();
  4448. const sycl::float2 ds8f =
  4449. ds8.convert<float, sycl::rounding_mode::automatic>();
  4450. const float d4d8 = dm4f.x() * ds8f.x();
  4451. const float m4s8 = dm4f.y() * ds8f.y();
  4452. #endif // GGML_SYCL_F16
  4453. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  4454. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  4455. }
  4456. #define VDR_Q5_0_Q8_1_MMVQ 2
  4457. #define VDR_Q5_0_Q8_1_MMQ 4
  4458. template <int vdr>
  4459. static __dpct_inline__ float
  4460. vec_dot_q5_0_q8_1_impl(const int *vl, const int *vh, const int *u,
  4461. const float &d5, const sycl::half2 &ds8) {
  4462. int sumi = 0;
  4463. #pragma unroll
  4464. for (int i = 0; i < vdr; ++i) {
  4465. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4466. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4467. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4468. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4469. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4470. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4471. sumi); // SIMD dot product of quantized values
  4472. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4473. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4474. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4475. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4476. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4477. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4478. sumi); // SIMD dot product of quantized values
  4479. }
  4480. const sycl::float2 ds8f =
  4481. ds8.convert<float, sycl::rounding_mode::automatic>();
  4482. // second part effectively subtracts 16 from each quant value
  4483. return d5 * (sumi * ds8f.x() - (16 * vdr / QI5_0) * ds8f.y());
  4484. }
  4485. #define VDR_Q5_1_Q8_1_MMVQ 2
  4486. #define VDR_Q5_1_Q8_1_MMQ 4
  4487. template <int vdr>
  4488. static __dpct_inline__ float
  4489. vec_dot_q5_1_q8_1_impl(const int *vl, const int *vh, const int *u,
  4490. const sycl::half2 &dm5, const sycl::half2 &ds8) {
  4491. int sumi = 0;
  4492. #pragma unroll
  4493. for (int i = 0; i < vdr; ++i) {
  4494. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4495. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4496. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4497. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4498. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4499. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4500. sumi); // SIMD dot product of quantized values
  4501. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4502. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4503. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4504. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4505. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4506. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4507. sumi); // SIMD dot product of quantized values
  4508. }
  4509. #ifdef GGML_SYCL_F16
  4510. const sycl::float2 tmp =
  4511. (dm5 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4512. const float d5d8 = tmp.x();
  4513. const float m5s8 = tmp.y();
  4514. #else
  4515. const sycl::float2 dm5f =
  4516. dm5.convert<float, sycl::rounding_mode::automatic>();
  4517. const sycl::float2 ds8f =
  4518. ds8.convert<float, sycl::rounding_mode::automatic>();
  4519. const float d5d8 = dm5f.x() * ds8f.x();
  4520. const float m5s8 = dm5f.y() * ds8f.y();
  4521. #endif // GGML_SYCL_F16
  4522. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  4523. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  4524. }
  4525. #define VDR_Q8_0_Q8_1_MMVQ 2
  4526. #define VDR_Q8_0_Q8_1_MMQ 8
  4527. template <int vdr>
  4528. static __dpct_inline__ float vec_dot_q8_0_q8_1_impl(const int *v, const int *u,
  4529. const float &d8_0,
  4530. const float &d8_1) {
  4531. int sumi = 0;
  4532. #pragma unroll
  4533. for (int i = 0; i < vdr; ++i) {
  4534. // SIMD dot product of quantized values
  4535. sumi = dpct::dp4a(v[i], u[i], sumi);
  4536. }
  4537. return d8_0*d8_1 * sumi;
  4538. }
  4539. template <int vdr>
  4540. static __dpct_inline__ float vec_dot_q8_1_q8_1_impl(const int *v, const int *u,
  4541. const sycl::half2 &dm8,
  4542. const sycl::half2 &ds8) {
  4543. int sumi = 0;
  4544. #pragma unroll
  4545. for (int i = 0; i < vdr; ++i) {
  4546. // SIMD dot product of quantized values
  4547. sumi = dpct::dp4a(v[i], u[i], sumi);
  4548. }
  4549. #ifdef GGML_SYCL_F16
  4550. const sycl::float2 tmp =
  4551. (dm8 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4552. const float d8d8 = tmp.x();
  4553. const float m8s8 = tmp.y();
  4554. #else
  4555. const sycl::float2 dm8f =
  4556. dm8.convert<float, sycl::rounding_mode::automatic>();
  4557. const sycl::float2 ds8f =
  4558. ds8.convert<float, sycl::rounding_mode::automatic>();
  4559. const float d8d8 = dm8f.x() * ds8f.x();
  4560. const float m8s8 = dm8f.y() * ds8f.y();
  4561. #endif // GGML_SYCL_F16
  4562. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  4563. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  4564. }
  4565. #define VDR_Q2_K_Q8_1_MMVQ 1
  4566. #define VDR_Q2_K_Q8_1_MMQ 2
  4567. // contiguous v/x values
  4568. static __dpct_inline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  4569. const int &v, const int *__restrict__ u, const uint8_t *__restrict__ scales,
  4570. const sycl::half2 &dm2, const float *__restrict__ d8) {
  4571. float sumf_d = 0.0f;
  4572. float sumf_m = 0.0f;
  4573. #pragma unroll
  4574. for (int i = 0; i < QR2_K; ++i) {
  4575. const int sc = scales[2*i];
  4576. const int vi = (v >> (2*i)) & 0x03030303;
  4577. sumf_d +=
  4578. d8[i] * (dpct::dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  4579. // fill int with 4x m
  4580. int m = sc >> 4;
  4581. m |= m << 8;
  4582. m |= m << 16;
  4583. sumf_m += d8[i] *
  4584. dpct::dp4a(
  4585. m, u[i],
  4586. 0); // multiply constant q2_K part with sum of q8_1 values
  4587. }
  4588. const sycl::float2 dm2f =
  4589. dm2.convert<float, sycl::rounding_mode::automatic>();
  4590. return dm2f.x() * sumf_d - dm2f.y() * sumf_m;
  4591. }
  4592. // contiguous u/y values
  4593. static __dpct_inline__ float
  4594. vec_dot_q2_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4595. const uint8_t *__restrict__ scales,
  4596. const sycl::half2 &dm2, const float &d8) {
  4597. int sumi_d = 0;
  4598. int sumi_m = 0;
  4599. #pragma unroll
  4600. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  4601. int sumi_d_sc = 0;
  4602. const int sc = scales[i0 / (QI8_1/2)];
  4603. // fill int with 4x m
  4604. int m = sc >> 4;
  4605. m |= m << 8;
  4606. m |= m << 16;
  4607. #pragma unroll
  4608. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  4609. sumi_d_sc = dpct::dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  4610. sumi_m = dpct::dp4a(m, u[i],
  4611. sumi_m); // multiply sum of q8_1 values with m
  4612. }
  4613. sumi_d += sumi_d_sc * (sc & 0xF);
  4614. }
  4615. const sycl::float2 dm2f =
  4616. dm2.convert<float, sycl::rounding_mode::automatic>();
  4617. return d8 * (dm2f.x() * sumi_d - dm2f.y() * sumi_m);
  4618. }
  4619. #define VDR_Q3_K_Q8_1_MMVQ 1
  4620. #define VDR_Q3_K_Q8_1_MMQ 2
  4621. // contiguous v/x values
  4622. static __dpct_inline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  4623. const int &vl, const int &vh, const int *__restrict__ u,
  4624. const uint8_t *__restrict__ scales, const int &scale_offset,
  4625. const float &d3, const float *__restrict__ d8) {
  4626. float sumf = 0.0f;
  4627. #pragma unroll
  4628. for (int i = 0; i < QR3_K; ++i) {
  4629. const int isc = scale_offset + 2*i;
  4630. const int isc_low = isc % (QK_K/32);
  4631. const int sc_shift_low = 4 * (isc / (QK_K/32));
  4632. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  4633. const int isc_high = isc % (QK_K/64);
  4634. const int sc_shift_high = 2 * (isc / (QK_K/64));
  4635. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  4636. const int sc = (sc_low | sc_high) - 32;
  4637. const int vil = (vl >> (2*i)) & 0x03030303;
  4638. const int vih = ((vh >> i) << 2) & 0x04040404;
  4639. const int vi =
  4640. dpct::vectorized_binary<sycl::char4>(vil, vih, dpct::sub_sat());
  4641. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  4642. }
  4643. return d3 * sumf;
  4644. }
  4645. // contiguous u/y values
  4646. static __dpct_inline__ float
  4647. vec_dot_q3_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4648. const int8_t *__restrict__ scales, const float &d3,
  4649. const float &d8) {
  4650. int sumi = 0;
  4651. #pragma unroll
  4652. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  4653. int sumi_sc = 0;
  4654. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  4655. sumi_sc = dpct::dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  4656. }
  4657. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  4658. }
  4659. return d3*d8 * sumi;
  4660. }
  4661. #define VDR_Q4_K_Q8_1_MMVQ 2
  4662. #define VDR_Q4_K_Q8_1_MMQ 8
  4663. // contiguous v/x values
  4664. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  4665. const int *__restrict__ v, const int *__restrict__ u,
  4666. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4667. const sycl::half2 &dm4, const float *__restrict__ d8) {
  4668. float sumf_d = 0.0f;
  4669. float sumf_m = 0.0f;
  4670. #pragma unroll
  4671. for (int i = 0; i < QR4_K; ++i) {
  4672. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  4673. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  4674. const int dot1 =
  4675. dpct::dp4a(v1i, u[2 * i + 1],
  4676. dpct::dp4a(v0i, u[2 * i + 0], 0)); // SIMD dot product
  4677. const int dot2 =
  4678. dpct::dp4a(0x01010101, u[2 * i + 1],
  4679. dpct::dp4a(0x01010101, u[2 * i + 0], 0)); // sum of u
  4680. sumf_d += d8[i] * (dot1 * sc[i]);
  4681. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  4682. }
  4683. const sycl::float2 dm4f =
  4684. dm4.convert<float, sycl::rounding_mode::automatic>();
  4685. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4686. }
  4687. // contiguous u/y values
  4688. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_mmq(
  4689. const int *__restrict__ v, const int *__restrict__ u,
  4690. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4691. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  4692. float sumf_d = 0.0f;
  4693. float sumf_m = 0.0f;
  4694. #pragma unroll
  4695. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  4696. int sumi_d = 0;
  4697. #pragma unroll
  4698. for (int j = 0; j < QI8_1; ++j) {
  4699. sumi_d = dpct::dp4a((v[j] >> (4 * i)) & 0x0F0F0F0F,
  4700. u[i * QI8_1 + j], sumi_d); // SIMD dot product
  4701. }
  4702. const sycl::float2 ds8f =
  4703. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  4704. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  4705. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  4706. }
  4707. const sycl::float2 dm4f =
  4708. dm4.convert<float, sycl::rounding_mode::automatic>();
  4709. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4710. }
  4711. #define VDR_Q5_K_Q8_1_MMVQ 2
  4712. #define VDR_Q5_K_Q8_1_MMQ 8
  4713. // contiguous v/x values
  4714. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  4715. const int *__restrict__ vl, const int *__restrict__ vh,
  4716. const int *__restrict__ u, const uint8_t *__restrict__ sc,
  4717. const uint8_t *__restrict__ m, const sycl::half2 &dm5,
  4718. const float *__restrict__ d8) {
  4719. float sumf_d = 0.0f;
  4720. float sumf_m = 0.0f;
  4721. #pragma unroll
  4722. for (int i = 0; i < QR5_K; ++i) {
  4723. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  4724. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  4725. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  4726. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  4727. const int v0i = vl0i | vh0i;
  4728. const int v1i = vl1i | vh1i;
  4729. const int dot1 =
  4730. dpct::dp4a(v0i, u[2 * i + 0],
  4731. dpct::dp4a(v1i, u[2 * i + 1], 0)); // SIMD dot product
  4732. const int dot2 =
  4733. dpct::dp4a(0x01010101, u[2 * i + 0],
  4734. dpct::dp4a(0x01010101, u[2 * i + 1], 0)); // sum of u
  4735. sumf_d += d8[i] * (dot1 * sc[i]);
  4736. sumf_m += d8[i] * (dot2 * m[i]);
  4737. }
  4738. const sycl::float2 dm5f =
  4739. dm5.convert<float, sycl::rounding_mode::automatic>();
  4740. return dm5f.x() * sumf_d - dm5f.y() * sumf_m;
  4741. }
  4742. // contiguous u/y values
  4743. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_mmq(
  4744. const int *__restrict__ v, const int *__restrict__ u,
  4745. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4746. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  4747. float sumf_d = 0.0f;
  4748. float sumf_m = 0.0f;
  4749. #pragma unroll
  4750. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  4751. int sumi_d = 0;
  4752. #pragma unroll
  4753. for (int j = 0; j < QI8_1; ++j) {
  4754. sumi_d = dpct::dp4a(v[i * QI8_1 + j], u[i * QI8_1 + j],
  4755. sumi_d); // SIMD dot product
  4756. }
  4757. const sycl::float2 ds8f =
  4758. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  4759. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  4760. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  4761. }
  4762. const sycl::float2 dm4f =
  4763. dm4.convert<float, sycl::rounding_mode::automatic>();
  4764. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4765. }
  4766. #define VDR_Q6_K_Q8_1_MMVQ 1
  4767. #define VDR_Q6_K_Q8_1_MMQ 8
  4768. // contiguous v/x values
  4769. static __dpct_inline__ float
  4770. vec_dot_q6_K_q8_1_impl_mmvq(const int &vl, const int &vh,
  4771. const int *__restrict__ u,
  4772. const int8_t *__restrict__ scales, const float &d,
  4773. const float *__restrict__ d8) {
  4774. float sumf = 0.0f;
  4775. #pragma unroll
  4776. for (int i = 0; i < QR6_K; ++i) {
  4777. const int sc = scales[4*i];
  4778. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  4779. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  4780. const int vi = dpct::vectorized_binary<sycl::char4>(
  4781. (vil | vih), 0x20202020, dpct::sub_sat()); // vi = (vil | vih) - 32
  4782. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  4783. }
  4784. return d*sumf;
  4785. }
  4786. // contiguous u/y values
  4787. static __dpct_inline__ float
  4788. vec_dot_q6_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4789. const int8_t *__restrict__ sc, const float &d6,
  4790. const float *__restrict__ d8) {
  4791. float sumf_d = 0.0f;
  4792. #pragma unroll
  4793. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  4794. sycl::int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  4795. #pragma unroll
  4796. for (int i = i0; i < i0 + 2; ++i) {
  4797. sumi_d.x() = dpct::dp4a(v[2 * i + 0], u[2 * i + 0],
  4798. sumi_d.x()); // SIMD dot product
  4799. sumi_d.x() = dpct::dp4a(v[2 * i + 1], u[2 * i + 1],
  4800. sumi_d.x()); // SIMD dot product
  4801. sumi_d.y() = dpct::dp4a(v[2 * i + 4], u[2 * i + 4],
  4802. sumi_d.y()); // SIMD dot product
  4803. sumi_d.y() = dpct::dp4a(v[2 * i + 5], u[2 * i + 5],
  4804. sumi_d.y()); // SIMD dot product
  4805. }
  4806. sumf_d += d8[i0 / 4] *
  4807. (sc[i0 / 2 + 0] * sumi_d.x() + sc[i0 / 2 + 1] * sumi_d.y());
  4808. }
  4809. return d6 * sumf_d;
  4810. }
  4811. static __dpct_inline__ float
  4812. vec_dot_q4_0_q8_1(const void *__restrict__ vbq,
  4813. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  4814. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  4815. int v[VDR_Q4_0_Q8_1_MMVQ];
  4816. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  4817. #pragma unroll
  4818. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  4819. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  4820. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  4821. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  4822. }
  4823. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  4824. }
  4825. template <int mmq_y>
  4826. static __dpct_inline__ void
  4827. allocate_tiles_q4_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  4828. int *tile_x_qs_q4_0, float *tile_x_d_q4_0) {
  4829. (void)x_qh; (void)x_sc;
  4830. *x_ql = tile_x_qs_q4_0;
  4831. *x_dm = (sycl::half2 *)tile_x_d_q4_0;
  4832. }
  4833. template <int mmq_y, int nwarps, bool need_check>
  4834. static __dpct_inline__ void
  4835. load_tiles_q4_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  4836. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  4837. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  4838. const int &k, const int &blocks_per_row) {
  4839. (void)x_qh; (void)x_sc;
  4840. GGML_SYCL_ASSUME(i_offset >= 0);
  4841. GGML_SYCL_ASSUME(i_offset < nwarps);
  4842. GGML_SYCL_ASSUME(k >= 0);
  4843. GGML_SYCL_ASSUME(k < WARP_SIZE);
  4844. const int kbx = k / QI4_0;
  4845. const int kqsx = k % QI4_0;
  4846. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  4847. float * x_dmf = (float *) x_dm;
  4848. #pragma unroll
  4849. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  4850. int i = i0 + i_offset;
  4851. if (need_check) {
  4852. i = sycl::min(i, i_max);
  4853. }
  4854. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  4855. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  4856. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  4857. }
  4858. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  4859. const int kbxd = k % blocks_per_tile_x_row;
  4860. #pragma unroll
  4861. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  4862. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  4863. if (need_check) {
  4864. i = sycl::min(i, i_max);
  4865. }
  4866. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  4867. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  4868. }
  4869. }
  4870. static __dpct_inline__ float vec_dot_q4_0_q8_1_mul_mat(
  4871. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  4872. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  4873. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  4874. const int &i, const int &j, const int &k) {
  4875. (void)x_qh; (void)x_sc;
  4876. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  4877. const float * x_dmf = (const float *) x_dm;
  4878. int u[2*VDR_Q4_0_Q8_1_MMQ];
  4879. #pragma unroll
  4880. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  4881. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  4882. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  4883. }
  4884. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  4885. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  4886. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  4887. }
  4888. static __dpct_inline__ float
  4889. vec_dot_q4_1_q8_1(const void *__restrict__ vbq,
  4890. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  4891. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  4892. int v[VDR_Q4_1_Q8_1_MMVQ];
  4893. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  4894. #pragma unroll
  4895. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  4896. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  4897. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  4898. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  4899. }
  4900. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  4901. }
  4902. template <int mmq_y>
  4903. static __dpct_inline__ void
  4904. allocate_tiles_q4_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  4905. int *tile_x_qs_q4_1, sycl::half2 *tile_x_dm_q4_1) {
  4906. (void)x_qh; (void)x_sc;
  4907. *x_ql = tile_x_qs_q4_1;
  4908. *x_dm = tile_x_dm_q4_1;
  4909. }
  4910. template <int mmq_y, int nwarps, bool need_check>
  4911. static __dpct_inline__ void
  4912. load_tiles_q4_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  4913. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  4914. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  4915. const int &k, const int &blocks_per_row) {
  4916. (void)x_qh; (void)x_sc;
  4917. GGML_SYCL_ASSUME(i_offset >= 0);
  4918. GGML_SYCL_ASSUME(i_offset < nwarps);
  4919. GGML_SYCL_ASSUME(k >= 0);
  4920. GGML_SYCL_ASSUME(k < WARP_SIZE);
  4921. const int kbx = k / QI4_1;
  4922. const int kqsx = k % QI4_1;
  4923. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  4924. #pragma unroll
  4925. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  4926. int i = i0 + i_offset;
  4927. if (need_check) {
  4928. i = sycl::min(i, i_max);
  4929. }
  4930. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  4931. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  4932. }
  4933. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  4934. const int kbxd = k % blocks_per_tile_x_row;
  4935. #pragma unroll
  4936. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  4937. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  4938. if (need_check) {
  4939. i = sycl::min(i, i_max);
  4940. }
  4941. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  4942. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  4943. }
  4944. }
  4945. static __dpct_inline__ float vec_dot_q4_1_q8_1_mul_mat(
  4946. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  4947. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  4948. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  4949. const int &i, const int &j, const int &k) {
  4950. (void)x_qh; (void)x_sc;
  4951. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  4952. int u[2*VDR_Q4_1_Q8_1_MMQ];
  4953. #pragma unroll
  4954. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  4955. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  4956. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  4957. }
  4958. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  4959. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  4960. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  4961. }
  4962. static __dpct_inline__ float
  4963. vec_dot_q5_0_q8_1(const void *__restrict__ vbq,
  4964. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  4965. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  4966. int vl[VDR_Q5_0_Q8_1_MMVQ];
  4967. int vh[VDR_Q5_0_Q8_1_MMVQ];
  4968. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  4969. #pragma unroll
  4970. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  4971. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  4972. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  4973. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  4974. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  4975. }
  4976. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  4977. }
  4978. template <int mmq_y>
  4979. static __dpct_inline__ void
  4980. allocate_tiles_q5_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  4981. int *tile_x_ql_q5_0, float *tile_x_d_q5_0) {
  4982. (void)x_qh; (void)x_sc;
  4983. *x_ql = tile_x_ql_q5_0;
  4984. *x_dm = (sycl::half2 *)tile_x_d_q5_0;
  4985. }
  4986. template <int mmq_y, int nwarps, bool need_check>
  4987. static __dpct_inline__ void
  4988. load_tiles_q5_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  4989. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  4990. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  4991. const int &k, const int &blocks_per_row) {
  4992. (void)x_qh; (void)x_sc;
  4993. GGML_SYCL_ASSUME(i_offset >= 0);
  4994. GGML_SYCL_ASSUME(i_offset < nwarps);
  4995. GGML_SYCL_ASSUME(k >= 0);
  4996. GGML_SYCL_ASSUME(k < WARP_SIZE);
  4997. const int kbx = k / QI5_0;
  4998. const int kqsx = k % QI5_0;
  4999. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  5000. #pragma unroll
  5001. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5002. int i = i0 + i_offset;
  5003. if (need_check) {
  5004. i = sycl::min(i, i_max);
  5005. }
  5006. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5007. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  5008. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  5009. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5010. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5011. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5012. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5013. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5014. qs0 = dpct::vectorized_binary<sycl::char4>(
  5015. qs0, 0x10101010, dpct::sub_sat()); // subtract 16
  5016. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5017. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5018. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5019. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5020. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5021. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5022. qs1 = dpct::vectorized_binary<sycl::char4>(
  5023. qs1, 0x10101010, dpct::sub_sat()); // subtract 16
  5024. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5025. }
  5026. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  5027. const int kbxd = k % blocks_per_tile_x_row;
  5028. float * x_dmf = (float *) x_dm;
  5029. #pragma unroll
  5030. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  5031. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  5032. if (need_check) {
  5033. i = sycl::min(i, i_max);
  5034. }
  5035. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5036. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  5037. }
  5038. }
  5039. static __dpct_inline__ float vec_dot_q5_0_q8_1_mul_mat(
  5040. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5041. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5042. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5043. const int &i, const int &j, const int &k) {
  5044. (void)x_qh; (void)x_sc;
  5045. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5046. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  5047. const float * x_dmf = (const float *) x_dm;
  5048. const float * y_df = (const float *) y_ds;
  5049. int u[2*VDR_Q5_0_Q8_1_MMQ];
  5050. #pragma unroll
  5051. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  5052. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5053. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  5054. }
  5055. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  5056. (&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)]);
  5057. }
  5058. static __dpct_inline__ float
  5059. vec_dot_q5_1_q8_1(const void *__restrict__ vbq,
  5060. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5061. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  5062. int vl[VDR_Q5_1_Q8_1_MMVQ];
  5063. int vh[VDR_Q5_1_Q8_1_MMVQ];
  5064. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  5065. #pragma unroll
  5066. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  5067. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  5068. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  5069. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5070. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  5071. }
  5072. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  5073. }
  5074. template <int mmq_y>
  5075. static __dpct_inline__ void
  5076. allocate_tiles_q5_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5077. int *tile_x_ql_q5_1, sycl::half2 *tile_x_dm_q5_1) {
  5078. (void)x_qh; (void)x_sc;
  5079. *x_ql = tile_x_ql_q5_1;
  5080. *x_dm = tile_x_dm_q5_1;
  5081. }
  5082. template <int mmq_y, int nwarps, bool need_check>
  5083. static __dpct_inline__ void
  5084. load_tiles_q5_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5085. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5086. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5087. const int &k, const int &blocks_per_row) {
  5088. (void)x_qh; (void)x_sc;
  5089. GGML_SYCL_ASSUME(i_offset >= 0);
  5090. GGML_SYCL_ASSUME(i_offset < nwarps);
  5091. GGML_SYCL_ASSUME(k >= 0);
  5092. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5093. const int kbx = k / QI5_1;
  5094. const int kqsx = k % QI5_1;
  5095. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  5096. #pragma unroll
  5097. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5098. int i = i0 + i_offset;
  5099. if (need_check) {
  5100. i = sycl::min(i, i_max);
  5101. }
  5102. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5103. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5104. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  5105. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5106. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5107. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5108. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5109. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5110. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5111. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5112. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5113. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5114. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5115. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5116. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5117. }
  5118. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  5119. const int kbxd = k % blocks_per_tile_x_row;
  5120. #pragma unroll
  5121. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  5122. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  5123. if (need_check) {
  5124. i = sycl::min(i, i_max);
  5125. }
  5126. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5127. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  5128. }
  5129. }
  5130. static __dpct_inline__ float vec_dot_q5_1_q8_1_mul_mat(
  5131. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5132. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5133. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5134. const int &i, const int &j, const int &k) {
  5135. (void)x_qh; (void)x_sc;
  5136. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5137. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  5138. int u[2*VDR_Q5_1_Q8_1_MMQ];
  5139. #pragma unroll
  5140. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  5141. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5142. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  5143. }
  5144. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  5145. (&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)]);
  5146. }
  5147. static __dpct_inline__ float
  5148. vec_dot_q8_0_q8_1(const void *__restrict__ vbq,
  5149. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5150. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  5151. int v[VDR_Q8_0_Q8_1_MMVQ];
  5152. int u[VDR_Q8_0_Q8_1_MMVQ];
  5153. #pragma unroll
  5154. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  5155. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  5156. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5157. }
  5158. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d,
  5159. bq8_1->ds[0]);
  5160. }
  5161. template <int mmq_y>
  5162. static __dpct_inline__ void
  5163. allocate_tiles_q8_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5164. int *tile_x_qs_q8_0, float *tile_x_d_q8_0) {
  5165. (void)x_qh; (void)x_sc;
  5166. *x_ql = tile_x_qs_q8_0;
  5167. *x_dm = (sycl::half2 *)tile_x_d_q8_0;
  5168. }
  5169. template <int mmq_y, int nwarps, bool need_check>
  5170. static __dpct_inline__ void
  5171. load_tiles_q8_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5172. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5173. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5174. const int &k, const int &blocks_per_row) {
  5175. (void)x_qh; (void)x_sc;
  5176. GGML_SYCL_ASSUME(i_offset >= 0);
  5177. GGML_SYCL_ASSUME(i_offset < nwarps);
  5178. GGML_SYCL_ASSUME(k >= 0);
  5179. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5180. const int kbx = k / QI8_0;
  5181. const int kqsx = k % QI8_0;
  5182. float * x_dmf = (float *) x_dm;
  5183. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  5184. #pragma unroll
  5185. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5186. int i = i0 + i_offset;
  5187. if (need_check) {
  5188. i = sycl::min(i, i_max);
  5189. }
  5190. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5191. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  5192. }
  5193. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  5194. const int kbxd = k % blocks_per_tile_x_row;
  5195. #pragma unroll
  5196. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  5197. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  5198. if (need_check) {
  5199. i = sycl::min(i, i_max);
  5200. }
  5201. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5202. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  5203. }
  5204. }
  5205. static __dpct_inline__ float vec_dot_q8_0_q8_1_mul_mat(
  5206. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5207. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5208. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5209. const int &i, const int &j, const int &k) {
  5210. (void)x_qh; (void)x_sc;
  5211. const float * x_dmf = (const float *) x_dm;
  5212. const float * y_df = (const float *) y_ds;
  5213. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  5214. (&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],
  5215. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  5216. }
  5217. static __dpct_inline__ float
  5218. vec_dot_q2_K_q8_1(const void *__restrict__ vbq,
  5219. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5220. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  5221. const int bq8_offset = QR2_K * (iqs / QI8_1);
  5222. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5223. const uint8_t * scales = bq2_K->scales + scale_offset;
  5224. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  5225. int u[QR2_K];
  5226. float d8[QR2_K];
  5227. #pragma unroll
  5228. for (int i = 0; i < QR2_K; ++ i) {
  5229. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5230. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5231. }
  5232. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  5233. }
  5234. template <int mmq_y>
  5235. static __dpct_inline__ void
  5236. allocate_tiles_q2_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5237. int *tile_x_ql_q2_K, sycl::half2 *tile_x_dm_q2_K,
  5238. int *tile_x_sc_q2_K) {
  5239. (void)x_qh;
  5240. *x_ql = tile_x_ql_q2_K;
  5241. *x_dm = tile_x_dm_q2_K;
  5242. *x_sc = tile_x_sc_q2_K;
  5243. }
  5244. template <int mmq_y, int nwarps, bool need_check>
  5245. static __dpct_inline__ void
  5246. load_tiles_q2_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5247. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5248. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5249. const int &k, const int &blocks_per_row) {
  5250. (void)x_qh;
  5251. GGML_SYCL_ASSUME(i_offset >= 0);
  5252. GGML_SYCL_ASSUME(i_offset < nwarps);
  5253. GGML_SYCL_ASSUME(k >= 0);
  5254. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5255. const int kbx = k / QI2_K;
  5256. const int kqsx = k % QI2_K;
  5257. const block_q2_K * bx0 = (const block_q2_K *) vx;
  5258. #pragma unroll
  5259. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5260. int i = i0 + i_offset;
  5261. if (need_check) {
  5262. i = sycl::min(i, i_max);
  5263. }
  5264. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  5265. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5266. }
  5267. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  5268. const int kbxd = k % blocks_per_tile_x_row;
  5269. #pragma unroll
  5270. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  5271. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  5272. if (need_check) {
  5273. i = sycl::min(i, i_max);
  5274. }
  5275. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5276. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  5277. }
  5278. #pragma unroll
  5279. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5280. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5281. if (need_check) {
  5282. i = sycl::min(i, i_max);
  5283. }
  5284. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  5285. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  5286. }
  5287. }
  5288. static __dpct_inline__ float vec_dot_q2_K_q8_1_mul_mat(
  5289. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5290. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5291. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5292. const int &i, const int &j, const int &k) {
  5293. (void)x_qh;
  5294. const int kbx = k / QI2_K;
  5295. const int ky = (k % QI2_K) * QR2_K;
  5296. const float * y_df = (const float *) y_ds;
  5297. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  5298. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  5299. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  5300. #pragma unroll
  5301. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  5302. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5303. }
  5304. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  5305. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  5306. 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]);
  5307. }
  5308. static __dpct_inline__ float
  5309. vec_dot_q3_K_q8_1(const void *__restrict__ vbq,
  5310. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5311. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  5312. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  5313. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5314. const float d = bq3_K->d;
  5315. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  5316. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5317. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  5318. int u[QR3_K];
  5319. float d8[QR3_K];
  5320. #pragma unroll
  5321. for (int i = 0; i < QR3_K; ++i) {
  5322. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5323. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5324. }
  5325. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  5326. }
  5327. template <int mmq_y>
  5328. static __dpct_inline__ void
  5329. allocate_tiles_q3_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5330. int *tile_x_ql_q3_K, sycl::half2 *tile_x_dm_q3_K,
  5331. int *tile_x_qh_q3_K, int *tile_x_sc_q3_K) {
  5332. *x_ql = tile_x_ql_q3_K;
  5333. *x_dm = tile_x_dm_q3_K;
  5334. *x_qh = tile_x_qh_q3_K;
  5335. *x_sc = tile_x_sc_q3_K;
  5336. }
  5337. template <int mmq_y, int nwarps, bool need_check>
  5338. static __dpct_inline__ void
  5339. load_tiles_q3_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5340. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5341. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5342. const int &k, const int &blocks_per_row) {
  5343. GGML_SYCL_ASSUME(i_offset >= 0);
  5344. GGML_SYCL_ASSUME(i_offset < nwarps);
  5345. GGML_SYCL_ASSUME(k >= 0);
  5346. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5347. const int kbx = k / QI3_K;
  5348. const int kqsx = k % QI3_K;
  5349. const block_q3_K * bx0 = (const block_q3_K *) vx;
  5350. #pragma unroll
  5351. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5352. int i = i0 + i_offset;
  5353. if (need_check) {
  5354. i = sycl::min(i, i_max);
  5355. }
  5356. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  5357. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5358. }
  5359. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  5360. const int kbxd = k % blocks_per_tile_x_row;
  5361. float * x_dmf = (float *) x_dm;
  5362. #pragma unroll
  5363. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  5364. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  5365. if (need_check) {
  5366. i = sycl::min(i, i_max);
  5367. }
  5368. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5369. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  5370. }
  5371. #pragma unroll
  5372. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  5373. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  5374. if (need_check) {
  5375. i = sycl::min(i, i_max);
  5376. }
  5377. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  5378. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5379. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  5380. }
  5381. #pragma unroll
  5382. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5383. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  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/4)) / (QI3_K/4);
  5388. const int ksc = k % (QI3_K/4);
  5389. const int ksc_low = ksc % (QI3_K/8);
  5390. const int shift_low = 4 * (ksc / (QI3_K/8));
  5391. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  5392. const int ksc_high = QI3_K/8;
  5393. const int shift_high = 2 * ksc;
  5394. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  5395. const int sc = dpct::vectorized_binary<sycl::char4>(
  5396. sc_low | sc_high, 0x20202020, dpct::sub_sat());
  5397. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  5398. }
  5399. }
  5400. static __dpct_inline__ float vec_dot_q3_K_q8_1_mul_mat(
  5401. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5402. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5403. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5404. const int &i, const int &j, const int &k) {
  5405. const int kbx = k / QI3_K;
  5406. const int ky = (k % QI3_K) * QR3_K;
  5407. const float * x_dmf = (const float *) x_dm;
  5408. const float * y_df = (const float *) y_ds;
  5409. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  5410. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  5411. #pragma unroll
  5412. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  5413. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  5414. const int shift = 2 * ((ky % 32) / 8);
  5415. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5416. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  5417. const int vlh = (vh << 2) & 0x04040404;
  5418. v[l] = dpct::vectorized_binary<sycl::char4>(vll, vlh, dpct::sub_sat());
  5419. }
  5420. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  5421. 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]);
  5422. }
  5423. static __dpct_inline__ float
  5424. vec_dot_q4_K_q8_1(const void *__restrict__ vbq,
  5425. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5426. #ifndef GGML_QKK_64
  5427. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5428. int v[2];
  5429. int u[2*QR4_K];
  5430. float d8[QR4_K];
  5431. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  5432. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  5433. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  5434. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  5435. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  5436. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  5437. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5438. v[0] = q4[0];
  5439. v[1] = q4[4];
  5440. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  5441. uint16_t aux[2];
  5442. const int j = bq8_offset/2;
  5443. if (j < 2) {
  5444. aux[0] = scales[j+0] & 0x3f3f;
  5445. aux[1] = scales[j+2] & 0x3f3f;
  5446. } else {
  5447. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5448. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5449. }
  5450. const uint8_t * sc = (const uint8_t *)aux;
  5451. const uint8_t * m = sc + 2;
  5452. for (int i = 0; i < QR4_K; ++i) {
  5453. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5454. d8[i] = bq8i->ds[0];
  5455. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5456. u[2*i+0] = q8[0];
  5457. u[2*i+1] = q8[4];
  5458. }
  5459. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  5460. #else
  5461. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  5462. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5463. float sumf_d = 0.0f;
  5464. float sumf_m = 0.0f;
  5465. uint16_t aux16[2];
  5466. const uint8_t * s = (const uint8_t *)aux16;
  5467. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  5468. aux16[0] = a[0] & 0x0f0f;
  5469. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5470. const float dall = bq4_K->dm[0];
  5471. const float dmin = bq4_K->dm[1];
  5472. const float d8_1 = __low2float(bq8_1[0].ds);
  5473. const float d8_2 = __low2float(bq8_1[1].ds);
  5474. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  5475. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  5476. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  5477. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  5478. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  5479. const int v1 = q4[0];
  5480. const int v2 = q4[4];
  5481. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  5482. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  5483. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  5484. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  5485. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  5486. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  5487. return dall * sumf_d - dmin * sumf_m;
  5488. #else
  5489. bad_arch();
  5490. #endif // __SYCL_ARCH__ >= VER_4VEC
  5491. #endif
  5492. }
  5493. template <int mmq_y>
  5494. static __dpct_inline__ void
  5495. allocate_tiles_q4_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5496. int *tile_x_ql_q4_K, sycl::half2 *tile_x_dm_q4_K,
  5497. int *tile_x_sc_q4_K) {
  5498. (void)x_qh;
  5499. *x_ql = tile_x_ql_q4_K;
  5500. *x_dm = tile_x_dm_q4_K;
  5501. *x_sc = tile_x_sc_q4_K;
  5502. }
  5503. template <int mmq_y, int nwarps, bool need_check>
  5504. static __dpct_inline__ void
  5505. load_tiles_q4_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5506. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5507. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5508. const int &k, const int &blocks_per_row) {
  5509. (void)x_qh;
  5510. GGML_SYCL_ASSUME(i_offset >= 0);
  5511. GGML_SYCL_ASSUME(i_offset < nwarps);
  5512. GGML_SYCL_ASSUME(k >= 0);
  5513. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5514. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  5515. const int kqsx = k % QI4_K; // == k if QK_K == 256
  5516. const block_q4_K * bx0 = (const block_q4_K *) vx;
  5517. #pragma unroll
  5518. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5519. int i = i0 + i_offset;
  5520. if (need_check) {
  5521. i = sycl::min(i, i_max);
  5522. }
  5523. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  5524. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5525. }
  5526. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  5527. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5528. #pragma unroll
  5529. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  5530. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  5531. if (need_check) {
  5532. i = sycl::min(i, i_max);
  5533. }
  5534. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5535. #if QK_K == 256
  5536. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  5537. #else
  5538. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  5539. #endif
  5540. }
  5541. #pragma unroll
  5542. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5543. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5544. if (need_check) {
  5545. i = sycl::min(i, i_max);
  5546. }
  5547. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  5548. const int * scales = (const int *) bxi->scales;
  5549. const int ksc = k % (WARP_SIZE/8);
  5550. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5551. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5552. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5553. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5554. }
  5555. }
  5556. static __dpct_inline__ float vec_dot_q4_K_q8_1_mul_mat(
  5557. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5558. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5559. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5560. const int &i, const int &j, const int &k) {
  5561. (void)x_qh;
  5562. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  5563. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  5564. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  5565. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  5566. }
  5567. static __dpct_inline__ float
  5568. vec_dot_q5_K_q8_1(const void *__restrict__ vbq,
  5569. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5570. #ifndef GGML_QKK_64
  5571. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  5572. int vl[2];
  5573. int vh[2];
  5574. int u[2*QR5_K];
  5575. float d8[QR5_K];
  5576. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  5577. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5578. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  5579. vl[0] = ql[0];
  5580. vl[1] = ql[4];
  5581. vh[0] = qh[0] >> bq8_offset;
  5582. vh[1] = qh[4] >> bq8_offset;
  5583. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  5584. uint16_t aux[2];
  5585. const int j = bq8_offset/2;
  5586. if (j < 2) {
  5587. aux[0] = scales[j+0] & 0x3f3f;
  5588. aux[1] = scales[j+2] & 0x3f3f;
  5589. } else {
  5590. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5591. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5592. }
  5593. const uint8_t * sc = (const uint8_t *)aux;
  5594. const uint8_t * m = sc + 2;
  5595. #pragma unroll
  5596. for (int i = 0; i < QR5_K; ++i) {
  5597. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5598. d8[i] = bq8i->ds[0];
  5599. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5600. u[2*i+0] = q8[0];
  5601. u[2*i+1] = q8[4];
  5602. }
  5603. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  5604. #else
  5605. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  5606. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  5607. const int8_t * s = bq5_K->scales;
  5608. const float d = bq5_K->d;
  5609. const float d8_1 = __low2half(bq8_1[0].ds);
  5610. const float d8_2 = __low2half(bq8_1[1].ds);
  5611. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  5612. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  5613. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  5614. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  5615. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  5616. const int vl1 = ql[0];
  5617. const int vl2 = ql[4];
  5618. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  5619. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  5620. const int in = step%8; // 0, 4, 0, 4
  5621. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  5622. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  5623. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  5624. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  5625. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  5626. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  5627. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  5628. return d * sumf_d;
  5629. #else
  5630. bad_arch();
  5631. #endif // __SYCL_ARCH__ >= VER_4VEC
  5632. #endif
  5633. }
  5634. template <int mmq_y>
  5635. static __dpct_inline__ void
  5636. allocate_tiles_q5_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5637. int *tile_x_ql_q5_K, sycl::half2 *tile_x_dm_q5_K,
  5638. int *tile_x_sc_q5_K) {
  5639. (void)x_qh;
  5640. *x_ql = tile_x_ql_q5_K;
  5641. *x_dm = tile_x_dm_q5_K;
  5642. *x_sc = tile_x_sc_q5_K;
  5643. }
  5644. template <int mmq_y, int nwarps, bool need_check>
  5645. static __dpct_inline__ void
  5646. load_tiles_q5_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5647. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5648. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5649. const int &k, const int &blocks_per_row) {
  5650. (void)x_qh;
  5651. GGML_SYCL_ASSUME(i_offset >= 0);
  5652. GGML_SYCL_ASSUME(i_offset < nwarps);
  5653. GGML_SYCL_ASSUME(k >= 0);
  5654. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5655. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  5656. const int kqsx = k % QI5_K; // == k if QK_K == 256
  5657. const block_q5_K * bx0 = (const block_q5_K *) vx;
  5658. #pragma unroll
  5659. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5660. int i = i0 + i_offset;
  5661. if (need_check) {
  5662. i = sycl::min(i, i_max);
  5663. }
  5664. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  5665. const int ky = QR5_K*kqsx;
  5666. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5667. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  5668. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  5669. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  5670. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  5671. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  5672. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  5673. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  5674. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  5675. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  5676. }
  5677. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  5678. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5679. #pragma unroll
  5680. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  5681. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  5682. if (need_check) {
  5683. i = sycl::min(i, i_max);
  5684. }
  5685. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5686. #if QK_K == 256
  5687. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  5688. #endif
  5689. }
  5690. #pragma unroll
  5691. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5692. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5693. if (need_check) {
  5694. i = sycl::min(i, i_max);
  5695. }
  5696. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  5697. const int * scales = (const int *) bxi->scales;
  5698. const int ksc = k % (WARP_SIZE/8);
  5699. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5700. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5701. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5702. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5703. }
  5704. }
  5705. static __dpct_inline__ float vec_dot_q5_K_q8_1_mul_mat(
  5706. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5707. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5708. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5709. const int &i, const int &j, const int &k) {
  5710. (void)x_qh;
  5711. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  5712. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  5713. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  5714. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  5715. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  5716. }
  5717. static __dpct_inline__ float
  5718. vec_dot_q6_K_q8_1(const void *__restrict__ vbq,
  5719. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5720. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  5721. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  5722. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  5723. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  5724. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  5725. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  5726. const int8_t * scales = bq6_K->scales + scale_offset;
  5727. int u[QR6_K];
  5728. float d8[QR6_K];
  5729. #pragma unroll
  5730. for (int i = 0; i < QR6_K; ++i) {
  5731. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  5732. d8[i] = bq8_1[bq8_offset + 2 * i].ds[0];
  5733. }
  5734. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  5735. }
  5736. template <int mmq_y>
  5737. static __dpct_inline__ void
  5738. allocate_tiles_q6_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5739. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_sc) {
  5740. (void)x_qh;
  5741. *x_ql = tile_x_ql;
  5742. *x_dm = tile_x_dm;
  5743. *x_sc = tile_x_sc;
  5744. }
  5745. template <int mmq_y, int nwarps, bool need_check>
  5746. static __dpct_inline__ void
  5747. load_tiles_q6_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5748. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5749. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5750. const int &k, const int &blocks_per_row) {
  5751. (void)x_qh;
  5752. GGML_SYCL_ASSUME(i_offset >= 0);
  5753. GGML_SYCL_ASSUME(i_offset < nwarps);
  5754. GGML_SYCL_ASSUME(k >= 0);
  5755. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5756. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  5757. const int kqsx = k % QI6_K; // == k if QK_K == 256
  5758. const block_q6_K * bx0 = (const block_q6_K *) vx;
  5759. #pragma unroll
  5760. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5761. int i = i0 + i_offset;
  5762. if (need_check) {
  5763. i = sycl::min(i, i_max);
  5764. }
  5765. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  5766. const int ky = QR6_K*kqsx;
  5767. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  5768. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  5769. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  5770. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  5771. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  5772. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  5773. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  5774. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  5775. x_ql[i * (2 * WARP_SIZE + 1) + kq0] =
  5776. dpct::vectorized_binary<sycl::char4>(ql0 | qh0, 0x20202020,
  5777. dpct::sub_sat());
  5778. x_ql[i * (2 * WARP_SIZE + 1) + kq1] =
  5779. dpct::vectorized_binary<sycl::char4>(ql1 | qh1, 0x20202020,
  5780. dpct::sub_sat());
  5781. }
  5782. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  5783. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5784. float * x_dmf = (float *) x_dm;
  5785. #pragma unroll
  5786. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  5787. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  5788. if (need_check) {
  5789. i = sycl::min(i, i_max);
  5790. }
  5791. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5792. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  5793. }
  5794. #pragma unroll
  5795. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5796. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5797. if (need_check) {
  5798. i = sycl::min(i, i_max);
  5799. }
  5800. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  5801. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  5802. }
  5803. }
  5804. static __dpct_inline__ float vec_dot_q6_K_q8_1_mul_mat(
  5805. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5806. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5807. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5808. const int &i, const int &j, const int &k) {
  5809. (void)x_qh;
  5810. const float * x_dmf = (const float *) x_dm;
  5811. const float * y_df = (const float *) y_ds;
  5812. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  5813. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  5814. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  5815. 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]);
  5816. }
  5817. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x,
  5818. int mmq_y, int nwarps, load_tiles_sycl_t load_tiles, int vdr,
  5819. vec_dot_q_mul_mat_sycl_t vec_dot>
  5820. /*
  5821. DPCT1110:8: The total declared local variable size in device function mul_mat_q
  5822. exceeds 128 bytes and may cause high register pressure. Consult with your
  5823. hardware vendor to find the total register size available and adjust the code,
  5824. or use smaller sub-group size to avoid high register pressure.
  5825. */
  5826. static __dpct_inline__ void
  5827. mul_mat_q(const void *__restrict__ vx, const void *__restrict__ vy,
  5828. float *__restrict__ dst, const int ncols_x, const int nrows_x,
  5829. const int ncols_y, const int nrows_y, const int nrows_dst,
  5830. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_qh,
  5831. int *tile_x_sc, const sycl::nd_item<3> &item_ct1, int *tile_y_qs,
  5832. sycl::half2 *tile_y_ds) {
  5833. const block_q_t * x = (const block_q_t *) vx;
  5834. const block_q8_1 * y = (const block_q8_1 *) vy;
  5835. const int blocks_per_row_x = ncols_x / qk;
  5836. const int blocks_per_col_y = nrows_y / QK8_1;
  5837. const int blocks_per_warp = WARP_SIZE / qi;
  5838. const int & ncols_dst = ncols_y;
  5839. const int row_dst_0 = item_ct1.get_group(2) * mmq_y;
  5840. const int & row_x_0 = row_dst_0;
  5841. const int col_dst_0 = item_ct1.get_group(1) * mmq_x;
  5842. const int & col_y_0 = col_dst_0;
  5843. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  5844. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  5845. load_tiles(x + row_x_0 * blocks_per_row_x + ib0, tile_x_ql, tile_x_dm,
  5846. tile_x_qh, tile_x_sc, item_ct1.get_local_id(1),
  5847. nrows_x - row_x_0 - 1, item_ct1.get_local_id(2),
  5848. blocks_per_row_x);
  5849. #pragma unroll
  5850. for (int ir = 0; ir < qr; ++ir) {
  5851. const int kqs = ir * WARP_SIZE + item_ct1.get_local_id(2);
  5852. const int kbxd = kqs / QI8_1;
  5853. #pragma unroll
  5854. for (int i = 0; i < mmq_x; i += nwarps) {
  5855. const int col_y_eff = dpct::min(
  5856. (unsigned int)(col_y_0 + item_ct1.get_local_id(1) + i),
  5857. ncols_y - 1); // to prevent out-of-bounds memory accesses
  5858. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  5859. const int index_y = (item_ct1.get_local_id(1) + i) * WARP_SIZE +
  5860. kqs % WARP_SIZE;
  5861. tile_y_qs[index_y] = get_int_from_int8_aligned(
  5862. by0->qs, item_ct1.get_local_id(2) % QI8_1);
  5863. }
  5864. #pragma unroll
  5865. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  5866. const int ids =
  5867. (ids0 + item_ct1.get_local_id(1) * QI8_1 +
  5868. item_ct1.get_local_id(2) / (WARP_SIZE / QI8_1)) %
  5869. mmq_x;
  5870. const int kby = item_ct1.get_local_id(2) % (WARP_SIZE / QI8_1);
  5871. const int col_y_eff = sycl::min(col_y_0 + ids, ncols_y - 1);
  5872. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  5873. const sycl::half2 *dsi_src =
  5874. &y[col_y_eff * blocks_per_col_y + ib0 * (qk / QK8_1) +
  5875. ir * (WARP_SIZE / QI8_1) + kby]
  5876. .ds;
  5877. sycl::half2 *dsi_dst =
  5878. &tile_y_ds[ids * (WARP_SIZE / QI8_1) + kby];
  5879. if (need_sum) {
  5880. *dsi_dst = *dsi_src;
  5881. } else {
  5882. float * dfi_dst = (float *) dsi_dst;
  5883. *dfi_dst = (*dsi_src)[0];
  5884. }
  5885. }
  5886. /*
  5887. DPCT1118:9: SYCL group functions and algorithms must be encountered
  5888. in converged control flow. You may need to adjust the code.
  5889. */
  5890. /*
  5891. DPCT1065:56: Consider replacing sycl::nd_item::barrier() with
  5892. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  5893. better performance if there is no access to global memory.
  5894. */
  5895. item_ct1.barrier();
  5896. // #pragma unroll // unrolling this loop causes too much register pressure
  5897. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  5898. #pragma unroll
  5899. for (int j = 0; j < mmq_x; j += nwarps) {
  5900. #pragma unroll
  5901. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  5902. sum[i / WARP_SIZE][j / nwarps] += vec_dot(
  5903. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  5904. tile_y_qs, tile_y_ds, item_ct1.get_local_id(2) + i,
  5905. item_ct1.get_local_id(1) + j, k);
  5906. }
  5907. }
  5908. }
  5909. /*
  5910. DPCT1118:10: SYCL group functions and algorithms must be encountered
  5911. in converged control flow. You may need to adjust the code.
  5912. */
  5913. /*
  5914. DPCT1065:57: Consider replacing sycl::nd_item::barrier() with
  5915. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  5916. better performance if there is no access to global memory.
  5917. */
  5918. item_ct1.barrier();
  5919. }
  5920. }
  5921. #pragma unroll
  5922. for (int j = 0; j < mmq_x; j += nwarps) {
  5923. const int col_dst = col_dst_0 + j + item_ct1.get_local_id(1);
  5924. if (col_dst >= ncols_dst) {
  5925. return;
  5926. }
  5927. #pragma unroll
  5928. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  5929. const int row_dst = row_dst_0 + item_ct1.get_local_id(2) + i;
  5930. if (row_dst >= nrows_dst) {
  5931. continue;
  5932. }
  5933. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  5934. }
  5935. }
  5936. }
  5937. #define MMQ_X_Q4_0_RDNA2 64
  5938. #define MMQ_Y_Q4_0_RDNA2 128
  5939. #define NWARPS_Q4_0_RDNA2 8
  5940. #define MMQ_X_Q4_0_RDNA1 64
  5941. #define MMQ_Y_Q4_0_RDNA1 64
  5942. #define NWARPS_Q4_0_RDNA1 8
  5943. #if defined(SYCL_USE_XMX)
  5944. #define MMQ_X_Q4_0_AMPERE 4
  5945. #define MMQ_Y_Q4_0_AMPERE 32
  5946. #define NWARPS_Q4_0_AMPERE 4
  5947. #else
  5948. #define MMQ_X_Q4_0_AMPERE 64
  5949. #define MMQ_Y_Q4_0_AMPERE 128
  5950. #define NWARPS_Q4_0_AMPERE 4
  5951. #endif
  5952. #define MMQ_X_Q4_0_PASCAL 64
  5953. #define MMQ_Y_Q4_0_PASCAL 64
  5954. #define NWARPS_Q4_0_PASCAL 8
  5955. template <bool need_check> static void
  5956. mul_mat_q4_0(
  5957. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  5958. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  5959. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_0, float *tile_x_d_q4_0,
  5960. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  5961. int * tile_x_ql = nullptr;
  5962. sycl::half2 *tile_x_dm = nullptr;
  5963. int * tile_x_qh = nullptr;
  5964. int * tile_x_sc = nullptr;
  5965. //sycl_todo: change according to hardware
  5966. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  5967. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  5968. const int nwarps = NWARPS_Q4_0_AMPERE;
  5969. allocate_tiles_q4_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  5970. tile_x_qs_q4_0, tile_x_d_q4_0);
  5971. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps,
  5972. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ,
  5973. vec_dot_q4_0_q8_1_mul_mat>(
  5974. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  5975. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  5976. }
  5977. #define MMQ_X_Q4_1_RDNA2 64
  5978. #define MMQ_Y_Q4_1_RDNA2 128
  5979. #define NWARPS_Q4_1_RDNA2 8
  5980. #define MMQ_X_Q4_1_RDNA1 64
  5981. #define MMQ_Y_Q4_1_RDNA1 64
  5982. #define NWARPS_Q4_1_RDNA1 8
  5983. #if defined(SYCL_USE_XMX)
  5984. #define MMQ_X_Q4_1_AMPERE 4
  5985. #define MMQ_Y_Q4_1_AMPERE 32
  5986. #define NWARPS_Q4_1_AMPERE 4
  5987. #else
  5988. #define MMQ_X_Q4_1_AMPERE 64
  5989. #define MMQ_Y_Q4_1_AMPERE 128
  5990. #define NWARPS_Q4_1_AMPERE 4
  5991. #endif
  5992. #define MMQ_X_Q4_1_PASCAL 64
  5993. #define MMQ_Y_Q4_1_PASCAL 64
  5994. #define NWARPS_Q4_1_PASCAL 8
  5995. template <bool need_check> static void
  5996. mul_mat_q4_1(
  5997. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  5998. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  5999. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_1,
  6000. sycl::half2 *tile_x_dm_q4_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6001. int * tile_x_ql = nullptr;
  6002. sycl::half2 *tile_x_dm = nullptr;
  6003. int * tile_x_qh = nullptr;
  6004. int * tile_x_sc = nullptr;
  6005. //sycl_todo: change according to hardware
  6006. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  6007. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  6008. const int nwarps = NWARPS_Q4_1_AMPERE;
  6009. allocate_tiles_q4_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6010. tile_x_qs_q4_1, tile_x_dm_q4_1);
  6011. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps,
  6012. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ,
  6013. vec_dot_q4_1_q8_1_mul_mat>(
  6014. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6015. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6016. }
  6017. #define MMQ_X_Q5_0_RDNA2 64
  6018. #define MMQ_Y_Q5_0_RDNA2 128
  6019. #define NWARPS_Q5_0_RDNA2 8
  6020. #define MMQ_X_Q5_0_RDNA1 64
  6021. #define MMQ_Y_Q5_0_RDNA1 64
  6022. #define NWARPS_Q5_0_RDNA1 8
  6023. #if defined(SYCL_USE_XMX)
  6024. #define MMQ_X_Q5_0_AMPERE 4
  6025. #define MMQ_Y_Q5_0_AMPERE 32
  6026. #define NWARPS_Q5_0_AMPERE 4
  6027. #else
  6028. #define MMQ_X_Q5_0_AMPERE 128
  6029. #define MMQ_Y_Q5_0_AMPERE 64
  6030. #define NWARPS_Q5_0_AMPERE 4
  6031. #endif
  6032. #define MMQ_X_Q5_0_PASCAL 64
  6033. #define MMQ_Y_Q5_0_PASCAL 64
  6034. #define NWARPS_Q5_0_PASCAL 8
  6035. template <bool need_check> static void
  6036. mul_mat_q5_0(
  6037. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6038. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6039. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_0, float *tile_x_d_q5_0,
  6040. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6041. int * tile_x_ql = nullptr;
  6042. sycl::half2 *tile_x_dm = nullptr;
  6043. int * tile_x_qh = nullptr;
  6044. int * tile_x_sc = nullptr;
  6045. //sycl_todo: change according to hardware
  6046. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  6047. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  6048. const int nwarps = NWARPS_Q5_0_AMPERE;
  6049. allocate_tiles_q5_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6050. tile_x_ql_q5_0, tile_x_d_q5_0);
  6051. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps,
  6052. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ,
  6053. vec_dot_q5_0_q8_1_mul_mat>(
  6054. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6055. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6056. }
  6057. #define MMQ_X_Q5_1_RDNA2 64
  6058. #define MMQ_Y_Q5_1_RDNA2 128
  6059. #define NWARPS_Q5_1_RDNA2 8
  6060. #define MMQ_X_Q5_1_RDNA1 64
  6061. #define MMQ_Y_Q5_1_RDNA1 64
  6062. #define NWARPS_Q5_1_RDNA1 8
  6063. #if defined(SYCL_USE_XMX)
  6064. #define MMQ_X_Q5_1_AMPERE 4
  6065. #define MMQ_Y_Q5_1_AMPERE 32
  6066. #define NWARPS_Q5_1_AMPERE 4
  6067. #else
  6068. #define MMQ_X_Q5_1_AMPERE 128
  6069. #define MMQ_Y_Q5_1_AMPERE 64
  6070. #define NWARPS_Q5_1_AMPERE 4
  6071. #endif
  6072. #define MMQ_X_Q5_1_PASCAL 64
  6073. #define MMQ_Y_Q5_1_PASCAL 64
  6074. #define NWARPS_Q5_1_PASCAL 8
  6075. template <bool need_check> static void
  6076. mul_mat_q5_1(
  6077. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6078. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6079. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_1,
  6080. sycl::half2 *tile_x_dm_q5_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6081. int * tile_x_ql = nullptr;
  6082. sycl::half2 *tile_x_dm = nullptr;
  6083. int * tile_x_qh = nullptr;
  6084. int * tile_x_sc = nullptr;
  6085. //sycl_todo: change according to hardware
  6086. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  6087. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  6088. const int nwarps = NWARPS_Q5_1_AMPERE;
  6089. allocate_tiles_q5_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6090. tile_x_ql_q5_1, tile_x_dm_q5_1);
  6091. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps,
  6092. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ,
  6093. vec_dot_q5_1_q8_1_mul_mat>(
  6094. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6095. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6096. }
  6097. #define MMQ_X_Q8_0_RDNA2 64
  6098. #define MMQ_Y_Q8_0_RDNA2 128
  6099. #define NWARPS_Q8_0_RDNA2 8
  6100. #define MMQ_X_Q8_0_RDNA1 64
  6101. #define MMQ_Y_Q8_0_RDNA1 64
  6102. #define NWARPS_Q8_0_RDNA1 8
  6103. #if defined(SYCL_USE_XMX)
  6104. #define MMQ_X_Q8_0_AMPERE 4
  6105. #define MMQ_Y_Q8_0_AMPERE 32
  6106. #define NWARPS_Q8_0_AMPERE 4
  6107. #else
  6108. #define MMQ_X_Q8_0_AMPERE 128
  6109. #define MMQ_Y_Q8_0_AMPERE 64
  6110. #define NWARPS_Q8_0_AMPERE 4
  6111. #endif
  6112. #define MMQ_X_Q8_0_PASCAL 64
  6113. #define MMQ_Y_Q8_0_PASCAL 64
  6114. #define NWARPS_Q8_0_PASCAL 8
  6115. template <bool need_check> static void
  6116. mul_mat_q8_0(
  6117. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6118. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6119. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q8_0, float *tile_x_d_q8_0,
  6120. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6121. int * tile_x_ql = nullptr;
  6122. sycl::half2 *tile_x_dm = nullptr;
  6123. int * tile_x_qh = nullptr;
  6124. int * tile_x_sc = nullptr;
  6125. //sycl_todo: change according to hardware
  6126. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  6127. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  6128. const int nwarps = NWARPS_Q8_0_AMPERE;
  6129. allocate_tiles_q8_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6130. tile_x_qs_q8_0, tile_x_d_q8_0);
  6131. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps,
  6132. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ,
  6133. vec_dot_q8_0_q8_1_mul_mat>(
  6134. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6135. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6136. }
  6137. #define MMQ_X_Q2_K_RDNA2 64
  6138. #define MMQ_Y_Q2_K_RDNA2 128
  6139. #define NWARPS_Q2_K_RDNA2 8
  6140. #define MMQ_X_Q2_K_RDNA1 128
  6141. #define MMQ_Y_Q2_K_RDNA1 32
  6142. #define NWARPS_Q2_K_RDNA1 8
  6143. #if defined(SYCL_USE_XMX)
  6144. #define MMQ_X_Q2_K_AMPERE 4
  6145. #define MMQ_Y_Q2_K_AMPERE 32
  6146. #define NWARPS_Q2_K_AMPERE 4
  6147. #else
  6148. #define MMQ_X_Q2_K_AMPERE 64
  6149. #define MMQ_Y_Q2_K_AMPERE 128
  6150. #define NWARPS_Q2_K_AMPERE 4
  6151. #endif
  6152. #define MMQ_X_Q2_K_PASCAL 64
  6153. #define MMQ_Y_Q2_K_PASCAL 64
  6154. #define NWARPS_Q2_K_PASCAL 8
  6155. template <bool need_check> static void
  6156. mul_mat_q2_K(
  6157. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6158. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6159. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q2_K,
  6160. sycl::half2 *tile_x_dm_q2_K, int *tile_x_sc_q2_K, int *tile_y_qs,
  6161. sycl::half2 *tile_y_ds) {
  6162. int * tile_x_ql = nullptr;
  6163. sycl::half2 *tile_x_dm = nullptr;
  6164. int * tile_x_qh = nullptr;
  6165. int * tile_x_sc = nullptr;
  6166. //sycl_todo: change according to hardware
  6167. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  6168. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  6169. const int nwarps = NWARPS_Q2_K_AMPERE;
  6170. allocate_tiles_q2_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6171. tile_x_ql_q2_K, tile_x_dm_q2_K, tile_x_sc_q2_K);
  6172. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps,
  6173. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ,
  6174. vec_dot_q2_K_q8_1_mul_mat>(
  6175. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6176. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6177. }
  6178. #define MMQ_X_Q3_K_RDNA2 128
  6179. #define MMQ_Y_Q3_K_RDNA2 64
  6180. #define NWARPS_Q3_K_RDNA2 8
  6181. #define MMQ_X_Q3_K_RDNA1 32
  6182. #define MMQ_Y_Q3_K_RDNA1 128
  6183. #define NWARPS_Q3_K_RDNA1 8
  6184. #if defined(SYCL_USE_XMX)
  6185. #define MMQ_X_Q3_K_AMPERE 4
  6186. #define MMQ_Y_Q3_K_AMPERE 32
  6187. #define NWARPS_Q3_K_AMPERE 4
  6188. #else
  6189. #define MMQ_X_Q3_K_AMPERE 128
  6190. #define MMQ_Y_Q3_K_AMPERE 128
  6191. #define NWARPS_Q3_K_AMPERE 4
  6192. #endif
  6193. #define MMQ_X_Q3_K_PASCAL 64
  6194. #define MMQ_Y_Q3_K_PASCAL 64
  6195. #define NWARPS_Q3_K_PASCAL 8
  6196. template <bool need_check> static void
  6197. mul_mat_q3_K(
  6198. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6199. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6200. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q3_K,
  6201. sycl::half2 *tile_x_dm_q3_K, int *tile_x_qh_q3_K, int *tile_x_sc_q3_K,
  6202. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6203. int * tile_x_ql = nullptr;
  6204. sycl::half2 *tile_x_dm = nullptr;
  6205. int * tile_x_qh = nullptr;
  6206. int * tile_x_sc = nullptr;
  6207. //sycl_todo: change according to hardware
  6208. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  6209. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  6210. const int nwarps = NWARPS_Q3_K_AMPERE;
  6211. allocate_tiles_q3_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6212. tile_x_ql_q3_K, tile_x_dm_q3_K, tile_x_qh_q3_K,
  6213. tile_x_sc_q3_K);
  6214. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps,
  6215. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ,
  6216. vec_dot_q3_K_q8_1_mul_mat>(
  6217. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6218. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6219. }
  6220. #define MMQ_X_Q4_K_RDNA2 64
  6221. #define MMQ_Y_Q4_K_RDNA2 128
  6222. #define NWARPS_Q4_K_RDNA2 8
  6223. #define MMQ_X_Q4_K_RDNA1 32
  6224. #define MMQ_Y_Q4_K_RDNA1 64
  6225. #define NWARPS_Q4_K_RDNA1 8
  6226. #if defined(SYCL_USE_XMX)
  6227. #define MMQ_X_Q4_K_AMPERE 4
  6228. #define MMQ_Y_Q4_K_AMPERE 32
  6229. #define NWARPS_Q4_K_AMPERE 4
  6230. #else
  6231. #define MMQ_X_Q4_K_AMPERE 64
  6232. #define MMQ_Y_Q4_K_AMPERE 128
  6233. #define NWARPS_Q4_K_AMPERE 4
  6234. #endif
  6235. #define MMQ_X_Q4_K_PASCAL 64
  6236. #define MMQ_Y_Q4_K_PASCAL 64
  6237. #define NWARPS_Q4_K_PASCAL 8
  6238. template <bool need_check> static void
  6239. mul_mat_q4_K(
  6240. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6241. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6242. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q4_K,
  6243. sycl::half2 *tile_x_dm_q4_K, int *tile_x_sc_q4_K, int *tile_y_qs,
  6244. sycl::half2 *tile_y_ds) {
  6245. int * tile_x_ql = nullptr;
  6246. sycl::half2 *tile_x_dm = nullptr;
  6247. int * tile_x_qh = nullptr;
  6248. int * tile_x_sc = nullptr;
  6249. //sycl_todo: change according to hardware
  6250. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  6251. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  6252. const int nwarps = NWARPS_Q4_K_AMPERE;
  6253. allocate_tiles_q4_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6254. tile_x_ql_q4_K, tile_x_dm_q4_K, tile_x_sc_q4_K);
  6255. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps,
  6256. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ,
  6257. vec_dot_q4_K_q8_1_mul_mat>(
  6258. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6259. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6260. }
  6261. #define MMQ_X_Q5_K_RDNA2 64
  6262. #define MMQ_Y_Q5_K_RDNA2 128
  6263. #define NWARPS_Q5_K_RDNA2 8
  6264. #define MMQ_X_Q5_K_RDNA1 32
  6265. #define MMQ_Y_Q5_K_RDNA1 64
  6266. #define NWARPS_Q5_K_RDNA1 8
  6267. #if defined(SYCL_USE_XMX)
  6268. #define MMQ_X_Q5_K_AMPERE 4
  6269. #define MMQ_Y_Q5_K_AMPERE 32
  6270. #define NWARPS_Q5_K_AMPERE 4
  6271. #else
  6272. #define MMQ_X_Q5_K_AMPERE 64
  6273. #define MMQ_Y_Q5_K_AMPERE 128
  6274. #define NWARPS_Q5_K_AMPERE 4
  6275. #endif
  6276. #define MMQ_X_Q5_K_PASCAL 64
  6277. #define MMQ_Y_Q5_K_PASCAL 64
  6278. #define NWARPS_Q5_K_PASCAL 8
  6279. template <bool need_check> static void
  6280. mul_mat_q5_K(
  6281. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6282. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6283. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_K,
  6284. sycl::half2 *tile_x_dm_q5_K, int *tile_x_sc_q5_K, int *tile_y_qs,
  6285. sycl::half2 *tile_y_ds) {
  6286. int * tile_x_ql = nullptr;
  6287. sycl::half2 *tile_x_dm = nullptr;
  6288. int * tile_x_qh = nullptr;
  6289. int * tile_x_sc = nullptr;
  6290. //sycl_todo: change according to hardware
  6291. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  6292. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  6293. const int nwarps = NWARPS_Q5_K_AMPERE;
  6294. allocate_tiles_q5_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6295. tile_x_ql_q5_K, tile_x_dm_q5_K, tile_x_sc_q5_K);
  6296. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps,
  6297. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ,
  6298. vec_dot_q5_K_q8_1_mul_mat>(
  6299. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6300. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6301. }
  6302. #define MMQ_X_Q6_K_RDNA2 64
  6303. #define MMQ_Y_Q6_K_RDNA2 128
  6304. #define NWARPS_Q6_K_RDNA2 8
  6305. #define MMQ_X_Q6_K_RDNA1 32
  6306. #define MMQ_Y_Q6_K_RDNA1 64
  6307. #define NWARPS_Q6_K_RDNA1 8
  6308. #if defined(SYCL_USE_XMX)
  6309. #define MMQ_X_Q6_K_AMPERE 4
  6310. #define MMQ_Y_Q6_K_AMPERE 32
  6311. #define NWARPS_Q6_K_AMPERE 4
  6312. #else
  6313. #define MMQ_X_Q6_K_AMPERE 64
  6314. #define MMQ_Y_Q6_K_AMPERE 64
  6315. #define NWARPS_Q6_K_AMPERE 4
  6316. #endif
  6317. #define MMQ_X_Q6_K_PASCAL 64
  6318. #define MMQ_Y_Q6_K_PASCAL 64
  6319. #define NWARPS_Q6_K_PASCAL 8
  6320. template <bool need_check> static void
  6321. mul_mat_q6_K(
  6322. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6323. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6324. const sycl::nd_item<3> &item_ct1, int *tile_x_ql, sycl::half2 *tile_x_dm,
  6325. int *tile_x_sc, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6326. // int * tile_x_ql = nullptr;
  6327. // sycl::half2 *tile_x_dm = nullptr;
  6328. int * tile_x_qh = nullptr;
  6329. // int * tile_x_sc = nullptr;
  6330. //sycl_todo: change according to hardware
  6331. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  6332. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  6333. const int nwarps = NWARPS_Q6_K_AMPERE;
  6334. allocate_tiles_q6_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6335. tile_x_ql, tile_x_dm, tile_x_sc);
  6336. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps,
  6337. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ,
  6338. vec_dot_q6_K_q8_1_mul_mat>(
  6339. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6340. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6341. }
  6342. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl>
  6343. static void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  6344. const sycl::nd_item<3> &item_ct1) {
  6345. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6346. item_ct1.get_local_id(1);
  6347. if (row >= nrows) {
  6348. return;
  6349. }
  6350. const int blocks_per_row = ncols / qk;
  6351. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6352. // partial sum for each thread
  6353. float tmp = 0.0f;
  6354. const block_q_t * x = (const block_q_t *) vx;
  6355. const block_q8_1 * y = (const block_q8_1 *) vy;
  6356. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  6357. const int ibx = row * blocks_per_row + i +
  6358. item_ct1.get_local_id(2) / (qi / vdr); // x block index
  6359. const int iby = (i + item_ct1.get_local_id(2) / (qi / vdr)) *
  6360. (qk / QK8_1); // y block index that aligns with ibx
  6361. const int iqs =
  6362. vdr *
  6363. (item_ct1.get_local_id(2) %
  6364. (qi / vdr)); // x block quant index when casting the quants to int
  6365. tmp += vec_dot_q_sycl(&x[ibx], &y[iby], iqs);
  6366. }
  6367. // sum up partial sums and write back result
  6368. #pragma unroll
  6369. for (int mask = 16; mask > 0; mask >>= 1) {
  6370. tmp +=
  6371. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6372. }
  6373. if (item_ct1.get_local_id(2) == 0) {
  6374. dst[row] = tmp;
  6375. }
  6376. }
  6377. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  6378. static void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows,
  6379. const sycl::nd_item<3> &item_ct1) {
  6380. // qk = quantized weights per x block
  6381. // qr = number of quantized weights per data value in x block
  6382. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6383. item_ct1.get_local_id(1);
  6384. if (row >= nrows) {
  6385. return;
  6386. }
  6387. const int tid = item_ct1.get_local_id(2);
  6388. const int iter_stride = 2*GGML_SYCL_DMMV_X;
  6389. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  6390. const int y_offset = qr == 1 ? 1 : qk/2;
  6391. // partial sum for each thread
  6392. #ifdef GGML_SYCL_F16
  6393. sycl::half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  6394. #else
  6395. float tmp = 0.0f;
  6396. #endif // GGML_SYCL_F16
  6397. for (int i = 0; i < ncols; i += iter_stride) {
  6398. const int col = i + vals_per_iter*tid;
  6399. const int ib = (row*ncols + col)/qk; // x block index
  6400. const int iqs = (col%qk)/qr; // x quant index
  6401. const int iybs = col - col%qk; // y block start index
  6402. // processing >2 values per i iter is faster for fast GPUs
  6403. #pragma unroll
  6404. for (int j = 0; j < vals_per_iter; j += 2) {
  6405. // process 2 vals per j iter
  6406. // dequantize
  6407. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  6408. dfloat2 v;
  6409. dequantize_kernel(vx, ib, iqs + j/qr, v);
  6410. // matrix multiplication
  6411. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  6412. #ifdef GGML_SYCL_F16
  6413. dfloat2 t1{y[iybs + iqs + j / qr + 0],
  6414. y[iybs + iqs + j / qr + y_offset]};
  6415. tmp += v * t1;
  6416. #else
  6417. tmp += v.x() * y[iybs + iqs + j / qr + 0];
  6418. tmp += v.y() * y[iybs + iqs + j / qr + y_offset];
  6419. #endif // GGML_SYCL_F16
  6420. }
  6421. }
  6422. // sum up partial sums and write back result
  6423. #pragma unroll
  6424. for (int mask = 16; mask > 0; mask >>= 1) {
  6425. tmp +=
  6426. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6427. }
  6428. if (tid == 0) {
  6429. #ifdef GGML_SYCL_F16
  6430. dst[row] = tmp.x() + tmp.y();
  6431. #else
  6432. dst[row] = tmp;
  6433. #endif // GGML_SYCL_F16
  6434. }
  6435. }
  6436. static void mul_mat_p021_f16_f32(
  6437. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  6438. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y,
  6439. const sycl::nd_item<3> &item_ct1) {
  6440. const sycl::half *x = (const sycl::half *)vx;
  6441. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6442. item_ct1.get_local_id(1);
  6443. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  6444. item_ct1.get_local_id(0);
  6445. const int channel_x = channel / (nchannels_y / nchannels_x);
  6446. const int nrows_y = ncols_x;
  6447. const int nrows_dst = nrows_x;
  6448. const int row_dst = row_x;
  6449. float tmp = 0.0f;
  6450. for (int col_x0 = 0; col_x0 < ncols_x;
  6451. col_x0 += item_ct1.get_local_range(2)) {
  6452. const int col_x = col_x0 + item_ct1.get_local_id(2);
  6453. if (col_x >= ncols_x) {
  6454. break;
  6455. }
  6456. // x is transposed and permuted
  6457. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  6458. const float xi =
  6459. sycl::vec<sycl::half, 1>(x[ix])
  6460. .convert<float, sycl::rounding_mode::automatic>()[0];
  6461. const int row_y = col_x;
  6462. // y is not transposed but permuted
  6463. const int iy = channel*nrows_y + row_y;
  6464. tmp += xi * y[iy];
  6465. }
  6466. // dst is not transposed and not permuted
  6467. const int idst = channel*nrows_dst + row_dst;
  6468. // sum up partial sums and write back result
  6469. #pragma unroll
  6470. for (int mask = 16; mask > 0; mask >>= 1) {
  6471. tmp +=
  6472. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6473. }
  6474. if (item_ct1.get_local_id(2) == 0) {
  6475. dst[idst] = tmp;
  6476. }
  6477. }
  6478. static void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  6479. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  6480. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor,
  6481. const sycl::nd_item<3> &item_ct1) {
  6482. const sycl::half *x = (const sycl::half *)vx;
  6483. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6484. item_ct1.get_local_id(1);
  6485. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  6486. item_ct1.get_local_id(0);
  6487. const int channel_x = channel / channel_x_divisor;
  6488. const int nrows_y = ncols_x;
  6489. const int nrows_dst = nrows_x;
  6490. const int row_dst = row_x;
  6491. const int idst = channel*nrows_dst + row_dst;
  6492. float tmp = 0.0f;
  6493. for (int col_x0 = 0; col_x0 < ncols_x;
  6494. col_x0 += item_ct1.get_local_range(2)) {
  6495. const int col_x = col_x0 + item_ct1.get_local_id(2);
  6496. if (col_x >= ncols_x) {
  6497. break;
  6498. }
  6499. const int row_y = col_x;
  6500. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  6501. const int iy = channel*nrows_y + row_y;
  6502. const float xi =
  6503. sycl::vec<sycl::half, 1>(x[ix])
  6504. .convert<float, sycl::rounding_mode::automatic>()[0];
  6505. tmp += xi * y[iy];
  6506. }
  6507. // sum up partial sums and write back result
  6508. #pragma unroll
  6509. for (int mask = 16; mask > 0; mask >>= 1) {
  6510. tmp +=
  6511. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6512. }
  6513. if (item_ct1.get_local_id(2) == 0) {
  6514. dst[idst] = tmp;
  6515. }
  6516. }
  6517. static void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  6518. const float * xi = (const float *) cxi;
  6519. float * dsti = (float *) cdsti;
  6520. *dsti = *xi;
  6521. }
  6522. static void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  6523. const float * xi = (const float *) cxi;
  6524. sycl::half *dsti = (sycl::half *)cdsti;
  6525. *dsti = sycl::vec<float, 1>(*xi)
  6526. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  6527. }
  6528. static void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  6529. const sycl::half *xi = (const sycl::half *)cxi;
  6530. sycl::half *dsti = (sycl::half *)cdsti;
  6531. *dsti = *xi;
  6532. }
  6533. static void cpy_1_i16_i16(const char * cxi, char * cdsti) {
  6534. const int16_t *xi = (const int16_t *)cxi;
  6535. int16_t *dsti = (int16_t *)cdsti;
  6536. *dsti = *xi;
  6537. }
  6538. static void cpy_1_i32_i32(const char * cxi, char * cdsti) {
  6539. const int32_t *xi = (const int32_t *)cxi;
  6540. int32_t *dsti = (int32_t *)cdsti;
  6541. *dsti = *xi;
  6542. }
  6543. template <cpy_kernel_t cpy_1>
  6544. static void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  6545. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  6546. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12,
  6547. const sycl::nd_item<3> &item_ct1) {
  6548. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6549. item_ct1.get_local_id(2);
  6550. if (i >= ne) {
  6551. return;
  6552. }
  6553. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  6554. // then combine those indices with the corresponding byte offsets to get the total offsets
  6555. const int i02 = i / (ne00*ne01);
  6556. const int i01 = (i - i02*ne01*ne00) / ne00;
  6557. const int i00 = i - i02*ne01*ne00 - i01*ne00;
  6558. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  6559. const int i12 = i / (ne10*ne11);
  6560. const int i11 = (i - i12*ne10*ne11) / ne10;
  6561. const int i10 = i - i12*ne10*ne11 - i11*ne10;
  6562. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  6563. cpy_1(cx + x_offset, cdst + dst_offset);
  6564. }
  6565. static void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  6566. const float * xi = (const float *) cxi;
  6567. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  6568. float amax = 0.0f; // absolute max
  6569. for (int j = 0; j < QK8_0; j++) {
  6570. const float v = xi[j];
  6571. amax = sycl::fmax(amax, sycl::fabs((float)v));
  6572. }
  6573. const float d = amax / ((1 << 7) - 1);
  6574. const float id = d ? 1.0f/d : 0.0f;
  6575. dsti->d = d;
  6576. for (int j = 0; j < QK8_0; ++j) {
  6577. const float x0 = xi[j]*id;
  6578. dsti->qs[j] = sycl::round((float)x0);
  6579. }
  6580. }
  6581. static void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  6582. const float * xi = (const float *) cxi;
  6583. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  6584. float amax = 0.0f;
  6585. float vmax = 0.0f;
  6586. for (int j = 0; j < QK4_0; ++j) {
  6587. const float v = xi[j];
  6588. if (amax < sycl::fabs((float)v)) {
  6589. amax = sycl::fabs((float)v);
  6590. vmax = v;
  6591. }
  6592. }
  6593. const float d = vmax / -8;
  6594. const float id = d ? 1.0f/d : 0.0f;
  6595. dsti->d = d;
  6596. for (int j = 0; j < QK4_0/2; ++j) {
  6597. const float x0 = xi[0 + j]*id;
  6598. const float x1 = xi[QK4_0/2 + j]*id;
  6599. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 8.5f));
  6600. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 8.5f));
  6601. dsti->qs[j] = xi0;
  6602. dsti->qs[j] |= xi1 << 4;
  6603. }
  6604. }
  6605. static void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  6606. const float * xi = (const float *) cxi;
  6607. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  6608. float vmin = FLT_MAX;
  6609. float vmax = -FLT_MAX;
  6610. for (int j = 0; j < QK4_1; ++j) {
  6611. const float v = xi[j];
  6612. if (v < vmin) vmin = v;
  6613. if (v > vmax) vmax = v;
  6614. }
  6615. const float d = (vmax - vmin) / ((1 << 4) - 1);
  6616. const float id = d ? 1.0f/d : 0.0f;
  6617. dsti->dm.x() = d;
  6618. dsti->dm.y() = vmin;
  6619. for (int j = 0; j < QK4_1/2; ++j) {
  6620. const float x0 = (xi[0 + j] - vmin)*id;
  6621. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  6622. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 0.5f));
  6623. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 0.5f));
  6624. dsti->qs[j] = xi0;
  6625. dsti->qs[j] |= xi1 << 4;
  6626. }
  6627. }
  6628. template <cpy_kernel_t cpy_blck, int qk>
  6629. static void cpy_f32_q(const char * cx, char * cdst, const int ne,
  6630. const int ne00, const int ne01, const int nb00, const int nb01, const int nb02,
  6631. const int ne10, const int ne11, const int nb10, const int nb11, const int nb12,
  6632. const sycl::nd_item<3> &item_ct1) {
  6633. const int i = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6634. item_ct1.get_local_id(2)) *
  6635. qk;
  6636. if (i >= ne) {
  6637. return;
  6638. }
  6639. const int i02 = i / (ne00*ne01);
  6640. const int i01 = (i - i02*ne01*ne00) / ne00;
  6641. const int i00 = (i - i02*ne01*ne00 - i01*ne00);
  6642. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02;
  6643. const int i12 = i / (ne10*ne11);
  6644. const int i11 = (i - i12*ne10*ne11) / ne10;
  6645. const int i10 = (i - i12*ne10*ne11 - i11*ne10)/qk;
  6646. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12;
  6647. cpy_blck(cx + x_offset, cdst + dst_offset);
  6648. }
  6649. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  6650. const float y = (i0 / 2 - low) / sycl::max(0.001f, high - low);
  6651. return 1.0f - sycl::min(1.0f, sycl::max(0.0f, y));
  6652. }
  6653. struct rope_corr_dims {
  6654. float v[4];
  6655. };
  6656. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  6657. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  6658. static void rope_yarn(
  6659. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  6660. float * cos_theta, float * sin_theta
  6661. ) {
  6662. // Get n-d rotational scaling corrected for extrapolation
  6663. float theta_interp = freq_scale * theta_extrap;
  6664. float theta = theta_interp;
  6665. if (ext_factor != 0.0f) {
  6666. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  6667. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  6668. // Get n-d magnitude scaling corrected for interpolation
  6669. mscale *= 1.0f + 0.1f * sycl::log(1.0f / freq_scale);
  6670. }
  6671. *cos_theta = sycl::cos(theta) * mscale;
  6672. *sin_theta = sycl::sin(theta) * mscale;
  6673. }
  6674. // rope == RoPE == rotary positional embedding
  6675. template<typename T, bool has_pos>
  6676. static void rope(
  6677. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  6678. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  6679. ,
  6680. const sycl::nd_item<3> &item_ct1) {
  6681. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6682. item_ct1.get_local_id(1));
  6683. if (col >= ncols) {
  6684. return;
  6685. }
  6686. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6687. item_ct1.get_local_id(2);
  6688. const int i = row*ncols + col;
  6689. const int i2 = row/p_delta_rows;
  6690. const int p = has_pos ? pos[i2] : 0;
  6691. const float theta_base = p * dpct::pow(freq_base, -float(col) / ncols);
  6692. float cos_theta, sin_theta;
  6693. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  6694. const float x0 = x[i + 0];
  6695. const float x1 = x[i + 1];
  6696. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  6697. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  6698. }
  6699. template<typename T, bool has_pos>
  6700. static void rope_neox(
  6701. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  6702. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  6703. ,
  6704. const sycl::nd_item<3> &item_ct1) {
  6705. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6706. item_ct1.get_local_id(1));
  6707. if (col >= ncols) {
  6708. return;
  6709. }
  6710. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6711. item_ct1.get_local_id(2);
  6712. const int ib = col / n_dims;
  6713. const int ic = col % n_dims;
  6714. if (ib > 0) {
  6715. const int i = row*ncols + ib*n_dims + ic;
  6716. dst[i + 0] = x[i + 0];
  6717. dst[i + 1] = x[i + 1];
  6718. return;
  6719. }
  6720. const int i = row*ncols + ib*n_dims + ic/2;
  6721. const int i2 = row/p_delta_rows;
  6722. float cur_rot = inv_ndims * ic - ib;
  6723. const int p = has_pos ? pos[i2] : 0;
  6724. const float theta_base =
  6725. p * freq_scale * dpct::pow(theta_scale, col / 2.0f);
  6726. float cos_theta, sin_theta;
  6727. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  6728. const float x0 = x[i + 0];
  6729. const float x1 = x[i + n_dims/2];
  6730. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  6731. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  6732. }
  6733. static void rope_glm_f32(
  6734. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  6735. int n_ctx
  6736. , const sycl::nd_item<3> &item_ct1) {
  6737. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6738. item_ct1.get_local_id(2);
  6739. const int half_n_dims = ncols/4;
  6740. if (col >= half_n_dims) {
  6741. return;
  6742. }
  6743. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6744. item_ct1.get_local_id(1);
  6745. const int i = row*ncols + col;
  6746. const int i2 = row/p_delta_rows;
  6747. const float col_theta_scale = dpct::pow(freq_base, -2.0f * col / ncols);
  6748. // FIXME: this is likely wrong
  6749. const int p = pos != nullptr ? pos[i2] : 0;
  6750. const float theta = sycl::min(p, n_ctx - 2) * freq_scale * col_theta_scale;
  6751. const float sin_theta = sycl::sin((float)theta);
  6752. const float cos_theta = sycl::cos((float)theta);
  6753. const float x0 = x[i + 0];
  6754. const float x1 = x[i + half_n_dims];
  6755. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  6756. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  6757. const float block_theta =
  6758. ((float)sycl::max(p - n_ctx - 2, 0)) * col_theta_scale;
  6759. const float sin_block_theta = sycl::sin((float)block_theta);
  6760. const float cos_block_theta = sycl::cos((float)block_theta);
  6761. const float x2 = x[i + half_n_dims * 2];
  6762. const float x3 = x[i + half_n_dims * 3];
  6763. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  6764. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  6765. }
  6766. static void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  6767. const int n_heads_log2_floor, const float m0, const float m1,
  6768. const sycl::nd_item<3> &item_ct1) {
  6769. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6770. item_ct1.get_local_id(2);
  6771. if (col >= ncols) {
  6772. return;
  6773. }
  6774. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6775. item_ct1.get_local_id(1);
  6776. const int i = row*ncols + col;
  6777. const int k = row/k_rows;
  6778. float m_k;
  6779. if (k < n_heads_log2_floor) {
  6780. m_k = dpct::pow(m0, k + 1);
  6781. } else {
  6782. m_k = dpct::pow(m1, 2 * (k - n_heads_log2_floor) + 1);
  6783. }
  6784. dst[i] = col * m_k + x[i];
  6785. }
  6786. static void k_sum_rows_f32(const float * x, float * dst, const int ncols,
  6787. const sycl::nd_item<3> &item_ct1) {
  6788. const int row = item_ct1.get_group(1);
  6789. const int col = item_ct1.get_local_id(2);
  6790. float sum = 0.0f;
  6791. for (int i = col; i < ncols; i += item_ct1.get_local_range(2)) {
  6792. sum += x[row * ncols + i];
  6793. }
  6794. sum = warp_reduce_sum(sum, item_ct1);
  6795. if (col == 0) {
  6796. dst[row] = sum;
  6797. }
  6798. }
  6799. template<typename T>
  6800. static inline void swap(T & a, T & b) {
  6801. T tmp = a;
  6802. a = b;
  6803. b = tmp;
  6804. }
  6805. template<ggml_sort_order order>
  6806. static void k_argsort_f32_i32(const float * x, int * dst, const int ncols,
  6807. const sycl::nd_item<3> &item_ct1) {
  6808. // bitonic sort
  6809. int col = item_ct1.get_local_id(2);
  6810. int row = item_ct1.get_group(1);
  6811. if (col >= ncols) return;
  6812. const float * x_row = x + row * ncols;
  6813. int * dst_row = dst + row * ncols;
  6814. // initialize indices
  6815. if (col < ncols) {
  6816. dst_row[col] = col;
  6817. }
  6818. /*
  6819. DPCT1065:58: Consider replacing sycl::nd_item::barrier() with
  6820. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better
  6821. performance if there is no access to global memory.
  6822. */
  6823. item_ct1.barrier();
  6824. for (int k = 2; k <= ncols; k *= 2) {
  6825. for (int j = k / 2; j > 0; j /= 2) {
  6826. int ixj = col ^ j;
  6827. if (ixj > col) {
  6828. if ((col & k) == 0) {
  6829. 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]]) {
  6830. swap(dst_row[col], dst_row[ixj]);
  6831. }
  6832. } else {
  6833. 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]]) {
  6834. swap(dst_row[col], dst_row[ixj]);
  6835. }
  6836. }
  6837. }
  6838. /*
  6839. DPCT1118:11: SYCL group functions and algorithms must be encountered
  6840. in converged control flow. You may need to adjust the code.
  6841. */
  6842. /*
  6843. DPCT1065:59: Consider replacing sycl::nd_item::barrier() with
  6844. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6845. better performance if there is no access to global memory.
  6846. */
  6847. item_ct1.barrier();
  6848. }
  6849. }
  6850. }
  6851. static void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past,
  6852. const sycl::nd_item<3> &item_ct1) {
  6853. const int col = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6854. item_ct1.get_local_id(1);
  6855. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6856. item_ct1.get_local_id(2);
  6857. if (col >= ncols) {
  6858. return;
  6859. }
  6860. const int i = row*ncols + col;
  6861. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  6862. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  6863. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  6864. }
  6865. static void soft_max_f32(const float * x, const float * y, float * dst, const int ncols, const int nrows_y, const float scale,
  6866. const sycl::nd_item<3> &item_ct1, float *buf) {
  6867. const int tid = item_ct1.get_local_id(2);
  6868. const int rowx = item_ct1.get_group(2);
  6869. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  6870. const int block_size = item_ct1.get_local_range(2);
  6871. const int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  6872. const int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  6873. float max_val = -INFINITY;
  6874. for (int col = tid; col < ncols; col += block_size) {
  6875. const int ix = rowx*ncols + col;
  6876. const int iy = rowy*ncols + col;
  6877. max_val = sycl::max(max_val, x[ix] * scale + (y ? y[iy] : 0.0f));
  6878. }
  6879. // find the max value in the block
  6880. max_val = warp_reduce_max(max_val, item_ct1);
  6881. if (block_size > WARP_SIZE) {
  6882. if (warp_id == 0) {
  6883. buf[lane_id] = -INFINITY;
  6884. }
  6885. /*
  6886. DPCT1118:12: SYCL group functions and algorithms must be encountered in
  6887. converged control flow. You may need to adjust the code.
  6888. */
  6889. /*
  6890. DPCT1065:60: Consider replacing sycl::nd_item::barrier() with
  6891. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6892. better performance if there is no access to global memory.
  6893. */
  6894. item_ct1.barrier();
  6895. if (lane_id == 0) {
  6896. buf[warp_id] = max_val;
  6897. }
  6898. /*
  6899. DPCT1118:13: SYCL group functions and algorithms must be encountered in
  6900. converged control flow. You may need to adjust the code.
  6901. */
  6902. /*
  6903. DPCT1065:61: Consider replacing sycl::nd_item::barrier() with
  6904. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6905. better performance if there is no access to global memory.
  6906. */
  6907. item_ct1.barrier();
  6908. max_val = buf[lane_id];
  6909. max_val = warp_reduce_max(max_val, item_ct1);
  6910. }
  6911. float tmp = 0.f;
  6912. for (int col = tid; col < ncols; col += block_size) {
  6913. const int ix = rowx*ncols + col;
  6914. const int iy = rowy*ncols + col;
  6915. const float val =
  6916. sycl::native::exp((x[ix] * scale + (y ? y[iy] : 0.0f)) - max_val);
  6917. tmp += val;
  6918. dst[ix] = val;
  6919. }
  6920. // find the sum of exps in the block
  6921. tmp = warp_reduce_sum(tmp, item_ct1);
  6922. if (block_size > WARP_SIZE) {
  6923. if (warp_id == 0) {
  6924. buf[lane_id] = 0.f;
  6925. }
  6926. /*
  6927. DPCT1118:14: SYCL group functions and algorithms must be encountered in
  6928. converged control flow. You may need to adjust the code.
  6929. */
  6930. /*
  6931. DPCT1065:62: Consider replacing sycl::nd_item::barrier() with
  6932. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6933. better performance if there is no access to global memory.
  6934. */
  6935. item_ct1.barrier();
  6936. if (lane_id == 0) {
  6937. buf[warp_id] = tmp;
  6938. }
  6939. /*
  6940. DPCT1118:15: SYCL group functions and algorithms must be encountered in
  6941. converged control flow. You may need to adjust the code.
  6942. */
  6943. /*
  6944. DPCT1065:63: Consider replacing sycl::nd_item::barrier() with
  6945. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6946. better performance if there is no access to global memory.
  6947. */
  6948. item_ct1.barrier();
  6949. tmp = buf[lane_id];
  6950. tmp = warp_reduce_sum(tmp, item_ct1);
  6951. }
  6952. const float inv_tmp = 1.f / tmp;
  6953. for (int col = tid; col < ncols; col += block_size) {
  6954. const int i = rowx*ncols + col;
  6955. dst[i] *= inv_tmp;
  6956. }
  6957. }
  6958. static void scale_f32(const float * x, float * dst, const float scale, const int k,
  6959. const sycl::nd_item<3> &item_ct1) {
  6960. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6961. item_ct1.get_local_id(2);
  6962. if (i >= k) {
  6963. return;
  6964. }
  6965. dst[i] = scale * x[i];
  6966. }
  6967. static void clamp_f32(const float * x, float * dst, const float min, const float max, const int k,
  6968. const sycl::nd_item<3> &item_ct1) {
  6969. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6970. item_ct1.get_local_id(2);
  6971. if (i >= k) {
  6972. return;
  6973. }
  6974. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  6975. }
  6976. static void im2col_f32_f16(const float *x, sycl::half *dst, int offset_delta,
  6977. int IW, int IH, int OW, int KW, int KH,
  6978. int pelements, int CHW, int s0, int s1, int p0,
  6979. int p1, int d0, int d1,
  6980. const sycl::nd_item<3> &item_ct1) {
  6981. const int i = item_ct1.get_local_id(2) +
  6982. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  6983. if (i >= pelements) {
  6984. return;
  6985. }
  6986. const int ksize = OW * (KH > 1 ? KW : 1);
  6987. const int kx = i / ksize;
  6988. const int kd = kx * ksize;
  6989. const int ky = (i - kd) / OW;
  6990. const int ix = i % OW;
  6991. const int64_t iiw = ix * s0 + kx * d0 - p0;
  6992. const int64_t iih = item_ct1.get_group(1) * s1 + ky * d1 - p1;
  6993. const int64_t offset_dst =
  6994. (item_ct1.get_group(1) * OW + ix) * CHW +
  6995. (item_ct1.get_group(0) * (KW * KH) + ky * KW + kx);
  6996. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  6997. dst[offset_dst] =
  6998. sycl::vec<float, 1>(0.0f)
  6999. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7000. } else {
  7001. const int64_t offset_src = item_ct1.get_group(0) * offset_delta;
  7002. dst[offset_dst] =
  7003. sycl::vec<float, 1>(x[offset_src + iih * IW + iiw])
  7004. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7005. }
  7006. }
  7007. template <int qk, int qr, dequantize_kernel_t dq>
  7008. static void get_rows_sycl(const ggml_tensor *src0, const ggml_tensor *src1,
  7009. ggml_tensor *dst, const void *src0_dd,
  7010. const int32_t *src1_dd, float *dst_dd,
  7011. dpct::queue_ptr stream) {
  7012. GGML_TENSOR_BINARY_OP_LOCALS
  7013. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  7014. const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
  7015. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  7016. // strides in elements
  7017. //const size_t s0 = nb0 / ggml_element_size(dst);
  7018. const size_t s1 = nb1 / ggml_element_size(dst);
  7019. const size_t s2 = nb2 / ggml_element_size(dst);
  7020. const size_t s3 = nb3 / ggml_element_size(dst);
  7021. const size_t s10 = nb10 / ggml_element_size(src1);
  7022. const size_t s11 = nb11 / ggml_element_size(src1);
  7023. const size_t s12 = nb12 / ggml_element_size(src1);
  7024. //const size_t s13 = nb13 / ggml_element_size(src1);
  7025. GGML_ASSERT(ne00 % 2 == 0);
  7026. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7027. [=](sycl::nd_item<3> item_ct1) {
  7028. k_get_rows<qk, qr, dq>(
  7029. src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  7030. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  7031. });
  7032. (void) dst;
  7033. }
  7034. template <typename src0_t>
  7035. static void get_rows_sycl_float(const ggml_tensor *src0,
  7036. const ggml_tensor *src1, ggml_tensor *dst,
  7037. const src0_t *src0_dd, const int32_t *src1_dd,
  7038. float *dst_dd, dpct::queue_ptr stream) {
  7039. GGML_TENSOR_BINARY_OP_LOCALS
  7040. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  7041. const int block_num_x = (ne00 + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
  7042. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  7043. // strides in elements
  7044. //const size_t s0 = nb0 / ggml_element_size(dst);
  7045. const size_t s1 = nb1 / ggml_element_size(dst);
  7046. const size_t s2 = nb2 / ggml_element_size(dst);
  7047. const size_t s3 = nb3 / ggml_element_size(dst);
  7048. const size_t s10 = nb10 / ggml_element_size(src1);
  7049. const size_t s11 = nb11 / ggml_element_size(src1);
  7050. const size_t s12 = nb12 / ggml_element_size(src1);
  7051. //const size_t s13 = nb13 / ggml_element_size(src1);
  7052. {
  7053. dpct::has_capability_or_fail(stream->get_device(),
  7054. {sycl::aspect::fp16});
  7055. stream->parallel_for(
  7056. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7057. [=](sycl::nd_item<3> item_ct1) {
  7058. k_get_rows_float(src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  7059. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  7060. });
  7061. }
  7062. (void) dst;
  7063. }
  7064. template<float (*bin_op)(const float, const float)>
  7065. struct bin_bcast_sycl {
  7066. template <typename src0_t, typename src1_t, typename dst_t>
  7067. void operator()(const struct ggml_tensor *src0,
  7068. const struct ggml_tensor *src1, struct ggml_tensor *dst,
  7069. const src0_t *src0_dd, const src1_t *src1_dd, dst_t *dst_dd,
  7070. dpct::queue_ptr stream) {
  7071. GGML_TENSOR_BINARY_OP_LOCALS
  7072. int nr0 = ne10/ne0;
  7073. int nr1 = ne11/ne1;
  7074. int nr2 = ne12/ne2;
  7075. int nr3 = ne13/ne3;
  7076. int nr[4] = { nr0, nr1, nr2, nr3 };
  7077. // collapse dimensions until first broadcast dimension
  7078. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  7079. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  7080. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  7081. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  7082. auto collapse = [](int64_t cne[]) {
  7083. cne[0] *= cne[1];
  7084. cne[1] = cne[2];
  7085. cne[2] = cne[3];
  7086. cne[3] = 1;
  7087. };
  7088. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  7089. cnb[1] *= cne[1];
  7090. cnb[2] *= cne[2];
  7091. cnb[3] *= cne[3];
  7092. };
  7093. for (int i = 0; i < 4; i++) {
  7094. if (nr[i] != 1) {
  7095. break;
  7096. }
  7097. if (i > 0) {
  7098. collapse_nb(cnb0, cne0);
  7099. collapse_nb(cnb1, cne1);
  7100. collapse(cne0);
  7101. collapse(cne1);
  7102. }
  7103. }
  7104. {
  7105. int64_t ne0 = cne0[0];
  7106. int64_t ne1 = cne0[1];
  7107. int64_t ne2 = cne0[2];
  7108. int64_t ne3 = cne0[3];
  7109. int64_t ne10 = cne1[0];
  7110. int64_t ne11 = cne1[1];
  7111. int64_t ne12 = cne1[2];
  7112. int64_t ne13 = cne1[3];
  7113. size_t nb0 = cnb0[0];
  7114. size_t nb1 = cnb0[1];
  7115. size_t nb2 = cnb0[2];
  7116. size_t nb3 = cnb0[3];
  7117. size_t nb10 = cnb1[0];
  7118. size_t nb11 = cnb1[1];
  7119. size_t nb12 = cnb1[2];
  7120. size_t nb13 = cnb1[3];
  7121. size_t s0 = nb0 / sizeof(dst_t);
  7122. size_t s1 = nb1 / sizeof(dst_t);
  7123. size_t s2 = nb2 / sizeof(dst_t);
  7124. size_t s3 = nb3 / sizeof(dst_t);
  7125. size_t s10 = nb10 / sizeof(src1_t);
  7126. size_t s11 = nb11 / sizeof(src1_t);
  7127. size_t s12 = nb12 / sizeof(src1_t);
  7128. size_t s13 = nb13 / sizeof(src1_t);
  7129. GGML_ASSERT(s0 == 1);
  7130. GGML_ASSERT(s10 == 1);
  7131. const int block_size = 128;
  7132. int64_t hne0 = std::max(ne0/2LL, 1LL);
  7133. sycl::range<3> block_dims(1, 1, 1);
  7134. block_dims[2] = std::min<unsigned int>(hne0, block_size);
  7135. block_dims[1] = std::min<unsigned int>(
  7136. ne1, block_size / (unsigned int)block_dims[2]);
  7137. block_dims[0] = std::min(
  7138. std::min<unsigned int>(
  7139. ne2 * ne3, block_size / (unsigned int)block_dims[2] /
  7140. (unsigned int)block_dims[1]),
  7141. 64U);
  7142. sycl::range<3> block_nums(
  7143. (ne2 * ne3 + block_dims[0] - 1) / block_dims[0],
  7144. (ne1 + block_dims[1] - 1) / block_dims[1],
  7145. (hne0 + block_dims[2] - 1) / block_dims[2]);
  7146. if (block_nums[0] > 65535) {
  7147. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  7148. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  7149. {
  7150. dpct::has_capability_or_fail(stream->get_device(),
  7151. {sycl::aspect::fp16});
  7152. stream->parallel_for(
  7153. sycl::nd_range<3>(sycl::range<3>(1, 1, block_num) *
  7154. sycl::range<3>(1, 1, block_size),
  7155. sycl::range<3>(1, 1, block_size)),
  7156. [=](sycl::nd_item<3> item_ct1) {
  7157. k_bin_bcast_unravel<bin_op>(
  7158. src0_dd, src1_dd, dst_dd, ne0, ne1, ne2, ne3,
  7159. ne10, ne11, ne12, ne13, s1, s2, s3, s11, s12,
  7160. s13, item_ct1);
  7161. });
  7162. }
  7163. } else {
  7164. /*
  7165. DPCT1049:16: The work-group size passed to the SYCL kernel may
  7166. exceed the limit. To get the device limit, query
  7167. info::device::max_work_group_size. Adjust the work-group size if
  7168. needed.
  7169. */
  7170. dpct::has_capability_or_fail(stream->get_device(),
  7171. {sycl::aspect::fp16});
  7172. stream->parallel_for(
  7173. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7174. [=](sycl::nd_item<3> item_ct1) {
  7175. k_bin_bcast<bin_op>(src0_dd, src1_dd, dst_dd, ne0, ne1,
  7176. ne2, ne3, ne10, ne11, ne12, ne13,
  7177. s1, s2, s3, s11, s12, s13,
  7178. item_ct1);
  7179. });
  7180. }
  7181. }
  7182. }
  7183. };
  7184. static void acc_f32_sycl(const float *x, const float *y, float *dst,
  7185. const int n_elements, const int ne10, const int ne11,
  7186. const int ne12, const int nb1, const int nb2,
  7187. const int offset, dpct::queue_ptr stream) {
  7188. int num_blocks = (n_elements + SYCL_ACC_BLOCK_SIZE - 1) / SYCL_ACC_BLOCK_SIZE;
  7189. stream->parallel_for(
  7190. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7191. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE),
  7192. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE)),
  7193. [=](sycl::nd_item<3> item_ct1) {
  7194. acc_f32(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset,
  7195. item_ct1);
  7196. });
  7197. }
  7198. static void gelu_f32_sycl(const float *x, float *dst, const int k,
  7199. dpct::queue_ptr stream) {
  7200. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  7201. stream->parallel_for(
  7202. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7203. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  7204. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  7205. [=](sycl::nd_item<3> item_ct1) {
  7206. gelu_f32(x, dst, k, item_ct1);
  7207. });
  7208. }
  7209. static void silu_f32_sycl(const float *x, float *dst, const int k,
  7210. dpct::queue_ptr stream) {
  7211. const int num_blocks = (k + SYCL_SILU_BLOCK_SIZE - 1) / SYCL_SILU_BLOCK_SIZE;
  7212. stream->parallel_for(
  7213. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7214. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE),
  7215. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE)),
  7216. [=](sycl::nd_item<3> item_ct1) {
  7217. silu_f32(x, dst, k, item_ct1);
  7218. });
  7219. }
  7220. static void gelu_quick_f32_sycl(const float *x, float *dst, const int k,
  7221. dpct::queue_ptr stream) {
  7222. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  7223. stream->parallel_for(
  7224. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7225. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  7226. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  7227. [=](sycl::nd_item<3> item_ct1) {
  7228. gelu_quick_f32(x, dst, k, item_ct1);
  7229. });
  7230. }
  7231. static void tanh_f32_sycl(const float *x, float *dst, const int k,
  7232. dpct::queue_ptr stream) {
  7233. const int num_blocks = (k + SYCL_TANH_BLOCK_SIZE - 1) / SYCL_TANH_BLOCK_SIZE;
  7234. stream->parallel_for(
  7235. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7236. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE),
  7237. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE)),
  7238. [=](sycl::nd_item<3> item_ct1) {
  7239. tanh_f32(x, dst, k, item_ct1);
  7240. });
  7241. }
  7242. static void relu_f32_sycl(const float *x, float *dst, const int k,
  7243. dpct::queue_ptr stream) {
  7244. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  7245. stream->parallel_for(
  7246. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7247. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  7248. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  7249. [=](sycl::nd_item<3> item_ct1) {
  7250. relu_f32(x, dst, k, item_ct1);
  7251. });
  7252. }
  7253. static void leaky_relu_f32_sycl(const float *x, float *dst, const int k,
  7254. const float negative_slope,
  7255. dpct::queue_ptr stream) {
  7256. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  7257. stream->parallel_for(
  7258. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7259. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  7260. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  7261. [=](sycl::nd_item<3> item_ct1) {
  7262. leaky_relu_f32(x, dst, k, negative_slope, item_ct1);
  7263. });
  7264. }
  7265. static void sqr_f32_sycl(const float *x, float *dst, const int k,
  7266. dpct::queue_ptr stream) {
  7267. const int num_blocks = (k + SYCL_SQR_BLOCK_SIZE - 1) / SYCL_SQR_BLOCK_SIZE;
  7268. stream->parallel_for(
  7269. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7270. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE),
  7271. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE)),
  7272. [=](sycl::nd_item<3> item_ct1) {
  7273. sqr_f32(x, dst, k, item_ct1);
  7274. });
  7275. }
  7276. static void norm_f32_sycl(const float *x, float *dst, const int ncols,
  7277. const int nrows, const float eps,
  7278. dpct::queue_ptr stream) {
  7279. GGML_ASSERT(ncols % WARP_SIZE == 0);
  7280. if (ncols < 1024) {
  7281. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  7282. stream->submit([&](sycl::handler &cgh) {
  7283. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  7284. sycl::range<1>(32), cgh);
  7285. cgh.parallel_for(
  7286. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  7287. block_dims),
  7288. [=](sycl::nd_item<3> item_ct1)
  7289. [[intel::reqd_sub_group_size(32)]] {
  7290. norm_f32(x, dst, ncols, eps, item_ct1,
  7291. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  7292. });
  7293. });
  7294. } else {
  7295. const int work_group_size = g_work_group_size;
  7296. const sycl::range<3> block_dims(1, 1, work_group_size);
  7297. /*
  7298. DPCT1049:17: The work-group size passed to the SYCL kernel may exceed
  7299. the limit. To get the device limit, query
  7300. info::device::max_work_group_size. Adjust the work-group size if needed.
  7301. */
  7302. stream->submit([&](sycl::handler &cgh) {
  7303. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  7304. sycl::range<1>(32), cgh);
  7305. cgh.parallel_for(
  7306. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  7307. block_dims),
  7308. [=](sycl::nd_item<3> item_ct1)
  7309. [[intel::reqd_sub_group_size(32)]] {
  7310. norm_f32(x, dst, ncols, eps, item_ct1,
  7311. s_sum_acc_ct1.get_pointer(), work_group_size);
  7312. });
  7313. });
  7314. }
  7315. }
  7316. static void group_norm_f32_sycl(const float *x, float *dst,
  7317. const int num_groups, const int group_size,
  7318. const int ne_elements, dpct::queue_ptr stream) {
  7319. static const float eps = 1e-6f;
  7320. if (group_size < 1024) {
  7321. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  7322. stream->submit([&](sycl::handler &cgh) {
  7323. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  7324. cgh);
  7325. const float eps_ct4 = eps;
  7326. cgh.parallel_for(
  7327. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  7328. block_dims),
  7329. [=](sycl::nd_item<3> item_ct1)
  7330. [[intel::reqd_sub_group_size(32)]] {
  7331. group_norm_f32(
  7332. x, dst, group_size, ne_elements, eps_ct4, item_ct1,
  7333. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  7334. });
  7335. });
  7336. } else {
  7337. const int work_group_size = g_work_group_size;
  7338. const sycl::range<3> block_dims(1, 1, work_group_size);
  7339. /*
  7340. DPCT1049:18: The work-group size passed to the SYCL kernel may exceed
  7341. the limit. To get the device limit, query
  7342. info::device::max_work_group_size. Adjust the work-group size if needed.
  7343. */
  7344. stream->submit([&](sycl::handler &cgh) {
  7345. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  7346. cgh);
  7347. const float eps_ct4 = eps;
  7348. cgh.parallel_for(
  7349. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  7350. block_dims),
  7351. [=](sycl::nd_item<3> item_ct1)
  7352. [[intel::reqd_sub_group_size(32)]] {
  7353. group_norm_f32(x, dst, group_size, ne_elements,
  7354. eps_ct4, item_ct1,
  7355. s_sum_acc_ct1.get_pointer(), work_group_size);
  7356. });
  7357. });
  7358. }
  7359. }
  7360. static void concat_f32_sycl(const float *x, const float *y, float *dst,
  7361. const int ne0, int ne1, int ne2, int ne02,
  7362. dpct::queue_ptr stream) {
  7363. int num_blocks = (ne0 + SYCL_CONCAT_BLOCK_SIZE - 1) / SYCL_CONCAT_BLOCK_SIZE;
  7364. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  7365. stream->parallel_for(
  7366. sycl::nd_range<3>(gridDim *
  7367. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE),
  7368. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE)),
  7369. [=](sycl::nd_item<3> item_ct1) {
  7370. concat_f32(x, y, dst, ne0, ne02, item_ct1);
  7371. });
  7372. }
  7373. static void upscale_f32_sycl(const float *x, float *dst, const int ne00,
  7374. const int ne01, const int ne02,
  7375. const int scale_factor, dpct::queue_ptr stream) {
  7376. int ne0 = (ne00 * scale_factor);
  7377. int num_blocks = (ne0 + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE;
  7378. sycl::range<3> gridDim(ne02, (ne01 * scale_factor), num_blocks);
  7379. stream->parallel_for(
  7380. sycl::nd_range<3>(gridDim *
  7381. sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE),
  7382. sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE)),
  7383. [=](sycl::nd_item<3> item_ct1) {
  7384. upscale_f32(x, dst, ne00, ne00 * ne01, scale_factor, item_ct1);
  7385. });
  7386. }
  7387. static void pad_f32_sycl(const float *x, float *dst, const int ne00,
  7388. const int ne01, const int ne02, const int ne0,
  7389. const int ne1, const int ne2, dpct::queue_ptr stream) {
  7390. int num_blocks = (ne0 + SYCL_PAD_BLOCK_SIZE - 1) / SYCL_PAD_BLOCK_SIZE;
  7391. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  7392. stream->parallel_for(
  7393. sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE),
  7394. sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE)),
  7395. [=](sycl::nd_item<3> item_ct1) {
  7396. pad_f32(x, dst, ne0, ne00, ne01, ne02, item_ct1);
  7397. });
  7398. }
  7399. static void rms_norm_f32_sycl(const float *x, float *dst, const int ncols,
  7400. const int nrows, const float eps,
  7401. dpct::queue_ptr stream) {
  7402. GGML_ASSERT(ncols % WARP_SIZE == 0);
  7403. // printf("%s ncols=%d, nrows=%d, WARP_SIZE=%d\n", __func__, ncols, nrows, WARP_SIZE);
  7404. if (ncols < 1024) {
  7405. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  7406. stream->submit([&](sycl::handler &cgh) {
  7407. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  7408. cgh);
  7409. cgh.parallel_for(
  7410. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  7411. block_dims),
  7412. [=](sycl::nd_item<3> item_ct1)
  7413. [[intel::reqd_sub_group_size(32)]] {
  7414. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  7415. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  7416. });
  7417. });
  7418. } else {
  7419. const int work_group_size = g_work_group_size;
  7420. const sycl::range<3> block_dims(1, 1, work_group_size);
  7421. /*
  7422. DPCT1049:19: The work-group size passed to the SYCL kernel may exceed
  7423. the limit. To get the device limit, query
  7424. info::device::max_work_group_size. Adjust the work-group size if needed.
  7425. */
  7426. stream->submit([&](sycl::handler &cgh) {
  7427. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  7428. cgh);
  7429. cgh.parallel_for(
  7430. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  7431. block_dims),
  7432. [=](sycl::nd_item<3> item_ct1)
  7433. [[intel::reqd_sub_group_size(32)]] {
  7434. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  7435. s_sum_acc_ct1.get_pointer(), work_group_size);
  7436. });
  7437. });
  7438. }
  7439. }
  7440. static void quantize_row_q8_1_sycl(const float *x, void *vy, const int kx,
  7441. const int ky, const int kx_padded,
  7442. dpct::queue_ptr stream) {
  7443. const int block_num_x = (kx_padded + SYCL_QUANTIZE_BLOCK_SIZE - 1) / SYCL_QUANTIZE_BLOCK_SIZE;
  7444. const sycl::range<3> num_blocks(1, ky, block_num_x);
  7445. const sycl::range<3> block_size(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE);
  7446. {
  7447. dpct::has_capability_or_fail(stream->get_device(),
  7448. {sycl::aspect::fp16});
  7449. stream->parallel_for(
  7450. sycl::nd_range<3>(num_blocks * block_size, block_size),
  7451. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7452. quantize_q8_1(x, vy, kx, kx_padded, item_ct1);
  7453. });
  7454. }
  7455. }
  7456. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  7457. static void dequantize_block_sycl(const void *__restrict__ vx,
  7458. dst_t *__restrict__ y, const int k,
  7459. dpct::queue_ptr stream) {
  7460. const int num_blocks = (k + SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / SYCL_DEQUANTIZE_BLOCK_SIZE;
  7461. {
  7462. dpct::has_capability_or_fail(stream->get_device(),
  7463. {sycl::aspect::fp16});
  7464. stream->parallel_for(
  7465. sycl::nd_range<3>(
  7466. sycl::range<3>(1, 1, num_blocks) *
  7467. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  7468. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  7469. [=](sycl::nd_item<3> item_ct1) {
  7470. dequantize_block<qk, qr, dequantize_kernel>(vx, y, k, item_ct1);
  7471. });
  7472. }
  7473. }
  7474. template <typename dst_t>
  7475. static void dequantize_row_q2_K_sycl(const void *vx, dst_t *y, const int k,
  7476. dpct::queue_ptr stream) {
  7477. const int nb = k / QK_K;
  7478. #if QK_K == 256
  7479. {
  7480. dpct::has_capability_or_fail(stream->get_device(),
  7481. {sycl::aspect::fp16});
  7482. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7483. sycl::range<3>(1, 1, 64),
  7484. sycl::range<3>(1, 1, 64)),
  7485. [=](sycl::nd_item<3> item_ct1) {
  7486. dequantize_block_q2_K(vx, y, item_ct1);
  7487. });
  7488. }
  7489. #else
  7490. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  7491. #endif
  7492. }
  7493. template <typename dst_t>
  7494. static void dequantize_row_q3_K_sycl(const void *vx, dst_t *y, const int k,
  7495. dpct::queue_ptr stream) {
  7496. const int nb = k / QK_K;
  7497. #if QK_K == 256
  7498. {
  7499. dpct::has_capability_or_fail(stream->get_device(),
  7500. {sycl::aspect::fp16});
  7501. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7502. sycl::range<3>(1, 1, 64),
  7503. sycl::range<3>(1, 1, 64)),
  7504. [=](sycl::nd_item<3> item_ct1) {
  7505. dequantize_block_q3_K(vx, y, item_ct1);
  7506. });
  7507. }
  7508. #else
  7509. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  7510. #endif
  7511. }
  7512. template <typename dst_t>
  7513. static void dequantize_row_q4_K_sycl(const void *vx, dst_t *y, const int k,
  7514. dpct::queue_ptr stream) {
  7515. const int nb = k / QK_K;
  7516. {
  7517. dpct::has_capability_or_fail(stream->get_device(),
  7518. {sycl::aspect::fp16});
  7519. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7520. sycl::range<3>(1, 1, 32),
  7521. sycl::range<3>(1, 1, 32)),
  7522. [=](sycl::nd_item<3> item_ct1) {
  7523. dequantize_block_q4_K(vx, y, item_ct1);
  7524. });
  7525. }
  7526. }
  7527. template <typename dst_t>
  7528. static void dequantize_row_q5_K_sycl(const void *vx, dst_t *y, const int k,
  7529. dpct::queue_ptr stream) {
  7530. const int nb = k / QK_K;
  7531. #if QK_K == 256
  7532. {
  7533. dpct::has_capability_or_fail(stream->get_device(),
  7534. {sycl::aspect::fp16});
  7535. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7536. sycl::range<3>(1, 1, 64),
  7537. sycl::range<3>(1, 1, 64)),
  7538. [=](sycl::nd_item<3> item_ct1) {
  7539. dequantize_block_q5_K(vx, y, item_ct1);
  7540. });
  7541. }
  7542. #else
  7543. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  7544. #endif
  7545. }
  7546. template <typename dst_t>
  7547. static void dequantize_row_q6_K_sycl(const void *vx, dst_t *y, const int k,
  7548. dpct::queue_ptr stream) {
  7549. const int nb = k / QK_K;
  7550. #if QK_K == 256
  7551. {
  7552. dpct::has_capability_or_fail(stream->get_device(),
  7553. {sycl::aspect::fp16});
  7554. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7555. sycl::range<3>(1, 1, 64),
  7556. sycl::range<3>(1, 1, 64)),
  7557. [=](sycl::nd_item<3> item_ct1) {
  7558. dequantize_block_q6_K(vx, y, item_ct1);
  7559. });
  7560. }
  7561. #else
  7562. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  7563. #endif
  7564. }
  7565. static to_fp16_sycl_t ggml_get_to_fp16_sycl(ggml_type type) {
  7566. switch (type) {
  7567. case GGML_TYPE_Q4_0:
  7568. return dequantize_block_sycl<QK4_0, QR4_0, dequantize_q4_0>;
  7569. case GGML_TYPE_Q4_1:
  7570. return dequantize_block_sycl<QK4_1, QR4_1, dequantize_q4_1>;
  7571. case GGML_TYPE_Q5_0:
  7572. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  7573. case GGML_TYPE_Q5_1:
  7574. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  7575. case GGML_TYPE_Q8_0:
  7576. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  7577. case GGML_TYPE_Q2_K:
  7578. return dequantize_row_q2_K_sycl;
  7579. case GGML_TYPE_Q3_K:
  7580. return dequantize_row_q3_K_sycl;
  7581. case GGML_TYPE_Q4_K:
  7582. return dequantize_row_q4_K_sycl;
  7583. case GGML_TYPE_Q5_K:
  7584. return dequantize_row_q5_K_sycl;
  7585. case GGML_TYPE_Q6_K:
  7586. return dequantize_row_q6_K_sycl;
  7587. case GGML_TYPE_F32:
  7588. return dequantize_block_sycl<1, 1, convert_f32>;
  7589. default:
  7590. return nullptr;
  7591. }
  7592. }
  7593. static to_fp32_sycl_t ggml_get_to_fp32_sycl(ggml_type type) {
  7594. switch (type) {
  7595. case GGML_TYPE_Q4_0:
  7596. return dequantize_block_sycl<QK4_0, QR4_0, dequantize_q4_0>;
  7597. case GGML_TYPE_Q4_1:
  7598. return dequantize_block_sycl<QK4_1, QR4_1, dequantize_q4_1>;
  7599. case GGML_TYPE_Q5_0:
  7600. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  7601. case GGML_TYPE_Q5_1:
  7602. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  7603. case GGML_TYPE_Q8_0:
  7604. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  7605. case GGML_TYPE_Q2_K:
  7606. return dequantize_row_q2_K_sycl;
  7607. case GGML_TYPE_Q3_K:
  7608. return dequantize_row_q3_K_sycl;
  7609. case GGML_TYPE_Q4_K:
  7610. return dequantize_row_q4_K_sycl;
  7611. case GGML_TYPE_Q5_K:
  7612. return dequantize_row_q5_K_sycl;
  7613. case GGML_TYPE_Q6_K:
  7614. return dequantize_row_q6_K_sycl;
  7615. case GGML_TYPE_F16:
  7616. return dequantize_block_sycl<1, 1, convert_f16>;
  7617. default:
  7618. return nullptr;
  7619. }
  7620. }
  7621. static void dequantize_mul_mat_vec_q4_0_sycl(const void *vx, const dfloat *y,
  7622. float *dst, const int ncols,
  7623. const int nrows,
  7624. dpct::queue_ptr stream) {
  7625. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7626. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7627. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  7628. const sycl::range<3> block_nums(1, 1, block_num_y);
  7629. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7630. {
  7631. dpct::has_capability_or_fail(stream->get_device(),
  7632. {sycl::aspect::fp16});
  7633. stream->parallel_for(
  7634. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7635. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7636. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>(
  7637. vx, y, dst, ncols, nrows, item_ct1);
  7638. });
  7639. }
  7640. }
  7641. static void dequantize_mul_mat_vec_q4_1_sycl(const void *vx, const dfloat *y,
  7642. float *dst, const int ncols,
  7643. const int nrows,
  7644. dpct::queue_ptr stream) {
  7645. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7646. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7647. const sycl::range<3> block_nums(1, 1, block_num_y);
  7648. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7649. {
  7650. dpct::has_capability_or_fail(stream->get_device(),
  7651. {sycl::aspect::fp16});
  7652. stream->parallel_for(
  7653. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7654. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7655. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>(
  7656. vx, y, dst, ncols, nrows, item_ct1);
  7657. });
  7658. }
  7659. }
  7660. static void dequantize_mul_mat_vec_q5_0_sycl(const void *vx, const dfloat *y,
  7661. float *dst, const int ncols,
  7662. const int nrows,
  7663. dpct::queue_ptr stream) {
  7664. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7665. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7666. const sycl::range<3> block_nums(1, 1, block_num_y);
  7667. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7668. {
  7669. dpct::has_capability_or_fail(stream->get_device(),
  7670. {sycl::aspect::fp16});
  7671. stream->parallel_for(
  7672. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7673. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7674. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>(
  7675. vx, y, dst, ncols, nrows, item_ct1);
  7676. });
  7677. }
  7678. }
  7679. static void dequantize_mul_mat_vec_q5_1_sycl(const void *vx, const dfloat *y,
  7680. float *dst, const int ncols,
  7681. const int nrows,
  7682. dpct::queue_ptr stream) {
  7683. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7684. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7685. const sycl::range<3> block_nums(1, 1, block_num_y);
  7686. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7687. {
  7688. dpct::has_capability_or_fail(stream->get_device(),
  7689. {sycl::aspect::fp16});
  7690. stream->parallel_for(
  7691. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7692. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7693. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>(
  7694. vx, y, dst, ncols, nrows, item_ct1);
  7695. });
  7696. }
  7697. }
  7698. static void dequantize_mul_mat_vec_q8_0_sycl(const void *vx, const dfloat *y,
  7699. float *dst, const int ncols,
  7700. const int nrows,
  7701. dpct::queue_ptr stream) {
  7702. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7703. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7704. const sycl::range<3> block_nums(1, 1, block_num_y);
  7705. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7706. {
  7707. dpct::has_capability_or_fail(stream->get_device(),
  7708. {sycl::aspect::fp16});
  7709. stream->parallel_for(
  7710. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7711. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7712. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>(
  7713. vx, y, dst, ncols, nrows, item_ct1);
  7714. });
  7715. }
  7716. }
  7717. static void dequantize_mul_mat_vec_q2_K_sycl(const void *vx, const float *y,
  7718. float *dst, const int ncols,
  7719. const int nrows,
  7720. dpct::queue_ptr stream) {
  7721. GGML_ASSERT(ncols % QK_K == 0);
  7722. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  7723. const int block_num_y = (nrows + ny - 1) / ny;
  7724. const sycl::range<3> block_nums(1, 1, block_num_y);
  7725. const sycl::range<3> block_dims(1, ny, 32);
  7726. stream->parallel_for(
  7727. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7728. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7729. dequantize_mul_mat_vec_q2_k(vx, y, dst, ncols, nrows, item_ct1);
  7730. });
  7731. }
  7732. static void dequantize_mul_mat_vec_q3_K_sycl(const void *vx, const float *y,
  7733. float *dst, const int ncols,
  7734. const int nrows,
  7735. dpct::queue_ptr stream) {
  7736. GGML_ASSERT(ncols % QK_K == 0);
  7737. const int ny = 2 / K_QUANTS_PER_ITERATION;
  7738. const int block_num_y = (nrows + ny - 1) / ny;
  7739. const sycl::range<3> block_nums(1, 1, block_num_y);
  7740. const sycl::range<3> block_dims(1, ny, 32);
  7741. stream->parallel_for(
  7742. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7743. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7744. dequantize_mul_mat_vec_q3_k(vx, y, dst, ncols, nrows, item_ct1);
  7745. });
  7746. }
  7747. static void dequantize_mul_mat_vec_q4_K_sycl(const void *vx, const float *y,
  7748. float *dst, const int ncols,
  7749. const int nrows,
  7750. dpct::queue_ptr stream) {
  7751. GGML_ASSERT(ncols % QK_K == 0);
  7752. const int ny = 2 / K_QUANTS_PER_ITERATION;
  7753. const int block_num_y = (nrows + ny - 1) / ny;
  7754. const sycl::range<3> block_nums(1, 1, block_num_y);
  7755. const sycl::range<3> block_dims(1, ny, 32);
  7756. stream->parallel_for(
  7757. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7758. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7759. dequantize_mul_mat_vec_q4_k(vx, y, dst, ncols, nrows, item_ct1);
  7760. });
  7761. }
  7762. static void dequantize_mul_mat_vec_q5_K_sycl(const void *vx, const float *y,
  7763. float *dst, const int ncols,
  7764. const int nrows,
  7765. dpct::queue_ptr stream) {
  7766. GGML_ASSERT(ncols % QK_K == 0);
  7767. const sycl::range<3> block_dims(1, 1, 32);
  7768. stream->parallel_for(
  7769. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims, block_dims),
  7770. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7771. dequantize_mul_mat_vec_q5_k(vx, y, dst, ncols, item_ct1);
  7772. });
  7773. }
  7774. static void dequantize_mul_mat_vec_q6_K_sycl(const void *vx, const float *y,
  7775. float *dst, const int ncols,
  7776. const int nrows,
  7777. dpct::queue_ptr stream) {
  7778. GGML_ASSERT(ncols % QK_K == 0);
  7779. const int ny = 2 / K_QUANTS_PER_ITERATION;
  7780. const int block_num_y = (nrows + ny - 1) / ny;
  7781. const sycl::range<3> block_nums(1, 1, block_num_y);
  7782. const sycl::range<3> block_dims(1, ny, 32);
  7783. stream->parallel_for(
  7784. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7785. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7786. dequantize_mul_mat_vec_q6_k(vx, y, dst, ncols, nrows, item_ct1);
  7787. });
  7788. }
  7789. static void convert_mul_mat_vec_f16_sycl(const void *vx, const dfloat *y,
  7790. float *dst, const int ncols,
  7791. const int nrows,
  7792. dpct::queue_ptr stream) {
  7793. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7794. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7795. const sycl::range<3> block_nums(1, 1, block_num_y);
  7796. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7797. {
  7798. dpct::has_capability_or_fail(stream->get_device(),
  7799. {sycl::aspect::fp16});
  7800. stream->parallel_for(
  7801. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7802. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7803. dequantize_mul_mat_vec<1, 1, convert_f16>(vx, y, dst, ncols,
  7804. nrows, item_ct1);
  7805. });
  7806. }
  7807. }
  7808. static void mul_mat_vec_q4_0_q8_1_sycl(const void *vx, const void *vy,
  7809. float *dst, const int ncols,
  7810. const int nrows,
  7811. dpct::queue_ptr stream) {
  7812. GGML_ASSERT(ncols % QK4_0 == 0);
  7813. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7814. const sycl::range<3> block_nums(1, 1, block_num_y);
  7815. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7816. stream->parallel_for(
  7817. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7818. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7819. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ,
  7820. vec_dot_q4_0_q8_1>(vx, vy, dst, ncols, nrows,
  7821. item_ct1);
  7822. });
  7823. }
  7824. static void mul_mat_vec_q4_1_q8_1_sycl(const void *vx, const void *vy,
  7825. float *dst, const int ncols,
  7826. const int nrows,
  7827. dpct::queue_ptr stream) {
  7828. GGML_ASSERT(ncols % QK4_1 == 0);
  7829. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7830. const sycl::range<3> block_nums(1, 1, block_num_y);
  7831. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7832. stream->parallel_for(
  7833. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7834. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7835. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ,
  7836. vec_dot_q4_1_q8_1>(vx, vy, dst, ncols, nrows,
  7837. item_ct1);
  7838. });
  7839. }
  7840. static void mul_mat_vec_q5_0_q8_1_sycl(const void *vx, const void *vy,
  7841. float *dst, const int ncols,
  7842. const int nrows,
  7843. dpct::queue_ptr stream) {
  7844. GGML_ASSERT(ncols % QK5_0 == 0);
  7845. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7846. const sycl::range<3> block_nums(1, 1, block_num_y);
  7847. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7848. stream->parallel_for(
  7849. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7850. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7851. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ,
  7852. vec_dot_q5_0_q8_1>(vx, vy, dst, ncols, nrows,
  7853. item_ct1);
  7854. });
  7855. }
  7856. static void mul_mat_vec_q5_1_q8_1_sycl(const void *vx, const void *vy,
  7857. float *dst, const int ncols,
  7858. const int nrows,
  7859. dpct::queue_ptr stream) {
  7860. GGML_ASSERT(ncols % QK5_1 == 0);
  7861. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7862. const sycl::range<3> block_nums(1, 1, block_num_y);
  7863. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7864. stream->parallel_for(
  7865. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7866. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7867. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ,
  7868. vec_dot_q5_1_q8_1>(vx, vy, dst, ncols, nrows,
  7869. item_ct1);
  7870. });
  7871. }
  7872. static void mul_mat_vec_q8_0_q8_1_sycl(const void *vx, const void *vy,
  7873. float *dst, const int ncols,
  7874. const int nrows,
  7875. dpct::queue_ptr stream) {
  7876. GGML_ASSERT(ncols % QK8_0 == 0);
  7877. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7878. const sycl::range<3> block_nums(1, 1, block_num_y);
  7879. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7880. stream->parallel_for(
  7881. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7882. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7883. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ,
  7884. vec_dot_q8_0_q8_1>(vx, vy, dst, ncols, nrows,
  7885. item_ct1);
  7886. });
  7887. }
  7888. static void mul_mat_vec_q2_K_q8_1_sycl(const void *vx, const void *vy,
  7889. float *dst, const int ncols,
  7890. const int nrows,
  7891. dpct::queue_ptr stream) {
  7892. GGML_ASSERT(ncols % QK_K == 0);
  7893. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7894. const sycl::range<3> block_nums(1, 1, block_num_y);
  7895. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7896. stream->parallel_for(
  7897. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7898. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7899. mul_mat_vec_q<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ,
  7900. vec_dot_q2_K_q8_1>(vx, vy, dst, ncols, nrows,
  7901. item_ct1);
  7902. });
  7903. }
  7904. static void mul_mat_vec_q3_K_q8_1_sycl(const void *vx, const void *vy,
  7905. float *dst, const int ncols,
  7906. const int nrows,
  7907. dpct::queue_ptr stream) {
  7908. GGML_ASSERT(ncols % QK_K == 0);
  7909. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7910. const sycl::range<3> block_nums(1, 1, block_num_y);
  7911. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7912. stream->parallel_for(
  7913. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7914. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7915. mul_mat_vec_q<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ,
  7916. vec_dot_q3_K_q8_1>(vx, vy, dst, ncols, nrows,
  7917. item_ct1);
  7918. });
  7919. }
  7920. static void mul_mat_vec_q4_K_q8_1_sycl(const void *vx, const void *vy,
  7921. float *dst, const int ncols,
  7922. const int nrows,
  7923. dpct::queue_ptr stream) {
  7924. GGML_ASSERT(ncols % QK_K == 0);
  7925. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7926. const sycl::range<3> block_nums(1, 1, block_num_y);
  7927. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7928. stream->parallel_for(
  7929. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7930. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7931. mul_mat_vec_q<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ,
  7932. vec_dot_q4_K_q8_1>(vx, vy, dst, ncols, nrows,
  7933. item_ct1);
  7934. });
  7935. }
  7936. static void mul_mat_vec_q5_K_q8_1_sycl(const void *vx, const void *vy,
  7937. float *dst, const int ncols,
  7938. const int nrows,
  7939. dpct::queue_ptr stream) {
  7940. GGML_ASSERT(ncols % QK_K == 0);
  7941. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7942. const sycl::range<3> block_nums(1, 1, block_num_y);
  7943. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7944. stream->parallel_for(
  7945. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7946. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7947. mul_mat_vec_q<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ,
  7948. vec_dot_q5_K_q8_1>(vx, vy, dst, ncols, nrows,
  7949. item_ct1);
  7950. });
  7951. }
  7952. static void mul_mat_vec_q6_K_q8_1_sycl(const void *vx, const void *vy,
  7953. float *dst, const int ncols,
  7954. const int nrows,
  7955. dpct::queue_ptr stream) {
  7956. GGML_ASSERT(ncols % QK_K == 0);
  7957. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7958. const sycl::range<3> block_nums(1, 1, block_num_y);
  7959. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7960. stream->parallel_for(
  7961. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7962. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7963. mul_mat_vec_q<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ,
  7964. vec_dot_q6_K_q8_1>(vx, vy, dst, ncols, nrows,
  7965. item_ct1);
  7966. });
  7967. }
  7968. int get_device_index_by_id(int id){
  7969. int res = g_sycl_device_id2index[id].index;
  7970. // GGML_SYCL_DEBUG("get_device_index_by_id id=%d device_index=%d\n", id, res);
  7971. GGML_ASSERT(res>=0);
  7972. return res;
  7973. }
  7974. int get_device_id_by_index(int index){
  7975. int res = g_device_caps[index].device_id;
  7976. GGML_ASSERT(res>=0);
  7977. return res;
  7978. }
  7979. int get_current_device_index(){
  7980. return get_device_index_by_id(dpct::dev_mgr::instance().current_device_id());
  7981. }
  7982. static void ggml_mul_mat_q4_0_q8_1_sycl(const void *vx, const void *vy,
  7983. float *dst, const int ncols_x,
  7984. const int nrows_x, const int ncols_y,
  7985. const int nrows_y, const int nrows_dst,
  7986. dpct::queue_ptr stream) try {
  7987. int id;
  7988. SYCL_CHECK(
  7989. CHECK_TRY_ERROR(id = get_current_device_index()));
  7990. const int compute_capability = g_device_caps[id].cc;
  7991. int mmq_x, mmq_y, nwarps;
  7992. if (compute_capability >= VER_GEN13) {
  7993. mmq_x = MMQ_X_Q4_0_RDNA2;
  7994. mmq_y = MMQ_Y_Q4_0_RDNA2;
  7995. nwarps = NWARPS_Q4_0_RDNA2;
  7996. } else if (compute_capability >= VER_GEN12) {
  7997. mmq_x = MMQ_X_Q4_0_RDNA1;
  7998. mmq_y = MMQ_Y_Q4_0_RDNA1;
  7999. nwarps = NWARPS_Q4_0_RDNA1;
  8000. } else if (compute_capability >= VER_GEN9) {
  8001. mmq_x = MMQ_X_Q4_0_AMPERE;
  8002. mmq_y = MMQ_Y_Q4_0_AMPERE;
  8003. nwarps = NWARPS_Q4_0_AMPERE;
  8004. } else if (compute_capability >= VER_4VEC) {
  8005. mmq_x = MMQ_X_Q4_0_PASCAL;
  8006. mmq_y = MMQ_Y_Q4_0_PASCAL;
  8007. nwarps = NWARPS_Q4_0_PASCAL;
  8008. } else {
  8009. GGML_ASSERT(false);
  8010. }
  8011. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8012. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8013. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8014. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8015. if (nrows_x % mmq_y == 0) {
  8016. const bool need_check = false;
  8017. /*
  8018. DPCT1049:20: The work-group size passed to the SYCL kernel may exceed
  8019. the limit. To get the device limit, query
  8020. info::device::max_work_group_size. Adjust the work-group size if needed.
  8021. */
  8022. {
  8023. dpct::has_capability_or_fail(stream->get_device(),
  8024. {sycl::aspect::fp16});
  8025. stream->submit([&](sycl::handler &cgh) {
  8026. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  8027. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8028. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  8029. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  8030. cgh);
  8031. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8032. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8033. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8034. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8035. cgh.parallel_for(
  8036. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8037. [=](sycl::nd_item<3> item_ct1) {
  8038. mul_mat_q4_0<need_check>(
  8039. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8040. nrows_dst, item_ct1,
  8041. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  8042. tile_x_d_q4_0_acc_ct1.get_pointer(),
  8043. tile_y_qs_acc_ct1.get_pointer(),
  8044. tile_y_ds_acc_ct1.get_pointer());
  8045. });
  8046. });
  8047. }
  8048. } else {
  8049. const bool need_check = true;
  8050. /*
  8051. DPCT1049:21: The work-group size passed to the SYCL kernel may exceed
  8052. the limit. To get the device limit, query
  8053. info::device::max_work_group_size. Adjust the work-group size if needed.
  8054. */
  8055. {
  8056. dpct::has_capability_or_fail(stream->get_device(),
  8057. {sycl::aspect::fp16});
  8058. stream->submit([&](sycl::handler &cgh) {
  8059. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  8060. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8061. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  8062. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  8063. cgh);
  8064. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8065. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8066. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8067. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8068. cgh.parallel_for(
  8069. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8070. [=](sycl::nd_item<3> item_ct1) {
  8071. mul_mat_q4_0<need_check>(
  8072. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8073. nrows_dst, item_ct1,
  8074. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  8075. tile_x_d_q4_0_acc_ct1.get_pointer(),
  8076. tile_y_qs_acc_ct1.get_pointer(),
  8077. tile_y_ds_acc_ct1.get_pointer());
  8078. });
  8079. });
  8080. }
  8081. }
  8082. }
  8083. catch (sycl::exception const &exc) {
  8084. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8085. << ", line:" << __LINE__ << std::endl;
  8086. std::exit(1);
  8087. }
  8088. static void ggml_mul_mat_q4_1_q8_1_sycl(const void *vx, const void *vy,
  8089. float *dst, const int ncols_x,
  8090. const int nrows_x, const int ncols_y,
  8091. const int nrows_y, const int nrows_dst,
  8092. dpct::queue_ptr stream) try {
  8093. int id;
  8094. SYCL_CHECK(
  8095. CHECK_TRY_ERROR(id = get_current_device_index()));
  8096. const int compute_capability = g_device_caps[id].cc;
  8097. int mmq_x, mmq_y, nwarps;
  8098. if (compute_capability >= VER_GEN13) {
  8099. mmq_x = MMQ_X_Q4_1_RDNA2;
  8100. mmq_y = MMQ_Y_Q4_1_RDNA2;
  8101. nwarps = NWARPS_Q4_1_RDNA2;
  8102. } else if (compute_capability >= VER_GEN12) {
  8103. mmq_x = MMQ_X_Q4_1_RDNA1;
  8104. mmq_y = MMQ_Y_Q4_1_RDNA1;
  8105. nwarps = NWARPS_Q4_1_RDNA1;
  8106. } else if (compute_capability >= VER_GEN9) {
  8107. mmq_x = MMQ_X_Q4_1_AMPERE;
  8108. mmq_y = MMQ_Y_Q4_1_AMPERE;
  8109. nwarps = NWARPS_Q4_1_AMPERE;
  8110. } else if (compute_capability >= VER_4VEC) {
  8111. mmq_x = MMQ_X_Q4_1_PASCAL;
  8112. mmq_y = MMQ_Y_Q4_1_PASCAL;
  8113. nwarps = NWARPS_Q4_1_PASCAL;
  8114. } else {
  8115. GGML_ASSERT(false);
  8116. }
  8117. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8118. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8119. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8120. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8121. if (nrows_x % mmq_y == 0) {
  8122. const bool need_check = false;
  8123. /*
  8124. DPCT1049:22: The work-group size passed to the SYCL kernel may exceed
  8125. the limit. To get the device limit, query
  8126. info::device::max_work_group_size. Adjust the work-group size if needed.
  8127. */
  8128. {
  8129. dpct::has_capability_or_fail(stream->get_device(),
  8130. {sycl::aspect::fp16});
  8131. stream->submit([&](sycl::handler &cgh) {
  8132. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  8133. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  8134. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  8135. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  8136. cgh);
  8137. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8138. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8139. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8140. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8141. cgh.parallel_for(
  8142. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8143. [=](sycl::nd_item<3> item_ct1) {
  8144. mul_mat_q4_1<need_check>(
  8145. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8146. nrows_dst, item_ct1,
  8147. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  8148. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  8149. tile_y_qs_acc_ct1.get_pointer(),
  8150. tile_y_ds_acc_ct1.get_pointer());
  8151. });
  8152. });
  8153. }
  8154. } else {
  8155. const bool need_check = true;
  8156. /*
  8157. DPCT1049:23: The work-group size passed to the SYCL kernel may exceed
  8158. the limit. To get the device limit, query
  8159. info::device::max_work_group_size. Adjust the work-group size if needed.
  8160. */
  8161. {
  8162. dpct::has_capability_or_fail(stream->get_device(),
  8163. {sycl::aspect::fp16});
  8164. stream->submit([&](sycl::handler &cgh) {
  8165. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  8166. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  8167. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  8168. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  8169. cgh);
  8170. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8171. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8172. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8173. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8174. cgh.parallel_for(
  8175. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8176. [=](sycl::nd_item<3> item_ct1) {
  8177. mul_mat_q4_1<need_check>(
  8178. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8179. nrows_dst, item_ct1,
  8180. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  8181. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  8182. tile_y_qs_acc_ct1.get_pointer(),
  8183. tile_y_ds_acc_ct1.get_pointer());
  8184. });
  8185. });
  8186. }
  8187. }
  8188. }
  8189. catch (sycl::exception const &exc) {
  8190. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8191. << ", line:" << __LINE__ << std::endl;
  8192. std::exit(1);
  8193. }
  8194. static void ggml_mul_mat_q5_0_q8_1_sycl(const void *vx, const void *vy,
  8195. float *dst, const int ncols_x,
  8196. const int nrows_x, const int ncols_y,
  8197. const int nrows_y, const int nrows_dst,
  8198. dpct::queue_ptr stream) try {
  8199. int id;
  8200. SYCL_CHECK(
  8201. CHECK_TRY_ERROR(id = get_current_device_index()));
  8202. const int compute_capability = g_device_caps[id].cc;
  8203. int mmq_x, mmq_y, nwarps;
  8204. if (compute_capability >= VER_GEN13) {
  8205. mmq_x = MMQ_X_Q5_0_RDNA2;
  8206. mmq_y = MMQ_Y_Q5_0_RDNA2;
  8207. nwarps = NWARPS_Q5_0_RDNA2;
  8208. } else if (compute_capability >= VER_GEN12) {
  8209. mmq_x = MMQ_X_Q5_0_RDNA1;
  8210. mmq_y = MMQ_Y_Q5_0_RDNA1;
  8211. nwarps = NWARPS_Q5_0_RDNA1;
  8212. } else if (compute_capability >= VER_GEN9) {
  8213. mmq_x = MMQ_X_Q5_0_AMPERE;
  8214. mmq_y = MMQ_Y_Q5_0_AMPERE;
  8215. nwarps = NWARPS_Q5_0_AMPERE;
  8216. } else if (compute_capability >= VER_4VEC) {
  8217. mmq_x = MMQ_X_Q5_0_PASCAL;
  8218. mmq_y = MMQ_Y_Q5_0_PASCAL;
  8219. nwarps = NWARPS_Q5_0_PASCAL;
  8220. } else {
  8221. GGML_ASSERT(false);
  8222. }
  8223. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8224. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8225. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8226. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8227. if (nrows_x % mmq_y == 0) {
  8228. const bool need_check = false;
  8229. /*
  8230. DPCT1049:24: The work-group size passed to the SYCL kernel may exceed
  8231. the limit. To get the device limit, query
  8232. info::device::max_work_group_size. Adjust the work-group size if needed.
  8233. */
  8234. {
  8235. dpct::has_capability_or_fail(stream->get_device(),
  8236. {sycl::aspect::fp16});
  8237. stream->submit([&](sycl::handler &cgh) {
  8238. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  8239. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8240. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  8241. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  8242. cgh);
  8243. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8244. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8245. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8246. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8247. cgh.parallel_for(
  8248. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8249. [=](sycl::nd_item<3> item_ct1) {
  8250. mul_mat_q5_0<need_check>(
  8251. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8252. nrows_dst, item_ct1,
  8253. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  8254. tile_x_d_q5_0_acc_ct1.get_pointer(),
  8255. tile_y_qs_acc_ct1.get_pointer(),
  8256. tile_y_ds_acc_ct1.get_pointer());
  8257. });
  8258. });
  8259. }
  8260. } else {
  8261. const bool need_check = true;
  8262. /*
  8263. DPCT1049:25: The work-group size passed to the SYCL kernel may exceed
  8264. the limit. To get the device limit, query
  8265. info::device::max_work_group_size. Adjust the work-group size if needed.
  8266. */
  8267. {
  8268. dpct::has_capability_or_fail(stream->get_device(),
  8269. {sycl::aspect::fp16});
  8270. stream->submit([&](sycl::handler &cgh) {
  8271. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  8272. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8273. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  8274. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  8275. cgh);
  8276. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8277. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8278. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8279. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8280. cgh.parallel_for(
  8281. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8282. [=](sycl::nd_item<3> item_ct1) {
  8283. mul_mat_q5_0<need_check>(
  8284. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8285. nrows_dst, item_ct1,
  8286. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  8287. tile_x_d_q5_0_acc_ct1.get_pointer(),
  8288. tile_y_qs_acc_ct1.get_pointer(),
  8289. tile_y_ds_acc_ct1.get_pointer());
  8290. });
  8291. });
  8292. }
  8293. }
  8294. }
  8295. catch (sycl::exception const &exc) {
  8296. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8297. << ", line:" << __LINE__ << std::endl;
  8298. std::exit(1);
  8299. }
  8300. static void ggml_mul_mat_q5_1_q8_1_sycl(const void *vx, const void *vy,
  8301. float *dst, const int ncols_x,
  8302. const int nrows_x, const int ncols_y,
  8303. const int nrows_y, const int nrows_dst,
  8304. dpct::queue_ptr stream) try {
  8305. int id;
  8306. SYCL_CHECK(
  8307. CHECK_TRY_ERROR(id = get_current_device_index()));
  8308. const int compute_capability = g_device_caps[id].cc;
  8309. int mmq_x, mmq_y, nwarps;
  8310. if (compute_capability >= VER_GEN13) {
  8311. mmq_x = MMQ_X_Q5_1_RDNA2;
  8312. mmq_y = MMQ_Y_Q5_1_RDNA2;
  8313. nwarps = NWARPS_Q5_1_RDNA2;
  8314. } else if (compute_capability >= VER_GEN12) {
  8315. mmq_x = MMQ_X_Q5_1_RDNA1;
  8316. mmq_y = MMQ_Y_Q5_1_RDNA1;
  8317. nwarps = NWARPS_Q5_1_RDNA1;
  8318. } else if (compute_capability >= VER_GEN9) {
  8319. mmq_x = MMQ_X_Q5_1_AMPERE;
  8320. mmq_y = MMQ_Y_Q5_1_AMPERE;
  8321. nwarps = NWARPS_Q5_1_AMPERE;
  8322. } else if (compute_capability >= VER_4VEC) {
  8323. mmq_x = MMQ_X_Q5_1_PASCAL;
  8324. mmq_y = MMQ_Y_Q5_1_PASCAL;
  8325. nwarps = NWARPS_Q5_1_PASCAL;
  8326. } else {
  8327. GGML_ASSERT(false);
  8328. }
  8329. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8330. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8331. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8332. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8333. if (nrows_x % mmq_y == 0) {
  8334. const bool need_check = false;
  8335. /*
  8336. DPCT1049:26: The work-group size passed to the SYCL kernel may exceed
  8337. the limit. To get the device limit, query
  8338. info::device::max_work_group_size. Adjust the work-group size if needed.
  8339. */
  8340. {
  8341. dpct::has_capability_or_fail(stream->get_device(),
  8342. {sycl::aspect::fp16});
  8343. stream->submit([&](sycl::handler &cgh) {
  8344. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  8345. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8346. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  8347. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  8348. cgh);
  8349. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8350. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8351. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8352. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8353. cgh.parallel_for(
  8354. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8355. [=](sycl::nd_item<3> item_ct1) {
  8356. mul_mat_q5_1<need_check>(
  8357. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8358. nrows_dst, item_ct1,
  8359. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  8360. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  8361. tile_y_qs_acc_ct1.get_pointer(),
  8362. tile_y_ds_acc_ct1.get_pointer());
  8363. });
  8364. });
  8365. }
  8366. } else {
  8367. const bool need_check = true;
  8368. /*
  8369. DPCT1049:27: The work-group size passed to the SYCL kernel may exceed
  8370. the limit. To get the device limit, query
  8371. info::device::max_work_group_size. Adjust the work-group size if needed.
  8372. */
  8373. {
  8374. dpct::has_capability_or_fail(stream->get_device(),
  8375. {sycl::aspect::fp16});
  8376. stream->submit([&](sycl::handler &cgh) {
  8377. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  8378. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8379. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  8380. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  8381. cgh);
  8382. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8383. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8384. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8385. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8386. cgh.parallel_for(
  8387. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8388. [=](sycl::nd_item<3> item_ct1) {
  8389. mul_mat_q5_1<need_check>(
  8390. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8391. nrows_dst, item_ct1,
  8392. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  8393. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  8394. tile_y_qs_acc_ct1.get_pointer(),
  8395. tile_y_ds_acc_ct1.get_pointer());
  8396. });
  8397. });
  8398. }
  8399. }
  8400. }
  8401. catch (sycl::exception const &exc) {
  8402. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8403. << ", line:" << __LINE__ << std::endl;
  8404. std::exit(1);
  8405. }
  8406. static void ggml_mul_mat_q8_0_q8_1_sycl(const void *vx, const void *vy,
  8407. float *dst, const int ncols_x,
  8408. const int nrows_x, const int ncols_y,
  8409. const int nrows_y, const int nrows_dst,
  8410. dpct::queue_ptr stream) try {
  8411. int id;
  8412. SYCL_CHECK(
  8413. CHECK_TRY_ERROR(id = get_current_device_index()));
  8414. const int compute_capability = g_device_caps[id].cc;
  8415. int mmq_x, mmq_y, nwarps;
  8416. if (compute_capability >= VER_GEN13) {
  8417. mmq_x = MMQ_X_Q8_0_RDNA2;
  8418. mmq_y = MMQ_Y_Q8_0_RDNA2;
  8419. nwarps = NWARPS_Q8_0_RDNA2;
  8420. } else if (compute_capability >= VER_GEN12) {
  8421. mmq_x = MMQ_X_Q8_0_RDNA1;
  8422. mmq_y = MMQ_Y_Q8_0_RDNA1;
  8423. nwarps = NWARPS_Q8_0_RDNA1;
  8424. } else if (compute_capability >= VER_GEN9) {
  8425. mmq_x = MMQ_X_Q8_0_AMPERE;
  8426. mmq_y = MMQ_Y_Q8_0_AMPERE;
  8427. nwarps = NWARPS_Q8_0_AMPERE;
  8428. } else if (compute_capability >= VER_4VEC) {
  8429. mmq_x = MMQ_X_Q8_0_PASCAL;
  8430. mmq_y = MMQ_Y_Q8_0_PASCAL;
  8431. nwarps = NWARPS_Q8_0_PASCAL;
  8432. } else {
  8433. GGML_ASSERT(false);
  8434. }
  8435. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8436. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8437. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8438. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8439. if (nrows_x % mmq_y == 0) {
  8440. const bool need_check = false;
  8441. /*
  8442. DPCT1049:28: The work-group size passed to the SYCL kernel may exceed
  8443. the limit. To get the device limit, query
  8444. info::device::max_work_group_size. Adjust the work-group size if needed.
  8445. */
  8446. {
  8447. dpct::has_capability_or_fail(stream->get_device(),
  8448. {sycl::aspect::fp16});
  8449. stream->submit([&](sycl::handler &cgh) {
  8450. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  8451. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8452. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  8453. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  8454. cgh);
  8455. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8456. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8457. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8458. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8459. cgh.parallel_for(
  8460. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8461. [=](sycl::nd_item<3> item_ct1) {
  8462. mul_mat_q8_0<need_check>(
  8463. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8464. nrows_dst, item_ct1,
  8465. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  8466. tile_x_d_q8_0_acc_ct1.get_pointer(),
  8467. tile_y_qs_acc_ct1.get_pointer(),
  8468. tile_y_ds_acc_ct1.get_pointer());
  8469. });
  8470. });
  8471. }
  8472. } else {
  8473. const bool need_check = true;
  8474. /*
  8475. DPCT1049:29: The work-group size passed to the SYCL kernel may exceed
  8476. the limit. To get the device limit, query
  8477. info::device::max_work_group_size. Adjust the work-group size if needed.
  8478. */
  8479. {
  8480. dpct::has_capability_or_fail(stream->get_device(),
  8481. {sycl::aspect::fp16});
  8482. stream->submit([&](sycl::handler &cgh) {
  8483. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  8484. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8485. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  8486. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  8487. cgh);
  8488. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8489. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8490. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8491. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8492. cgh.parallel_for(
  8493. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8494. [=](sycl::nd_item<3> item_ct1) {
  8495. mul_mat_q8_0<need_check>(
  8496. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8497. nrows_dst, item_ct1,
  8498. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  8499. tile_x_d_q8_0_acc_ct1.get_pointer(),
  8500. tile_y_qs_acc_ct1.get_pointer(),
  8501. tile_y_ds_acc_ct1.get_pointer());
  8502. });
  8503. });
  8504. }
  8505. }
  8506. }
  8507. catch (sycl::exception const &exc) {
  8508. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8509. << ", line:" << __LINE__ << std::endl;
  8510. std::exit(1);
  8511. }
  8512. static void ggml_mul_mat_q2_K_q8_1_sycl(const void *vx, const void *vy,
  8513. float *dst, const int ncols_x,
  8514. const int nrows_x, const int ncols_y,
  8515. const int nrows_y, const int nrows_dst,
  8516. dpct::queue_ptr stream) try {
  8517. int id;
  8518. SYCL_CHECK(
  8519. CHECK_TRY_ERROR(id = get_current_device_index()));
  8520. const int compute_capability = g_device_caps[id].cc;
  8521. int mmq_x, mmq_y, nwarps;
  8522. if (compute_capability >= VER_GEN13) {
  8523. mmq_x = MMQ_X_Q2_K_RDNA2;
  8524. mmq_y = MMQ_Y_Q2_K_RDNA2;
  8525. nwarps = NWARPS_Q2_K_RDNA2;
  8526. } else if (compute_capability >= VER_GEN12) {
  8527. mmq_x = MMQ_X_Q2_K_RDNA1;
  8528. mmq_y = MMQ_Y_Q2_K_RDNA1;
  8529. nwarps = NWARPS_Q2_K_RDNA1;
  8530. } else if (compute_capability >= VER_GEN9) {
  8531. mmq_x = MMQ_X_Q2_K_AMPERE;
  8532. mmq_y = MMQ_Y_Q2_K_AMPERE;
  8533. nwarps = NWARPS_Q2_K_AMPERE;
  8534. } else if (compute_capability >= VER_4VEC) {
  8535. mmq_x = MMQ_X_Q2_K_PASCAL;
  8536. mmq_y = MMQ_Y_Q2_K_PASCAL;
  8537. nwarps = NWARPS_Q2_K_PASCAL;
  8538. } else {
  8539. GGML_ASSERT(false);
  8540. }
  8541. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8542. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8543. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8544. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8545. if (nrows_x % mmq_y == 0) {
  8546. const bool need_check = false;
  8547. /*
  8548. DPCT1049:30: The work-group size passed to the SYCL kernel may exceed
  8549. the limit. To get the device limit, query
  8550. info::device::max_work_group_size. Adjust the work-group size if needed.
  8551. */
  8552. {
  8553. dpct::has_capability_or_fail(stream->get_device(),
  8554. {sycl::aspect::fp16});
  8555. stream->submit([&](sycl::handler &cgh) {
  8556. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  8557. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8558. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  8559. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  8560. cgh);
  8561. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  8562. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  8563. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8564. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8565. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8566. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8567. cgh.parallel_for(
  8568. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8569. [=](sycl::nd_item<3> item_ct1) {
  8570. mul_mat_q2_K<need_check>(
  8571. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8572. nrows_dst, item_ct1,
  8573. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  8574. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  8575. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  8576. tile_y_qs_acc_ct1.get_pointer(),
  8577. tile_y_ds_acc_ct1.get_pointer());
  8578. });
  8579. });
  8580. }
  8581. } else {
  8582. const bool need_check = true;
  8583. /*
  8584. DPCT1049:31: The work-group size passed to the SYCL kernel may exceed
  8585. the limit. To get the device limit, query
  8586. info::device::max_work_group_size. Adjust the work-group size if needed.
  8587. */
  8588. {
  8589. dpct::has_capability_or_fail(stream->get_device(),
  8590. {sycl::aspect::fp16});
  8591. stream->submit([&](sycl::handler &cgh) {
  8592. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  8593. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8594. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  8595. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  8596. cgh);
  8597. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  8598. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  8599. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8600. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8601. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8602. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8603. cgh.parallel_for(
  8604. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8605. [=](sycl::nd_item<3> item_ct1) {
  8606. mul_mat_q2_K<need_check>(
  8607. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8608. nrows_dst, item_ct1,
  8609. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  8610. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  8611. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  8612. tile_y_qs_acc_ct1.get_pointer(),
  8613. tile_y_ds_acc_ct1.get_pointer());
  8614. });
  8615. });
  8616. }
  8617. }
  8618. }
  8619. catch (sycl::exception const &exc) {
  8620. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8621. << ", line:" << __LINE__ << std::endl;
  8622. std::exit(1);
  8623. }
  8624. static void ggml_mul_mat_q3_K_q8_1_sycl(const void *vx, const void *vy,
  8625. float *dst, const int ncols_x,
  8626. const int nrows_x, const int ncols_y,
  8627. const int nrows_y, const int nrows_dst,
  8628. dpct::queue_ptr stream) try {
  8629. #if QK_K == 256
  8630. int id;
  8631. SYCL_CHECK(
  8632. CHECK_TRY_ERROR(id = get_current_device_index()));
  8633. const int compute_capability = g_device_caps[id].cc;
  8634. int mmq_x, mmq_y, nwarps;
  8635. if (compute_capability >= VER_GEN13) {
  8636. mmq_x = MMQ_X_Q3_K_RDNA2;
  8637. mmq_y = MMQ_Y_Q3_K_RDNA2;
  8638. nwarps = NWARPS_Q3_K_RDNA2;
  8639. } else if (compute_capability >= VER_GEN12) {
  8640. mmq_x = MMQ_X_Q3_K_RDNA1;
  8641. mmq_y = MMQ_Y_Q3_K_RDNA1;
  8642. nwarps = NWARPS_Q3_K_RDNA1;
  8643. } else if (compute_capability >= VER_GEN9) {
  8644. mmq_x = MMQ_X_Q3_K_AMPERE;
  8645. mmq_y = MMQ_Y_Q3_K_AMPERE;
  8646. nwarps = NWARPS_Q3_K_AMPERE;
  8647. } else if (compute_capability >= VER_4VEC) {
  8648. mmq_x = MMQ_X_Q3_K_PASCAL;
  8649. mmq_y = MMQ_Y_Q3_K_PASCAL;
  8650. nwarps = NWARPS_Q3_K_PASCAL;
  8651. } else {
  8652. GGML_ASSERT(false);
  8653. }
  8654. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8655. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8656. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8657. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8658. if (nrows_x % mmq_y == 0) {
  8659. const bool need_check = false;
  8660. /*
  8661. DPCT1049:32: The work-group size passed to the SYCL kernel may exceed
  8662. the limit. To get the device limit, query
  8663. info::device::max_work_group_size. Adjust the work-group size if needed.
  8664. */
  8665. {
  8666. dpct::has_capability_or_fail(stream->get_device(),
  8667. {sycl::aspect::fp16});
  8668. stream->submit([&](sycl::handler &cgh) {
  8669. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  8670. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8671. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  8672. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  8673. cgh);
  8674. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  8675. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  8676. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  8677. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  8678. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8679. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8680. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8681. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8682. cgh.parallel_for(
  8683. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8684. [=](sycl::nd_item<3> item_ct1) {
  8685. mul_mat_q3_K<need_check>(
  8686. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8687. nrows_dst, item_ct1,
  8688. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  8689. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  8690. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  8691. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  8692. tile_y_qs_acc_ct1.get_pointer(),
  8693. tile_y_ds_acc_ct1.get_pointer());
  8694. });
  8695. });
  8696. }
  8697. } else {
  8698. const bool need_check = true;
  8699. /*
  8700. DPCT1049:33: The work-group size passed to the SYCL kernel may exceed
  8701. the limit. To get the device limit, query
  8702. info::device::max_work_group_size. Adjust the work-group size if needed.
  8703. */
  8704. {
  8705. dpct::has_capability_or_fail(stream->get_device(),
  8706. {sycl::aspect::fp16});
  8707. stream->submit([&](sycl::handler &cgh) {
  8708. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  8709. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8710. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  8711. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  8712. cgh);
  8713. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  8714. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  8715. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  8716. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  8717. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8718. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8719. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8720. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8721. cgh.parallel_for(
  8722. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8723. [=](sycl::nd_item<3> item_ct1) {
  8724. mul_mat_q3_K<need_check>(
  8725. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8726. nrows_dst, item_ct1,
  8727. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  8728. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  8729. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  8730. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  8731. tile_y_qs_acc_ct1.get_pointer(),
  8732. tile_y_ds_acc_ct1.get_pointer());
  8733. });
  8734. });
  8735. }
  8736. }
  8737. #endif
  8738. }
  8739. catch (sycl::exception const &exc) {
  8740. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8741. << ", line:" << __LINE__ << std::endl;
  8742. std::exit(1);
  8743. }
  8744. static void ggml_mul_mat_q4_K_q8_1_sycl(const void *vx, const void *vy,
  8745. float *dst, const int ncols_x,
  8746. const int nrows_x, const int ncols_y,
  8747. const int nrows_y, const int nrows_dst,
  8748. dpct::queue_ptr stream) try {
  8749. int id;
  8750. SYCL_CHECK(
  8751. CHECK_TRY_ERROR(id = get_current_device_index()));
  8752. const int compute_capability = g_device_caps[id].cc;
  8753. int mmq_x, mmq_y, nwarps;
  8754. if (compute_capability >= VER_GEN13) {
  8755. mmq_x = MMQ_X_Q4_K_RDNA2;
  8756. mmq_y = MMQ_Y_Q4_K_RDNA2;
  8757. nwarps = NWARPS_Q4_K_RDNA2;
  8758. } else if (compute_capability >= VER_GEN12) {
  8759. mmq_x = MMQ_X_Q4_K_RDNA1;
  8760. mmq_y = MMQ_Y_Q4_K_RDNA1;
  8761. nwarps = NWARPS_Q4_K_RDNA1;
  8762. } else if (compute_capability >= VER_GEN9) {
  8763. mmq_x = MMQ_X_Q4_K_AMPERE;
  8764. mmq_y = MMQ_Y_Q4_K_AMPERE;
  8765. nwarps = NWARPS_Q4_K_AMPERE;
  8766. } else if (compute_capability >= VER_4VEC) {
  8767. mmq_x = MMQ_X_Q4_K_PASCAL;
  8768. mmq_y = MMQ_Y_Q4_K_PASCAL;
  8769. nwarps = NWARPS_Q4_K_PASCAL;
  8770. } else {
  8771. GGML_ASSERT(false);
  8772. }
  8773. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8774. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8775. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8776. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8777. if (nrows_x % mmq_y == 0) {
  8778. const bool need_check = false;
  8779. /*
  8780. DPCT1049:34: The work-group size passed to the SYCL kernel may exceed
  8781. the limit. To get the device limit, query
  8782. info::device::max_work_group_size. Adjust the work-group size if needed.
  8783. */
  8784. {
  8785. dpct::has_capability_or_fail(stream->get_device(),
  8786. {sycl::aspect::fp16});
  8787. stream->submit([&](sycl::handler &cgh) {
  8788. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  8789. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8790. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  8791. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  8792. cgh);
  8793. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  8794. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  8795. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8796. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8797. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8798. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8799. cgh.parallel_for(
  8800. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8801. [=](sycl::nd_item<3> item_ct1) {
  8802. mul_mat_q4_K<need_check>(
  8803. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8804. nrows_dst, item_ct1,
  8805. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  8806. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  8807. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  8808. tile_y_qs_acc_ct1.get_pointer(),
  8809. tile_y_ds_acc_ct1.get_pointer());
  8810. });
  8811. });
  8812. }
  8813. } else {
  8814. const bool need_check = true;
  8815. /*
  8816. DPCT1049:35: The work-group size passed to the SYCL kernel may exceed
  8817. the limit. To get the device limit, query
  8818. info::device::max_work_group_size. Adjust the work-group size if needed.
  8819. */
  8820. {
  8821. dpct::has_capability_or_fail(stream->get_device(),
  8822. {sycl::aspect::fp16});
  8823. stream->submit([&](sycl::handler &cgh) {
  8824. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  8825. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8826. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  8827. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  8828. cgh);
  8829. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  8830. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  8831. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8832. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8833. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8834. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8835. cgh.parallel_for(
  8836. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8837. [=](sycl::nd_item<3> item_ct1) {
  8838. mul_mat_q4_K<need_check>(
  8839. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8840. nrows_dst, item_ct1,
  8841. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  8842. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  8843. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  8844. tile_y_qs_acc_ct1.get_pointer(),
  8845. tile_y_ds_acc_ct1.get_pointer());
  8846. });
  8847. });
  8848. }
  8849. }
  8850. }
  8851. catch (sycl::exception const &exc) {
  8852. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8853. << ", line:" << __LINE__ << std::endl;
  8854. std::exit(1);
  8855. }
  8856. static void ggml_mul_mat_q5_K_q8_1_sycl(const void *vx, const void *vy,
  8857. float *dst, const int ncols_x,
  8858. const int nrows_x, const int ncols_y,
  8859. const int nrows_y, const int nrows_dst,
  8860. dpct::queue_ptr stream) try {
  8861. int id;
  8862. SYCL_CHECK(
  8863. CHECK_TRY_ERROR(id = get_current_device_index()));
  8864. const int compute_capability = g_device_caps[id].cc;
  8865. int mmq_x, mmq_y, nwarps;
  8866. if (compute_capability >= VER_GEN13) {
  8867. mmq_x = MMQ_X_Q5_K_RDNA2;
  8868. mmq_y = MMQ_Y_Q5_K_RDNA2;
  8869. nwarps = NWARPS_Q5_K_RDNA2;
  8870. } else if (compute_capability >= VER_GEN12) {
  8871. mmq_x = MMQ_X_Q5_K_RDNA1;
  8872. mmq_y = MMQ_Y_Q5_K_RDNA1;
  8873. nwarps = NWARPS_Q5_K_RDNA1;
  8874. } else if (compute_capability >= VER_GEN9) {
  8875. mmq_x = MMQ_X_Q5_K_AMPERE;
  8876. mmq_y = MMQ_Y_Q5_K_AMPERE;
  8877. nwarps = NWARPS_Q5_K_AMPERE;
  8878. } else if (compute_capability >= VER_4VEC) {
  8879. mmq_x = MMQ_X_Q5_K_PASCAL;
  8880. mmq_y = MMQ_Y_Q5_K_PASCAL;
  8881. nwarps = NWARPS_Q5_K_PASCAL;
  8882. } else {
  8883. GGML_ASSERT(false);
  8884. }
  8885. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8886. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8887. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8888. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8889. if (nrows_x % mmq_y == 0) {
  8890. const bool need_check = false;
  8891. /*
  8892. DPCT1049:36: The work-group size passed to the SYCL kernel may exceed
  8893. the limit. To get the device limit, query
  8894. info::device::max_work_group_size. Adjust the work-group size if needed.
  8895. */
  8896. {
  8897. dpct::has_capability_or_fail(stream->get_device(),
  8898. {sycl::aspect::fp16});
  8899. stream->submit([&](sycl::handler &cgh) {
  8900. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  8901. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8902. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  8903. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  8904. cgh);
  8905. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  8906. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  8907. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8908. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8909. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8910. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8911. cgh.parallel_for(
  8912. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8913. [=](sycl::nd_item<3> item_ct1) {
  8914. mul_mat_q5_K<need_check>(
  8915. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8916. nrows_dst, item_ct1,
  8917. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  8918. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  8919. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  8920. tile_y_qs_acc_ct1.get_pointer(),
  8921. tile_y_ds_acc_ct1.get_pointer());
  8922. });
  8923. });
  8924. }
  8925. } else {
  8926. const bool need_check = true;
  8927. /*
  8928. DPCT1049:37: The work-group size passed to the SYCL kernel may exceed
  8929. the limit. To get the device limit, query
  8930. info::device::max_work_group_size. Adjust the work-group size if needed.
  8931. */
  8932. {
  8933. dpct::has_capability_or_fail(stream->get_device(),
  8934. {sycl::aspect::fp16});
  8935. stream->submit([&](sycl::handler &cgh) {
  8936. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  8937. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8938. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  8939. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  8940. cgh);
  8941. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  8942. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  8943. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8944. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8945. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8946. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8947. cgh.parallel_for(
  8948. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8949. [=](sycl::nd_item<3> item_ct1) {
  8950. mul_mat_q5_K<need_check>(
  8951. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8952. nrows_dst, item_ct1,
  8953. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  8954. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  8955. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  8956. tile_y_qs_acc_ct1.get_pointer(),
  8957. tile_y_ds_acc_ct1.get_pointer());
  8958. });
  8959. });
  8960. }
  8961. }
  8962. }
  8963. catch (sycl::exception const &exc) {
  8964. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8965. << ", line:" << __LINE__ << std::endl;
  8966. std::exit(1);
  8967. }
  8968. static void ggml_mul_mat_q6_K_q8_1_sycl(const void *vx, const void *vy,
  8969. float *dst, const int ncols_x,
  8970. const int nrows_x, const int ncols_y,
  8971. const int nrows_y, const int nrows_dst,
  8972. dpct::queue_ptr stream) try {
  8973. int id;
  8974. SYCL_CHECK(
  8975. CHECK_TRY_ERROR(id = get_current_device_index()));
  8976. const int compute_capability = g_device_caps[id].cc;
  8977. int mmq_x, mmq_y, nwarps;
  8978. if (compute_capability >= VER_GEN13) {
  8979. mmq_x = MMQ_X_Q6_K_RDNA2;
  8980. mmq_y = MMQ_Y_Q6_K_RDNA2;
  8981. nwarps = NWARPS_Q6_K_RDNA2;
  8982. } else if (compute_capability >= VER_GEN12) {
  8983. mmq_x = MMQ_X_Q6_K_RDNA1;
  8984. mmq_y = MMQ_Y_Q6_K_RDNA1;
  8985. nwarps = NWARPS_Q6_K_RDNA1;
  8986. } else if (compute_capability >= VER_GEN9) {
  8987. mmq_x = MMQ_X_Q6_K_AMPERE;
  8988. mmq_y = MMQ_Y_Q6_K_AMPERE;
  8989. nwarps = NWARPS_Q6_K_AMPERE;
  8990. } else if (compute_capability >= VER_4VEC) {
  8991. mmq_x = MMQ_X_Q6_K_PASCAL;
  8992. mmq_y = MMQ_Y_Q6_K_PASCAL;
  8993. nwarps = NWARPS_Q6_K_PASCAL;
  8994. } else {
  8995. GGML_ASSERT(false);
  8996. }
  8997. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8998. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8999. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9000. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9001. if (nrows_x % mmq_y == 0) {
  9002. const bool need_check = false;
  9003. /*
  9004. DPCT1049:38: The work-group size passed to the SYCL kernel may exceed
  9005. the limit. To get the device limit, query
  9006. info::device::max_work_group_size. Adjust the work-group size if needed.
  9007. */
  9008. {
  9009. dpct::has_capability_or_fail(stream->get_device(),
  9010. {sycl::aspect::fp16});
  9011. stream->submit([&](sycl::handler &cgh) {
  9012. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  9013. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9014. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  9015. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  9016. cgh);
  9017. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  9018. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  9019. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9020. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9021. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9022. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9023. cgh.parallel_for(
  9024. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9025. [=](sycl::nd_item<3> item_ct1) {
  9026. mul_mat_q6_K<need_check>(
  9027. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9028. nrows_dst, item_ct1,
  9029. tile_x_ql_acc_ct1.get_pointer(),
  9030. tile_x_dm_acc_ct1.get_pointer(),
  9031. tile_x_sc_acc_ct1.get_pointer(),
  9032. tile_y_qs_acc_ct1.get_pointer(),
  9033. tile_y_ds_acc_ct1.get_pointer());
  9034. });
  9035. });
  9036. }
  9037. } else {
  9038. const bool need_check = true;
  9039. /*
  9040. DPCT1049:39: The work-group size passed to the SYCL kernel may exceed
  9041. the limit. To get the device limit, query
  9042. info::device::max_work_group_size. Adjust the work-group size if needed.
  9043. */
  9044. {
  9045. dpct::has_capability_or_fail(stream->get_device(),
  9046. {sycl::aspect::fp16});
  9047. stream->submit([&](sycl::handler &cgh) {
  9048. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  9049. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9050. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  9051. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  9052. cgh);
  9053. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  9054. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  9055. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9056. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9057. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9058. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9059. cgh.parallel_for(
  9060. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9061. [=](sycl::nd_item<3> item_ct1) {
  9062. mul_mat_q6_K<need_check>(
  9063. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9064. nrows_dst, item_ct1,
  9065. tile_x_ql_acc_ct1.get_pointer(),
  9066. tile_x_dm_acc_ct1.get_pointer(),
  9067. tile_x_sc_acc_ct1.get_pointer(),
  9068. tile_y_qs_acc_ct1.get_pointer(),
  9069. tile_y_ds_acc_ct1.get_pointer());
  9070. });
  9071. });
  9072. }
  9073. }
  9074. }
  9075. catch (sycl::exception const &exc) {
  9076. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9077. << ", line:" << __LINE__ << std::endl;
  9078. std::exit(1);
  9079. }
  9080. static void ggml_mul_mat_p021_f16_f32_sycl(const void *vx, const float *y,
  9081. float *dst, const int ncols_x,
  9082. const int nrows_x,
  9083. const int nchannels_x,
  9084. const int nchannels_y,
  9085. dpct::queue_ptr stream) {
  9086. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  9087. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  9088. {
  9089. dpct::has_capability_or_fail(stream->get_device(),
  9090. {sycl::aspect::fp16});
  9091. stream->parallel_for(
  9092. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9093. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9094. mul_mat_p021_f16_f32(vx, y, dst, ncols_x, nrows_x, nchannels_x,
  9095. nchannels_y, item_ct1);
  9096. });
  9097. }
  9098. }
  9099. static void ggml_mul_mat_vec_nc_f16_f32_sycl(
  9100. const void *vx, const float *y, float *dst, const int ncols_x,
  9101. const int nrows_x, const int row_stride_x, const int nchannels_x,
  9102. const int nchannels_y, const int channel_stride_x, dpct::queue_ptr stream) {
  9103. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  9104. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  9105. {
  9106. dpct::has_capability_or_fail(stream->get_device(),
  9107. {sycl::aspect::fp16});
  9108. stream->parallel_for(
  9109. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9110. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9111. mul_mat_vec_nc_f16_f32(vx, y, dst, ncols_x, nrows_x,
  9112. row_stride_x, channel_stride_x,
  9113. nchannels_y / nchannels_x, item_ct1);
  9114. });
  9115. }
  9116. }
  9117. static void ggml_cpy_f32_f32_sycl(const char *cx, char *cdst, const int ne,
  9118. const int ne00, const int ne01,
  9119. const int nb00, const int nb01,
  9120. const int nb02, const int ne10,
  9121. const int ne11, const int nb10,
  9122. const int nb11, const int nb12,
  9123. dpct::queue_ptr stream) {
  9124. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9125. {
  9126. dpct::has_capability_or_fail(stream->get_device(),
  9127. {sycl::aspect::fp16});
  9128. stream->parallel_for(
  9129. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9130. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9131. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9132. [=](sycl::nd_item<3> item_ct1) {
  9133. cpy_f32_f16<cpy_1_f32_f32>(cx, cdst, ne, ne00, ne01, nb00, nb01,
  9134. nb02, ne10, ne11, nb10, nb11, nb12,
  9135. item_ct1);
  9136. });
  9137. }
  9138. }
  9139. static void ggml_cpy_f32_f16_sycl(const char *cx, char *cdst, const int ne,
  9140. const int ne00, const int ne01,
  9141. const int nb00, const int nb01,
  9142. const int nb02, const int ne10,
  9143. const int ne11, const int nb10,
  9144. const int nb11, const int nb12,
  9145. dpct::queue_ptr stream) {
  9146. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9147. {
  9148. dpct::has_capability_or_fail(stream->get_device(),
  9149. {sycl::aspect::fp16});
  9150. stream->parallel_for(
  9151. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9152. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9153. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9154. [=](sycl::nd_item<3> item_ct1) {
  9155. cpy_f32_f16<cpy_1_f32_f16>(cx, cdst, ne, ne00, ne01, nb00, nb01,
  9156. nb02, ne10, ne11, nb10, nb11, nb12,
  9157. item_ct1);
  9158. });
  9159. }
  9160. }
  9161. static void ggml_cpy_f32_q8_0_sycl(const char *cx, char *cdst, const int ne,
  9162. const int ne00, const int ne01,
  9163. const int nb00, const int nb01,
  9164. const int nb02, const int ne10,
  9165. const int ne11, const int nb10,
  9166. const int nb11, const int nb12,
  9167. dpct::queue_ptr stream) {
  9168. GGML_ASSERT(ne % QK8_0 == 0);
  9169. const int num_blocks = ne / QK8_0;
  9170. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  9171. sycl::range<3>(1, 1, 1)),
  9172. [=](sycl::nd_item<3> item_ct1) {
  9173. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0>(
  9174. cx, cdst, ne, ne00, ne01, nb00, nb01, nb02,
  9175. ne10, ne11, nb10, nb11, nb12, item_ct1);
  9176. });
  9177. }
  9178. static void ggml_cpy_f32_q4_0_sycl(const char *cx, char *cdst, const int ne,
  9179. const int ne00, const int ne01,
  9180. const int nb00, const int nb01,
  9181. const int nb02, const int ne10,
  9182. const int ne11, const int nb10,
  9183. const int nb11, const int nb12,
  9184. dpct::queue_ptr stream) {
  9185. GGML_ASSERT(ne % QK4_0 == 0);
  9186. const int num_blocks = ne / QK4_0;
  9187. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  9188. sycl::range<3>(1, 1, 1)),
  9189. [=](sycl::nd_item<3> item_ct1) {
  9190. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0>(
  9191. cx, cdst, ne, ne00, ne01, nb00, nb01, nb02,
  9192. ne10, ne11, nb10, nb11, nb12, item_ct1);
  9193. });
  9194. }
  9195. static void ggml_cpy_f32_q4_1_sycl(const char *cx, char *cdst, const int ne,
  9196. const int ne00, const int ne01,
  9197. const int nb00, const int nb01,
  9198. const int nb02, const int ne10,
  9199. const int ne11, const int nb10,
  9200. const int nb11, const int nb12,
  9201. dpct::queue_ptr stream) {
  9202. GGML_ASSERT(ne % QK4_1 == 0);
  9203. const int num_blocks = ne / QK4_1;
  9204. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  9205. sycl::range<3>(1, 1, 1)),
  9206. [=](sycl::nd_item<3> item_ct1) {
  9207. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1>(
  9208. cx, cdst, ne, ne00, ne01, nb00, nb01, nb02,
  9209. ne10, ne11, nb10, nb11, nb12, item_ct1);
  9210. });
  9211. }
  9212. static void ggml_cpy_f16_f16_sycl(const char *cx, char *cdst, const int ne,
  9213. const int ne00, const int ne01,
  9214. const int nb00, const int nb01,
  9215. const int nb02, const int ne10,
  9216. const int ne11, const int nb10,
  9217. const int nb11, const int nb12,
  9218. dpct::queue_ptr stream) {
  9219. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9220. {
  9221. dpct::has_capability_or_fail(stream->get_device(),
  9222. {sycl::aspect::fp16});
  9223. stream->parallel_for(
  9224. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9225. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9226. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9227. [=](sycl::nd_item<3> item_ct1) {
  9228. cpy_f32_f16<cpy_1_f16_f16>(cx, cdst, ne, ne00, ne01, nb00, nb01,
  9229. nb02, ne10, ne11, nb10, nb11, nb12,
  9230. item_ct1);
  9231. });
  9232. }
  9233. }
  9234. static void ggml_cpy_i16_i16_sycl(const char *cx, char *cdst, const int ne,
  9235. const int ne00, const int ne01,
  9236. const int nb00, const int nb01,
  9237. const int nb02, const int ne10,
  9238. const int ne11, const int nb10,
  9239. const int nb11, const int nb12,
  9240. dpct::queue_ptr stream) {
  9241. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9242. {
  9243. // dpct::has_capability_or_fail(stream->get_device(),
  9244. // {sycl::aspect::fp16});
  9245. stream->parallel_for(
  9246. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9247. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9248. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9249. [=](sycl::nd_item<3> item_ct1) {
  9250. cpy_f32_f16<cpy_1_i16_i16>(cx, cdst, ne, ne00, ne01, nb00, nb01,
  9251. nb02, ne10, ne11, nb10, nb11, nb12,
  9252. item_ct1);
  9253. });
  9254. }
  9255. }
  9256. static void ggml_cpy_i32_i32_sycl(const char *cx, char *cdst, const int ne,
  9257. const int ne00, const int ne01,
  9258. const int nb00, const int nb01,
  9259. const int nb02, const int ne10,
  9260. const int ne11, const int nb10,
  9261. const int nb11, const int nb12,
  9262. dpct::queue_ptr stream) {
  9263. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9264. {
  9265. // dpct::has_capability_or_fail(stream->get_device(),
  9266. // {sycl::aspect::fp16});
  9267. stream->parallel_for(
  9268. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9269. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9270. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9271. [=](sycl::nd_item<3> item_ct1) {
  9272. cpy_f32_f16<cpy_1_i32_i32>(cx, cdst, ne, ne00, ne01, nb00, nb01,
  9273. nb02, ne10, ne11, nb10, nb11, nb12,
  9274. item_ct1);
  9275. });
  9276. }
  9277. }
  9278. static void scale_f32_sycl(const float *x, float *dst, const float scale,
  9279. const int k, dpct::queue_ptr stream) {
  9280. const int num_blocks = (k + SYCL_SCALE_BLOCK_SIZE - 1) / SYCL_SCALE_BLOCK_SIZE;
  9281. stream->parallel_for(
  9282. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9283. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE),
  9284. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE)),
  9285. [=](sycl::nd_item<3> item_ct1) {
  9286. scale_f32(x, dst, scale, k, item_ct1);
  9287. });
  9288. }
  9289. static void clamp_f32_sycl(const float *x, float *dst, const float min,
  9290. const float max, const int k,
  9291. dpct::queue_ptr stream) {
  9292. const int num_blocks = (k + SYCL_CLAMP_BLOCK_SIZE - 1) / SYCL_CLAMP_BLOCK_SIZE;
  9293. stream->parallel_for(
  9294. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9295. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE),
  9296. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE)),
  9297. [=](sycl::nd_item<3> item_ct1) {
  9298. clamp_f32(x, dst, min, max, k, item_ct1);
  9299. });
  9300. }
  9301. template <typename T>
  9302. static void rope_sycl(const T *x, T *dst, int ncols, int nrows,
  9303. const int32_t *pos, float freq_scale, int p_delta_rows,
  9304. float freq_base, float ext_factor, float attn_factor,
  9305. rope_corr_dims corr_dims, dpct::queue_ptr stream) {
  9306. GGML_ASSERT(ncols % 2 == 0);
  9307. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  9308. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  9309. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  9310. if (pos == nullptr) {
  9311. /*
  9312. DPCT1049:40: The work-group size passed to the SYCL kernel may exceed
  9313. the limit. To get the device limit, query
  9314. info::device::max_work_group_size. Adjust the work-group size if needed.
  9315. */
  9316. dpct::has_capability_or_fail(stream->get_device(),
  9317. {sycl::aspect::fp16});
  9318. stream->parallel_for(
  9319. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9320. [=](sycl::nd_item<3> item_ct1) {
  9321. rope<T, false>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  9322. freq_base, ext_factor, attn_factor, corr_dims,
  9323. item_ct1);
  9324. });
  9325. } else {
  9326. /*
  9327. DPCT1049:41: The work-group size passed to the SYCL kernel may exceed
  9328. the limit. To get the device limit, query
  9329. info::device::max_work_group_size. Adjust the work-group size if needed.
  9330. */
  9331. dpct::has_capability_or_fail(stream->get_device(),
  9332. {sycl::aspect::fp16});
  9333. stream->parallel_for(
  9334. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9335. [=](sycl::nd_item<3> item_ct1) {
  9336. rope<T, true>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  9337. freq_base, ext_factor, attn_factor, corr_dims,
  9338. item_ct1);
  9339. });
  9340. }
  9341. }
  9342. template <typename T>
  9343. static void rope_neox_sycl(const T *x, T *dst, int ncols, int n_dims, int nrows,
  9344. const int32_t *pos, float freq_scale,
  9345. int p_delta_rows, float freq_base, float ext_factor,
  9346. float attn_factor, rope_corr_dims corr_dims,
  9347. dpct::queue_ptr stream) {
  9348. GGML_ASSERT(ncols % 2 == 0);
  9349. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  9350. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  9351. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  9352. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  9353. const float inv_ndims = -1.0f / n_dims;
  9354. if (pos == nullptr) {
  9355. /*
  9356. DPCT1049:42: The work-group size passed to the SYCL kernel may exceed
  9357. the limit. To get the device limit, query
  9358. info::device::max_work_group_size. Adjust the work-group size if needed.
  9359. */
  9360. dpct::has_capability_or_fail(stream->get_device(),
  9361. {sycl::aspect::fp16});
  9362. stream->parallel_for(
  9363. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9364. [=](sycl::nd_item<3> item_ct1) {
  9365. rope_neox<T, false>(x, dst, ncols, n_dims, pos, freq_scale,
  9366. p_delta_rows, ext_factor, attn_factor,
  9367. corr_dims, theta_scale, inv_ndims,
  9368. item_ct1);
  9369. });
  9370. } else {
  9371. /*
  9372. DPCT1049:43: The work-group size passed to the SYCL kernel may exceed
  9373. the limit. To get the device limit, query
  9374. info::device::max_work_group_size. Adjust the work-group size if needed.
  9375. */
  9376. dpct::has_capability_or_fail(stream->get_device(),
  9377. {sycl::aspect::fp16});
  9378. stream->parallel_for(
  9379. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9380. [=](sycl::nd_item<3> item_ct1) {
  9381. rope_neox<T, true>(x, dst, ncols, n_dims, pos, freq_scale,
  9382. p_delta_rows, ext_factor, attn_factor,
  9383. corr_dims, theta_scale, inv_ndims, item_ct1);
  9384. });
  9385. }
  9386. }
  9387. static void rope_glm_f32_sycl(const float *x, float *dst, int ncols, int nrows,
  9388. const int32_t *pos, float freq_scale,
  9389. int p_delta_rows, float freq_base, int n_ctx,
  9390. dpct::queue_ptr stream) {
  9391. GGML_ASSERT(ncols % 4 == 0);
  9392. const sycl::range<3> block_dims(1, 1, SYCL_ROPE_BLOCK_SIZE / 4);
  9393. const int num_blocks_x = (ncols + SYCL_ROPE_BLOCK_SIZE - 1) / SYCL_ROPE_BLOCK_SIZE;
  9394. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  9395. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9396. [=](sycl::nd_item<3> item_ct1) {
  9397. rope_glm_f32(x, dst, ncols, pos, freq_scale,
  9398. p_delta_rows, freq_base, n_ctx,
  9399. item_ct1);
  9400. });
  9401. }
  9402. static void alibi_f32_sycl(const float *x, float *dst, const int ncols,
  9403. const int nrows, const int k_rows,
  9404. const int n_heads_log2_floor, const float m0,
  9405. const float m1, dpct::queue_ptr stream) {
  9406. const sycl::range<3> block_dims(1, 1, SYCL_ALIBI_BLOCK_SIZE);
  9407. const int num_blocks_x = (ncols + SYCL_ALIBI_BLOCK_SIZE - 1) / (SYCL_ALIBI_BLOCK_SIZE);
  9408. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  9409. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9410. [=](sycl::nd_item<3> item_ct1) {
  9411. alibi_f32(x, dst, ncols, k_rows,
  9412. n_heads_log2_floor, m0, m1, item_ct1);
  9413. });
  9414. }
  9415. static void sum_rows_f32_sycl(const float *x, float *dst, const int ncols,
  9416. const int nrows, dpct::queue_ptr stream) {
  9417. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  9418. const sycl::range<3> block_nums(1, nrows, 1);
  9419. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9420. [=](sycl::nd_item<3> item_ct1)
  9421. [[intel::reqd_sub_group_size(32)]] {
  9422. k_sum_rows_f32(x, dst, ncols, item_ct1);
  9423. });
  9424. }
  9425. static void argsort_f32_i32_sycl(const float *x, int *dst, const int ncols,
  9426. const int nrows, ggml_sort_order order,
  9427. dpct::queue_ptr stream) {
  9428. // bitonic sort requires ncols to be power of 2
  9429. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  9430. const sycl::range<3> block_dims(1, 1, ncols);
  9431. const sycl::range<3> block_nums(1, nrows, 1);
  9432. if (order == GGML_SORT_ASC) {
  9433. /*
  9434. DPCT1049:44: The work-group size passed to the SYCL kernel may exceed
  9435. the limit. To get the device limit, query
  9436. info::device::max_work_group_size. Adjust the work-group size if needed.
  9437. */
  9438. stream->parallel_for(
  9439. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9440. [=](sycl::nd_item<3> item_ct1) {
  9441. k_argsort_f32_i32<GGML_SORT_ASC>(x, dst, ncols, item_ct1);
  9442. });
  9443. } else if (order == GGML_SORT_DESC) {
  9444. /*
  9445. DPCT1049:45: The work-group size passed to the SYCL kernel may exceed
  9446. the limit. To get the device limit, query
  9447. info::device::max_work_group_size. Adjust the work-group size if needed.
  9448. */
  9449. stream->parallel_for(
  9450. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9451. [=](sycl::nd_item<3> item_ct1) {
  9452. k_argsort_f32_i32<GGML_SORT_DESC>(x, dst, ncols, item_ct1);
  9453. });
  9454. } else {
  9455. GGML_ASSERT(false);
  9456. }
  9457. }
  9458. static void diag_mask_inf_f32_sycl(const float *x, float *dst,
  9459. const int ncols_x, const int nrows_x,
  9460. const int rows_per_channel, const int n_past,
  9461. dpct::queue_ptr stream) {
  9462. const sycl::range<3> block_dims(1, SYCL_DIAG_MASK_INF_BLOCK_SIZE, 1);
  9463. const int block_num_x = (ncols_x + SYCL_DIAG_MASK_INF_BLOCK_SIZE - 1) / SYCL_DIAG_MASK_INF_BLOCK_SIZE;
  9464. const sycl::range<3> block_nums(1, block_num_x, nrows_x);
  9465. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9466. [=](sycl::nd_item<3> item_ct1) {
  9467. diag_mask_inf_f32(x, dst, ncols_x,
  9468. rows_per_channel, n_past,
  9469. item_ct1);
  9470. });
  9471. }
  9472. static void soft_max_f32_sycl(const float *x, const float *y, float *dst,
  9473. const int ncols_x, const int nrows_x,
  9474. const int nrows_y, const float scale,
  9475. dpct::queue_ptr stream) {
  9476. int nth = WARP_SIZE;
  9477. while (nth < ncols_x && nth < SYCL_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  9478. const sycl::range<3> block_dims(1, 1, nth);
  9479. const sycl::range<3> block_nums(1, 1, nrows_x);
  9480. /*
  9481. DPCT1049:46: The work-group size passed to the SYCL kernel may exceed the
  9482. limit. To get the device limit, query info::device::max_work_group_size.
  9483. Adjust the work-group size if needed.
  9484. */
  9485. stream->submit([&](sycl::handler &cgh) {
  9486. /*
  9487. DPCT1101:96: 'SYCL_SOFT_MAX_BLOCK_SIZE/WARP_SIZE' expression was
  9488. replaced with a value. Modify the code to use the original expression,
  9489. provided in comments, if it is correct.
  9490. */
  9491. sycl::local_accessor<float, 1> buf_acc_ct1(
  9492. sycl::range<1>(32 /*SYCL_SOFT_MAX_BLOCK_SIZE/WARP_SIZE*/), cgh);
  9493. cgh.parallel_for(
  9494. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9495. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9496. soft_max_f32(x, y, dst, ncols_x, nrows_y, scale, item_ct1,
  9497. buf_acc_ct1.get_pointer());
  9498. });
  9499. });
  9500. }
  9501. static void im2col_f32_f16_sycl(const float *x, sycl::half *dst, int IW, int IH,
  9502. int OW, int OH, int KW, int KH, int IC,
  9503. int offset_delta, int s0, int s1, int p0,
  9504. int p1, int d0, int d1,
  9505. dpct::queue_ptr stream) {
  9506. const int parallel_elements = OW * KW * KH;
  9507. const int num_blocks = (parallel_elements + SYCL_IM2COL_BLOCK_SIZE - 1) / SYCL_IM2COL_BLOCK_SIZE;
  9508. sycl::range<3> block_nums(IC, OH, num_blocks);
  9509. {
  9510. dpct::has_capability_or_fail(stream->get_device(),
  9511. {sycl::aspect::fp16});
  9512. stream->parallel_for(
  9513. sycl::nd_range<3>(block_nums *
  9514. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  9515. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  9516. [=](sycl::nd_item<3> item_ct1) {
  9517. im2col_f32_f16(x, dst, offset_delta, IW, IH, OW, KW, KH,
  9518. parallel_elements, (IC * KH * KW), s0, s1, p0,
  9519. p1, d0, d1, item_ct1);
  9520. });
  9521. }
  9522. }
  9523. // buffer pool for sycl
  9524. #define MAX_SYCL_BUFFERS 256
  9525. struct scoped_spin_lock {
  9526. std::atomic_flag& lock;
  9527. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  9528. while (lock.test_and_set(std::memory_order_acquire)) {
  9529. ; // spin
  9530. }
  9531. }
  9532. ~scoped_spin_lock() {
  9533. lock.clear(std::memory_order_release);
  9534. }
  9535. scoped_spin_lock(const scoped_spin_lock&) = delete;
  9536. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  9537. };
  9538. static std::atomic_flag g_sycl_pool_lock = ATOMIC_FLAG_INIT;
  9539. // #define DEBUG_SYCL_MALLOC
  9540. struct sycl_buffer {
  9541. void * ptr = nullptr;
  9542. size_t size = 0;
  9543. };
  9544. static sycl_buffer g_sycl_buffer_pool[GGML_SYCL_MAX_DEVICES][MAX_SYCL_BUFFERS];
  9545. static size_t g_sycl_pool_size[GGML_SYCL_MAX_DEVICES] = {0};
  9546. static void *ggml_sycl_pool_malloc_leg(size_t size, size_t *actual_size) try {
  9547. scoped_spin_lock lock(g_sycl_pool_lock);
  9548. int id;
  9549. SYCL_CHECK(
  9550. CHECK_TRY_ERROR(id = get_current_device_index()));
  9551. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg index %d\n", id);
  9552. #ifdef DEBUG_SYCL_MALLOC
  9553. int nnz = 0;
  9554. size_t max_size = 0;
  9555. #endif
  9556. size_t best_diff = 1ull << 36;
  9557. int ibest = -1;
  9558. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  9559. sycl_buffer& b = g_sycl_buffer_pool[id][i];
  9560. if (b.ptr != nullptr) {
  9561. #ifdef DEBUG_SYCL_MALLOC
  9562. ++nnz;
  9563. if (b.size > max_size) max_size = b.size;
  9564. #endif
  9565. if (b.size >= size) {
  9566. size_t diff = b.size - size;
  9567. if (diff < best_diff) {
  9568. best_diff = diff;
  9569. ibest = i;
  9570. if (!best_diff) {
  9571. void * ptr = b.ptr;
  9572. *actual_size = b.size;
  9573. b.ptr = nullptr;
  9574. b.size = 0;
  9575. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 1 %p\n", ptr);
  9576. return ptr;
  9577. }
  9578. }
  9579. }
  9580. }
  9581. }
  9582. if (ibest >= 0) {
  9583. sycl_buffer& b = g_sycl_buffer_pool[id][ibest];
  9584. void * ptr = b.ptr;
  9585. *actual_size = b.size;
  9586. b.ptr = nullptr;
  9587. b.size = 0;
  9588. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 2 %p\n", ptr);
  9589. return ptr;
  9590. }
  9591. void * ptr;
  9592. size_t look_ahead_size = (size_t) (1.05 * size);
  9593. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  9594. const dpct::queue_ptr stream = g_syclStreams[id][0];
  9595. SYCL_CHECK(
  9596. CHECK_TRY_ERROR(ptr = (void *)sycl::malloc_device(
  9597. look_ahead_size, *stream)));
  9598. *actual_size = look_ahead_size;
  9599. g_sycl_pool_size[id] += look_ahead_size;
  9600. #ifdef DEBUG_SYCL_MALLOC
  9601. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  9602. (uint32_t)(max_size/1024/1024), (uint32_t)(g_sycl_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  9603. #endif
  9604. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return %p\n", ptr);
  9605. return ptr;
  9606. }
  9607. catch (sycl::exception const &exc) {
  9608. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9609. << ", line:" << __LINE__ << std::endl;
  9610. std::exit(1);
  9611. }
  9612. static void ggml_sycl_pool_free_leg(void *ptr, size_t size) try {
  9613. scoped_spin_lock lock(g_sycl_pool_lock);
  9614. int id;
  9615. SYCL_CHECK(
  9616. CHECK_TRY_ERROR(id = get_current_device_index()));
  9617. const dpct::queue_ptr stream = g_syclStreams[id][0];
  9618. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  9619. sycl_buffer& b = g_sycl_buffer_pool[id][i];
  9620. if (b.ptr == nullptr) {
  9621. b.ptr = ptr;
  9622. b.size = size;
  9623. return;
  9624. }
  9625. }
  9626. fprintf(stderr, "WARNING: sycl buffer pool full, increase MAX_SYCL_BUFFERS\n");
  9627. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *stream)));
  9628. g_sycl_pool_size[id] -= size;
  9629. }
  9630. catch (sycl::exception const &exc) {
  9631. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9632. << ", line:" << __LINE__ << std::endl;
  9633. std::exit(1);
  9634. }
  9635. // pool with virtual memory
  9636. /*
  9637. DPCT1082:64: Migration of CUmemGenericAllocationHandle type is not supported.
  9638. */
  9639. // static std::vector<CUmemGenericAllocationHandle>
  9640. // g_sycl_pool_handles[GGML_SYCL_MAX_DEVICES];
  9641. static dpct::device_ptr g_sycl_pool_addr[GGML_SYCL_MAX_DEVICES] = {0};
  9642. static size_t g_sycl_pool_used[GGML_SYCL_MAX_DEVICES] = {0};
  9643. static const size_t SYCL_POOL_VMM_MAX_SIZE = 1ull << 36; // 64 GB
  9644. static void *ggml_sycl_pool_malloc_vmm(size_t size, size_t *actual_size) try {
  9645. return NULL;
  9646. }
  9647. catch (sycl::exception const &exc) {
  9648. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9649. << ", line:" << __LINE__ << std::endl;
  9650. std::exit(1);
  9651. }
  9652. static void ggml_sycl_pool_free_vmm(void *ptr, size_t size) try {
  9653. scoped_spin_lock lock(g_sycl_pool_lock);
  9654. int id;
  9655. SYCL_CHECK(
  9656. CHECK_TRY_ERROR(id = dpct::dev_mgr::instance().current_device_id()));
  9657. #ifdef DEBUG_SYCL_MALLOC
  9658. printf("sycl pool[%d]: freed %llu bytes at %llx\n", id, (unsigned long long) size, ptr);
  9659. #endif
  9660. g_sycl_pool_used[id] -= size;
  9661. // all deallocations must be in reverse order of the allocations
  9662. GGML_ASSERT(ptr == (void *) (g_sycl_pool_addr[id] + g_sycl_pool_used[id]));
  9663. }
  9664. catch (sycl::exception const &exc) {
  9665. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9666. << ", line:" << __LINE__ << std::endl;
  9667. std::exit(1);
  9668. }
  9669. static void *ggml_sycl_pool_malloc(size_t size, size_t *actual_size) try {
  9670. int id;
  9671. SYCL_CHECK(
  9672. CHECK_TRY_ERROR(id = get_current_device_index()));
  9673. if (g_device_caps[id].vmm) {
  9674. return ggml_sycl_pool_malloc_vmm(size, actual_size);
  9675. } else {
  9676. return ggml_sycl_pool_malloc_leg(size, actual_size);
  9677. }
  9678. }
  9679. catch (sycl::exception const &exc) {
  9680. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9681. << ", line:" << __LINE__ << std::endl;
  9682. std::exit(1);
  9683. }
  9684. static void ggml_sycl_pool_free(void *ptr, size_t size) try {
  9685. int id;
  9686. SYCL_CHECK(
  9687. CHECK_TRY_ERROR(id = get_current_device_index()));
  9688. if (g_device_caps[id].vmm) {
  9689. ggml_sycl_pool_free_vmm(ptr, size);
  9690. } else {
  9691. ggml_sycl_pool_free_leg(ptr, size);
  9692. }
  9693. }
  9694. catch (sycl::exception const &exc) {
  9695. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9696. << ", line:" << __LINE__ << std::endl;
  9697. std::exit(1);
  9698. }
  9699. template<typename T>
  9700. struct sycl_pool_alloc {
  9701. T * ptr = nullptr;
  9702. size_t actual_size = 0;
  9703. // size is in number of elements
  9704. T * alloc(size_t size) {
  9705. GGML_ASSERT(ptr == nullptr);
  9706. ptr = (T *) ggml_sycl_pool_malloc(size * sizeof(T), &this->actual_size);
  9707. // GGML_SYCL_DEBUG("alloc %lu return %p actual size=%lu\n", size * sizeof(T), ptr, this->actual_size);
  9708. return ptr;
  9709. }
  9710. sycl_pool_alloc(size_t size) {
  9711. alloc(size);
  9712. }
  9713. ~sycl_pool_alloc() {
  9714. if (ptr != nullptr) {
  9715. ggml_sycl_pool_free(ptr, actual_size);
  9716. }
  9717. }
  9718. T * get() {
  9719. return ptr;
  9720. }
  9721. sycl_pool_alloc() = default;
  9722. sycl_pool_alloc(const sycl_pool_alloc &) = delete;
  9723. sycl_pool_alloc(sycl_pool_alloc &&) = delete;
  9724. sycl_pool_alloc& operator=(const sycl_pool_alloc &) = delete;
  9725. sycl_pool_alloc& operator=(sycl_pool_alloc &&) = delete;
  9726. };
  9727. static bool g_sycl_loaded = false;
  9728. bool ggml_sycl_loaded(void) {
  9729. return g_sycl_loaded;
  9730. }
  9731. void ggml_backend_sycl_print_sycl_devices(){
  9732. int device_count = dpct::dev_mgr::instance().device_count();
  9733. fprintf(stderr, "found %d SYCL devices:\n", device_count);
  9734. for (int id = 0; id < device_count; ++id) {
  9735. dpct::device_info prop;
  9736. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  9737. prop, dpct::dev_mgr::instance().get_device(id))));
  9738. sycl::device cur_device = dpct::dev_mgr::instance().get_device(id);
  9739. 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,
  9740. prop.get_name(), prop.get_major_version(),
  9741. prop.get_minor_version(),
  9742. prop.get_max_compute_units(),
  9743. prop.get_max_work_group_size(),
  9744. prop.get_max_sub_group_size(),
  9745. prop.get_global_mem_size()
  9746. );
  9747. }
  9748. // fprintf(stderr, "\n");
  9749. }
  9750. int get_sycl_env(const char* env_name, int default_val){
  9751. char * user_device_string = getenv(env_name);
  9752. int user_number = default_val;
  9753. unsigned n;
  9754. if (user_device_string != NULL && sscanf(user_device_string, " %u", &n) == 1) {
  9755. user_number = (int)n;
  9756. } else {
  9757. user_number=default_val;
  9758. }
  9759. return user_number;
  9760. }
  9761. int get_work_group_size(int user_device_id){
  9762. dpct::device_info prop;
  9763. dpct::get_device_info(
  9764. prop,
  9765. dpct::dev_mgr::instance().get_device(user_device_id));
  9766. return prop.get_max_work_group_size();
  9767. }
  9768. void ggml_init_sycl() try {
  9769. static bool initialized = false;
  9770. if (!initialized) {
  9771. g_ggml_sycl_debug = get_sycl_env("GGML_SYCL_DEBUG", 0);
  9772. printf("GGML_SYCL_DEBUG=%d\n", g_ggml_sycl_debug);
  9773. int user_device_id = get_sycl_env("GGML_SYCL_DEVICE", 0);
  9774. if (CHECK_TRY_ERROR(g_all_sycl_device_count =
  9775. dpct::dev_mgr::instance().device_count()) !=
  9776. 0) {
  9777. initialized = true;
  9778. g_sycl_loaded = false;
  9779. return;
  9780. }
  9781. GGML_ASSERT(g_all_sycl_device_count <= GGML_SYCL_MAX_DEVICES);
  9782. int64_t total_vram = 0;
  9783. #if defined(GGML_SYCL_FP16)
  9784. fprintf(stderr, "%s: GGML_SYCL_FP16: yes\n", __func__);
  9785. #else
  9786. fprintf(stderr, "%s: GGML_SYCL_FP16: no\n", __func__);
  9787. #endif
  9788. #if defined(SYCL_USE_XMX)
  9789. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  9790. #else
  9791. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  9792. #endif
  9793. ggml_backend_sycl_print_sycl_devices();
  9794. for (int id = 0; id < GGML_SYCL_MAX_DEVICES; ++id) {
  9795. g_sycl_device_id2index[id].index = -1;
  9796. g_device_caps[id].vmm = 0;
  9797. g_device_caps[id].device_id = -1;
  9798. g_device_caps[id].cc = 0;
  9799. g_tensor_split[id] = 0;
  9800. }
  9801. int device_inx = -1;
  9802. for (int id = 0; id < g_all_sycl_device_count; ++id) {
  9803. if(id!=user_device_id) continue;
  9804. device_inx++;
  9805. int device_vmm = 0;
  9806. g_device_caps[device_inx].vmm = !!device_vmm;
  9807. g_device_caps[device_inx].device_id = id;
  9808. g_sycl_device_id2index[id].index = device_inx;
  9809. dpct::device_info prop;
  9810. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  9811. prop, dpct::dev_mgr::instance().get_device(id))));
  9812. // fprintf(stderr,
  9813. // " Device %d: %s, compute capability %d.%d, VMM: %s\n", id,
  9814. // prop.get_name(), prop.get_major_version(),
  9815. // prop.get_minor_version(), device_vmm ? "yes" : "no");
  9816. g_tensor_split[device_inx] = total_vram;
  9817. total_vram += prop.get_global_mem_size();
  9818. g_device_caps[device_inx].cc =
  9819. 100 * prop.get_major_version() + 10 * prop.get_minor_version();
  9820. // printf("g_device_caps[%d].cc=%d\n", device_inx, g_device_caps[device_inx].cc);
  9821. }
  9822. device_inx = -1;
  9823. for (int id = 0; id < g_all_sycl_device_count; ++id) {
  9824. if(id!=user_device_id) continue;
  9825. device_inx++;
  9826. g_tensor_split[device_inx] /= total_vram;
  9827. }
  9828. device_inx = -1;
  9829. for (int id = 0; id < g_all_sycl_device_count; ++id) {
  9830. if(id!=user_device_id) continue;
  9831. device_inx++;
  9832. SYCL_CHECK(ggml_sycl_set_device(id));
  9833. // create sycl streams
  9834. for (int is = 0; is < MAX_STREAMS; ++is) {
  9835. /*
  9836. DPCT1025:88: The SYCL queue is created ignoring the flag and
  9837. priority options.
  9838. */
  9839. SYCL_CHECK(CHECK_TRY_ERROR(
  9840. g_syclStreams[device_inx][is] =
  9841. dpct::get_current_device().create_queue()));
  9842. }
  9843. const dpct::queue_ptr stream = g_syclStreams[device_inx][0];
  9844. // create sycl handle
  9845. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[device_inx] =
  9846. stream));
  9847. /*
  9848. DPCT1027:89: The call to syclSetMathMode was replaced with 0
  9849. because this functionality is redundant in SYCL.
  9850. */
  9851. SYCL_CHECK(0);
  9852. }
  9853. // configure logging to stdout
  9854. // SYCL_CHECK(syclLoggerConfigure(1, 1, 0, nullptr));
  9855. //hardcode, force set to 1 device
  9856. g_device_count = 1;
  9857. ggml_sycl_set_main_device(user_device_id);
  9858. ggml_sycl_set_device(user_device_id);
  9859. g_work_group_size = get_work_group_size(user_device_id);
  9860. // fprintf(stderr, "Using Device %d\n", user_device_id);
  9861. // for (int id = 0; id < g_all_sycl_device_count; ++id) {
  9862. // GGML_SYCL_DEBUG("id=%d g_device_caps[%d].device_id=%d g_sycl_device_id2index[%d].index=%d ", id, id,
  9863. // g_device_caps[id].device_id, id, g_sycl_device_id2index[id].index);
  9864. // }
  9865. initialized = true;
  9866. g_sycl_loaded = true;
  9867. }
  9868. }
  9869. catch (sycl::exception const &exc) {
  9870. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9871. << ", line:" << __LINE__ << std::endl;
  9872. std::exit(1);
  9873. }
  9874. void ggml_sycl_set_tensor_split(const float * tensor_split) {
  9875. if (tensor_split == nullptr) {
  9876. return;
  9877. }
  9878. bool all_zero = true;
  9879. for (int i = 0; i < g_device_count; ++i) {
  9880. if (tensor_split[i] != 0.0f) {
  9881. all_zero = false;
  9882. break;
  9883. }
  9884. }
  9885. if (all_zero) {
  9886. return;
  9887. }
  9888. float split_sum = 0.0f;
  9889. for (int i = 0; i < g_device_count; ++i) {
  9890. g_tensor_split[i] = split_sum;
  9891. split_sum += tensor_split[i];
  9892. }
  9893. for (int i = 0; i < g_device_count; ++i) {
  9894. g_tensor_split[i] /= split_sum;
  9895. }
  9896. }
  9897. void *ggml_sycl_host_malloc(size_t size) try {
  9898. if (getenv("GGML_SYCL_NO_PINNED") != nullptr) {
  9899. return nullptr;
  9900. }
  9901. void * ptr = nullptr;
  9902. //allow to use dpct::get_in_order_queue() for host malloc
  9903. dpct::err0 err = CHECK_TRY_ERROR(
  9904. ptr = (void *)sycl::malloc_host(size, dpct::get_in_order_queue()));
  9905. /*
  9906. DPCT1000:82: Error handling if-stmt was detected but could not be rewritten.
  9907. */
  9908. if (err != 0) {
  9909. // clear the error
  9910. /*
  9911. DPCT1026:83: The call to syclGetLastError was removed because this
  9912. functionality is redundant in SYCL.
  9913. */
  9914. /*
  9915. DPCT1001:81: The statement could not be removed.
  9916. */
  9917. fprintf(
  9918. stderr,
  9919. "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  9920. /*
  9921. DPCT1009:84: SYCL uses exceptions to report errors and does not use
  9922. the error codes. The original code was commented out and a warning
  9923. string was inserted. You need to rewrite this code.
  9924. */
  9925. size / 1024.0 / 1024.0,
  9926. "syclGetErrorString is not supported" /*syclGetErrorString(err)*/);
  9927. return nullptr;
  9928. }
  9929. return ptr;
  9930. }
  9931. catch (sycl::exception const &exc) {
  9932. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9933. << ", line:" << __LINE__ << std::endl;
  9934. std::exit(1);
  9935. }
  9936. void ggml_sycl_host_free(void *ptr) try {
  9937. //allow to use dpct::get_in_order_queue() for host malloc
  9938. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, dpct::get_in_order_queue())));
  9939. }
  9940. catch (sycl::exception const &exc) {
  9941. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9942. << ", line:" << __LINE__ << std::endl;
  9943. std::exit(1);
  9944. }
  9945. static dpct::err0 ggml_sycl_cpy_tensor_2d(void *dst,
  9946. const struct ggml_tensor *src,
  9947. int64_t i3, int64_t i2,
  9948. int64_t i1_low, int64_t i1_high,
  9949. dpct::queue_ptr stream) try {
  9950. dpct::memcpy_direction kind;
  9951. char * src_ptr;
  9952. if (src->backend == GGML_BACKEND_CPU) {
  9953. kind = dpct::host_to_device;
  9954. src_ptr = (char *) src->data;
  9955. // GGML_SYCL_DEBUG("ggml_sycl_cpy_tensor_2d GGML_BACKEND_CPU src_ptr %p\n", src_ptr);
  9956. } else if (src->backend == GGML_BACKEND_GPU || src->backend == GGML_BACKEND_GPU_SPLIT) {
  9957. GGML_ASSERT(src->backend != GGML_BACKEND_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  9958. kind = dpct::device_to_device;
  9959. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  9960. int id;
  9961. SYCL_CHECK(CHECK_TRY_ERROR(
  9962. id = get_current_device_index()));
  9963. // GGML_SYCL_DEBUG("current device index %d\n", id);
  9964. src_ptr = (char *) extra->data_device[id];
  9965. } else {
  9966. // GGML_SYCL_DEBUG("GGML_ASSERT(false)\n");
  9967. GGML_ASSERT(false);
  9968. }
  9969. char * dst_ptr = (char *) dst;
  9970. const int64_t ne0 = src->ne[0];
  9971. const int64_t nb0 = src->nb[0];
  9972. const int64_t nb1 = src->nb[1];
  9973. const int64_t nb2 = src->nb[2];
  9974. const int64_t nb3 = src->nb[3];
  9975. const enum ggml_type type = src->type;
  9976. const int64_t ts = ggml_type_size(type);
  9977. const int64_t bs = ggml_blck_size(type);
  9978. int64_t i1_diff = i1_high - i1_low;
  9979. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  9980. if (nb0 == ts && nb1 == ts*ne0/bs) {
  9981. // GGML_SYCL_DEBUG("stream->memcpy: dst_ptr=%p, x=%p, size=%lu\n", dst_ptr, x, i1_diff * nb1);
  9982. // return CHECK_TRY_ERROR(stream->memcpy(dst_ptr, x, i1_diff * nb1));
  9983. return CHECK_TRY_ERROR(dpct::async_dpct_memcpy(dst_ptr, x, i1_diff * nb1,
  9984. kind, *stream));
  9985. } else if (nb0 == ts) {
  9986. return CHECK_TRY_ERROR(
  9987. dpct::async_dpct_memcpy(dst_ptr, ts * ne0 / bs, x, nb1,
  9988. ts * ne0 / bs, i1_diff, kind, *stream));
  9989. } else {
  9990. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  9991. const void * rx = (const void *) ((const char *) x + i1*nb1);
  9992. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  9993. // pretend the row is a matrix with cols=1
  9994. dpct::err0 r = CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  9995. rd, ts / bs, rx, nb0, ts / bs, ne0, kind, *stream));
  9996. /*
  9997. DPCT1001:85: The statement could not be removed.
  9998. */
  9999. /*
  10000. DPCT1000:86: Error handling if-stmt was detected but could not be
  10001. rewritten.
  10002. */
  10003. if (r != 0) return r;
  10004. }
  10005. return 0;
  10006. }
  10007. }
  10008. catch (sycl::exception const &exc) {
  10009. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10010. << ", line:" << __LINE__ << std::endl;
  10011. std::exit(1);
  10012. }
  10013. static void ggml_sycl_op_get_rows(const ggml_tensor *src0,
  10014. const ggml_tensor *src1, ggml_tensor *dst,
  10015. const float *src0_d, const float *src1_d,
  10016. float *dst_d, const dpct::queue_ptr &stream) {
  10017. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  10018. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10019. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  10020. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  10021. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  10022. const int32_t * src1_i32 = (const int32_t *) src1_d;
  10023. switch (src0->type) {
  10024. case GGML_TYPE_F16:
  10025. get_rows_sycl_float(src0, src1, dst, (const sycl::half *)src0_d,
  10026. src1_i32, dst_d, stream);
  10027. break;
  10028. case GGML_TYPE_F32:
  10029. get_rows_sycl_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10030. break;
  10031. case GGML_TYPE_Q4_0:
  10032. get_rows_sycl<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10033. break;
  10034. case GGML_TYPE_Q4_1:
  10035. get_rows_sycl<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10036. break;
  10037. case GGML_TYPE_Q5_0:
  10038. get_rows_sycl<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10039. break;
  10040. case GGML_TYPE_Q5_1:
  10041. get_rows_sycl<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10042. break;
  10043. case GGML_TYPE_Q8_0:
  10044. get_rows_sycl<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10045. break;
  10046. default:
  10047. // TODO: k-quants
  10048. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  10049. GGML_ASSERT(false);
  10050. break;
  10051. }
  10052. }
  10053. template <class op>
  10054. inline void ggml_sycl_op_bin_bcast(const ggml_tensor *src0,
  10055. const ggml_tensor *src1, ggml_tensor *dst,
  10056. const float *src0_dd, const float *src1_dd,
  10057. float *dst_dd,
  10058. const dpct::queue_ptr &main_stream) {
  10059. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  10060. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  10061. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  10062. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd,
  10063. (sycl::half *)dst_dd, main_stream);
  10064. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  10065. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd, dst_dd,
  10066. main_stream);
  10067. } else if (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  10068. op()(src0, src1, dst, (const int32_t *)src0_dd, (const int32_t *)src1_dd, (int32_t *)dst_dd,
  10069. main_stream);
  10070. } else if (src0->type == GGML_TYPE_I16 && dst->type == GGML_TYPE_I16) {
  10071. op()(src0, src1, dst, (const int16_t *)src0_dd, (const int16_t *)src1_dd, (int16_t *)dst_dd,
  10072. main_stream);
  10073. } else {
  10074. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  10075. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  10076. GGML_ASSERT(false);
  10077. }
  10078. }
  10079. static void ggml_sycl_op_repeat(const ggml_tensor *src0,
  10080. const ggml_tensor *src1, ggml_tensor *dst,
  10081. const float *src0_d, const float *src1_d,
  10082. float *dst_d,
  10083. const dpct::queue_ptr &main_stream) {
  10084. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  10085. (void) src1;
  10086. (void) src1_d;
  10087. }
  10088. inline void ggml_sycl_op_add(const ggml_tensor *src0, const ggml_tensor *src1,
  10089. ggml_tensor *dst, const float *src0_dd,
  10090. const float *src1_dd, float *dst_dd,
  10091. const dpct::queue_ptr &main_stream) {
  10092. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  10093. }
  10094. inline void ggml_sycl_op_acc(const ggml_tensor *src0, const ggml_tensor *src1,
  10095. ggml_tensor *dst, const float *src0_dd,
  10096. const float *src1_dd, float *dst_dd,
  10097. const dpct::queue_ptr &main_stream) {
  10098. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10099. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10100. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10101. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  10102. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  10103. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  10104. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  10105. int offset = dst->op_params[3] / 4; // offset in bytes
  10106. 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);
  10107. (void) dst;
  10108. }
  10109. inline void ggml_sycl_op_mul(const ggml_tensor *src0, const ggml_tensor *src1,
  10110. ggml_tensor *dst, const float *src0_dd,
  10111. const float *src1_dd, float *dst_dd,
  10112. const dpct::queue_ptr &main_stream) {
  10113. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  10114. }
  10115. inline void ggml_sycl_op_div(const ggml_tensor *src0, const ggml_tensor *src1,
  10116. ggml_tensor *dst, const float *src0_dd,
  10117. const float *src1_dd, float *dst_dd,
  10118. const dpct::queue_ptr &main_stream) {
  10119. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  10120. }
  10121. inline void ggml_sycl_op_gelu(const ggml_tensor *src0, const ggml_tensor *src1,
  10122. ggml_tensor *dst, const float *src0_dd,
  10123. const float *src1_dd, float *dst_dd,
  10124. const dpct::queue_ptr &main_stream) {
  10125. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10126. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10127. gelu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10128. (void) src1;
  10129. (void) dst;
  10130. (void) src1_dd;
  10131. }
  10132. inline void ggml_sycl_op_silu(const ggml_tensor *src0, const ggml_tensor *src1,
  10133. ggml_tensor *dst, const float *src0_dd,
  10134. const float *src1_dd, float *dst_dd,
  10135. const dpct::queue_ptr &main_stream) {
  10136. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10137. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10138. silu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10139. (void) src1;
  10140. (void) dst;
  10141. (void) src1_dd;
  10142. }
  10143. inline void ggml_sycl_op_gelu_quick(const ggml_tensor *src0,
  10144. const ggml_tensor *src1, ggml_tensor *dst,
  10145. const float *src0_dd, const float *src1_dd,
  10146. float *dst_dd,
  10147. const dpct::queue_ptr &main_stream) {
  10148. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10149. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10150. gelu_quick_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10151. (void) src1;
  10152. (void) dst;
  10153. (void) src1_dd;
  10154. }
  10155. inline void ggml_sycl_op_tanh(const ggml_tensor *src0, const ggml_tensor *src1,
  10156. ggml_tensor *dst, const float *src0_dd,
  10157. const float *src1_dd, float *dst_dd,
  10158. const dpct::queue_ptr &main_stream) {
  10159. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10160. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10161. tanh_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10162. (void) src1;
  10163. (void) dst;
  10164. (void) src1_dd;
  10165. }
  10166. inline void ggml_sycl_op_relu(const ggml_tensor *src0, const ggml_tensor *src1,
  10167. ggml_tensor *dst, const float *src0_dd,
  10168. const float *src1_dd, float *dst_dd,
  10169. const dpct::queue_ptr &main_stream) {
  10170. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10171. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10172. relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10173. (void) src1;
  10174. (void) dst;
  10175. (void) src1_dd;
  10176. }
  10177. inline void ggml_sycl_op_leaky_relu(const ggml_tensor *src0,
  10178. const ggml_tensor *src1, ggml_tensor *dst,
  10179. const float *src0_dd, const float *src1_dd,
  10180. float *dst_dd,
  10181. const dpct::queue_ptr &main_stream) {
  10182. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10183. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10184. float negative_slope;
  10185. memcpy(&negative_slope, dst->op_params, sizeof(float));
  10186. leaky_relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  10187. (void) src1;
  10188. (void) dst;
  10189. (void) src1_dd;
  10190. }
  10191. inline void ggml_sycl_op_sqr(const ggml_tensor *src0, const ggml_tensor *src1,
  10192. ggml_tensor *dst, const float *src0_dd,
  10193. const float *src1_dd, float *dst_dd,
  10194. const dpct::queue_ptr &main_stream) {
  10195. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10196. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10197. sqr_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10198. (void) src1;
  10199. (void) dst;
  10200. (void) src1_dd;
  10201. }
  10202. inline void ggml_sycl_op_norm(const ggml_tensor *src0, const ggml_tensor *src1,
  10203. ggml_tensor *dst, const float *src0_dd,
  10204. const float *src1_dd, float *dst_dd,
  10205. const dpct::queue_ptr &main_stream) {
  10206. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10207. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10208. const int64_t ne00 = src0->ne[0];
  10209. const int64_t nrows = ggml_nrows(src0);
  10210. float eps;
  10211. memcpy(&eps, dst->op_params, sizeof(float));
  10212. norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  10213. (void) src1;
  10214. (void) dst;
  10215. (void) src1_dd;
  10216. }
  10217. inline void ggml_sycl_op_group_norm(const ggml_tensor *src0,
  10218. const ggml_tensor *src1, ggml_tensor *dst,
  10219. const float *src0_dd, const float *src1_dd,
  10220. float *dst_dd,
  10221. const dpct::queue_ptr &main_stream) {
  10222. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10223. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10224. int num_groups = dst->op_params[0];
  10225. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  10226. group_norm_f32_sycl(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  10227. (void) src1;
  10228. (void) dst;
  10229. (void) src1_dd;
  10230. }
  10231. inline void ggml_sycl_op_concat(const ggml_tensor *src0,
  10232. const ggml_tensor *src1, ggml_tensor *dst,
  10233. const float *src0_dd, const float *src1_dd,
  10234. float *dst_dd,
  10235. const dpct::queue_ptr &main_stream) {
  10236. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10237. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10238. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10239. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  10240. 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);
  10241. }
  10242. (void) src1;
  10243. (void) dst;
  10244. }
  10245. inline void ggml_sycl_op_upscale(const ggml_tensor *src0,
  10246. const ggml_tensor *src1, ggml_tensor *dst,
  10247. const float *src0_dd, const float *src1_dd,
  10248. float *dst_dd,
  10249. const dpct::queue_ptr &main_stream) {
  10250. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10251. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10252. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  10253. const int scale_factor = dst->op_params[0];
  10254. upscale_f32_sycl(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  10255. (void) src1;
  10256. (void) dst;
  10257. (void) src1_dd;
  10258. }
  10259. inline void ggml_sycl_op_pad(const ggml_tensor *src0, const ggml_tensor *src1,
  10260. ggml_tensor *dst, const float *src0_dd,
  10261. const float *src1_dd, float *dst_dd,
  10262. const dpct::queue_ptr &main_stream) {
  10263. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10264. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10265. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  10266. pad_f32_sycl(src0_dd, dst_dd,
  10267. src0->ne[0], src0->ne[1], src0->ne[2],
  10268. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  10269. (void) src1;
  10270. (void) dst;
  10271. (void) src1_dd;
  10272. }
  10273. inline void ggml_sycl_op_rms_norm(const ggml_tensor *src0,
  10274. const ggml_tensor *src1, ggml_tensor *dst,
  10275. const float *src0_dd, const float *src1_dd,
  10276. float *dst_dd,
  10277. const dpct::queue_ptr &main_stream) {
  10278. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10279. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10280. const int64_t ne00 = src0->ne[0];
  10281. const int64_t nrows = ggml_nrows(src0);
  10282. float eps;
  10283. memcpy(&eps, dst->op_params, sizeof(float));
  10284. rms_norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  10285. (void) src1;
  10286. (void) dst;
  10287. (void) src1_dd;
  10288. }
  10289. inline void ggml_sycl_op_mul_mat_q(
  10290. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  10291. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  10292. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  10293. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  10294. const dpct::queue_ptr &stream) try {
  10295. const int64_t ne00 = src0->ne[0];
  10296. const int64_t ne10 = src1->ne[0];
  10297. GGML_ASSERT(ne10 % QK8_1 == 0);
  10298. const int64_t ne0 = dst->ne[0];
  10299. const int64_t row_diff = row_high - row_low;
  10300. int device_id;
  10301. SYCL_CHECK(
  10302. CHECK_TRY_ERROR(device_id = dpct::dev_mgr::instance().current_device_id()));
  10303. // the main device has a larger memory buffer to hold the results from all GPUs
  10304. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  10305. const int64_t nrows_dst = dst->backend == GGML_BACKEND_GPU && device_id == g_main_device ? ne0 : row_diff;
  10306. switch (src0->type) {
  10307. case GGML_TYPE_Q4_0:
  10308. 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);
  10309. break;
  10310. case GGML_TYPE_Q4_1:
  10311. 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);
  10312. break;
  10313. case GGML_TYPE_Q5_0:
  10314. 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);
  10315. break;
  10316. case GGML_TYPE_Q5_1:
  10317. 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);
  10318. break;
  10319. case GGML_TYPE_Q8_0:
  10320. 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);
  10321. break;
  10322. case GGML_TYPE_Q2_K:
  10323. 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);
  10324. break;
  10325. case GGML_TYPE_Q3_K:
  10326. 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);
  10327. break;
  10328. case GGML_TYPE_Q4_K:
  10329. 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);
  10330. break;
  10331. case GGML_TYPE_Q5_K:
  10332. 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);
  10333. break;
  10334. case GGML_TYPE_Q6_K:
  10335. 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);
  10336. break;
  10337. default:
  10338. GGML_ASSERT(false);
  10339. break;
  10340. }
  10341. (void) src1;
  10342. (void) dst;
  10343. (void) src1_ddf_i;
  10344. }
  10345. catch (sycl::exception const &exc) {
  10346. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10347. << ", line:" << __LINE__ << std::endl;
  10348. std::exit(1);
  10349. }
  10350. static int64_t get_row_rounding(ggml_type type) {
  10351. int64_t min_compute_capability = INT_MAX;
  10352. int64_t max_compute_capability = INT_MIN;
  10353. for (int64_t id = 0; id < g_device_count; ++id) {
  10354. if (g_tensor_split[id] < (id + 1 < g_device_count ? g_tensor_split[id + 1] : 1.0f)) {
  10355. if (min_compute_capability > g_device_caps[id].cc) {
  10356. min_compute_capability = g_device_caps[id].cc;
  10357. }
  10358. if (max_compute_capability < g_device_caps[id].cc) {
  10359. max_compute_capability = g_device_caps[id].cc;
  10360. }
  10361. }
  10362. }
  10363. switch(type) {
  10364. case GGML_TYPE_Q4_0:
  10365. case GGML_TYPE_Q4_1:
  10366. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  10367. case GGML_TYPE_Q5_0:
  10368. case GGML_TYPE_Q5_1:
  10369. case GGML_TYPE_Q8_0:
  10370. return 64;
  10371. case GGML_TYPE_F16:
  10372. case GGML_TYPE_F32:
  10373. return 1;
  10374. case GGML_TYPE_Q2_K:
  10375. case GGML_TYPE_Q3_K:
  10376. case GGML_TYPE_Q4_K:
  10377. case GGML_TYPE_Q5_K:
  10378. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  10379. case GGML_TYPE_Q6_K:
  10380. return 64;
  10381. default:
  10382. GGML_ASSERT(false);
  10383. }
  10384. }
  10385. inline void ggml_sycl_op_mul_mat_vec_q(
  10386. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  10387. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  10388. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  10389. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  10390. const dpct::queue_ptr &stream) {
  10391. GGML_ASSERT(ggml_nrows(src1) == 1);
  10392. const int64_t ne00 = src0->ne[0];
  10393. const int64_t row_diff = row_high - row_low;
  10394. switch (src0->type) {
  10395. case GGML_TYPE_Q4_0:
  10396. mul_mat_vec_q4_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10397. break;
  10398. case GGML_TYPE_Q4_1:
  10399. mul_mat_vec_q4_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10400. break;
  10401. case GGML_TYPE_Q5_0:
  10402. mul_mat_vec_q5_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10403. break;
  10404. case GGML_TYPE_Q5_1:
  10405. mul_mat_vec_q5_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10406. break;
  10407. case GGML_TYPE_Q8_0:
  10408. mul_mat_vec_q8_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10409. break;
  10410. case GGML_TYPE_Q2_K:
  10411. mul_mat_vec_q2_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10412. break;
  10413. case GGML_TYPE_Q3_K:
  10414. mul_mat_vec_q3_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10415. break;
  10416. case GGML_TYPE_Q4_K:
  10417. mul_mat_vec_q4_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10418. break;
  10419. case GGML_TYPE_Q5_K:
  10420. mul_mat_vec_q5_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10421. break;
  10422. case GGML_TYPE_Q6_K:
  10423. mul_mat_vec_q6_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10424. break;
  10425. default:
  10426. GGML_ASSERT(false);
  10427. break;
  10428. }
  10429. (void) src1;
  10430. (void) dst;
  10431. (void) src1_ddf_i;
  10432. (void) src1_ncols;
  10433. (void) src1_padded_row_size;
  10434. }
  10435. inline void ggml_sycl_op_dequantize_mul_mat_vec(
  10436. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  10437. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  10438. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  10439. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  10440. const dpct::queue_ptr &stream) {
  10441. const int64_t ne00 = src0->ne[0];
  10442. const int64_t row_diff = row_high - row_low;
  10443. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  10444. #ifdef GGML_SYCL_F16
  10445. sycl_pool_alloc<sycl::half> src1_dfloat_a;
  10446. sycl::half *src1_dfloat = nullptr; // dfloat == half
  10447. bool src1_convert_f16 =
  10448. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  10449. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  10450. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  10451. if (src1_convert_f16) {
  10452. if (src1->type == GGML_TYPE_F16) {
  10453. src1_dfloat = (sycl::half *)src1->data + src1_padded_row_size;
  10454. } else {
  10455. src1_dfloat = src1_dfloat_a.alloc(ne00);
  10456. ggml_cpy_f32_f16_sycl((const char *)src1_ddf_i, (char *)src1_dfloat,
  10457. ne00, ne00, 1, sizeof(float), 0, 0, ne00, 1,
  10458. sizeof(sycl::half), 0, 0, stream);
  10459. }
  10460. }
  10461. #else
  10462. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  10463. #endif // GGML_SYCL_F16
  10464. switch (src0->type) {
  10465. case GGML_TYPE_Q4_0:
  10466. dequantize_mul_mat_vec_q4_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10467. break;
  10468. case GGML_TYPE_Q4_1:
  10469. dequantize_mul_mat_vec_q4_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10470. break;
  10471. case GGML_TYPE_Q5_0:
  10472. dequantize_mul_mat_vec_q5_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10473. break;
  10474. case GGML_TYPE_Q5_1:
  10475. dequantize_mul_mat_vec_q5_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10476. break;
  10477. case GGML_TYPE_Q8_0:
  10478. dequantize_mul_mat_vec_q8_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10479. break;
  10480. case GGML_TYPE_Q2_K:
  10481. dequantize_mul_mat_vec_q2_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10482. break;
  10483. case GGML_TYPE_Q3_K:
  10484. dequantize_mul_mat_vec_q3_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10485. break;
  10486. case GGML_TYPE_Q4_K:
  10487. dequantize_mul_mat_vec_q4_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10488. break;
  10489. case GGML_TYPE_Q5_K:
  10490. dequantize_mul_mat_vec_q5_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10491. break;
  10492. case GGML_TYPE_Q6_K:
  10493. dequantize_mul_mat_vec_q6_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10494. break;
  10495. case GGML_TYPE_F16:
  10496. convert_mul_mat_vec_f16_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10497. break;
  10498. default:
  10499. GGML_ASSERT(false);
  10500. break;
  10501. }
  10502. (void) src1;
  10503. (void) dst;
  10504. (void) src1_ddq_i;
  10505. (void) src1_ncols;
  10506. (void) src1_padded_row_size;
  10507. }
  10508. inline void ggml_sycl_op_mul_mat_sycl(
  10509. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  10510. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  10511. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  10512. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  10513. const dpct::queue_ptr &stream) try {
  10514. GGML_ASSERT(src0_dd_i != nullptr);
  10515. GGML_ASSERT(src1_ddf_i != nullptr);
  10516. GGML_ASSERT(dst_dd_i != nullptr);
  10517. const int64_t ne00 = src0->ne[0];
  10518. const int64_t ne10 = src1->ne[0];
  10519. const int64_t ne0 = dst->ne[0];
  10520. const int64_t row_diff = row_high - row_low;
  10521. int id;
  10522. int device_id = dpct::dev_mgr::instance().current_device_id();
  10523. SYCL_CHECK(
  10524. CHECK_TRY_ERROR(id = get_current_device_index()));
  10525. // the main device has a larger memory buffer to hold the results from all GPUs
  10526. // ldc == nrows of the matrix that cuBLAS writes into
  10527. int ldc = dst->backend == GGML_BACKEND_GPU && device_id == g_main_device ? ne0 : row_diff;
  10528. const int compute_capability = g_device_caps[id].cc;
  10529. #ifdef GGML_SYCL_F16
  10530. bool use_fp16 = true; // TODO(Yu) SYCL capability check
  10531. #else
  10532. bool use_fp16 = false;
  10533. #endif
  10534. // if (compute_capability >= VER_GEN9 && (src0->type == GGML_TYPE_F16 ||
  10535. // ggml_is_quantized(src0->type)) && ggml_is_contiguous(src0) && row_diff ==
  10536. // src0->ne[1] && dst->op_params[0] == GGML_PREC_DEFAULT) {
  10537. if ((src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  10538. use_fp16 && ggml_is_contiguous(src0) && row_diff == src0->ne[1] &&
  10539. dst->op_params[0] == GGML_PREC_DEFAULT) {
  10540. // convert src0 and src1 to fp16, multiply as fp16, convert dst to fp32
  10541. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp16 path\n");
  10542. sycl_pool_alloc<sycl::half> src0_as_f16;
  10543. if (src0->type != GGML_TYPE_F16) {
  10544. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src0->type);
  10545. GGML_ASSERT(to_fp16_sycl != nullptr);
  10546. size_t ne = row_diff*ne00;
  10547. src0_as_f16.alloc(ne);
  10548. to_fp16_sycl(src0_dd_i, src0_as_f16.get(), ne, stream);
  10549. }
  10550. const sycl::half *src0_ptr = src0->type == GGML_TYPE_F16
  10551. ? (const sycl::half *)src0_dd_i
  10552. : src0_as_f16.get();
  10553. sycl_pool_alloc<sycl::half> src1_as_f16;
  10554. if (src1->type != GGML_TYPE_F16) {
  10555. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  10556. GGML_ASSERT(to_fp16_sycl != nullptr);
  10557. size_t ne = src1_ncols*ne10;
  10558. src1_as_f16.alloc(ne);
  10559. to_fp16_sycl(src1_ddf_i, src1_as_f16.get(), ne, stream);
  10560. }
  10561. const sycl::half *src1_ptr = src1->type == GGML_TYPE_F16
  10562. ? (const sycl::half *)src1->data + src1_padded_row_size
  10563. : src1_as_f16.get();
  10564. sycl_pool_alloc<sycl::half> dst_f16(row_diff * src1_ncols);
  10565. const sycl::half alpha_f16 = 1.0f;
  10566. const sycl::half beta_f16 = 0.0f;
  10567. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  10568. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
  10569. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  10570. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  10571. &alpha_f16, src0_ptr, dpct::library_data_t::real_half, ne00,
  10572. src1_ptr, dpct::library_data_t::real_half, ne10, &beta_f16,
  10573. dst_f16.get(), dpct::library_data_t::real_half, ldc,
  10574. dpct::library_data_t::real_half)));
  10575. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  10576. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  10577. }
  10578. else {
  10579. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
  10580. sycl_pool_alloc<float> src0_ddq_as_f32;
  10581. if (src0->type != GGML_TYPE_F32) {
  10582. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src0->type);
  10583. GGML_ASSERT(to_fp32_sycl != nullptr);
  10584. src0_ddq_as_f32.alloc(row_diff*ne00);
  10585. to_fp32_sycl(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  10586. }
  10587. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  10588. const float alpha = 1.0f;
  10589. const float beta = 0.0f;
  10590. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  10591. SYCL_CHECK(CHECK_TRY_ERROR(oneapi::mkl::blas::column_major::gemm(
  10592. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  10593. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  10594. dpct::get_value(&alpha, *g_sycl_handles[id]), src0_ddf_i, ne00,
  10595. src1_ddf_i, ne10, dpct::get_value(&beta, *g_sycl_handles[id]),
  10596. dst_dd_i, ldc)));
  10597. }
  10598. (void) dst;
  10599. (void) src1_ddq_i;
  10600. (void) src1_padded_row_size;
  10601. }
  10602. catch (sycl::exception const &exc) {
  10603. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10604. << ", line:" << __LINE__ << std::endl;
  10605. std::exit(1);
  10606. }
  10607. inline void ggml_sycl_op_rope(const ggml_tensor *src0, const ggml_tensor *src1,
  10608. ggml_tensor *dst, const float *src0_dd,
  10609. const float *src1_dd, float *dst_dd,
  10610. const dpct::queue_ptr &main_stream) {
  10611. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  10612. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  10613. GGML_ASSERT(src0->type == dst->type);
  10614. const int64_t ne00 = src0->ne[0];
  10615. const int64_t ne01 = src0->ne[1];
  10616. const int64_t ne2 = dst->ne[2];
  10617. const int64_t nrows = ggml_nrows(src0);
  10618. //const int n_past = ((int32_t *) dst->op_params)[0];
  10619. const int n_dims = ((int32_t *) dst->op_params)[1];
  10620. const int mode = ((int32_t *) dst->op_params)[2];
  10621. const int n_ctx = ((int32_t *) dst->op_params)[3];
  10622. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  10623. // RoPE alteration for extended context
  10624. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  10625. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  10626. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  10627. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  10628. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  10629. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  10630. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  10631. const int32_t * pos = nullptr;
  10632. if ((mode & 1) == 0) {
  10633. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  10634. GGML_ASSERT(src1->ne[0] == ne2);
  10635. pos = (const int32_t *) src1_dd;
  10636. }
  10637. const bool is_neox = mode & 2;
  10638. const bool is_glm = mode & 4;
  10639. rope_corr_dims corr_dims;
  10640. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  10641. // compute
  10642. if (is_glm) {
  10643. GGML_ASSERT(false);
  10644. rope_glm_f32_sycl(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  10645. } else if (is_neox) {
  10646. if (src0->type == GGML_TYPE_F32) {
  10647. rope_neox_sycl(
  10648. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  10649. attn_factor, corr_dims, main_stream
  10650. );
  10651. } else if (src0->type == GGML_TYPE_F16) {
  10652. rope_neox_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd,
  10653. ne00, n_dims, nrows, pos, freq_scale, ne01,
  10654. freq_base, ext_factor, attn_factor, corr_dims,
  10655. main_stream);
  10656. } else {
  10657. GGML_ASSERT(false);
  10658. }
  10659. } else {
  10660. if (src0->type == GGML_TYPE_F32) {
  10661. rope_sycl(
  10662. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  10663. attn_factor, corr_dims, main_stream
  10664. );
  10665. } else if (src0->type == GGML_TYPE_F16) {
  10666. rope_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd, ne00,
  10667. nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  10668. attn_factor, corr_dims, main_stream);
  10669. } else {
  10670. GGML_ASSERT(false);
  10671. }
  10672. }
  10673. (void) src1;
  10674. (void) dst;
  10675. (void) src1_dd;
  10676. }
  10677. inline void ggml_sycl_op_alibi(const ggml_tensor *src0, const ggml_tensor *src1,
  10678. ggml_tensor *dst, const float *src0_dd,
  10679. const float *src1_dd, float *dst_dd,
  10680. const dpct::queue_ptr &main_stream) {
  10681. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10682. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10683. const int64_t ne00 = src0->ne[0];
  10684. const int64_t ne01 = src0->ne[1];
  10685. const int64_t ne02 = src0->ne[2];
  10686. const int64_t nrows = ggml_nrows(src0);
  10687. //const int n_past = ((int32_t *) dst->op_params)[0];
  10688. const int n_head = ((int32_t *) dst->op_params)[1];
  10689. float max_bias;
  10690. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  10691. //GGML_ASSERT(ne01 + n_past == ne00);
  10692. GGML_ASSERT(n_head == ne02);
  10693. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  10694. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  10695. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  10696. alibi_f32_sycl(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  10697. (void) src1;
  10698. (void) src1_dd;
  10699. }
  10700. inline void ggml_sycl_op_im2col(const ggml_tensor *src0,
  10701. const ggml_tensor *src1, ggml_tensor *dst,
  10702. const float *src0_dd, const float *src1_dd,
  10703. float *dst_dd,
  10704. const dpct::queue_ptr &main_stream) {
  10705. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  10706. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10707. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  10708. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  10709. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  10710. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  10711. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  10712. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  10713. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  10714. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  10715. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  10716. const int64_t IH = is_2D ? src1->ne[1] : 1;
  10717. const int64_t IW = src1->ne[0];
  10718. const int64_t KH = is_2D ? src0->ne[1] : 1;
  10719. const int64_t KW = src0->ne[0];
  10720. const int64_t OH = is_2D ? dst->ne[2] : 1;
  10721. const int64_t OW = dst->ne[1];
  10722. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  10723. im2col_f32_f16_sycl(src1_dd, (sycl::half *)dst_dd, IW, IH, OW, OH, KW, KH,
  10724. IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  10725. (void) src0;
  10726. (void) src0_dd;
  10727. }
  10728. inline void ggml_sycl_op_sum_rows(const ggml_tensor *src0,
  10729. const ggml_tensor *src1, ggml_tensor *dst,
  10730. const float *src0_dd, const float *src1_dd,
  10731. float *dst_dd,
  10732. const dpct::queue_ptr &main_stream) {
  10733. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10734. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10735. const int64_t ncols = src0->ne[0];
  10736. const int64_t nrows = ggml_nrows(src0);
  10737. sum_rows_f32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
  10738. (void) src1;
  10739. (void) dst;
  10740. (void) src1_dd;
  10741. }
  10742. inline void ggml_sycl_op_argsort(const ggml_tensor *src0,
  10743. const ggml_tensor *src1, ggml_tensor *dst,
  10744. const float *src0_dd, const float *src1_dd,
  10745. float *dst_dd,
  10746. const dpct::queue_ptr &main_stream) {
  10747. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10748. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  10749. const int64_t ncols = src0->ne[0];
  10750. const int64_t nrows = ggml_nrows(src0);
  10751. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  10752. argsort_f32_i32_sycl(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  10753. (void) src1;
  10754. (void) dst;
  10755. (void) src1_dd;
  10756. }
  10757. inline void ggml_sycl_op_diag_mask_inf(const ggml_tensor *src0,
  10758. const ggml_tensor *src1,
  10759. ggml_tensor *dst, const float *src0_dd,
  10760. const float *src1_dd, float *dst_dd,
  10761. const dpct::queue_ptr &main_stream) {
  10762. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10763. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10764. const int64_t ne00 = src0->ne[0];
  10765. const int64_t ne01 = src0->ne[1];
  10766. const int nrows0 = ggml_nrows(src0);
  10767. const int n_past = ((int32_t *) dst->op_params)[0];
  10768. diag_mask_inf_f32_sycl(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  10769. (void) src1;
  10770. (void) dst;
  10771. (void) src1_dd;
  10772. }
  10773. inline void ggml_sycl_op_soft_max(const ggml_tensor *src0,
  10774. const ggml_tensor *src1, ggml_tensor *dst,
  10775. const float *src0_dd, const float *src1_dd,
  10776. float *dst_dd,
  10777. const dpct::queue_ptr &main_stream) {
  10778. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10779. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10780. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  10781. const int64_t ne00 = src0->ne[0];
  10782. const int64_t nrows_x = ggml_nrows(src0);
  10783. const int64_t nrows_y = src1 ? ggml_nrows(src1) : 1;
  10784. float scale = 1.0f;
  10785. memcpy(&scale, dst->op_params, sizeof(float));
  10786. soft_max_f32_sycl(src0_dd, src1 ? src1_dd : nullptr, dst_dd, ne00, nrows_x, nrows_y, scale, main_stream);
  10787. (void) dst;
  10788. }
  10789. inline void ggml_sycl_op_scale(const ggml_tensor *src0, const ggml_tensor *src1,
  10790. ggml_tensor *dst, const float *src0_dd,
  10791. const float *src1_dd, float *dst_dd,
  10792. const dpct::queue_ptr &main_stream) {
  10793. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10794. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10795. float scale;
  10796. memcpy(&scale, dst->op_params, sizeof(float));
  10797. scale_f32_sycl(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  10798. /*
  10799. DPCT1010:87: SYCL uses exceptions to report errors and does not use the
  10800. error codes. The call was replaced with 0. You need to rewrite this code.
  10801. */
  10802. SYCL_CHECK(0);
  10803. (void) src1;
  10804. (void) dst;
  10805. (void) src1_dd;
  10806. }
  10807. inline void ggml_sycl_op_clamp(const ggml_tensor *src0, const ggml_tensor *src1,
  10808. ggml_tensor *dst, const float *src0_dd,
  10809. const float *src1_dd, float *dst_dd,
  10810. const dpct::queue_ptr &main_stream) {
  10811. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10812. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10813. float min;
  10814. float max;
  10815. memcpy(&min, dst->op_params, sizeof(float));
  10816. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  10817. clamp_f32_sycl(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  10818. /*
  10819. DPCT1010:88: SYCL uses exceptions to report errors and does not use the
  10820. error codes. The call was replaced with 0. You need to rewrite this code.
  10821. */
  10822. SYCL_CHECK(0);
  10823. (void) src1;
  10824. (void) dst;
  10825. (void) src1_dd;
  10826. }
  10827. static void ggml_sycl_op_flatten(const ggml_tensor *src0,
  10828. const ggml_tensor *src1, ggml_tensor *dst,
  10829. const ggml_sycl_op_flatten_t op) try {
  10830. const int64_t nrows0 = ggml_nrows(src0);
  10831. const bool use_src1 = src1 != nullptr;
  10832. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  10833. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_GPU_SPLIT);
  10834. GGML_ASSERT( dst->backend != GGML_BACKEND_GPU_SPLIT);
  10835. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  10836. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  10837. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  10838. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  10839. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_GPU;
  10840. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU;
  10841. // dd = data device
  10842. float * src0_ddf = nullptr;
  10843. float * src1_ddf = nullptr;
  10844. float * dst_ddf = nullptr;
  10845. sycl_pool_alloc<float> src0_f;
  10846. sycl_pool_alloc<float> src1_f;
  10847. sycl_pool_alloc<float> dst_f;
  10848. ggml_sycl_set_device(g_main_device);
  10849. dpct::queue_ptr main_stream = g_syclStreams[g_main_device_index][0];
  10850. // GGML_SYCL_DEBUG("g_main_device_index=%d, main_stream=%p src0_on_device=%d, src1_on_device=%d, dst_on_device=%d\n",
  10851. // g_main_device_index, main_stream, src0_on_device, src1_on_device, dst_on_device);
  10852. if (src0_on_device) {
  10853. src0_ddf = (float *) src0_extra->data_device[g_main_device_index];
  10854. } else {
  10855. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  10856. // GGML_SYCL_DEBUG("before ggml_sycl_cpy_tensor_2d src0_ddf=%p, src0=%p\n", src0_ddf, src0);
  10857. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  10858. }
  10859. if (use_src1) {
  10860. if (src1_on_device) {
  10861. src1_ddf = (float *) src1_extra->data_device[g_main_device_index];
  10862. } else {
  10863. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  10864. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  10865. }
  10866. }
  10867. if (dst_on_device) {
  10868. dst_ddf = (float *) dst_extra->data_device[g_main_device_index];
  10869. // printf("zjy dst_ddf=%p main_stream=%p g_main_device_index=%d\n", dst_ddf, main_stream, g_main_device_index);
  10870. } else {
  10871. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  10872. }
  10873. // GGML_SYCL_DEBUG("op src0=%p, src1=%p, dst=%p, src0_ddf=%p, src1_ddf=%p, dst_ddf=%p, main_stream=%p\n",
  10874. // src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  10875. // do the computation
  10876. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  10877. /*
  10878. DPCT1010:89: SYCL uses exceptions to report errors and does not use the
  10879. error codes. The call was replaced with 0. You need to rewrite this code.
  10880. */
  10881. SYCL_CHECK(0);
  10882. // copy dst to host if necessary
  10883. if (!dst_on_device) {
  10884. SYCL_CHECK(CHECK_TRY_ERROR(
  10885. main_stream->memcpy(dst->data, dst_ddf, ggml_nbytes(dst))));
  10886. }
  10887. if (dst->backend == GGML_BACKEND_CPU) {
  10888. SYCL_CHECK(CHECK_TRY_ERROR(
  10889. dpct::get_current_device().queues_wait_and_throw()));
  10890. }
  10891. // print_ggml_tensor("tensor", dst);
  10892. }
  10893. catch (sycl::exception const &exc) {
  10894. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10895. << ", line:" << __LINE__ << std::endl;
  10896. std::exit(1);
  10897. }
  10898. static void ggml_sycl_set_peer_access(const int n_tokens) {
  10899. static bool peer_access_enabled = false;
  10900. const bool enable_peer_access = n_tokens <= GGML_SYCL_PEER_MAX_BATCH_SIZE;
  10901. if (peer_access_enabled == enable_peer_access) {
  10902. return;
  10903. }
  10904. #ifdef NDEBUG
  10905. for (int id = 0; id < g_device_count; ++id) {
  10906. SYCL_CHECK(ggml_sycl_set_device(get_device_id_by_index(id)));
  10907. // SYCL_CHECK(syclDeviceSynchronize());
  10908. }
  10909. for (int id = 0; id < g_device_count; ++id) {
  10910. SYCL_CHECK(ggml_sycl_set_device(get_device_id_by_index(id)));
  10911. int device_id = g_device_caps[id].device_id;
  10912. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  10913. int device_id_other = g_device_caps[id_other].device_id;
  10914. if (device_id == id_other) {
  10915. continue;
  10916. }
  10917. if (device_id != g_main_device && device_id_other != g_main_device) {
  10918. continue;
  10919. }
  10920. int can_access_peer;
  10921. // SYCL_CHECK(syclDeviceCanAccessPeer(&can_access_peer, id, id_other));
  10922. // if (can_access_peer) {
  10923. // if (enable_peer_access) {
  10924. // SYCL_CHECK(syclDeviceEnablePeerAccess(id_other, 0));
  10925. // } else {
  10926. // SYCL_CHECK(syclDeviceDisablePeerAccess(id_other));
  10927. // }
  10928. // }
  10929. }
  10930. }
  10931. #endif // NDEBUG
  10932. peer_access_enabled = enable_peer_access;
  10933. }
  10934. static void ggml_sycl_op_mul_mat(const ggml_tensor *src0,
  10935. const ggml_tensor *src1, ggml_tensor *dst,
  10936. ggml_sycl_op_mul_mat_t op,
  10937. const bool convert_src1_to_q8_1) try {
  10938. const int64_t ne00 = src0->ne[0];
  10939. const int64_t ne01 = src0->ne[1];
  10940. const int64_t ne02 = src0->ne[2];
  10941. const int64_t ne03 = src0->ne[3];
  10942. const int64_t nrows0 = ggml_nrows(src0);
  10943. const int64_t ne10 = src1->ne[0];
  10944. const int64_t ne11 = src1->ne[1];
  10945. const int64_t ne12 = src1->ne[2];
  10946. const int64_t ne13 = src1->ne[3];
  10947. const int64_t nrows1 = ggml_nrows(src1);
  10948. GGML_ASSERT(ne03 == ne13);
  10949. const int64_t ne0 = dst->ne[0];
  10950. const int64_t ne1 = dst->ne[1];
  10951. const int nb2 = dst->nb[2];
  10952. const int nb3 = dst->nb[3];
  10953. GGML_ASSERT(dst->backend != GGML_BACKEND_GPU_SPLIT);
  10954. GGML_ASSERT(src1->backend != GGML_BACKEND_GPU_SPLIT);
  10955. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  10956. const int64_t i02_divisor = ne12 / ne02;
  10957. const size_t src0_ts = ggml_type_size(src0->type);
  10958. const size_t src0_bs = ggml_blck_size(src0->type);
  10959. const size_t q8_1_ts = sizeof(block_q8_1);
  10960. const size_t q8_1_bs = QK8_1;
  10961. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  10962. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  10963. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  10964. const bool src0_on_device = src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT;
  10965. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  10966. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  10967. int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  10968. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  10969. GGML_ASSERT(!(split && ne02 > 1));
  10970. GGML_ASSERT(!(split && ne03 > 1));
  10971. GGML_ASSERT(!(split && ne02 < ne12));
  10972. // dd = data device
  10973. char * src0_dd[GGML_SYCL_MAX_DEVICES] = {nullptr};
  10974. float * src1_ddf[GGML_SYCL_MAX_DEVICES] = {nullptr}; // float
  10975. char * src1_ddq[GGML_SYCL_MAX_DEVICES] = {nullptr}; // q8_1
  10976. float * dst_dd[GGML_SYCL_MAX_DEVICES] = {nullptr};
  10977. // as = actual size
  10978. size_t src0_as[GGML_SYCL_MAX_DEVICES] = {0};
  10979. size_t src1_asf[GGML_SYCL_MAX_DEVICES] = {0};
  10980. size_t src1_asq[GGML_SYCL_MAX_DEVICES] = {0};
  10981. size_t dst_as[GGML_SYCL_MAX_DEVICES] = {0};
  10982. int64_t row_low[GGML_SYCL_MAX_DEVICES];
  10983. int64_t row_high[GGML_SYCL_MAX_DEVICES];
  10984. int used_devices = 0;
  10985. for (int64_t id = 0; id < g_device_count; ++id) {
  10986. // by default, use all rows
  10987. row_low[id] = 0;
  10988. row_high[id] = ne01;
  10989. // for multi GPU, get the row boundaries from tensor split
  10990. // and round to mul_mat_q tile sizes
  10991. if (split) {
  10992. const int64_t rounding = get_row_rounding(src0->type);
  10993. if (id != 0) {
  10994. row_low[id] = ne01*g_tensor_split[id];
  10995. if (row_low[id] < ne01) {
  10996. row_low[id] -= row_low[id] % rounding;
  10997. }
  10998. }
  10999. if (id != g_device_count - 1) {
  11000. row_high[id] = ne01*g_tensor_split[id + 1];
  11001. if (row_high[id] < ne01) {
  11002. row_high[id] -= row_high[id] % rounding;
  11003. }
  11004. }
  11005. }
  11006. }
  11007. for (int64_t id = 0; id < g_device_count; ++id) {
  11008. if ((!split && id != g_main_device_index) || row_low[id] == row_high[id]) {
  11009. continue;
  11010. }
  11011. used_devices++;
  11012. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device_index;
  11013. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device_index;
  11014. ggml_sycl_set_device(get_device_id_by_index(id));
  11015. const dpct::queue_ptr stream = g_syclStreams[id][0];
  11016. if (src0_on_device && src0_is_contiguous) {
  11017. src0_dd[id] = (char *) src0_extra->data_device[id];
  11018. } else {
  11019. // const size_t size_src0_ddq = split ? (row_high[id]-row_low[id])*ne00 * src0_ts/src0_bs : ggml_nbytes(src0);
  11020. src0_dd[id] = (char *) ggml_sycl_pool_malloc(ggml_nbytes(src0), &src0_as[id]);
  11021. }
  11022. if (src1_on_device && src1_is_contiguous) {
  11023. src1_ddf[id] = (float *) src1_extra->data_device[id];
  11024. } else {
  11025. src1_ddf[id] = (float *) ggml_sycl_pool_malloc(ggml_nbytes(src1), &src1_asf[id]);
  11026. }
  11027. if (convert_src1_to_q8_1) {
  11028. src1_ddq[id] = (char *) ggml_sycl_pool_malloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs, &src1_asq[id]);
  11029. if (src1_on_device && src1_is_contiguous) {
  11030. quantize_row_q8_1_sycl(src1_ddf[id], src1_ddq[id], ne10, nrows1, src1_padded_col_size, stream);
  11031. /*
  11032. DPCT1010:90: SYCL uses exceptions to report errors and does not
  11033. use the error codes. The call was replaced with 0. You need to
  11034. rewrite this code.
  11035. */
  11036. SYCL_CHECK(0);
  11037. }
  11038. }
  11039. if (dst_on_device) {
  11040. dst_dd[id] = (float *) dst_extra->data_device[id];
  11041. } else {
  11042. const size_t size_dst_ddf = split ? (row_high[id]-row_low[id])*ne1*sizeof(float) : ggml_nbytes(dst);
  11043. dst_dd[id] = (float *) ggml_sycl_pool_malloc(size_dst_ddf, &dst_as[id]);
  11044. }
  11045. }
  11046. // if multiple devices are used they need to wait for the main device
  11047. // here an event is recorded that signals that the main device has finished calculating the input data
  11048. if (split && used_devices > 1) {
  11049. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11050. /*
  11051. DPCT1024:91: The original code returned the error code that was further
  11052. consumed by the program logic. This original code was replaced with 0.
  11053. You may need to rewrite the program logic consuming the error code.
  11054. */
  11055. SYCL_CHECK(CHECK_TRY_ERROR(
  11056. *src0_extra->events[g_main_device_index][0] =
  11057. g_syclStreams[g_main_device_index][0]->ext_oneapi_submit_barrier()));
  11058. }
  11059. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  11060. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  11061. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  11062. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  11063. for (int64_t id = 0; id < g_device_count; ++id) {
  11064. if ((!split && id != g_main_device_index) || row_low[id] == row_high[id]) {
  11065. continue;
  11066. }
  11067. const bool src1_on_device = src1->backend == GGML_BACKEND_GPU && id == g_main_device_index;
  11068. const bool dst_on_device = dst->backend == GGML_BACKEND_GPU && id == g_main_device_index;
  11069. const int64_t row_diff = row_high[id] - row_low[id];
  11070. ggml_sycl_set_device(get_device_id_by_index(id));
  11071. const dpct::queue_ptr stream = g_syclStreams[id][is];
  11072. // wait for main GPU data if necessary
  11073. if (split && (id != g_main_device_index || is != 0)) {
  11074. SYCL_CHECK(CHECK_TRY_ERROR(stream->ext_oneapi_submit_barrier(
  11075. {*src0_extra->events[g_main_device_index][0]})));
  11076. }
  11077. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  11078. const int64_t i03 = i0 / ne12;
  11079. const int64_t i02 = i0 % ne12;
  11080. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  11081. // for split tensors the data begins at i0 == i0_offset_low
  11082. char * src0_dd_i = src0_dd[id] + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  11083. float * src1_ddf_i = src1_ddf[id] + (i0*ne11 + src1_col_0) * ne10;
  11084. char * src1_ddq_i = src1_ddq[id] + src1_ddq_i_offset;
  11085. float * dst_dd_i = dst_dd[id] + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  11086. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  11087. // in that case an offset on dst_ddf_i is needed
  11088. if (dst->backend == GGML_BACKEND_GPU && id == g_main_device_index) {
  11089. dst_dd_i += row_low[id]; // offset is 0 if no tensor split
  11090. }
  11091. // copy src0, src1 to device if necessary
  11092. if (src1->backend == GGML_BACKEND_GPU && src1_is_contiguous) {
  11093. if (id != g_main_device_index) {
  11094. if (convert_src1_to_q8_1) {
  11095. char * src1_ddq_i_source = src1_ddq[g_main_device_index] + src1_ddq_i_offset;
  11096. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  11097. src1_ddq_i, src1_ddq_i_source,
  11098. src1_ncols * src1_padded_col_size * q8_1_ts /
  11099. q8_1_bs)));
  11100. } else {
  11101. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device_index];
  11102. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  11103. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  11104. src1_ddf_i, src1_ddf_i_source,
  11105. src1_ncols * ne10 * sizeof(float))));
  11106. }
  11107. }
  11108. } else if (src1->backend == GGML_BACKEND_CPU || (src1_on_device && !src1_is_contiguous)) {
  11109. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(
  11110. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  11111. } else {
  11112. GGML_ASSERT(false);
  11113. }
  11114. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_CPU || !src1_is_contiguous)) {
  11115. quantize_row_q8_1_sycl(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  11116. /*
  11117. DPCT1010:92: SYCL uses exceptions to report errors and does
  11118. not use the error codes. The call was replaced with 0. You
  11119. need to rewrite this code.
  11120. */
  11121. SYCL_CHECK(0);
  11122. }
  11123. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  11124. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, row_low[id], row_high[id], stream));
  11125. }
  11126. if (src1->type == GGML_TYPE_F16) {
  11127. src1_padded_col_size = (i0 * ne11 + src1_col_0) * ne10;
  11128. }
  11129. // do the computation
  11130. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  11131. row_low[id], row_high[id], src1_ncols, src1_padded_col_size, stream);
  11132. /*
  11133. DPCT1010:93: SYCL uses exceptions to report errors and does not
  11134. use the error codes. The call was replaced with 0. You need to
  11135. rewrite this code.
  11136. */
  11137. SYCL_CHECK(0);
  11138. // copy dst to host or other device if necessary
  11139. if (!dst_on_device) {
  11140. void * dst_off_device;
  11141. dpct::memcpy_direction kind;
  11142. if (dst->backend == GGML_BACKEND_CPU) {
  11143. dst_off_device = dst->data;
  11144. kind = dpct::device_to_host;
  11145. } else if (dst->backend == GGML_BACKEND_GPU) {
  11146. dst_off_device = dst_extra->data_device[g_main_device_index];
  11147. kind = dpct::device_to_device;
  11148. } else {
  11149. GGML_ASSERT(false);
  11150. }
  11151. if (split) {
  11152. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  11153. // dst is NOT transposed.
  11154. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  11155. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  11156. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  11157. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  11158. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  11159. dhf_dst_i += src1_col_0*ne0 + row_low[id];
  11160. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  11161. dhf_dst_i, ne0 * sizeof(float), dst_dd_i,
  11162. row_diff * sizeof(float), row_diff * sizeof(float),
  11163. src1_ncols, kind, *stream)));
  11164. } else {
  11165. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  11166. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  11167. dhf_dst_i += src1_col_0*ne0;
  11168. SYCL_CHECK(CHECK_TRY_ERROR(
  11169. stream->memcpy(dhf_dst_i, dst_dd_i,
  11170. src1_ncols * ne0 * sizeof(float))));
  11171. }
  11172. }
  11173. // add event for the main device to wait on until other device is done
  11174. if (split && (id != g_main_device_index || is != 0)) {
  11175. /*
  11176. DPCT1024:94: The original code returned the error code that
  11177. was further consumed by the program logic. This original
  11178. code was replaced with 0. You may need to rewrite the
  11179. program logic consuming the error code.
  11180. */
  11181. SYCL_CHECK(CHECK_TRY_ERROR(
  11182. *src0_extra->events[id][is] =
  11183. stream->ext_oneapi_submit_barrier()));
  11184. }
  11185. }
  11186. }
  11187. }
  11188. for (int64_t id = 0; id < g_device_count; ++id) {
  11189. if ((!split && id != g_main_device_index) || row_low[id] == row_high[id]) {
  11190. continue;
  11191. }
  11192. SYCL_CHECK(ggml_sycl_set_device(get_device_id_by_index(id)));
  11193. // free buffers again when done
  11194. if (dst_as[id] > 0) {
  11195. ggml_sycl_pool_free(dst_dd[id], dst_as[id]);
  11196. }
  11197. if (src1_asq[id] > 0) {
  11198. ggml_sycl_pool_free(src1_ddq[id], src1_asq[id]);
  11199. }
  11200. if (src1_asf[id] > 0) {
  11201. ggml_sycl_pool_free(src1_ddf[id], src1_asf[id]);
  11202. }
  11203. if (src0_as[id] > 0) {
  11204. ggml_sycl_pool_free(src0_dd[id], src0_as[id]);
  11205. }
  11206. }
  11207. // main device waits for all other devices to be finished
  11208. if (split && g_device_count > 1) {
  11209. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  11210. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  11211. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11212. for (int64_t id = 0; id < g_device_count; ++id) {
  11213. if (row_low[id] == row_high[id]) {
  11214. continue;
  11215. }
  11216. for (int64_t is = 0; is < is_max; ++is) {
  11217. SYCL_CHECK(CHECK_TRY_ERROR(
  11218. g_syclStreams[g_main_device_index][0]->ext_oneapi_submit_barrier(
  11219. {*src0_extra->events[id][is]})));
  11220. }
  11221. }
  11222. }
  11223. if (dst->backend == GGML_BACKEND_CPU) {
  11224. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11225. SYCL_CHECK(CHECK_TRY_ERROR(
  11226. dpct::get_current_device().queues_wait_and_throw()));
  11227. }
  11228. }
  11229. catch (sycl::exception const &exc) {
  11230. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11231. << ", line:" << __LINE__ << std::endl;
  11232. std::exit(1);
  11233. }
  11234. static void ggml_sycl_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11235. GGML_SYCL_DEBUG("call %s\n", __func__);
  11236. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_repeat);
  11237. }
  11238. static void ggml_sycl_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11239. GGML_SYCL_DEBUG("call %s\n", __func__);
  11240. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_get_rows);
  11241. }
  11242. static void ggml_sycl_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11243. GGML_SYCL_DEBUG("call %s\n", __func__);
  11244. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_add);
  11245. // log_tensor_with_cnt("log_ggml_sycl_add_src0", (struct ggml_tensor *) src0, 6);
  11246. // log_tensor_with_cnt("log_ggml_sycl_add_src1", (struct ggml_tensor *)src1, 6);
  11247. // log_tensor_with_cnt("log_ggml_sycl_add_dst", dst, 6);
  11248. }
  11249. static void ggml_sycl_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11250. GGML_SYCL_DEBUG("call %s\n", __func__);
  11251. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_acc);
  11252. }
  11253. static void ggml_sycl_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11254. GGML_SYCL_DEBUG("call %s\n", __func__);
  11255. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_mul);
  11256. // log_tensor_with_cnt("log_ggml_sycl_mul_src0", (struct ggml_tensor *)src0, 6);
  11257. // log_tensor_with_cnt("log_ggml_sycl_mul_src1", (struct ggml_tensor *)src1, 6);
  11258. // log_tensor_with_cnt("log_ggml_sycl_mul_dst", dst, 6);
  11259. }
  11260. static void ggml_sycl_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11261. GGML_SYCL_DEBUG("call %s\n", __func__);
  11262. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_div);
  11263. }
  11264. static void ggml_sycl_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11265. GGML_SYCL_DEBUG("call %s\n", __func__);
  11266. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu);
  11267. }
  11268. static void ggml_sycl_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11269. GGML_SYCL_DEBUG("call %s\n", __func__);
  11270. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_silu);
  11271. }
  11272. static void ggml_sycl_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11273. GGML_SYCL_DEBUG("call %s\n", __func__);
  11274. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu_quick);
  11275. }
  11276. static void ggml_sycl_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11277. GGML_SYCL_DEBUG("call %s\n", __func__);
  11278. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_tanh);
  11279. }
  11280. static void ggml_sycl_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11281. GGML_SYCL_DEBUG("call %s\n", __func__);
  11282. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_relu);
  11283. }
  11284. static void ggml_sycl_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11285. GGML_SYCL_DEBUG("call %s\n", __func__);
  11286. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_leaky_relu);
  11287. }
  11288. static void ggml_sycl_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11289. GGML_SYCL_DEBUG("call %s\n", __func__);
  11290. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sqr);
  11291. }
  11292. static void ggml_sycl_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11293. GGML_SYCL_DEBUG("call %s\n", __func__);
  11294. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_norm);
  11295. }
  11296. static void ggml_sycl_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11297. GGML_SYCL_DEBUG("call %s\n", __func__);
  11298. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_group_norm);
  11299. }
  11300. static void ggml_sycl_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11301. GGML_SYCL_DEBUG("call %s\n", __func__);
  11302. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_concat);
  11303. }
  11304. static void ggml_sycl_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11305. GGML_SYCL_DEBUG("call %s\n", __func__);
  11306. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_upscale);
  11307. }
  11308. static void ggml_sycl_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11309. GGML_SYCL_DEBUG("call %s\n", __func__);
  11310. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pad);
  11311. }
  11312. static void ggml_sycl_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11313. GGML_SYCL_DEBUG("call %s\n", __func__);
  11314. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rms_norm);
  11315. // log_tensor_with_cnt("log_ggml_sycl_rms_norm_src0", (struct ggml_tensor *)src0, 6);
  11316. // log_tensor_with_cnt("log_ggml_sycl_rms_norm_src1", (struct ggml_tensor *)src1, 6);
  11317. // log_tensor_with_cnt("log_ggml_sycl_rms_norm_dst", dst, 6);
  11318. }
  11319. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  11320. if (!g_sycl_loaded) return false;
  11321. const int64_t ne10 = src1->ne[0];
  11322. const int64_t ne0 = dst->ne[0];
  11323. const int64_t ne1 = dst->ne[1];
  11324. // TODO: find the optimal values for these
  11325. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  11326. src1->type == GGML_TYPE_F32 &&
  11327. dst->type == GGML_TYPE_F32 &&
  11328. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  11329. }
  11330. static void ggml_sycl_mul_mat_vec_p021(const ggml_tensor *src0,
  11331. const ggml_tensor *src1,
  11332. ggml_tensor *dst) try {
  11333. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  11334. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  11335. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  11336. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  11337. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  11338. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11339. const int64_t ne00 = src0->ne[0];
  11340. const int64_t ne01 = src0->ne[1];
  11341. const int64_t ne02 = src0->ne[2];
  11342. const int64_t ne12 = src1->ne[2];
  11343. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11344. dpct::queue_ptr main_stream = g_syclStreams[g_main_device_index][0];
  11345. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11346. void * src0_ddq = src0_extra->data_device[g_main_device_index];
  11347. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11348. float * src1_ddf = (float *) src1_extra->data_device[g_main_device_index];
  11349. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11350. float * dst_ddf = (float *) dst_extra->data_device[g_main_device_index];
  11351. ggml_mul_mat_p021_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  11352. }
  11353. catch (sycl::exception const &exc) {
  11354. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11355. << ", line:" << __LINE__ << std::endl;
  11356. std::exit(1);
  11357. }
  11358. static void ggml_sycl_mul_mat_vec_nc(const ggml_tensor *src0,
  11359. const ggml_tensor *src1,
  11360. ggml_tensor *dst) try {
  11361. GGML_ASSERT(!ggml_is_transposed(src0));
  11362. GGML_ASSERT(!ggml_is_transposed(src1));
  11363. GGML_ASSERT(!ggml_is_permuted(src0));
  11364. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  11365. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  11366. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11367. const int64_t ne00 = src0->ne[0];
  11368. const int64_t ne01 = src0->ne[1];
  11369. const int64_t ne02 = src0->ne[2];
  11370. const int64_t nb01 = src0->nb[1];
  11371. const int64_t nb02 = src0->nb[2];
  11372. const int64_t ne12 = src1->ne[2];
  11373. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11374. dpct::queue_ptr main_stream = g_syclStreams[g_main_device_index][0];
  11375. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11376. void * src0_ddq = src0_extra->data_device[g_main_device_index];
  11377. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11378. float * src1_ddf = (float *) src1_extra->data_device[g_main_device_index];
  11379. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11380. float * dst_ddf = (float *) dst_extra->data_device[g_main_device_index];
  11381. const int64_t row_stride_x = nb01 / sizeof(sycl::half);
  11382. const int64_t channel_stride_x = nb02 / sizeof(sycl::half);
  11383. 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);
  11384. }
  11385. catch (sycl::exception const &exc) {
  11386. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11387. << ", line:" << __LINE__ << std::endl;
  11388. std::exit(1);
  11389. }
  11390. static void k_compute_batched_ptrs(const sycl::half *src0_as_f16,
  11391. const sycl::half *src1_as_f16, char *dst,
  11392. const void **ptrs_src, void **ptrs_dst,
  11393. int64_t ne12, int64_t ne13, int64_t ne23,
  11394. size_t nb02, size_t nb03, size_t nb12,
  11395. size_t nb13, size_t nbd2, size_t nbd3,
  11396. int64_t r2, int64_t r3,
  11397. const sycl::nd_item<3> &item_ct1) {
  11398. int64_t i13 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  11399. item_ct1.get_local_id(2);
  11400. int64_t i12 = item_ct1.get_group(1) * item_ct1.get_local_range(1) +
  11401. item_ct1.get_local_id(1);
  11402. if (i13 >= ne13 || i12 >= ne12) {
  11403. return;
  11404. }
  11405. int64_t i03 = i13 / r3;
  11406. int64_t i02 = i12 / r2;
  11407. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  11408. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12/2 + i13*nb13/2;
  11409. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  11410. }
  11411. static void ggml_sycl_mul_mat_mat_batched_sycl(const ggml_tensor *src0,
  11412. const ggml_tensor *src1,
  11413. ggml_tensor *dst) try {
  11414. GGML_ASSERT(!ggml_is_transposed(src0));
  11415. GGML_ASSERT(!ggml_is_transposed(src1));
  11416. GGML_ASSERT(src0->backend != GGML_BACKEND_GPU_SPLIT);
  11417. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  11418. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11419. const int64_t ne00 = src0->ne[0]; GGML_UNUSED(ne00);
  11420. const int64_t ne01 = src0->ne[1];
  11421. const int64_t ne02 = src0->ne[2];
  11422. const int64_t ne03 = src0->ne[3];
  11423. const int64_t nb01 = src0->nb[1];
  11424. const int64_t nb02 = src0->nb[2]; GGML_UNUSED(nb02);
  11425. const int64_t nb03 = src0->nb[3]; GGML_UNUSED(nb03);
  11426. const int64_t ne10 = src1->ne[0];
  11427. const int64_t ne11 = src1->ne[1];
  11428. const int64_t ne12 = src1->ne[2];
  11429. const int64_t ne13 = src1->ne[3];
  11430. const int64_t nb11 = src1->nb[1];
  11431. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  11432. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  11433. const int64_t ne1 = ggml_nelements(src1);
  11434. const int64_t ne = ggml_nelements(dst);
  11435. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11436. dpct::queue_ptr main_stream = g_syclStreams[g_main_device_index][0];
  11437. SYCL_CHECK(
  11438. CHECK_TRY_ERROR(g_sycl_handles[g_main_device_index] = main_stream));
  11439. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11440. void * src0_ddq = src0_extra->data_device[g_main_device_index];
  11441. sycl::half *src0_as_f16 = (sycl::half *)src0_ddq;
  11442. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11443. float * src1_ddf = (float *) src1_extra->data_device[g_main_device_index];
  11444. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11445. float * dst_ddf = (float *) dst_extra->data_device[g_main_device_index];
  11446. // convert src1 to fp16
  11447. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  11448. GGML_ASSERT(to_fp16_sycl != nullptr);
  11449. sycl_pool_alloc<sycl::half> src1_as_f16(ne1);
  11450. to_fp16_sycl(src1_ddf, src1_as_f16.get(), ne1, main_stream);
  11451. sycl_pool_alloc<sycl::half> dst_f16;
  11452. char * dst_t;
  11453. dpct::library_data_t cu_compute_type = dpct::library_data_t::real_half;
  11454. dpct::library_data_t cu_data_type = dpct::library_data_t::real_half;
  11455. // dst strides
  11456. size_t nbd2 = dst->nb[2];
  11457. size_t nbd3 = dst->nb[3];
  11458. const sycl::half alpha_f16 = 1.0f;
  11459. const sycl::half beta_f16 = 0.0f;
  11460. const float alpha_f32 = 1.0f;
  11461. const float beta_f32 = 0.0f;
  11462. const void * alpha = &alpha_f16;
  11463. const void * beta = &beta_f16;
  11464. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  11465. dst_t = (char *) dst_f16.alloc(ne);
  11466. nbd2 /= sizeof(float) / sizeof(sycl::half);
  11467. nbd3 /= sizeof(float) / sizeof(sycl::half);
  11468. } else {
  11469. dst_t = (char *) dst_ddf;
  11470. cu_compute_type = dpct::library_data_t::real_float;
  11471. cu_data_type = dpct::library_data_t::real_float;
  11472. alpha = &alpha_f32;
  11473. beta = &beta_f32;
  11474. }
  11475. GGML_ASSERT(ne12 % ne02 == 0);
  11476. GGML_ASSERT(ne13 % ne03 == 0);
  11477. // broadcast factors
  11478. const int64_t r2 = ne12/ne02;
  11479. const int64_t r3 = ne13/ne03;
  11480. #if 0
  11481. // use syclGemmEx
  11482. {
  11483. for (int i13 = 0; i13 < ne13; ++i13) {
  11484. for (int i12 = 0; i12 < ne12; ++i12) {
  11485. int i03 = i13 / r3;
  11486. int i02 = i12 / r2;
  11487. SYCL_CHECK(
  11488. syclGemmEx(g_sycl_handles[g_main_device_index], CUBLAS_OP_T, CUBLAS_OP_N,
  11489. ne01, ne11, ne10,
  11490. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , SYCL_R_16F, nb01/sizeof(half),
  11491. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, SYCL_R_16F, nb11/sizeof(float),
  11492. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  11493. cu_compute_type,
  11494. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  11495. }
  11496. }
  11497. }
  11498. #else
  11499. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  11500. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  11501. // use syclGemmStridedBatchedEx
  11502. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  11503. *g_sycl_handles[g_main_device_index], oneapi::mkl::transpose::trans,
  11504. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  11505. (const char *)src0_as_f16, dpct::library_data_t::real_half,
  11506. nb01 / sizeof(sycl::half), src0->nb[2] / sizeof(sycl::half),
  11507. (const char *)src1_as_f16.get(), dpct::library_data_t::real_half,
  11508. nb11 / sizeof(float), src1->nb[2] / sizeof(float), beta,
  11509. (char *)dst_t, cu_data_type, ne01, dst->nb[2] / sizeof(float),
  11510. ne12 * ne13, cu_compute_type)));
  11511. } else {
  11512. // use syclGemmBatchedEx
  11513. const int ne23 = ne12*ne13;
  11514. sycl_pool_alloc<const void *> ptrs_src(2*ne23);
  11515. sycl_pool_alloc< void *> ptrs_dst(1*ne23);
  11516. sycl::range<3> block_dims(1, ne12, ne13);
  11517. /*
  11518. DPCT1049:47: The work-group size passed to the SYCL kernel may exceed
  11519. the limit. To get the device limit, query
  11520. info::device::max_work_group_size. Adjust the work-group size if needed.
  11521. */
  11522. {
  11523. dpct::has_capability_or_fail(main_stream->get_device(),
  11524. {sycl::aspect::fp16});
  11525. main_stream->submit([&](sycl::handler &cgh) {
  11526. const sycl::half *src1_as_f16_get_ct1 = src1_as_f16.get();
  11527. const void **ptrs_src_get_ct3 = ptrs_src.get();
  11528. void **ptrs_dst_get_ct4 = ptrs_dst.get();
  11529. cgh.parallel_for(sycl::nd_range<3>(block_dims, block_dims),
  11530. [=](sycl::nd_item<3> item_ct1) {
  11531. k_compute_batched_ptrs(
  11532. src0_as_f16, src1_as_f16_get_ct1,
  11533. dst_t, ptrs_src_get_ct3,
  11534. ptrs_dst_get_ct4, ne12, ne13, ne23,
  11535. nb02, nb03, nb12, nb13, nbd2, nbd3, r2,
  11536. r3, item_ct1);
  11537. });
  11538. });
  11539. }
  11540. /*
  11541. DPCT1010:95: SYCL uses exceptions to report errors and does not use the
  11542. error codes. The call was replaced with 0. You need to rewrite this
  11543. code.
  11544. */
  11545. SYCL_CHECK(0);
  11546. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  11547. *g_sycl_handles[g_main_device_index], oneapi::mkl::transpose::trans,
  11548. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  11549. (const void **)(ptrs_src.get() + 0 * ne23),
  11550. dpct::library_data_t::real_half, nb01 / sizeof(sycl::half),
  11551. (const void **)(ptrs_src.get() + 1 * ne23),
  11552. dpct::library_data_t::real_half, nb11 / sizeof(float), beta,
  11553. (void **)(ptrs_dst.get() + 0 * ne23), cu_data_type, ne01, ne23,
  11554. cu_compute_type)));
  11555. }
  11556. #endif
  11557. if (dst->op_params[0] == GGML_PREC_DEFAULT) {
  11558. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  11559. to_fp32_sycl(dst_f16.get(), dst_ddf, ne, main_stream);
  11560. }
  11561. }
  11562. catch (sycl::exception const &exc) {
  11563. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11564. << ", line:" << __LINE__ << std::endl;
  11565. std::exit(1);
  11566. }
  11567. static void ggml_sycl_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11568. const bool all_on_device =
  11569. (src0->backend == GGML_BACKEND_GPU || src0->backend == GGML_BACKEND_GPU_SPLIT) &&
  11570. (src1->backend == GGML_BACKEND_GPU) &&
  11571. ( dst->backend == GGML_BACKEND_GPU);
  11572. const bool split = src0->backend == GGML_BACKEND_GPU_SPLIT;
  11573. int64_t min_compute_capability = INT_MAX;
  11574. for (int64_t id = 0; id < g_device_count; ++id) {
  11575. 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)) {
  11576. min_compute_capability = g_device_caps[id].cc;
  11577. }
  11578. }
  11579. #ifdef SYCL_USE_XMX
  11580. const bool use_xmx = true;
  11581. #else
  11582. const bool use_xmx = false;
  11583. #endif
  11584. // debug helpers
  11585. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  11586. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  11587. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  11588. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  11589. //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);
  11590. //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);
  11591. if (!split && all_on_device && !use_xmx && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  11592. // KQ single-batch
  11593. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_p021\n");
  11594. ggml_sycl_mul_mat_vec_p021(src0, src1, dst);
  11595. } 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) {
  11596. // KQV single-batch
  11597. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_nc\n");
  11598. ggml_sycl_mul_mat_vec_nc(src0, src1, dst);
  11599. } 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)) {
  11600. // KQ + KQV multi-batch
  11601. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_mat_batched_sycl\n");
  11602. ggml_sycl_mul_mat_mat_batched_sycl(src0, src1, dst);
  11603. } else if (src0->type == GGML_TYPE_F32) {
  11604. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat\n");
  11605. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  11606. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  11607. // GGML_SYCL_DEBUG("ggml_is_quantized or GGML_TYPE_F16\n");
  11608. if (src1->ne[1] == 1 && src0->ne[0] % GGML_SYCL_DMMV_X == 0) {
  11609. #ifdef GGML_SYCL_FORCE_DMMV
  11610. const bool use_mul_mat_vec_q = false;
  11611. #else
  11612. const bool use_mul_mat_vec_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  11613. #endif // GGML_SYCL_FORCE_DMMV
  11614. if (use_mul_mat_vec_q) {
  11615. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  11616. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_vec_q path\n");
  11617. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_vec_q, true);
  11618. } else {
  11619. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_dequantize_mul_mat_vec path\n");
  11620. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_dequantize_mul_mat_vec, false);
  11621. }
  11622. } else {
  11623. bool use_mul_mat_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type);
  11624. if (use_xmx && min_compute_capability >= VER_GEN9 && src1->ne[1] > XMX_MAX_BATCH_SIZE) {
  11625. use_mul_mat_q = false;
  11626. }
  11627. if (use_mul_mat_q) {
  11628. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_q path\n");
  11629. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_q, true);
  11630. } else {
  11631. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_sycl path\n");
  11632. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  11633. }
  11634. }
  11635. } else {
  11636. GGML_ASSERT(false);
  11637. }
  11638. }
  11639. #if 0
  11640. template<typename ... Srcs>
  11641. static __global__ void k_compute_batched_ptrs_id(
  11642. const void ** ptrs_src, void ** ptrs_dst,
  11643. int ne12, int ne13,
  11644. int ne23,
  11645. int nb02, int nb03,
  11646. int nb12, int nb13,
  11647. int nb2, int nb3,
  11648. int r2, int r3,
  11649. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  11650. const half * src1_f16, half * dst_f16,
  11651. const int32_t * ids, const int id,
  11652. Srcs... src0s) {
  11653. int i = ids[id];
  11654. half * src0_f16;
  11655. const void * srcs_ar[] = { (const half *) src0s... };
  11656. if (src0_type == GGML_TYPE_F16) {
  11657. src0_f16 = (half *) srcs_ar[i];
  11658. } else {
  11659. src0_f16 = src0_as_f16;
  11660. if (threadIdx.x == 0 && threadIdx.y == 0) {
  11661. const to_fp16_sycl_t to_fp16 = ggml_get_to_fp16_sycl(src0_type);
  11662. to_fp16(srcs_ar[i], src0_f16, src0_ne, syclStreamFireAndForget);
  11663. }
  11664. }
  11665. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  11666. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  11667. if (i13 >= ne13 || i12 >= ne12) {
  11668. return;
  11669. }
  11670. int i03 = i13 / r3;
  11671. int i02 = i12 / r2;
  11672. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  11673. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  11674. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  11675. }
  11676. static void ggml_sycl_mul_mat_id_sycl(ggml_tensor * dst) {
  11677. const struct ggml_tensor * ids = dst->src[0];
  11678. const struct ggml_tensor * src1 = dst->src[1];
  11679. const struct ggml_tensor * src00 = dst->src[2];
  11680. const int id = dst->op_params[0];
  11681. GGML_ASSERT(!ggml_is_transposed(src00));
  11682. GGML_ASSERT(!ggml_is_transposed(src1));
  11683. GGML_ASSERT(src00->backend != GGML_BACKEND_GPU_SPLIT);
  11684. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11685. const int64_t ne00 = src00->ne[0]; GGML_UNUSED(ne00);
  11686. const int64_t ne01 = src00->ne[1];
  11687. const int64_t ne02 = src00->ne[2];
  11688. const int64_t ne03 = src00->ne[3];
  11689. //const int64_t nb01 = src00->nb[1];
  11690. const int64_t nb02 = src00->nb[2]; GGML_UNUSED(nb02);
  11691. const int64_t nb03 = src00->nb[3]; GGML_UNUSED(nb03);
  11692. const int64_t ne10 = src1->ne[0];
  11693. const int64_t ne11 = src1->ne[1];
  11694. const int64_t ne12 = src1->ne[2];
  11695. const int64_t ne13 = src1->ne[3];
  11696. //const int64_t nb11 = src1->nb[1];
  11697. const int64_t nb12 = src1->nb[2]; GGML_UNUSED(nb12);
  11698. const int64_t nb13 = src1->nb[3]; GGML_UNUSED(nb13);
  11699. const int64_t ne1 = ggml_nelements(src1);
  11700. const int64_t ne = ggml_nelements(dst);
  11701. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11702. syclStream_t main_stream = g_syclStreams[g_main_device_index][0];
  11703. SYCL_CHECK(syclSetStream(g_sycl_handles[g_main_device_index], main_stream));
  11704. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11705. //void * src0_ddq = src0_extra->data_device[g_main_device_index];
  11706. //half * src0_as_f16 = (half *) src0_ddq;
  11707. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11708. float * src1_ddf = (float *) src1_extra->data_device[g_main_device_index];
  11709. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11710. float * dst_ddf = (float *) dst_extra->data_device[g_main_device_index];
  11711. // convert src1 to fp16
  11712. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  11713. GGML_ASSERT(to_fp16_sycl != nullptr);
  11714. size_t src1_as = 0;
  11715. half * src1_as_f16 = (half *) ggml_sycl_pool_malloc(ne1 * sizeof(half), &src1_as);
  11716. to_fp16_sycl(src1_ddf, src1_as_f16, ne1, main_stream);
  11717. size_t dst_as = 0;
  11718. half * dst_f16 = (half *) ggml_sycl_pool_malloc(ne * sizeof(half), &dst_as);
  11719. GGML_ASSERT(ne12 % ne02 == 0);
  11720. GGML_ASSERT(ne13 % ne03 == 0);
  11721. // broadcast factors
  11722. const int64_t r2 = ne12/ne02;
  11723. const int64_t r3 = ne13/ne03;
  11724. const half alpha_f16 = 1.0f;
  11725. const half beta_f16 = 0.0f;
  11726. // use syclGemmBatchedEx
  11727. const int ne23 = ne12*ne13;
  11728. const void ** ptrs_src = nullptr;
  11729. void ** ptrs_dst = nullptr;
  11730. size_t ptrs_src_s = 0;
  11731. size_t ptrs_dst_s = 0;
  11732. ptrs_src = (const void **) ggml_sycl_pool_malloc(2*ne23*sizeof(void *), &ptrs_src_s);
  11733. ptrs_dst = ( void **) ggml_sycl_pool_malloc(1*ne23*sizeof(void *), &ptrs_dst_s);
  11734. int64_t src0_ne = ggml_nelements(src00);
  11735. half * src0_as_f16 = nullptr;
  11736. size_t src0_as = 0;
  11737. if (src00->type != GGML_TYPE_F16) {
  11738. src0_as_f16 = (half *) ggml_sycl_pool_malloc(src0_ne * sizeof(half), &src0_as);
  11739. }
  11740. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  11741. dim3 block_dims(ne13, ne12);
  11742. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  11743. ptrs_src, ptrs_dst,
  11744. ne12, ne13,
  11745. ne23,
  11746. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  11747. nb12, nb13,
  11748. dst->nb[2], dst->nb[3],
  11749. r2, r3,
  11750. src00->type, src0_as_f16, src0_ne,
  11751. src1_as_f16, dst_f16,
  11752. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device_index], id,
  11753. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device_index] : nullptr,
  11754. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device_index] : nullptr,
  11755. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device_index] : nullptr,
  11756. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device_index] : nullptr
  11757. );
  11758. SYCL_CHECK(syclGetLastError());
  11759. SYCL_CHECK(
  11760. syclGemmBatchedEx(g_sycl_handles[g_main_device_index], CUBLAS_OP_T, CUBLAS_OP_N,
  11761. ne01, ne11, ne10,
  11762. &alpha_f16, (const void **) (ptrs_src + 0*ne23), SYCL_R_16F, ne00,
  11763. (const void **) (ptrs_src + 1*ne23), SYCL_R_16F, ne10,
  11764. &beta_f16, ( void **) (ptrs_dst + 0*ne23), SYCL_R_16F, ne01,
  11765. ne23,
  11766. CUBLAS_COMPUTE_16F,
  11767. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  11768. if (src0_as != 0) {
  11769. ggml_sycl_pool_free(src0_as_f16, src0_as);
  11770. }
  11771. if (ptrs_src_s != 0) {
  11772. ggml_sycl_pool_free(ptrs_src, ptrs_src_s);
  11773. }
  11774. if (ptrs_dst_s != 0) {
  11775. ggml_sycl_pool_free(ptrs_dst, ptrs_dst_s);
  11776. }
  11777. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  11778. to_fp32_sycl(dst_f16, dst_ddf, ne, main_stream);
  11779. ggml_sycl_pool_free(src1_as_f16, src1_as);
  11780. ggml_sycl_pool_free(dst_f16, dst_as);
  11781. }
  11782. #endif
  11783. static void ggml_sycl_mul_mat_id(const ggml_tensor *src0,
  11784. const ggml_tensor *src1,
  11785. ggml_tensor *dst) try {
  11786. #if 0
  11787. ggml_sycl_mul_mat_id_sycl(dst);
  11788. // TODO: mmq/mmv support
  11789. #endif
  11790. const int64_t nb11 = src1->nb[1];
  11791. const int64_t nb1 = dst->nb[1];
  11792. const struct ggml_tensor * ids = src0;
  11793. const int32_t id = ((int32_t *) dst->op_params)[0];
  11794. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  11795. std::vector<char> ids_host(ggml_nbytes(ids));
  11796. const dpct::queue_ptr stream = g_syclStreams[g_main_device_index][0];
  11797. if (ids->backend == GGML_BACKEND_GPU) {
  11798. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device_index];
  11799. SYCL_CHECK(CHECK_TRY_ERROR(
  11800. stream->memcpy(ids_host.data(), ids_dev, ggml_nbytes(ids))));
  11801. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  11802. } else {
  11803. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  11804. }
  11805. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  11806. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  11807. ggml_tensor_extra_gpu src1_row_extra;
  11808. ggml_tensor_extra_gpu dst_row_extra;
  11809. ggml_tensor src1_row = *src1;
  11810. ggml_tensor dst_row = *dst;
  11811. src1_row.backend = GGML_BACKEND_GPU;
  11812. dst_row.backend = GGML_BACKEND_GPU;
  11813. src1_row.extra = &src1_row_extra;
  11814. dst_row.extra = &dst_row_extra;
  11815. char * src1_original = src1->backend == GGML_BACKEND_CPU ?
  11816. (char *) src1->data : (char *) src1_extra->data_device[g_main_device_index];
  11817. char * dst_original = dst->backend == GGML_BACKEND_CPU ?
  11818. (char *) dst->data : (char *) dst_extra->data_device[g_main_device_index];
  11819. if (src1->ne[1] == 1) {
  11820. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  11821. GGML_ASSERT(dst->backend == GGML_BACKEND_GPU);
  11822. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  11823. //int32_t row_id;
  11824. //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]));
  11825. //SYCL_CHECK(syclStreamSynchronize(g_syclStreams[g_main_device][0]));
  11826. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  11827. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  11828. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  11829. src1_row_extra.data_device[g_main_device_index] = src1_original + i01*src1->nb[1];
  11830. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  11831. dst_row_extra.data_device[g_main_device_index] = dst_original + i01*dst->nb[1];
  11832. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  11833. ggml_sycl_mul_mat(src0_row, &src1_row, &dst_row);
  11834. }
  11835. } else {
  11836. sycl_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  11837. sycl_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  11838. src1_row_extra.data_device[g_main_device_index] = src1_contiguous.get();
  11839. dst_row_extra.data_device[g_main_device_index] = dst_contiguous.get();
  11840. const dpct::memcpy_direction src1_kind =
  11841. src1->backend == GGML_BACKEND_CPU ? dpct::host_to_device
  11842. : dpct::device_to_device;
  11843. const dpct::memcpy_direction dst_kind = dst->backend == GGML_BACKEND_CPU
  11844. ? dpct::device_to_host
  11845. : dpct::device_to_device;
  11846. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  11847. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  11848. int64_t num_src1_rows = 0;
  11849. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  11850. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  11851. if (row_id_i != row_id) {
  11852. continue;
  11853. }
  11854. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  11855. SYCL_CHECK(CHECK_TRY_ERROR(
  11856. stream->memcpy(src1_contiguous.get() + num_src1_rows * nb11,
  11857. src1_original + i01 * nb11, nb11)));
  11858. num_src1_rows++;
  11859. }
  11860. if (num_src1_rows == 0) {
  11861. continue;
  11862. }
  11863. src1_row.ne[1] = num_src1_rows;
  11864. dst_row.ne[1] = num_src1_rows;
  11865. src1_row.nb[1] = nb11;
  11866. src1_row.nb[2] = num_src1_rows*nb11;
  11867. src1_row.nb[3] = num_src1_rows*nb11;
  11868. dst_row.nb[1] = nb1;
  11869. dst_row.nb[2] = num_src1_rows*nb1;
  11870. dst_row.nb[3] = num_src1_rows*nb1;
  11871. ggml_sycl_mul_mat(src0_row, &src1_row, &dst_row);
  11872. num_src1_rows = 0;
  11873. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  11874. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  11875. if (row_id_i != row_id) {
  11876. continue;
  11877. }
  11878. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  11879. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  11880. dst_original + i01 * nb1,
  11881. dst_contiguous.get() + num_src1_rows * nb1, nb1)));
  11882. num_src1_rows++;
  11883. }
  11884. }
  11885. }
  11886. if (dst->backend == GGML_BACKEND_CPU) {
  11887. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  11888. }
  11889. }
  11890. catch (sycl::exception const &exc) {
  11891. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11892. << ", line:" << __LINE__ << std::endl;
  11893. std::exit(1);
  11894. }
  11895. static void ggml_sycl_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11896. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_scale);
  11897. }
  11898. static void ggml_sycl_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11899. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_clamp);
  11900. }
  11901. static void ggml_sycl_cpy(const ggml_tensor *src0, const ggml_tensor *src1,
  11902. ggml_tensor *dst) try {
  11903. const int64_t ne = ggml_nelements(src0);
  11904. GGML_ASSERT(ne == ggml_nelements(src1));
  11905. GGML_ASSERT(src0->backend == GGML_BACKEND_GPU);
  11906. GGML_ASSERT(src1->backend == GGML_BACKEND_GPU);
  11907. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  11908. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  11909. const int64_t ne00 = src0->ne[0];
  11910. const int64_t ne01 = src0->ne[1];
  11911. GGML_ASSERT(src0->ne[3] == 1);
  11912. const int64_t nb00 = src0->nb[0];
  11913. const int64_t nb01 = src0->nb[1];
  11914. const int64_t nb02 = src0->nb[2];
  11915. const int64_t ne10 = src1->ne[0];
  11916. const int64_t ne11 = src1->ne[1];
  11917. GGML_ASSERT(src1->ne[3] == 1);
  11918. const int64_t nb10 = src1->nb[0];
  11919. const int64_t nb11 = src1->nb[1];
  11920. const int64_t nb12 = src1->nb[2];
  11921. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11922. dpct::queue_ptr main_stream = g_syclStreams[g_main_device_index][0];
  11923. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11924. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11925. char * src0_ddc = (char *) src0_extra->data_device[g_main_device_index];
  11926. char * src1_ddc = (char *) src1_extra->data_device[g_main_device_index];
  11927. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  11928. ggml_cpy_f32_f32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11929. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  11930. ggml_cpy_f32_f16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11931. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  11932. ggml_cpy_f32_q8_0_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11933. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  11934. ggml_cpy_f32_q4_0_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11935. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  11936. ggml_cpy_f32_q4_1_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11937. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  11938. ggml_cpy_f16_f16_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_I16 && src1->type == GGML_TYPE_I16) {
  11940. ggml_cpy_i16_i16_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_I32 && src1->type == GGML_TYPE_I32) {
  11942. ggml_cpy_i32_i32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, nb00, nb01, nb02, ne10, ne11, nb10, nb11, nb12, main_stream);
  11943. } else {
  11944. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  11945. ggml_type_name(src0->type), ggml_type_name(src1->type));
  11946. GGML_ASSERT(false);
  11947. }
  11948. (void) dst;
  11949. }
  11950. catch (sycl::exception const &exc) {
  11951. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11952. << ", line:" << __LINE__ << std::endl;
  11953. std::exit(1);
  11954. }
  11955. static void ggml_sycl_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11956. // TODO: why do we pass dst as src1 here?
  11957. ggml_sycl_cpy(src0, dst, nullptr);
  11958. (void) src1;
  11959. }
  11960. static void ggml_sycl_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11961. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_diag_mask_inf);
  11962. }
  11963. static void ggml_sycl_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11964. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_soft_max);
  11965. }
  11966. static void ggml_sycl_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11967. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  11968. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rope);
  11969. }
  11970. static void ggml_sycl_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11971. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_alibi);
  11972. }
  11973. static void ggml_sycl_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11974. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_im2col);
  11975. }
  11976. static void ggml_sycl_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11977. GGML_ASSERT(ggml_is_contiguous(src0));
  11978. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sum_rows);
  11979. }
  11980. static void ggml_sycl_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11981. GGML_ASSERT(ggml_is_contiguous(src0));
  11982. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_argsort);
  11983. }
  11984. static void ggml_sycl_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11985. (void) src0;
  11986. (void) src1;
  11987. (void) dst;
  11988. }
  11989. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  11990. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  11991. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  11992. }
  11993. void ggml_sycl_transform_tensor(void *data, struct ggml_tensor *tensor) try {
  11994. const int64_t nrows = ggml_nrows(tensor);
  11995. const int64_t ne0 = tensor->ne[0];
  11996. const size_t nb1 = tensor->nb[1];
  11997. ggml_backend_type backend = tensor->backend;
  11998. ggml_tensor_extra_gpu * extra = new struct ggml_tensor_extra_gpu;
  11999. memset(extra, 0, sizeof(*extra));
  12000. for (int64_t id = 0; id < g_device_count; ++id) {
  12001. if (backend == GGML_BACKEND_GPU && id != g_main_device_index) {
  12002. continue;
  12003. }
  12004. ggml_sycl_set_device(get_device_id_by_index(id));
  12005. const dpct::queue_ptr stream = g_syclStreams[id][0];
  12006. int64_t row_low, row_high;
  12007. if (backend == GGML_BACKEND_GPU) {
  12008. row_low = 0;
  12009. row_high = nrows;
  12010. } else if (backend == GGML_BACKEND_GPU_SPLIT) {
  12011. const int64_t rounding = get_row_rounding(tensor->type);
  12012. row_low = id == 0 ? 0 : nrows*g_tensor_split[id];
  12013. row_low -= row_low % rounding;
  12014. if (id == g_device_count - 1) {
  12015. row_high = nrows;
  12016. } else {
  12017. row_high = nrows*g_tensor_split[id + 1];
  12018. row_high -= row_high % rounding;
  12019. }
  12020. } else {
  12021. GGML_ASSERT(false);
  12022. }
  12023. if (row_low == row_high) {
  12024. continue;
  12025. }
  12026. int64_t nrows_split = row_high - row_low;
  12027. const size_t offset_split = row_low*nb1;
  12028. size_t size = ggml_nbytes_split(tensor, nrows_split);
  12029. const size_t original_size = size;
  12030. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  12031. if (ne0 % MATRIX_ROW_PADDING != 0) {
  12032. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  12033. }
  12034. char * buf;
  12035. SYCL_CHECK(CHECK_TRY_ERROR(buf = (char *)sycl::malloc_device(
  12036. size, *stream)));
  12037. char * buf_host = (char *)data + offset_split;
  12038. // set padding to 0 to avoid possible NaN values
  12039. if (size > original_size) {
  12040. SYCL_CHECK(CHECK_TRY_ERROR(
  12041. (*stream)
  12042. .memset(buf + original_size, 0, size - original_size)
  12043. .wait()));
  12044. }
  12045. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  12046. .memcpy(buf, buf_host, original_size)
  12047. .wait()));
  12048. extra->data_device[id] = buf;
  12049. if (backend == GGML_BACKEND_GPU_SPLIT) {
  12050. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  12051. SYCL_CHECK(CHECK_TRY_ERROR(extra->events[id][is] =
  12052. new sycl::event()));
  12053. }
  12054. }
  12055. }
  12056. tensor->extra = extra;
  12057. }
  12058. catch (sycl::exception const &exc) {
  12059. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12060. << ", line:" << __LINE__ << std::endl;
  12061. std::exit(1);
  12062. }
  12063. void ggml_sycl_free_data(struct ggml_tensor *tensor) try {
  12064. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_GPU && tensor->backend != GGML_BACKEND_GPU_SPLIT) ) {
  12065. return;
  12066. }
  12067. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  12068. for (int64_t id = 0; id < g_device_count; ++id) {
  12069. const dpct::queue_ptr stream = g_syclStreams[id][0];
  12070. if (extra->data_device[id] != nullptr) {
  12071. SYCL_CHECK(ggml_sycl_set_device(get_device_id_by_index(id)));
  12072. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(extra->data_device[id], *stream)));
  12073. }
  12074. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  12075. if (extra->events[id][is] != nullptr) {
  12076. SYCL_CHECK(ggml_sycl_set_device(get_device_id_by_index(id)));
  12077. SYCL_CHECK(CHECK_TRY_ERROR(
  12078. dpct::destroy_event(extra->events[id][is])));
  12079. }
  12080. }
  12081. }
  12082. delete extra;
  12083. }
  12084. catch (sycl::exception const &exc) {
  12085. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12086. << ", line:" << __LINE__ << std::endl;
  12087. std::exit(1);
  12088. }
  12089. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  12090. static size_t g_temp_tensor_extra_index = 0;
  12091. static ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  12092. if (g_temp_tensor_extras == nullptr) {
  12093. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  12094. }
  12095. size_t alloc_index = g_temp_tensor_extra_index;
  12096. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  12097. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  12098. memset(extra, 0, sizeof(*extra));
  12099. return extra;
  12100. }
  12101. static void ggml_sycl_assign_buffers_impl(struct ggml_tensor *tensor,
  12102. bool scratch, bool force_inplace,
  12103. bool no_alloc) try {
  12104. if (scratch && g_scratch_size == 0) {
  12105. return;
  12106. }
  12107. tensor->backend = GGML_BACKEND_GPU;
  12108. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_CPU) {
  12109. const ggml_op src0_op = tensor->src[0]->op;
  12110. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  12111. ggml_sycl_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  12112. }
  12113. }
  12114. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_CPU) {
  12115. ggml_sycl_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  12116. }
  12117. if (scratch && no_alloc) {
  12118. return;
  12119. }
  12120. ggml_tensor_extra_gpu * extra;
  12121. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  12122. tensor->op == GGML_OP_VIEW ||
  12123. force_inplace;
  12124. const size_t size = ggml_nbytes(tensor);
  12125. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12126. const dpct::queue_ptr stream = g_syclStreams[g_main_device_index][0];
  12127. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT)) {
  12128. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  12129. char * src0_ddc = (char *) src0_extra->data_device[g_main_device_index];
  12130. size_t offset = 0;
  12131. if (tensor->op == GGML_OP_VIEW) {
  12132. memcpy(&offset, tensor->op_params, sizeof(size_t));
  12133. }
  12134. extra = ggml_sycl_alloc_temp_tensor_extra();
  12135. extra->data_device[g_main_device_index] = src0_ddc + offset;
  12136. } else if (tensor->op == GGML_OP_CPY) {
  12137. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  12138. void * src1_ddv = src1_extra->data_device[g_main_device_index];
  12139. extra = ggml_sycl_alloc_temp_tensor_extra();
  12140. extra->data_device[g_main_device_index] = src1_ddv;
  12141. } else if (scratch) {
  12142. GGML_ASSERT(size <= g_scratch_size);
  12143. if (g_scratch_offset + size > g_scratch_size) {
  12144. g_scratch_offset = 0;
  12145. }
  12146. char * data = (char *) g_scratch_buffer;
  12147. if (data == nullptr) {
  12148. SYCL_CHECK(CHECK_TRY_ERROR(
  12149. data = (char *)sycl::malloc_device(
  12150. g_scratch_size, *stream)));
  12151. g_scratch_buffer = data;
  12152. }
  12153. extra = ggml_sycl_alloc_temp_tensor_extra();
  12154. extra->data_device[g_main_device_index] = data + g_scratch_offset;
  12155. g_scratch_offset += size;
  12156. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  12157. } else { // allocate new buffers outside of scratch
  12158. void * data;
  12159. SYCL_CHECK(CHECK_TRY_ERROR(data = (void *)sycl::malloc_device(
  12160. size, *stream)));
  12161. SYCL_CHECK(CHECK_TRY_ERROR(
  12162. (*stream).memset(data, 0, size).wait()));
  12163. extra = new ggml_tensor_extra_gpu;
  12164. memset(extra, 0, sizeof(*extra));
  12165. extra->data_device[g_main_device_index] = data;
  12166. }
  12167. tensor->extra = extra;
  12168. }
  12169. catch (sycl::exception const &exc) {
  12170. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12171. << ", line:" << __LINE__ << std::endl;
  12172. std::exit(1);
  12173. }
  12174. void ggml_sycl_assign_scratch_offset(struct ggml_tensor *tensor,
  12175. size_t offset) try {
  12176. if (g_scratch_size == 0) {
  12177. return;
  12178. }
  12179. if (g_scratch_buffer == nullptr) {
  12180. ggml_sycl_set_device(g_main_device);
  12181. const dpct::queue_ptr stream = g_syclStreams[g_main_device_index][0];
  12182. SYCL_CHECK(
  12183. CHECK_TRY_ERROR(g_scratch_buffer = (void *)sycl::malloc_device(
  12184. g_scratch_size, *stream)));
  12185. }
  12186. ggml_tensor_extra_gpu * extra = ggml_sycl_alloc_temp_tensor_extra();
  12187. const bool inplace = tensor->view_src != nullptr;
  12188. if (inplace && (tensor->view_src->backend == GGML_BACKEND_GPU || tensor->view_src->backend == GGML_BACKEND_GPU_SPLIT)) {
  12189. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->view_src->extra;
  12190. char * src0_ddc = (char *) src0_extra->data_device[g_main_device_index];
  12191. size_t view_offset = 0;
  12192. if (tensor->op == GGML_OP_VIEW) {
  12193. memcpy(&view_offset, tensor->op_params, sizeof(size_t));
  12194. }
  12195. extra->data_device[g_main_device_index] = src0_ddc + view_offset;
  12196. } else {
  12197. extra->data_device[g_main_device_index] = (char *) g_scratch_buffer + offset;
  12198. }
  12199. tensor->extra = extra;
  12200. }
  12201. catch (sycl::exception const &exc) {
  12202. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12203. << ", line:" << __LINE__ << std::endl;
  12204. std::exit(1);
  12205. }
  12206. void ggml_sycl_copy_to_device(struct ggml_tensor *tensor) try {
  12207. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  12208. GGML_ASSERT(ggml_is_contiguous(tensor));
  12209. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  12210. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12211. const dpct::queue_ptr stream = g_syclStreams[g_main_device_index][0];
  12212. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  12213. .memcpy(extra->data_device[g_main_device_index],
  12214. tensor->data, ggml_nbytes(tensor))
  12215. .wait()));
  12216. }
  12217. catch (sycl::exception const &exc) {
  12218. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12219. << ", line:" << __LINE__ << std::endl;
  12220. std::exit(1);
  12221. }
  12222. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor) {
  12223. ggml_sycl_assign_buffers_impl(tensor, true, false, false);
  12224. }
  12225. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  12226. ggml_sycl_assign_buffers_impl(tensor, true, false, true);
  12227. }
  12228. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  12229. ggml_sycl_assign_buffers_impl(tensor, false, false, false);
  12230. }
  12231. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  12232. ggml_sycl_assign_buffers_impl(tensor, false, true, false);
  12233. }
  12234. void ggml_sycl_set_main_device(const int main_device) try {
  12235. if (main_device >= g_all_sycl_device_count) {
  12236. fprintf(stderr, "warning: cannot set main_device=%d because there are only %d devices. Using device %d instead.\n",
  12237. main_device, g_all_sycl_device_count, g_main_device);
  12238. return;
  12239. }
  12240. if (g_main_device != main_device && g_device_count >= 1) {
  12241. g_main_device = main_device;
  12242. g_main_device_index = get_device_index_by_id(g_main_device);
  12243. dpct::device_info prop;
  12244. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  12245. prop, dpct::dev_mgr::instance().get_device(g_main_device))));
  12246. fprintf(stderr, "Using device %d (%s) as main device\n",
  12247. g_main_device, prop.get_name());
  12248. }
  12249. }
  12250. catch (sycl::exception const &exc) {
  12251. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12252. << ", line:" << __LINE__ << std::endl;
  12253. std::exit(1);
  12254. }
  12255. void ggml_sycl_set_scratch_size(const size_t scratch_size) {
  12256. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  12257. // it still won't always work as expected, but it's better than nothing
  12258. if (scratch_size > g_scratch_size) {
  12259. ggml_sycl_free_scratch();
  12260. }
  12261. g_scratch_size = std::max(g_scratch_size, scratch_size);
  12262. }
  12263. void ggml_sycl_free_scratch() try {
  12264. if (g_scratch_buffer == nullptr) {
  12265. return;
  12266. }
  12267. ggml_sycl_set_device(g_main_device);
  12268. const dpct::queue_ptr stream = g_syclStreams[g_main_device_index][0];
  12269. SYCL_CHECK(CHECK_TRY_ERROR(
  12270. sycl::free(g_scratch_buffer, *stream)));
  12271. g_scratch_buffer = nullptr;
  12272. }
  12273. catch (sycl::exception const &exc) {
  12274. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12275. << ", line:" << __LINE__ << std::endl;
  12276. std::exit(1);
  12277. }
  12278. bool ggml_sycl_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  12279. if (!g_sycl_loaded) return false;
  12280. ggml_sycl_func_t func;
  12281. const bool any_on_device = tensor->backend == GGML_BACKEND_GPU
  12282. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_GPU || tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT))
  12283. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_GPU);
  12284. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  12285. return false;
  12286. }
  12287. if (tensor->op == GGML_OP_MUL_MAT) {
  12288. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  12289. #ifndef NDEBUG
  12290. 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]);
  12291. #endif
  12292. return false;
  12293. }
  12294. }
  12295. switch (tensor->op) {
  12296. case GGML_OP_REPEAT:
  12297. func = ggml_sycl_repeat;
  12298. break;
  12299. case GGML_OP_GET_ROWS:
  12300. func = ggml_sycl_get_rows;
  12301. break;
  12302. case GGML_OP_DUP:
  12303. func = ggml_sycl_dup;
  12304. break;
  12305. case GGML_OP_ADD:
  12306. func = ggml_sycl_add;
  12307. break;
  12308. case GGML_OP_ACC:
  12309. func = ggml_sycl_acc;
  12310. break;
  12311. case GGML_OP_MUL:
  12312. func = ggml_sycl_mul;
  12313. break;
  12314. case GGML_OP_DIV:
  12315. func = ggml_sycl_div;
  12316. break;
  12317. case GGML_OP_UNARY:
  12318. switch (ggml_get_unary_op(tensor)) {
  12319. case GGML_UNARY_OP_GELU:
  12320. func = ggml_sycl_gelu;
  12321. break;
  12322. case GGML_UNARY_OP_SILU:
  12323. func = ggml_sycl_silu;
  12324. break;
  12325. case GGML_UNARY_OP_GELU_QUICK:
  12326. func = ggml_sycl_gelu_quick;
  12327. break;
  12328. case GGML_UNARY_OP_TANH:
  12329. func = ggml_sycl_tanh;
  12330. break;
  12331. case GGML_UNARY_OP_RELU:
  12332. func = ggml_sycl_relu;
  12333. break;
  12334. default:
  12335. return false;
  12336. }
  12337. break;
  12338. case GGML_OP_NORM:
  12339. func = ggml_sycl_norm;
  12340. break;
  12341. case GGML_OP_GROUP_NORM:
  12342. func = ggml_sycl_group_norm;
  12343. break;
  12344. case GGML_OP_CONCAT:
  12345. func = ggml_sycl_concat;
  12346. break;
  12347. case GGML_OP_UPSCALE:
  12348. func = ggml_sycl_upscale;
  12349. break;
  12350. case GGML_OP_PAD:
  12351. func = ggml_sycl_pad;
  12352. break;
  12353. case GGML_OP_LEAKY_RELU:
  12354. func = ggml_sycl_leaky_relu;
  12355. break;
  12356. case GGML_OP_RMS_NORM:
  12357. func = ggml_sycl_rms_norm;
  12358. break;
  12359. case GGML_OP_MUL_MAT:
  12360. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  12361. return false;
  12362. }
  12363. func = ggml_sycl_mul_mat;
  12364. break;
  12365. case GGML_OP_MUL_MAT_ID:
  12366. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  12367. return false;
  12368. }
  12369. func = ggml_sycl_mul_mat_id;
  12370. break;
  12371. case GGML_OP_SCALE:
  12372. func = ggml_sycl_scale;
  12373. break;
  12374. case GGML_OP_SQR:
  12375. func = ggml_sycl_sqr;
  12376. break;
  12377. case GGML_OP_CLAMP:
  12378. func = ggml_sycl_clamp;
  12379. break;
  12380. case GGML_OP_CPY:
  12381. func = ggml_sycl_cpy;
  12382. break;
  12383. case GGML_OP_CONT:
  12384. func = ggml_sycl_dup;
  12385. break;
  12386. case GGML_OP_NONE:
  12387. case GGML_OP_RESHAPE:
  12388. case GGML_OP_VIEW:
  12389. case GGML_OP_PERMUTE:
  12390. case GGML_OP_TRANSPOSE:
  12391. func = ggml_sycl_nop;
  12392. break;
  12393. case GGML_OP_DIAG_MASK_INF:
  12394. func = ggml_sycl_diag_mask_inf;
  12395. break;
  12396. case GGML_OP_SOFT_MAX:
  12397. func = ggml_sycl_soft_max;
  12398. break;
  12399. case GGML_OP_ROPE:
  12400. func = ggml_sycl_rope;
  12401. break;
  12402. case GGML_OP_ALIBI:
  12403. func = ggml_sycl_alibi;
  12404. break;
  12405. case GGML_OP_IM2COL:
  12406. func = ggml_sycl_im2col;
  12407. break;
  12408. case GGML_OP_SUM_ROWS:
  12409. func = ggml_sycl_sum_rows;
  12410. break;
  12411. case GGML_OP_ARGSORT:
  12412. func = ggml_sycl_argsort;
  12413. break;
  12414. default:
  12415. return false;
  12416. }
  12417. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_GPU_SPLIT) {
  12418. ggml_sycl_set_peer_access(tensor->src[1]->ne[1]);
  12419. }
  12420. if (params->ith != 0) {
  12421. return true;
  12422. }
  12423. if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
  12424. return true;
  12425. }
  12426. func(tensor->src[0], tensor->src[1], tensor);
  12427. return true;
  12428. }
  12429. int ggml_sycl_get_device_count() try {
  12430. int device_count;
  12431. if (CHECK_TRY_ERROR(device_count =
  12432. dpct::dev_mgr::instance().device_count()) != 0) {
  12433. return 0;
  12434. }
  12435. return device_count;
  12436. }
  12437. catch (sycl::exception const &exc) {
  12438. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12439. << ", line:" << __LINE__ << std::endl;
  12440. std::exit(1);
  12441. }
  12442. void ggml_sycl_get_device_description(int device, char *description,
  12443. size_t description_size) try {
  12444. dpct::device_info prop;
  12445. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  12446. prop, dpct::dev_mgr::instance().get_device(device))));
  12447. snprintf(description, description_size, "%s", prop.get_name());
  12448. }
  12449. catch (sycl::exception const &exc) {
  12450. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12451. << ", line:" << __LINE__ << std::endl;
  12452. std::exit(1);
  12453. }
  12454. ////////////////////////////////////////////////////////////////////////////////
  12455. // backend interface
  12456. #define UNUSED GGML_UNUSED
  12457. struct ggml_backend_sycl_context {
  12458. int device;
  12459. std::string name;
  12460. };
  12461. // sycl buffer
  12462. struct ggml_backend_sycl_buffer_context {
  12463. int device;
  12464. void * dev_ptr = nullptr;
  12465. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  12466. size_t temp_tensor_extra_index = 0;
  12467. std::string name;
  12468. ggml_backend_sycl_buffer_context(int device, void * dev_ptr) : device(device), dev_ptr(dev_ptr) {}
  12469. ~ ggml_backend_sycl_buffer_context() {
  12470. delete[] temp_tensor_extras;
  12471. }
  12472. ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  12473. if (temp_tensor_extras == nullptr) {
  12474. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  12475. }
  12476. size_t alloc_index = temp_tensor_extra_index;
  12477. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  12478. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  12479. memset(extra, 0, sizeof(*extra));
  12480. return extra;
  12481. }
  12482. };
  12483. GGML_CALL static const char * ggml_backend_sycl_buffer_get_name(ggml_backend_buffer_t buffer) {
  12484. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  12485. return ctx->name.c_str();
  12486. }
  12487. GGML_CALL static bool ggml_backend_buffer_is_sycl(ggml_backend_buffer_t buffer) {
  12488. return buffer->iface.get_name == ggml_backend_sycl_buffer_get_name;
  12489. }
  12490. static void
  12491. ggml_backend_sycl_buffer_free_buffer(ggml_backend_buffer_t buffer) try {
  12492. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12493. ggml_sycl_set_device(ctx->device);
  12494. int device_index = get_device_index_by_id(ctx->device);
  12495. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  12496. SYCL_CHECK(
  12497. CHECK_TRY_ERROR(sycl::free(ctx->dev_ptr, *stream)));
  12498. delete ctx;
  12499. }
  12500. catch (sycl::exception const &exc) {
  12501. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12502. << ", line:" << __LINE__ << std::endl;
  12503. std::exit(1);
  12504. }
  12505. static void * ggml_backend_sycl_buffer_get_base(ggml_backend_buffer_t buffer) {
  12506. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12507. return ctx->dev_ptr;
  12508. }
  12509. static void ggml_backend_sycl_buffer_init_tensor(ggml_backend_buffer_t buffer,
  12510. ggml_tensor *tensor) try {
  12511. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12512. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  12513. assert(tensor->view_src->buffer->buft == buffer->buft);
  12514. tensor->backend = tensor->view_src->backend;
  12515. tensor->extra = tensor->view_src->extra;
  12516. return;
  12517. }
  12518. ggml_tensor_extra_gpu * extra = ctx->ggml_sycl_alloc_temp_tensor_extra();
  12519. extra->data_device[ctx->device] = tensor->data;
  12520. tensor->backend = GGML_BACKEND_GPU;
  12521. tensor->extra = extra;
  12522. if (ggml_is_quantized(tensor->type)) {
  12523. // initialize padding to 0 to avoid possible NaN values
  12524. int64_t row_low = 0;
  12525. int64_t row_high = ggml_nrows(tensor);
  12526. int64_t nrows_split = row_high - row_low;
  12527. size_t original_size = ggml_nbytes_split(tensor, nrows_split);
  12528. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  12529. if (padded_size > original_size && tensor->view_src == nullptr) {
  12530. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[ctx->device][0]->memset(
  12531. (char *)tensor->data + original_size, 0,
  12532. padded_size - original_size)));
  12533. }
  12534. }
  12535. UNUSED(buffer);
  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_set_tensor(ggml_backend_buffer_t buffer,
  12543. ggml_tensor *tensor,
  12544. const void *data, size_t offset,
  12545. size_t size) try {
  12546. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  12547. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12548. ggml_sycl_set_device(ctx->device);
  12549. int device_index = get_device_index_by_id(ctx->device);
  12550. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  12551. SYCL_CHECK(
  12552. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  12553. SYCL_CHECK(
  12554. CHECK_TRY_ERROR((*stream)
  12555. .memcpy((char *)tensor->data + offset, data, size)
  12556. .wait()));
  12557. }
  12558. catch (sycl::exception const &exc) {
  12559. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12560. << ", line:" << __LINE__ << std::endl;
  12561. std::exit(1);
  12562. }
  12563. static void ggml_backend_sycl_buffer_get_tensor(ggml_backend_buffer_t buffer,
  12564. const ggml_tensor *tensor,
  12565. void *data, size_t offset,
  12566. size_t size) try {
  12567. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  12568. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12569. ggml_sycl_set_device(ctx->device);
  12570. int device_index = get_device_index_by_id(ctx->device);
  12571. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  12572. SYCL_CHECK(
  12573. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  12574. SYCL_CHECK(CHECK_TRY_ERROR(
  12575. (*stream)
  12576. .memcpy(data, (const char *)tensor->data + offset, size)
  12577. .wait()));
  12578. }
  12579. catch (sycl::exception const &exc) {
  12580. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12581. << ", line:" << __LINE__ << std::endl;
  12582. std::exit(1);
  12583. }
  12584. static void ggml_backend_sycl_buffer_clear(ggml_backend_buffer_t buffer,
  12585. uint8_t value) try {
  12586. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12587. ggml_sycl_set_device(ctx->device);
  12588. int device_index = get_device_index_by_id(ctx->device);
  12589. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  12590. SYCL_CHECK(
  12591. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  12592. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  12593. .memset(ctx->dev_ptr, value, buffer->size)
  12594. .wait()));
  12595. }
  12596. catch (sycl::exception const &exc) {
  12597. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12598. << ", line:" << __LINE__ << std::endl;
  12599. std::exit(1);
  12600. }
  12601. static struct ggml_backend_buffer_i ggml_backend_sycl_buffer_interface = {
  12602. /* .get_name = */ ggml_backend_sycl_buffer_get_name,
  12603. /* .free_buffer = */ ggml_backend_sycl_buffer_free_buffer,
  12604. /* .get_base = */ ggml_backend_sycl_buffer_get_base,
  12605. /* .init_tensor = */ ggml_backend_sycl_buffer_init_tensor,
  12606. /* .set_tensor = */ ggml_backend_sycl_buffer_set_tensor,
  12607. /* .get_tensor = */ ggml_backend_sycl_buffer_get_tensor,
  12608. /* .cpy_tensor = */ NULL,
  12609. /* .clear = */ ggml_backend_sycl_buffer_clear,
  12610. /* .reset = */ NULL,
  12611. };
  12612. // sycl buffer type
  12613. struct ggml_backend_sycl_buffer_type_context {
  12614. int device;
  12615. std::string name;
  12616. };
  12617. GGML_CALL static const char * ggml_backend_sycl_buffer_type_name(ggml_backend_buffer_type_t buft) {
  12618. ggml_backend_sycl_buffer_type_context * ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  12619. return ctx->name.c_str();
  12620. }
  12621. static ggml_backend_buffer_t
  12622. ggml_backend_sycl_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
  12623. size_t size) try {
  12624. int device = (int) (intptr_t) buft->context;
  12625. ggml_sycl_set_device(device);
  12626. int device_index = get_device_index_by_id(device);
  12627. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  12628. size = std::max(size, (size_t)1); // syclMalloc returns null for size 0
  12629. void * dev_ptr;
  12630. SYCL_CHECK(CHECK_TRY_ERROR(dev_ptr = (void *)sycl::malloc_device(
  12631. size, *stream)));
  12632. ggml_backend_sycl_buffer_context * ctx = new ggml_backend_sycl_buffer_context(device, dev_ptr);
  12633. return ggml_backend_buffer_init(buft, ggml_backend_sycl_buffer_interface, ctx, size);
  12634. }
  12635. catch (sycl::exception const &exc) {
  12636. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12637. << ", line:" << __LINE__ << std::endl;
  12638. std::exit(1);
  12639. }
  12640. static size_t ggml_backend_sycl_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  12641. return 128;
  12642. UNUSED(buft);
  12643. }
  12644. static size_t ggml_backend_sycl_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  12645. int64_t row_low = 0;
  12646. int64_t row_high = ggml_nrows(tensor);
  12647. int64_t nrows_split = row_high - row_low;
  12648. size_t size = ggml_nbytes_split(tensor, nrows_split);
  12649. int64_t ne0 = tensor->ne[0];
  12650. if (ggml_is_quantized(tensor->type)) {
  12651. if (ne0 % MATRIX_ROW_PADDING != 0) {
  12652. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  12653. }
  12654. }
  12655. return size;
  12656. UNUSED(buft);
  12657. }
  12658. static bool ggml_backend_sycl_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  12659. return ggml_backend_is_sycl(backend);
  12660. UNUSED(buft);
  12661. }
  12662. static ggml_backend_buffer_type_i ggml_backend_sycl_buffer_type_interface = {
  12663. /* .get_name = */ ggml_backend_sycl_buffer_type_name,
  12664. /* .alloc_buffer = */ ggml_backend_sycl_buffer_type_alloc_buffer,
  12665. /* .get_alignment = */ ggml_backend_sycl_buffer_type_get_alignment,
  12666. /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
  12667. /* .get_alloc_size = */ ggml_backend_sycl_buffer_type_get_alloc_size,
  12668. /* .supports_backend = */ ggml_backend_sycl_buffer_type_supports_backend,
  12669. /* .is_host = */ nullptr,
  12670. };
  12671. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(int device) {
  12672. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  12673. static bool ggml_backend_sycl_buffer_type_initialized = false;
  12674. if (!ggml_backend_sycl_buffer_type_initialized) {
  12675. for (int i = 0; i < GGML_SYCL_MAX_DEVICES; i++) {
  12676. ggml_backend_sycl_buffer_types[i] = {
  12677. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  12678. /* .context = */ (ggml_backend_buffer_type_context_t) (intptr_t) i,
  12679. };
  12680. }
  12681. ggml_backend_sycl_buffer_type_initialized = true;
  12682. }
  12683. return &ggml_backend_sycl_buffer_types[device];
  12684. }
  12685. // host buffer type
  12686. GGML_CALL static const char * ggml_backend_sycl_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  12687. return GGML_SYCL_NAME "_Host";
  12688. UNUSED(buft);
  12689. }
  12690. GGML_CALL static const char * ggml_backend_sycl_host_buffer_name(ggml_backend_buffer_t buffer) {
  12691. return GGML_SYCL_NAME "_Host";
  12692. UNUSED(buffer);
  12693. }
  12694. static void ggml_backend_sycl_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  12695. ggml_sycl_host_free(buffer->context);
  12696. }
  12697. static ggml_backend_buffer_t ggml_backend_sycl_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  12698. void * ptr = ggml_sycl_host_malloc(size);
  12699. if (ptr == nullptr) {
  12700. // fallback to cpu buffer
  12701. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  12702. }
  12703. // FIXME: this is a hack to avoid having to implement a new buffer type
  12704. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  12705. buffer->buft = buft;
  12706. buffer->iface.free_buffer = ggml_backend_sycl_host_buffer_free_buffer;
  12707. return buffer;
  12708. }
  12709. ggml_backend_buffer_type_t ggml_backend_sycl_host_buffer_type() {
  12710. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_type_host = {
  12711. /* .iface = */ {
  12712. /* .get_name = */ ggml_backend_sycl_host_buffer_type_name,
  12713. /* .alloc_buffer = */ ggml_backend_sycl_host_buffer_type_alloc_buffer,
  12714. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  12715. /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
  12716. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  12717. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  12718. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  12719. },
  12720. /* .context = */ nullptr,
  12721. };
  12722. return &ggml_backend_sycl_buffer_type_host;
  12723. }
  12724. // backend
  12725. struct ggml_backend_context_sycl {
  12726. int device;
  12727. };
  12728. static const char * ggml_backend_sycl_name(ggml_backend_t backend) {
  12729. return GGML_SYCL_NAME;
  12730. UNUSED(backend);
  12731. }
  12732. static void ggml_backend_sycl_free(ggml_backend_t backend) {
  12733. ggml_backend_context_sycl * sycl_ctx = (ggml_backend_context_sycl *)backend->context;
  12734. delete sycl_ctx;
  12735. delete backend;
  12736. }
  12737. static ggml_backend_buffer_type_t ggml_backend_sycl_get_default_buffer_type(ggml_backend_t backend) {
  12738. ggml_backend_context_sycl * sycl_ctx = (ggml_backend_context_sycl *)backend->context;
  12739. return ggml_backend_sycl_buffer_type(sycl_ctx->device);
  12740. }
  12741. static void ggml_backend_sycl_set_tensor_async(ggml_backend_t backend,
  12742. ggml_tensor *tensor,
  12743. const void *data, size_t offset,
  12744. size_t size) try {
  12745. ggml_backend_context_sycl * sycl_ctx = (ggml_backend_context_sycl *)backend->context;
  12746. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  12747. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  12748. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  12749. (char *)tensor->data + offset, data, size)));
  12750. }
  12751. catch (sycl::exception const &exc) {
  12752. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12753. << ", line:" << __LINE__ << std::endl;
  12754. std::exit(1);
  12755. }
  12756. static void ggml_backend_sycl_get_tensor_async(ggml_backend_t backend,
  12757. const ggml_tensor *tensor,
  12758. void *data, size_t offset,
  12759. size_t size) try {
  12760. ggml_backend_context_sycl * sycl_ctx = (ggml_backend_context_sycl *)backend->context;
  12761. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  12762. GGML_ASSERT(tensor->backend == GGML_BACKEND_GPU);
  12763. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  12764. data, (const char *)tensor->data + offset, size)));
  12765. }
  12766. catch (sycl::exception const &exc) {
  12767. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12768. << ", line:" << __LINE__ << std::endl;
  12769. std::exit(1);
  12770. }
  12771. static void ggml_backend_sycl_synchronize(ggml_backend_t backend) try {
  12772. ggml_backend_context_sycl * sycl_ctx = (ggml_backend_context_sycl *)backend->context;
  12773. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->wait()));
  12774. UNUSED(backend);
  12775. }
  12776. catch (sycl::exception const &exc) {
  12777. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12778. << ", line:" << __LINE__ << std::endl;
  12779. std::exit(1);
  12780. }
  12781. static ggml_backend_graph_plan_t ggml_backend_sycl_graph_plan_create(ggml_backend_t backend, const ggml_cgraph * cgraph) {
  12782. GGML_ASSERT(!"not implemented");
  12783. return nullptr;
  12784. UNUSED(backend);
  12785. UNUSED(cgraph);
  12786. }
  12787. static void ggml_backend_sycl_graph_plan_free(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  12788. GGML_ASSERT(!"not implemented");
  12789. UNUSED(backend);
  12790. UNUSED(plan);
  12791. }
  12792. static void ggml_backend_sycl_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan) {
  12793. GGML_ASSERT(!"not implemented");
  12794. UNUSED(backend);
  12795. UNUSED(plan);
  12796. }
  12797. static bool ggml_backend_sycl_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  12798. ggml_backend_context_sycl * sycl_ctx = (ggml_backend_context_sycl *)backend->context;
  12799. ggml_sycl_set_main_device(sycl_ctx->device);
  12800. ggml_compute_params params = {};
  12801. params.type = GGML_TASK_COMPUTE;
  12802. params.ith = 0;
  12803. for (int i = 0; i < cgraph->n_nodes; i++) {
  12804. ggml_tensor * node = cgraph->nodes[i];
  12805. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE)
  12806. continue;
  12807. assert(node->backend == GGML_BACKEND_GPU);
  12808. assert(node->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  12809. assert(node->extra != nullptr);
  12810. for (int j = 0; j < GGML_MAX_SRC; j++) {
  12811. if (node->src[j] != nullptr) {
  12812. assert(node->src[j]->backend == GGML_BACKEND_GPU);
  12813. assert(node->src[j]->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  12814. assert(node->src[j]->extra != nullptr);
  12815. }
  12816. }
  12817. bool ok = ggml_sycl_compute_forward(&params, node);
  12818. if (!ok) {
  12819. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  12820. }
  12821. GGML_ASSERT(ok);
  12822. #if 0
  12823. if (node->type == GGML_TYPE_F32) {
  12824. syclDeviceSynchronize();
  12825. std::vector<float> tmp(ggml_nelements(node), 0.0f);
  12826. syclMemcpy(tmp.data(), node->data, ggml_nelements(node)*sizeof(float), syclMemcpyDeviceToHost);
  12827. printf("\n%s (%s) (%s %s) (%s %s): ", node->name, ggml_op_name(node->op),
  12828. ggml_type_name(node->src[0]->type),
  12829. node->src[1] ? ggml_type_name(node->src[1]->type) : "none",
  12830. node->src[0]->name,
  12831. node->src[1] ? node->src[1]->name : "none");
  12832. double sum = 0.0;
  12833. double sq_sum = 0.0;
  12834. for (int i = 0; i < ggml_nelements(node); i++) {
  12835. printf("%f ", tmp[i]);
  12836. sum += tmp[i];
  12837. sq_sum += tmp[i]*tmp[i];
  12838. }
  12839. printf("\n");
  12840. printf("sum: %f, ", sum);
  12841. printf("sq_sum: %f\n", sq_sum);
  12842. }
  12843. #endif
  12844. }
  12845. UNUSED(backend);
  12846. return true;
  12847. }
  12848. static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  12849. switch (op->op) {
  12850. case GGML_OP_UNARY:
  12851. switch (ggml_get_unary_op(op)) {
  12852. case GGML_UNARY_OP_GELU:
  12853. case GGML_UNARY_OP_SILU:
  12854. case GGML_UNARY_OP_RELU:
  12855. case GGML_UNARY_OP_GELU_QUICK:
  12856. case GGML_UNARY_OP_TANH:
  12857. return true;
  12858. default:
  12859. return false;
  12860. }
  12861. break;
  12862. case GGML_OP_MUL_MAT:
  12863. case GGML_OP_MUL_MAT_ID:
  12864. {
  12865. struct ggml_tensor * a;
  12866. struct ggml_tensor * b;
  12867. if (op->op == GGML_OP_MUL_MAT) {
  12868. a = op->src[0];
  12869. b = op->src[1];
  12870. } else {
  12871. a = op->src[2];
  12872. b = op->src[1];
  12873. }
  12874. if (a->ne[3] != b->ne[3]) {
  12875. return false;
  12876. }
  12877. if (a->type == GGML_TYPE_IQ2_XXS) {
  12878. return false;
  12879. }
  12880. if (a->type == GGML_TYPE_IQ2_XS) {
  12881. return false;
  12882. }
  12883. return true;
  12884. } break;
  12885. case GGML_OP_GET_ROWS:
  12886. {
  12887. switch (op->src[0]->type) {
  12888. case GGML_TYPE_F16:
  12889. case GGML_TYPE_F32:
  12890. case GGML_TYPE_Q4_0:
  12891. case GGML_TYPE_Q4_1:
  12892. case GGML_TYPE_Q5_0:
  12893. case GGML_TYPE_Q5_1:
  12894. case GGML_TYPE_Q8_0:
  12895. return true;
  12896. default:
  12897. return false;
  12898. }
  12899. } break;
  12900. case GGML_OP_CPY:
  12901. {
  12902. ggml_type src0_type = op->src[0]->type;
  12903. ggml_type src1_type = op->src[1]->type;
  12904. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  12905. return true;
  12906. }
  12907. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  12908. return true;
  12909. }
  12910. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  12911. return true;
  12912. }
  12913. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  12914. return true;
  12915. }
  12916. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  12917. return true;
  12918. }
  12919. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  12920. return true;
  12921. }
  12922. return false;
  12923. } break;
  12924. case GGML_OP_CONCAT:
  12925. {
  12926. ggml_type src0_type = op->src[0]->type;
  12927. if (src0_type == GGML_TYPE_F32) {
  12928. return true;
  12929. } else {
  12930. return false;
  12931. }
  12932. } break;
  12933. case GGML_OP_NONE:
  12934. case GGML_OP_RESHAPE:
  12935. case GGML_OP_VIEW:
  12936. case GGML_OP_PERMUTE:
  12937. case GGML_OP_TRANSPOSE:
  12938. case GGML_OP_NORM:
  12939. case GGML_OP_REPEAT:
  12940. case GGML_OP_DUP:
  12941. case GGML_OP_ADD:
  12942. case GGML_OP_MUL:
  12943. case GGML_OP_DIV:
  12944. case GGML_OP_RMS_NORM:
  12945. case GGML_OP_SCALE:
  12946. case GGML_OP_SQR:
  12947. case GGML_OP_CLAMP:
  12948. case GGML_OP_CONT:
  12949. case GGML_OP_DIAG_MASK_INF:
  12950. case GGML_OP_SOFT_MAX:
  12951. case GGML_OP_ROPE:
  12952. case GGML_OP_ALIBI:
  12953. case GGML_OP_IM2COL:
  12954. case GGML_OP_SUM_ROWS:
  12955. case GGML_OP_ARGSORT:
  12956. case GGML_OP_ACC:
  12957. case GGML_OP_GROUP_NORM:
  12958. case GGML_OP_UPSCALE:
  12959. case GGML_OP_PAD:
  12960. case GGML_OP_LEAKY_RELU:
  12961. return true;
  12962. default:
  12963. return false;
  12964. }
  12965. UNUSED(backend);
  12966. }
  12967. static ggml_backend_i ggml_backend_sycl_interface = {
  12968. /* .get_name = */ ggml_backend_sycl_name,
  12969. /* .free = */ ggml_backend_sycl_free,
  12970. /* .get_default_buffer_type = */ ggml_backend_sycl_get_default_buffer_type,
  12971. /* .set_tensor_async = */ ggml_backend_sycl_set_tensor_async,
  12972. /* .get_tensor_async = */ ggml_backend_sycl_get_tensor_async,
  12973. /* .cpy_tensor_async = */ NULL,
  12974. /* .synchronize = */ ggml_backend_sycl_synchronize,
  12975. /* .graph_plan_create = */ ggml_backend_sycl_graph_plan_create,
  12976. /* .graph_plan_free = */ ggml_backend_sycl_graph_plan_free,
  12977. /* .graph_plan_compute = */ ggml_backend_sycl_graph_plan_compute,
  12978. /* .graph_compute = */ ggml_backend_sycl_graph_compute,
  12979. /* .supports_op = */ ggml_backend_sycl_supports_op,
  12980. };
  12981. ggml_backend_t ggml_backend_sycl_init(int device) {
  12982. ggml_init_sycl(); // TODO: remove from ggml.c
  12983. if (device < 0 || device >= ggml_sycl_get_device_count()) {
  12984. fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
  12985. return nullptr;
  12986. }
  12987. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  12988. ggml_sycl_set_main_device(device);
  12989. ggml_backend_context_sycl * ctx = new ggml_backend_context_sycl {
  12990. /* .device = */ device
  12991. };
  12992. ggml_backend_t sycl_backend = new ggml_backend {
  12993. /* .interface = */ ggml_backend_sycl_interface,
  12994. /* .context = */ ctx
  12995. };
  12996. return sycl_backend;
  12997. }
  12998. bool ggml_backend_is_sycl(ggml_backend_t backend) {
  12999. return backend->iface.get_name == ggml_backend_sycl_name;
  13000. }
  13001. static ggml_backend_t ggml_backend_reg_sycl_init(const char * params, void * user_data) {
  13002. ggml_backend_t sycl_backend = ggml_backend_sycl_init((int) (intptr_t) user_data);
  13003. return sycl_backend;
  13004. UNUSED(params);
  13005. }
  13006. extern "C" int ggml_backend_sycl_reg_devices();
  13007. int ggml_backend_sycl_reg_devices() {
  13008. int device_count = ggml_sycl_get_device_count();
  13009. for (int i = 0; i < device_count; i++) {
  13010. char name[128];
  13011. snprintf(name, sizeof(name), "%s%d", GGML_SYCL_NAME, i);
  13012. ggml_backend_register(name, ggml_backend_reg_sycl_init, ggml_backend_sycl_buffer_type(i), (void *) (intptr_t) i);
  13013. }
  13014. return device_count;
  13015. }