ggml-sycl.cpp 609 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825
  1. //
  2. // MIT license
  3. // Copyright (C) 2024 Intel Corporation
  4. // SPDX-License-Identifier: MIT
  5. //
  6. //
  7. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  8. // See https://llvm.org/LICENSE.txt for license information.
  9. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  10. //
  11. #include <algorithm>
  12. #include <assert.h>
  13. #include <atomic>
  14. #include <cinttypes>
  15. #include <cstddef>
  16. #include <cstdint>
  17. #include <float.h>
  18. #include <limits>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <vector>
  22. #include <cmath>
  23. #include <iostream>
  24. #include <fstream>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <sycl/sycl.hpp>
  28. #include <sycl/half_type.hpp>
  29. #include "ggml-sycl.h"
  30. #include "ggml.h"
  31. #include "ggml-backend-impl.h"
  32. /*
  33. Following definition copied from DPCT head files, which are used by ggml-sycl.cpp
  34. */
  35. // COPY from DPCT head files
  36. #include <sycl/sycl.hpp>
  37. #include <oneapi/mkl.hpp>
  38. #include <map>
  39. #if defined(__linux__)
  40. #include <sys/mman.h>
  41. #elif defined(_WIN64)
  42. #ifndef NOMINMAX
  43. #define NOMINMAX
  44. #endif
  45. #include <windows.h>
  46. #else
  47. #error "Only support Windows and Linux."
  48. #endif
  49. #if defined(__linux__)
  50. #include <unistd.h>
  51. #include <sys/syscall.h>
  52. #endif
  53. #if defined(_WIN64)
  54. #ifndef NOMINMAX
  55. #define NOMINMAX
  56. #endif
  57. #include <windows.h>
  58. #endif
  59. #define DPCT_COMPATIBILITY_TEMP (900)
  60. #if defined(_MSC_VER)
  61. #define __dpct_align__(n) __declspec(align(n))
  62. #define __dpct_inline__ __forceinline
  63. #else
  64. #define __dpct_align__(n) __attribute__((aligned(n)))
  65. #define __dpct_inline__ __inline__ __attribute__((always_inline))
  66. #endif
  67. #if defined(_MSC_VER)
  68. #define __dpct_noinline__ __declspec(noinline)
  69. #else
  70. #define __dpct_noinline__ __attribute__((noinline))
  71. #endif
  72. namespace dpct
  73. {
  74. typedef sycl::queue *queue_ptr;
  75. typedef sycl::event *event_ptr;
  76. typedef char *device_ptr;
  77. typedef uint8_t byte_t;
  78. typedef sycl::buffer<byte_t> buffer_t;
  79. /// SYCL default exception handler
  80. inline auto exception_handler = [](sycl::exception_list exceptions)
  81. {
  82. for (std::exception_ptr const &e : exceptions)
  83. {
  84. try
  85. {
  86. std::rethrow_exception(e);
  87. }
  88. catch (sycl::exception const &e)
  89. {
  90. std::cerr << "Caught asynchronous SYCL exception:" << std::endl
  91. << e.what() << std::endl
  92. << "Exception caught at file:" << __FILE__
  93. << ", line:" << __LINE__ << std::endl;
  94. }
  95. }
  96. };
  97. enum error_code
  98. {
  99. success = 0,
  100. default_error = 999
  101. };
  102. enum memcpy_direction
  103. {
  104. host_to_host,
  105. host_to_device,
  106. device_to_host,
  107. device_to_device,
  108. automatic
  109. };
  110. enum memory_region
  111. {
  112. global = 0, // device global memory
  113. constant, // device constant memory
  114. local, // device local memory
  115. shared, // memory which can be accessed by host and device
  116. };
  117. enum class library_data_t : unsigned char
  118. {
  119. real_float = 0,
  120. complex_float,
  121. real_double,
  122. complex_double,
  123. real_half,
  124. complex_half,
  125. real_bfloat16,
  126. complex_bfloat16,
  127. real_int4,
  128. complex_int4,
  129. real_uint4,
  130. complex_uint4,
  131. real_int8,
  132. complex_int8,
  133. real_uint8,
  134. complex_uint8,
  135. real_int16,
  136. complex_int16,
  137. real_uint16,
  138. complex_uint16,
  139. real_int32,
  140. complex_int32,
  141. real_uint32,
  142. complex_uint32,
  143. real_int64,
  144. complex_int64,
  145. real_uint64,
  146. complex_uint64,
  147. real_int8_4,
  148. real_int8_32,
  149. real_uint8_4,
  150. library_data_t_size
  151. };
  152. template <typename T>
  153. struct DataType
  154. {
  155. using T2 = T;
  156. };
  157. template <typename T>
  158. struct DataType<sycl::vec<T, 2>>
  159. {
  160. using T2 = std::complex<T>;
  161. };
  162. static void destroy_event(event_ptr event)
  163. {
  164. delete event;
  165. }
  166. static inline unsigned int get_tid()
  167. {
  168. #if defined(__linux__)
  169. return syscall(SYS_gettid);
  170. #elif defined(_WIN64)
  171. return GetCurrentThreadId();
  172. #else
  173. #error "Only support Windows and Linux."
  174. #endif
  175. }
  176. namespace detail
  177. {
  178. static void get_version(const sycl::device &dev, int &major, int &minor)
  179. {
  180. // Version string has the following format:
  181. // a. OpenCL<space><major.minor><space><vendor-specific-information>
  182. // b. <major.minor>
  183. std::string ver;
  184. ver = dev.get_info<sycl::info::device::version>();
  185. std::string::size_type i = 0;
  186. while (i < ver.size())
  187. {
  188. if (isdigit(ver[i]))
  189. break;
  190. i++;
  191. }
  192. major = std::stoi(&(ver[i]));
  193. while (i < ver.size())
  194. {
  195. if (ver[i] == '.')
  196. break;
  197. i++;
  198. }
  199. i++;
  200. minor = std::stoi(&(ver[i]));
  201. }
  202. template <typename tag, typename T>
  203. class generic_error_type
  204. {
  205. public:
  206. generic_error_type() = default;
  207. generic_error_type(T value) : value{value} {}
  208. operator T() const { return value; }
  209. private:
  210. T value;
  211. };
  212. } // namespace detail
  213. /// Pitched 2D/3D memory data.
  214. class pitched_data
  215. {
  216. public:
  217. pitched_data() : pitched_data(nullptr, 0, 0, 0) {}
  218. pitched_data(void *data, size_t pitch, size_t x, size_t y)
  219. : _data(data), _pitch(pitch), _x(x), _y(y) {}
  220. void *get_data_ptr() { return _data; }
  221. void set_data_ptr(void *data) { _data = data; }
  222. size_t get_pitch() { return _pitch; }
  223. void set_pitch(size_t pitch) { _pitch = pitch; }
  224. size_t get_x() { return _x; }
  225. void set_x(size_t x) { _x = x; };
  226. size_t get_y() { return _y; }
  227. void set_y(size_t y) { _y = y; }
  228. private:
  229. void *_data;
  230. size_t _pitch, _x, _y;
  231. };
  232. class device_info
  233. {
  234. public:
  235. // get interface
  236. const char *get_name() const { return _name; }
  237. char *get_name() { return _name; }
  238. template <typename WorkItemSizesTy = sycl::range<3>,
  239. std::enable_if_t<std::is_same_v<WorkItemSizesTy, sycl::range<3>> ||
  240. std::is_same_v<WorkItemSizesTy, int *>,
  241. int> = 0>
  242. auto get_max_work_item_sizes() const
  243. {
  244. if constexpr (std::is_same_v<WorkItemSizesTy, sycl::range<3>>)
  245. return sycl::range<3>(_max_work_item_sizes_i[0],
  246. _max_work_item_sizes_i[1],
  247. _max_work_item_sizes_i[2]);
  248. else
  249. {
  250. return _max_work_item_sizes_i;
  251. }
  252. }
  253. template <typename WorkItemSizesTy = sycl::range<3>,
  254. std::enable_if_t<std::is_same_v<WorkItemSizesTy, sycl::range<3>> ||
  255. std::is_same_v<WorkItemSizesTy, int *>,
  256. int> = 0>
  257. auto get_max_work_item_sizes()
  258. {
  259. if constexpr (std::is_same_v<WorkItemSizesTy, sycl::range<3>>)
  260. return sycl::range<3>(_max_work_item_sizes_i[0],
  261. _max_work_item_sizes_i[1],
  262. _max_work_item_sizes_i[2]);
  263. else
  264. {
  265. return _max_work_item_sizes_i;
  266. }
  267. }
  268. bool get_host_unified_memory() const { return _host_unified_memory; }
  269. int get_major_version() const { return _major; }
  270. int get_minor_version() const { return _minor; }
  271. int get_integrated() const { return _integrated; }
  272. int get_max_clock_frequency() const { return _frequency; }
  273. int get_max_compute_units() const { return _max_compute_units; }
  274. int get_max_work_group_size() const { return _max_work_group_size; }
  275. int get_max_sub_group_size() const { return _max_sub_group_size; }
  276. int get_max_work_items_per_compute_unit() const
  277. {
  278. return _max_work_items_per_compute_unit;
  279. }
  280. int get_max_register_size_per_work_group() const
  281. {
  282. return _max_register_size_per_work_group;
  283. }
  284. template <typename NDRangeSizeTy = size_t *,
  285. std::enable_if_t<std::is_same_v<NDRangeSizeTy, size_t *> ||
  286. std::is_same_v<NDRangeSizeTy, int *>,
  287. int> = 0>
  288. auto get_max_nd_range_size() const
  289. {
  290. if constexpr (std::is_same_v<NDRangeSizeTy, size_t *>)
  291. return _max_nd_range_size;
  292. else
  293. return _max_nd_range_size_i;
  294. }
  295. template <typename NDRangeSizeTy = size_t *,
  296. std::enable_if_t<std::is_same_v<NDRangeSizeTy, size_t *> ||
  297. std::is_same_v<NDRangeSizeTy, int *>,
  298. int> = 0>
  299. auto get_max_nd_range_size()
  300. {
  301. if constexpr (std::is_same_v<NDRangeSizeTy, size_t *>)
  302. return _max_nd_range_size;
  303. else
  304. return _max_nd_range_size_i;
  305. }
  306. size_t get_global_mem_size() const { return _global_mem_size; }
  307. size_t get_local_mem_size() const { return _local_mem_size; }
  308. size_t get_max_mem_alloc_size() const { return _max_mem_alloc_size; }
  309. /// Returns the maximum clock rate of device's global memory in kHz. If
  310. /// compiler does not support this API then returns default value 3200000 kHz.
  311. unsigned int get_memory_clock_rate() const { return _memory_clock_rate; }
  312. /// Returns the maximum bus width between device and memory in bits. If
  313. /// compiler does not support this API then returns default value 64 bits.
  314. unsigned int get_memory_bus_width() const { return _memory_bus_width; }
  315. uint32_t get_device_id() const { return _device_id; }
  316. std::array<unsigned char, 16> get_uuid() const { return _uuid; }
  317. /// Returns global memory cache size in bytes.
  318. unsigned int get_global_mem_cache_size() const
  319. {
  320. return _global_mem_cache_size;
  321. }
  322. // set interface
  323. void set_name(const char *name)
  324. {
  325. size_t length = strlen(name);
  326. if (length < 256)
  327. {
  328. std::memcpy(_name, name, length + 1);
  329. }
  330. else
  331. {
  332. std::memcpy(_name, name, 255);
  333. _name[255] = '\0';
  334. }
  335. }
  336. void set_max_work_item_sizes(const sycl::range<3> max_work_item_sizes)
  337. {
  338. for (int i = 0; i < 3; ++i)
  339. _max_work_item_sizes_i[i] = max_work_item_sizes[i];
  340. }
  341. [[deprecated]] void
  342. set_max_work_item_sizes(const sycl::id<3> max_work_item_sizes)
  343. {
  344. for (int i = 0; i < 3; ++i)
  345. {
  346. _max_work_item_sizes_i[i] = max_work_item_sizes[i];
  347. }
  348. }
  349. void set_host_unified_memory(bool host_unified_memory)
  350. {
  351. _host_unified_memory = host_unified_memory;
  352. }
  353. void set_major_version(int major) { _major = major; }
  354. void set_minor_version(int minor) { _minor = minor; }
  355. void set_integrated(int integrated) { _integrated = integrated; }
  356. void set_max_clock_frequency(int frequency) { _frequency = frequency; }
  357. void set_max_compute_units(int max_compute_units)
  358. {
  359. _max_compute_units = max_compute_units;
  360. }
  361. void set_global_mem_size(size_t global_mem_size)
  362. {
  363. _global_mem_size = global_mem_size;
  364. }
  365. void set_local_mem_size(size_t local_mem_size)
  366. {
  367. _local_mem_size = local_mem_size;
  368. }
  369. void set_max_mem_alloc_size(size_t max_mem_alloc_size)
  370. {
  371. _max_mem_alloc_size = max_mem_alloc_size;
  372. }
  373. void set_max_work_group_size(int max_work_group_size)
  374. {
  375. _max_work_group_size = max_work_group_size;
  376. }
  377. void set_max_sub_group_size(int max_sub_group_size)
  378. {
  379. _max_sub_group_size = max_sub_group_size;
  380. }
  381. void
  382. set_max_work_items_per_compute_unit(int max_work_items_per_compute_unit)
  383. {
  384. _max_work_items_per_compute_unit = max_work_items_per_compute_unit;
  385. }
  386. void set_max_nd_range_size(int max_nd_range_size[])
  387. {
  388. for (int i = 0; i < 3; i++)
  389. {
  390. _max_nd_range_size[i] = max_nd_range_size[i];
  391. _max_nd_range_size_i[i] = max_nd_range_size[i];
  392. }
  393. }
  394. void set_memory_clock_rate(unsigned int memory_clock_rate)
  395. {
  396. _memory_clock_rate = memory_clock_rate;
  397. }
  398. void set_memory_bus_width(unsigned int memory_bus_width)
  399. {
  400. _memory_bus_width = memory_bus_width;
  401. }
  402. void
  403. set_max_register_size_per_work_group(int max_register_size_per_work_group)
  404. {
  405. _max_register_size_per_work_group = max_register_size_per_work_group;
  406. }
  407. void set_device_id(uint32_t device_id)
  408. {
  409. _device_id = device_id;
  410. }
  411. void set_uuid(std::array<unsigned char, 16> uuid)
  412. {
  413. _uuid = std::move(uuid);
  414. }
  415. void set_global_mem_cache_size(unsigned int global_mem_cache_size)
  416. {
  417. _global_mem_cache_size = global_mem_cache_size;
  418. }
  419. private:
  420. char _name[256];
  421. int _max_work_item_sizes_i[3];
  422. bool _host_unified_memory = false;
  423. int _major;
  424. int _minor;
  425. int _integrated = 0;
  426. int _frequency;
  427. // Set estimated value 3200000 kHz as default value.
  428. unsigned int _memory_clock_rate = 3200000;
  429. // Set estimated value 64 bits as default value.
  430. unsigned int _memory_bus_width = 64;
  431. unsigned int _global_mem_cache_size;
  432. int _max_compute_units;
  433. int _max_work_group_size;
  434. int _max_sub_group_size;
  435. int _max_work_items_per_compute_unit;
  436. int _max_register_size_per_work_group;
  437. size_t _global_mem_size;
  438. size_t _local_mem_size;
  439. size_t _max_mem_alloc_size;
  440. size_t _max_nd_range_size[3];
  441. int _max_nd_range_size_i[3];
  442. uint32_t _device_id;
  443. std::array<unsigned char, 16> _uuid;
  444. };
  445. static int get_major_version(const sycl::device &dev)
  446. {
  447. int major, minor;
  448. detail::get_version(dev, major, minor);
  449. return major;
  450. }
  451. static int get_minor_version(const sycl::device &dev)
  452. {
  453. int major, minor;
  454. detail::get_version(dev, major, minor);
  455. return minor;
  456. }
  457. static void get_device_info(device_info &out, const sycl::device &dev)
  458. {
  459. device_info prop;
  460. prop.set_name(dev.get_info<sycl::info::device::name>().c_str());
  461. int major, minor;
  462. detail::get_version(dev, major, minor);
  463. prop.set_major_version(major);
  464. prop.set_minor_version(minor);
  465. prop.set_max_work_item_sizes(
  466. #if (__SYCL_COMPILER_VERSION && __SYCL_COMPILER_VERSION < 20220902)
  467. // oneAPI DPC++ compiler older than 2022/09/02, where max_work_item_sizes
  468. // is an enum class element
  469. dev.get_info<sycl::info::device::max_work_item_sizes>());
  470. #else
  471. // SYCL 2020-conformant code, max_work_item_sizes is a struct templated by
  472. // an int
  473. dev.get_info<sycl::info::device::max_work_item_sizes<3>>());
  474. #endif
  475. prop.set_host_unified_memory(dev.has(sycl::aspect::usm_host_allocations));
  476. prop.set_max_clock_frequency(
  477. dev.get_info<sycl::info::device::max_clock_frequency>() * 1000);
  478. prop.set_max_compute_units(
  479. dev.get_info<sycl::info::device::max_compute_units>());
  480. prop.set_max_work_group_size(
  481. dev.get_info<sycl::info::device::max_work_group_size>());
  482. prop.set_global_mem_size(dev.get_info<sycl::info::device::global_mem_size>());
  483. prop.set_local_mem_size(dev.get_info<sycl::info::device::local_mem_size>());
  484. prop.set_max_mem_alloc_size(dev.get_info<sycl::info::device::max_mem_alloc_size>());
  485. #if (defined(SYCL_EXT_INTEL_DEVICE_INFO) && SYCL_EXT_INTEL_DEVICE_INFO >= 6)
  486. if (dev.has(sycl::aspect::ext_intel_memory_clock_rate))
  487. {
  488. unsigned int tmp =
  489. dev.get_info<sycl::ext::intel::info::device::memory_clock_rate>();
  490. if (tmp != 0)
  491. prop.set_memory_clock_rate(1000 * tmp);
  492. }
  493. if (dev.has(sycl::aspect::ext_intel_memory_bus_width))
  494. {
  495. prop.set_memory_bus_width(
  496. dev.get_info<sycl::ext::intel::info::device::memory_bus_width>());
  497. }
  498. if (dev.has(sycl::aspect::ext_intel_device_id))
  499. {
  500. prop.set_device_id(
  501. dev.get_info<sycl::ext::intel::info::device::device_id>());
  502. }
  503. if (dev.has(sycl::aspect::ext_intel_device_info_uuid))
  504. {
  505. prop.set_uuid(dev.get_info<sycl::ext::intel::info::device::uuid>());
  506. }
  507. #elif defined(_MSC_VER) && !defined(__clang__)
  508. #pragma message("get_device_info: querying memory_clock_rate and \
  509. memory_bus_width are not supported by the compiler used. \
  510. Use 3200000 kHz as memory_clock_rate default value. \
  511. Use 64 bits as memory_bus_width default value.")
  512. #else
  513. #warning "get_device_info: querying memory_clock_rate and \
  514. memory_bus_width are not supported by the compiler used. \
  515. Use 3200000 kHz as memory_clock_rate default value. \
  516. Use 64 bits as memory_bus_width default value."
  517. #endif
  518. size_t max_sub_group_size = 1;
  519. std::vector<size_t> sub_group_sizes =
  520. dev.get_info<sycl::info::device::sub_group_sizes>();
  521. for (const auto &sub_group_size : sub_group_sizes)
  522. {
  523. if (max_sub_group_size < sub_group_size)
  524. max_sub_group_size = sub_group_size;
  525. }
  526. prop.set_max_sub_group_size(max_sub_group_size);
  527. prop.set_max_work_items_per_compute_unit(
  528. dev.get_info<sycl::info::device::max_work_group_size>());
  529. int max_nd_range_size[] = {0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF};
  530. prop.set_max_nd_range_size(max_nd_range_size);
  531. // Estimates max register size per work group, feel free to update the value
  532. // according to device properties.
  533. prop.set_max_register_size_per_work_group(65536);
  534. prop.set_global_mem_cache_size(
  535. dev.get_info<sycl::info::device::global_mem_cache_size>());
  536. out = prop;
  537. }
  538. /// dpct device extension
  539. class device_ext : public sycl::device
  540. {
  541. typedef std::mutex mutex_type;
  542. public:
  543. device_ext() : sycl::device(), _ctx(*this) {}
  544. ~device_ext()
  545. {
  546. std::lock_guard<mutex_type> lock(m_mutex);
  547. clear_queues();
  548. }
  549. device_ext(const sycl::device &base) : sycl::device(base), _ctx(*this)
  550. {
  551. std::lock_guard<mutex_type> lock(m_mutex);
  552. init_queues();
  553. }
  554. int is_native_atomic_supported() { return 0; }
  555. int get_major_version() const
  556. {
  557. return dpct::get_major_version(*this);
  558. }
  559. int get_minor_version() const
  560. {
  561. return dpct::get_minor_version(*this);
  562. }
  563. int get_max_compute_units() const
  564. {
  565. return get_device_info().get_max_compute_units();
  566. }
  567. /// Return the maximum clock frequency of this device in KHz.
  568. int get_max_clock_frequency() const
  569. {
  570. return get_device_info().get_max_clock_frequency();
  571. }
  572. int get_integrated() const { return get_device_info().get_integrated(); }
  573. int get_max_sub_group_size() const
  574. {
  575. return get_device_info().get_max_sub_group_size();
  576. }
  577. int get_max_register_size_per_work_group() const
  578. {
  579. return get_device_info().get_max_register_size_per_work_group();
  580. }
  581. int get_max_work_group_size() const
  582. {
  583. return get_device_info().get_max_work_group_size();
  584. }
  585. int get_mem_base_addr_align() const
  586. {
  587. return get_info<sycl::info::device::mem_base_addr_align>();
  588. }
  589. size_t get_global_mem_size() const
  590. {
  591. return get_device_info().get_global_mem_size();
  592. }
  593. size_t get_max_mem_alloc_size() const
  594. {
  595. return get_device_info().get_max_mem_alloc_size();
  596. }
  597. /// Get the number of bytes of free and total memory on the SYCL device.
  598. /// \param [out] free_memory The number of bytes of free memory on the SYCL device.
  599. /// \param [out] total_memory The number of bytes of total memory on the SYCL device.
  600. void get_memory_info(size_t &free_memory, size_t &total_memory)
  601. {
  602. total_memory = get_device_info().get_global_mem_size();
  603. const char *warning_info = "get_memory_info: [warning] ext_intel_free_memory is not "
  604. "supported (export/set ZES_ENABLE_SYSMAN=1 to support), "
  605. "use total memory as free memory";
  606. #if (defined(__SYCL_COMPILER_VERSION) && __SYCL_COMPILER_VERSION >= 20221105)
  607. if (!has(sycl::aspect::ext_intel_free_memory))
  608. {
  609. std::cerr << warning_info << std::endl;
  610. free_memory = total_memory;
  611. }
  612. else
  613. {
  614. free_memory = get_info<sycl::ext::intel::info::device::free_memory>();
  615. }
  616. #else
  617. std::cerr << warning_info << std::endl;
  618. free_memory = total_memory;
  619. #if defined(_MSC_VER) && !defined(__clang__)
  620. #pragma message("Querying the number of bytes of free memory is not supported")
  621. #else
  622. #warning "Querying the number of bytes of free memory is not supported"
  623. #endif
  624. #endif
  625. }
  626. void get_device_info(device_info &out) const
  627. {
  628. dpct::get_device_info(out, *this);
  629. }
  630. device_info get_device_info() const
  631. {
  632. device_info prop;
  633. dpct::get_device_info(prop, *this);
  634. return prop;
  635. }
  636. void reset()
  637. {
  638. std::lock_guard<mutex_type> lock(m_mutex);
  639. clear_queues();
  640. init_queues();
  641. }
  642. sycl::queue &in_order_queue() { return *_q_in_order; }
  643. sycl::queue &out_of_order_queue() { return *_q_out_of_order; }
  644. sycl::queue &default_queue()
  645. {
  646. #ifdef DPCT_USM_LEVEL_NONE
  647. return out_of_order_queue();
  648. #else
  649. return in_order_queue();
  650. #endif // DPCT_USM_LEVEL_NONE
  651. }
  652. void queues_wait_and_throw()
  653. {
  654. std::unique_lock<mutex_type> lock(m_mutex);
  655. std::vector<std::shared_ptr<sycl::queue>> current_queues(
  656. _queues);
  657. lock.unlock();
  658. for (const auto &q : current_queues)
  659. {
  660. q->wait_and_throw();
  661. }
  662. // Guard the destruct of current_queues to make sure the ref count is safe.
  663. lock.lock();
  664. }
  665. sycl::queue *create_queue(bool enable_exception_handler = false)
  666. {
  667. #ifdef DPCT_USM_LEVEL_NONE
  668. return create_out_of_order_queue(enable_exception_handler);
  669. #else
  670. return create_in_order_queue(enable_exception_handler);
  671. #endif // DPCT_USM_LEVEL_NONE
  672. }
  673. sycl::queue *create_queue(sycl::context context, sycl::device device,
  674. bool enable_exception_handler = false) {
  675. return create_in_order_queue(context, device, enable_exception_handler);
  676. }
  677. sycl::queue *create_in_order_queue(bool enable_exception_handler = false) {
  678. std::lock_guard<mutex_type> lock(m_mutex);
  679. return create_queue_impl(enable_exception_handler,
  680. sycl::property::queue::in_order());
  681. }
  682. sycl::queue *create_in_order_queue(sycl::context context, sycl::device device,
  683. bool enable_exception_handler = false) {
  684. std::lock_guard<mutex_type> lock(m_mutex);
  685. return create_queue_impl(context, device, enable_exception_handler,
  686. sycl::property::queue::in_order());
  687. }
  688. sycl::queue *create_out_of_order_queue(bool enable_exception_handler = false) {
  689. std::lock_guard<mutex_type> lock(m_mutex);
  690. return create_queue_impl(enable_exception_handler);
  691. }
  692. void destroy_queue(sycl::queue *&queue)
  693. {
  694. std::lock_guard<mutex_type> lock(m_mutex);
  695. _queues.erase(std::remove_if(_queues.begin(), _queues.end(),
  696. [=](const std::shared_ptr<sycl::queue> &q) -> bool
  697. {
  698. return q.get() == queue;
  699. }),
  700. _queues.end());
  701. queue = nullptr;
  702. }
  703. void set_saved_queue(sycl::queue *q)
  704. {
  705. std::lock_guard<mutex_type> lock(m_mutex);
  706. _saved_queue = q;
  707. }
  708. sycl::queue *get_saved_queue() const
  709. {
  710. std::lock_guard<mutex_type> lock(m_mutex);
  711. return _saved_queue;
  712. }
  713. sycl::context get_context() const { return _ctx; }
  714. private:
  715. void clear_queues()
  716. {
  717. _queues.clear();
  718. _q_in_order = _q_out_of_order = _saved_queue = nullptr;
  719. }
  720. void init_queues()
  721. {
  722. _q_in_order = create_queue_impl(true, sycl::property::queue::in_order());
  723. _q_out_of_order = create_queue_impl(true);
  724. _saved_queue = &default_queue();
  725. }
  726. /// Caller should acquire resource \p m_mutex before calling this function.
  727. template <class... Properties>
  728. sycl::queue *create_queue_impl(bool enable_exception_handler,
  729. Properties... properties)
  730. {
  731. sycl::async_handler eh = {};
  732. if (enable_exception_handler)
  733. {
  734. eh = exception_handler;
  735. }
  736. _queues.push_back(std::make_shared<sycl::queue>(
  737. _ctx, *this, eh,
  738. sycl::property_list(
  739. #ifdef DPCT_PROFILING_ENABLED
  740. sycl::property::queue::enable_profiling(),
  741. #endif
  742. properties...)));
  743. return _queues.back().get();
  744. }
  745. template <class... Properties>
  746. sycl::queue *create_queue_impl(sycl::context context, sycl::device device,
  747. bool enable_exception_handler,
  748. Properties... properties) {
  749. sycl::async_handler eh = {};
  750. if (enable_exception_handler) {
  751. eh = exception_handler;
  752. }
  753. _queues.push_back(std::make_shared<sycl::queue>(
  754. context, device, eh,
  755. sycl::property_list(
  756. #ifdef DPCT_PROFILING_ENABLED
  757. sycl::property::queue::enable_profiling(),
  758. #endif
  759. properties...)));
  760. return _queues.back().get();
  761. }
  762. void get_version(int &major, int &minor) const
  763. {
  764. detail::get_version(*this, major, minor);
  765. }
  766. sycl::queue *_q_in_order, *_q_out_of_order;
  767. sycl::queue *_saved_queue;
  768. sycl::context _ctx;
  769. std::vector<std::shared_ptr<sycl::queue>> _queues;
  770. mutable mutex_type m_mutex;
  771. };
  772. /// device manager
  773. class dev_mgr
  774. {
  775. public:
  776. device_ext &current_device()
  777. {
  778. unsigned int dev_id = current_device_id();
  779. check_id(dev_id);
  780. return *_devs[dev_id];
  781. }
  782. device_ext &cpu_device() const
  783. {
  784. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  785. if (_cpu_device == -1)
  786. {
  787. throw std::runtime_error("no valid cpu device");
  788. }
  789. else
  790. {
  791. return *_devs[_cpu_device];
  792. }
  793. }
  794. device_ext &get_device(unsigned int id) const
  795. {
  796. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  797. check_id(id);
  798. return *_devs[id];
  799. }
  800. unsigned int current_device_id() const
  801. {
  802. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  803. auto it = _thread2dev_map.find(get_tid());
  804. if (it != _thread2dev_map.end())
  805. return it->second;
  806. return DEFAULT_DEVICE_ID;
  807. }
  808. /// Select device with a device ID.
  809. /// \param [in] id The id of the device which can
  810. /// be obtained through get_device_id(const sycl::device).
  811. void select_device(unsigned int id)
  812. {
  813. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  814. check_id(id);
  815. _thread2dev_map[get_tid()] = id;
  816. }
  817. unsigned int device_count() { return _devs.size(); }
  818. unsigned int get_device_id(const sycl::device &dev)
  819. {
  820. unsigned int id = 0;
  821. for (auto dev_item : _devs)
  822. {
  823. if (*dev_item == dev)
  824. {
  825. break;
  826. }
  827. id++;
  828. }
  829. return id;
  830. }
  831. template <class DeviceSelector>
  832. std::enable_if_t<
  833. std::is_invocable_r_v<int, DeviceSelector, const sycl::device &>>
  834. select_device(const DeviceSelector &selector = sycl::gpu_selector_v)
  835. {
  836. sycl::device selected_device = sycl::device(selector);
  837. unsigned int selected_device_id = get_device_id(selected_device);
  838. select_device(selected_device_id);
  839. }
  840. /// Returns the instance of device manager singleton.
  841. static dev_mgr &instance()
  842. {
  843. static dev_mgr d_m;
  844. return d_m;
  845. }
  846. dev_mgr(const dev_mgr &) = delete;
  847. dev_mgr &operator=(const dev_mgr &) = delete;
  848. dev_mgr(dev_mgr &&) = delete;
  849. dev_mgr &operator=(dev_mgr &&) = delete;
  850. private:
  851. mutable std::recursive_mutex m_mutex;
  852. dev_mgr()
  853. {
  854. sycl::device default_device =
  855. sycl::device(sycl::default_selector_v);
  856. _devs.push_back(std::make_shared<device_ext>(default_device));
  857. std::vector<sycl::device> sycl_all_devs =
  858. sycl::device::get_devices(sycl::info::device_type::all);
  859. // Collect other devices except for the default device.
  860. if (default_device.is_cpu())
  861. _cpu_device = 0;
  862. for (auto &dev : sycl_all_devs)
  863. {
  864. if (dev == default_device)
  865. {
  866. continue;
  867. }
  868. _devs.push_back(std::make_shared<device_ext>(dev));
  869. if (_cpu_device == -1 && dev.is_cpu())
  870. {
  871. _cpu_device = _devs.size() - 1;
  872. }
  873. }
  874. }
  875. void check_id(unsigned int id) const
  876. {
  877. if (id >= _devs.size())
  878. {
  879. throw std::runtime_error("invalid device id");
  880. }
  881. }
  882. std::vector<std::shared_ptr<device_ext>> _devs;
  883. /// DEFAULT_DEVICE_ID is used, if current_device_id() can not find current
  884. /// thread id in _thread2dev_map, which means default device should be used
  885. /// for the current thread.
  886. const unsigned int DEFAULT_DEVICE_ID = 0;
  887. /// thread-id to device-id map.
  888. std::map<unsigned int, unsigned int> _thread2dev_map;
  889. int _cpu_device = -1;
  890. };
  891. static inline sycl::queue &get_default_queue()
  892. {
  893. return dev_mgr::instance().current_device().default_queue();
  894. }
  895. namespace detail
  896. {
  897. enum class pointer_access_attribute
  898. {
  899. host_only = 0,
  900. device_only,
  901. host_device,
  902. end
  903. };
  904. static pointer_access_attribute get_pointer_attribute(sycl::queue &q,
  905. const void *ptr)
  906. {
  907. #ifdef DPCT_USM_LEVEL_NONE
  908. return mem_mgr::instance().is_device_ptr(ptr)
  909. ? pointer_access_attribute::device_only
  910. : pointer_access_attribute::host_only;
  911. #else
  912. switch (sycl::get_pointer_type(ptr, q.get_context()))
  913. {
  914. case sycl::usm::alloc::unknown:
  915. return pointer_access_attribute::host_only;
  916. case sycl::usm::alloc::device:
  917. return pointer_access_attribute::device_only;
  918. case sycl::usm::alloc::shared:
  919. case sycl::usm::alloc::host:
  920. return pointer_access_attribute::host_device;
  921. }
  922. #endif
  923. }
  924. template <typename ArgT>
  925. inline constexpr std::uint64_t get_type_combination_id(ArgT Val)
  926. {
  927. static_assert((unsigned char)library_data_t::library_data_t_size <=
  928. std::numeric_limits<unsigned char>::max() &&
  929. "library_data_t size exceeds limit.");
  930. static_assert(std::is_same_v<ArgT, library_data_t>, "Unsupported ArgT");
  931. return (std::uint64_t)Val;
  932. }
  933. template <typename FirstT, typename... RestT>
  934. inline constexpr std::uint64_t get_type_combination_id(FirstT FirstVal,
  935. RestT... RestVal)
  936. {
  937. static_assert((std::uint8_t)library_data_t::library_data_t_size <=
  938. std::numeric_limits<unsigned char>::max() &&
  939. "library_data_t size exceeds limit.");
  940. static_assert(sizeof...(RestT) <= 8 && "Too many parameters");
  941. static_assert(std::is_same_v<FirstT, library_data_t>, "Unsupported FirstT");
  942. return get_type_combination_id(RestVal...) << 8 | ((std::uint64_t)FirstVal);
  943. }
  944. class mem_mgr
  945. {
  946. mem_mgr()
  947. {
  948. // Reserved address space, no real memory allocation happens here.
  949. #if defined(__linux__)
  950. mapped_address_space =
  951. (byte_t *)mmap(nullptr, mapped_region_size, PROT_NONE,
  952. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  953. #elif defined(_WIN64)
  954. mapped_address_space = (byte_t *)VirtualAlloc(
  955. NULL, // NULL specified as the base address parameter
  956. mapped_region_size, // Size of allocation
  957. MEM_RESERVE, // Allocate reserved pages
  958. PAGE_NOACCESS); // Protection = no access
  959. #else
  960. #error "Only support Windows and Linux."
  961. #endif
  962. next_free = mapped_address_space;
  963. };
  964. public:
  965. using buffer_id_t = int;
  966. struct allocation
  967. {
  968. buffer_t buffer;
  969. byte_t *alloc_ptr;
  970. size_t size;
  971. };
  972. ~mem_mgr()
  973. {
  974. #if defined(__linux__)
  975. munmap(mapped_address_space, mapped_region_size);
  976. #elif defined(_WIN64)
  977. VirtualFree(mapped_address_space, 0, MEM_RELEASE);
  978. #else
  979. #error "Only support Windows and Linux."
  980. #endif
  981. };
  982. mem_mgr(const mem_mgr &) = delete;
  983. mem_mgr &operator=(const mem_mgr &) = delete;
  984. mem_mgr(mem_mgr &&) = delete;
  985. mem_mgr &operator=(mem_mgr &&) = delete;
  986. /// Allocate
  987. void *mem_alloc(size_t size)
  988. {
  989. if (!size)
  990. return nullptr;
  991. std::lock_guard<std::mutex> lock(m_mutex);
  992. if (next_free + size > mapped_address_space + mapped_region_size)
  993. {
  994. throw std::runtime_error("dpct_malloc: out of memory for virtual memory pool");
  995. }
  996. // Allocation
  997. sycl::range<1> r(size);
  998. buffer_t buf(r);
  999. allocation A{buf, next_free, size};
  1000. // Map allocation to device pointer
  1001. void *result = next_free;
  1002. m_map.emplace(next_free + size, A);
  1003. // Update pointer to the next free space.
  1004. next_free += (size + extra_padding + alignment - 1) & ~(alignment - 1);
  1005. return result;
  1006. }
  1007. /// Deallocate
  1008. void mem_free(const void *ptr)
  1009. {
  1010. if (!ptr)
  1011. return;
  1012. std::lock_guard<std::mutex> lock(m_mutex);
  1013. auto it = get_map_iterator(ptr);
  1014. m_map.erase(it);
  1015. }
  1016. /// map: device pointer -> allocation(buffer, alloc_ptr, size)
  1017. allocation translate_ptr(const void *ptr)
  1018. {
  1019. std::lock_guard<std::mutex> lock(m_mutex);
  1020. auto it = get_map_iterator(ptr);
  1021. return it->second;
  1022. }
  1023. /// Check if the pointer represents device pointer or not.
  1024. bool is_device_ptr(const void *ptr) const
  1025. {
  1026. std::lock_guard<std::mutex> lock(m_mutex);
  1027. return (mapped_address_space <= ptr) &&
  1028. (ptr < mapped_address_space + mapped_region_size);
  1029. }
  1030. /// Returns the instance of memory manager singleton.
  1031. static mem_mgr &instance()
  1032. {
  1033. static mem_mgr m;
  1034. return m;
  1035. }
  1036. private:
  1037. std::map<byte_t *, allocation> m_map;
  1038. mutable std::mutex m_mutex;
  1039. byte_t *mapped_address_space;
  1040. byte_t *next_free;
  1041. const size_t mapped_region_size = 128ull * 1024 * 1024 * 1024;
  1042. const size_t alignment = 256;
  1043. /// This padding may be defined to some positive value to debug
  1044. /// out of bound accesses.
  1045. const size_t extra_padding = 0;
  1046. std::map<byte_t *, allocation>::iterator get_map_iterator(const void *ptr)
  1047. {
  1048. auto it = m_map.upper_bound((byte_t *)ptr);
  1049. if (it == m_map.end())
  1050. {
  1051. // Not a virtual pointer.
  1052. throw std::runtime_error("can not get buffer from non-virtual pointer");
  1053. }
  1054. const allocation &alloc = it->second;
  1055. if (ptr < alloc.alloc_ptr)
  1056. {
  1057. // Out of bound.
  1058. // This may happen if there's a gap between allocations due to alignment
  1059. // or extra padding and pointer points to this gap.
  1060. throw std::runtime_error("invalid virtual pointer");
  1061. }
  1062. return it;
  1063. }
  1064. };
  1065. template <class T, memory_region Memory, size_t Dimension>
  1066. class accessor;
  1067. template <memory_region Memory, class T = byte_t>
  1068. class memory_traits
  1069. {
  1070. public:
  1071. static constexpr sycl::access::target target =
  1072. sycl::access::target::device;
  1073. static constexpr sycl::access_mode mode =
  1074. (Memory == constant) ? sycl::access_mode::read
  1075. : sycl::access_mode::read_write;
  1076. static constexpr size_t type_size = sizeof(T);
  1077. using element_t =
  1078. typename std::conditional<Memory == constant, const T, T>::type;
  1079. using value_t = typename std::remove_cv<T>::type;
  1080. template <size_t Dimension = 1>
  1081. using accessor_t = typename std::conditional<
  1082. Memory == local, sycl::local_accessor<value_t, Dimension>,
  1083. sycl::accessor<T, Dimension, mode, target>>::type;
  1084. using pointer_t = T *;
  1085. };
  1086. static inline void *dpct_malloc(size_t size, sycl::queue &q)
  1087. {
  1088. #ifdef DPCT_USM_LEVEL_NONE
  1089. return mem_mgr::instance().mem_alloc(size * sizeof(byte_t));
  1090. #else
  1091. return sycl::malloc_device(size, q.get_device(), q.get_context());
  1092. #endif // DPCT_USM_LEVEL_NONE
  1093. }
  1094. #define PITCH_DEFAULT_ALIGN(x) (((x) + 31) & ~(0x1F))
  1095. static inline void *dpct_malloc(size_t &pitch, size_t x, size_t y, size_t z,
  1096. sycl::queue &q)
  1097. {
  1098. pitch = PITCH_DEFAULT_ALIGN(x);
  1099. return dpct_malloc(pitch * y * z, q);
  1100. }
  1101. /**
  1102. * @brief Sets \p value to the first \p size elements starting from \p dev_ptr in \p q.
  1103. * @tparam valueT The type of the element to be set.
  1104. * @param [in] q The queue in which the operation is done.
  1105. * @param [in] dev_ptr Pointer to the virtual device memory address.
  1106. * @param [in] value The value to be set.
  1107. * @param [in] size Number of elements to be set to the value.
  1108. * @return An event representing the memset operation.
  1109. */
  1110. template <typename valueT>
  1111. static inline sycl::event dpct_memset(sycl::queue &q, void *dev_ptr,
  1112. valueT value, size_t size)
  1113. {
  1114. #ifdef DPCT_USM_LEVEL_NONE
  1115. auto &mm = mem_mgr::instance();
  1116. assert(mm.is_device_ptr(dev_ptr));
  1117. auto alloc = mm.translate_ptr(dev_ptr);
  1118. size_t offset = (valueT *)dev_ptr - (valueT *)alloc.alloc_ptr;
  1119. return q.submit([&](sycl::handler &cgh)
  1120. {
  1121. auto r = sycl::range<1>(size);
  1122. auto o = sycl::id<1>(offset);
  1123. auto new_buffer = alloc.buffer.reinterpret<valueT>(
  1124. sycl::range<1>(alloc.size / sizeof(valueT)));
  1125. sycl::accessor<valueT, 1, sycl::access_mode::write,
  1126. sycl::access::target::device>
  1127. acc(new_buffer, cgh, r, o);
  1128. cgh.fill(acc, value); });
  1129. #else
  1130. return q.fill(dev_ptr, value, size);
  1131. #endif // DPCT_USM_LEVEL_NONE
  1132. }
  1133. /**
  1134. * @brief Sets \p value to the 3D memory region pointed by \p data in \p q.
  1135. * @tparam valueT The type of the element to be set.
  1136. * @param [in] q The queue in which the operation is done.
  1137. * @param [in] data Pointer to the pitched device memory region.
  1138. * @param [in] value The value to be set.
  1139. * @param [in] size 3D memory region by number of elements.
  1140. * @return An event list representing the memset operations.
  1141. */
  1142. template <typename valueT>
  1143. static inline std::vector<sycl::event>
  1144. dpct_memset(sycl::queue &q, pitched_data data, valueT value,
  1145. sycl::range<3> size)
  1146. {
  1147. std::vector<sycl::event> event_list;
  1148. size_t slice = data.get_pitch() * data.get_y();
  1149. unsigned char *data_surface = (unsigned char *)data.get_data_ptr();
  1150. for (size_t z = 0; z < size.get(2); ++z)
  1151. {
  1152. unsigned char *data_ptr = data_surface;
  1153. for (size_t y = 0; y < size.get(1); ++y)
  1154. {
  1155. event_list.push_back(dpct_memset(q, data_ptr, value, size.get(0)));
  1156. data_ptr += data.get_pitch();
  1157. }
  1158. data_surface += slice;
  1159. }
  1160. return event_list;
  1161. }
  1162. /**
  1163. * @brief Sets \p val to the pitched 2D memory region pointed by \p ptr in \p q.
  1164. * @tparam valueT The type of the element to be set.
  1165. * @param [in] q The queue in which the operation is done.
  1166. * @param [in] ptr Pointer to the virtual device memory.
  1167. * @param [in] pitch The pitch size by number of elements, including padding.
  1168. * @param [in] val The value to be set.
  1169. * @param [in] x The width of memory region by number of elements.
  1170. * @param [in] y The height of memory region by number of elements.
  1171. * @return An event list representing the memset operations.
  1172. */
  1173. template <typename valueT>
  1174. static inline std::vector<sycl::event>
  1175. dpct_memset(sycl::queue &q, void *ptr, size_t pitch, valueT val, size_t x,
  1176. size_t y)
  1177. {
  1178. return dpct_memset(q, pitched_data(ptr, pitch, x, 1), val,
  1179. sycl::range<3>(x, y, 1));
  1180. }
  1181. static memcpy_direction deduce_memcpy_direction(sycl::queue &q, void *to_ptr,
  1182. const void *from_ptr,
  1183. memcpy_direction dir)
  1184. {
  1185. switch (dir)
  1186. {
  1187. case memcpy_direction::host_to_host:
  1188. case memcpy_direction::host_to_device:
  1189. case memcpy_direction::device_to_host:
  1190. case memcpy_direction::device_to_device:
  1191. return dir;
  1192. case memcpy_direction::automatic:
  1193. {
  1194. // table[to_attribute][from_attribute]
  1195. static const memcpy_direction
  1196. direction_table[static_cast<unsigned>(pointer_access_attribute::end)]
  1197. [static_cast<unsigned>(pointer_access_attribute::end)] =
  1198. {{memcpy_direction::host_to_host,
  1199. memcpy_direction::device_to_host,
  1200. memcpy_direction::host_to_host},
  1201. {memcpy_direction::host_to_device,
  1202. memcpy_direction::device_to_device,
  1203. memcpy_direction::device_to_device},
  1204. {memcpy_direction::host_to_host,
  1205. memcpy_direction::device_to_device,
  1206. memcpy_direction::device_to_device}};
  1207. return direction_table[static_cast<unsigned>(get_pointer_attribute(
  1208. q, to_ptr))][static_cast<unsigned>(get_pointer_attribute(q, from_ptr))];
  1209. }
  1210. default:
  1211. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1212. }
  1213. }
  1214. static sycl::event
  1215. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1216. memcpy_direction direction,
  1217. const std::vector<sycl::event> &dep_events = {})
  1218. {
  1219. if (!size)
  1220. return sycl::event{};
  1221. #ifdef DPCT_USM_LEVEL_NONE
  1222. auto &mm = mem_mgr::instance();
  1223. auto real_direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1224. switch (real_direction)
  1225. {
  1226. case host_to_host:
  1227. return q.submit([&](sycl::handler &cgh)
  1228. {
  1229. cgh.depends_on(dep_events);
  1230. cgh.host_task([=] { std::memcpy(to_ptr, from_ptr, size); }); });
  1231. case host_to_device:
  1232. {
  1233. auto alloc = mm.translate_ptr(to_ptr);
  1234. size_t offset = (byte_t *)to_ptr - alloc.alloc_ptr;
  1235. return q.submit([&](sycl::handler &cgh)
  1236. {
  1237. cgh.depends_on(dep_events);
  1238. auto r = sycl::range<1>(size);
  1239. auto o = sycl::id<1>(offset);
  1240. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1241. sycl::access::target::device>
  1242. acc(alloc.buffer, cgh, r, o);
  1243. cgh.copy(from_ptr, acc); });
  1244. }
  1245. case device_to_host:
  1246. {
  1247. auto alloc = mm.translate_ptr(from_ptr);
  1248. size_t offset = (byte_t *)from_ptr - alloc.alloc_ptr;
  1249. return q.submit([&](sycl::handler &cgh)
  1250. {
  1251. cgh.depends_on(dep_events);
  1252. auto r = sycl::range<1>(size);
  1253. auto o = sycl::id<1>(offset);
  1254. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1255. sycl::access::target::device>
  1256. acc(alloc.buffer, cgh, r, o);
  1257. cgh.copy(acc, to_ptr); });
  1258. }
  1259. case device_to_device:
  1260. {
  1261. auto to_alloc = mm.translate_ptr(to_ptr);
  1262. auto from_alloc = mm.translate_ptr(from_ptr);
  1263. size_t to_offset = (byte_t *)to_ptr - to_alloc.alloc_ptr;
  1264. size_t from_offset = (byte_t *)from_ptr - from_alloc.alloc_ptr;
  1265. return q.submit([&](sycl::handler &cgh)
  1266. {
  1267. cgh.depends_on(dep_events);
  1268. auto r = sycl::range<1>(size);
  1269. auto to_o = sycl::id<1>(to_offset);
  1270. auto from_o = sycl::id<1>(from_offset);
  1271. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1272. sycl::access::target::device>
  1273. to_acc(to_alloc.buffer, cgh, r, to_o);
  1274. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1275. sycl::access::target::device>
  1276. from_acc(from_alloc.buffer, cgh, r, from_o);
  1277. cgh.copy(from_acc, to_acc); });
  1278. }
  1279. default:
  1280. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1281. }
  1282. #else
  1283. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  1284. GGML_UNUSED(direction);
  1285. #endif // DPCT_USM_LEVEL_NONE
  1286. }
  1287. // Get actual copy range and make sure it will not exceed range.
  1288. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  1289. size_t pitch)
  1290. {
  1291. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  1292. }
  1293. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  1294. size_t pitch)
  1295. {
  1296. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  1297. }
  1298. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  1299. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  1300. static inline std::vector<sycl::event>
  1301. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1302. sycl::range<3> to_range, sycl::range<3> from_range,
  1303. sycl::id<3> to_id, sycl::id<3> from_id,
  1304. sycl::range<3> size, memcpy_direction direction,
  1305. const std::vector<sycl::event> &dep_events = {})
  1306. {
  1307. // RAII for host pointer
  1308. class host_buffer
  1309. {
  1310. void *_buf;
  1311. size_t _size;
  1312. sycl::queue &_q;
  1313. const std::vector<sycl::event> &_deps; // free operation depends
  1314. public:
  1315. host_buffer(size_t size, sycl::queue &q,
  1316. const std::vector<sycl::event> &deps)
  1317. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  1318. void *get_ptr() const { return _buf; }
  1319. size_t get_size() const { return _size; }
  1320. ~host_buffer()
  1321. {
  1322. if (_buf)
  1323. {
  1324. _q.submit([&](sycl::handler &cgh)
  1325. {
  1326. cgh.depends_on(_deps);
  1327. cgh.host_task([buf = _buf] { std::free(buf); }); });
  1328. }
  1329. }
  1330. };
  1331. std::vector<sycl::event> event_list;
  1332. size_t to_slice = to_range.get(1) * to_range.get(0),
  1333. from_slice = from_range.get(1) * from_range.get(0);
  1334. unsigned char *to_surface =
  1335. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  1336. const unsigned char *from_surface =
  1337. (const unsigned char *)from_ptr +
  1338. get_offset(from_id, from_slice, from_range.get(0));
  1339. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  1340. {
  1341. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  1342. direction, dep_events)};
  1343. }
  1344. direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1345. size_t size_slice = size.get(1) * size.get(0);
  1346. switch (direction)
  1347. {
  1348. case host_to_host:
  1349. for (size_t z = 0; z < size.get(2); ++z)
  1350. {
  1351. unsigned char *to_ptr = to_surface;
  1352. const unsigned char *from_ptr = from_surface;
  1353. if (to_range.get(0) == from_range.get(0) &&
  1354. to_range.get(0) == size.get(0))
  1355. {
  1356. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  1357. direction, dep_events));
  1358. }
  1359. else
  1360. {
  1361. for (size_t y = 0; y < size.get(1); ++y)
  1362. {
  1363. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  1364. direction, dep_events));
  1365. to_ptr += to_range.get(0);
  1366. from_ptr += from_range.get(0);
  1367. }
  1368. }
  1369. to_surface += to_slice;
  1370. from_surface += from_slice;
  1371. }
  1372. break;
  1373. case host_to_device:
  1374. {
  1375. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  1376. event_list);
  1377. std::vector<sycl::event> host_events;
  1378. if (to_slice == size_slice)
  1379. {
  1380. // Copy host data to a temp host buffer with the shape of target.
  1381. host_events =
  1382. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  1383. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  1384. host_to_host, dep_events);
  1385. }
  1386. else
  1387. {
  1388. // Copy host data to a temp host buffer with the shape of target.
  1389. host_events = dpct_memcpy(
  1390. q, buf.get_ptr(), from_surface, to_range, from_range,
  1391. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  1392. // If has padding data, not sure whether it is useless. So fill temp
  1393. // buffer with it.
  1394. std::vector<sycl::event>{
  1395. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  1396. device_to_host, dep_events)});
  1397. }
  1398. // Copy from temp host buffer to device with only one submit.
  1399. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  1400. buf.get_size(), host_to_device,
  1401. host_events));
  1402. break;
  1403. }
  1404. case device_to_host:
  1405. {
  1406. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  1407. event_list);
  1408. // Copy from host temp buffer to host target with reshaping.
  1409. event_list = dpct_memcpy(
  1410. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  1411. sycl::id<3>(0, 0, 0), size, host_to_host,
  1412. // Copy from device to temp host buffer with only one submit.
  1413. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  1414. buf.get_size(),
  1415. device_to_host, dep_events)});
  1416. break;
  1417. }
  1418. case device_to_device:
  1419. #ifdef DPCT_USM_LEVEL_NONE
  1420. {
  1421. auto &mm = mem_mgr::instance();
  1422. auto to_alloc = mm.translate_ptr(to_surface);
  1423. auto from_alloc = mm.translate_ptr(from_surface);
  1424. size_t to_offset = (byte_t *)to_surface - to_alloc.alloc_ptr;
  1425. size_t from_offset = (byte_t *)from_surface - from_alloc.alloc_ptr;
  1426. event_list.push_back(q.submit([&](sycl::handler &cgh)
  1427. {
  1428. cgh.depends_on(dep_events);
  1429. auto to_o = sycl::id<1>(to_offset);
  1430. auto from_o = sycl::id<1>(from_offset);
  1431. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  1432. sycl::access::target::device>
  1433. to_acc(to_alloc.buffer, cgh,
  1434. get_copy_range(size, to_slice, to_range.get(0)), to_o);
  1435. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  1436. sycl::access::target::device>
  1437. from_acc(from_alloc.buffer, cgh,
  1438. get_copy_range(size, from_slice, from_range.get(0)), from_o);
  1439. cgh.parallel_for<class dpct_memcpy_3d_detail_usmnone>(
  1440. size,
  1441. [=](sycl::id<3> id) {
  1442. to_acc[get_offset(id, to_slice, to_range.get(0))] =
  1443. from_acc[get_offset(id, from_slice, from_range.get(0))];
  1444. }); }));
  1445. }
  1446. #else
  1447. event_list.push_back(q.submit([&](sycl::handler &cgh)
  1448. {
  1449. cgh.depends_on(dep_events);
  1450. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  1451. size,
  1452. [=](sycl::id<3> id) {
  1453. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  1454. from_surface[get_offset(id, from_slice, from_range.get(0))];
  1455. }); }));
  1456. #endif
  1457. break;
  1458. default:
  1459. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1460. }
  1461. return event_list;
  1462. }
  1463. /// memcpy 2D/3D matrix specified by pitched_data.
  1464. static inline std::vector<sycl::event>
  1465. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  1466. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  1467. memcpy_direction direction = automatic)
  1468. {
  1469. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  1470. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  1471. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  1472. size, direction);
  1473. }
  1474. /// memcpy 2D matrix with pitch.
  1475. static inline std::vector<sycl::event>
  1476. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1477. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  1478. memcpy_direction direction = automatic)
  1479. {
  1480. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  1481. sycl::range<3>(from_pitch, y, 1),
  1482. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  1483. sycl::range<3>(x, y, 1), direction);
  1484. }
  1485. namespace deprecated
  1486. {
  1487. template <typename T, sycl::usm::alloc AllocKind>
  1488. class usm_allocator
  1489. {
  1490. private:
  1491. using Alloc = sycl::usm_allocator<T, AllocKind>;
  1492. Alloc _impl;
  1493. public:
  1494. using value_type = typename std::allocator_traits<Alloc>::value_type;
  1495. using pointer = typename std::allocator_traits<Alloc>::pointer;
  1496. using const_pointer = typename std::allocator_traits<Alloc>::const_pointer;
  1497. using void_pointer = typename std::allocator_traits<Alloc>::void_pointer;
  1498. using const_void_pointer =
  1499. typename std::allocator_traits<Alloc>::const_void_pointer;
  1500. using reference = typename std::allocator_traits<Alloc>::value_type &;
  1501. using const_reference =
  1502. const typename std::allocator_traits<Alloc>::value_type &;
  1503. using difference_type =
  1504. typename std::allocator_traits<Alloc>::difference_type;
  1505. using size_type = typename std::allocator_traits<Alloc>::size_type;
  1506. using propagate_on_container_copy_assignment = typename std::allocator_traits<
  1507. Alloc>::propagate_on_container_copy_assignment;
  1508. using propagate_on_container_move_assignment = typename std::allocator_traits<
  1509. Alloc>::propagate_on_container_move_assignment;
  1510. using propagate_on_container_swap =
  1511. typename std::allocator_traits<Alloc>::propagate_on_container_swap;
  1512. using is_always_equal =
  1513. typename std::allocator_traits<Alloc>::is_always_equal;
  1514. template <typename U>
  1515. struct rebind
  1516. {
  1517. typedef usm_allocator<U, AllocKind> other;
  1518. };
  1519. usm_allocator() : _impl(dpct::get_default_queue()) {}
  1520. ~usm_allocator() {}
  1521. usm_allocator(const usm_allocator &other) : _impl(other._impl) {}
  1522. usm_allocator(usm_allocator &&other) : _impl(std::move(other._impl)) {}
  1523. pointer address(reference r) { return &r; }
  1524. const_pointer address(const_reference r) { return &r; }
  1525. pointer allocate(size_type cnt, const_void_pointer hint = nullptr)
  1526. {
  1527. return std::allocator_traits<Alloc>::allocate(_impl, cnt, hint);
  1528. }
  1529. void deallocate(pointer p, size_type cnt)
  1530. {
  1531. std::allocator_traits<Alloc>::deallocate(_impl, p, cnt);
  1532. }
  1533. size_type max_size() const
  1534. {
  1535. return std::allocator_traits<Alloc>::max_size(_impl);
  1536. }
  1537. bool operator==(const usm_allocator &other) const { return _impl == other._impl; }
  1538. bool operator!=(const usm_allocator &other) const { return _impl != other._impl; }
  1539. };
  1540. } // namespace deprecated
  1541. inline void dpct_free(void *ptr,
  1542. const sycl::queue &q)
  1543. {
  1544. if (ptr)
  1545. {
  1546. #ifdef DPCT_USM_LEVEL_NONE
  1547. detail::mem_mgr::instance().mem_free(ptr);
  1548. #else
  1549. sycl::free(ptr, q.get_context());
  1550. #endif // DPCT_USM_LEVEL_NONE
  1551. }
  1552. }
  1553. template <typename T>
  1554. inline auto get_memory(const void *x)
  1555. {
  1556. T *new_x = reinterpret_cast<T *>(const_cast<void *>(x));
  1557. #ifdef DPCT_USM_LEVEL_NONE
  1558. return dpct::get_buffer<std::remove_cv_t<T>>(new_x);
  1559. #else
  1560. return new_x;
  1561. #endif
  1562. }
  1563. template <typename T>
  1564. inline typename DataType<T>::T2 get_value(const T *s, sycl::queue &q)
  1565. {
  1566. using Ty = typename DataType<T>::T2;
  1567. Ty s_h;
  1568. if (get_pointer_attribute(q, s) == pointer_access_attribute::device_only)
  1569. detail::dpct_memcpy(q, (void *)&s_h, (const void *)s, sizeof(T), device_to_host)
  1570. .wait();
  1571. else
  1572. s_h = *reinterpret_cast<const Ty *>(s);
  1573. return s_h;
  1574. }
  1575. } // namespace detail
  1576. template <typename T>
  1577. inline auto get_value(const T *s, sycl::queue &q)
  1578. {
  1579. return detail::get_value(s, q);
  1580. }
  1581. namespace detail
  1582. {
  1583. template <class Ta, class Tb, class Tc, class Ts>
  1584. inline void gemm_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1585. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1586. const void *alpha, const void *a, int lda, const void *b,
  1587. int ldb, const void *beta, void *c, int ldc)
  1588. {
  1589. #ifndef __INTEL_MKL__
  1590. GGML_UNUSED(q);
  1591. GGML_UNUSED(a_trans);
  1592. GGML_UNUSED(b_trans);
  1593. GGML_UNUSED(m);
  1594. GGML_UNUSED(n);
  1595. GGML_UNUSED(k);
  1596. GGML_UNUSED(alpha);
  1597. GGML_UNUSED(a);
  1598. GGML_UNUSED(lda);
  1599. GGML_UNUSED(b);
  1600. GGML_UNUSED(ldb);
  1601. GGML_UNUSED(beta);
  1602. GGML_UNUSED(c);
  1603. GGML_UNUSED(ldc);
  1604. throw std::runtime_error("The oneAPI Math Kernel Library (oneMKL) Interfaces "
  1605. "Project does not support this API.");
  1606. #else
  1607. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1608. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1609. auto data_a = get_memory<const Ta>(a);
  1610. auto data_b = get_memory<const Tb>(b);
  1611. auto data_c = get_memory<Tc>(c);
  1612. oneapi::mkl::blas::column_major::gemm(
  1613. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1614. data_b, ldb, beta_value, data_c, ldc);
  1615. #endif
  1616. }
  1617. template <typename VecT, class BinaryOperation, class = void>
  1618. class vectorized_binary
  1619. {
  1620. public:
  1621. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1622. {
  1623. VecT v4;
  1624. for (size_t i = 0; i < v4.size(); ++i)
  1625. {
  1626. v4[i] = binary_op(a[i], b[i]);
  1627. }
  1628. return v4;
  1629. }
  1630. };
  1631. template <typename VecT, class BinaryOperation>
  1632. class vectorized_binary<
  1633. VecT, BinaryOperation,
  1634. std::void_t<std::invoke_result_t<BinaryOperation, VecT, VecT>>>
  1635. {
  1636. public:
  1637. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1638. {
  1639. return binary_op(a, b).template as<VecT>();
  1640. }
  1641. };
  1642. template <class Ta, class Tb, class Tc, class Ts>
  1643. inline void gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1644. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1645. const void *alpha, const void **a, int lda,
  1646. const void **b, int ldb, const void *beta, void **c,
  1647. int ldc, int batch_size)
  1648. {
  1649. struct matrix_info_t
  1650. {
  1651. oneapi::mkl::transpose transpose_info[2];
  1652. Ts value_info[2];
  1653. std::int64_t size_info[3];
  1654. std::int64_t ld_info[3];
  1655. std::int64_t groupsize_info;
  1656. };
  1657. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1658. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1659. matrix_info_t *matrix_info =
  1660. (matrix_info_t *)std::malloc(sizeof(matrix_info_t));
  1661. matrix_info->transpose_info[0] = a_trans;
  1662. matrix_info->transpose_info[1] = b_trans;
  1663. matrix_info->value_info[0] = alpha_value;
  1664. matrix_info->value_info[1] = beta_value;
  1665. matrix_info->size_info[0] = m;
  1666. matrix_info->size_info[1] = n;
  1667. matrix_info->size_info[2] = k;
  1668. matrix_info->ld_info[0] = lda;
  1669. matrix_info->ld_info[1] = ldb;
  1670. matrix_info->ld_info[2] = ldc;
  1671. matrix_info->groupsize_info = batch_size;
  1672. sycl::event e = oneapi::mkl::blas::column_major::gemm_batch(
  1673. q, matrix_info->transpose_info, matrix_info->transpose_info + 1,
  1674. matrix_info->size_info, matrix_info->size_info + 1,
  1675. matrix_info->size_info + 2, matrix_info->value_info,
  1676. reinterpret_cast<const Ta **>(a), matrix_info->ld_info,
  1677. reinterpret_cast<const Tb **>(b), matrix_info->ld_info + 1,
  1678. matrix_info->value_info + 1, reinterpret_cast<Tc **>(c),
  1679. matrix_info->ld_info + 2, 1, &(matrix_info->groupsize_info));
  1680. q.submit([&](sycl::handler &cgh)
  1681. {
  1682. cgh.depends_on(e);
  1683. cgh.host_task([=] { std::free(matrix_info); }); });
  1684. }
  1685. template <class Ta, class Tb, class Tc, class Ts>
  1686. inline void
  1687. gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1688. oneapi::mkl::transpose b_trans, int m, int n,
  1689. int k, const void *alpha, const void *a, int lda,
  1690. long long int stride_a, const void *b, int ldb,
  1691. long long int stride_b, const void *beta, void *c,
  1692. int ldc, long long int stride_c, int batch_size)
  1693. {
  1694. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1695. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1696. auto data_a = get_memory<const Ta>(a);
  1697. auto data_b = get_memory<const Tb>(b);
  1698. auto data_c = get_memory<Tc>(c);
  1699. oneapi::mkl::blas::column_major::gemm_batch(
  1700. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1701. stride_a, data_b, ldb, stride_b, beta_value,
  1702. data_c, ldc, stride_c, batch_size);
  1703. }
  1704. } // namespace detail
  1705. template <typename VecT, class BinaryOperation>
  1706. inline unsigned vectorized_binary(unsigned a, unsigned b,
  1707. const BinaryOperation binary_op)
  1708. {
  1709. sycl::vec<unsigned, 1> v0{a}, v1{b};
  1710. auto v2 = v0.as<VecT>();
  1711. auto v3 = v1.as<VecT>();
  1712. auto v4 =
  1713. detail::vectorized_binary<VecT, BinaryOperation>()(v2, v3, binary_op);
  1714. v0 = v4.template as<sycl::vec<unsigned, 1>>();
  1715. return v0;
  1716. }
  1717. static void async_dpct_memcpy(void *to_ptr, const void *from_ptr, size_t size,
  1718. memcpy_direction direction = automatic,
  1719. sycl::queue &q = dpct::get_default_queue())
  1720. {
  1721. detail::dpct_memcpy(q, to_ptr, from_ptr, size, direction);
  1722. }
  1723. static inline unsigned int select_device(unsigned int id)
  1724. {
  1725. dev_mgr::instance().select_device(id);
  1726. return id;
  1727. }
  1728. template <typename T>
  1729. T permute_sub_group_by_xor(sycl::sub_group g, T x, unsigned int mask,
  1730. unsigned int logical_sub_group_size = 32)
  1731. {
  1732. unsigned int id = g.get_local_linear_id();
  1733. unsigned int start_index =
  1734. id / logical_sub_group_size * logical_sub_group_size;
  1735. unsigned int target_offset = (id % logical_sub_group_size) ^ mask;
  1736. return sycl::select_from_group(g, x,
  1737. target_offset < logical_sub_group_size
  1738. ? start_index + target_offset
  1739. : id);
  1740. }
  1741. template <typename T>
  1742. sycl::vec<T, 4> extract_and_sign_or_zero_extend4(T val)
  1743. {
  1744. return sycl::vec<T, 1>(val)
  1745. .template as<sycl::vec<
  1746. std::conditional_t<std::is_signed_v<T>, int8_t, uint8_t>, 4>>()
  1747. .template convert<T>();
  1748. }
  1749. template <typename T1, typename T2>
  1750. using dot_product_acc_t =
  1751. std::conditional_t<std::is_unsigned_v<T1> && std::is_unsigned_v<T2>,
  1752. uint32_t, int32_t>;
  1753. template <typename T1, typename T2, typename T3>
  1754. inline auto dp4a(T1 a, T2 b, T3 c)
  1755. {
  1756. dot_product_acc_t<T1, T2> res = c;
  1757. auto va = extract_and_sign_or_zero_extend4(a);
  1758. auto vb = extract_and_sign_or_zero_extend4(b);
  1759. res += va[0] * vb[0];
  1760. res += va[1] * vb[1];
  1761. res += va[2] * vb[2];
  1762. res += va[3] * vb[3];
  1763. return res;
  1764. }
  1765. struct sub_sat
  1766. {
  1767. template <typename T>
  1768. auto operator()(const T x, const T y) const
  1769. {
  1770. return sycl::sub_sat(x, y);
  1771. }
  1772. };
  1773. template <typename S, typename T>
  1774. inline T vectorized_min(T a, T b)
  1775. {
  1776. sycl::vec<T, 1> v0{a}, v1{b};
  1777. auto v2 = v0.template as<S>();
  1778. auto v3 = v1.template as<S>();
  1779. auto v4 = sycl::min(v2, v3);
  1780. v0 = v4.template as<sycl::vec<T, 1>>();
  1781. return v0;
  1782. }
  1783. inline float pow(const float a, const int b) { return sycl::pown(a, b); }
  1784. inline double pow(const double a, const int b) { return sycl::pown(a, b); }
  1785. inline float pow(const float a, const float b) { return sycl::pow(a, b); }
  1786. inline double pow(const double a, const double b) { return sycl::pow(a, b); }
  1787. template <typename T, typename U>
  1788. inline typename std::enable_if_t<std::is_floating_point_v<T>, T>
  1789. pow(const T a, const U b)
  1790. {
  1791. return sycl::pow(a, static_cast<T>(b));
  1792. }
  1793. template <typename T, typename U>
  1794. inline typename std::enable_if_t<!std::is_floating_point_v<T>, double>
  1795. pow(const T a, const U b)
  1796. {
  1797. return sycl::pow(static_cast<double>(a), static_cast<double>(b));
  1798. }
  1799. inline double min(const double a, const float b)
  1800. {
  1801. return sycl::fmin(a, static_cast<double>(b));
  1802. }
  1803. inline double min(const float a, const double b)
  1804. {
  1805. return sycl::fmin(static_cast<double>(a), b);
  1806. }
  1807. inline float min(const float a, const float b) { return sycl::fmin(a, b); }
  1808. inline double min(const double a, const double b) { return sycl::fmin(a, b); }
  1809. inline std::uint32_t min(const std::uint32_t a, const std::int32_t b)
  1810. {
  1811. return sycl::min(a, static_cast<std::uint32_t>(b));
  1812. }
  1813. inline std::uint32_t min(const std::int32_t a, const std::uint32_t b)
  1814. {
  1815. return sycl::min(static_cast<std::uint32_t>(a), b);
  1816. }
  1817. inline std::int32_t min(const std::int32_t a, const std::int32_t b)
  1818. {
  1819. return sycl::min(a, b);
  1820. }
  1821. inline std::uint32_t min(const std::uint32_t a, const std::uint32_t b)
  1822. {
  1823. return sycl::min(a, b);
  1824. }
  1825. inline std::uint64_t min(const std::uint64_t a, const std::int64_t b)
  1826. {
  1827. return sycl::min(a, static_cast<std::uint64_t>(b));
  1828. }
  1829. inline std::uint64_t min(const std::int64_t a, const std::uint64_t b)
  1830. {
  1831. return sycl::min(static_cast<std::uint64_t>(a), b);
  1832. }
  1833. inline std::int64_t min(const std::int64_t a, const std::int64_t b)
  1834. {
  1835. return sycl::min(a, b);
  1836. }
  1837. inline std::uint64_t min(const std::uint64_t a, const std::uint64_t b)
  1838. {
  1839. return sycl::min(a, b);
  1840. }
  1841. inline std::uint64_t min(const std::uint64_t a, const std::int32_t b)
  1842. {
  1843. return sycl::min(a, static_cast<std::uint64_t>(b));
  1844. }
  1845. inline std::uint64_t min(const std::int32_t a, const std::uint64_t b)
  1846. {
  1847. return sycl::min(static_cast<std::uint64_t>(a), b);
  1848. }
  1849. inline std::uint64_t min(const std::uint64_t a, const std::uint32_t b)
  1850. {
  1851. return sycl::min(a, static_cast<std::uint64_t>(b));
  1852. }
  1853. inline std::uint64_t min(const std::uint32_t a, const std::uint64_t b)
  1854. {
  1855. return sycl::min(static_cast<std::uint64_t>(a), b);
  1856. }
  1857. // max function overloads.
  1858. // For floating-point types, `float` or `double` arguments are acceptable.
  1859. // For integer types, `std::uint32_t`, `std::int32_t`, `std::uint64_t` or
  1860. // `std::int64_t` type arguments are acceptable.
  1861. inline double max(const double a, const float b)
  1862. {
  1863. return sycl::fmax(a, static_cast<double>(b));
  1864. }
  1865. inline double max(const float a, const double b)
  1866. {
  1867. return sycl::fmax(static_cast<double>(a), b);
  1868. }
  1869. inline float max(const float a, const float b) { return sycl::fmax(a, b); }
  1870. inline double max(const double a, const double b) { return sycl::fmax(a, b); }
  1871. inline std::uint32_t max(const std::uint32_t a, const std::int32_t b)
  1872. {
  1873. return sycl::max(a, static_cast<std::uint32_t>(b));
  1874. }
  1875. inline std::uint32_t max(const std::int32_t a, const std::uint32_t b)
  1876. {
  1877. return sycl::max(static_cast<std::uint32_t>(a), b);
  1878. }
  1879. inline std::int32_t max(const std::int32_t a, const std::int32_t b)
  1880. {
  1881. return sycl::max(a, b);
  1882. }
  1883. inline std::uint32_t max(const std::uint32_t a, const std::uint32_t b)
  1884. {
  1885. return sycl::max(a, b);
  1886. }
  1887. inline std::uint64_t max(const std::uint64_t a, const std::int64_t b)
  1888. {
  1889. return sycl::max(a, static_cast<std::uint64_t>(b));
  1890. }
  1891. inline std::uint64_t max(const std::int64_t a, const std::uint64_t b)
  1892. {
  1893. return sycl::max(static_cast<std::uint64_t>(a), b);
  1894. }
  1895. inline std::int64_t max(const std::int64_t a, const std::int64_t b)
  1896. {
  1897. return sycl::max(a, b);
  1898. }
  1899. inline std::uint64_t max(const std::uint64_t a, const std::uint64_t b)
  1900. {
  1901. return sycl::max(a, b);
  1902. }
  1903. inline std::uint64_t max(const std::uint64_t a, const std::int32_t b)
  1904. {
  1905. return sycl::max(a, static_cast<std::uint64_t>(b));
  1906. }
  1907. inline std::uint64_t max(const std::int32_t a, const std::uint64_t b)
  1908. {
  1909. return sycl::max(static_cast<std::uint64_t>(a), b);
  1910. }
  1911. inline std::uint64_t max(const std::uint64_t a, const std::uint32_t b)
  1912. {
  1913. return sycl::max(a, static_cast<std::uint64_t>(b));
  1914. }
  1915. inline std::uint64_t max(const std::uint32_t a, const std::uint64_t b)
  1916. {
  1917. return sycl::max(static_cast<std::uint64_t>(a), b);
  1918. }
  1919. inline void
  1920. has_capability_or_fail(const sycl::device &dev,
  1921. const std::initializer_list<sycl::aspect> &props)
  1922. {
  1923. for (const auto &it : props)
  1924. {
  1925. if (dev.has(it))
  1926. continue;
  1927. switch (it)
  1928. {
  1929. case sycl::aspect::fp64:
  1930. throw std::runtime_error("'double' is not supported in '" +
  1931. dev.get_info<sycl::info::device::name>() +
  1932. "' device");
  1933. break;
  1934. case sycl::aspect::fp16:
  1935. throw std::runtime_error("'half' is not supported in '" +
  1936. dev.get_info<sycl::info::device::name>() +
  1937. "' device");
  1938. break;
  1939. default:
  1940. #define __SYCL_ASPECT(ASPECT, ID) \
  1941. case sycl::aspect::ASPECT: \
  1942. return #ASPECT;
  1943. #define __SYCL_ASPECT_DEPRECATED(ASPECT, ID, MESSAGE) __SYCL_ASPECT(ASPECT, ID)
  1944. #define __SYCL_ASPECT_DEPRECATED_ALIAS(ASPECT, ID, MESSAGE)
  1945. auto getAspectNameStr = [](sycl::aspect AspectNum) -> std::string
  1946. {
  1947. switch (AspectNum)
  1948. {
  1949. #include <sycl/info/aspects.def>
  1950. #include <sycl/info/aspects_deprecated.def>
  1951. default:
  1952. return "unknown aspect";
  1953. }
  1954. };
  1955. #undef __SYCL_ASPECT_DEPRECATED_ALIAS
  1956. #undef __SYCL_ASPECT_DEPRECATED
  1957. #undef __SYCL_ASPECT
  1958. throw std::runtime_error(
  1959. "'" + getAspectNameStr(it) + "' is not supported in '" +
  1960. dev.get_info<sycl::info::device::name>() + "' device");
  1961. }
  1962. break;
  1963. }
  1964. }
  1965. static inline unsigned int get_current_device_id()
  1966. {
  1967. return dev_mgr::instance().current_device_id();
  1968. }
  1969. static inline device_ext &get_current_device()
  1970. {
  1971. return dev_mgr::instance().current_device();
  1972. }
  1973. static inline sycl::queue &get_in_order_queue()
  1974. {
  1975. return dev_mgr::instance().current_device().in_order_queue();
  1976. }
  1977. static sycl::event
  1978. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1979. memcpy_direction direction,
  1980. const std::vector<sycl::event> &dep_events = {})
  1981. {
  1982. if (!size)
  1983. return sycl::event{};
  1984. #ifdef DPCT_USM_LEVEL_NONE
  1985. auto &mm = mem_mgr::instance();
  1986. auto real_direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1987. switch (real_direction)
  1988. {
  1989. case host_to_host:
  1990. return q.submit([&](sycl::handler &cgh)
  1991. {
  1992. cgh.depends_on(dep_events);
  1993. cgh.host_task([=] { std::memcpy(to_ptr, from_ptr, size); }); });
  1994. case host_to_device:
  1995. {
  1996. auto alloc = mm.translate_ptr(to_ptr);
  1997. size_t offset = (byte_t *)to_ptr - alloc.alloc_ptr;
  1998. return q.submit([&](sycl::handler &cgh)
  1999. {
  2000. cgh.depends_on(dep_events);
  2001. auto r = sycl::range<1>(size);
  2002. auto o = sycl::id<1>(offset);
  2003. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  2004. sycl::access::target::device>
  2005. acc(alloc.buffer, cgh, r, o);
  2006. cgh.copy(from_ptr, acc); });
  2007. }
  2008. case device_to_host:
  2009. {
  2010. auto alloc = mm.translate_ptr(from_ptr);
  2011. size_t offset = (byte_t *)from_ptr - alloc.alloc_ptr;
  2012. return q.submit([&](sycl::handler &cgh)
  2013. {
  2014. cgh.depends_on(dep_events);
  2015. auto r = sycl::range<1>(size);
  2016. auto o = sycl::id<1>(offset);
  2017. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  2018. sycl::access::target::device>
  2019. acc(alloc.buffer, cgh, r, o);
  2020. cgh.copy(acc, to_ptr); });
  2021. }
  2022. case device_to_device:
  2023. {
  2024. auto to_alloc = mm.translate_ptr(to_ptr);
  2025. auto from_alloc = mm.translate_ptr(from_ptr);
  2026. size_t to_offset = (byte_t *)to_ptr - to_alloc.alloc_ptr;
  2027. size_t from_offset = (byte_t *)from_ptr - from_alloc.alloc_ptr;
  2028. return q.submit([&](sycl::handler &cgh)
  2029. {
  2030. cgh.depends_on(dep_events);
  2031. auto r = sycl::range<1>(size);
  2032. auto to_o = sycl::id<1>(to_offset);
  2033. auto from_o = sycl::id<1>(from_offset);
  2034. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  2035. sycl::access::target::device>
  2036. to_acc(to_alloc.buffer, cgh, r, to_o);
  2037. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  2038. sycl::access::target::device>
  2039. from_acc(from_alloc.buffer, cgh, r, from_o);
  2040. cgh.copy(from_acc, to_acc); });
  2041. }
  2042. default:
  2043. throw std::runtime_error("dpct_memcpy: invalid direction value");
  2044. }
  2045. #else
  2046. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  2047. GGML_UNUSED(direction);
  2048. #endif // DPCT_USM_LEVEL_NONE
  2049. }
  2050. // Get actual copy range and make sure it will not exceed range.
  2051. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  2052. size_t pitch)
  2053. {
  2054. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  2055. }
  2056. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  2057. size_t pitch)
  2058. {
  2059. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  2060. }
  2061. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  2062. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  2063. static inline std::vector<sycl::event>
  2064. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  2065. sycl::range<3> to_range, sycl::range<3> from_range,
  2066. sycl::id<3> to_id, sycl::id<3> from_id,
  2067. sycl::range<3> size, memcpy_direction direction,
  2068. const std::vector<sycl::event> &dep_events = {})
  2069. {
  2070. // RAII for host pointer
  2071. class host_buffer
  2072. {
  2073. void *_buf;
  2074. size_t _size;
  2075. sycl::queue &_q;
  2076. const std::vector<sycl::event> &_deps; // free operation depends
  2077. public:
  2078. host_buffer(size_t size, sycl::queue &q,
  2079. const std::vector<sycl::event> &deps)
  2080. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  2081. void *get_ptr() const { return _buf; }
  2082. size_t get_size() const { return _size; }
  2083. ~host_buffer()
  2084. {
  2085. if (_buf)
  2086. {
  2087. _q.submit([&](sycl::handler &cgh)
  2088. {
  2089. cgh.depends_on(_deps);
  2090. cgh.host_task([buf = _buf] { std::free(buf); }); });
  2091. }
  2092. }
  2093. };
  2094. std::vector<sycl::event> event_list;
  2095. size_t to_slice = to_range.get(1) * to_range.get(0),
  2096. from_slice = from_range.get(1) * from_range.get(0);
  2097. unsigned char *to_surface =
  2098. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  2099. const unsigned char *from_surface =
  2100. (const unsigned char *)from_ptr +
  2101. get_offset(from_id, from_slice, from_range.get(0));
  2102. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  2103. {
  2104. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  2105. direction, dep_events)};
  2106. }
  2107. direction = detail::deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  2108. size_t size_slice = size.get(1) * size.get(0);
  2109. switch (direction)
  2110. {
  2111. case host_to_host:
  2112. for (size_t z = 0; z < size.get(2); ++z)
  2113. {
  2114. unsigned char *to_ptr = to_surface;
  2115. const unsigned char *from_ptr = from_surface;
  2116. if (to_range.get(0) == from_range.get(0) &&
  2117. to_range.get(0) == size.get(0))
  2118. {
  2119. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  2120. direction, dep_events));
  2121. }
  2122. else
  2123. {
  2124. for (size_t y = 0; y < size.get(1); ++y)
  2125. {
  2126. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  2127. direction, dep_events));
  2128. to_ptr += to_range.get(0);
  2129. from_ptr += from_range.get(0);
  2130. }
  2131. }
  2132. to_surface += to_slice;
  2133. from_surface += from_slice;
  2134. }
  2135. break;
  2136. case host_to_device:
  2137. {
  2138. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  2139. event_list);
  2140. std::vector<sycl::event> host_events;
  2141. if (to_slice == size_slice)
  2142. {
  2143. // Copy host data to a temp host buffer with the shape of target.
  2144. host_events =
  2145. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  2146. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  2147. host_to_host, dep_events);
  2148. }
  2149. else
  2150. {
  2151. // Copy host data to a temp host buffer with the shape of target.
  2152. host_events = dpct_memcpy(
  2153. q, buf.get_ptr(), from_surface, to_range, from_range,
  2154. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  2155. // If has padding data, not sure whether it is useless. So fill temp
  2156. // buffer with it.
  2157. std::vector<sycl::event>{
  2158. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  2159. device_to_host, dep_events)});
  2160. }
  2161. // Copy from temp host buffer to device with only one submit.
  2162. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  2163. buf.get_size(), host_to_device,
  2164. host_events));
  2165. break;
  2166. }
  2167. case device_to_host:
  2168. {
  2169. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  2170. event_list);
  2171. // Copy from host temp buffer to host target with reshaping.
  2172. event_list = dpct_memcpy(
  2173. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  2174. sycl::id<3>(0, 0, 0), size, host_to_host,
  2175. // Copy from device to temp host buffer with only one submit.
  2176. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  2177. buf.get_size(),
  2178. device_to_host, dep_events)});
  2179. break;
  2180. }
  2181. case device_to_device:
  2182. #ifdef DPCT_USM_LEVEL_NONE
  2183. {
  2184. auto &mm = mem_mgr::instance();
  2185. auto to_alloc = mm.translate_ptr(to_surface);
  2186. auto from_alloc = mm.translate_ptr(from_surface);
  2187. size_t to_offset = (byte_t *)to_surface - to_alloc.alloc_ptr;
  2188. size_t from_offset = (byte_t *)from_surface - from_alloc.alloc_ptr;
  2189. event_list.push_back(q.submit([&](sycl::handler &cgh)
  2190. {
  2191. cgh.depends_on(dep_events);
  2192. auto to_o = sycl::id<1>(to_offset);
  2193. auto from_o = sycl::id<1>(from_offset);
  2194. sycl::accessor<byte_t, 1, sycl::access_mode::write,
  2195. sycl::access::target::device>
  2196. to_acc(to_alloc.buffer, cgh,
  2197. get_copy_range(size, to_slice, to_range.get(0)), to_o);
  2198. sycl::accessor<byte_t, 1, sycl::access_mode::read,
  2199. sycl::access::target::device>
  2200. from_acc(from_alloc.buffer, cgh,
  2201. get_copy_range(size, from_slice, from_range.get(0)), from_o);
  2202. cgh.parallel_for<class dpct_memcpy_3d_detail_usmnone>(
  2203. size,
  2204. [=](sycl::id<3> id) {
  2205. to_acc[get_offset(id, to_slice, to_range.get(0))] =
  2206. from_acc[get_offset(id, from_slice, from_range.get(0))];
  2207. }); }));
  2208. }
  2209. #else
  2210. event_list.push_back(q.submit([&](sycl::handler &cgh)
  2211. {
  2212. cgh.depends_on(dep_events);
  2213. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  2214. size,
  2215. [=](sycl::id<3> id) {
  2216. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  2217. from_surface[get_offset(id, from_slice, from_range.get(0))];
  2218. }); }));
  2219. #endif
  2220. break;
  2221. default:
  2222. throw std::runtime_error("dpct_memcpy: invalid direction value");
  2223. }
  2224. return event_list;
  2225. }
  2226. /// memcpy 2D/3D matrix specified by pitched_data.
  2227. static inline std::vector<sycl::event>
  2228. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  2229. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  2230. memcpy_direction direction = automatic)
  2231. {
  2232. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  2233. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  2234. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  2235. size, direction);
  2236. }
  2237. /// memcpy 2D matrix with pitch.
  2238. static inline std::vector<sycl::event>
  2239. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  2240. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  2241. memcpy_direction direction = automatic)
  2242. {
  2243. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  2244. sycl::range<3>(from_pitch, y, 1),
  2245. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  2246. sycl::range<3>(x, y, 1), direction);
  2247. }
  2248. inline void gemm(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2249. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2250. const void *alpha, const void *a, library_data_t a_type,
  2251. int lda, const void *b, library_data_t b_type, int ldb,
  2252. const void *beta, void *c, library_data_t c_type, int ldc,
  2253. library_data_t scaling_type)
  2254. {
  2255. if (scaling_type == library_data_t::real_float &&
  2256. c_type == library_data_t::complex_float)
  2257. {
  2258. scaling_type = library_data_t::complex_float;
  2259. }
  2260. else if (scaling_type == library_data_t::real_double &&
  2261. c_type == library_data_t::complex_double)
  2262. {
  2263. scaling_type = library_data_t::complex_double;
  2264. }
  2265. std::uint64_t key =
  2266. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2267. switch (key)
  2268. {
  2269. case detail::get_type_combination_id(
  2270. library_data_t::real_float, library_data_t::real_float,
  2271. library_data_t::real_float, library_data_t::real_float):
  2272. {
  2273. detail::gemm_impl<float, float, float, float>(
  2274. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2275. break;
  2276. }
  2277. case detail::get_type_combination_id(
  2278. library_data_t::real_double, library_data_t::real_double,
  2279. library_data_t::real_double, library_data_t::real_double):
  2280. {
  2281. detail::gemm_impl<double, double, double, double>(
  2282. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2283. break;
  2284. }
  2285. case detail::get_type_combination_id(
  2286. library_data_t::complex_float, library_data_t::complex_float,
  2287. library_data_t::complex_float, library_data_t::complex_float):
  2288. {
  2289. detail::gemm_impl<std::complex<float>, std::complex<float>,
  2290. std::complex<float>, std::complex<float>>(
  2291. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2292. break;
  2293. }
  2294. case detail::get_type_combination_id(
  2295. library_data_t::complex_double, library_data_t::complex_double,
  2296. library_data_t::complex_double, library_data_t::complex_double):
  2297. {
  2298. detail::gemm_impl<std::complex<double>, std::complex<double>,
  2299. std::complex<double>, std::complex<double>>(
  2300. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2301. break;
  2302. }
  2303. case detail::get_type_combination_id(
  2304. library_data_t::real_half, library_data_t::real_half,
  2305. library_data_t::real_half, library_data_t::real_half):
  2306. {
  2307. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2308. sycl::half>(q, a_trans, b_trans, m, n, k, alpha, a,
  2309. lda, b, ldb, beta, c, ldc);
  2310. break;
  2311. }
  2312. case detail::get_type_combination_id(
  2313. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2314. library_data_t::real_float, library_data_t::real_float):
  2315. {
  2316. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2317. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda, b,
  2318. ldb, beta, c, ldc);
  2319. break;
  2320. }
  2321. case detail::get_type_combination_id(
  2322. library_data_t::real_half, library_data_t::real_half,
  2323. library_data_t::real_float, library_data_t::real_float):
  2324. {
  2325. detail::gemm_impl<sycl::half, sycl::half, float, float>(
  2326. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2327. break;
  2328. }
  2329. case detail::get_type_combination_id(
  2330. library_data_t::real_half, library_data_t::real_half,
  2331. library_data_t::real_half, library_data_t::real_float):
  2332. {
  2333. float alpha_value =
  2334. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2335. float beta_value =
  2336. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2337. sycl::half alpha_half(alpha_value);
  2338. sycl::half beta_half(beta_value);
  2339. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2340. sycl::half>(q, a_trans, b_trans, m, n, k, &alpha_half,
  2341. a, lda, b, ldb, &beta_half, c, ldc);
  2342. break;
  2343. }
  2344. case detail::get_type_combination_id(
  2345. library_data_t::real_int8, library_data_t::real_int8,
  2346. library_data_t::real_float, library_data_t::real_float):
  2347. {
  2348. detail::gemm_impl<std::int8_t, std::int8_t, float, float>(
  2349. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2350. break;
  2351. }
  2352. case detail::get_type_combination_id(
  2353. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2354. library_data_t::real_bfloat16, library_data_t::real_float):
  2355. {
  2356. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2357. oneapi::mkl::bfloat16, float>(
  2358. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2359. break;
  2360. }
  2361. case detail::get_type_combination_id(
  2362. library_data_t::real_int8, library_data_t::real_int8,
  2363. library_data_t::real_int32, library_data_t::real_int32):
  2364. {
  2365. float alpha_float =
  2366. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2367. float beta_float =
  2368. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2369. detail::gemm_impl<std::int8_t, std::int8_t, std::int32_t, float>(
  2370. q, a_trans, b_trans, m, n, k, &alpha_float, a, lda, b, ldb, &beta_float, c, ldc);
  2371. break;
  2372. }
  2373. default:
  2374. throw std::runtime_error("the combination of data type is unsupported");
  2375. }
  2376. } // gemm()
  2377. /// Computes a batch of matrix-matrix product with general matrices.
  2378. /// \param [in] q The queue where the routine should be executed.
  2379. /// \param [in] a_trans Specifies the operation applied to A.
  2380. /// \param [in] b_trans Specifies the operation applied to B.
  2381. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2382. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2383. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2384. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2385. /// \param [in] a Input matrix A.
  2386. /// \param [in] a_type Data type of the matrix A.
  2387. /// \param [in] lda Leading dimension of A.
  2388. /// \param [in] b Input matrix B.
  2389. /// \param [in] b_type Data type of the matrix B.
  2390. /// \param [in] ldb Leading dimension of B.
  2391. /// \param [in] beta Scaling factor for matrix C.
  2392. /// \param [in, out] c Input/Output matrix C.
  2393. /// \param [in] c_type Data type of the matrix C.
  2394. /// \param [in] ldc Leading dimension of C.
  2395. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2396. /// \param [in] scaling_type Data type of the scaling factors.
  2397. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2398. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2399. const void *alpha, const void *a[],
  2400. library_data_t a_type, int lda, const void *b[],
  2401. library_data_t b_type, int ldb, const void *beta,
  2402. void *c[], library_data_t c_type, int ldc,
  2403. int batch_size, library_data_t scaling_type)
  2404. {
  2405. #ifdef DPCT_USM_LEVEL_NONE
  2406. throw std::runtime_error("this API is unsupported when USM level is none");
  2407. #else
  2408. if (scaling_type == library_data_t::real_float &&
  2409. c_type == library_data_t::complex_float)
  2410. {
  2411. scaling_type = library_data_t::complex_float;
  2412. }
  2413. else if (scaling_type == library_data_t::real_double &&
  2414. c_type == library_data_t::complex_double)
  2415. {
  2416. scaling_type = library_data_t::complex_double;
  2417. }
  2418. std::uint64_t key =
  2419. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2420. switch (key)
  2421. {
  2422. case detail::get_type_combination_id(
  2423. library_data_t::real_float, library_data_t::real_float,
  2424. library_data_t::real_float, library_data_t::real_float):
  2425. {
  2426. detail::gemm_batch_impl<float, float, float, float>(
  2427. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2428. batch_size);
  2429. break;
  2430. }
  2431. case detail::get_type_combination_id(
  2432. library_data_t::real_double, library_data_t::real_double,
  2433. library_data_t::real_double, library_data_t::real_double):
  2434. {
  2435. detail::gemm_batch_impl<double, double, double, double>(
  2436. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2437. batch_size);
  2438. break;
  2439. }
  2440. case detail::get_type_combination_id(
  2441. library_data_t::complex_float, library_data_t::complex_float,
  2442. library_data_t::complex_float, library_data_t::complex_float):
  2443. {
  2444. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2445. std::complex<float>, std::complex<float>>(
  2446. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2447. batch_size);
  2448. break;
  2449. }
  2450. case detail::get_type_combination_id(
  2451. library_data_t::complex_double, library_data_t::complex_double,
  2452. library_data_t::complex_double, library_data_t::complex_double):
  2453. {
  2454. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2455. std::complex<double>, std::complex<double>>(
  2456. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2457. batch_size);
  2458. break;
  2459. }
  2460. case detail::get_type_combination_id(
  2461. library_data_t::real_half, library_data_t::real_half,
  2462. library_data_t::real_half, library_data_t::real_half):
  2463. {
  2464. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2465. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2466. a, lda, b, ldb, beta, c, ldc,
  2467. batch_size);
  2468. break;
  2469. }
  2470. #ifdef __INTEL_MKL__
  2471. case detail::get_type_combination_id(
  2472. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2473. library_data_t::real_bfloat16, library_data_t::real_float):
  2474. {
  2475. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2476. oneapi::mkl::bfloat16, float>(
  2477. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2478. batch_size);
  2479. break;
  2480. }
  2481. case detail::get_type_combination_id(
  2482. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2483. library_data_t::real_float, library_data_t::real_float):
  2484. {
  2485. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2486. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2487. b, ldb, beta, c, ldc, batch_size);
  2488. break;
  2489. }
  2490. case detail::get_type_combination_id(
  2491. library_data_t::real_int8, library_data_t::real_int8,
  2492. library_data_t::real_int32, library_data_t::real_int32):
  2493. {
  2494. float alpha_float =
  2495. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2496. float beta_float =
  2497. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2498. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2499. float>(q, a_trans, b_trans, m, n, k, &alpha_float,
  2500. a, lda, b, ldb, &beta_float, c, ldc,
  2501. batch_size);
  2502. break;
  2503. }
  2504. case detail::get_type_combination_id(
  2505. library_data_t::real_int8, library_data_t::real_int8,
  2506. library_data_t::real_float, library_data_t::real_float):
  2507. {
  2508. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2509. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2510. batch_size);
  2511. break;
  2512. }
  2513. case detail::get_type_combination_id(
  2514. library_data_t::real_half, library_data_t::real_half,
  2515. library_data_t::real_float, library_data_t::real_float):
  2516. {
  2517. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2518. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2519. batch_size);
  2520. break;
  2521. }
  2522. #endif
  2523. case detail::get_type_combination_id(
  2524. library_data_t::real_half, library_data_t::real_half,
  2525. library_data_t::real_half, library_data_t::real_float):
  2526. {
  2527. float alpha_value =
  2528. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2529. float beta_value =
  2530. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2531. sycl::half alpha_half(alpha_value);
  2532. sycl::half beta_half(beta_value);
  2533. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2534. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, b, ldb, &beta_half, c, ldc,
  2535. batch_size);
  2536. break;
  2537. }
  2538. default:
  2539. throw std::runtime_error("the combination of data type is unsupported");
  2540. }
  2541. #endif
  2542. }
  2543. /// Computes a batch of matrix-matrix product with general matrices.
  2544. /// \param [in] q The queue where the routine should be executed.
  2545. /// \param [in] a_trans Specifies the operation applied to A.
  2546. /// \param [in] b_trans Specifies the operation applied to B.
  2547. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2548. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2549. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2550. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2551. /// \param [in] a Input matrix A.
  2552. /// \param [in] a_type Data type of the matrix A.
  2553. /// \param [in] lda Leading dimension of A.
  2554. /// \param [in] stride_a Stride between the different A matrices.
  2555. /// \param [in] b Input matrix B.
  2556. /// \param [in] b_type Data type of the matrix B.
  2557. /// \param [in] ldb Leading dimension of B.
  2558. /// \param [in] stride_b Stride between the different B matrices.
  2559. /// \param [in] beta Scaling factor for matrix C.
  2560. /// \param [in, out] c Input/Output matrix C.
  2561. /// \param [in] c_type Data type of the matrix C.
  2562. /// \param [in] ldc Leading dimension of C.
  2563. /// \param [in] stride_c Stride between the different C matrices.
  2564. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2565. /// \param [in] scaling_type Data type of the scaling factors.
  2566. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2567. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2568. const void *alpha, const void *a, library_data_t a_type,
  2569. int lda, long long int stride_a, const void *b,
  2570. library_data_t b_type, int ldb, long long int stride_b,
  2571. const void *beta, void *c, library_data_t c_type,
  2572. int ldc, long long int stride_c, int batch_size,
  2573. library_data_t scaling_type)
  2574. {
  2575. if (scaling_type == library_data_t::real_float &&
  2576. c_type == library_data_t::complex_float)
  2577. {
  2578. scaling_type = library_data_t::complex_float;
  2579. }
  2580. else if (scaling_type == library_data_t::real_double &&
  2581. c_type == library_data_t::complex_double)
  2582. {
  2583. scaling_type = library_data_t::complex_double;
  2584. }
  2585. std::uint64_t key =
  2586. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2587. switch (key)
  2588. {
  2589. case detail::get_type_combination_id(
  2590. library_data_t::real_float, library_data_t::real_float,
  2591. library_data_t::real_float, library_data_t::real_float):
  2592. {
  2593. detail::gemm_batch_impl<float, float, float, float>(
  2594. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2595. beta, c, ldc, stride_c, batch_size);
  2596. break;
  2597. }
  2598. case detail::get_type_combination_id(
  2599. library_data_t::real_double, library_data_t::real_double,
  2600. library_data_t::real_double, library_data_t::real_double):
  2601. {
  2602. detail::gemm_batch_impl<double, double, double, double>(
  2603. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2604. beta, c, ldc, stride_c, batch_size);
  2605. break;
  2606. }
  2607. case detail::get_type_combination_id(
  2608. library_data_t::complex_float, library_data_t::complex_float,
  2609. library_data_t::complex_float, library_data_t::complex_float):
  2610. {
  2611. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2612. std::complex<float>, std::complex<float>>(
  2613. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2614. beta, c, ldc, stride_c, batch_size);
  2615. break;
  2616. }
  2617. case detail::get_type_combination_id(
  2618. library_data_t::complex_double, library_data_t::complex_double,
  2619. library_data_t::complex_double, library_data_t::complex_double):
  2620. {
  2621. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2622. std::complex<double>, std::complex<double>>(
  2623. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2624. beta, c, ldc, stride_c, batch_size);
  2625. break;
  2626. }
  2627. case detail::get_type_combination_id(
  2628. library_data_t::real_half, library_data_t::real_half,
  2629. library_data_t::real_half, library_data_t::real_half):
  2630. {
  2631. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2632. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2633. a, lda, stride_a, b, ldb, stride_b,
  2634. beta, c, ldc, stride_c, batch_size);
  2635. break;
  2636. }
  2637. #ifdef __INTEL_MKL__
  2638. case detail::get_type_combination_id(
  2639. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2640. library_data_t::real_bfloat16, library_data_t::real_float):
  2641. {
  2642. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2643. oneapi::mkl::bfloat16, float>(
  2644. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2645. beta, c, ldc, stride_c, batch_size);
  2646. break;
  2647. }
  2648. case detail::get_type_combination_id(
  2649. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2650. library_data_t::real_float, library_data_t::real_float):
  2651. {
  2652. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2653. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2654. stride_a, b, ldb, stride_b, beta, c, ldc,
  2655. stride_c, batch_size);
  2656. break;
  2657. }
  2658. case detail::get_type_combination_id(
  2659. library_data_t::real_int8, library_data_t::real_int8,
  2660. library_data_t::real_int32, library_data_t::real_int32):
  2661. {
  2662. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2663. std::int32_t>(q, a_trans, b_trans, m, n, k, alpha,
  2664. a, lda, stride_a, b, ldb, stride_b,
  2665. beta, c, ldc, stride_c, batch_size);
  2666. break;
  2667. }
  2668. case detail::get_type_combination_id(
  2669. library_data_t::real_int8, library_data_t::real_int8,
  2670. library_data_t::real_float, library_data_t::real_float):
  2671. {
  2672. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2673. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2674. beta, c, ldc, stride_c, batch_size);
  2675. break;
  2676. }
  2677. case detail::get_type_combination_id(
  2678. library_data_t::real_half, library_data_t::real_half,
  2679. library_data_t::real_float, library_data_t::real_float):
  2680. {
  2681. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2682. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2683. beta, c, ldc, stride_c, batch_size);
  2684. break;
  2685. }
  2686. #endif
  2687. case detail::get_type_combination_id(
  2688. library_data_t::real_half, library_data_t::real_half,
  2689. library_data_t::real_half, library_data_t::real_float):
  2690. {
  2691. float alpha_value =
  2692. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2693. float beta_value =
  2694. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2695. sycl::half alpha_half(alpha_value);
  2696. sycl::half beta_half(beta_value);
  2697. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2698. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, stride_a, b, ldb, stride_b,
  2699. &beta_half, c, ldc, stride_c, batch_size);
  2700. break;
  2701. }
  2702. default:
  2703. throw std::runtime_error("the combination of data type is unsupported");
  2704. }
  2705. }
  2706. static inline void
  2707. async_dpct_memcpy(void *to_ptr, size_t to_pitch, const void *from_ptr,
  2708. size_t from_pitch, size_t x, size_t y,
  2709. memcpy_direction direction = automatic,
  2710. sycl::queue &q = get_default_queue())
  2711. {
  2712. detail::dpct_memcpy(q, to_ptr, from_ptr, to_pitch, from_pitch, x, y,
  2713. direction);
  2714. }
  2715. using err0 = detail::generic_error_type<struct err0_tag, int>;
  2716. using err1 = detail::generic_error_type<struct err1_tag, int>;
  2717. } // COPY from DPCT head files
  2718. static int g_ggml_sycl_debug=0;
  2719. #define GGML_SYCL_DEBUG(...) do{if(g_ggml_sycl_debug) printf(__VA_ARGS__);}while(0)
  2720. #define CHECK_TRY_ERROR(expr) \
  2721. [&]() { \
  2722. try { \
  2723. expr; \
  2724. return dpct::success; \
  2725. } catch (std::exception const &e) { \
  2726. std::cerr << e.what()<< "\nException caught at file:" << __FILE__ \
  2727. << ", line:" << __LINE__ <<", func:"<<__func__<< std::endl; \
  2728. return dpct::default_error; \
  2729. } \
  2730. }()
  2731. // #define DEBUG_SYCL_MALLOC
  2732. static int g_work_group_size = 0;
  2733. // typedef sycl::half ggml_fp16_t;
  2734. #define __SYCL_ARCH__ DPCT_COMPATIBILITY_TEMP
  2735. #define VER_4VEC 610 //todo for hardward optimize.
  2736. #define VER_GEN9 700 //todo for hardward optimize.
  2737. #define VER_GEN12 1000000 //todo for hardward optimize.
  2738. #define VER_GEN13 (VER_GEN12 + 1030) //todo for hardward optimize.
  2739. #define GGML_SYCL_MAX_NODES 8192 //TODO: adapt to hardwares
  2740. //define for XMX in Intel GPU
  2741. //TODO: currently, it's not used for XMX really.
  2742. #define SYCL_USE_XMX
  2743. // max batch size to use MMQ kernels when tensor cores are available
  2744. #define XMX_MAX_BATCH_SIZE 32
  2745. #if defined(_MSC_VER)
  2746. #pragma warning(disable: 4244 4267) // possible loss of data
  2747. #endif
  2748. static_assert(sizeof(sycl::half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  2749. static void crash(){
  2750. int *ptr = NULL;
  2751. *ptr = 0;
  2752. }
  2753. static void ggml_sycl_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  2754. fprintf(stderr, "SYCL error: %s: %s\n", stmt, msg);
  2755. fprintf(stderr, " in function %s at %s:%d\n", func, file, line);
  2756. GGML_ASSERT(!"SYCL error");
  2757. }
  2758. #define SYCL_CHECK(err) do { \
  2759. auto err_ = (err); if (err_ != 0) ggml_sycl_error( \
  2760. #err, __func__, __FILE__, __LINE__, \
  2761. "Meet error in this line code!"); \
  2762. } while (0)
  2763. #if DPCT_COMPAT_RT_VERSION >= 11100
  2764. #define GGML_SYCL_ASSUME(x) __builtin_assume(x)
  2765. #else
  2766. #define GGML_SYCL_ASSUME(x)
  2767. #endif // DPCT_COMPAT_RT_VERSION >= 11100
  2768. #ifdef GGML_SYCL_F16
  2769. typedef sycl::half dfloat; // dequantize float
  2770. typedef sycl::half2 dfloat2;
  2771. #else
  2772. typedef float dfloat; // dequantize float
  2773. typedef sycl::float2 dfloat2;
  2774. #endif //GGML_SYCL_F16
  2775. bool ggml_sycl_loaded(void);
  2776. void * ggml_sycl_host_malloc(size_t size);
  2777. void ggml_sycl_host_free(void * ptr);
  2778. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
  2779. void ggml_sycl_free_data(struct ggml_tensor * tensor);
  2780. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor);
  2781. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor);
  2782. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor);
  2783. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor);
  2784. void ggml_sycl_copy_to_device(struct ggml_tensor * tensor);
  2785. void ggml_sycl_set_main_device(int main_device);
  2786. void ggml_sycl_set_mul_mat_q(bool mul_mat_q);
  2787. void ggml_sycl_set_scratch_size(size_t scratch_size);
  2788. void ggml_sycl_free_scratch(void);
  2789. void ggml_sycl_get_device_description(int device, char * description, size_t description_size);
  2790. bool ggml_backend_is_sycl(ggml_backend_t backend);
  2791. int ggml_backend_sycl_get_device(ggml_backend_t backend);
  2792. int get_main_device();
  2793. void print_ggml_tensor(const char*name, struct ggml_tensor *src);
  2794. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt);
  2795. void dev2dev_memcpy(sycl::queue &q_dst, sycl::queue &q_src, void *ptr_dst,
  2796. const void *ptr_src, size_t size) {
  2797. char *host_buf = (char *)malloc(size);
  2798. q_src.memcpy(host_buf, (const char *)ptr_src, size).wait();
  2799. q_dst.memcpy((char *)ptr_dst, host_buf, size).wait();
  2800. free(host_buf);
  2801. }
  2802. static __dpct_inline__ int get_int_from_int8(const int8_t *x8, const int &i32) {
  2803. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2804. int x32 = 0;
  2805. x32 |= x16[0] << 0;
  2806. x32 |= x16[1] << 16;
  2807. return x32;
  2808. }
  2809. static __dpct_inline__ int get_int_from_uint8(const uint8_t *x8,
  2810. const int &i32) {
  2811. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2812. int x32 = 0;
  2813. x32 |= x16[0] << 0;
  2814. x32 |= x16[1] << 16;
  2815. return x32;
  2816. }
  2817. static __dpct_inline__ int get_int_from_int8_aligned(const int8_t *x8,
  2818. const int &i32) {
  2819. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2820. }
  2821. static __dpct_inline__ int get_int_from_uint8_aligned(const uint8_t *x8,
  2822. const int &i32) {
  2823. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2824. }
  2825. template <typename T>
  2826. using to_t_sycl_t = void (*)(const void *__restrict__ x, T *__restrict__ y,
  2827. int k, dpct::queue_ptr stream);
  2828. typedef to_t_sycl_t<float> to_fp32_sycl_t;
  2829. typedef to_t_sycl_t<sycl::half> to_fp16_sycl_t;
  2830. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  2831. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  2832. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  2833. typedef void (*ggml_sycl_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  2834. typedef void (*ggml_sycl_op_mul_mat_t)(
  2835. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  2836. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  2837. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  2838. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  2839. const dpct::queue_ptr &stream);
  2840. typedef void (*ggml_sycl_op_flatten_t)(const ggml_tensor *src0,
  2841. const ggml_tensor *src1,
  2842. ggml_tensor *dst, const float *src0_dd,
  2843. const float *src1_dd, float *dst_dd,
  2844. const dpct::queue_ptr &main_stream);
  2845. // QK = number of values after dequantization
  2846. // QR = QK / number of values before dequantization
  2847. // QI = number of 32 bit integers before dequantization
  2848. #define QK4_0 32
  2849. #define QR4_0 2
  2850. #define QI4_0 (QK4_0 / (4 * QR4_0))
  2851. typedef struct dpct_type_471834 {
  2852. sycl::half d; // delta
  2853. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  2854. } block_q4_0;
  2855. static_assert(sizeof(block_q4_0) == sizeof(ggml_fp16_t) + QK4_0 / 2, "wrong q4_0 block size/padding");
  2856. #define QK4_1 32
  2857. #define QR4_1 2
  2858. #define QI4_1 (QK4_1 / (4 * QR4_1))
  2859. typedef struct dpct_type_143705 {
  2860. sycl::half2 dm; // dm.x = delta, dm.y = min
  2861. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  2862. } block_q4_1;
  2863. static_assert(sizeof(block_q4_1) == sizeof(ggml_fp16_t) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  2864. #define QK5_0 32
  2865. #define QR5_0 2
  2866. #define QI5_0 (QK5_0 / (4 * QR5_0))
  2867. typedef struct dpct_type_673649 {
  2868. sycl::half d; // delta
  2869. uint8_t qh[4]; // 5-th bit of quants
  2870. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  2871. } block_q5_0;
  2872. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  2873. #define QK5_1 32
  2874. #define QR5_1 2
  2875. #define QI5_1 (QK5_1 / (4 * QR5_1))
  2876. typedef struct dpct_type_135589 {
  2877. sycl::half2 dm; // dm.x = delta, dm.y = min
  2878. uint8_t qh[4]; // 5-th bit of quants
  2879. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  2880. } block_q5_1;
  2881. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  2882. #define QK8_0 32
  2883. #define QR8_0 1
  2884. #define QI8_0 (QK8_0 / (4 * QR8_0))
  2885. typedef struct dpct_type_122878 {
  2886. sycl::half d; // delta
  2887. int8_t qs[QK8_0]; // quants
  2888. } block_q8_0;
  2889. static_assert(sizeof(block_q8_0) == sizeof(ggml_fp16_t) + QK8_0, "wrong q8_0 block size/padding");
  2890. #define QK8_1 32
  2891. #define QR8_1 1
  2892. #define QI8_1 (QK8_1 / (4 * QR8_1))
  2893. typedef struct dpct_type_143721 {
  2894. sycl::half2 ds; // ds.x = delta, ds.y = sum
  2895. int8_t qs[QK8_0]; // quants
  2896. } block_q8_1;
  2897. static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_fp16_t) + QK8_0, "wrong q8_1 block size/padding");
  2898. typedef float (*vec_dot_q_sycl_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  2899. typedef void (*allocate_tiles_sycl_t)(int **x_ql, sycl::half2 **x_dm,
  2900. int **x_qh, int **x_sc);
  2901. typedef void (*load_tiles_sycl_t)(const void *__restrict__ vx,
  2902. int *__restrict__ x_ql,
  2903. sycl::half2 *__restrict__ x_dm,
  2904. int *__restrict__ x_qh,
  2905. int *__restrict__ x_sc, const int &i_offset,
  2906. const int &i_max, const int &k,
  2907. const int &blocks_per_row);
  2908. typedef float (*vec_dot_q_mul_mat_sycl_t)(
  2909. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  2910. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  2911. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ms,
  2912. const int &i, const int &j, const int &k);
  2913. //================================= k-quants
  2914. #ifdef GGML_QKK_64
  2915. #define QK_K 64
  2916. #define K_SCALE_SIZE 4
  2917. #else
  2918. #define QK_K 256
  2919. #define K_SCALE_SIZE 12
  2920. #endif
  2921. #define QR2_K 4
  2922. #define QI2_K (QK_K / (4*QR2_K))
  2923. typedef struct dpct_type_619598 {
  2924. uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
  2925. uint8_t qs[QK_K/4]; // quants
  2926. sycl::half2 dm; // super-block scale for quantized scales/mins
  2927. } block_q2_K;
  2928. static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_fp16_t) + QK_K/16 + QK_K/4, "wrong q2_K block size/padding");
  2929. #define QR3_K 4
  2930. #define QI3_K (QK_K / (4*QR3_K))
  2931. typedef struct dpct_type_138576 {
  2932. uint8_t hmask[QK_K/8]; // quants - high bit
  2933. uint8_t qs[QK_K/4]; // quants - low 2 bits
  2934. #ifdef GGML_QKK_64
  2935. uint8_t scales[2]; // scales, quantized with 8 bits
  2936. #else
  2937. uint8_t scales[K_SCALE_SIZE]; // scales, quantized with 6 bits
  2938. #endif
  2939. sycl::half d; // super-block scale
  2940. } block_q3_K;
  2941. //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");
  2942. #define QR4_K 2
  2943. #define QI4_K (QK_K / (4*QR4_K))
  2944. #ifdef GGML_QKK_64
  2945. typedef struct {
  2946. half dm[2]; // super-block scales/mins
  2947. uint8_t scales[2]; // 4-bit block scales/mins
  2948. uint8_t qs[QK_K/2]; // 4--bit quants
  2949. } block_q4_K;
  2950. static_assert(sizeof(block_q4_K) == sizeof(half2) + QK_K/2 + 2, "wrong q4_K block size/padding");
  2951. #else
  2952. typedef struct dpct_type_154943 {
  2953. sycl::half2 dm; // super-block scale for quantized scales/mins
  2954. uint8_t scales[3*QK_K/64]; // scales, quantized with 6 bits
  2955. uint8_t qs[QK_K/2]; // 4--bit quants
  2956. } block_q4_K;
  2957. static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_fp16_t) + 3*QK_K/64 + QK_K/2, "wrong q4_K block size/padding");
  2958. #endif
  2959. #define QR5_K 2
  2960. #define QI5_K (QK_K / (4*QR5_K))
  2961. #ifdef GGML_QKK_64
  2962. typedef struct {
  2963. half d; // super-block scale
  2964. int8_t scales[QK_K/16]; // block scales
  2965. uint8_t qh[QK_K/8]; // quants, high bit
  2966. uint8_t qs[QK_K/2]; // quants, low 4 bits
  2967. } block_q5_K;
  2968. 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");
  2969. #else
  2970. typedef struct dpct_type_866817 {
  2971. sycl::half2 dm; // super-block scale for quantized scales/mins
  2972. uint8_t scales[K_SCALE_SIZE]; // scales and mins, quantized with 6 bits
  2973. uint8_t qh[QK_K/8]; // quants, high bit
  2974. uint8_t qs[QK_K/2]; // quants, low 4 bits
  2975. } block_q5_K;
  2976. 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");
  2977. #endif
  2978. #define QR6_K 2
  2979. #define QI6_K (QK_K / (4*QR6_K))
  2980. typedef struct dpct_type_107281 {
  2981. uint8_t ql[QK_K/2]; // quants, lower 4 bits
  2982. uint8_t qh[QK_K/4]; // quants, upper 2 bits
  2983. int8_t scales[QK_K/16]; // scales
  2984. sycl::half d; // delta
  2985. } block_q6_K;
  2986. static_assert(sizeof(block_q6_K) == sizeof(ggml_fp16_t) + 13*QK_K/16, "wrong q6_K block size/padding");
  2987. #define WARP_SIZE 32
  2988. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  2989. #define SYCL_GELU_BLOCK_SIZE 256
  2990. #define SYCL_SILU_BLOCK_SIZE 256
  2991. #define SYCL_TANH_BLOCK_SIZE 256
  2992. #define SYCL_RELU_BLOCK_SIZE 256
  2993. #define SYCL_HARDSIGMOID_BLOCK_SIZE 256
  2994. #define SYCL_HARDSWISH_BLOCK_SIZE 256
  2995. #define SYCL_SQR_BLOCK_SIZE 256
  2996. #define SYCL_CPY_BLOCK_SIZE 32
  2997. #define SYCL_SCALE_BLOCK_SIZE 256
  2998. #define SYCL_CLAMP_BLOCK_SIZE 256
  2999. #define SYCL_ROPE_BLOCK_SIZE 256
  3000. #define SYCL_SOFT_MAX_BLOCK_SIZE 1024
  3001. #define SYCL_ALIBI_BLOCK_SIZE 32
  3002. #define SYCL_DIAG_MASK_INF_BLOCK_SIZE 32
  3003. #define SYCL_QUANTIZE_BLOCK_SIZE 256
  3004. #define SYCL_DEQUANTIZE_BLOCK_SIZE 256
  3005. #define SYCL_GET_ROWS_BLOCK_SIZE 256
  3006. #define SYCL_UPSCALE_BLOCK_SIZE 256
  3007. #define SYCL_CONCAT_BLOCK_SIZE 256
  3008. #define SYCL_PAD_BLOCK_SIZE 256
  3009. #define SYCL_ACC_BLOCK_SIZE 256
  3010. #define SYCL_IM2COL_BLOCK_SIZE 256
  3011. #define SYCL_POOL2D_BLOCK_SIZE 256
  3012. // dmmv = dequantize_mul_mat_vec
  3013. #ifndef GGML_SYCL_DMMV_X
  3014. #define GGML_SYCL_DMMV_X 32
  3015. #endif
  3016. #ifndef GGML_SYCL_MMV_Y
  3017. #define GGML_SYCL_MMV_Y 1
  3018. #endif
  3019. #ifndef K_QUANTS_PER_ITERATION
  3020. #define K_QUANTS_PER_ITERATION 2
  3021. #else
  3022. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  3023. #endif
  3024. #ifndef GGML_SYCL_PEER_MAX_BATCH_SIZE
  3025. #define GGML_SYCL_PEER_MAX_BATCH_SIZE 128
  3026. #endif // GGML_SYCL_PEER_MAX_BATCH_SIZE
  3027. #define MUL_MAT_SRC1_COL_STRIDE 128
  3028. #define MAX_STREAMS 8
  3029. static dpct::queue_ptr g_syclStreams[GGML_SYCL_MAX_DEVICES][MAX_STREAMS] = {{0}};
  3030. struct ggml_tensor_extra_gpu {
  3031. void * data_device[GGML_SYCL_MAX_DEVICES]; // 1 pointer for each device for split tensors
  3032. dpct::event_ptr
  3033. events[GGML_SYCL_MAX_DEVICES]
  3034. [MAX_STREAMS]; // events for synchronizing multiple GPUs
  3035. };
  3036. class sycl_gpu_mgr {
  3037. public:
  3038. std::vector<int> gpus;
  3039. std::vector<sycl::device> devices;
  3040. sycl::queue *first_queue;
  3041. sycl::context co_ctx;
  3042. int max_compute_units = 0;
  3043. int work_group_size = 0;
  3044. std::string gpus_list = "";
  3045. sycl_gpu_mgr() {
  3046. detect_sycl_gpu_list_with_max_cu();
  3047. get_allow_gpus();
  3048. create_context_with_gpus();
  3049. }
  3050. void create_context_with_gpus() {
  3051. sycl::context ctx = sycl::context(devices);
  3052. assert(gpus.size() > 0);
  3053. first_queue = dpct::get_current_device().create_queue(ctx, devices[0]);
  3054. co_ctx = first_queue->get_context();
  3055. }
  3056. sycl::context &get_co_ctx() { return co_ctx; }
  3057. void get_allow_gpus() {
  3058. gpus_list = "";
  3059. for (size_t i = 0; i < gpus.size(); ++i) {
  3060. gpus_list += std::to_string(gpus[i]);
  3061. gpus_list += ",";
  3062. }
  3063. if (gpus_list.length() > 2) {
  3064. gpus_list.pop_back();
  3065. }
  3066. }
  3067. bool is_allowed_gpu(int device_id) {
  3068. return std::find(gpus.begin(), gpus.end(), device_id) != gpus.end();
  3069. }
  3070. void detect_sycl_gpu_list_with_max_cu() try {
  3071. int device_count = dpct::dev_mgr::instance().device_count();
  3072. for (int id = 0; id < device_count; id++) {
  3073. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  3074. if (!device.is_gpu())
  3075. continue;
  3076. dpct::device_info prop;
  3077. dpct::get_device_info(prop, device);
  3078. if (max_compute_units < prop.get_max_compute_units())
  3079. max_compute_units = prop.get_max_compute_units();
  3080. }
  3081. for (int id = 0; id < device_count; id++) {
  3082. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  3083. if (!device.is_gpu())
  3084. continue;
  3085. dpct::device_info prop;
  3086. dpct::get_device_info(prop, device);
  3087. if (max_compute_units == prop.get_max_compute_units() &&
  3088. prop.get_major_version() == 1) {
  3089. gpus.push_back(id);
  3090. devices.push_back(device);
  3091. work_group_size = prop.get_max_work_group_size();
  3092. }
  3093. }
  3094. return;
  3095. } catch (sycl::exception const &exc) {
  3096. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3097. << ", line:" << __LINE__ << std::endl;
  3098. std::exit(1);
  3099. }
  3100. int get_gpu_count() { return (int)gpus.size(); }
  3101. int get_index(int id) {
  3102. for (int i = 0; i < (int)gpus.size(); i++) {
  3103. if (gpus[i] == id)
  3104. return i;
  3105. }
  3106. assert(false);
  3107. return -1;
  3108. }
  3109. int get_next_index(int id) {
  3110. int cur_index = get_index(id);
  3111. for (int i = cur_index + 1; i < (int)gpus.size(); i++) {
  3112. if (gpus[i] == id)
  3113. return i;
  3114. }
  3115. assert(false);
  3116. return -1;
  3117. }
  3118. };
  3119. static sycl_gpu_mgr *g_sycl_gpu_mgr = NULL;
  3120. static int g_device_count = -1;
  3121. static int g_all_sycl_device_count = -1;
  3122. static int g_main_device = -1;
  3123. static int g_main_device_id = -1;
  3124. static std::array<float, GGML_SYCL_MAX_DEVICES> g_default_tensor_split = {};
  3125. static float g_tensor_split[GGML_SYCL_MAX_DEVICES] = {0};
  3126. struct sycl_device_capabilities {
  3127. int cc; // compute capability
  3128. bool vmm; // virtual memory support
  3129. size_t vmm_granularity; // granularity of virtual memory
  3130. int device_id;
  3131. };
  3132. static sycl_device_capabilities g_device_caps[GGML_SYCL_MAX_DEVICES] = { {0, false, 0, -1} };
  3133. struct sycl_device_id2index {
  3134. int index;
  3135. };
  3136. static void * g_scratch_buffer = nullptr;
  3137. static size_t g_scratch_size = 0; // disabled by default
  3138. static size_t g_scratch_offset = 0;
  3139. static dpct::queue_ptr g_sycl_handles[GGML_SYCL_MAX_DEVICES] = {nullptr};
  3140. int get_main_device(){
  3141. return g_main_device;
  3142. }
  3143. [[noreturn]]
  3144. static void bad_arch(const sycl::stream &stream_ct1) {
  3145. stream_ct1 << "ERROR: ggml-sycl was compiled without support for the "
  3146. "current GPU architecture.\n";
  3147. // __trap();
  3148. std::exit(1);
  3149. (void) bad_arch; // suppress unused function warning
  3150. }
  3151. /*
  3152. device_index: device index from 0 to n (continue numbers).
  3153. It is used for device select/set in SYCL backend internal data structure.
  3154. */
  3155. void check_allow_gpu_index(const int device_index) {
  3156. if (device_index >= g_device_count) {
  3157. char error_buf[256];
  3158. snprintf(error_buf, sizeof(error_buf),
  3159. "%s error: device_index:%d is out of range: [0-%d]", __func__,
  3160. device_index, g_device_count - 1);
  3161. fprintf(stderr, "%s\n", error_buf);
  3162. assert(false);
  3163. }
  3164. }
  3165. /*
  3166. device_id: device ID is shown by ggml_backend_sycl_print_sycl_devices().
  3167. It is only used to set current working device.
  3168. */
  3169. void check_allow_gpu_id(const int device_id) {
  3170. if (!g_sycl_gpu_mgr->is_allowed_gpu(device_id)) {
  3171. char error_buf[256];
  3172. snprintf(error_buf, sizeof(error_buf),
  3173. "error: cannot set device=%d, which is not allowed. Please "
  3174. "set GPU ID in: [%s]",
  3175. device_id, g_sycl_gpu_mgr->gpus_list.c_str());
  3176. fprintf(stderr, "%s\n", error_buf);
  3177. throw std::invalid_argument(error_buf);
  3178. }
  3179. }
  3180. int get_current_device_id() {
  3181. return dpct::dev_mgr::instance().current_device_id();
  3182. }
  3183. inline dpct::err0 ggml_sycl_set_device(const int device) try {
  3184. int device_id = g_sycl_gpu_mgr->gpus[device];
  3185. check_allow_gpu_id(device_id);
  3186. int current_device_id;
  3187. SYCL_CHECK(CHECK_TRY_ERROR(current_device_id = get_current_device_id()));
  3188. // GGML_SYCL_DEBUG("ggml_sycl_set_device device_id=%d,
  3189. // current_device_id=%d\n", device, current_device);
  3190. if (device_id == current_device_id) {
  3191. return 0;
  3192. }
  3193. return CHECK_TRY_ERROR(dpct::select_device(device_id));
  3194. } catch (sycl::exception const &exc) {
  3195. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3196. << ", line:" << __LINE__ << std::endl;
  3197. crash();
  3198. std::exit(1);
  3199. }
  3200. void log_ggml_var_device(const char*name, float *src, size_t total_elements, bool src_on_device){
  3201. if(!g_ggml_sycl_debug) return;
  3202. if(!src){
  3203. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3204. return;
  3205. }
  3206. char filename[1024];
  3207. sprintf(filename, "%s.txt", name);
  3208. printf("GGML Tensor:%s save to %s\n", name, filename);
  3209. size_t total_size = total_elements*sizeof(float);
  3210. float *local_buf = NULL;
  3211. if(src_on_device) {
  3212. local_buf = (float *) ggml_sycl_host_malloc(total_size);
  3213. ggml_sycl_set_device(g_main_device);
  3214. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3215. main_stream->memcpy(local_buf, src, total_size);
  3216. }
  3217. else {
  3218. local_buf = (float *)src;
  3219. }
  3220. std::ofstream logfile;
  3221. logfile.open(filename);
  3222. for(size_t i=0; i<total_elements; i++){
  3223. if((i+1)%20 ==0) logfile <<std::endl;
  3224. else logfile << local_buf[i] <<" ";
  3225. }
  3226. logfile <<std::endl;
  3227. logfile.close();
  3228. if(src_on_device) ggml_sycl_host_free(local_buf);
  3229. }
  3230. //todo: debug for crash in some case
  3231. void print_ggml_tensor(const char*name, struct ggml_tensor *src){
  3232. if(!g_ggml_sycl_debug) return;
  3233. if(!src){
  3234. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3235. return;
  3236. }
  3237. size_t total_elements = ggml_nelements(src);
  3238. const bool src_on_device = src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  3239. float *src_data =NULL;
  3240. if(src_on_device) {
  3241. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  3242. src_data = (float*)src_extra->data_device[g_main_device];
  3243. }
  3244. else {
  3245. src_data = (float *)src->data;
  3246. }
  3247. log_ggml_var_device(name, src_data, total_elements, src_on_device);
  3248. }
  3249. static int log_file_name_idx=0;
  3250. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt) {
  3251. stop_cnt = 4;
  3252. if(log_file_name_idx>=stop_cnt) return;
  3253. char filename[1280];
  3254. sprintf(filename, "%s_%07d", name, log_file_name_idx);
  3255. log_file_name_idx++;
  3256. print_ggml_tensor(filename, src);
  3257. }
  3258. static __dpct_inline__ float warp_reduce_sum(float x,
  3259. const sycl::nd_item<3> &item_ct1) {
  3260. #pragma unroll
  3261. for (int mask = 16; mask > 0; mask >>= 1) {
  3262. /*
  3263. DPCT1096:98: The right-most dimension of the work-group used in the SYCL
  3264. kernel that calls this function may be less than "32". The function
  3265. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3266. CPU device. Modify the size of the work-group to ensure that the value
  3267. of the right-most dimension is a multiple of "32".
  3268. */
  3269. x += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), x, mask);
  3270. }
  3271. return x;
  3272. }
  3273. static __dpct_inline__ sycl::float2
  3274. warp_reduce_sum(sycl::float2 a, const sycl::nd_item<3> &item_ct1) {
  3275. #pragma unroll
  3276. for (int mask = 16; mask > 0; mask >>= 1) {
  3277. a.x() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.x(),
  3278. mask);
  3279. a.y() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.y(),
  3280. mask);
  3281. }
  3282. return a;
  3283. }
  3284. static __dpct_inline__ float warp_reduce_max(float x,
  3285. const sycl::nd_item<3> &item_ct1) {
  3286. #pragma unroll
  3287. for (int mask = 16; mask > 0; mask >>= 1) {
  3288. /*
  3289. DPCT1096:97: The right-most dimension of the work-group used in the SYCL
  3290. kernel that calls this function may be less than "32". The function
  3291. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3292. CPU device. Modify the size of the work-group to ensure that the value
  3293. of the right-most dimension is a multiple of "32".
  3294. */
  3295. x = sycl::fmax(x, dpct::permute_sub_group_by_xor(
  3296. item_ct1.get_sub_group(), x, mask));
  3297. }
  3298. return x;
  3299. }
  3300. static __dpct_inline__ float op_repeat(const float a, const float b) {
  3301. return b;
  3302. GGML_UNUSED(a);
  3303. }
  3304. static __dpct_inline__ float op_add(const float a, const float b) {
  3305. return a + b;
  3306. }
  3307. static __dpct_inline__ float op_mul(const float a, const float b) {
  3308. return a * b;
  3309. }
  3310. static __dpct_inline__ float op_div(const float a, const float b) {
  3311. return a / b;
  3312. }
  3313. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3314. static void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3315. int ne0, int ne1, int ne2, int ne3,
  3316. int ne10, int ne11, int ne12, int ne13,
  3317. /*int s0, */ int s1, int s2, int s3,
  3318. /*int s10,*/ int s11, int s12, int s13,
  3319. const sycl::nd_item<3> &item_ct1) {
  3320. const int i0s = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3321. item_ct1.get_local_id(2);
  3322. const int i1 = (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  3323. item_ct1.get_local_id(1));
  3324. const int i2 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3325. item_ct1.get_local_id(0)) /
  3326. ne3;
  3327. const int i3 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3328. item_ct1.get_local_id(0)) %
  3329. ne3;
  3330. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3331. return;
  3332. }
  3333. const int i11 = i1 % ne11;
  3334. const int i12 = i2 % ne12;
  3335. const int i13 = i3 % ne13;
  3336. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3337. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3338. const size_t i_dst = i_src0;
  3339. const src0_t * src0_row = src0 + i_src0;
  3340. const src1_t * src1_row = src1 + i_src1;
  3341. dst_t * dst_row = dst + i_dst;
  3342. for (int i0 = i0s; i0 < ne0;
  3343. i0 += item_ct1.get_local_range(2) * item_ct1.get_group_range(2)) {
  3344. const int i10 = i0 % ne10;
  3345. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3346. }
  3347. }
  3348. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3349. static void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3350. int ne0, int ne1, int ne2, int ne3,
  3351. int ne10, int ne11, int ne12, int ne13,
  3352. /*int s0, */ int s1, int s2, int s3,
  3353. /*int s10,*/ int s11, int s12, int s13,
  3354. const sycl::nd_item<3> &item_ct1) {
  3355. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3356. item_ct1.get_local_id(2);
  3357. const int i3 = i/(ne2*ne1*ne0);
  3358. const int i2 = (i/(ne1*ne0)) % ne2;
  3359. const int i1 = (i/ne0) % ne1;
  3360. const int i0 = i % ne0;
  3361. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3362. return;
  3363. }
  3364. const int i11 = i1 % ne11;
  3365. const int i12 = i2 % ne12;
  3366. const int i13 = i3 % ne13;
  3367. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3368. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3369. const size_t i_dst = i_src0;
  3370. const src0_t * src0_row = src0 + i_src0;
  3371. const src1_t * src1_row = src1 + i_src1;
  3372. dst_t * dst_row = dst + i_dst;
  3373. const int i10 = i0 % ne10;
  3374. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3375. }
  3376. static void acc_f32(const float * x, const float * y, float * dst, const int ne,
  3377. const int ne10, const int ne11, const int ne12,
  3378. const int nb1, const int nb2, int offset, const sycl::nd_item<3> &item_ct1) {
  3379. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3380. item_ct1.get_local_id(2);
  3381. if (i >= ne) {
  3382. return;
  3383. }
  3384. int src1_idx = i - offset;
  3385. int oz = src1_idx / nb2;
  3386. int oy = (src1_idx - (oz * nb2)) / nb1;
  3387. int ox = src1_idx % nb1;
  3388. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  3389. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  3390. } else {
  3391. dst[i] = x[i];
  3392. }
  3393. }
  3394. static void gelu_f32(const float * x, float * dst, const int k,
  3395. const sycl::nd_item<3> &item_ct1) {
  3396. const float GELU_COEF_A = 0.044715f;
  3397. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  3398. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3399. item_ct1.get_local_id(2);
  3400. if (i >= k) {
  3401. return;
  3402. }
  3403. float xi = x[i];
  3404. dst[i] = 0.5f * xi *
  3405. (1.0f +
  3406. sycl::tanh(SQRT_2_OVER_PI * xi * (1.0f + GELU_COEF_A * xi * xi)));
  3407. }
  3408. static void silu_f32(const float * x, float * dst, const int k,
  3409. const sycl::nd_item<3> &item_ct1) {
  3410. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3411. item_ct1.get_local_id(2);
  3412. if (i >= k) {
  3413. return;
  3414. }
  3415. dst[i] = x[i] / (1.0f + sycl::native::exp(-x[i]));
  3416. }
  3417. static void gelu_quick_f32(const float *x, float *dst, int k,
  3418. const sycl::nd_item<3> &item_ct1) {
  3419. const float GELU_QUICK_COEF = -1.702f;
  3420. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3421. item_ct1.get_local_id(2);
  3422. if (i >= k) {
  3423. return;
  3424. }
  3425. dst[i] = x[i] * (1.0f / (1.0f + sycl::native::exp(GELU_QUICK_COEF * x[i])));
  3426. }
  3427. static void tanh_f32(const float *x, float *dst, int k,
  3428. const sycl::nd_item<3> &item_ct1) {
  3429. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3430. item_ct1.get_local_id(2);
  3431. if (i >= k) {
  3432. return;
  3433. }
  3434. dst[i] = sycl::tanh((float)(x[i]));
  3435. }
  3436. static void relu_f32(const float * x, float * dst, const int k,
  3437. const sycl::nd_item<3> &item_ct1) {
  3438. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3439. item_ct1.get_local_id(2);
  3440. if (i >= k) {
  3441. return;
  3442. }
  3443. dst[i] = sycl::fmax((float)(x[i]), (float)0);
  3444. }
  3445. static void hardsigmoid_f32(const float * x, float * dst, const int k,
  3446. const sycl::nd_item<3> &item_ct1) {
  3447. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3448. item_ct1.get_local_id(2);
  3449. if (i >= k) {
  3450. return;
  3451. }
  3452. dst[i] = sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3453. }
  3454. static void hardswish_f32(const float * x, float * dst, const int k,
  3455. const sycl::nd_item<3> &item_ct1) {
  3456. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3457. item_ct1.get_local_id(2);
  3458. if (i >= k) {
  3459. return;
  3460. }
  3461. dst[i] = x[i] * sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3462. }
  3463. static void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope,
  3464. const sycl::nd_item<3> &item_ct1) {
  3465. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3466. item_ct1.get_local_id(2);
  3467. if (i >= k) {
  3468. return;
  3469. }
  3470. dst[i] = sycl::fmax((float)(x[i]), (float)0) +
  3471. sycl::fmin((float)(x[i]), 0.0f) * negative_slope;
  3472. }
  3473. static void sqr_f32(const float * x, float * dst, const int k,
  3474. const sycl::nd_item<3> &item_ct1) {
  3475. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3476. item_ct1.get_local_id(2);
  3477. if (i >= k) {
  3478. return;
  3479. }
  3480. dst[i] = x[i] * x[i];
  3481. }
  3482. static void norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3483. const sycl::nd_item<3> &item_ct1, sycl::float2 *s_sum, int block_size) {
  3484. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3485. item_ct1.get_local_id(1);
  3486. const int tid = item_ct1.get_local_id(2);
  3487. sycl::float2 mean_var = sycl::float2(0.f, 0.f);
  3488. for (int col = tid; col < ncols; col += block_size) {
  3489. const float xi = x[row*ncols + col];
  3490. mean_var.x() += xi;
  3491. mean_var.y() += xi * xi;
  3492. }
  3493. // sum up partial sums
  3494. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3495. if (block_size > WARP_SIZE) {
  3496. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3497. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3498. if (lane_id == 0) {
  3499. s_sum[warp_id] = mean_var;
  3500. }
  3501. /*
  3502. DPCT1118:0: SYCL group functions and algorithms must be encountered in
  3503. converged control flow. You may need to adjust the code.
  3504. */
  3505. item_ct1.barrier(sycl::access::fence_space::local_space);
  3506. mean_var = s_sum[lane_id];
  3507. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3508. }
  3509. const float mean = mean_var.x() / ncols;
  3510. const float var = mean_var.y() / ncols - mean * mean;
  3511. const float inv_std = sycl::rsqrt(var + eps);
  3512. for (int col = tid; col < ncols; col += block_size) {
  3513. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  3514. }
  3515. }
  3516. static void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02,
  3517. const sycl::nd_item<3> &item_ct1) {
  3518. int nidx = item_ct1.get_local_id(2) +
  3519. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3520. if (nidx >= ne0) {
  3521. return;
  3522. }
  3523. // operation
  3524. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3525. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3526. if (item_ct1.get_group(0) < ne02) { // src0
  3527. int offset_src =
  3528. nidx + item_ct1.get_group(1) * ne0 +
  3529. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3530. dst[offset_dst] = x[offset_src];
  3531. } else {
  3532. int offset_src =
  3533. nidx + item_ct1.get_group(1) * ne0 +
  3534. (item_ct1.get_group(0) - ne02) * ne0 * item_ct1.get_group_range(1);
  3535. dst[offset_dst] = y[offset_src];
  3536. }
  3537. }
  3538. static void upscale_f32(const float *x, float *dst, const int ne00, const int nb02, const int scale_factor,
  3539. const sycl::nd_item<3> &item_ct1) {
  3540. int ne0 = ne00 * scale_factor;
  3541. int nidx = item_ct1.get_local_id(2) +
  3542. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3543. if (nidx >= ne0) {
  3544. return;
  3545. }
  3546. // operation
  3547. int i00 = nidx / scale_factor;
  3548. int i01 = item_ct1.get_group(1) / scale_factor;
  3549. int offset_src = i00 + i01 * ne00 + item_ct1.get_group(0) * nb02;
  3550. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3551. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3552. dst[offset_dst] = x[offset_src];
  3553. }
  3554. static void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02,
  3555. const sycl::nd_item<3> &item_ct1) {
  3556. int nidx = item_ct1.get_local_id(2) +
  3557. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3558. if (nidx >= ne0) {
  3559. return;
  3560. }
  3561. // operation
  3562. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3563. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3564. if (nidx < ne00 && item_ct1.get_group(1) < ne01 &&
  3565. item_ct1.get_group(0) < ne02) {
  3566. int offset_src = nidx + item_ct1.get_group(1) * ne00 +
  3567. item_ct1.get_group(0) * ne00 * ne01;
  3568. dst[offset_dst] = x[offset_src];
  3569. } else {
  3570. dst[offset_dst] = 0.0f;
  3571. }
  3572. }
  3573. static void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps,
  3574. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3575. int start = item_ct1.get_group(2) * group_size;
  3576. int end = start + group_size;
  3577. start += item_ct1.get_local_id(2);
  3578. if (end >= ne_elements) {
  3579. end = ne_elements;
  3580. }
  3581. float tmp = 0.0f; // partial sum for thread in warp
  3582. for (int j = start; j < end; j += block_size) {
  3583. tmp += x[j];
  3584. }
  3585. tmp = warp_reduce_sum(tmp, item_ct1);
  3586. if (block_size > WARP_SIZE) {
  3587. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3588. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3589. if (lane_id == 0) {
  3590. s_sum[warp_id] = tmp;
  3591. }
  3592. /*
  3593. DPCT1118:1: SYCL group functions and algorithms must be encountered in
  3594. converged control flow. You may need to adjust the code.
  3595. */
  3596. /*
  3597. DPCT1065:54: Consider replacing sycl::nd_item::barrier() with
  3598. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3599. better performance if there is no access to global memory.
  3600. */
  3601. item_ct1.barrier();
  3602. tmp = s_sum[lane_id];
  3603. tmp = warp_reduce_sum(tmp, item_ct1);
  3604. }
  3605. float mean = tmp / group_size;
  3606. tmp = 0.0f;
  3607. for (int j = start; j < end; j += block_size) {
  3608. float xi = x[j] - mean;
  3609. dst[j] = xi;
  3610. tmp += xi * xi;
  3611. }
  3612. tmp = warp_reduce_sum(tmp, item_ct1);
  3613. if (block_size > WARP_SIZE) {
  3614. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3615. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3616. if (lane_id == 0) {
  3617. s_sum[warp_id] = tmp;
  3618. }
  3619. /*
  3620. DPCT1118:2: SYCL group functions and algorithms must be encountered in
  3621. converged control flow. You may need to adjust the code.
  3622. */
  3623. /*
  3624. DPCT1065:55: Consider replacing sycl::nd_item::barrier() with
  3625. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3626. better performance if there is no access to global memory.
  3627. */
  3628. item_ct1.barrier();
  3629. tmp = s_sum[lane_id];
  3630. tmp = warp_reduce_sum(tmp, item_ct1);
  3631. }
  3632. float variance = tmp / group_size;
  3633. float scale = sycl::rsqrt(variance + eps);
  3634. for (int j = start; j < end; j += block_size) {
  3635. dst[j] *= scale;
  3636. }
  3637. }
  3638. static void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3639. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3640. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3641. item_ct1.get_local_id(1);
  3642. const int tid = item_ct1.get_local_id(2);
  3643. float tmp = 0.0f; // partial sum for thread in warp
  3644. for (int col = tid; col < ncols; col += block_size) {
  3645. const float xi = x[row*ncols + col];
  3646. tmp += xi * xi;
  3647. }
  3648. // sum up partial sums
  3649. tmp = warp_reduce_sum(tmp, item_ct1);
  3650. if (block_size > WARP_SIZE) {
  3651. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3652. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3653. if (lane_id == 0) {
  3654. s_sum[warp_id] = tmp;
  3655. }
  3656. /*
  3657. DPCT1118:3: SYCL group functions and algorithms must be encountered in
  3658. converged control flow. You may need to adjust the code.
  3659. */
  3660. item_ct1.barrier(sycl::access::fence_space::local_space);
  3661. tmp = s_sum[lane_id];
  3662. tmp = warp_reduce_sum(tmp, item_ct1);
  3663. }
  3664. const float mean = tmp / ncols;
  3665. const float scale = sycl::rsqrt(mean + eps);
  3666. for (int col = tid; col < ncols; col += block_size) {
  3667. dst[row*ncols + col] = scale * x[row*ncols + col];
  3668. }
  3669. }
  3670. static __dpct_inline__ void dequantize_q4_0(const void *vx, const int ib,
  3671. const int iqs, dfloat2 &v) {
  3672. const block_q4_0 * x = (const block_q4_0 *) vx;
  3673. const dfloat d = x[ib].d;
  3674. const int vui = x[ib].qs[iqs];
  3675. v.x() = vui & 0xF;
  3676. v.y() = vui >> 4;
  3677. #ifdef GGML_SYCL_F16
  3678. // v = v - {8.0f, 8.0f};
  3679. // v = v * {d, d};
  3680. v.s0() = (v.s0() - 8.0f) * d;
  3681. v.s1() = (v.s1() - 8.0f) * d;
  3682. #else
  3683. v.x() = (v.x() - 8.0f) * d;
  3684. v.y() = (v.y() - 8.0f) * d;
  3685. #endif // GGML_SYCL_F16
  3686. }
  3687. static __dpct_inline__ void dequantize_q4_1(const void *vx, const int ib,
  3688. const int iqs, dfloat2 &v) {
  3689. const block_q4_1 * x = (const block_q4_1 *) vx;
  3690. const dfloat d = x[ib].dm[0];
  3691. const dfloat m = x[ib].dm[1];
  3692. const int vui = x[ib].qs[iqs];
  3693. v.x() = vui & 0xF;
  3694. v.y() = vui >> 4;
  3695. #ifdef GGML_SYCL_F16
  3696. // v = v * {d, d};
  3697. // v = v + {m, m};
  3698. v.s0() = (v.s0() * d) + m;
  3699. v.s1() = (v.s1() * d) + m;
  3700. #else
  3701. v.x() = (v.x() * d) + m;
  3702. v.y() = (v.y() * d) + m;
  3703. #endif // GGML_SYCL_F16
  3704. }
  3705. static __dpct_inline__ void dequantize_q5_0(const void *vx, const int ib,
  3706. const int iqs, dfloat2 &v) {
  3707. const block_q5_0 * x = (const block_q5_0 *) vx;
  3708. const dfloat d = x[ib].d;
  3709. uint32_t qh;
  3710. memcpy(&qh, x[ib].qh, sizeof(qh));
  3711. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3712. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3713. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3714. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3715. #ifdef GGML_SYCL_F16
  3716. // v = v - {16.0f, 16.0f};
  3717. // v = v * {d, d};
  3718. v.s0() = (v.s0() - 16.0f) * d;
  3719. v.s1() = (v.s1() - 16.0f) * d;
  3720. #else
  3721. v.x() = (v.x() - 16.0f) * d;
  3722. v.y() = (v.y() - 16.0f) * d;
  3723. #endif // GGML_SYCL_F16
  3724. }
  3725. static __dpct_inline__ void dequantize_q5_1(const void *vx, const int ib,
  3726. const int iqs, dfloat2 &v) {
  3727. const block_q5_1 * x = (const block_q5_1 *) vx;
  3728. const dfloat d = x[ib].dm[0];
  3729. const dfloat m = x[ib].dm[1];
  3730. uint32_t qh;
  3731. memcpy(&qh, x[ib].qh, sizeof(qh));
  3732. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3733. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3734. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3735. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3736. #ifdef GGML_SYCL_F16
  3737. // v = v * {d, d};
  3738. // v = v + {m, m};
  3739. v.s0() = (v.s0() * d) + m;
  3740. v.s1() = (v.s1() * d) + m;
  3741. #else
  3742. v.x() = (v.x() * d) + m;
  3743. v.y() = (v.y() * d) + m;
  3744. #endif // GGML_SYCL_F16
  3745. }
  3746. static __dpct_inline__ void dequantize_q8_0(const void *vx, const int ib,
  3747. const int iqs, dfloat2 &v) {
  3748. const block_q8_0 * x = (const block_q8_0 *) vx;
  3749. const dfloat d = x[ib].d;
  3750. v.x() = x[ib].qs[iqs + 0];
  3751. v.y() = x[ib].qs[iqs + 1];
  3752. #ifdef GGML_SYCL_F16
  3753. // v = v * {d, d};
  3754. v.s0() *= d;
  3755. v.s1() *= d;
  3756. #else
  3757. v.x() *= d;
  3758. v.y() *= d;
  3759. #endif // GGML_SYCL_F16
  3760. }
  3761. //================================== k-quants
  3762. template<typename dst_t>
  3763. static void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3764. const sycl::nd_item<3> &item_ct1) {
  3765. const int i = item_ct1.get_group(2);
  3766. const block_q2_K * x = (const block_q2_K *) vx;
  3767. const int tid = item_ct1.get_local_id(2);
  3768. #if QK_K == 256
  3769. const int n = tid/32;
  3770. const int l = tid - 32*n;
  3771. const int is = 8*n + l/16;
  3772. const uint8_t q = x[i].qs[32*n + l];
  3773. dst_t * y = yy + i*QK_K + 128*n;
  3774. float dall = x[i].dm[0];
  3775. float dmin = x[i].dm[1];
  3776. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3777. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3778. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  3779. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  3780. #else
  3781. const int is = tid/16; // 0 or 1
  3782. const int il = tid%16; // 0...15
  3783. const uint8_t q = x[i].qs[il] >> (2*is);
  3784. dst_t * y = yy + i*QK_K + 16*is + il;
  3785. float dall = __low2half(x[i].dm);
  3786. float dmin = __high2half(x[i].dm);
  3787. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3788. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3789. #endif
  3790. }
  3791. template<typename dst_t>
  3792. static void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3793. const sycl::nd_item<3> &item_ct1) {
  3794. const int i = item_ct1.get_group(2);
  3795. const block_q3_K * x = (const block_q3_K *) vx;
  3796. #if QK_K == 256
  3797. const int r = item_ct1.get_local_id(2) / 4;
  3798. const int tid = r/2;
  3799. const int is0 = r%2;
  3800. const int l0 = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4);
  3801. const int n = tid / 4;
  3802. const int j = tid - 4*n;
  3803. uint8_t m = 1 << (4*n + j);
  3804. int is = 8*n + 2*j + is0;
  3805. int shift = 2*j;
  3806. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  3807. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  3808. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  3809. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  3810. float d_all = x[i].d;
  3811. float dl = d_all * (us - 32);
  3812. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  3813. const uint8_t * q = x[i].qs + 32*n;
  3814. const uint8_t * hm = x[i].hmask;
  3815. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  3816. #else
  3817. const int tid = threadIdx.x;
  3818. const int is = tid/16; // 0 or 1
  3819. const int il = tid%16; // 0...15
  3820. const int im = il/8; // 0...1
  3821. const int in = il%8; // 0...7
  3822. dst_t * y = yy + i*QK_K + 16*is + il;
  3823. const uint8_t q = x[i].qs[il] >> (2*is);
  3824. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  3825. const float d = (float)x[i].d;
  3826. if (is == 0) {
  3827. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  3828. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  3829. } else {
  3830. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  3831. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  3832. }
  3833. #endif
  3834. }
  3835. #if QK_K == 256
  3836. static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  3837. if (j < 4) {
  3838. d = q[j] & 63; m = q[j + 4] & 63;
  3839. } else {
  3840. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  3841. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  3842. }
  3843. }
  3844. #endif
  3845. template<typename dst_t>
  3846. static void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3847. const sycl::nd_item<3> &item_ct1) {
  3848. const block_q4_K * x = (const block_q4_K *) vx;
  3849. const int i = item_ct1.get_group(2);
  3850. #if QK_K == 256
  3851. // assume 32 threads
  3852. const int tid = item_ct1.get_local_id(2);
  3853. const int il = tid/8;
  3854. const int ir = tid%8;
  3855. const int is = 2*il;
  3856. const int n = 4;
  3857. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  3858. const float dall = x[i].dm[0];
  3859. const float dmin = x[i].dm[1];
  3860. const uint8_t * q = x[i].qs + 32*il + n*ir;
  3861. uint8_t sc, m;
  3862. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3863. const float d1 = dall * sc; const float m1 = dmin * m;
  3864. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3865. const float d2 = dall * sc; const float m2 = dmin * m;
  3866. for (int l = 0; l < n; ++l) {
  3867. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  3868. y[l +32] = d2 * (q[l] >> 4) - m2;
  3869. }
  3870. #else
  3871. const int tid = threadIdx.x;
  3872. const uint8_t * q = x[i].qs;
  3873. dst_t * y = yy + i*QK_K;
  3874. const float d = (float)x[i].dm[0];
  3875. const float m = (float)x[i].dm[1];
  3876. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  3877. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  3878. #endif
  3879. }
  3880. template<typename dst_t>
  3881. static void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3882. const sycl::nd_item<3> &item_ct1) {
  3883. const block_q5_K * x = (const block_q5_K *) vx;
  3884. const int i = item_ct1.get_group(2);
  3885. #if QK_K == 256
  3886. // assume 64 threads - this is very slightly better than the one below
  3887. const int tid = item_ct1.get_local_id(2);
  3888. const int il = tid/16; // il is in 0...3
  3889. const int ir = tid%16; // ir is in 0...15
  3890. const int is = 2*il; // is is in 0...6
  3891. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  3892. const float dall = x[i].dm[0];
  3893. const float dmin = x[i].dm[1];
  3894. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  3895. const uint8_t * qh = x[i].qh + 2*ir;
  3896. uint8_t sc, m;
  3897. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3898. const float d1 = dall * sc; const float m1 = dmin * m;
  3899. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3900. const float d2 = dall * sc; const float m2 = dmin * m;
  3901. uint8_t hm = 1 << (2*il);
  3902. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  3903. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  3904. hm <<= 1;
  3905. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  3906. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  3907. #else
  3908. const int tid = threadIdx.x;
  3909. const uint8_t q = x[i].qs[tid];
  3910. const int im = tid/8; // 0...3
  3911. const int in = tid%8; // 0...7
  3912. const int is = tid/16; // 0 or 1
  3913. const uint8_t h = x[i].qh[in] >> im;
  3914. const float d = x[i].d;
  3915. dst_t * y = yy + i*QK_K + tid;
  3916. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  3917. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  3918. #endif
  3919. }
  3920. template<typename dst_t>
  3921. static void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3922. const sycl::nd_item<3> &item_ct1) {
  3923. const block_q6_K * x = (const block_q6_K *) vx;
  3924. const int i = item_ct1.get_group(2);
  3925. #if QK_K == 256
  3926. // assume 64 threads - this is very slightly better than the one below
  3927. const int tid = item_ct1.get_local_id(2);
  3928. const int ip = tid/32; // ip is 0 or 1
  3929. const int il = tid - 32*ip; // 0...32
  3930. const int is = 8*ip + il/16;
  3931. dst_t * y = yy + i*QK_K + 128*ip + il;
  3932. const float d = x[i].d;
  3933. const uint8_t * ql = x[i].ql + 64*ip + il;
  3934. const uint8_t qh = x[i].qh[32*ip + il];
  3935. const int8_t * sc = x[i].scales + is;
  3936. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3937. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  3938. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3939. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  3940. #else
  3941. // assume 32 threads
  3942. const int tid = threadIdx.x;
  3943. const int ip = tid/16; // 0 or 1
  3944. const int il = tid - 16*ip; // 0...15
  3945. dst_t * y = yy + i*QK_K + 16*ip + il;
  3946. const float d = x[i].d;
  3947. const uint8_t ql = x[i].ql[16*ip + il];
  3948. const uint8_t qh = x[i].qh[il] >> (2*ip);
  3949. const int8_t * sc = x[i].scales;
  3950. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3951. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3952. #endif
  3953. }
  3954. /*
  3955. DPCT1110:4: The total declared local variable size in device function
  3956. dequantize_mul_mat_vec_q2_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_q2_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. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  3967. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3968. item_ct1.get_local_id(1);
  3969. if (row > nrows) return;
  3970. const int num_blocks_per_row = ncols / QK_K;
  3971. const int ib0 = row*num_blocks_per_row;
  3972. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  3973. float tmp = 0; // partial sum for thread in warp
  3974. #if QK_K == 256
  3975. const int tid =
  3976. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  3977. const int ix =
  3978. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  3979. const int step = 16/K_QUANTS_PER_ITERATION;
  3980. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  3981. const int in = tid - step*im; // 0...15 or 0...7
  3982. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  3983. const int q_offset = 32*im + l0;
  3984. const int s_offset = 8*im;
  3985. const int y_offset = 128*im + l0;
  3986. uint32_t aux[4];
  3987. const uint8_t * d = (const uint8_t *)aux;
  3988. const uint8_t * m = (const uint8_t *)(aux + 2);
  3989. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  3990. const float * y = yy + i * QK_K + y_offset;
  3991. const uint8_t * q = x[i].qs + q_offset;
  3992. const float dall = x[i].dm[0];
  3993. const float dmin = x[i].dm[1];
  3994. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  3995. aux[0] = a[0] & 0x0f0f0f0f;
  3996. aux[1] = a[1] & 0x0f0f0f0f;
  3997. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  3998. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  3999. float sum1 = 0, sum2 = 0;
  4000. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4001. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  4002. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  4003. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  4004. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  4005. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  4006. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  4007. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  4008. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  4009. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  4010. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  4011. }
  4012. tmp += dall * sum1 - dmin * sum2;
  4013. }
  4014. #else
  4015. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  4016. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  4017. const int offset = tid * K_QUANTS_PER_ITERATION;
  4018. uint32_t uaux[2];
  4019. const uint8_t * d = (const uint8_t *)uaux;
  4020. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4021. const float * y = yy + i * QK_K + offset;
  4022. const uint8_t * q = x[i].qs + offset;
  4023. const uint32_t * s = (const uint32_t *)x[i].scales;
  4024. uaux[0] = s[0] & 0x0f0f0f0f;
  4025. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  4026. const float2 dall = __half22float2(x[i].dm);
  4027. float sum1 = 0, sum2 = 0;
  4028. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4029. const uint8_t ql = q[l];
  4030. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  4031. + y[l+16] * d[1] * ((ql >> 2) & 3)
  4032. + y[l+32] * d[2] * ((ql >> 4) & 3)
  4033. + y[l+48] * d[3] * ((ql >> 6) & 3);
  4034. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  4035. }
  4036. tmp += dall.x * sum1 - dall.y * sum2;
  4037. }
  4038. #endif
  4039. // sum up partial sums and write back result
  4040. #pragma unroll
  4041. for (int mask = 16; mask > 0; mask >>= 1) {
  4042. tmp +=
  4043. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4044. }
  4045. if (item_ct1.get_local_id(2) == 0) {
  4046. dst[row] = tmp;
  4047. }
  4048. }
  4049. /*
  4050. DPCT1110:5: The total declared local variable size in device function
  4051. dequantize_mul_mat_vec_q3_k exceeds 128 bytes and may cause high register
  4052. pressure. Consult with your hardware vendor to find the total register size
  4053. available and adjust the code, or use smaller sub-group size to avoid high
  4054. register pressure.
  4055. */
  4056. static void dequantize_mul_mat_vec_q3_k(const void *__restrict__ vx,
  4057. const float *__restrict__ yy,
  4058. float *__restrict__ dst,
  4059. const int ncols, int nrows,
  4060. const sycl::nd_item<3> &item_ct1) {
  4061. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4062. item_ct1.get_local_id(1);
  4063. if (row > nrows) return;
  4064. const int num_blocks_per_row = ncols / QK_K;
  4065. const int ib0 = row*num_blocks_per_row;
  4066. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  4067. float tmp = 0; // partial sum for thread in warp
  4068. #if QK_K == 256
  4069. const uint16_t kmask1 = 0x0303;
  4070. const uint16_t kmask2 = 0x0f0f;
  4071. const int tid =
  4072. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4073. const int ix =
  4074. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4075. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  4076. const int step = 16/K_QUANTS_PER_ITERATION;
  4077. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4078. const int in = tid - step*im; // 0....15 or 0...7
  4079. const uint8_t m = 1 << (4*im);
  4080. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  4081. const int q_offset = 32*im + l0;
  4082. const int y_offset = 128*im + l0;
  4083. uint16_t utmp[4];
  4084. const int8_t * s = (const int8_t *)utmp;
  4085. const uint16_t s_shift = 4*im;
  4086. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4087. const float * y = yy + i * QK_K + y_offset;
  4088. const uint8_t * q = x[i].qs + q_offset;
  4089. const uint8_t * h = x[i].hmask + l0;
  4090. const uint16_t * a = (const uint16_t *)x[i].scales;
  4091. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  4092. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  4093. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  4094. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  4095. const float d = x[i].d;
  4096. float sum = 0;
  4097. for (int l = 0; l < n; ++l) {
  4098. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  4099. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  4100. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  4101. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  4102. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  4103. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  4104. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  4105. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  4106. }
  4107. tmp += d * sum;
  4108. }
  4109. #else
  4110. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  4111. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  4112. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  4113. const int in = offset/8; // 0 or 1
  4114. const int im = offset%8; // 0...7
  4115. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4116. const float * y = yy + i * QK_K + offset;
  4117. const uint8_t * q = x[i].qs + offset;
  4118. const uint8_t * s = x[i].scales;
  4119. const float dall = (float)x[i].d;
  4120. float sum = 0;
  4121. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4122. const uint8_t hl = x[i].hmask[im+l] >> in;
  4123. const uint8_t ql = q[l];
  4124. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  4125. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  4126. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  4127. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  4128. }
  4129. tmp += sum;
  4130. }
  4131. #endif
  4132. // sum up partial sums and write back result
  4133. #pragma unroll
  4134. for (int mask = 16; mask > 0; mask >>= 1) {
  4135. tmp +=
  4136. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4137. }
  4138. if (item_ct1.get_local_id(2) == 0) {
  4139. dst[row] = tmp;
  4140. }
  4141. }
  4142. /*
  4143. DPCT1110:6: The total declared local variable size in device function
  4144. dequantize_mul_mat_vec_q4_k exceeds 128 bytes and may cause high register
  4145. pressure. Consult with your hardware vendor to find the total register size
  4146. available and adjust the code, or use smaller sub-group size to avoid high
  4147. register pressure.
  4148. */
  4149. static void dequantize_mul_mat_vec_q4_k(const void *__restrict__ vx,
  4150. const float *__restrict__ yy,
  4151. float *__restrict__ dst,
  4152. const int ncols, int nrows,
  4153. const sycl::nd_item<3> &item_ct1) {
  4154. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4155. item_ct1.get_local_id(1);
  4156. if (row > nrows) return;
  4157. const int num_blocks_per_row = ncols / QK_K;
  4158. const int ib0 = row*num_blocks_per_row;
  4159. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  4160. #if QK_K == 256
  4161. const uint16_t kmask1 = 0x3f3f;
  4162. const uint16_t kmask2 = 0x0f0f;
  4163. const uint16_t kmask3 = 0xc0c0;
  4164. const int tid =
  4165. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4166. const int ix =
  4167. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4168. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  4169. const int il = tid/step; // 0...3
  4170. const int ir = tid - step*il; // 0...7 or 0...3
  4171. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  4172. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4173. const int in = il%2;
  4174. const int l0 = n*(2*ir + in);
  4175. const int q_offset = 32*im + l0;
  4176. const int y_offset = 64*im + l0;
  4177. uint16_t aux[4];
  4178. const uint8_t * sc = (const uint8_t *)aux;
  4179. #if K_QUANTS_PER_ITERATION == 2
  4180. uint32_t q32[4];
  4181. const uint8_t * q4 = (const uint8_t *)q32;
  4182. #else
  4183. uint16_t q16[4];
  4184. const uint8_t * q4 = (const uint8_t *)q16;
  4185. #endif
  4186. float tmp = 0; // partial sum for thread in warp
  4187. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4188. const float * y1 = yy + i*QK_K + y_offset;
  4189. const float * y2 = y1 + 128;
  4190. const float dall = x[i].dm[0];
  4191. const float dmin = x[i].dm[1];
  4192. const uint16_t * a = (const uint16_t *)x[i].scales;
  4193. aux[0] = a[im+0] & kmask1;
  4194. aux[1] = a[im+2] & kmask1;
  4195. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4196. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4197. #if K_QUANTS_PER_ITERATION == 2
  4198. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  4199. const uint32_t * q2 = q1 + 16;
  4200. q32[0] = q1[0] & 0x0f0f0f0f;
  4201. q32[1] = q1[0] & 0xf0f0f0f0;
  4202. q32[2] = q2[0] & 0x0f0f0f0f;
  4203. q32[3] = q2[0] & 0xf0f0f0f0;
  4204. sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
  4205. float smin = 0;
  4206. for (int l = 0; l < 4; ++l) {
  4207. s.x() += y1[l] * q4[l + 0]; s.y() += y1[l + 32] * q4[l + 4];
  4208. s.z() += y2[l] * q4[l + 8]; s.w() += y2[l + 32] * q4[l + 12];
  4209. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4210. }
  4211. tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f / 16.f +
  4212. s.z() * sc[4] + s.w() * sc[5] * 1.f / 16.f) -
  4213. dmin * smin;
  4214. #else
  4215. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  4216. const uint16_t * q2 = q1 + 32;
  4217. q16[0] = q1[0] & 0x0f0f;
  4218. q16[1] = q1[0] & 0xf0f0;
  4219. q16[2] = q2[0] & 0x0f0f;
  4220. q16[3] = q2[0] & 0xf0f0;
  4221. float4 s = {0.f, 0.f, 0.f, 0.f};
  4222. float smin = 0;
  4223. for (int l = 0; l < 2; ++l) {
  4224. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  4225. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  4226. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4227. }
  4228. 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;
  4229. #endif
  4230. }
  4231. #else
  4232. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  4233. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  4234. const int step = tid * K_QUANTS_PER_ITERATION;
  4235. uint16_t aux16[2];
  4236. const uint8_t * s = (const uint8_t *)aux16;
  4237. float tmp = 0;
  4238. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4239. const uint8_t * q = x[i].qs + step;
  4240. const float * y = yy + i*QK_K + step;
  4241. const uint16_t * a = (const uint16_t *)x[i].scales;
  4242. aux16[0] = a[0] & 0x0f0f;
  4243. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4244. const float d = (float)x[i].dm[0];
  4245. const float m = (float)x[i].dm[1];
  4246. float sum = 0.f;
  4247. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4248. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  4249. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  4250. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  4251. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  4252. }
  4253. tmp += sum;
  4254. }
  4255. #endif
  4256. // sum up partial sums and write back result
  4257. #pragma unroll
  4258. for (int mask = 16; mask > 0; mask >>= 1) {
  4259. tmp +=
  4260. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4261. }
  4262. if (tid == 0) {
  4263. dst[row] = tmp;
  4264. }
  4265. }
  4266. /*
  4267. DPCT1110:7: The total declared local variable size in device function
  4268. dequantize_mul_mat_vec_q5_k exceeds 128 bytes and may cause high register
  4269. pressure. Consult with your hardware vendor to find the total register size
  4270. available and adjust the code, or use smaller sub-group size to avoid high
  4271. register pressure.
  4272. */
  4273. static void dequantize_mul_mat_vec_q5_k(const void *__restrict__ vx,
  4274. const float *__restrict__ yy,
  4275. float *__restrict__ dst,
  4276. const int ncols,
  4277. const sycl::nd_item<3> &item_ct1) {
  4278. const int row = item_ct1.get_group(2);
  4279. const int num_blocks_per_row = ncols / QK_K;
  4280. const int ib0 = row*num_blocks_per_row;
  4281. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  4282. float tmp = 0; // partial sum for thread in warp
  4283. #if QK_K == 256
  4284. const uint16_t kmask1 = 0x3f3f;
  4285. const uint16_t kmask2 = 0x0f0f;
  4286. const uint16_t kmask3 = 0xc0c0;
  4287. const int tid = item_ct1.get_local_id(2) / 2; // 0...15
  4288. const int ix = item_ct1.get_local_id(2) % 2;
  4289. const int il = tid/4; // 0...3
  4290. const int ir = tid - 4*il;// 0...3
  4291. const int n = 2;
  4292. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4293. const int in = il%2;
  4294. const int l0 = n*(2*ir + in);
  4295. const int q_offset = 32*im + l0;
  4296. const int y_offset = 64*im + l0;
  4297. const uint8_t hm1 = 1 << (2*im);
  4298. const uint8_t hm2 = hm1 << 4;
  4299. uint16_t aux[4];
  4300. const uint8_t * sc = (const uint8_t *)aux;
  4301. uint16_t q16[8];
  4302. const uint8_t * q4 = (const uint8_t *)q16;
  4303. for (int i = ix; i < num_blocks_per_row; i += 2) {
  4304. const uint8_t * ql1 = x[i].qs + q_offset;
  4305. const uint8_t * qh = x[i].qh + l0;
  4306. const float * y1 = yy + i*QK_K + y_offset;
  4307. const float * y2 = y1 + 128;
  4308. const float dall = x[i].dm[0];
  4309. const float dmin = x[i].dm[1];
  4310. const uint16_t * a = (const uint16_t *)x[i].scales;
  4311. aux[0] = a[im+0] & kmask1;
  4312. aux[1] = a[im+2] & kmask1;
  4313. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4314. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4315. sycl::float4 sum = {0.f, 0.f, 0.f, 0.f};
  4316. float smin = 0;
  4317. const uint16_t * q1 = (const uint16_t *)ql1;
  4318. const uint16_t * q2 = q1 + 32;
  4319. q16[0] = q1[0] & 0x0f0f;
  4320. q16[1] = q1[8] & 0x0f0f;
  4321. q16[2] = (q1[0] >> 4) & 0x0f0f;
  4322. q16[3] = (q1[8] >> 4) & 0x0f0f;
  4323. q16[4] = q2[0] & 0x0f0f;
  4324. q16[5] = q2[8] & 0x0f0f;
  4325. q16[6] = (q2[0] >> 4) & 0x0f0f;
  4326. q16[7] = (q2[8] >> 4) & 0x0f0f;
  4327. for (int l = 0; l < n; ++l) {
  4328. sum.x() +=
  4329. y1[l + 0] * (q4[l + 0] + (qh[l + 0] & (hm1 << 0) ? 16 : 0)) +
  4330. y1[l + 16] * (q4[l + 2] + (qh[l + 16] & (hm1 << 0) ? 16 : 0));
  4331. sum.y() +=
  4332. y1[l + 32] * (q4[l + 4] + (qh[l + 0] & (hm1 << 1) ? 16 : 0)) +
  4333. y1[l + 48] * (q4[l + 6] + (qh[l + 16] & (hm1 << 1) ? 16 : 0));
  4334. sum.z() +=
  4335. y2[l + 0] * (q4[l + 8] + (qh[l + 0] & (hm2 << 0) ? 16 : 0)) +
  4336. y2[l + 16] * (q4[l + 10] + (qh[l + 16] & (hm2 << 0) ? 16 : 0));
  4337. sum.w() +=
  4338. y2[l + 32] * (q4[l + 12] + (qh[l + 0] & (hm2 << 1) ? 16 : 0)) +
  4339. y2[l + 48] * (q4[l + 14] + (qh[l + 16] & (hm2 << 1) ? 16 : 0));
  4340. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  4341. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  4342. }
  4343. tmp += dall * (sum.x() * sc[0] + sum.y() * sc[1] + sum.z() * sc[4] +
  4344. sum.w() * sc[5]) -
  4345. dmin * smin;
  4346. }
  4347. #else
  4348. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
  4349. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
  4350. const int step = tid * K_QUANTS_PER_ITERATION;
  4351. const int im = step/8;
  4352. const int in = step%8;
  4353. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4354. const uint8_t * q = x[i].qs + step;
  4355. const int8_t * s = x[i].scales;
  4356. const float * y = yy + i*QK_K + step;
  4357. const float d = x[i].d;
  4358. float sum = 0.f;
  4359. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4360. const uint8_t h = x[i].qh[in+j] >> im;
  4361. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  4362. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  4363. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  4364. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  4365. }
  4366. tmp += sum;
  4367. }
  4368. #endif
  4369. // sum up partial sums and write back result
  4370. #pragma unroll
  4371. for (int mask = 16; mask > 0; mask >>= 1) {
  4372. tmp +=
  4373. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4374. }
  4375. if (item_ct1.get_local_id(2) == 0) {
  4376. dst[row] = tmp;
  4377. }
  4378. }
  4379. static void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows,
  4380. const sycl::nd_item<3> &item_ct1) {
  4381. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4382. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4383. item_ct1.get_local_id(1);
  4384. if (row > nrows) return;
  4385. const int num_blocks_per_row = ncols / QK_K;
  4386. const int ib0 = row*num_blocks_per_row;
  4387. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  4388. #if QK_K == 256
  4389. const int tid =
  4390. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4391. const int ix =
  4392. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0, 1
  4393. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  4394. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4395. const int in = tid - step*im; // 0...15 or 0...7
  4396. #if K_QUANTS_PER_ITERATION == 1
  4397. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  4398. const int is = 0;
  4399. #else
  4400. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  4401. const int is = in / 4;
  4402. #endif
  4403. const int ql_offset = 64*im + l0;
  4404. const int qh_offset = 32*im + l0;
  4405. const int s_offset = 8*im + is;
  4406. const int y_offset = 128*im + l0;
  4407. float tmp = 0; // partial sum for thread in warp
  4408. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4409. const float * y = yy + i * QK_K + y_offset;
  4410. const uint8_t * ql = x[i].ql + ql_offset;
  4411. const uint8_t * qh = x[i].qh + qh_offset;
  4412. const int8_t * s = x[i].scales + s_offset;
  4413. const float d = x[i].d;
  4414. #if K_QUANTS_PER_ITERATION == 1
  4415. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  4416. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  4417. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  4418. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  4419. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  4420. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  4421. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  4422. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  4423. tmp += sum;
  4424. #else
  4425. float sum = 0;
  4426. for (int l = 0; l < 4; ++l) {
  4427. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  4428. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  4429. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  4430. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  4431. }
  4432. tmp += sum;
  4433. #endif
  4434. }
  4435. #else
  4436. const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
  4437. const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
  4438. const int step = tid * K_QUANTS_PER_ITERATION;
  4439. float tmp = 0; // partial sum for thread in warp
  4440. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4441. const float * y = yy + i * QK_K + step;
  4442. const uint8_t * ql = x[i].ql + step;
  4443. const uint8_t * qh = x[i].qh + step;
  4444. const int8_t * s = x[i].scales;
  4445. const float d = x[i+0].d;
  4446. float sum = 0;
  4447. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4448. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  4449. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  4450. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  4451. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  4452. }
  4453. tmp += sum;
  4454. }
  4455. #endif
  4456. // sum up partial sums and write back result
  4457. #pragma unroll
  4458. for (int mask = 16; mask > 0; mask >>= 1) {
  4459. tmp +=
  4460. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4461. }
  4462. if (tid == 0) {
  4463. dst[row] = tmp;
  4464. }
  4465. }
  4466. static void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4467. const sycl::half *x = (const sycl::half *)vx;
  4468. // automatic half -> float type cast if dfloat == float
  4469. v.x() = x[ib + iqs + 0];
  4470. v.y() = x[ib + iqs + 1];
  4471. }
  4472. static void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4473. const float * x = (const float *) vx;
  4474. // automatic half -> float type cast if dfloat == float
  4475. v.x() = x[ib + iqs + 0];
  4476. v.y() = x[ib + iqs + 1];
  4477. }
  4478. static void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded,
  4479. const sycl::nd_item<3> &item_ct1) {
  4480. const int ix = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4481. item_ct1.get_local_id(2);
  4482. if (ix >= kx_padded) {
  4483. return;
  4484. }
  4485. const int iy = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4486. item_ct1.get_local_id(1);
  4487. const int i_padded = iy*kx_padded + ix;
  4488. block_q8_1 * y = (block_q8_1 *) vy;
  4489. const int ib = i_padded / QK8_1; // block index
  4490. const int iqs = i_padded % QK8_1; // quant index
  4491. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  4492. float amax = sycl::fabs((float)xi);
  4493. float sum = xi;
  4494. #pragma unroll
  4495. for (int mask = 16; mask > 0; mask >>= 1) {
  4496. amax = sycl::fmax(amax, dpct::permute_sub_group_by_xor(
  4497. item_ct1.get_sub_group(), amax, mask));
  4498. sum +=
  4499. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), sum, mask);
  4500. }
  4501. const float d = amax / 127;
  4502. const int8_t q = amax == 0.0f ? 0 : sycl::round(xi / d);
  4503. y[ib].qs[iqs] = q;
  4504. if (iqs > 0) {
  4505. return;
  4506. }
  4507. reinterpret_cast<sycl::half &>(y[ib].ds.x()) = d;
  4508. reinterpret_cast<sycl::half &>(y[ib].ds.y()) = sum;
  4509. }
  4510. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4511. static void k_get_rows(
  4512. const void * src0, const int32_t * src1, dst_t * dst,
  4513. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4514. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4515. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4516. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4517. size_t s10, size_t s11, size_t s12,
  4518. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4519. const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4520. item_ct1.get_local_id(2)) *
  4521. 2;
  4522. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4523. item_ct1.get_local_id(1);
  4524. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4525. item_ct1.get_local_id(0)) /
  4526. ne12;
  4527. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4528. item_ct1.get_local_id(0)) %
  4529. ne12;
  4530. if (i00 >= ne00) {
  4531. return;
  4532. }
  4533. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4534. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4535. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  4536. const int ib = i00/qk; // block index
  4537. const int iqs = (i00%qk)/qr; // quant index
  4538. const int iybs = i00 - i00%qk; // dst block start index
  4539. const int y_offset = qr == 1 ? 1 : qk/2;
  4540. // dequantize
  4541. dfloat2 v;
  4542. dequantize_kernel(src0_row, ib, iqs, v);
  4543. dst_row[iybs + iqs + 0] = v.x();
  4544. dst_row[iybs + iqs + y_offset] = v.y();
  4545. }
  4546. template<typename src0_t, typename dst_t>
  4547. static void k_get_rows_float(
  4548. const src0_t * src0, const int32_t * src1, dst_t * dst,
  4549. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4550. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4551. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4552. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4553. size_t s10, size_t s11, size_t s12,
  4554. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4555. const int i00 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4556. item_ct1.get_local_id(2);
  4557. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4558. item_ct1.get_local_id(1);
  4559. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4560. item_ct1.get_local_id(0)) /
  4561. ne12;
  4562. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4563. item_ct1.get_local_id(0)) %
  4564. ne12;
  4565. if (i00 >= ne00) {
  4566. return;
  4567. }
  4568. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4569. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4570. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  4571. dst_row[i00] = src0_row[i00];
  4572. }
  4573. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4574. static void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4575. const sycl::nd_item<3> &item_ct1) {
  4576. const int i = 2 * (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4577. item_ct1.get_local_id(2));
  4578. if (i >= k) {
  4579. return;
  4580. }
  4581. const int ib = i/qk; // block index
  4582. const int iqs = (i%qk)/qr; // quant index
  4583. const int iybs = i - i%qk; // y block start index
  4584. const int y_offset = qr == 1 ? 1 : qk/2;
  4585. // dequantize
  4586. dfloat2 v;
  4587. dequantize_kernel(vx, ib, iqs, v);
  4588. y[iybs + iqs + 0] = v.x();
  4589. y[iybs + iqs + y_offset] = v.y();
  4590. }
  4591. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  4592. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  4593. #define VDR_Q4_0_Q8_1_MMVQ 2
  4594. #define VDR_Q4_0_Q8_1_MMQ 4
  4595. template <int vdr>
  4596. static __dpct_inline__ float vec_dot_q4_0_q8_1_impl(const int *v, const int *u,
  4597. const float &d4,
  4598. const sycl::half2 &ds8) {
  4599. int sumi = 0;
  4600. #pragma unroll
  4601. for (int i = 0; i < vdr; ++i) {
  4602. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4603. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4604. // SIMD dot product of quantized values
  4605. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4606. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4607. }
  4608. const sycl::float2 ds8f =
  4609. ds8.convert<float, sycl::rounding_mode::automatic>();
  4610. // second part effectively subtracts 8 from each quant value
  4611. return d4 * (sumi * ds8f.x() - (8 * vdr / QI4_0) * ds8f.y());
  4612. }
  4613. #define VDR_Q4_1_Q8_1_MMVQ 2
  4614. #define VDR_Q4_1_Q8_1_MMQ 4
  4615. template <int vdr>
  4616. static __dpct_inline__ float vec_dot_q4_1_q8_1_impl(const int *v, const int *u,
  4617. const sycl::half2 &dm4,
  4618. const sycl::half2 &ds8) {
  4619. int sumi = 0;
  4620. #pragma unroll
  4621. for (int i = 0; i < vdr; ++i) {
  4622. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4623. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4624. // SIMD dot product of quantized values
  4625. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4626. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4627. }
  4628. #ifdef GGML_SYCL_F16
  4629. const sycl::float2 tmp =
  4630. (dm4 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4631. const float d4d8 = tmp.x();
  4632. const float m4s8 = tmp.y();
  4633. #else
  4634. const sycl::float2 dm4f =
  4635. dm4.convert<float, sycl::rounding_mode::automatic>();
  4636. const sycl::float2 ds8f =
  4637. ds8.convert<float, sycl::rounding_mode::automatic>();
  4638. const float d4d8 = dm4f.x() * ds8f.x();
  4639. const float m4s8 = dm4f.y() * ds8f.y();
  4640. #endif // GGML_SYCL_F16
  4641. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  4642. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  4643. }
  4644. #define VDR_Q5_0_Q8_1_MMVQ 2
  4645. #define VDR_Q5_0_Q8_1_MMQ 4
  4646. template <int vdr>
  4647. static __dpct_inline__ float
  4648. vec_dot_q5_0_q8_1_impl(const int *vl, const int *vh, const int *u,
  4649. const float &d5, const sycl::half2 &ds8) {
  4650. int sumi = 0;
  4651. #pragma unroll
  4652. for (int i = 0; i < vdr; ++i) {
  4653. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4654. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4655. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4656. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4657. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4658. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4659. sumi); // SIMD dot product of quantized values
  4660. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4661. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4662. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4663. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4664. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4665. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4666. sumi); // SIMD dot product of quantized values
  4667. }
  4668. const sycl::float2 ds8f =
  4669. ds8.convert<float, sycl::rounding_mode::automatic>();
  4670. // second part effectively subtracts 16 from each quant value
  4671. return d5 * (sumi * ds8f.x() - (16 * vdr / QI5_0) * ds8f.y());
  4672. }
  4673. #define VDR_Q5_1_Q8_1_MMVQ 2
  4674. #define VDR_Q5_1_Q8_1_MMQ 4
  4675. template <int vdr>
  4676. static __dpct_inline__ float
  4677. vec_dot_q5_1_q8_1_impl(const int *vl, const int *vh, const int *u,
  4678. const sycl::half2 &dm5, const sycl::half2 &ds8) {
  4679. int sumi = 0;
  4680. #pragma unroll
  4681. for (int i = 0; i < vdr; ++i) {
  4682. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4683. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4684. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4685. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4686. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4687. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4688. sumi); // SIMD dot product of quantized values
  4689. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4690. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4691. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4692. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4693. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4694. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4695. sumi); // SIMD dot product of quantized values
  4696. }
  4697. #ifdef GGML_SYCL_F16
  4698. const sycl::float2 tmp =
  4699. (dm5 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4700. const float d5d8 = tmp.x();
  4701. const float m5s8 = tmp.y();
  4702. #else
  4703. const sycl::float2 dm5f =
  4704. dm5.convert<float, sycl::rounding_mode::automatic>();
  4705. const sycl::float2 ds8f =
  4706. ds8.convert<float, sycl::rounding_mode::automatic>();
  4707. const float d5d8 = dm5f.x() * ds8f.x();
  4708. const float m5s8 = dm5f.y() * ds8f.y();
  4709. #endif // GGML_SYCL_F16
  4710. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  4711. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  4712. }
  4713. #define VDR_Q8_0_Q8_1_MMVQ 2
  4714. #define VDR_Q8_0_Q8_1_MMQ 8
  4715. template <int vdr>
  4716. static __dpct_inline__ float vec_dot_q8_0_q8_1_impl(const int *v, const int *u,
  4717. const float &d8_0,
  4718. const float &d8_1) {
  4719. int sumi = 0;
  4720. #pragma unroll
  4721. for (int i = 0; i < vdr; ++i) {
  4722. // SIMD dot product of quantized values
  4723. sumi = dpct::dp4a(v[i], u[i], sumi);
  4724. }
  4725. return d8_0*d8_1 * sumi;
  4726. }
  4727. template <int vdr>
  4728. static __dpct_inline__ float vec_dot_q8_1_q8_1_impl(const int *v, const int *u,
  4729. const sycl::half2 &dm8,
  4730. const sycl::half2 &ds8) {
  4731. int sumi = 0;
  4732. #pragma unroll
  4733. for (int i = 0; i < vdr; ++i) {
  4734. // SIMD dot product of quantized values
  4735. sumi = dpct::dp4a(v[i], u[i], sumi);
  4736. }
  4737. #ifdef GGML_SYCL_F16
  4738. const sycl::float2 tmp =
  4739. (dm8 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4740. const float d8d8 = tmp.x();
  4741. const float m8s8 = tmp.y();
  4742. #else
  4743. const sycl::float2 dm8f =
  4744. dm8.convert<float, sycl::rounding_mode::automatic>();
  4745. const sycl::float2 ds8f =
  4746. ds8.convert<float, sycl::rounding_mode::automatic>();
  4747. const float d8d8 = dm8f.x() * ds8f.x();
  4748. const float m8s8 = dm8f.y() * ds8f.y();
  4749. #endif // GGML_SYCL_F16
  4750. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  4751. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  4752. }
  4753. #define VDR_Q2_K_Q8_1_MMVQ 1
  4754. #define VDR_Q2_K_Q8_1_MMQ 2
  4755. // contiguous v/x values
  4756. static __dpct_inline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  4757. const int &v, const int *__restrict__ u, const uint8_t *__restrict__ scales,
  4758. const sycl::half2 &dm2, const float *__restrict__ d8) {
  4759. float sumf_d = 0.0f;
  4760. float sumf_m = 0.0f;
  4761. #pragma unroll
  4762. for (int i = 0; i < QR2_K; ++i) {
  4763. const int sc = scales[2*i];
  4764. const int vi = (v >> (2*i)) & 0x03030303;
  4765. sumf_d +=
  4766. d8[i] * (dpct::dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  4767. // fill int with 4x m
  4768. int m = sc >> 4;
  4769. m |= m << 8;
  4770. m |= m << 16;
  4771. sumf_m += d8[i] *
  4772. dpct::dp4a(
  4773. m, u[i],
  4774. 0); // multiply constant q2_K part with sum of q8_1 values
  4775. }
  4776. const sycl::float2 dm2f =
  4777. dm2.convert<float, sycl::rounding_mode::automatic>();
  4778. return dm2f.x() * sumf_d - dm2f.y() * sumf_m;
  4779. }
  4780. // contiguous u/y values
  4781. static __dpct_inline__ float
  4782. vec_dot_q2_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4783. const uint8_t *__restrict__ scales,
  4784. const sycl::half2 &dm2, const float &d8) {
  4785. int sumi_d = 0;
  4786. int sumi_m = 0;
  4787. #pragma unroll
  4788. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  4789. int sumi_d_sc = 0;
  4790. const int sc = scales[i0 / (QI8_1/2)];
  4791. // fill int with 4x m
  4792. int m = sc >> 4;
  4793. m |= m << 8;
  4794. m |= m << 16;
  4795. #pragma unroll
  4796. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  4797. sumi_d_sc = dpct::dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  4798. sumi_m = dpct::dp4a(m, u[i],
  4799. sumi_m); // multiply sum of q8_1 values with m
  4800. }
  4801. sumi_d += sumi_d_sc * (sc & 0xF);
  4802. }
  4803. const sycl::float2 dm2f =
  4804. dm2.convert<float, sycl::rounding_mode::automatic>();
  4805. return d8 * (dm2f.x() * sumi_d - dm2f.y() * sumi_m);
  4806. }
  4807. #define VDR_Q3_K_Q8_1_MMVQ 1
  4808. #define VDR_Q3_K_Q8_1_MMQ 2
  4809. // contiguous v/x values
  4810. static __dpct_inline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  4811. const int &vl, const int &vh, const int *__restrict__ u,
  4812. const uint8_t *__restrict__ scales, const int &scale_offset,
  4813. const float &d3, const float *__restrict__ d8) {
  4814. float sumf = 0.0f;
  4815. #pragma unroll
  4816. for (int i = 0; i < QR3_K; ++i) {
  4817. const int isc = scale_offset + 2*i;
  4818. const int isc_low = isc % (QK_K/32);
  4819. const int sc_shift_low = 4 * (isc / (QK_K/32));
  4820. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  4821. const int isc_high = isc % (QK_K/64);
  4822. const int sc_shift_high = 2 * (isc / (QK_K/64));
  4823. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  4824. const int sc = (sc_low | sc_high) - 32;
  4825. const int vil = (vl >> (2*i)) & 0x03030303;
  4826. const int vih = ((vh >> i) << 2) & 0x04040404;
  4827. const int vi =
  4828. dpct::vectorized_binary<sycl::char4>(vil, vih, dpct::sub_sat());
  4829. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  4830. }
  4831. return d3 * sumf;
  4832. }
  4833. // contiguous u/y values
  4834. static __dpct_inline__ float
  4835. vec_dot_q3_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4836. const int8_t *__restrict__ scales, const float &d3,
  4837. const float &d8) {
  4838. int sumi = 0;
  4839. #pragma unroll
  4840. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  4841. int sumi_sc = 0;
  4842. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  4843. sumi_sc = dpct::dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  4844. }
  4845. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  4846. }
  4847. return d3*d8 * sumi;
  4848. }
  4849. #define VDR_Q4_K_Q8_1_MMVQ 2
  4850. #define VDR_Q4_K_Q8_1_MMQ 8
  4851. // contiguous v/x values
  4852. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  4853. const int *__restrict__ v, const int *__restrict__ u,
  4854. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4855. const sycl::half2 &dm4, const float *__restrict__ d8) {
  4856. float sumf_d = 0.0f;
  4857. float sumf_m = 0.0f;
  4858. #pragma unroll
  4859. for (int i = 0; i < QR4_K; ++i) {
  4860. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  4861. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  4862. const int dot1 =
  4863. dpct::dp4a(v1i, u[2 * i + 1],
  4864. dpct::dp4a(v0i, u[2 * i + 0], 0)); // SIMD dot product
  4865. const int dot2 =
  4866. dpct::dp4a(0x01010101, u[2 * i + 1],
  4867. dpct::dp4a(0x01010101, u[2 * i + 0], 0)); // sum of u
  4868. sumf_d += d8[i] * (dot1 * sc[i]);
  4869. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  4870. }
  4871. const sycl::float2 dm4f =
  4872. dm4.convert<float, sycl::rounding_mode::automatic>();
  4873. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4874. }
  4875. // contiguous u/y values
  4876. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_mmq(
  4877. const int *__restrict__ v, const int *__restrict__ u,
  4878. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4879. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  4880. float sumf_d = 0.0f;
  4881. float sumf_m = 0.0f;
  4882. #pragma unroll
  4883. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  4884. int sumi_d = 0;
  4885. #pragma unroll
  4886. for (int j = 0; j < QI8_1; ++j) {
  4887. sumi_d = dpct::dp4a((v[j] >> (4 * i)) & 0x0F0F0F0F,
  4888. u[i * QI8_1 + j], sumi_d); // SIMD dot product
  4889. }
  4890. const sycl::float2 ds8f =
  4891. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  4892. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  4893. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  4894. }
  4895. const sycl::float2 dm4f =
  4896. dm4.convert<float, sycl::rounding_mode::automatic>();
  4897. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4898. }
  4899. #define VDR_Q5_K_Q8_1_MMVQ 2
  4900. #define VDR_Q5_K_Q8_1_MMQ 8
  4901. // contiguous v/x values
  4902. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  4903. const int *__restrict__ vl, const int *__restrict__ vh,
  4904. const int *__restrict__ u, const uint8_t *__restrict__ sc,
  4905. const uint8_t *__restrict__ m, const sycl::half2 &dm5,
  4906. const float *__restrict__ d8) {
  4907. float sumf_d = 0.0f;
  4908. float sumf_m = 0.0f;
  4909. #pragma unroll
  4910. for (int i = 0; i < QR5_K; ++i) {
  4911. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  4912. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  4913. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  4914. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  4915. const int v0i = vl0i | vh0i;
  4916. const int v1i = vl1i | vh1i;
  4917. const int dot1 =
  4918. dpct::dp4a(v0i, u[2 * i + 0],
  4919. dpct::dp4a(v1i, u[2 * i + 1], 0)); // SIMD dot product
  4920. const int dot2 =
  4921. dpct::dp4a(0x01010101, u[2 * i + 0],
  4922. dpct::dp4a(0x01010101, u[2 * i + 1], 0)); // sum of u
  4923. sumf_d += d8[i] * (dot1 * sc[i]);
  4924. sumf_m += d8[i] * (dot2 * m[i]);
  4925. }
  4926. const sycl::float2 dm5f =
  4927. dm5.convert<float, sycl::rounding_mode::automatic>();
  4928. return dm5f.x() * sumf_d - dm5f.y() * sumf_m;
  4929. }
  4930. // contiguous u/y values
  4931. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_mmq(
  4932. const int *__restrict__ v, const int *__restrict__ u,
  4933. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  4934. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  4935. float sumf_d = 0.0f;
  4936. float sumf_m = 0.0f;
  4937. #pragma unroll
  4938. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  4939. int sumi_d = 0;
  4940. #pragma unroll
  4941. for (int j = 0; j < QI8_1; ++j) {
  4942. sumi_d = dpct::dp4a(v[i * QI8_1 + j], u[i * QI8_1 + j],
  4943. sumi_d); // SIMD dot product
  4944. }
  4945. const sycl::float2 ds8f =
  4946. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  4947. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  4948. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  4949. }
  4950. const sycl::float2 dm4f =
  4951. dm4.convert<float, sycl::rounding_mode::automatic>();
  4952. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  4953. }
  4954. #define VDR_Q6_K_Q8_1_MMVQ 1
  4955. #define VDR_Q6_K_Q8_1_MMQ 8
  4956. // contiguous v/x values
  4957. static __dpct_inline__ float
  4958. vec_dot_q6_K_q8_1_impl_mmvq(const int &vl, const int &vh,
  4959. const int *__restrict__ u,
  4960. const int8_t *__restrict__ scales, const float &d,
  4961. const float *__restrict__ d8) {
  4962. float sumf = 0.0f;
  4963. #pragma unroll
  4964. for (int i = 0; i < QR6_K; ++i) {
  4965. const int sc = scales[4*i];
  4966. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  4967. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  4968. const int vi = dpct::vectorized_binary<sycl::char4>(
  4969. (vil | vih), 0x20202020, dpct::sub_sat()); // vi = (vil | vih) - 32
  4970. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  4971. }
  4972. return d*sumf;
  4973. }
  4974. // contiguous u/y values
  4975. static __dpct_inline__ float
  4976. vec_dot_q6_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4977. const int8_t *__restrict__ sc, const float &d6,
  4978. const float *__restrict__ d8) {
  4979. float sumf_d = 0.0f;
  4980. #pragma unroll
  4981. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  4982. sycl::int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  4983. #pragma unroll
  4984. for (int i = i0; i < i0 + 2; ++i) {
  4985. sumi_d.x() = dpct::dp4a(v[2 * i + 0], u[2 * i + 0],
  4986. sumi_d.x()); // SIMD dot product
  4987. sumi_d.x() = dpct::dp4a(v[2 * i + 1], u[2 * i + 1],
  4988. sumi_d.x()); // SIMD dot product
  4989. sumi_d.y() = dpct::dp4a(v[2 * i + 4], u[2 * i + 4],
  4990. sumi_d.y()); // SIMD dot product
  4991. sumi_d.y() = dpct::dp4a(v[2 * i + 5], u[2 * i + 5],
  4992. sumi_d.y()); // SIMD dot product
  4993. }
  4994. sumf_d += d8[i0 / 4] *
  4995. (sc[i0 / 2 + 0] * sumi_d.x() + sc[i0 / 2 + 1] * sumi_d.y());
  4996. }
  4997. return d6 * sumf_d;
  4998. }
  4999. static __dpct_inline__ float
  5000. vec_dot_q4_0_q8_1(const void *__restrict__ vbq,
  5001. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5002. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  5003. int v[VDR_Q4_0_Q8_1_MMVQ];
  5004. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  5005. #pragma unroll
  5006. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  5007. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  5008. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5009. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  5010. }
  5011. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  5012. }
  5013. template <int mmq_y>
  5014. static __dpct_inline__ void
  5015. allocate_tiles_q4_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5016. int *tile_x_qs_q4_0, float *tile_x_d_q4_0) {
  5017. (void)x_qh; (void)x_sc;
  5018. *x_ql = tile_x_qs_q4_0;
  5019. *x_dm = (sycl::half2 *)tile_x_d_q4_0;
  5020. }
  5021. template <int mmq_y, int nwarps, bool need_check>
  5022. static __dpct_inline__ void
  5023. load_tiles_q4_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5024. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5025. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5026. const int &k, const int &blocks_per_row) {
  5027. (void)x_qh; (void)x_sc;
  5028. GGML_SYCL_ASSUME(i_offset >= 0);
  5029. GGML_SYCL_ASSUME(i_offset < nwarps);
  5030. GGML_SYCL_ASSUME(k >= 0);
  5031. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5032. const int kbx = k / QI4_0;
  5033. const int kqsx = k % QI4_0;
  5034. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  5035. float * x_dmf = (float *) x_dm;
  5036. #pragma unroll
  5037. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5038. int i = i0 + i_offset;
  5039. if (need_check) {
  5040. i = sycl::min(i, i_max);
  5041. }
  5042. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5043. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5044. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  5045. }
  5046. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  5047. const int kbxd = k % blocks_per_tile_x_row;
  5048. #pragma unroll
  5049. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  5050. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  5051. if (need_check) {
  5052. i = sycl::min(i, i_max);
  5053. }
  5054. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5055. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  5056. }
  5057. }
  5058. static __dpct_inline__ float vec_dot_q4_0_q8_1_mul_mat(
  5059. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5060. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5061. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5062. const int &i, const int &j, const int &k) {
  5063. (void)x_qh; (void)x_sc;
  5064. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5065. const float * x_dmf = (const float *) x_dm;
  5066. int u[2*VDR_Q4_0_Q8_1_MMQ];
  5067. #pragma unroll
  5068. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  5069. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5070. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  5071. }
  5072. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  5073. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  5074. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5075. }
  5076. static __dpct_inline__ float
  5077. vec_dot_q4_1_q8_1(const void *__restrict__ vbq,
  5078. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5079. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  5080. int v[VDR_Q4_1_Q8_1_MMVQ];
  5081. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  5082. #pragma unroll
  5083. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  5084. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  5085. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5086. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  5087. }
  5088. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  5089. }
  5090. template <int mmq_y>
  5091. static __dpct_inline__ void
  5092. allocate_tiles_q4_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5093. int *tile_x_qs_q4_1, sycl::half2 *tile_x_dm_q4_1) {
  5094. (void)x_qh; (void)x_sc;
  5095. *x_ql = tile_x_qs_q4_1;
  5096. *x_dm = tile_x_dm_q4_1;
  5097. }
  5098. template <int mmq_y, int nwarps, bool need_check>
  5099. static __dpct_inline__ void
  5100. load_tiles_q4_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5101. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5102. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5103. const int &k, const int &blocks_per_row) {
  5104. (void)x_qh; (void)x_sc;
  5105. GGML_SYCL_ASSUME(i_offset >= 0);
  5106. GGML_SYCL_ASSUME(i_offset < nwarps);
  5107. GGML_SYCL_ASSUME(k >= 0);
  5108. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5109. const int kbx = k / QI4_1;
  5110. const int kqsx = k % QI4_1;
  5111. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  5112. #pragma unroll
  5113. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5114. int i = i0 + i_offset;
  5115. if (need_check) {
  5116. i = sycl::min(i, i_max);
  5117. }
  5118. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5119. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5120. }
  5121. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  5122. const int kbxd = k % blocks_per_tile_x_row;
  5123. #pragma unroll
  5124. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  5125. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  5126. if (need_check) {
  5127. i = sycl::min(i, i_max);
  5128. }
  5129. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5130. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  5131. }
  5132. }
  5133. static __dpct_inline__ float vec_dot_q4_1_q8_1_mul_mat(
  5134. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5135. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5136. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5137. const int &i, const int &j, const int &k) {
  5138. (void)x_qh; (void)x_sc;
  5139. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5140. int u[2*VDR_Q4_1_Q8_1_MMQ];
  5141. #pragma unroll
  5142. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  5143. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5144. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  5145. }
  5146. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  5147. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  5148. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5149. }
  5150. static __dpct_inline__ float
  5151. vec_dot_q5_0_q8_1(const void *__restrict__ vbq,
  5152. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5153. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  5154. int vl[VDR_Q5_0_Q8_1_MMVQ];
  5155. int vh[VDR_Q5_0_Q8_1_MMVQ];
  5156. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  5157. #pragma unroll
  5158. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  5159. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  5160. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  5161. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5162. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  5163. }
  5164. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  5165. }
  5166. template <int mmq_y>
  5167. static __dpct_inline__ void
  5168. allocate_tiles_q5_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5169. int *tile_x_ql_q5_0, float *tile_x_d_q5_0) {
  5170. (void)x_qh; (void)x_sc;
  5171. *x_ql = tile_x_ql_q5_0;
  5172. *x_dm = (sycl::half2 *)tile_x_d_q5_0;
  5173. }
  5174. template <int mmq_y, int nwarps, bool need_check>
  5175. static __dpct_inline__ void
  5176. load_tiles_q5_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5177. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5178. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5179. const int &k, const int &blocks_per_row) {
  5180. (void)x_qh; (void)x_sc;
  5181. GGML_SYCL_ASSUME(i_offset >= 0);
  5182. GGML_SYCL_ASSUME(i_offset < nwarps);
  5183. GGML_SYCL_ASSUME(k >= 0);
  5184. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5185. const int kbx = k / QI5_0;
  5186. const int kqsx = k % QI5_0;
  5187. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  5188. #pragma unroll
  5189. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5190. int i = i0 + i_offset;
  5191. if (need_check) {
  5192. i = sycl::min(i, i_max);
  5193. }
  5194. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5195. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  5196. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  5197. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5198. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5199. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5200. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5201. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5202. qs0 = dpct::vectorized_binary<sycl::char4>(
  5203. qs0, 0x10101010, dpct::sub_sat()); // subtract 16
  5204. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5205. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5206. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5207. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5208. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5209. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5210. qs1 = dpct::vectorized_binary<sycl::char4>(
  5211. qs1, 0x10101010, dpct::sub_sat()); // subtract 16
  5212. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5213. }
  5214. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  5215. const int kbxd = k % blocks_per_tile_x_row;
  5216. float * x_dmf = (float *) x_dm;
  5217. #pragma unroll
  5218. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  5219. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  5220. if (need_check) {
  5221. i = sycl::min(i, i_max);
  5222. }
  5223. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5224. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  5225. }
  5226. }
  5227. static __dpct_inline__ float vec_dot_q5_0_q8_1_mul_mat(
  5228. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5229. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5230. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5231. const int &i, const int &j, const int &k) {
  5232. (void)x_qh; (void)x_sc;
  5233. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5234. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  5235. const float * x_dmf = (const float *) x_dm;
  5236. const float * y_df = (const float *) y_ds;
  5237. int u[2*VDR_Q5_0_Q8_1_MMQ];
  5238. #pragma unroll
  5239. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  5240. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5241. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  5242. }
  5243. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  5244. (&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)]);
  5245. }
  5246. static __dpct_inline__ float
  5247. vec_dot_q5_1_q8_1(const void *__restrict__ vbq,
  5248. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5249. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  5250. int vl[VDR_Q5_1_Q8_1_MMVQ];
  5251. int vh[VDR_Q5_1_Q8_1_MMVQ];
  5252. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  5253. #pragma unroll
  5254. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  5255. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  5256. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  5257. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5258. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  5259. }
  5260. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  5261. }
  5262. template <int mmq_y>
  5263. static __dpct_inline__ void
  5264. allocate_tiles_q5_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5265. int *tile_x_ql_q5_1, sycl::half2 *tile_x_dm_q5_1) {
  5266. (void)x_qh; (void)x_sc;
  5267. *x_ql = tile_x_ql_q5_1;
  5268. *x_dm = tile_x_dm_q5_1;
  5269. }
  5270. template <int mmq_y, int nwarps, bool need_check>
  5271. static __dpct_inline__ void
  5272. load_tiles_q5_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5273. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5274. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5275. const int &k, const int &blocks_per_row) {
  5276. (void)x_qh; (void)x_sc;
  5277. GGML_SYCL_ASSUME(i_offset >= 0);
  5278. GGML_SYCL_ASSUME(i_offset < nwarps);
  5279. GGML_SYCL_ASSUME(k >= 0);
  5280. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5281. const int kbx = k / QI5_1;
  5282. const int kqsx = k % QI5_1;
  5283. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  5284. #pragma unroll
  5285. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5286. int i = i0 + i_offset;
  5287. if (need_check) {
  5288. i = sycl::min(i, i_max);
  5289. }
  5290. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5291. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5292. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  5293. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5294. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5295. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5296. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5297. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5298. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5299. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5300. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5301. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5302. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5303. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5304. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5305. }
  5306. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  5307. const int kbxd = k % blocks_per_tile_x_row;
  5308. #pragma unroll
  5309. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  5310. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  5311. if (need_check) {
  5312. i = sycl::min(i, i_max);
  5313. }
  5314. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5315. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  5316. }
  5317. }
  5318. static __dpct_inline__ float vec_dot_q5_1_q8_1_mul_mat(
  5319. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5320. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5321. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5322. const int &i, const int &j, const int &k) {
  5323. (void)x_qh; (void)x_sc;
  5324. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5325. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  5326. int u[2*VDR_Q5_1_Q8_1_MMQ];
  5327. #pragma unroll
  5328. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  5329. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5330. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  5331. }
  5332. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  5333. (&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)]);
  5334. }
  5335. static __dpct_inline__ float
  5336. vec_dot_q8_0_q8_1(const void *__restrict__ vbq,
  5337. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5338. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  5339. int v[VDR_Q8_0_Q8_1_MMVQ];
  5340. int u[VDR_Q8_0_Q8_1_MMVQ];
  5341. #pragma unroll
  5342. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  5343. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  5344. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5345. }
  5346. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d,
  5347. bq8_1->ds[0]);
  5348. }
  5349. template <int mmq_y>
  5350. static __dpct_inline__ void
  5351. allocate_tiles_q8_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5352. int *tile_x_qs_q8_0, float *tile_x_d_q8_0) {
  5353. (void)x_qh; (void)x_sc;
  5354. *x_ql = tile_x_qs_q8_0;
  5355. *x_dm = (sycl::half2 *)tile_x_d_q8_0;
  5356. }
  5357. template <int mmq_y, int nwarps, bool need_check>
  5358. static __dpct_inline__ void
  5359. load_tiles_q8_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5360. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5361. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5362. const int &k, const int &blocks_per_row) {
  5363. (void)x_qh; (void)x_sc;
  5364. GGML_SYCL_ASSUME(i_offset >= 0);
  5365. GGML_SYCL_ASSUME(i_offset < nwarps);
  5366. GGML_SYCL_ASSUME(k >= 0);
  5367. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5368. const int kbx = k / QI8_0;
  5369. const int kqsx = k % QI8_0;
  5370. float * x_dmf = (float *) x_dm;
  5371. const block_q8_0 * bx0 = (const block_q8_0 *) vx;
  5372. #pragma unroll
  5373. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5374. int i = i0 + i_offset;
  5375. if (need_check) {
  5376. i = sycl::min(i, i_max);
  5377. }
  5378. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5379. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  5380. }
  5381. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  5382. const int kbxd = k % blocks_per_tile_x_row;
  5383. #pragma unroll
  5384. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  5385. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  5386. if (need_check) {
  5387. i = sycl::min(i, i_max);
  5388. }
  5389. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5390. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  5391. }
  5392. }
  5393. static __dpct_inline__ float vec_dot_q8_0_q8_1_mul_mat(
  5394. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5395. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5396. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5397. const int &i, const int &j, const int &k) {
  5398. (void)x_qh; (void)x_sc;
  5399. const float * x_dmf = (const float *) x_dm;
  5400. const float * y_df = (const float *) y_ds;
  5401. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  5402. (&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],
  5403. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  5404. }
  5405. static __dpct_inline__ float
  5406. vec_dot_q2_K_q8_1(const void *__restrict__ vbq,
  5407. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5408. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  5409. const int bq8_offset = QR2_K * (iqs / QI8_1);
  5410. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5411. const uint8_t * scales = bq2_K->scales + scale_offset;
  5412. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  5413. int u[QR2_K];
  5414. float d8[QR2_K];
  5415. #pragma unroll
  5416. for (int i = 0; i < QR2_K; ++ i) {
  5417. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5418. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5419. }
  5420. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  5421. }
  5422. template <int mmq_y>
  5423. static __dpct_inline__ void
  5424. allocate_tiles_q2_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5425. int *tile_x_ql_q2_K, sycl::half2 *tile_x_dm_q2_K,
  5426. int *tile_x_sc_q2_K) {
  5427. (void)x_qh;
  5428. *x_ql = tile_x_ql_q2_K;
  5429. *x_dm = tile_x_dm_q2_K;
  5430. *x_sc = tile_x_sc_q2_K;
  5431. }
  5432. template <int mmq_y, int nwarps, bool need_check>
  5433. static __dpct_inline__ void
  5434. load_tiles_q2_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5435. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5436. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5437. const int &k, const int &blocks_per_row) {
  5438. (void)x_qh;
  5439. GGML_SYCL_ASSUME(i_offset >= 0);
  5440. GGML_SYCL_ASSUME(i_offset < nwarps);
  5441. GGML_SYCL_ASSUME(k >= 0);
  5442. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5443. const int kbx = k / QI2_K;
  5444. const int kqsx = k % QI2_K;
  5445. const block_q2_K * bx0 = (const block_q2_K *) vx;
  5446. #pragma unroll
  5447. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5448. int i = i0 + i_offset;
  5449. if (need_check) {
  5450. i = sycl::min(i, i_max);
  5451. }
  5452. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  5453. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5454. }
  5455. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  5456. const int kbxd = k % blocks_per_tile_x_row;
  5457. #pragma unroll
  5458. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  5459. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  5460. if (need_check) {
  5461. i = sycl::min(i, i_max);
  5462. }
  5463. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5464. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  5465. }
  5466. #pragma unroll
  5467. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5468. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5469. if (need_check) {
  5470. i = sycl::min(i, i_max);
  5471. }
  5472. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  5473. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  5474. }
  5475. }
  5476. static __dpct_inline__ float vec_dot_q2_K_q8_1_mul_mat(
  5477. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5478. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5479. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5480. const int &i, const int &j, const int &k) {
  5481. (void)x_qh;
  5482. const int kbx = k / QI2_K;
  5483. const int ky = (k % QI2_K) * QR2_K;
  5484. const float * y_df = (const float *) y_ds;
  5485. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  5486. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  5487. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  5488. #pragma unroll
  5489. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  5490. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5491. }
  5492. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  5493. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  5494. 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]);
  5495. }
  5496. static __dpct_inline__ float
  5497. vec_dot_q3_K_q8_1(const void *__restrict__ vbq,
  5498. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5499. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  5500. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  5501. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5502. const float d = bq3_K->d;
  5503. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  5504. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5505. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  5506. int u[QR3_K];
  5507. float d8[QR3_K];
  5508. #pragma unroll
  5509. for (int i = 0; i < QR3_K; ++i) {
  5510. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5511. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5512. }
  5513. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  5514. }
  5515. template <int mmq_y>
  5516. static __dpct_inline__ void
  5517. allocate_tiles_q3_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5518. int *tile_x_ql_q3_K, sycl::half2 *tile_x_dm_q3_K,
  5519. int *tile_x_qh_q3_K, int *tile_x_sc_q3_K) {
  5520. *x_ql = tile_x_ql_q3_K;
  5521. *x_dm = tile_x_dm_q3_K;
  5522. *x_qh = tile_x_qh_q3_K;
  5523. *x_sc = tile_x_sc_q3_K;
  5524. }
  5525. template <int mmq_y, int nwarps, bool need_check>
  5526. static __dpct_inline__ void
  5527. load_tiles_q3_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5528. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5529. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5530. const int &k, const int &blocks_per_row) {
  5531. GGML_SYCL_ASSUME(i_offset >= 0);
  5532. GGML_SYCL_ASSUME(i_offset < nwarps);
  5533. GGML_SYCL_ASSUME(k >= 0);
  5534. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5535. const int kbx = k / QI3_K;
  5536. const int kqsx = k % QI3_K;
  5537. const block_q3_K * bx0 = (const block_q3_K *) vx;
  5538. #pragma unroll
  5539. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5540. int i = i0 + i_offset;
  5541. if (need_check) {
  5542. i = sycl::min(i, i_max);
  5543. }
  5544. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  5545. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5546. }
  5547. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  5548. const int kbxd = k % blocks_per_tile_x_row;
  5549. float * x_dmf = (float *) x_dm;
  5550. #pragma unroll
  5551. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  5552. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  5553. if (need_check) {
  5554. i = sycl::min(i, i_max);
  5555. }
  5556. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5557. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  5558. }
  5559. #pragma unroll
  5560. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  5561. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  5562. if (need_check) {
  5563. i = sycl::min(i, i_max);
  5564. }
  5565. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  5566. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5567. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  5568. }
  5569. #pragma unroll
  5570. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5571. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5572. if (need_check) {
  5573. i = sycl::min(i, i_max);
  5574. }
  5575. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  5576. const int ksc = k % (QI3_K/4);
  5577. const int ksc_low = ksc % (QI3_K/8);
  5578. const int shift_low = 4 * (ksc / (QI3_K/8));
  5579. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  5580. const int ksc_high = QI3_K/8;
  5581. const int shift_high = 2 * ksc;
  5582. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  5583. const int sc = dpct::vectorized_binary<sycl::char4>(
  5584. sc_low | sc_high, 0x20202020, dpct::sub_sat());
  5585. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  5586. }
  5587. }
  5588. static __dpct_inline__ float vec_dot_q3_K_q8_1_mul_mat(
  5589. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5590. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5591. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5592. const int &i, const int &j, const int &k) {
  5593. const int kbx = k / QI3_K;
  5594. const int ky = (k % QI3_K) * QR3_K;
  5595. const float * x_dmf = (const float *) x_dm;
  5596. const float * y_df = (const float *) y_ds;
  5597. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  5598. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  5599. #pragma unroll
  5600. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  5601. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  5602. const int shift = 2 * ((ky % 32) / 8);
  5603. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5604. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  5605. const int vlh = (vh << 2) & 0x04040404;
  5606. v[l] = dpct::vectorized_binary<sycl::char4>(vll, vlh, dpct::sub_sat());
  5607. }
  5608. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  5609. 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]);
  5610. }
  5611. static __dpct_inline__ float
  5612. vec_dot_q4_K_q8_1(const void *__restrict__ vbq,
  5613. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5614. #ifndef GGML_QKK_64
  5615. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5616. int v[2];
  5617. int u[2*QR4_K];
  5618. float d8[QR4_K];
  5619. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  5620. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  5621. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  5622. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  5623. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  5624. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  5625. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5626. v[0] = q4[0];
  5627. v[1] = q4[4];
  5628. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  5629. uint16_t aux[2];
  5630. const int j = bq8_offset/2;
  5631. if (j < 2) {
  5632. aux[0] = scales[j+0] & 0x3f3f;
  5633. aux[1] = scales[j+2] & 0x3f3f;
  5634. } else {
  5635. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5636. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5637. }
  5638. const uint8_t * sc = (const uint8_t *)aux;
  5639. const uint8_t * m = sc + 2;
  5640. for (int i = 0; i < QR4_K; ++i) {
  5641. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5642. d8[i] = bq8i->ds[0];
  5643. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5644. u[2*i+0] = q8[0];
  5645. u[2*i+1] = q8[4];
  5646. }
  5647. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  5648. #else
  5649. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  5650. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5651. float sumf_d = 0.0f;
  5652. float sumf_m = 0.0f;
  5653. uint16_t aux16[2];
  5654. const uint8_t * s = (const uint8_t *)aux16;
  5655. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  5656. aux16[0] = a[0] & 0x0f0f;
  5657. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5658. const float dall = bq4_K->dm[0];
  5659. const float dmin = bq4_K->dm[1];
  5660. const float d8_1 = __low2float(bq8_1[0].ds);
  5661. const float d8_2 = __low2float(bq8_1[1].ds);
  5662. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  5663. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  5664. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  5665. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  5666. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  5667. const int v1 = q4[0];
  5668. const int v2 = q4[4];
  5669. const int dot1 = __dp4a(ui2, v2 & 0x0f0f0f0f, __dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  5670. const int dot2 = __dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, __dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  5671. const int dot3 = __dp4a(0x01010101, ui2, __dp4a(0x01010101, ui1, 0));
  5672. const int dot4 = __dp4a(0x01010101, ui4, __dp4a(0x01010101, ui3, 0));
  5673. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  5674. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  5675. return dall * sumf_d - dmin * sumf_m;
  5676. #else
  5677. bad_arch();
  5678. #endif // __SYCL_ARCH__ >= VER_4VEC
  5679. #endif
  5680. }
  5681. template <int mmq_y>
  5682. static __dpct_inline__ void
  5683. allocate_tiles_q4_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5684. int *tile_x_ql_q4_K, sycl::half2 *tile_x_dm_q4_K,
  5685. int *tile_x_sc_q4_K) {
  5686. (void)x_qh;
  5687. *x_ql = tile_x_ql_q4_K;
  5688. *x_dm = tile_x_dm_q4_K;
  5689. *x_sc = tile_x_sc_q4_K;
  5690. }
  5691. template <int mmq_y, int nwarps, bool need_check>
  5692. static __dpct_inline__ void
  5693. load_tiles_q4_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5694. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5695. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5696. const int &k, const int &blocks_per_row) {
  5697. (void)x_qh;
  5698. GGML_SYCL_ASSUME(i_offset >= 0);
  5699. GGML_SYCL_ASSUME(i_offset < nwarps);
  5700. GGML_SYCL_ASSUME(k >= 0);
  5701. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5702. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  5703. const int kqsx = k % QI4_K; // == k if QK_K == 256
  5704. const block_q4_K * bx0 = (const block_q4_K *) vx;
  5705. #pragma unroll
  5706. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5707. int i = i0 + i_offset;
  5708. if (need_check) {
  5709. i = sycl::min(i, i_max);
  5710. }
  5711. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  5712. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5713. }
  5714. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  5715. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5716. #pragma unroll
  5717. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  5718. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  5719. if (need_check) {
  5720. i = sycl::min(i, i_max);
  5721. }
  5722. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5723. #if QK_K == 256
  5724. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  5725. #else
  5726. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  5727. #endif
  5728. }
  5729. #pragma unroll
  5730. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5731. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5732. if (need_check) {
  5733. i = sycl::min(i, i_max);
  5734. }
  5735. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  5736. const int * scales = (const int *) bxi->scales;
  5737. const int ksc = k % (WARP_SIZE/8);
  5738. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5739. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5740. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5741. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5742. }
  5743. }
  5744. static __dpct_inline__ float vec_dot_q4_K_q8_1_mul_mat(
  5745. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5746. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5747. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5748. const int &i, const int &j, const int &k) {
  5749. (void)x_qh;
  5750. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  5751. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  5752. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  5753. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  5754. }
  5755. static __dpct_inline__ float
  5756. vec_dot_q5_K_q8_1(const void *__restrict__ vbq,
  5757. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5758. #ifndef GGML_QKK_64
  5759. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  5760. int vl[2];
  5761. int vh[2];
  5762. int u[2*QR5_K];
  5763. float d8[QR5_K];
  5764. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  5765. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5766. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  5767. vl[0] = ql[0];
  5768. vl[1] = ql[4];
  5769. vh[0] = qh[0] >> bq8_offset;
  5770. vh[1] = qh[4] >> bq8_offset;
  5771. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  5772. uint16_t aux[2];
  5773. const int j = bq8_offset/2;
  5774. if (j < 2) {
  5775. aux[0] = scales[j+0] & 0x3f3f;
  5776. aux[1] = scales[j+2] & 0x3f3f;
  5777. } else {
  5778. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5779. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5780. }
  5781. const uint8_t * sc = (const uint8_t *)aux;
  5782. const uint8_t * m = sc + 2;
  5783. #pragma unroll
  5784. for (int i = 0; i < QR5_K; ++i) {
  5785. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5786. d8[i] = bq8i->ds[0];
  5787. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5788. u[2*i+0] = q8[0];
  5789. u[2*i+1] = q8[4];
  5790. }
  5791. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  5792. #else
  5793. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  5794. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  5795. const int8_t * s = bq5_K->scales;
  5796. const float d = bq5_K->d;
  5797. const float d8_1 = __low2half(bq8_1[0].ds);
  5798. const float d8_2 = __low2half(bq8_1[1].ds);
  5799. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  5800. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  5801. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  5802. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  5803. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  5804. const int vl1 = ql[0];
  5805. const int vl2 = ql[4];
  5806. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  5807. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  5808. const int in = step%8; // 0, 4, 0, 4
  5809. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  5810. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  5811. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  5812. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  5813. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  5814. const float sumf_d = d8_1 * (__dp4a(ui1, v1, 0) * s[0] + __dp4a(ui2, v2, 0) * s[1])
  5815. + d8_2 * (__dp4a(ui3, v3, 0) * s[2] + __dp4a(ui4, v4, 0) * s[3]);
  5816. return d * sumf_d;
  5817. #else
  5818. bad_arch();
  5819. #endif // __SYCL_ARCH__ >= VER_4VEC
  5820. #endif
  5821. }
  5822. template <int mmq_y>
  5823. static __dpct_inline__ void
  5824. allocate_tiles_q5_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5825. int *tile_x_ql_q5_K, sycl::half2 *tile_x_dm_q5_K,
  5826. int *tile_x_sc_q5_K) {
  5827. (void)x_qh;
  5828. *x_ql = tile_x_ql_q5_K;
  5829. *x_dm = tile_x_dm_q5_K;
  5830. *x_sc = tile_x_sc_q5_K;
  5831. }
  5832. template <int mmq_y, int nwarps, bool need_check>
  5833. static __dpct_inline__ void
  5834. load_tiles_q5_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5835. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5836. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5837. const int &k, const int &blocks_per_row) {
  5838. (void)x_qh;
  5839. GGML_SYCL_ASSUME(i_offset >= 0);
  5840. GGML_SYCL_ASSUME(i_offset < nwarps);
  5841. GGML_SYCL_ASSUME(k >= 0);
  5842. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5843. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  5844. const int kqsx = k % QI5_K; // == k if QK_K == 256
  5845. const block_q5_K * bx0 = (const block_q5_K *) vx;
  5846. #pragma unroll
  5847. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5848. int i = i0 + i_offset;
  5849. if (need_check) {
  5850. i = sycl::min(i, i_max);
  5851. }
  5852. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  5853. const int ky = QR5_K*kqsx;
  5854. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5855. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  5856. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  5857. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  5858. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  5859. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  5860. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  5861. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  5862. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  5863. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  5864. }
  5865. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  5866. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5867. #pragma unroll
  5868. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  5869. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  5870. if (need_check) {
  5871. i = sycl::min(i, i_max);
  5872. }
  5873. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5874. #if QK_K == 256
  5875. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  5876. #endif
  5877. }
  5878. #pragma unroll
  5879. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5880. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5881. if (need_check) {
  5882. i = sycl::min(i, i_max);
  5883. }
  5884. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  5885. const int * scales = (const int *) bxi->scales;
  5886. const int ksc = k % (WARP_SIZE/8);
  5887. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5888. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5889. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5890. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5891. }
  5892. }
  5893. static __dpct_inline__ float vec_dot_q5_K_q8_1_mul_mat(
  5894. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5895. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5896. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5897. const int &i, const int &j, const int &k) {
  5898. (void)x_qh;
  5899. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  5900. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  5901. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  5902. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  5903. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  5904. }
  5905. static __dpct_inline__ float
  5906. vec_dot_q6_K_q8_1(const void *__restrict__ vbq,
  5907. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5908. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  5909. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  5910. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  5911. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  5912. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  5913. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  5914. const int8_t * scales = bq6_K->scales + scale_offset;
  5915. int u[QR6_K];
  5916. float d8[QR6_K];
  5917. #pragma unroll
  5918. for (int i = 0; i < QR6_K; ++i) {
  5919. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  5920. d8[i] = bq8_1[bq8_offset + 2 * i].ds[0];
  5921. }
  5922. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  5923. }
  5924. template <int mmq_y>
  5925. static __dpct_inline__ void
  5926. allocate_tiles_q6_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5927. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_sc) {
  5928. (void)x_qh;
  5929. *x_ql = tile_x_ql;
  5930. *x_dm = tile_x_dm;
  5931. *x_sc = tile_x_sc;
  5932. }
  5933. template <int mmq_y, int nwarps, bool need_check>
  5934. static __dpct_inline__ void
  5935. load_tiles_q6_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5936. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5937. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5938. const int &k, const int &blocks_per_row) {
  5939. (void)x_qh;
  5940. GGML_SYCL_ASSUME(i_offset >= 0);
  5941. GGML_SYCL_ASSUME(i_offset < nwarps);
  5942. GGML_SYCL_ASSUME(k >= 0);
  5943. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5944. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  5945. const int kqsx = k % QI6_K; // == k if QK_K == 256
  5946. const block_q6_K * bx0 = (const block_q6_K *) vx;
  5947. #pragma unroll
  5948. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5949. int i = i0 + i_offset;
  5950. if (need_check) {
  5951. i = sycl::min(i, i_max);
  5952. }
  5953. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  5954. const int ky = QR6_K*kqsx;
  5955. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  5956. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  5957. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  5958. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  5959. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  5960. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  5961. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  5962. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  5963. x_ql[i * (2 * WARP_SIZE + 1) + kq0] =
  5964. dpct::vectorized_binary<sycl::char4>(ql0 | qh0, 0x20202020,
  5965. dpct::sub_sat());
  5966. x_ql[i * (2 * WARP_SIZE + 1) + kq1] =
  5967. dpct::vectorized_binary<sycl::char4>(ql1 | qh1, 0x20202020,
  5968. dpct::sub_sat());
  5969. }
  5970. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  5971. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5972. float * x_dmf = (float *) x_dm;
  5973. #pragma unroll
  5974. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  5975. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  5976. if (need_check) {
  5977. i = sycl::min(i, i_max);
  5978. }
  5979. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5980. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  5981. }
  5982. #pragma unroll
  5983. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5984. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5985. if (need_check) {
  5986. i = sycl::min(i, i_max);
  5987. }
  5988. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  5989. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  5990. }
  5991. }
  5992. static __dpct_inline__ float vec_dot_q6_K_q8_1_mul_mat(
  5993. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5994. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5995. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5996. const int &i, const int &j, const int &k) {
  5997. (void)x_qh;
  5998. const float * x_dmf = (const float *) x_dm;
  5999. const float * y_df = (const float *) y_ds;
  6000. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  6001. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  6002. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  6003. 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]);
  6004. }
  6005. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x,
  6006. int mmq_y, int nwarps, load_tiles_sycl_t load_tiles, int vdr,
  6007. vec_dot_q_mul_mat_sycl_t vec_dot>
  6008. /*
  6009. DPCT1110:8: The total declared local variable size in device function mul_mat_q
  6010. exceeds 128 bytes and may cause high register pressure. Consult with your
  6011. hardware vendor to find the total register size available and adjust the code,
  6012. or use smaller sub-group size to avoid high register pressure.
  6013. */
  6014. static __dpct_inline__ void
  6015. mul_mat_q(const void *__restrict__ vx, const void *__restrict__ vy,
  6016. float *__restrict__ dst, const int ncols_x, const int nrows_x,
  6017. const int ncols_y, const int nrows_y, const int nrows_dst,
  6018. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_qh,
  6019. int *tile_x_sc, const sycl::nd_item<3> &item_ct1, int *tile_y_qs,
  6020. sycl::half2 *tile_y_ds) {
  6021. const block_q_t * x = (const block_q_t *) vx;
  6022. const block_q8_1 * y = (const block_q8_1 *) vy;
  6023. const int blocks_per_row_x = ncols_x / qk;
  6024. const int blocks_per_col_y = nrows_y / QK8_1;
  6025. const int blocks_per_warp = WARP_SIZE / qi;
  6026. const int & ncols_dst = ncols_y;
  6027. const int row_dst_0 = item_ct1.get_group(2) * mmq_y;
  6028. const int & row_x_0 = row_dst_0;
  6029. const int col_dst_0 = item_ct1.get_group(1) * mmq_x;
  6030. const int & col_y_0 = col_dst_0;
  6031. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  6032. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  6033. load_tiles(x + row_x_0 * blocks_per_row_x + ib0, tile_x_ql, tile_x_dm,
  6034. tile_x_qh, tile_x_sc, item_ct1.get_local_id(1),
  6035. nrows_x - row_x_0 - 1, item_ct1.get_local_id(2),
  6036. blocks_per_row_x);
  6037. #pragma unroll
  6038. for (int ir = 0; ir < qr; ++ir) {
  6039. const int kqs = ir * WARP_SIZE + item_ct1.get_local_id(2);
  6040. const int kbxd = kqs / QI8_1;
  6041. #pragma unroll
  6042. for (int i = 0; i < mmq_x; i += nwarps) {
  6043. const int col_y_eff = dpct::min(
  6044. (unsigned int)(col_y_0 + item_ct1.get_local_id(1) + i),
  6045. ncols_y - 1); // to prevent out-of-bounds memory accesses
  6046. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  6047. const int index_y = (item_ct1.get_local_id(1) + i) * WARP_SIZE +
  6048. kqs % WARP_SIZE;
  6049. tile_y_qs[index_y] = get_int_from_int8_aligned(
  6050. by0->qs, item_ct1.get_local_id(2) % QI8_1);
  6051. }
  6052. #pragma unroll
  6053. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  6054. const int ids =
  6055. (ids0 + item_ct1.get_local_id(1) * QI8_1 +
  6056. item_ct1.get_local_id(2) / (WARP_SIZE / QI8_1)) %
  6057. mmq_x;
  6058. const int kby = item_ct1.get_local_id(2) % (WARP_SIZE / QI8_1);
  6059. const int col_y_eff = sycl::min(col_y_0 + ids, ncols_y - 1);
  6060. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  6061. const sycl::half2 *dsi_src =
  6062. &y[col_y_eff * blocks_per_col_y + ib0 * (qk / QK8_1) +
  6063. ir * (WARP_SIZE / QI8_1) + kby]
  6064. .ds;
  6065. sycl::half2 *dsi_dst =
  6066. &tile_y_ds[ids * (WARP_SIZE / QI8_1) + kby];
  6067. if (need_sum) {
  6068. *dsi_dst = *dsi_src;
  6069. } else {
  6070. float * dfi_dst = (float *) dsi_dst;
  6071. *dfi_dst = (*dsi_src)[0];
  6072. }
  6073. }
  6074. /*
  6075. DPCT1118:9: SYCL group functions and algorithms must be encountered
  6076. in converged control flow. You may need to adjust the code.
  6077. */
  6078. /*
  6079. DPCT1065:56: Consider replacing sycl::nd_item::barrier() with
  6080. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6081. better performance if there is no access to global memory.
  6082. */
  6083. item_ct1.barrier();
  6084. // #pragma unroll // unrolling this loop causes too much register pressure
  6085. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  6086. #pragma unroll
  6087. for (int j = 0; j < mmq_x; j += nwarps) {
  6088. #pragma unroll
  6089. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  6090. sum[i / WARP_SIZE][j / nwarps] += vec_dot(
  6091. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  6092. tile_y_qs, tile_y_ds, item_ct1.get_local_id(2) + i,
  6093. item_ct1.get_local_id(1) + j, k);
  6094. }
  6095. }
  6096. }
  6097. /*
  6098. DPCT1118:10: SYCL group functions and algorithms must be encountered
  6099. in converged control flow. You may need to adjust the code.
  6100. */
  6101. /*
  6102. DPCT1065:57: Consider replacing sycl::nd_item::barrier() with
  6103. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6104. better performance if there is no access to global memory.
  6105. */
  6106. item_ct1.barrier();
  6107. }
  6108. }
  6109. #pragma unroll
  6110. for (int j = 0; j < mmq_x; j += nwarps) {
  6111. const int col_dst = col_dst_0 + j + item_ct1.get_local_id(1);
  6112. if (col_dst >= ncols_dst) {
  6113. return;
  6114. }
  6115. #pragma unroll
  6116. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  6117. const int row_dst = row_dst_0 + item_ct1.get_local_id(2) + i;
  6118. if (row_dst >= nrows_dst) {
  6119. continue;
  6120. }
  6121. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  6122. }
  6123. }
  6124. }
  6125. #define MMQ_X_Q4_0_RDNA2 64
  6126. #define MMQ_Y_Q4_0_RDNA2 128
  6127. #define NWARPS_Q4_0_RDNA2 8
  6128. #define MMQ_X_Q4_0_RDNA1 64
  6129. #define MMQ_Y_Q4_0_RDNA1 64
  6130. #define NWARPS_Q4_0_RDNA1 8
  6131. #if defined(SYCL_USE_XMX)
  6132. #define MMQ_X_Q4_0_AMPERE 4
  6133. #define MMQ_Y_Q4_0_AMPERE 32
  6134. #define NWARPS_Q4_0_AMPERE 4
  6135. #else
  6136. #define MMQ_X_Q4_0_AMPERE 64
  6137. #define MMQ_Y_Q4_0_AMPERE 128
  6138. #define NWARPS_Q4_0_AMPERE 4
  6139. #endif
  6140. #define MMQ_X_Q4_0_PASCAL 64
  6141. #define MMQ_Y_Q4_0_PASCAL 64
  6142. #define NWARPS_Q4_0_PASCAL 8
  6143. template <bool need_check> static void
  6144. mul_mat_q4_0(
  6145. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6146. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6147. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_0, float *tile_x_d_q4_0,
  6148. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6149. int * tile_x_ql = nullptr;
  6150. sycl::half2 *tile_x_dm = nullptr;
  6151. int * tile_x_qh = nullptr;
  6152. int * tile_x_sc = nullptr;
  6153. //sycl_todo: change according to hardware
  6154. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  6155. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  6156. const int nwarps = NWARPS_Q4_0_AMPERE;
  6157. allocate_tiles_q4_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6158. tile_x_qs_q4_0, tile_x_d_q4_0);
  6159. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps,
  6160. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ,
  6161. vec_dot_q4_0_q8_1_mul_mat>(
  6162. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6163. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6164. }
  6165. #define MMQ_X_Q4_1_RDNA2 64
  6166. #define MMQ_Y_Q4_1_RDNA2 128
  6167. #define NWARPS_Q4_1_RDNA2 8
  6168. #define MMQ_X_Q4_1_RDNA1 64
  6169. #define MMQ_Y_Q4_1_RDNA1 64
  6170. #define NWARPS_Q4_1_RDNA1 8
  6171. #if defined(SYCL_USE_XMX)
  6172. #define MMQ_X_Q4_1_AMPERE 4
  6173. #define MMQ_Y_Q4_1_AMPERE 32
  6174. #define NWARPS_Q4_1_AMPERE 4
  6175. #else
  6176. #define MMQ_X_Q4_1_AMPERE 64
  6177. #define MMQ_Y_Q4_1_AMPERE 128
  6178. #define NWARPS_Q4_1_AMPERE 4
  6179. #endif
  6180. #define MMQ_X_Q4_1_PASCAL 64
  6181. #define MMQ_Y_Q4_1_PASCAL 64
  6182. #define NWARPS_Q4_1_PASCAL 8
  6183. template <bool need_check> static void
  6184. mul_mat_q4_1(
  6185. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6186. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6187. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_1,
  6188. sycl::half2 *tile_x_dm_q4_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6189. int * tile_x_ql = nullptr;
  6190. sycl::half2 *tile_x_dm = nullptr;
  6191. int * tile_x_qh = nullptr;
  6192. int * tile_x_sc = nullptr;
  6193. //sycl_todo: change according to hardware
  6194. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  6195. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  6196. const int nwarps = NWARPS_Q4_1_AMPERE;
  6197. allocate_tiles_q4_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6198. tile_x_qs_q4_1, tile_x_dm_q4_1);
  6199. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps,
  6200. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ,
  6201. vec_dot_q4_1_q8_1_mul_mat>(
  6202. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6203. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6204. }
  6205. #define MMQ_X_Q5_0_RDNA2 64
  6206. #define MMQ_Y_Q5_0_RDNA2 128
  6207. #define NWARPS_Q5_0_RDNA2 8
  6208. #define MMQ_X_Q5_0_RDNA1 64
  6209. #define MMQ_Y_Q5_0_RDNA1 64
  6210. #define NWARPS_Q5_0_RDNA1 8
  6211. #if defined(SYCL_USE_XMX)
  6212. #define MMQ_X_Q5_0_AMPERE 4
  6213. #define MMQ_Y_Q5_0_AMPERE 32
  6214. #define NWARPS_Q5_0_AMPERE 4
  6215. #else
  6216. #define MMQ_X_Q5_0_AMPERE 128
  6217. #define MMQ_Y_Q5_0_AMPERE 64
  6218. #define NWARPS_Q5_0_AMPERE 4
  6219. #endif
  6220. #define MMQ_X_Q5_0_PASCAL 64
  6221. #define MMQ_Y_Q5_0_PASCAL 64
  6222. #define NWARPS_Q5_0_PASCAL 8
  6223. template <bool need_check> static void
  6224. mul_mat_q5_0(
  6225. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6226. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6227. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_0, float *tile_x_d_q5_0,
  6228. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6229. int * tile_x_ql = nullptr;
  6230. sycl::half2 *tile_x_dm = nullptr;
  6231. int * tile_x_qh = nullptr;
  6232. int * tile_x_sc = nullptr;
  6233. //sycl_todo: change according to hardware
  6234. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  6235. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  6236. const int nwarps = NWARPS_Q5_0_AMPERE;
  6237. allocate_tiles_q5_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6238. tile_x_ql_q5_0, tile_x_d_q5_0);
  6239. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps,
  6240. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ,
  6241. vec_dot_q5_0_q8_1_mul_mat>(
  6242. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6243. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6244. }
  6245. #define MMQ_X_Q5_1_RDNA2 64
  6246. #define MMQ_Y_Q5_1_RDNA2 128
  6247. #define NWARPS_Q5_1_RDNA2 8
  6248. #define MMQ_X_Q5_1_RDNA1 64
  6249. #define MMQ_Y_Q5_1_RDNA1 64
  6250. #define NWARPS_Q5_1_RDNA1 8
  6251. #if defined(SYCL_USE_XMX)
  6252. #define MMQ_X_Q5_1_AMPERE 4
  6253. #define MMQ_Y_Q5_1_AMPERE 32
  6254. #define NWARPS_Q5_1_AMPERE 4
  6255. #else
  6256. #define MMQ_X_Q5_1_AMPERE 128
  6257. #define MMQ_Y_Q5_1_AMPERE 64
  6258. #define NWARPS_Q5_1_AMPERE 4
  6259. #endif
  6260. #define MMQ_X_Q5_1_PASCAL 64
  6261. #define MMQ_Y_Q5_1_PASCAL 64
  6262. #define NWARPS_Q5_1_PASCAL 8
  6263. template <bool need_check> static void
  6264. mul_mat_q5_1(
  6265. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6266. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6267. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_1,
  6268. sycl::half2 *tile_x_dm_q5_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6269. int * tile_x_ql = nullptr;
  6270. sycl::half2 *tile_x_dm = nullptr;
  6271. int * tile_x_qh = nullptr;
  6272. int * tile_x_sc = nullptr;
  6273. //sycl_todo: change according to hardware
  6274. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  6275. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  6276. const int nwarps = NWARPS_Q5_1_AMPERE;
  6277. allocate_tiles_q5_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6278. tile_x_ql_q5_1, tile_x_dm_q5_1);
  6279. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps,
  6280. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ,
  6281. vec_dot_q5_1_q8_1_mul_mat>(
  6282. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6283. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6284. }
  6285. #define MMQ_X_Q8_0_RDNA2 64
  6286. #define MMQ_Y_Q8_0_RDNA2 128
  6287. #define NWARPS_Q8_0_RDNA2 8
  6288. #define MMQ_X_Q8_0_RDNA1 64
  6289. #define MMQ_Y_Q8_0_RDNA1 64
  6290. #define NWARPS_Q8_0_RDNA1 8
  6291. #if defined(SYCL_USE_XMX)
  6292. #define MMQ_X_Q8_0_AMPERE 4
  6293. #define MMQ_Y_Q8_0_AMPERE 32
  6294. #define NWARPS_Q8_0_AMPERE 4
  6295. #else
  6296. #define MMQ_X_Q8_0_AMPERE 128
  6297. #define MMQ_Y_Q8_0_AMPERE 64
  6298. #define NWARPS_Q8_0_AMPERE 4
  6299. #endif
  6300. #define MMQ_X_Q8_0_PASCAL 64
  6301. #define MMQ_Y_Q8_0_PASCAL 64
  6302. #define NWARPS_Q8_0_PASCAL 8
  6303. template <bool need_check> static void
  6304. mul_mat_q8_0(
  6305. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6306. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6307. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q8_0, float *tile_x_d_q8_0,
  6308. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6309. int * tile_x_ql = nullptr;
  6310. sycl::half2 *tile_x_dm = nullptr;
  6311. int * tile_x_qh = nullptr;
  6312. int * tile_x_sc = nullptr;
  6313. //sycl_todo: change according to hardware
  6314. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  6315. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  6316. const int nwarps = NWARPS_Q8_0_AMPERE;
  6317. allocate_tiles_q8_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6318. tile_x_qs_q8_0, tile_x_d_q8_0);
  6319. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps,
  6320. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ,
  6321. vec_dot_q8_0_q8_1_mul_mat>(
  6322. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6323. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6324. }
  6325. #define MMQ_X_Q2_K_RDNA2 64
  6326. #define MMQ_Y_Q2_K_RDNA2 128
  6327. #define NWARPS_Q2_K_RDNA2 8
  6328. #define MMQ_X_Q2_K_RDNA1 128
  6329. #define MMQ_Y_Q2_K_RDNA1 32
  6330. #define NWARPS_Q2_K_RDNA1 8
  6331. #if defined(SYCL_USE_XMX)
  6332. #define MMQ_X_Q2_K_AMPERE 4
  6333. #define MMQ_Y_Q2_K_AMPERE 32
  6334. #define NWARPS_Q2_K_AMPERE 4
  6335. #else
  6336. #define MMQ_X_Q2_K_AMPERE 64
  6337. #define MMQ_Y_Q2_K_AMPERE 128
  6338. #define NWARPS_Q2_K_AMPERE 4
  6339. #endif
  6340. #define MMQ_X_Q2_K_PASCAL 64
  6341. #define MMQ_Y_Q2_K_PASCAL 64
  6342. #define NWARPS_Q2_K_PASCAL 8
  6343. template <bool need_check> static void
  6344. mul_mat_q2_K(
  6345. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6346. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6347. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q2_K,
  6348. sycl::half2 *tile_x_dm_q2_K, int *tile_x_sc_q2_K, int *tile_y_qs,
  6349. sycl::half2 *tile_y_ds) {
  6350. int * tile_x_ql = nullptr;
  6351. sycl::half2 *tile_x_dm = nullptr;
  6352. int * tile_x_qh = nullptr;
  6353. int * tile_x_sc = nullptr;
  6354. //sycl_todo: change according to hardware
  6355. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  6356. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  6357. const int nwarps = NWARPS_Q2_K_AMPERE;
  6358. allocate_tiles_q2_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6359. tile_x_ql_q2_K, tile_x_dm_q2_K, tile_x_sc_q2_K);
  6360. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps,
  6361. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ,
  6362. vec_dot_q2_K_q8_1_mul_mat>(
  6363. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6364. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6365. }
  6366. #define MMQ_X_Q3_K_RDNA2 128
  6367. #define MMQ_Y_Q3_K_RDNA2 64
  6368. #define NWARPS_Q3_K_RDNA2 8
  6369. #define MMQ_X_Q3_K_RDNA1 32
  6370. #define MMQ_Y_Q3_K_RDNA1 128
  6371. #define NWARPS_Q3_K_RDNA1 8
  6372. #if defined(SYCL_USE_XMX)
  6373. #define MMQ_X_Q3_K_AMPERE 4
  6374. #define MMQ_Y_Q3_K_AMPERE 32
  6375. #define NWARPS_Q3_K_AMPERE 4
  6376. #else
  6377. #define MMQ_X_Q3_K_AMPERE 128
  6378. #define MMQ_Y_Q3_K_AMPERE 128
  6379. #define NWARPS_Q3_K_AMPERE 4
  6380. #endif
  6381. #define MMQ_X_Q3_K_PASCAL 64
  6382. #define MMQ_Y_Q3_K_PASCAL 64
  6383. #define NWARPS_Q3_K_PASCAL 8
  6384. template <bool need_check> static void
  6385. mul_mat_q3_K(
  6386. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6387. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6388. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q3_K,
  6389. sycl::half2 *tile_x_dm_q3_K, int *tile_x_qh_q3_K, int *tile_x_sc_q3_K,
  6390. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6391. int * tile_x_ql = nullptr;
  6392. sycl::half2 *tile_x_dm = nullptr;
  6393. int * tile_x_qh = nullptr;
  6394. int * tile_x_sc = nullptr;
  6395. //sycl_todo: change according to hardware
  6396. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  6397. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  6398. const int nwarps = NWARPS_Q3_K_AMPERE;
  6399. allocate_tiles_q3_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6400. tile_x_ql_q3_K, tile_x_dm_q3_K, tile_x_qh_q3_K,
  6401. tile_x_sc_q3_K);
  6402. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps,
  6403. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ,
  6404. vec_dot_q3_K_q8_1_mul_mat>(
  6405. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6406. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6407. }
  6408. #define MMQ_X_Q4_K_RDNA2 64
  6409. #define MMQ_Y_Q4_K_RDNA2 128
  6410. #define NWARPS_Q4_K_RDNA2 8
  6411. #define MMQ_X_Q4_K_RDNA1 32
  6412. #define MMQ_Y_Q4_K_RDNA1 64
  6413. #define NWARPS_Q4_K_RDNA1 8
  6414. #if defined(SYCL_USE_XMX)
  6415. #define MMQ_X_Q4_K_AMPERE 4
  6416. #define MMQ_Y_Q4_K_AMPERE 32
  6417. #define NWARPS_Q4_K_AMPERE 4
  6418. #else
  6419. #define MMQ_X_Q4_K_AMPERE 64
  6420. #define MMQ_Y_Q4_K_AMPERE 128
  6421. #define NWARPS_Q4_K_AMPERE 4
  6422. #endif
  6423. #define MMQ_X_Q4_K_PASCAL 64
  6424. #define MMQ_Y_Q4_K_PASCAL 64
  6425. #define NWARPS_Q4_K_PASCAL 8
  6426. template <bool need_check> static void
  6427. mul_mat_q4_K(
  6428. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6429. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6430. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q4_K,
  6431. sycl::half2 *tile_x_dm_q4_K, int *tile_x_sc_q4_K, int *tile_y_qs,
  6432. sycl::half2 *tile_y_ds) {
  6433. int * tile_x_ql = nullptr;
  6434. sycl::half2 *tile_x_dm = nullptr;
  6435. int * tile_x_qh = nullptr;
  6436. int * tile_x_sc = nullptr;
  6437. //sycl_todo: change according to hardware
  6438. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  6439. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  6440. const int nwarps = NWARPS_Q4_K_AMPERE;
  6441. allocate_tiles_q4_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6442. tile_x_ql_q4_K, tile_x_dm_q4_K, tile_x_sc_q4_K);
  6443. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps,
  6444. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ,
  6445. vec_dot_q4_K_q8_1_mul_mat>(
  6446. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6447. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6448. }
  6449. #define MMQ_X_Q5_K_RDNA2 64
  6450. #define MMQ_Y_Q5_K_RDNA2 128
  6451. #define NWARPS_Q5_K_RDNA2 8
  6452. #define MMQ_X_Q5_K_RDNA1 32
  6453. #define MMQ_Y_Q5_K_RDNA1 64
  6454. #define NWARPS_Q5_K_RDNA1 8
  6455. #if defined(SYCL_USE_XMX)
  6456. #define MMQ_X_Q5_K_AMPERE 4
  6457. #define MMQ_Y_Q5_K_AMPERE 32
  6458. #define NWARPS_Q5_K_AMPERE 4
  6459. #else
  6460. #define MMQ_X_Q5_K_AMPERE 64
  6461. #define MMQ_Y_Q5_K_AMPERE 128
  6462. #define NWARPS_Q5_K_AMPERE 4
  6463. #endif
  6464. #define MMQ_X_Q5_K_PASCAL 64
  6465. #define MMQ_Y_Q5_K_PASCAL 64
  6466. #define NWARPS_Q5_K_PASCAL 8
  6467. template <bool need_check> static void
  6468. mul_mat_q5_K(
  6469. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6470. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6471. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_K,
  6472. sycl::half2 *tile_x_dm_q5_K, int *tile_x_sc_q5_K, int *tile_y_qs,
  6473. sycl::half2 *tile_y_ds) {
  6474. int * tile_x_ql = nullptr;
  6475. sycl::half2 *tile_x_dm = nullptr;
  6476. int * tile_x_qh = nullptr;
  6477. int * tile_x_sc = nullptr;
  6478. //sycl_todo: change according to hardware
  6479. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  6480. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  6481. const int nwarps = NWARPS_Q5_K_AMPERE;
  6482. allocate_tiles_q5_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6483. tile_x_ql_q5_K, tile_x_dm_q5_K, tile_x_sc_q5_K);
  6484. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps,
  6485. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ,
  6486. vec_dot_q5_K_q8_1_mul_mat>(
  6487. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6488. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6489. }
  6490. #define MMQ_X_Q6_K_RDNA2 64
  6491. #define MMQ_Y_Q6_K_RDNA2 128
  6492. #define NWARPS_Q6_K_RDNA2 8
  6493. #define MMQ_X_Q6_K_RDNA1 32
  6494. #define MMQ_Y_Q6_K_RDNA1 64
  6495. #define NWARPS_Q6_K_RDNA1 8
  6496. #if defined(SYCL_USE_XMX)
  6497. #define MMQ_X_Q6_K_AMPERE 4
  6498. #define MMQ_Y_Q6_K_AMPERE 32
  6499. #define NWARPS_Q6_K_AMPERE 4
  6500. #else
  6501. #define MMQ_X_Q6_K_AMPERE 64
  6502. #define MMQ_Y_Q6_K_AMPERE 64
  6503. #define NWARPS_Q6_K_AMPERE 4
  6504. #endif
  6505. #define MMQ_X_Q6_K_PASCAL 64
  6506. #define MMQ_Y_Q6_K_PASCAL 64
  6507. #define NWARPS_Q6_K_PASCAL 8
  6508. template <bool need_check> static void
  6509. mul_mat_q6_K(
  6510. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6511. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6512. const sycl::nd_item<3> &item_ct1, int *tile_x_ql, sycl::half2 *tile_x_dm,
  6513. int *tile_x_sc, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6514. // int * tile_x_ql = nullptr;
  6515. // sycl::half2 *tile_x_dm = nullptr;
  6516. int * tile_x_qh = nullptr;
  6517. // int * tile_x_sc = nullptr;
  6518. //sycl_todo: change according to hardware
  6519. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  6520. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  6521. const int nwarps = NWARPS_Q6_K_AMPERE;
  6522. allocate_tiles_q6_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6523. tile_x_ql, tile_x_dm, tile_x_sc);
  6524. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps,
  6525. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ,
  6526. vec_dot_q6_K_q8_1_mul_mat>(
  6527. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6528. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6529. }
  6530. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl>
  6531. static void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  6532. const sycl::nd_item<3> &item_ct1) {
  6533. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6534. item_ct1.get_local_id(1);
  6535. if (row >= nrows) {
  6536. return;
  6537. }
  6538. const int blocks_per_row = ncols / qk;
  6539. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6540. // partial sum for each thread
  6541. float tmp = 0.0f;
  6542. const block_q_t * x = (const block_q_t *) vx;
  6543. const block_q8_1 * y = (const block_q8_1 *) vy;
  6544. for (int i = 0; i < blocks_per_row; i += blocks_per_warp) {
  6545. const int ibx = row * blocks_per_row + i +
  6546. item_ct1.get_local_id(2) / (qi / vdr); // x block index
  6547. const int iby = (i + item_ct1.get_local_id(2) / (qi / vdr)) *
  6548. (qk / QK8_1); // y block index that aligns with ibx
  6549. const int iqs =
  6550. vdr *
  6551. (item_ct1.get_local_id(2) %
  6552. (qi / vdr)); // x block quant index when casting the quants to int
  6553. tmp += vec_dot_q_sycl(&x[ibx], &y[iby], iqs);
  6554. }
  6555. // sum up partial sums and write back result
  6556. #pragma unroll
  6557. for (int mask = 16; mask > 0; mask >>= 1) {
  6558. tmp +=
  6559. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6560. }
  6561. if (item_ct1.get_local_id(2) == 0) {
  6562. dst[row] = tmp;
  6563. }
  6564. }
  6565. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  6566. static void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows,
  6567. const sycl::nd_item<3> &item_ct1) {
  6568. // qk = quantized weights per x block
  6569. // qr = number of quantized weights per data value in x block
  6570. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6571. item_ct1.get_local_id(1);
  6572. if (row >= nrows) {
  6573. return;
  6574. }
  6575. const int tid = item_ct1.get_local_id(2);
  6576. const int iter_stride = 2*GGML_SYCL_DMMV_X;
  6577. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  6578. const int y_offset = qr == 1 ? 1 : qk/2;
  6579. // partial sum for each thread
  6580. #ifdef GGML_SYCL_F16
  6581. sycl::half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  6582. #else
  6583. float tmp = 0.0f;
  6584. #endif // GGML_SYCL_F16
  6585. for (int i = 0; i < ncols; i += iter_stride) {
  6586. const int col = i + vals_per_iter*tid;
  6587. const int ib = (row*ncols + col)/qk; // x block index
  6588. const int iqs = (col%qk)/qr; // x quant index
  6589. const int iybs = col - col%qk; // y block start index
  6590. // processing >2 values per i iter is faster for fast GPUs
  6591. #pragma unroll
  6592. for (int j = 0; j < vals_per_iter; j += 2) {
  6593. // process 2 vals per j iter
  6594. // dequantize
  6595. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  6596. dfloat2 v;
  6597. dequantize_kernel(vx, ib, iqs + j/qr, v);
  6598. // matrix multiplication
  6599. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  6600. #ifdef GGML_SYCL_F16
  6601. dfloat2 t1{y[iybs + iqs + j / qr + 0],
  6602. y[iybs + iqs + j / qr + y_offset]};
  6603. tmp += v * t1;
  6604. #else
  6605. tmp += v.x() * y[iybs + iqs + j / qr + 0];
  6606. tmp += v.y() * y[iybs + iqs + j / qr + y_offset];
  6607. #endif // GGML_SYCL_F16
  6608. }
  6609. }
  6610. // sum up partial sums and write back result
  6611. #pragma unroll
  6612. for (int mask = 16; mask > 0; mask >>= 1) {
  6613. tmp +=
  6614. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6615. }
  6616. if (tid == 0) {
  6617. #ifdef GGML_SYCL_F16
  6618. dst[row] = tmp.x() + tmp.y();
  6619. #else
  6620. dst[row] = tmp;
  6621. #endif // GGML_SYCL_F16
  6622. }
  6623. }
  6624. static void mul_mat_p021_f16_f32(
  6625. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  6626. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y,
  6627. const sycl::nd_item<3> &item_ct1) {
  6628. const sycl::half *x = (const sycl::half *)vx;
  6629. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6630. item_ct1.get_local_id(1);
  6631. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  6632. item_ct1.get_local_id(0);
  6633. const int channel_x = channel / (nchannels_y / nchannels_x);
  6634. const int nrows_y = ncols_x;
  6635. const int nrows_dst = nrows_x;
  6636. const int row_dst = row_x;
  6637. float tmp = 0.0f;
  6638. for (int col_x0 = 0; col_x0 < ncols_x;
  6639. col_x0 += item_ct1.get_local_range(2)) {
  6640. const int col_x = col_x0 + item_ct1.get_local_id(2);
  6641. if (col_x >= ncols_x) {
  6642. break;
  6643. }
  6644. // x is transposed and permuted
  6645. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  6646. const float xi =
  6647. sycl::vec<sycl::half, 1>(x[ix])
  6648. .convert<float, sycl::rounding_mode::automatic>()[0];
  6649. const int row_y = col_x;
  6650. // y is not transposed but permuted
  6651. const int iy = channel*nrows_y + row_y;
  6652. tmp += xi * y[iy];
  6653. }
  6654. // dst is not transposed and not permuted
  6655. const int idst = channel*nrows_dst + row_dst;
  6656. // sum up partial sums and write back result
  6657. #pragma unroll
  6658. for (int mask = 16; mask > 0; mask >>= 1) {
  6659. tmp +=
  6660. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6661. }
  6662. if (item_ct1.get_local_id(2) == 0) {
  6663. dst[idst] = tmp;
  6664. }
  6665. }
  6666. static void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  6667. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  6668. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor,
  6669. const sycl::nd_item<3> &item_ct1) {
  6670. const sycl::half *x = (const sycl::half *)vx;
  6671. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6672. item_ct1.get_local_id(1);
  6673. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  6674. item_ct1.get_local_id(0);
  6675. const int channel_x = channel / channel_x_divisor;
  6676. const int nrows_y = ncols_x;
  6677. const int nrows_dst = nrows_x;
  6678. const int row_dst = row_x;
  6679. const int idst = channel*nrows_dst + row_dst;
  6680. float tmp = 0.0f;
  6681. for (int col_x0 = 0; col_x0 < ncols_x;
  6682. col_x0 += item_ct1.get_local_range(2)) {
  6683. const int col_x = col_x0 + item_ct1.get_local_id(2);
  6684. if (col_x >= ncols_x) {
  6685. break;
  6686. }
  6687. const int row_y = col_x;
  6688. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  6689. const int iy = channel*nrows_y + row_y;
  6690. const float xi =
  6691. sycl::vec<sycl::half, 1>(x[ix])
  6692. .convert<float, sycl::rounding_mode::automatic>()[0];
  6693. tmp += xi * y[iy];
  6694. }
  6695. // sum up partial sums and write back result
  6696. #pragma unroll
  6697. for (int mask = 16; mask > 0; mask >>= 1) {
  6698. tmp +=
  6699. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6700. }
  6701. if (item_ct1.get_local_id(2) == 0) {
  6702. dst[idst] = tmp;
  6703. }
  6704. }
  6705. static void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  6706. const float * xi = (const float *) cxi;
  6707. float * dsti = (float *) cdsti;
  6708. *dsti = *xi;
  6709. }
  6710. static void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  6711. const float * xi = (const float *) cxi;
  6712. sycl::half *dsti = (sycl::half *)cdsti;
  6713. *dsti = sycl::vec<float, 1>(*xi)
  6714. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  6715. }
  6716. static void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  6717. const sycl::half *xi = (const sycl::half *)cxi;
  6718. sycl::half *dsti = (sycl::half *)cdsti;
  6719. *dsti = *xi;
  6720. }
  6721. static void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  6722. const sycl::half *xi = (const sycl::half *)cxi;
  6723. float * dsti = (float *) cdsti;
  6724. *dsti = *xi;
  6725. }
  6726. static void cpy_1_i16_i16(const char * cxi, char * cdsti) {
  6727. const int16_t *xi = (const int16_t *)cxi;
  6728. int16_t *dsti = (int16_t *)cdsti;
  6729. *dsti = *xi;
  6730. }
  6731. static void cpy_1_i32_i32(const char * cxi, char * cdsti) {
  6732. const int32_t *xi = (const int32_t *)cxi;
  6733. int32_t *dsti = (int32_t *)cdsti;
  6734. *dsti = *xi;
  6735. }
  6736. template <cpy_kernel_t cpy_1>
  6737. static void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  6738. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6739. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  6740. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  6741. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6742. item_ct1.get_local_id(2);
  6743. if (i >= ne) {
  6744. return;
  6745. }
  6746. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  6747. // then combine those indices with the corresponding byte offsets to get the total offsets
  6748. const int i03 = i/(ne00 * ne01 * ne02);
  6749. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  6750. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  6751. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  6752. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  6753. const int i13 = i/(ne10 * ne11 * ne12);
  6754. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  6755. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  6756. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  6757. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  6758. cpy_1(cx + x_offset, cdst + dst_offset);
  6759. }
  6760. static void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  6761. const float * xi = (const float *) cxi;
  6762. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  6763. float amax = 0.0f; // absolute max
  6764. for (int j = 0; j < QK8_0; j++) {
  6765. const float v = xi[j];
  6766. amax = sycl::fmax(amax, sycl::fabs((float)v));
  6767. }
  6768. const float d = amax / ((1 << 7) - 1);
  6769. const float id = d ? 1.0f/d : 0.0f;
  6770. dsti->d = d;
  6771. for (int j = 0; j < QK8_0; ++j) {
  6772. const float x0 = xi[j]*id;
  6773. dsti->qs[j] = sycl::round((float)x0);
  6774. }
  6775. }
  6776. static void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  6777. const float * xi = (const float *) cxi;
  6778. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  6779. float amax = 0.0f;
  6780. float vmax = 0.0f;
  6781. for (int j = 0; j < QK4_0; ++j) {
  6782. const float v = xi[j];
  6783. if (amax < sycl::fabs((float)v)) {
  6784. amax = sycl::fabs((float)v);
  6785. vmax = v;
  6786. }
  6787. }
  6788. const float d = vmax / -8;
  6789. const float id = d ? 1.0f/d : 0.0f;
  6790. dsti->d = d;
  6791. for (int j = 0; j < QK4_0/2; ++j) {
  6792. const float x0 = xi[0 + j]*id;
  6793. const float x1 = xi[QK4_0/2 + j]*id;
  6794. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 8.5f));
  6795. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 8.5f));
  6796. dsti->qs[j] = xi0;
  6797. dsti->qs[j] |= xi1 << 4;
  6798. }
  6799. }
  6800. static void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  6801. const float * xi = (const float *) cxi;
  6802. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  6803. float vmin = FLT_MAX;
  6804. float vmax = -FLT_MAX;
  6805. for (int j = 0; j < QK4_1; ++j) {
  6806. const float v = xi[j];
  6807. if (v < vmin) vmin = v;
  6808. if (v > vmax) vmax = v;
  6809. }
  6810. const float d = (vmax - vmin) / ((1 << 4) - 1);
  6811. const float id = d ? 1.0f/d : 0.0f;
  6812. dsti->dm.x() = d;
  6813. dsti->dm.y() = vmin;
  6814. for (int j = 0; j < QK4_1/2; ++j) {
  6815. const float x0 = (xi[0 + j] - vmin)*id;
  6816. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  6817. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 0.5f));
  6818. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 0.5f));
  6819. dsti->qs[j] = xi0;
  6820. dsti->qs[j] |= xi1 << 4;
  6821. }
  6822. }
  6823. template <cpy_kernel_t cpy_blck, int qk>
  6824. static void cpy_f32_q(const char * cx, char * cdst, const int ne,
  6825. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  6826. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  6827. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  6828. const int i = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6829. item_ct1.get_local_id(2)) *
  6830. qk;
  6831. if (i >= ne) {
  6832. return;
  6833. }
  6834. const int i03 = i/(ne00 * ne01 * ne02);
  6835. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  6836. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  6837. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  6838. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  6839. const int i13 = i/(ne10 * ne11 * ne12);
  6840. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  6841. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  6842. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  6843. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  6844. cpy_blck(cx + x_offset, cdst + dst_offset);
  6845. }
  6846. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  6847. const float y = (i0 / 2 - low) / sycl::max(0.001f, high - low);
  6848. return 1.0f - sycl::min(1.0f, sycl::max(0.0f, y));
  6849. }
  6850. struct rope_corr_dims {
  6851. float v[4];
  6852. };
  6853. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  6854. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  6855. static void rope_yarn(
  6856. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  6857. float * cos_theta, float * sin_theta
  6858. ) {
  6859. // Get n-d rotational scaling corrected for extrapolation
  6860. float theta_interp = freq_scale * theta_extrap;
  6861. float theta = theta_interp;
  6862. if (ext_factor != 0.0f) {
  6863. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  6864. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  6865. // Get n-d magnitude scaling corrected for interpolation
  6866. mscale *= 1.0f + 0.1f * sycl::log(1.0f / freq_scale);
  6867. }
  6868. *cos_theta = sycl::cos(theta) * mscale;
  6869. *sin_theta = sycl::sin(theta) * mscale;
  6870. }
  6871. // rope == RoPE == rotary positional embedding
  6872. template<typename T, bool has_pos>
  6873. static void rope(
  6874. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  6875. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  6876. ,
  6877. const sycl::nd_item<3> &item_ct1) {
  6878. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6879. item_ct1.get_local_id(1));
  6880. if (col >= ncols) {
  6881. return;
  6882. }
  6883. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6884. item_ct1.get_local_id(2);
  6885. const int i = row*ncols + col;
  6886. const int i2 = row/p_delta_rows;
  6887. const int p = has_pos ? pos[i2] : 0;
  6888. const float theta_base = p * dpct::pow(freq_base, -float(col) / ncols);
  6889. float cos_theta, sin_theta;
  6890. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  6891. const float x0 = x[i + 0];
  6892. const float x1 = x[i + 1];
  6893. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  6894. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  6895. }
  6896. template<typename T, bool has_pos>
  6897. static void rope_neox(
  6898. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  6899. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  6900. ,
  6901. const sycl::nd_item<3> &item_ct1) {
  6902. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6903. item_ct1.get_local_id(1));
  6904. if (col >= ncols) {
  6905. return;
  6906. }
  6907. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6908. item_ct1.get_local_id(2);
  6909. const int ib = col / n_dims;
  6910. const int ic = col % n_dims;
  6911. if (ib > 0) {
  6912. const int i = row*ncols + ib*n_dims + ic;
  6913. dst[i + 0] = x[i + 0];
  6914. dst[i + 1] = x[i + 1];
  6915. return;
  6916. }
  6917. const int i = row*ncols + ib*n_dims + ic/2;
  6918. const int i2 = row/p_delta_rows;
  6919. float cur_rot = inv_ndims * ic - ib;
  6920. const int p = has_pos ? pos[i2] : 0;
  6921. const float theta_base =
  6922. p * freq_scale * dpct::pow(theta_scale, col / 2.0f);
  6923. float cos_theta, sin_theta;
  6924. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  6925. const float x0 = x[i + 0];
  6926. const float x1 = x[i + n_dims/2];
  6927. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  6928. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  6929. }
  6930. static void rope_glm_f32(
  6931. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  6932. int n_ctx
  6933. , const sycl::nd_item<3> &item_ct1) {
  6934. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6935. item_ct1.get_local_id(2);
  6936. const int half_n_dims = ncols/4;
  6937. if (col >= half_n_dims) {
  6938. return;
  6939. }
  6940. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6941. item_ct1.get_local_id(1);
  6942. const int i = row*ncols + col;
  6943. const int i2 = row/p_delta_rows;
  6944. const float col_theta_scale = dpct::pow(freq_base, -2.0f * col / ncols);
  6945. // FIXME: this is likely wrong
  6946. const int p = pos != nullptr ? pos[i2] : 0;
  6947. const float theta = sycl::min(p, n_ctx - 2) * freq_scale * col_theta_scale;
  6948. const float sin_theta = sycl::sin((float)theta);
  6949. const float cos_theta = sycl::cos((float)theta);
  6950. const float x0 = x[i + 0];
  6951. const float x1 = x[i + half_n_dims];
  6952. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  6953. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  6954. const float block_theta =
  6955. ((float)sycl::max(p - n_ctx - 2, 0)) * col_theta_scale;
  6956. const float sin_block_theta = sycl::sin((float)block_theta);
  6957. const float cos_block_theta = sycl::cos((float)block_theta);
  6958. const float x2 = x[i + half_n_dims * 2];
  6959. const float x3 = x[i + half_n_dims * 3];
  6960. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  6961. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  6962. }
  6963. static void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  6964. const int n_heads_log2_floor, const float m0, const float m1,
  6965. const sycl::nd_item<3> &item_ct1) {
  6966. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  6967. item_ct1.get_local_id(2);
  6968. if (col >= ncols) {
  6969. return;
  6970. }
  6971. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  6972. item_ct1.get_local_id(1);
  6973. const int i = row*ncols + col;
  6974. const int k = row/k_rows;
  6975. float m_k;
  6976. if (k < n_heads_log2_floor) {
  6977. m_k = dpct::pow(m0, k + 1);
  6978. } else {
  6979. m_k = dpct::pow(m1, 2 * (k - n_heads_log2_floor) + 1);
  6980. }
  6981. dst[i] = col * m_k + x[i];
  6982. }
  6983. static void k_sum_rows_f32(const float * x, float * dst, const int ncols,
  6984. const sycl::nd_item<3> &item_ct1) {
  6985. const int row = item_ct1.get_group(1);
  6986. const int col = item_ct1.get_local_id(2);
  6987. float sum = 0.0f;
  6988. for (int i = col; i < ncols; i += item_ct1.get_local_range(2)) {
  6989. sum += x[row * ncols + i];
  6990. }
  6991. sum = warp_reduce_sum(sum, item_ct1);
  6992. if (col == 0) {
  6993. dst[row] = sum;
  6994. }
  6995. }
  6996. template<typename T>
  6997. static inline void swap(T & a, T & b) {
  6998. T tmp = a;
  6999. a = b;
  7000. b = tmp;
  7001. }
  7002. template<ggml_sort_order order>
  7003. static void k_argsort_f32_i32(const float * x, int * dst, const int ncols,
  7004. const sycl::nd_item<3> &item_ct1) {
  7005. // bitonic sort
  7006. int col = item_ct1.get_local_id(2);
  7007. int row = item_ct1.get_group(1);
  7008. if (col >= ncols) return;
  7009. const float * x_row = x + row * ncols;
  7010. int * dst_row = dst + row * ncols;
  7011. // initialize indices
  7012. if (col < ncols) {
  7013. dst_row[col] = col;
  7014. }
  7015. /*
  7016. DPCT1065:58: Consider replacing sycl::nd_item::barrier() with
  7017. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better
  7018. performance if there is no access to global memory.
  7019. */
  7020. item_ct1.barrier();
  7021. for (int k = 2; k <= ncols; k *= 2) {
  7022. for (int j = k / 2; j > 0; j /= 2) {
  7023. int ixj = col ^ j;
  7024. if (ixj > col) {
  7025. if ((col & k) == 0) {
  7026. if (order == GGML_SORT_ORDER_ASC ? x_row[dst_row[col]] > x_row[dst_row[ixj]] : x_row[dst_row[col]] < x_row[dst_row[ixj]]) {
  7027. swap(dst_row[col], dst_row[ixj]);
  7028. }
  7029. } else {
  7030. if (order == GGML_SORT_ORDER_ASC ? x_row[dst_row[col]] < x_row[dst_row[ixj]] : x_row[dst_row[col]] > x_row[dst_row[ixj]]) {
  7031. swap(dst_row[col], dst_row[ixj]);
  7032. }
  7033. }
  7034. }
  7035. /*
  7036. DPCT1118:11: SYCL group functions and algorithms must be encountered
  7037. in converged control flow. You may need to adjust the code.
  7038. */
  7039. /*
  7040. DPCT1065:59: Consider replacing sycl::nd_item::barrier() with
  7041. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  7042. better performance if there is no access to global memory.
  7043. */
  7044. item_ct1.barrier();
  7045. }
  7046. }
  7047. }
  7048. static void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past,
  7049. const sycl::nd_item<3> &item_ct1) {
  7050. const int col = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7051. item_ct1.get_local_id(1);
  7052. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7053. item_ct1.get_local_id(2);
  7054. if (col >= ncols) {
  7055. return;
  7056. }
  7057. const int i = row*ncols + col;
  7058. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  7059. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  7060. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  7061. }
  7062. template <bool vals_smem, int ncols_template, int block_size_template>
  7063. static void soft_max_f32(const float * x, const float * mask, const float *pos, float * dst, const int ncols_par,
  7064. const int nrows_y, const float scale, const float max_bias, const float m0,
  7065. const float m1, uint32_t n_head_log2, const sycl::nd_item<3> &item_ct1, float *buf) {
  7066. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  7067. const int tid = item_ct1.get_local_id(2);
  7068. const int rowx = item_ct1.get_group(2);
  7069. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  7070. const int block_size = block_size_template == 0 ? item_ct1.get_local_range(2) : block_size_template;
  7071. const int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  7072. const int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  7073. float slope = 0.0f;
  7074. // ALiBi
  7075. if (max_bias > 0.0f) {
  7076. const uint32_t h = rowx/nrows_y; // head index
  7077. const float base = h < n_head_log2 ? m0 : m1;
  7078. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  7079. slope = sycl::pow(base, float(exp));
  7080. }
  7081. float * vals = vals_smem ? buf + WARP_SIZE : dst + rowx*ncols;
  7082. float max_val = -INFINITY;
  7083. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7084. const int col = col0 + tid;
  7085. if (ncols_template == 0 && col >= ncols) {
  7086. break;
  7087. }
  7088. const int ix = rowx*ncols + col;
  7089. const int iy = rowy*ncols + col;
  7090. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
  7091. vals[col] = val;
  7092. max_val = sycl::max(max_val, val);
  7093. }
  7094. // find the max value in the block
  7095. max_val = warp_reduce_max(max_val, item_ct1);
  7096. if (block_size > WARP_SIZE) {
  7097. if (warp_id == 0) {
  7098. buf[lane_id] = -INFINITY;
  7099. }
  7100. item_ct1.barrier(sycl::access::fence_space::local_space);
  7101. if (lane_id == 0) {
  7102. buf[warp_id] = max_val;
  7103. }
  7104. item_ct1.barrier(sycl::access::fence_space::local_space);
  7105. max_val = buf[lane_id];
  7106. max_val = warp_reduce_max(max_val, item_ct1);
  7107. }
  7108. float tmp = 0.f;
  7109. #pragma unroll
  7110. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7111. const int col = col0 + tid;
  7112. if (ncols_template == 0 && col >= ncols) {
  7113. break;
  7114. }
  7115. const float val = sycl::native::exp(vals[col] - max_val);
  7116. tmp += val;
  7117. vals[col] = val;
  7118. }
  7119. // find the sum of exps in the block
  7120. tmp = warp_reduce_sum(tmp, item_ct1);
  7121. if (block_size > WARP_SIZE) {
  7122. if (warp_id == 0) {
  7123. buf[lane_id] = 0.f;
  7124. }
  7125. item_ct1.barrier(sycl::access::fence_space::local_space);
  7126. if (lane_id == 0) {
  7127. buf[warp_id] = tmp;
  7128. }
  7129. item_ct1.barrier(sycl::access::fence_space::local_space);
  7130. tmp = buf[lane_id];
  7131. tmp = warp_reduce_sum(tmp, item_ct1);
  7132. }
  7133. const float inv_sum = 1.f / tmp;
  7134. #pragma unroll
  7135. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7136. const int col = col0 + tid;
  7137. if (ncols_template == 0 && col >= ncols) {
  7138. return;
  7139. }
  7140. const int idst = rowx*ncols + col;
  7141. dst[idst] = vals[col] * inv_sum;
  7142. }
  7143. }
  7144. static void scale_f32(const float * x, float * dst, const float scale, const int k,
  7145. const sycl::nd_item<3> &item_ct1) {
  7146. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7147. item_ct1.get_local_id(2);
  7148. if (i >= k) {
  7149. return;
  7150. }
  7151. dst[i] = scale * x[i];
  7152. }
  7153. static void clamp_f32(const float * x, float * dst, const float min, const float max, const int k,
  7154. const sycl::nd_item<3> &item_ct1) {
  7155. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7156. item_ct1.get_local_id(2);
  7157. if (i >= k) {
  7158. return;
  7159. }
  7160. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  7161. }
  7162. template <typename T>
  7163. static void im2col_kernel(const float *x, T *dst, int offset_delta,
  7164. int IW, int IH, int OW, int KW, int KH,
  7165. int pelements, int CHW, int s0, int s1, int p0,
  7166. int p1, int d0, int d1,
  7167. const sycl::nd_item<3> &item_ct1) {
  7168. const int i = item_ct1.get_local_id(2) +
  7169. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  7170. if (i >= pelements) {
  7171. return;
  7172. }
  7173. const int ksize = OW * (KH > 1 ? KW : 1);
  7174. const int kx = i / ksize;
  7175. const int kd = kx * ksize;
  7176. const int ky = (i - kd) / OW;
  7177. const int ix = i % OW;
  7178. const int64_t iiw = ix * s0 + kx * d0 - p0;
  7179. const int64_t iih = item_ct1.get_group(1) * s1 + ky * d1 - p1;
  7180. const int64_t offset_dst =
  7181. (item_ct1.get_group(1) * OW + ix) * CHW +
  7182. (item_ct1.get_group(0) * (KW * KH) + ky * KW + kx);
  7183. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  7184. dst[offset_dst] =
  7185. sycl::vec<float, 1>(0.0f)
  7186. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7187. } else {
  7188. const int64_t offset_src = item_ct1.get_group(0) * offset_delta;
  7189. dst[offset_dst] =
  7190. sycl::vec<float, 1>(x[offset_src + iih * IW + iiw])
  7191. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7192. }
  7193. }
  7194. template <typename Ti, typename To>
  7195. static void pool2d_nchw_kernel(
  7196. const int ih, const int iw, const int oh, const int ow,
  7197. const int kh, const int kw, const int sh, const int sw,
  7198. const int ph, const int pw, const int parallel_elements,
  7199. const Ti* src, To* dst, const enum ggml_op_pool op,
  7200. const sycl::nd_item<3> &item_ct1) {
  7201. int idx = item_ct1.get_local_id(2) +
  7202. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  7203. if (idx >= parallel_elements) {
  7204. return;
  7205. }
  7206. const int I_HW = ih * iw;
  7207. const int O_HW = oh * ow;
  7208. const int nc = idx / O_HW;
  7209. const int cur_oh = idx % O_HW / ow;
  7210. const int cur_ow = idx % O_HW % ow;
  7211. const Ti* i_ptr = src + nc * I_HW;
  7212. To* o_ptr = dst + nc * O_HW;
  7213. const int start_h = cur_oh * sh - ph;
  7214. const int bh = sycl::max(0, start_h);
  7215. const int eh = sycl::min(ih, start_h + kh);
  7216. const int start_w = cur_ow * sw - pw;
  7217. const int bw = sycl::max(0, start_w);
  7218. const int ew = sycl::min(iw, start_w + kw);
  7219. To res = 0;
  7220. switch (op) {
  7221. case GGML_OP_POOL_AVG: res = 0; break;
  7222. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  7223. }
  7224. for (int i = bh; i < eh; i += 1) {
  7225. for (int j = bw; j < ew; j += 1) {
  7226. #if DPCT_COMPATIBILITY_TEMP >= 350
  7227. /*
  7228. DPCT1098:106: The '*' expression is used instead of the __ldg
  7229. call. These two expressions do not provide the exact same
  7230. functionality. Check the generated code for potential precision
  7231. and/or performance issues.
  7232. */
  7233. Ti cur = *(i_ptr + i * iw + j);
  7234. #else
  7235. Ti cur = i_ptr[i * iw + j];
  7236. #endif
  7237. switch (op) {
  7238. case GGML_OP_POOL_AVG: res += (cur / (kh * kw)); break;
  7239. case GGML_OP_POOL_MAX: res = sycl::max(res, (To)cur); break;
  7240. }
  7241. }
  7242. }
  7243. o_ptr[cur_oh * ow + cur_ow] = res;
  7244. }
  7245. template <int qk, int qr, dequantize_kernel_t dq>
  7246. static void get_rows_sycl(const ggml_tensor *src0, const ggml_tensor *src1,
  7247. ggml_tensor *dst, const void *src0_dd,
  7248. const int32_t *src1_dd, float *dst_dd,
  7249. dpct::queue_ptr stream) {
  7250. GGML_TENSOR_BINARY_OP_LOCALS
  7251. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  7252. const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
  7253. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  7254. // strides in elements
  7255. //const size_t s0 = nb0 / ggml_element_size(dst);
  7256. const size_t s1 = nb1 / ggml_element_size(dst);
  7257. const size_t s2 = nb2 / ggml_element_size(dst);
  7258. const size_t s3 = nb3 / ggml_element_size(dst);
  7259. const size_t s10 = nb10 / ggml_element_size(src1);
  7260. const size_t s11 = nb11 / ggml_element_size(src1);
  7261. const size_t s12 = nb12 / ggml_element_size(src1);
  7262. //const size_t s13 = nb13 / ggml_element_size(src1);
  7263. GGML_ASSERT(ne00 % 2 == 0);
  7264. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7265. [=](sycl::nd_item<3> item_ct1) {
  7266. k_get_rows<qk, qr, dq>(
  7267. src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  7268. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  7269. });
  7270. (void) dst;
  7271. }
  7272. template <typename src0_t>
  7273. static void get_rows_sycl_float(const ggml_tensor *src0,
  7274. const ggml_tensor *src1, ggml_tensor *dst,
  7275. const src0_t *src0_dd, const int32_t *src1_dd,
  7276. float *dst_dd, dpct::queue_ptr stream) {
  7277. GGML_TENSOR_BINARY_OP_LOCALS
  7278. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  7279. const int block_num_x = (ne00 + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
  7280. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  7281. // strides in elements
  7282. //const size_t s0 = nb0 / ggml_element_size(dst);
  7283. const size_t s1 = nb1 / ggml_element_size(dst);
  7284. const size_t s2 = nb2 / ggml_element_size(dst);
  7285. const size_t s3 = nb3 / ggml_element_size(dst);
  7286. const size_t s10 = nb10 / ggml_element_size(src1);
  7287. const size_t s11 = nb11 / ggml_element_size(src1);
  7288. const size_t s12 = nb12 / ggml_element_size(src1);
  7289. //const size_t s13 = nb13 / ggml_element_size(src1);
  7290. {
  7291. dpct::has_capability_or_fail(stream->get_device(),
  7292. {sycl::aspect::fp16});
  7293. stream->parallel_for(
  7294. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7295. [=](sycl::nd_item<3> item_ct1) {
  7296. k_get_rows_float(src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  7297. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  7298. });
  7299. }
  7300. (void) dst;
  7301. }
  7302. template<float (*bin_op)(const float, const float)>
  7303. struct bin_bcast_sycl {
  7304. template <typename src0_t, typename src1_t, typename dst_t>
  7305. void operator()(const struct ggml_tensor *src0,
  7306. const struct ggml_tensor *src1, struct ggml_tensor *dst,
  7307. const src0_t *src0_dd, const src1_t *src1_dd, dst_t *dst_dd,
  7308. dpct::queue_ptr stream) {
  7309. GGML_TENSOR_BINARY_OP_LOCALS
  7310. int nr0 = ne10/ne0;
  7311. int nr1 = ne11/ne1;
  7312. int nr2 = ne12/ne2;
  7313. int nr3 = ne13/ne3;
  7314. int nr[4] = { nr0, nr1, nr2, nr3 };
  7315. // collapse dimensions until first broadcast dimension
  7316. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  7317. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  7318. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  7319. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  7320. auto collapse = [](int64_t cne[]) {
  7321. cne[0] *= cne[1];
  7322. cne[1] = cne[2];
  7323. cne[2] = cne[3];
  7324. cne[3] = 1;
  7325. };
  7326. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  7327. cnb[1] *= cne[1];
  7328. cnb[2] *= cne[2];
  7329. cnb[3] *= cne[3];
  7330. };
  7331. for (int i = 0; i < 4; i++) {
  7332. if (nr[i] != 1) {
  7333. break;
  7334. }
  7335. if (i > 0) {
  7336. collapse_nb(cnb0, cne0);
  7337. collapse_nb(cnb1, cne1);
  7338. collapse(cne0);
  7339. collapse(cne1);
  7340. }
  7341. }
  7342. {
  7343. int64_t ne0 = cne0[0];
  7344. int64_t ne1 = cne0[1];
  7345. int64_t ne2 = cne0[2];
  7346. int64_t ne3 = cne0[3];
  7347. int64_t ne10 = cne1[0];
  7348. int64_t ne11 = cne1[1];
  7349. int64_t ne12 = cne1[2];
  7350. int64_t ne13 = cne1[3];
  7351. size_t nb0 = cnb0[0];
  7352. size_t nb1 = cnb0[1];
  7353. size_t nb2 = cnb0[2];
  7354. size_t nb3 = cnb0[3];
  7355. size_t nb10 = cnb1[0];
  7356. size_t nb11 = cnb1[1];
  7357. size_t nb12 = cnb1[2];
  7358. size_t nb13 = cnb1[3];
  7359. size_t s0 = nb0 / sizeof(dst_t);
  7360. size_t s1 = nb1 / sizeof(dst_t);
  7361. size_t s2 = nb2 / sizeof(dst_t);
  7362. size_t s3 = nb3 / sizeof(dst_t);
  7363. size_t s10 = nb10 / sizeof(src1_t);
  7364. size_t s11 = nb11 / sizeof(src1_t);
  7365. size_t s12 = nb12 / sizeof(src1_t);
  7366. size_t s13 = nb13 / sizeof(src1_t);
  7367. GGML_ASSERT(s0 == 1);
  7368. GGML_ASSERT(s10 == 1);
  7369. const int block_size = 128;
  7370. int64_t hne0 = std::max(ne0/2LL, 1LL);
  7371. sycl::range<3> block_dims(1, 1, 1);
  7372. block_dims[2] = std::min<unsigned int>(hne0, block_size);
  7373. block_dims[1] = std::min<unsigned int>(
  7374. ne1, block_size / (unsigned int)block_dims[2]);
  7375. block_dims[0] = std::min(
  7376. std::min<unsigned int>(
  7377. ne2 * ne3, block_size / (unsigned int)block_dims[2] /
  7378. (unsigned int)block_dims[1]),
  7379. 64U);
  7380. sycl::range<3> block_nums(
  7381. (ne2 * ne3 + block_dims[0] - 1) / block_dims[0],
  7382. (ne1 + block_dims[1] - 1) / block_dims[1],
  7383. (hne0 + block_dims[2] - 1) / block_dims[2]);
  7384. if (block_nums[0] > 65535) {
  7385. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  7386. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  7387. {
  7388. dpct::has_capability_or_fail(stream->get_device(),
  7389. {sycl::aspect::fp16});
  7390. stream->parallel_for(
  7391. sycl::nd_range<3>(sycl::range<3>(1, 1, block_num) *
  7392. sycl::range<3>(1, 1, block_size),
  7393. sycl::range<3>(1, 1, block_size)),
  7394. [=](sycl::nd_item<3> item_ct1) {
  7395. k_bin_bcast_unravel<bin_op>(
  7396. src0_dd, src1_dd, dst_dd, ne0, ne1, ne2, ne3,
  7397. ne10, ne11, ne12, ne13, s1, s2, s3, s11, s12,
  7398. s13, item_ct1);
  7399. });
  7400. }
  7401. } else {
  7402. /*
  7403. DPCT1049:16: The work-group size passed to the SYCL kernel may
  7404. exceed the limit. To get the device limit, query
  7405. info::device::max_work_group_size. Adjust the work-group size if
  7406. needed.
  7407. */
  7408. dpct::has_capability_or_fail(stream->get_device(),
  7409. {sycl::aspect::fp16});
  7410. stream->parallel_for(
  7411. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7412. [=](sycl::nd_item<3> item_ct1) {
  7413. k_bin_bcast<bin_op>(src0_dd, src1_dd, dst_dd, ne0, ne1,
  7414. ne2, ne3, ne10, ne11, ne12, ne13,
  7415. s1, s2, s3, s11, s12, s13,
  7416. item_ct1);
  7417. });
  7418. }
  7419. }
  7420. }
  7421. };
  7422. static void acc_f32_sycl(const float *x, const float *y, float *dst,
  7423. const int n_elements, const int ne10, const int ne11,
  7424. const int ne12, const int nb1, const int nb2,
  7425. const int offset, dpct::queue_ptr stream) {
  7426. int num_blocks = (n_elements + SYCL_ACC_BLOCK_SIZE - 1) / SYCL_ACC_BLOCK_SIZE;
  7427. stream->parallel_for(
  7428. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7429. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE),
  7430. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE)),
  7431. [=](sycl::nd_item<3> item_ct1) {
  7432. acc_f32(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset,
  7433. item_ct1);
  7434. });
  7435. }
  7436. static void gelu_f32_sycl(const float *x, float *dst, const int k,
  7437. dpct::queue_ptr stream) {
  7438. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  7439. stream->parallel_for(
  7440. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7441. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  7442. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  7443. [=](sycl::nd_item<3> item_ct1) {
  7444. gelu_f32(x, dst, k, item_ct1);
  7445. });
  7446. }
  7447. static void silu_f32_sycl(const float *x, float *dst, const int k,
  7448. dpct::queue_ptr stream) {
  7449. const int num_blocks = (k + SYCL_SILU_BLOCK_SIZE - 1) / SYCL_SILU_BLOCK_SIZE;
  7450. stream->parallel_for(
  7451. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7452. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE),
  7453. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE)),
  7454. [=](sycl::nd_item<3> item_ct1) {
  7455. silu_f32(x, dst, k, item_ct1);
  7456. });
  7457. }
  7458. static void gelu_quick_f32_sycl(const float *x, float *dst, const int k,
  7459. dpct::queue_ptr stream) {
  7460. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  7461. stream->parallel_for(
  7462. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7463. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  7464. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  7465. [=](sycl::nd_item<3> item_ct1) {
  7466. gelu_quick_f32(x, dst, k, item_ct1);
  7467. });
  7468. }
  7469. static void tanh_f32_sycl(const float *x, float *dst, const int k,
  7470. dpct::queue_ptr stream) {
  7471. const int num_blocks = (k + SYCL_TANH_BLOCK_SIZE - 1) / SYCL_TANH_BLOCK_SIZE;
  7472. stream->parallel_for(
  7473. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7474. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE),
  7475. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE)),
  7476. [=](sycl::nd_item<3> item_ct1) {
  7477. tanh_f32(x, dst, k, item_ct1);
  7478. });
  7479. }
  7480. static void relu_f32_sycl(const float *x, float *dst, const int k,
  7481. dpct::queue_ptr stream) {
  7482. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  7483. stream->parallel_for(
  7484. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7485. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  7486. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  7487. [=](sycl::nd_item<3> item_ct1) {
  7488. relu_f32(x, dst, k, item_ct1);
  7489. });
  7490. }
  7491. static void hardsigmoid_f32_sycl(const float *x, float *dst, const int k,
  7492. dpct::queue_ptr stream) {
  7493. const int num_blocks = (k + SYCL_HARDSIGMOID_BLOCK_SIZE - 1) / SYCL_HARDSIGMOID_BLOCK_SIZE;
  7494. stream->parallel_for(
  7495. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7496. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE),
  7497. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE)),
  7498. [=](sycl::nd_item<3> item_ct1) {
  7499. hardsigmoid_f32(x, dst, k, item_ct1);
  7500. });
  7501. }
  7502. static void hardswish_f32_sycl(const float *x, float *dst, const int k,
  7503. dpct::queue_ptr stream) {
  7504. const int num_blocks = (k + SYCL_HARDSWISH_BLOCK_SIZE - 1) / SYCL_HARDSWISH_BLOCK_SIZE;
  7505. stream->parallel_for(
  7506. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7507. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE),
  7508. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE)),
  7509. [=](sycl::nd_item<3> item_ct1) {
  7510. hardswish_f32(x, dst, k, item_ct1);
  7511. });
  7512. }
  7513. static void leaky_relu_f32_sycl(const float *x, float *dst, const int k,
  7514. const float negative_slope,
  7515. dpct::queue_ptr stream) {
  7516. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  7517. stream->parallel_for(
  7518. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7519. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  7520. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  7521. [=](sycl::nd_item<3> item_ct1) {
  7522. leaky_relu_f32(x, dst, k, negative_slope, item_ct1);
  7523. });
  7524. }
  7525. static void sqr_f32_sycl(const float *x, float *dst, const int k,
  7526. dpct::queue_ptr stream) {
  7527. const int num_blocks = (k + SYCL_SQR_BLOCK_SIZE - 1) / SYCL_SQR_BLOCK_SIZE;
  7528. stream->parallel_for(
  7529. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7530. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE),
  7531. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE)),
  7532. [=](sycl::nd_item<3> item_ct1) {
  7533. sqr_f32(x, dst, k, item_ct1);
  7534. });
  7535. }
  7536. static void norm_f32_sycl(const float *x, float *dst, const int ncols,
  7537. const int nrows, const float eps,
  7538. dpct::queue_ptr stream) {
  7539. GGML_ASSERT(ncols % WARP_SIZE == 0);
  7540. if (ncols < 1024) {
  7541. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  7542. stream->submit([&](sycl::handler &cgh) {
  7543. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  7544. sycl::range<1>(32), cgh);
  7545. cgh.parallel_for(
  7546. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  7547. block_dims),
  7548. [=](sycl::nd_item<3> item_ct1)
  7549. [[intel::reqd_sub_group_size(32)]] {
  7550. norm_f32(x, dst, ncols, eps, item_ct1,
  7551. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  7552. });
  7553. });
  7554. } else {
  7555. const int work_group_size = g_work_group_size;
  7556. const sycl::range<3> block_dims(1, 1, work_group_size);
  7557. /*
  7558. DPCT1049:17: The work-group size passed to the SYCL kernel may exceed
  7559. the limit. To get the device limit, query
  7560. info::device::max_work_group_size. Adjust the work-group size if needed.
  7561. */
  7562. stream->submit([&](sycl::handler &cgh) {
  7563. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  7564. sycl::range<1>(32), cgh);
  7565. cgh.parallel_for(
  7566. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  7567. block_dims),
  7568. [=](sycl::nd_item<3> item_ct1)
  7569. [[intel::reqd_sub_group_size(32)]] {
  7570. norm_f32(x, dst, ncols, eps, item_ct1,
  7571. s_sum_acc_ct1.get_pointer(), work_group_size);
  7572. });
  7573. });
  7574. }
  7575. }
  7576. static void group_norm_f32_sycl(const float *x, float *dst,
  7577. const int num_groups, const int group_size,
  7578. const int ne_elements, dpct::queue_ptr stream) {
  7579. static const float eps = 1e-6f;
  7580. if (group_size < 1024) {
  7581. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  7582. stream->submit([&](sycl::handler &cgh) {
  7583. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  7584. cgh);
  7585. const float eps_ct4 = eps;
  7586. cgh.parallel_for(
  7587. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  7588. block_dims),
  7589. [=](sycl::nd_item<3> item_ct1)
  7590. [[intel::reqd_sub_group_size(32)]] {
  7591. group_norm_f32(
  7592. x, dst, group_size, ne_elements, eps_ct4, item_ct1,
  7593. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  7594. });
  7595. });
  7596. } else {
  7597. const int work_group_size = g_work_group_size;
  7598. const sycl::range<3> block_dims(1, 1, work_group_size);
  7599. /*
  7600. DPCT1049:18: The work-group size passed to the SYCL kernel may exceed
  7601. the limit. To get the device limit, query
  7602. info::device::max_work_group_size. Adjust the work-group size if needed.
  7603. */
  7604. stream->submit([&](sycl::handler &cgh) {
  7605. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  7606. cgh);
  7607. const float eps_ct4 = eps;
  7608. cgh.parallel_for(
  7609. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  7610. block_dims),
  7611. [=](sycl::nd_item<3> item_ct1)
  7612. [[intel::reqd_sub_group_size(32)]] {
  7613. group_norm_f32(x, dst, group_size, ne_elements,
  7614. eps_ct4, item_ct1,
  7615. s_sum_acc_ct1.get_pointer(), work_group_size);
  7616. });
  7617. });
  7618. }
  7619. }
  7620. static void concat_f32_sycl(const float *x, const float *y, float *dst,
  7621. const int ne0, int ne1, int ne2, int ne02,
  7622. dpct::queue_ptr stream) {
  7623. int num_blocks = (ne0 + SYCL_CONCAT_BLOCK_SIZE - 1) / SYCL_CONCAT_BLOCK_SIZE;
  7624. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  7625. stream->parallel_for(
  7626. sycl::nd_range<3>(gridDim *
  7627. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE),
  7628. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE)),
  7629. [=](sycl::nd_item<3> item_ct1) {
  7630. concat_f32(x, y, dst, ne0, ne02, item_ct1);
  7631. });
  7632. }
  7633. static void upscale_f32_sycl(const float *x, float *dst, const int ne00,
  7634. const int ne01, const int ne02,
  7635. const int scale_factor, dpct::queue_ptr stream) {
  7636. int ne0 = (ne00 * scale_factor);
  7637. int num_blocks = (ne0 + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE;
  7638. sycl::range<3> gridDim(ne02, (ne01 * scale_factor), num_blocks);
  7639. stream->parallel_for(
  7640. sycl::nd_range<3>(gridDim *
  7641. sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE),
  7642. sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE)),
  7643. [=](sycl::nd_item<3> item_ct1) {
  7644. upscale_f32(x, dst, ne00, ne00 * ne01, scale_factor, item_ct1);
  7645. });
  7646. }
  7647. static void pad_f32_sycl(const float *x, float *dst, const int ne00,
  7648. const int ne01, const int ne02, const int ne0,
  7649. const int ne1, const int ne2, dpct::queue_ptr stream) {
  7650. int num_blocks = (ne0 + SYCL_PAD_BLOCK_SIZE - 1) / SYCL_PAD_BLOCK_SIZE;
  7651. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  7652. stream->parallel_for(
  7653. sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE),
  7654. sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE)),
  7655. [=](sycl::nd_item<3> item_ct1) {
  7656. pad_f32(x, dst, ne0, ne00, ne01, ne02, item_ct1);
  7657. });
  7658. }
  7659. static void rms_norm_f32_sycl(const float *x, float *dst, const int ncols,
  7660. const int nrows, const float eps,
  7661. dpct::queue_ptr stream) {
  7662. GGML_ASSERT(ncols % WARP_SIZE == 0);
  7663. // printf("%s ncols=%d, nrows=%d, WARP_SIZE=%d\n", __func__, ncols, nrows, WARP_SIZE);
  7664. if (ncols < 1024) {
  7665. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  7666. stream->submit([&](sycl::handler &cgh) {
  7667. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  7668. cgh);
  7669. cgh.parallel_for(
  7670. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  7671. block_dims),
  7672. [=](sycl::nd_item<3> item_ct1)
  7673. [[intel::reqd_sub_group_size(32)]] {
  7674. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  7675. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  7676. });
  7677. });
  7678. } else {
  7679. const int work_group_size = g_work_group_size;
  7680. const sycl::range<3> block_dims(1, 1, work_group_size);
  7681. /*
  7682. DPCT1049:19: The work-group size passed to the SYCL kernel may exceed
  7683. the limit. To get the device limit, query
  7684. info::device::max_work_group_size. Adjust the work-group size if needed.
  7685. */
  7686. stream->submit([&](sycl::handler &cgh) {
  7687. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  7688. cgh);
  7689. cgh.parallel_for(
  7690. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  7691. block_dims),
  7692. [=](sycl::nd_item<3> item_ct1)
  7693. [[intel::reqd_sub_group_size(32)]] {
  7694. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  7695. s_sum_acc_ct1.get_pointer(), work_group_size);
  7696. });
  7697. });
  7698. }
  7699. }
  7700. static void quantize_row_q8_1_sycl(const float *x, void *vy, const int kx,
  7701. const int ky, const int kx_padded,
  7702. dpct::queue_ptr stream) {
  7703. const int block_num_x = (kx_padded + SYCL_QUANTIZE_BLOCK_SIZE - 1) / SYCL_QUANTIZE_BLOCK_SIZE;
  7704. const sycl::range<3> num_blocks(1, ky, block_num_x);
  7705. const sycl::range<3> block_size(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE);
  7706. {
  7707. dpct::has_capability_or_fail(stream->get_device(),
  7708. {sycl::aspect::fp16});
  7709. stream->parallel_for(
  7710. sycl::nd_range<3>(num_blocks * block_size, block_size),
  7711. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7712. quantize_q8_1(x, vy, kx, kx_padded, item_ct1);
  7713. });
  7714. }
  7715. }
  7716. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  7717. static void dequantize_block_sycl(const void *__restrict__ vx,
  7718. dst_t *__restrict__ y, const int k,
  7719. dpct::queue_ptr stream) {
  7720. const int num_blocks = (k + 2*SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / (2*SYCL_DEQUANTIZE_BLOCK_SIZE);
  7721. {
  7722. dpct::has_capability_or_fail(stream->get_device(),
  7723. {sycl::aspect::fp16});
  7724. stream->parallel_for(
  7725. sycl::nd_range<3>(
  7726. sycl::range<3>(1, 1, num_blocks) *
  7727. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  7728. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  7729. [=](sycl::nd_item<3> item_ct1) {
  7730. dequantize_block<qk, qr, dequantize_kernel>(vx, y, k, item_ct1);
  7731. });
  7732. }
  7733. }
  7734. template <typename dst_t>
  7735. static void dequantize_row_q2_K_sycl(const void *vx, dst_t *y, const int k,
  7736. dpct::queue_ptr stream) {
  7737. const int nb = k / QK_K;
  7738. #if QK_K == 256
  7739. {
  7740. dpct::has_capability_or_fail(stream->get_device(),
  7741. {sycl::aspect::fp16});
  7742. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7743. sycl::range<3>(1, 1, 64),
  7744. sycl::range<3>(1, 1, 64)),
  7745. [=](sycl::nd_item<3> item_ct1) {
  7746. dequantize_block_q2_K(vx, y, item_ct1);
  7747. });
  7748. }
  7749. #else
  7750. dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
  7751. #endif
  7752. }
  7753. template <typename dst_t>
  7754. static void dequantize_row_q3_K_sycl(const void *vx, dst_t *y, const int k,
  7755. dpct::queue_ptr stream) {
  7756. const int nb = k / QK_K;
  7757. #if QK_K == 256
  7758. {
  7759. dpct::has_capability_or_fail(stream->get_device(),
  7760. {sycl::aspect::fp16});
  7761. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7762. sycl::range<3>(1, 1, 64),
  7763. sycl::range<3>(1, 1, 64)),
  7764. [=](sycl::nd_item<3> item_ct1) {
  7765. dequantize_block_q3_K(vx, y, item_ct1);
  7766. });
  7767. }
  7768. #else
  7769. dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
  7770. #endif
  7771. }
  7772. template <typename dst_t>
  7773. static void dequantize_row_q4_K_sycl(const void *vx, dst_t *y, const int k,
  7774. dpct::queue_ptr stream) {
  7775. const int nb = k / QK_K;
  7776. {
  7777. dpct::has_capability_or_fail(stream->get_device(),
  7778. {sycl::aspect::fp16});
  7779. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7780. sycl::range<3>(1, 1, 32),
  7781. sycl::range<3>(1, 1, 32)),
  7782. [=](sycl::nd_item<3> item_ct1) {
  7783. dequantize_block_q4_K(vx, y, item_ct1);
  7784. });
  7785. }
  7786. }
  7787. template <typename dst_t>
  7788. static void dequantize_row_q5_K_sycl(const void *vx, dst_t *y, const int k,
  7789. dpct::queue_ptr stream) {
  7790. const int nb = k / QK_K;
  7791. #if QK_K == 256
  7792. {
  7793. dpct::has_capability_or_fail(stream->get_device(),
  7794. {sycl::aspect::fp16});
  7795. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7796. sycl::range<3>(1, 1, 64),
  7797. sycl::range<3>(1, 1, 64)),
  7798. [=](sycl::nd_item<3> item_ct1) {
  7799. dequantize_block_q5_K(vx, y, item_ct1);
  7800. });
  7801. }
  7802. #else
  7803. dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
  7804. #endif
  7805. }
  7806. template <typename dst_t>
  7807. static void dequantize_row_q6_K_sycl(const void *vx, dst_t *y, const int k,
  7808. dpct::queue_ptr stream) {
  7809. const int nb = k / QK_K;
  7810. #if QK_K == 256
  7811. {
  7812. dpct::has_capability_or_fail(stream->get_device(),
  7813. {sycl::aspect::fp16});
  7814. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  7815. sycl::range<3>(1, 1, 64),
  7816. sycl::range<3>(1, 1, 64)),
  7817. [=](sycl::nd_item<3> item_ct1) {
  7818. dequantize_block_q6_K(vx, y, item_ct1);
  7819. });
  7820. }
  7821. #else
  7822. dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
  7823. #endif
  7824. }
  7825. static to_fp16_sycl_t ggml_get_to_fp16_sycl(ggml_type type) {
  7826. switch (type) {
  7827. case GGML_TYPE_Q4_0:
  7828. return dequantize_block_sycl<QK4_0, QR4_0, dequantize_q4_0>;
  7829. case GGML_TYPE_Q4_1:
  7830. return dequantize_block_sycl<QK4_1, QR4_1, dequantize_q4_1>;
  7831. case GGML_TYPE_Q5_0:
  7832. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  7833. case GGML_TYPE_Q5_1:
  7834. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  7835. case GGML_TYPE_Q8_0:
  7836. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  7837. case GGML_TYPE_Q2_K:
  7838. return dequantize_row_q2_K_sycl;
  7839. case GGML_TYPE_Q3_K:
  7840. return dequantize_row_q3_K_sycl;
  7841. case GGML_TYPE_Q4_K:
  7842. return dequantize_row_q4_K_sycl;
  7843. case GGML_TYPE_Q5_K:
  7844. return dequantize_row_q5_K_sycl;
  7845. case GGML_TYPE_Q6_K:
  7846. return dequantize_row_q6_K_sycl;
  7847. case GGML_TYPE_F32:
  7848. return dequantize_block_sycl<1, 1, convert_f32>;
  7849. default:
  7850. return nullptr;
  7851. }
  7852. }
  7853. static to_fp32_sycl_t ggml_get_to_fp32_sycl(ggml_type type) {
  7854. switch (type) {
  7855. case GGML_TYPE_Q4_0:
  7856. return dequantize_block_sycl<QK4_0, QR4_0, dequantize_q4_0>;
  7857. case GGML_TYPE_Q4_1:
  7858. return dequantize_block_sycl<QK4_1, QR4_1, dequantize_q4_1>;
  7859. case GGML_TYPE_Q5_0:
  7860. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  7861. case GGML_TYPE_Q5_1:
  7862. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  7863. case GGML_TYPE_Q8_0:
  7864. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  7865. case GGML_TYPE_Q2_K:
  7866. return dequantize_row_q2_K_sycl;
  7867. case GGML_TYPE_Q3_K:
  7868. return dequantize_row_q3_K_sycl;
  7869. case GGML_TYPE_Q4_K:
  7870. return dequantize_row_q4_K_sycl;
  7871. case GGML_TYPE_Q5_K:
  7872. return dequantize_row_q5_K_sycl;
  7873. case GGML_TYPE_Q6_K:
  7874. return dequantize_row_q6_K_sycl;
  7875. case GGML_TYPE_F16:
  7876. return dequantize_block_sycl<1, 1, convert_f16>;
  7877. default:
  7878. return nullptr;
  7879. }
  7880. }
  7881. static void dequantize_mul_mat_vec_q4_0_sycl(const void *vx, const dfloat *y,
  7882. float *dst, const int ncols,
  7883. const int nrows,
  7884. dpct::queue_ptr stream) {
  7885. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7886. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7887. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  7888. const sycl::range<3> block_nums(1, 1, block_num_y);
  7889. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7890. {
  7891. dpct::has_capability_or_fail(stream->get_device(),
  7892. {sycl::aspect::fp16});
  7893. stream->parallel_for(
  7894. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7895. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7896. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>(
  7897. vx, y, dst, ncols, nrows, item_ct1);
  7898. });
  7899. }
  7900. }
  7901. static void dequantize_mul_mat_vec_q4_1_sycl(const void *vx, const dfloat *y,
  7902. float *dst, const int ncols,
  7903. const int nrows,
  7904. dpct::queue_ptr stream) {
  7905. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7906. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7907. const sycl::range<3> block_nums(1, 1, block_num_y);
  7908. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7909. {
  7910. dpct::has_capability_or_fail(stream->get_device(),
  7911. {sycl::aspect::fp16});
  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. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>(
  7916. vx, y, dst, ncols, nrows, item_ct1);
  7917. });
  7918. }
  7919. }
  7920. static void dequantize_mul_mat_vec_q5_0_sycl(const void *vx, const dfloat *y,
  7921. float *dst, const int ncols,
  7922. const int nrows,
  7923. dpct::queue_ptr stream) {
  7924. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 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. {
  7929. dpct::has_capability_or_fail(stream->get_device(),
  7930. {sycl::aspect::fp16});
  7931. stream->parallel_for(
  7932. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7933. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7934. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>(
  7935. vx, y, dst, ncols, nrows, item_ct1);
  7936. });
  7937. }
  7938. }
  7939. static void dequantize_mul_mat_vec_q5_1_sycl(const void *vx, const dfloat *y,
  7940. float *dst, const int ncols,
  7941. const int nrows,
  7942. dpct::queue_ptr stream) {
  7943. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7944. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7945. const sycl::range<3> block_nums(1, 1, block_num_y);
  7946. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7947. {
  7948. dpct::has_capability_or_fail(stream->get_device(),
  7949. {sycl::aspect::fp16});
  7950. stream->parallel_for(
  7951. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7952. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7953. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>(
  7954. vx, y, dst, ncols, nrows, item_ct1);
  7955. });
  7956. }
  7957. }
  7958. static void dequantize_mul_mat_vec_q8_0_sycl(const void *vx, const dfloat *y,
  7959. float *dst, const int ncols,
  7960. const int nrows,
  7961. dpct::queue_ptr stream) {
  7962. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  7963. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  7964. const sycl::range<3> block_nums(1, 1, block_num_y);
  7965. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  7966. {
  7967. dpct::has_capability_or_fail(stream->get_device(),
  7968. {sycl::aspect::fp16});
  7969. stream->parallel_for(
  7970. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7971. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7972. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>(
  7973. vx, y, dst, ncols, nrows, item_ct1);
  7974. });
  7975. }
  7976. }
  7977. static void dequantize_mul_mat_vec_q2_K_sycl(const void *vx, const float *y,
  7978. float *dst, const int ncols,
  7979. const int nrows,
  7980. dpct::queue_ptr stream) {
  7981. GGML_ASSERT(ncols % QK_K == 0);
  7982. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  7983. const int block_num_y = (nrows + ny - 1) / ny;
  7984. const sycl::range<3> block_nums(1, 1, block_num_y);
  7985. const sycl::range<3> block_dims(1, ny, 32);
  7986. stream->parallel_for(
  7987. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7988. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  7989. dequantize_mul_mat_vec_q2_k(vx, y, dst, ncols, nrows, item_ct1);
  7990. });
  7991. }
  7992. static void dequantize_mul_mat_vec_q3_K_sycl(const void *vx, const float *y,
  7993. float *dst, const int ncols,
  7994. const int nrows,
  7995. dpct::queue_ptr stream) {
  7996. GGML_ASSERT(ncols % QK_K == 0);
  7997. const int ny = 2 / K_QUANTS_PER_ITERATION;
  7998. const int block_num_y = (nrows + ny - 1) / ny;
  7999. const sycl::range<3> block_nums(1, 1, block_num_y);
  8000. const sycl::range<3> block_dims(1, ny, 32);
  8001. stream->parallel_for(
  8002. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8003. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8004. dequantize_mul_mat_vec_q3_k(vx, y, dst, ncols, nrows, item_ct1);
  8005. });
  8006. }
  8007. static void dequantize_mul_mat_vec_q4_K_sycl(const void *vx, const float *y,
  8008. float *dst, const int ncols,
  8009. const int nrows,
  8010. dpct::queue_ptr stream) {
  8011. GGML_ASSERT(ncols % QK_K == 0);
  8012. const int ny = 2 / K_QUANTS_PER_ITERATION;
  8013. const int block_num_y = (nrows + ny - 1) / ny;
  8014. const sycl::range<3> block_nums(1, 1, block_num_y);
  8015. const sycl::range<3> block_dims(1, ny, 32);
  8016. stream->parallel_for(
  8017. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8018. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8019. dequantize_mul_mat_vec_q4_k(vx, y, dst, ncols, nrows, item_ct1);
  8020. });
  8021. }
  8022. static void dequantize_mul_mat_vec_q5_K_sycl(const void *vx, const float *y,
  8023. float *dst, const int ncols,
  8024. const int nrows,
  8025. dpct::queue_ptr stream) {
  8026. GGML_ASSERT(ncols % QK_K == 0);
  8027. const sycl::range<3> block_dims(1, 1, 32);
  8028. stream->parallel_for(
  8029. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims, block_dims),
  8030. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8031. dequantize_mul_mat_vec_q5_k(vx, y, dst, ncols, item_ct1);
  8032. });
  8033. }
  8034. static void dequantize_mul_mat_vec_q6_K_sycl(const void *vx, const float *y,
  8035. float *dst, const int ncols,
  8036. const int nrows,
  8037. dpct::queue_ptr stream) {
  8038. GGML_ASSERT(ncols % QK_K == 0);
  8039. const int ny = 2 / K_QUANTS_PER_ITERATION;
  8040. const int block_num_y = (nrows + ny - 1) / ny;
  8041. const sycl::range<3> block_nums(1, 1, block_num_y);
  8042. const sycl::range<3> block_dims(1, ny, 32);
  8043. stream->parallel_for(
  8044. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8045. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8046. dequantize_mul_mat_vec_q6_k(vx, y, dst, ncols, nrows, item_ct1);
  8047. });
  8048. }
  8049. static void convert_mul_mat_vec_f16_sycl(const void *vx, const dfloat *y,
  8050. float *dst, const int ncols,
  8051. const int nrows,
  8052. dpct::queue_ptr stream) {
  8053. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8054. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8055. const sycl::range<3> block_nums(1, 1, block_num_y);
  8056. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8057. {
  8058. dpct::has_capability_or_fail(stream->get_device(),
  8059. {sycl::aspect::fp16});
  8060. stream->parallel_for(
  8061. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8062. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8063. dequantize_mul_mat_vec<1, 1, convert_f16>(vx, y, dst, ncols,
  8064. nrows, item_ct1);
  8065. });
  8066. }
  8067. }
  8068. template <int qk, int qi, typename block_q_t, int vdr,
  8069. vec_dot_q_sycl_t vec_dot_q_sycl>
  8070. static void mul_mat_vec_q_sycl_submitter(const void *vx, const void *vy,
  8071. float *dst, const int ncols,
  8072. const int nrows,
  8073. dpct::queue_ptr stream) {
  8074. GGML_ASSERT(ncols % QK4_0 == 0);
  8075. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8076. const sycl::range<3> block_nums(1, 1, block_num_y);
  8077. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8078. stream->parallel_for(
  8079. sycl::nd_range<3>(block_nums * block_dims, block_dims), [=
  8080. ](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8081. mul_mat_vec_q<qk, qi, block_q_t, vdr, vec_dot_q_sycl>(
  8082. vx, vy, dst, ncols, nrows, item_ct1);
  8083. });
  8084. }
  8085. static void ggml_mul_mat_q4_0_q8_1_sycl(const void *vx, const void *vy,
  8086. float *dst, const int ncols_x,
  8087. const int nrows_x, const int ncols_y,
  8088. const int nrows_y, const int nrows_dst,
  8089. dpct::queue_ptr stream) try {
  8090. int id;
  8091. SYCL_CHECK(
  8092. CHECK_TRY_ERROR(id = get_current_device_id()));
  8093. const int compute_capability = g_device_caps[id].cc;
  8094. int mmq_x, mmq_y, nwarps;
  8095. if (compute_capability >= VER_GEN13) {
  8096. mmq_x = MMQ_X_Q4_0_RDNA2;
  8097. mmq_y = MMQ_Y_Q4_0_RDNA2;
  8098. nwarps = NWARPS_Q4_0_RDNA2;
  8099. } else if (compute_capability >= VER_GEN12) {
  8100. mmq_x = MMQ_X_Q4_0_RDNA1;
  8101. mmq_y = MMQ_Y_Q4_0_RDNA1;
  8102. nwarps = NWARPS_Q4_0_RDNA1;
  8103. } else if (compute_capability >= VER_GEN9) {
  8104. mmq_x = MMQ_X_Q4_0_AMPERE;
  8105. mmq_y = MMQ_Y_Q4_0_AMPERE;
  8106. nwarps = NWARPS_Q4_0_AMPERE;
  8107. } else if (compute_capability >= VER_4VEC) {
  8108. mmq_x = MMQ_X_Q4_0_PASCAL;
  8109. mmq_y = MMQ_Y_Q4_0_PASCAL;
  8110. nwarps = NWARPS_Q4_0_PASCAL;
  8111. } else {
  8112. GGML_ASSERT(false);
  8113. }
  8114. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8115. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8116. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8117. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8118. if (nrows_x % mmq_y == 0) {
  8119. const bool need_check = false;
  8120. /*
  8121. DPCT1049:20: The work-group size passed to the SYCL kernel may exceed
  8122. the limit. To get the device limit, query
  8123. info::device::max_work_group_size. Adjust the work-group size if needed.
  8124. */
  8125. {
  8126. dpct::has_capability_or_fail(stream->get_device(),
  8127. {sycl::aspect::fp16});
  8128. stream->submit([&](sycl::handler &cgh) {
  8129. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  8130. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8131. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  8132. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  8133. cgh);
  8134. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8135. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8136. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8137. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8138. cgh.parallel_for(
  8139. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8140. [=](sycl::nd_item<3> item_ct1) {
  8141. mul_mat_q4_0<need_check>(
  8142. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8143. nrows_dst, item_ct1,
  8144. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  8145. tile_x_d_q4_0_acc_ct1.get_pointer(),
  8146. tile_y_qs_acc_ct1.get_pointer(),
  8147. tile_y_ds_acc_ct1.get_pointer());
  8148. });
  8149. });
  8150. }
  8151. } else {
  8152. const bool need_check = true;
  8153. /*
  8154. DPCT1049:21: The work-group size passed to the SYCL kernel may exceed
  8155. the limit. To get the device limit, query
  8156. info::device::max_work_group_size. Adjust the work-group size if needed.
  8157. */
  8158. {
  8159. dpct::has_capability_or_fail(stream->get_device(),
  8160. {sycl::aspect::fp16});
  8161. stream->submit([&](sycl::handler &cgh) {
  8162. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  8163. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8164. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  8165. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  8166. cgh);
  8167. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8168. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8169. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8170. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8171. cgh.parallel_for(
  8172. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8173. [=](sycl::nd_item<3> item_ct1) {
  8174. mul_mat_q4_0<need_check>(
  8175. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8176. nrows_dst, item_ct1,
  8177. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  8178. tile_x_d_q4_0_acc_ct1.get_pointer(),
  8179. tile_y_qs_acc_ct1.get_pointer(),
  8180. tile_y_ds_acc_ct1.get_pointer());
  8181. });
  8182. });
  8183. }
  8184. }
  8185. }
  8186. catch (sycl::exception const &exc) {
  8187. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8188. << ", line:" << __LINE__ << std::endl;
  8189. std::exit(1);
  8190. }
  8191. static void ggml_mul_mat_q4_1_q8_1_sycl(const void *vx, const void *vy,
  8192. float *dst, const int ncols_x,
  8193. const int nrows_x, const int ncols_y,
  8194. const int nrows_y, const int nrows_dst,
  8195. dpct::queue_ptr stream) try {
  8196. int id;
  8197. SYCL_CHECK(
  8198. CHECK_TRY_ERROR(id = get_current_device_id()));
  8199. const int compute_capability = g_device_caps[id].cc;
  8200. int mmq_x, mmq_y, nwarps;
  8201. if (compute_capability >= VER_GEN13) {
  8202. mmq_x = MMQ_X_Q4_1_RDNA2;
  8203. mmq_y = MMQ_Y_Q4_1_RDNA2;
  8204. nwarps = NWARPS_Q4_1_RDNA2;
  8205. } else if (compute_capability >= VER_GEN12) {
  8206. mmq_x = MMQ_X_Q4_1_RDNA1;
  8207. mmq_y = MMQ_Y_Q4_1_RDNA1;
  8208. nwarps = NWARPS_Q4_1_RDNA1;
  8209. } else if (compute_capability >= VER_GEN9) {
  8210. mmq_x = MMQ_X_Q4_1_AMPERE;
  8211. mmq_y = MMQ_Y_Q4_1_AMPERE;
  8212. nwarps = NWARPS_Q4_1_AMPERE;
  8213. } else if (compute_capability >= VER_4VEC) {
  8214. mmq_x = MMQ_X_Q4_1_PASCAL;
  8215. mmq_y = MMQ_Y_Q4_1_PASCAL;
  8216. nwarps = NWARPS_Q4_1_PASCAL;
  8217. } else {
  8218. GGML_ASSERT(false);
  8219. }
  8220. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8221. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8222. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8223. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8224. if (nrows_x % mmq_y == 0) {
  8225. const bool need_check = false;
  8226. /*
  8227. DPCT1049:22: The work-group size passed to the SYCL kernel may exceed
  8228. the limit. To get the device limit, query
  8229. info::device::max_work_group_size. Adjust the work-group size if needed.
  8230. */
  8231. {
  8232. dpct::has_capability_or_fail(stream->get_device(),
  8233. {sycl::aspect::fp16});
  8234. stream->submit([&](sycl::handler &cgh) {
  8235. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  8236. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  8237. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  8238. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  8239. cgh);
  8240. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8241. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8242. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8243. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8244. cgh.parallel_for(
  8245. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8246. [=](sycl::nd_item<3> item_ct1) {
  8247. mul_mat_q4_1<need_check>(
  8248. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8249. nrows_dst, item_ct1,
  8250. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  8251. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  8252. tile_y_qs_acc_ct1.get_pointer(),
  8253. tile_y_ds_acc_ct1.get_pointer());
  8254. });
  8255. });
  8256. }
  8257. } else {
  8258. const bool need_check = true;
  8259. /*
  8260. DPCT1049:23: The work-group size passed to the SYCL kernel may exceed
  8261. the limit. To get the device limit, query
  8262. info::device::max_work_group_size. Adjust the work-group size if needed.
  8263. */
  8264. {
  8265. dpct::has_capability_or_fail(stream->get_device(),
  8266. {sycl::aspect::fp16});
  8267. stream->submit([&](sycl::handler &cgh) {
  8268. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  8269. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  8270. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  8271. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  8272. cgh);
  8273. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8274. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8275. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8276. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8277. cgh.parallel_for(
  8278. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8279. [=](sycl::nd_item<3> item_ct1) {
  8280. mul_mat_q4_1<need_check>(
  8281. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8282. nrows_dst, item_ct1,
  8283. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  8284. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  8285. tile_y_qs_acc_ct1.get_pointer(),
  8286. tile_y_ds_acc_ct1.get_pointer());
  8287. });
  8288. });
  8289. }
  8290. }
  8291. }
  8292. catch (sycl::exception const &exc) {
  8293. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8294. << ", line:" << __LINE__ << std::endl;
  8295. std::exit(1);
  8296. }
  8297. static void ggml_mul_mat_q5_0_q8_1_sycl(const void *vx, const void *vy,
  8298. float *dst, const int ncols_x,
  8299. const int nrows_x, const int ncols_y,
  8300. const int nrows_y, const int nrows_dst,
  8301. dpct::queue_ptr stream) try {
  8302. int id;
  8303. SYCL_CHECK(
  8304. CHECK_TRY_ERROR(id = get_current_device_id()));
  8305. const int compute_capability = g_device_caps[id].cc;
  8306. int mmq_x, mmq_y, nwarps;
  8307. if (compute_capability >= VER_GEN13) {
  8308. mmq_x = MMQ_X_Q5_0_RDNA2;
  8309. mmq_y = MMQ_Y_Q5_0_RDNA2;
  8310. nwarps = NWARPS_Q5_0_RDNA2;
  8311. } else if (compute_capability >= VER_GEN12) {
  8312. mmq_x = MMQ_X_Q5_0_RDNA1;
  8313. mmq_y = MMQ_Y_Q5_0_RDNA1;
  8314. nwarps = NWARPS_Q5_0_RDNA1;
  8315. } else if (compute_capability >= VER_GEN9) {
  8316. mmq_x = MMQ_X_Q5_0_AMPERE;
  8317. mmq_y = MMQ_Y_Q5_0_AMPERE;
  8318. nwarps = NWARPS_Q5_0_AMPERE;
  8319. } else if (compute_capability >= VER_4VEC) {
  8320. mmq_x = MMQ_X_Q5_0_PASCAL;
  8321. mmq_y = MMQ_Y_Q5_0_PASCAL;
  8322. nwarps = NWARPS_Q5_0_PASCAL;
  8323. } else {
  8324. GGML_ASSERT(false);
  8325. }
  8326. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8327. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8328. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8329. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8330. if (nrows_x % mmq_y == 0) {
  8331. const bool need_check = false;
  8332. /*
  8333. DPCT1049:24: The work-group size passed to the SYCL kernel may exceed
  8334. the limit. To get the device limit, query
  8335. info::device::max_work_group_size. Adjust the work-group size if needed.
  8336. */
  8337. {
  8338. dpct::has_capability_or_fail(stream->get_device(),
  8339. {sycl::aspect::fp16});
  8340. stream->submit([&](sycl::handler &cgh) {
  8341. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  8342. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8343. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  8344. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  8345. cgh);
  8346. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8347. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8348. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8349. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8350. cgh.parallel_for(
  8351. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8352. [=](sycl::nd_item<3> item_ct1) {
  8353. mul_mat_q5_0<need_check>(
  8354. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8355. nrows_dst, item_ct1,
  8356. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  8357. tile_x_d_q5_0_acc_ct1.get_pointer(),
  8358. tile_y_qs_acc_ct1.get_pointer(),
  8359. tile_y_ds_acc_ct1.get_pointer());
  8360. });
  8361. });
  8362. }
  8363. } else {
  8364. const bool need_check = true;
  8365. /*
  8366. DPCT1049:25: The work-group size passed to the SYCL kernel may exceed
  8367. the limit. To get the device limit, query
  8368. info::device::max_work_group_size. Adjust the work-group size if needed.
  8369. */
  8370. {
  8371. dpct::has_capability_or_fail(stream->get_device(),
  8372. {sycl::aspect::fp16});
  8373. stream->submit([&](sycl::handler &cgh) {
  8374. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  8375. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8376. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  8377. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  8378. cgh);
  8379. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8380. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8381. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8382. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8383. cgh.parallel_for(
  8384. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8385. [=](sycl::nd_item<3> item_ct1) {
  8386. mul_mat_q5_0<need_check>(
  8387. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8388. nrows_dst, item_ct1,
  8389. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  8390. tile_x_d_q5_0_acc_ct1.get_pointer(),
  8391. tile_y_qs_acc_ct1.get_pointer(),
  8392. tile_y_ds_acc_ct1.get_pointer());
  8393. });
  8394. });
  8395. }
  8396. }
  8397. }
  8398. catch (sycl::exception const &exc) {
  8399. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8400. << ", line:" << __LINE__ << std::endl;
  8401. std::exit(1);
  8402. }
  8403. static void ggml_mul_mat_q5_1_q8_1_sycl(const void *vx, const void *vy,
  8404. float *dst, const int ncols_x,
  8405. const int nrows_x, const int ncols_y,
  8406. const int nrows_y, const int nrows_dst,
  8407. dpct::queue_ptr stream) try {
  8408. int id;
  8409. SYCL_CHECK(
  8410. CHECK_TRY_ERROR(id = get_current_device_id()));
  8411. const int compute_capability = g_device_caps[id].cc;
  8412. int mmq_x, mmq_y, nwarps;
  8413. if (compute_capability >= VER_GEN13) {
  8414. mmq_x = MMQ_X_Q5_1_RDNA2;
  8415. mmq_y = MMQ_Y_Q5_1_RDNA2;
  8416. nwarps = NWARPS_Q5_1_RDNA2;
  8417. } else if (compute_capability >= VER_GEN12) {
  8418. mmq_x = MMQ_X_Q5_1_RDNA1;
  8419. mmq_y = MMQ_Y_Q5_1_RDNA1;
  8420. nwarps = NWARPS_Q5_1_RDNA1;
  8421. } else if (compute_capability >= VER_GEN9) {
  8422. mmq_x = MMQ_X_Q5_1_AMPERE;
  8423. mmq_y = MMQ_Y_Q5_1_AMPERE;
  8424. nwarps = NWARPS_Q5_1_AMPERE;
  8425. } else if (compute_capability >= VER_4VEC) {
  8426. mmq_x = MMQ_X_Q5_1_PASCAL;
  8427. mmq_y = MMQ_Y_Q5_1_PASCAL;
  8428. nwarps = NWARPS_Q5_1_PASCAL;
  8429. } else {
  8430. GGML_ASSERT(false);
  8431. }
  8432. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8433. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8434. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8435. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8436. if (nrows_x % mmq_y == 0) {
  8437. const bool need_check = false;
  8438. /*
  8439. DPCT1049:26: The work-group size passed to the SYCL kernel may exceed
  8440. the limit. To get the device limit, query
  8441. info::device::max_work_group_size. Adjust the work-group size if needed.
  8442. */
  8443. {
  8444. dpct::has_capability_or_fail(stream->get_device(),
  8445. {sycl::aspect::fp16});
  8446. stream->submit([&](sycl::handler &cgh) {
  8447. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  8448. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8449. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  8450. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  8451. cgh);
  8452. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8453. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8454. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8455. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8456. cgh.parallel_for(
  8457. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8458. [=](sycl::nd_item<3> item_ct1) {
  8459. mul_mat_q5_1<need_check>(
  8460. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8461. nrows_dst, item_ct1,
  8462. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  8463. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  8464. tile_y_qs_acc_ct1.get_pointer(),
  8465. tile_y_ds_acc_ct1.get_pointer());
  8466. });
  8467. });
  8468. }
  8469. } else {
  8470. const bool need_check = true;
  8471. /*
  8472. DPCT1049:27: The work-group size passed to the SYCL kernel may exceed
  8473. the limit. To get the device limit, query
  8474. info::device::max_work_group_size. Adjust the work-group size if needed.
  8475. */
  8476. {
  8477. dpct::has_capability_or_fail(stream->get_device(),
  8478. {sycl::aspect::fp16});
  8479. stream->submit([&](sycl::handler &cgh) {
  8480. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  8481. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  8482. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  8483. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  8484. cgh);
  8485. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8486. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8487. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8488. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8489. cgh.parallel_for(
  8490. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8491. [=](sycl::nd_item<3> item_ct1) {
  8492. mul_mat_q5_1<need_check>(
  8493. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8494. nrows_dst, item_ct1,
  8495. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  8496. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  8497. tile_y_qs_acc_ct1.get_pointer(),
  8498. tile_y_ds_acc_ct1.get_pointer());
  8499. });
  8500. });
  8501. }
  8502. }
  8503. }
  8504. catch (sycl::exception const &exc) {
  8505. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8506. << ", line:" << __LINE__ << std::endl;
  8507. std::exit(1);
  8508. }
  8509. static void ggml_mul_mat_q8_0_q8_1_sycl(const void *vx, const void *vy,
  8510. float *dst, const int ncols_x,
  8511. const int nrows_x, const int ncols_y,
  8512. const int nrows_y, const int nrows_dst,
  8513. dpct::queue_ptr stream) try {
  8514. int id;
  8515. SYCL_CHECK(
  8516. CHECK_TRY_ERROR(id = get_current_device_id()));
  8517. const int compute_capability = g_device_caps[id].cc;
  8518. int mmq_x, mmq_y, nwarps;
  8519. if (compute_capability >= VER_GEN13) {
  8520. mmq_x = MMQ_X_Q8_0_RDNA2;
  8521. mmq_y = MMQ_Y_Q8_0_RDNA2;
  8522. nwarps = NWARPS_Q8_0_RDNA2;
  8523. } else if (compute_capability >= VER_GEN12) {
  8524. mmq_x = MMQ_X_Q8_0_RDNA1;
  8525. mmq_y = MMQ_Y_Q8_0_RDNA1;
  8526. nwarps = NWARPS_Q8_0_RDNA1;
  8527. } else if (compute_capability >= VER_GEN9) {
  8528. mmq_x = MMQ_X_Q8_0_AMPERE;
  8529. mmq_y = MMQ_Y_Q8_0_AMPERE;
  8530. nwarps = NWARPS_Q8_0_AMPERE;
  8531. } else if (compute_capability >= VER_4VEC) {
  8532. mmq_x = MMQ_X_Q8_0_PASCAL;
  8533. mmq_y = MMQ_Y_Q8_0_PASCAL;
  8534. nwarps = NWARPS_Q8_0_PASCAL;
  8535. } else {
  8536. GGML_ASSERT(false);
  8537. }
  8538. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8539. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8540. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8541. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8542. if (nrows_x % mmq_y == 0) {
  8543. const bool need_check = false;
  8544. /*
  8545. DPCT1049:28: The work-group size passed to the SYCL kernel may exceed
  8546. the limit. To get the device limit, query
  8547. info::device::max_work_group_size. Adjust the work-group size if needed.
  8548. */
  8549. {
  8550. dpct::has_capability_or_fail(stream->get_device(),
  8551. {sycl::aspect::fp16});
  8552. stream->submit([&](sycl::handler &cgh) {
  8553. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  8554. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8555. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  8556. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  8557. cgh);
  8558. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8559. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8560. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8561. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8562. cgh.parallel_for(
  8563. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8564. [=](sycl::nd_item<3> item_ct1) {
  8565. mul_mat_q8_0<need_check>(
  8566. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8567. nrows_dst, item_ct1,
  8568. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  8569. tile_x_d_q8_0_acc_ct1.get_pointer(),
  8570. tile_y_qs_acc_ct1.get_pointer(),
  8571. tile_y_ds_acc_ct1.get_pointer());
  8572. });
  8573. });
  8574. }
  8575. } else {
  8576. const bool need_check = true;
  8577. /*
  8578. DPCT1049:29: The work-group size passed to the SYCL kernel may exceed
  8579. the limit. To get the device limit, query
  8580. info::device::max_work_group_size. Adjust the work-group size if needed.
  8581. */
  8582. {
  8583. dpct::has_capability_or_fail(stream->get_device(),
  8584. {sycl::aspect::fp16});
  8585. stream->submit([&](sycl::handler &cgh) {
  8586. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  8587. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8588. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  8589. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  8590. cgh);
  8591. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8592. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8593. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8594. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8595. cgh.parallel_for(
  8596. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8597. [=](sycl::nd_item<3> item_ct1) {
  8598. mul_mat_q8_0<need_check>(
  8599. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8600. nrows_dst, item_ct1,
  8601. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  8602. tile_x_d_q8_0_acc_ct1.get_pointer(),
  8603. tile_y_qs_acc_ct1.get_pointer(),
  8604. tile_y_ds_acc_ct1.get_pointer());
  8605. });
  8606. });
  8607. }
  8608. }
  8609. }
  8610. catch (sycl::exception const &exc) {
  8611. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8612. << ", line:" << __LINE__ << std::endl;
  8613. std::exit(1);
  8614. }
  8615. static void ggml_mul_mat_q2_K_q8_1_sycl(const void *vx, const void *vy,
  8616. float *dst, const int ncols_x,
  8617. const int nrows_x, const int ncols_y,
  8618. const int nrows_y, const int nrows_dst,
  8619. dpct::queue_ptr stream) try {
  8620. int id;
  8621. SYCL_CHECK(
  8622. CHECK_TRY_ERROR(id = get_current_device_id()));
  8623. const int compute_capability = g_device_caps[id].cc;
  8624. int mmq_x, mmq_y, nwarps;
  8625. if (compute_capability >= VER_GEN13) {
  8626. mmq_x = MMQ_X_Q2_K_RDNA2;
  8627. mmq_y = MMQ_Y_Q2_K_RDNA2;
  8628. nwarps = NWARPS_Q2_K_RDNA2;
  8629. } else if (compute_capability >= VER_GEN12) {
  8630. mmq_x = MMQ_X_Q2_K_RDNA1;
  8631. mmq_y = MMQ_Y_Q2_K_RDNA1;
  8632. nwarps = NWARPS_Q2_K_RDNA1;
  8633. } else if (compute_capability >= VER_GEN9) {
  8634. mmq_x = MMQ_X_Q2_K_AMPERE;
  8635. mmq_y = MMQ_Y_Q2_K_AMPERE;
  8636. nwarps = NWARPS_Q2_K_AMPERE;
  8637. } else if (compute_capability >= VER_4VEC) {
  8638. mmq_x = MMQ_X_Q2_K_PASCAL;
  8639. mmq_y = MMQ_Y_Q2_K_PASCAL;
  8640. nwarps = NWARPS_Q2_K_PASCAL;
  8641. } else {
  8642. GGML_ASSERT(false);
  8643. }
  8644. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8645. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8646. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8647. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8648. if (nrows_x % mmq_y == 0) {
  8649. const bool need_check = false;
  8650. /*
  8651. DPCT1049:30: The work-group size passed to the SYCL kernel may exceed
  8652. the limit. To get the device limit, query
  8653. info::device::max_work_group_size. Adjust the work-group size if needed.
  8654. */
  8655. {
  8656. dpct::has_capability_or_fail(stream->get_device(),
  8657. {sycl::aspect::fp16});
  8658. stream->submit([&](sycl::handler &cgh) {
  8659. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  8660. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8661. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  8662. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  8663. cgh);
  8664. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  8665. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  8666. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8667. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8668. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8669. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8670. cgh.parallel_for(
  8671. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8672. [=](sycl::nd_item<3> item_ct1) {
  8673. mul_mat_q2_K<need_check>(
  8674. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8675. nrows_dst, item_ct1,
  8676. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  8677. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  8678. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  8679. tile_y_qs_acc_ct1.get_pointer(),
  8680. tile_y_ds_acc_ct1.get_pointer());
  8681. });
  8682. });
  8683. }
  8684. } else {
  8685. const bool need_check = true;
  8686. /*
  8687. DPCT1049:31: The work-group size passed to the SYCL kernel may exceed
  8688. the limit. To get the device limit, query
  8689. info::device::max_work_group_size. Adjust the work-group size if needed.
  8690. */
  8691. {
  8692. dpct::has_capability_or_fail(stream->get_device(),
  8693. {sycl::aspect::fp16});
  8694. stream->submit([&](sycl::handler &cgh) {
  8695. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  8696. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8697. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  8698. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  8699. cgh);
  8700. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  8701. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  8702. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8703. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8704. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8705. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8706. cgh.parallel_for(
  8707. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8708. [=](sycl::nd_item<3> item_ct1) {
  8709. mul_mat_q2_K<need_check>(
  8710. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8711. nrows_dst, item_ct1,
  8712. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  8713. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  8714. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  8715. tile_y_qs_acc_ct1.get_pointer(),
  8716. tile_y_ds_acc_ct1.get_pointer());
  8717. });
  8718. });
  8719. }
  8720. }
  8721. }
  8722. catch (sycl::exception const &exc) {
  8723. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8724. << ", line:" << __LINE__ << std::endl;
  8725. std::exit(1);
  8726. }
  8727. static void ggml_mul_mat_q3_K_q8_1_sycl(const void *vx, const void *vy,
  8728. float *dst, const int ncols_x,
  8729. const int nrows_x, const int ncols_y,
  8730. const int nrows_y, const int nrows_dst,
  8731. dpct::queue_ptr stream) try {
  8732. #if QK_K == 256
  8733. int id;
  8734. SYCL_CHECK(
  8735. CHECK_TRY_ERROR(id = get_current_device_id()));
  8736. const int compute_capability = g_device_caps[id].cc;
  8737. int mmq_x, mmq_y, nwarps;
  8738. if (compute_capability >= VER_GEN13) {
  8739. mmq_x = MMQ_X_Q3_K_RDNA2;
  8740. mmq_y = MMQ_Y_Q3_K_RDNA2;
  8741. nwarps = NWARPS_Q3_K_RDNA2;
  8742. } else if (compute_capability >= VER_GEN12) {
  8743. mmq_x = MMQ_X_Q3_K_RDNA1;
  8744. mmq_y = MMQ_Y_Q3_K_RDNA1;
  8745. nwarps = NWARPS_Q3_K_RDNA1;
  8746. } else if (compute_capability >= VER_GEN9) {
  8747. mmq_x = MMQ_X_Q3_K_AMPERE;
  8748. mmq_y = MMQ_Y_Q3_K_AMPERE;
  8749. nwarps = NWARPS_Q3_K_AMPERE;
  8750. } else if (compute_capability >= VER_4VEC) {
  8751. mmq_x = MMQ_X_Q3_K_PASCAL;
  8752. mmq_y = MMQ_Y_Q3_K_PASCAL;
  8753. nwarps = NWARPS_Q3_K_PASCAL;
  8754. } else {
  8755. GGML_ASSERT(false);
  8756. }
  8757. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8758. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8759. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8760. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8761. if (nrows_x % mmq_y == 0) {
  8762. const bool need_check = false;
  8763. /*
  8764. DPCT1049:32: The work-group size passed to the SYCL kernel may exceed
  8765. the limit. To get the device limit, query
  8766. info::device::max_work_group_size. Adjust the work-group size if needed.
  8767. */
  8768. {
  8769. dpct::has_capability_or_fail(stream->get_device(),
  8770. {sycl::aspect::fp16});
  8771. stream->submit([&](sycl::handler &cgh) {
  8772. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  8773. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8774. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  8775. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  8776. cgh);
  8777. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  8778. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  8779. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  8780. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  8781. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8782. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8783. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8784. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8785. cgh.parallel_for(
  8786. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8787. [=](sycl::nd_item<3> item_ct1) {
  8788. mul_mat_q3_K<need_check>(
  8789. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8790. nrows_dst, item_ct1,
  8791. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  8792. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  8793. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  8794. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  8795. tile_y_qs_acc_ct1.get_pointer(),
  8796. tile_y_ds_acc_ct1.get_pointer());
  8797. });
  8798. });
  8799. }
  8800. } else {
  8801. const bool need_check = true;
  8802. /*
  8803. DPCT1049:33: The work-group size passed to the SYCL kernel may exceed
  8804. the limit. To get the device limit, query
  8805. info::device::max_work_group_size. Adjust the work-group size if needed.
  8806. */
  8807. {
  8808. dpct::has_capability_or_fail(stream->get_device(),
  8809. {sycl::aspect::fp16});
  8810. stream->submit([&](sycl::handler &cgh) {
  8811. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  8812. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8813. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  8814. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  8815. cgh);
  8816. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  8817. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  8818. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  8819. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  8820. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8821. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8822. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8823. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8824. cgh.parallel_for(
  8825. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8826. [=](sycl::nd_item<3> item_ct1) {
  8827. mul_mat_q3_K<need_check>(
  8828. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8829. nrows_dst, item_ct1,
  8830. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  8831. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  8832. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  8833. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  8834. tile_y_qs_acc_ct1.get_pointer(),
  8835. tile_y_ds_acc_ct1.get_pointer());
  8836. });
  8837. });
  8838. }
  8839. }
  8840. #endif
  8841. }
  8842. catch (sycl::exception const &exc) {
  8843. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8844. << ", line:" << __LINE__ << std::endl;
  8845. std::exit(1);
  8846. }
  8847. static void ggml_mul_mat_q4_K_q8_1_sycl(const void *vx, const void *vy,
  8848. float *dst, const int ncols_x,
  8849. const int nrows_x, const int ncols_y,
  8850. const int nrows_y, const int nrows_dst,
  8851. dpct::queue_ptr stream) try {
  8852. int id;
  8853. SYCL_CHECK(
  8854. CHECK_TRY_ERROR(id = get_current_device_id()));
  8855. const int compute_capability = g_device_caps[id].cc;
  8856. int mmq_x, mmq_y, nwarps;
  8857. if (compute_capability >= VER_GEN13) {
  8858. mmq_x = MMQ_X_Q4_K_RDNA2;
  8859. mmq_y = MMQ_Y_Q4_K_RDNA2;
  8860. nwarps = NWARPS_Q4_K_RDNA2;
  8861. } else if (compute_capability >= VER_GEN12) {
  8862. mmq_x = MMQ_X_Q4_K_RDNA1;
  8863. mmq_y = MMQ_Y_Q4_K_RDNA1;
  8864. nwarps = NWARPS_Q4_K_RDNA1;
  8865. } else if (compute_capability >= VER_GEN9) {
  8866. mmq_x = MMQ_X_Q4_K_AMPERE;
  8867. mmq_y = MMQ_Y_Q4_K_AMPERE;
  8868. nwarps = NWARPS_Q4_K_AMPERE;
  8869. } else if (compute_capability >= VER_4VEC) {
  8870. mmq_x = MMQ_X_Q4_K_PASCAL;
  8871. mmq_y = MMQ_Y_Q4_K_PASCAL;
  8872. nwarps = NWARPS_Q4_K_PASCAL;
  8873. } else {
  8874. GGML_ASSERT(false);
  8875. }
  8876. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8877. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8878. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8879. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8880. if (nrows_x % mmq_y == 0) {
  8881. const bool need_check = false;
  8882. /*
  8883. DPCT1049:34: The work-group size passed to the SYCL kernel may exceed
  8884. the limit. To get the device limit, query
  8885. info::device::max_work_group_size. Adjust the work-group size if needed.
  8886. */
  8887. {
  8888. dpct::has_capability_or_fail(stream->get_device(),
  8889. {sycl::aspect::fp16});
  8890. stream->submit([&](sycl::handler &cgh) {
  8891. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  8892. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8893. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  8894. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  8895. cgh);
  8896. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  8897. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  8898. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8899. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8900. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8901. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8902. cgh.parallel_for(
  8903. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8904. [=](sycl::nd_item<3> item_ct1) {
  8905. mul_mat_q4_K<need_check>(
  8906. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8907. nrows_dst, item_ct1,
  8908. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  8909. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  8910. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  8911. tile_y_qs_acc_ct1.get_pointer(),
  8912. tile_y_ds_acc_ct1.get_pointer());
  8913. });
  8914. });
  8915. }
  8916. } else {
  8917. const bool need_check = true;
  8918. /*
  8919. DPCT1049:35: The work-group size passed to the SYCL kernel may exceed
  8920. the limit. To get the device limit, query
  8921. info::device::max_work_group_size. Adjust the work-group size if needed.
  8922. */
  8923. {
  8924. dpct::has_capability_or_fail(stream->get_device(),
  8925. {sycl::aspect::fp16});
  8926. stream->submit([&](sycl::handler &cgh) {
  8927. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  8928. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  8929. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  8930. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  8931. cgh);
  8932. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  8933. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  8934. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  8935. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  8936. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  8937. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  8938. cgh.parallel_for(
  8939. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8940. [=](sycl::nd_item<3> item_ct1) {
  8941. mul_mat_q4_K<need_check>(
  8942. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  8943. nrows_dst, item_ct1,
  8944. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  8945. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  8946. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  8947. tile_y_qs_acc_ct1.get_pointer(),
  8948. tile_y_ds_acc_ct1.get_pointer());
  8949. });
  8950. });
  8951. }
  8952. }
  8953. }
  8954. catch (sycl::exception const &exc) {
  8955. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8956. << ", line:" << __LINE__ << std::endl;
  8957. std::exit(1);
  8958. }
  8959. static void ggml_mul_mat_q5_K_q8_1_sycl(const void *vx, const void *vy,
  8960. float *dst, const int ncols_x,
  8961. const int nrows_x, const int ncols_y,
  8962. const int nrows_y, const int nrows_dst,
  8963. dpct::queue_ptr stream) try {
  8964. int id;
  8965. SYCL_CHECK(
  8966. CHECK_TRY_ERROR(id = get_current_device_id()));
  8967. const int compute_capability = g_device_caps[id].cc;
  8968. int mmq_x, mmq_y, nwarps;
  8969. if (compute_capability >= VER_GEN13) {
  8970. mmq_x = MMQ_X_Q5_K_RDNA2;
  8971. mmq_y = MMQ_Y_Q5_K_RDNA2;
  8972. nwarps = NWARPS_Q5_K_RDNA2;
  8973. } else if (compute_capability >= VER_GEN12) {
  8974. mmq_x = MMQ_X_Q5_K_RDNA1;
  8975. mmq_y = MMQ_Y_Q5_K_RDNA1;
  8976. nwarps = NWARPS_Q5_K_RDNA1;
  8977. } else if (compute_capability >= VER_GEN9) {
  8978. mmq_x = MMQ_X_Q5_K_AMPERE;
  8979. mmq_y = MMQ_Y_Q5_K_AMPERE;
  8980. nwarps = NWARPS_Q5_K_AMPERE;
  8981. } else if (compute_capability >= VER_4VEC) {
  8982. mmq_x = MMQ_X_Q5_K_PASCAL;
  8983. mmq_y = MMQ_Y_Q5_K_PASCAL;
  8984. nwarps = NWARPS_Q5_K_PASCAL;
  8985. } else {
  8986. GGML_ASSERT(false);
  8987. }
  8988. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  8989. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  8990. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  8991. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  8992. if (nrows_x % mmq_y == 0) {
  8993. const bool need_check = false;
  8994. /*
  8995. DPCT1049:36: The work-group size passed to the SYCL kernel may exceed
  8996. the limit. To get the device limit, query
  8997. info::device::max_work_group_size. Adjust the work-group size if needed.
  8998. */
  8999. {
  9000. dpct::has_capability_or_fail(stream->get_device(),
  9001. {sycl::aspect::fp16});
  9002. stream->submit([&](sycl::handler &cgh) {
  9003. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  9004. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9005. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  9006. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  9007. cgh);
  9008. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  9009. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  9010. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9011. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9012. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9013. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9014. cgh.parallel_for(
  9015. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9016. [=](sycl::nd_item<3> item_ct1) {
  9017. mul_mat_q5_K<need_check>(
  9018. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9019. nrows_dst, item_ct1,
  9020. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  9021. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  9022. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  9023. tile_y_qs_acc_ct1.get_pointer(),
  9024. tile_y_ds_acc_ct1.get_pointer());
  9025. });
  9026. });
  9027. }
  9028. } else {
  9029. const bool need_check = true;
  9030. /*
  9031. DPCT1049:37: The work-group size passed to the SYCL kernel may exceed
  9032. the limit. To get the device limit, query
  9033. info::device::max_work_group_size. Adjust the work-group size if needed.
  9034. */
  9035. {
  9036. dpct::has_capability_or_fail(stream->get_device(),
  9037. {sycl::aspect::fp16});
  9038. stream->submit([&](sycl::handler &cgh) {
  9039. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  9040. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9041. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  9042. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  9043. cgh);
  9044. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  9045. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  9046. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9047. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9048. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9049. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9050. cgh.parallel_for(
  9051. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9052. [=](sycl::nd_item<3> item_ct1) {
  9053. mul_mat_q5_K<need_check>(
  9054. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9055. nrows_dst, item_ct1,
  9056. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  9057. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  9058. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  9059. tile_y_qs_acc_ct1.get_pointer(),
  9060. tile_y_ds_acc_ct1.get_pointer());
  9061. });
  9062. });
  9063. }
  9064. }
  9065. }
  9066. catch (sycl::exception const &exc) {
  9067. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9068. << ", line:" << __LINE__ << std::endl;
  9069. std::exit(1);
  9070. }
  9071. static void ggml_mul_mat_q6_K_q8_1_sycl(const void *vx, const void *vy,
  9072. float *dst, const int ncols_x,
  9073. const int nrows_x, const int ncols_y,
  9074. const int nrows_y, const int nrows_dst,
  9075. dpct::queue_ptr stream) try {
  9076. int id;
  9077. SYCL_CHECK(
  9078. CHECK_TRY_ERROR(id = get_current_device_id()));
  9079. const int compute_capability = g_device_caps[id].cc;
  9080. int mmq_x, mmq_y, nwarps;
  9081. if (compute_capability >= VER_GEN13) {
  9082. mmq_x = MMQ_X_Q6_K_RDNA2;
  9083. mmq_y = MMQ_Y_Q6_K_RDNA2;
  9084. nwarps = NWARPS_Q6_K_RDNA2;
  9085. } else if (compute_capability >= VER_GEN12) {
  9086. mmq_x = MMQ_X_Q6_K_RDNA1;
  9087. mmq_y = MMQ_Y_Q6_K_RDNA1;
  9088. nwarps = NWARPS_Q6_K_RDNA1;
  9089. } else if (compute_capability >= VER_GEN9) {
  9090. mmq_x = MMQ_X_Q6_K_AMPERE;
  9091. mmq_y = MMQ_Y_Q6_K_AMPERE;
  9092. nwarps = NWARPS_Q6_K_AMPERE;
  9093. } else if (compute_capability >= VER_4VEC) {
  9094. mmq_x = MMQ_X_Q6_K_PASCAL;
  9095. mmq_y = MMQ_Y_Q6_K_PASCAL;
  9096. nwarps = NWARPS_Q6_K_PASCAL;
  9097. } else {
  9098. GGML_ASSERT(false);
  9099. }
  9100. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9101. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9102. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9103. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9104. if (nrows_x % mmq_y == 0) {
  9105. const bool need_check = false;
  9106. /*
  9107. DPCT1049:38: The work-group size passed to the SYCL kernel may exceed
  9108. the limit. To get the device limit, query
  9109. info::device::max_work_group_size. Adjust the work-group size if needed.
  9110. */
  9111. {
  9112. dpct::has_capability_or_fail(stream->get_device(),
  9113. {sycl::aspect::fp16});
  9114. stream->submit([&](sycl::handler &cgh) {
  9115. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  9116. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9117. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  9118. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  9119. cgh);
  9120. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  9121. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  9122. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9123. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9124. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9125. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9126. cgh.parallel_for(
  9127. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9128. [=](sycl::nd_item<3> item_ct1) {
  9129. mul_mat_q6_K<need_check>(
  9130. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9131. nrows_dst, item_ct1,
  9132. tile_x_ql_acc_ct1.get_pointer(),
  9133. tile_x_dm_acc_ct1.get_pointer(),
  9134. tile_x_sc_acc_ct1.get_pointer(),
  9135. tile_y_qs_acc_ct1.get_pointer(),
  9136. tile_y_ds_acc_ct1.get_pointer());
  9137. });
  9138. });
  9139. }
  9140. } else {
  9141. const bool need_check = true;
  9142. /*
  9143. DPCT1049:39: The work-group size passed to the SYCL kernel may exceed
  9144. the limit. To get the device limit, query
  9145. info::device::max_work_group_size. Adjust the work-group size if needed.
  9146. */
  9147. {
  9148. dpct::has_capability_or_fail(stream->get_device(),
  9149. {sycl::aspect::fp16});
  9150. stream->submit([&](sycl::handler &cgh) {
  9151. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  9152. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9153. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  9154. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  9155. cgh);
  9156. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  9157. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  9158. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9159. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9160. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9161. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9162. cgh.parallel_for(
  9163. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9164. [=](sycl::nd_item<3> item_ct1) {
  9165. mul_mat_q6_K<need_check>(
  9166. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9167. nrows_dst, item_ct1,
  9168. tile_x_ql_acc_ct1.get_pointer(),
  9169. tile_x_dm_acc_ct1.get_pointer(),
  9170. tile_x_sc_acc_ct1.get_pointer(),
  9171. tile_y_qs_acc_ct1.get_pointer(),
  9172. tile_y_ds_acc_ct1.get_pointer());
  9173. });
  9174. });
  9175. }
  9176. }
  9177. }
  9178. catch (sycl::exception const &exc) {
  9179. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9180. << ", line:" << __LINE__ << std::endl;
  9181. std::exit(1);
  9182. }
  9183. static void ggml_mul_mat_p021_f16_f32_sycl(const void *vx, const float *y,
  9184. float *dst, const int ncols_x,
  9185. const int nrows_x,
  9186. const int nchannels_x,
  9187. const int nchannels_y,
  9188. dpct::queue_ptr stream) {
  9189. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  9190. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  9191. {
  9192. dpct::has_capability_or_fail(stream->get_device(),
  9193. {sycl::aspect::fp16});
  9194. stream->parallel_for(
  9195. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9196. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9197. mul_mat_p021_f16_f32(vx, y, dst, ncols_x, nrows_x, nchannels_x,
  9198. nchannels_y, item_ct1);
  9199. });
  9200. }
  9201. }
  9202. static void ggml_mul_mat_vec_nc_f16_f32_sycl(
  9203. const void *vx, const float *y, float *dst, const int ncols_x,
  9204. const int nrows_x, const int row_stride_x, const int nchannels_x,
  9205. const int nchannels_y, const int channel_stride_x, dpct::queue_ptr stream) {
  9206. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  9207. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  9208. {
  9209. dpct::has_capability_or_fail(stream->get_device(),
  9210. {sycl::aspect::fp16});
  9211. stream->parallel_for(
  9212. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9213. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9214. mul_mat_vec_nc_f16_f32(vx, y, dst, ncols_x, nrows_x,
  9215. row_stride_x, channel_stride_x,
  9216. nchannels_y / nchannels_x, item_ct1);
  9217. });
  9218. }
  9219. }
  9220. static void
  9221. ggml_cpy_f16_f32_sycl(const char *cx, char *cdst, const int ne, const int ne00,
  9222. const int ne01, const int ne02, const int nb00,
  9223. const int nb01, const int nb02, const int nb03,
  9224. const int ne10, const int ne11, const int ne12,
  9225. const int nb10, const int nb11, const int nb12,
  9226. const int nb13, dpct::queue_ptr stream) {
  9227. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9228. {
  9229. dpct::has_capability_or_fail(stream->get_device(),
  9230. {sycl::aspect::fp16});
  9231. stream->parallel_for(
  9232. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9233. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9234. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9235. [=](sycl::nd_item<3> item_ct1) {
  9236. cpy_f32_f16<cpy_1_f16_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00,
  9237. nb01, nb02, nb03, ne10, ne11, ne12,
  9238. nb10, nb11, nb12, nb13, item_ct1);
  9239. });
  9240. }
  9241. }
  9242. static void ggml_cpy_f32_f32_sycl(const char *cx, char *cdst, const int ne,
  9243. const int ne00, const int ne01,
  9244. const int ne02, const int nb00,
  9245. const int nb01, const int nb02,
  9246. const int nb03, const int ne10,
  9247. const int ne11, const int ne12,
  9248. const int nb10, const int nb11,
  9249. const int nb12, const int nb13,
  9250. dpct::queue_ptr stream) {
  9251. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9252. {
  9253. dpct::has_capability_or_fail(stream->get_device(),
  9254. {sycl::aspect::fp16});
  9255. stream->parallel_for(
  9256. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9257. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9258. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9259. [=](sycl::nd_item<3> item_ct1) {
  9260. cpy_f32_f16<cpy_1_f32_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  9261. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  9262. item_ct1);
  9263. });
  9264. }
  9265. }
  9266. static void ggml_cpy_f32_f16_sycl(const char *cx, char *cdst, const int ne,
  9267. const int ne00, const int ne01,
  9268. const int ne02, const int nb00,
  9269. const int nb01, const int nb02,
  9270. const int nb03, const int ne10,
  9271. const int ne11, const int ne12,
  9272. const int nb10, const int nb11,
  9273. const int nb12, const int nb13,
  9274. dpct::queue_ptr stream) {
  9275. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9276. {
  9277. dpct::has_capability_or_fail(stream->get_device(),
  9278. {sycl::aspect::fp16});
  9279. stream->parallel_for(
  9280. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9281. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9282. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9283. [=](sycl::nd_item<3> item_ct1) {
  9284. cpy_f32_f16<cpy_1_f32_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  9285. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  9286. item_ct1);
  9287. });
  9288. }
  9289. }
  9290. static void ggml_cpy_f32_q8_0_sycl(const char *cx, char *cdst, const int ne,
  9291. const int ne00, const int ne01,
  9292. const int ne02, const int nb00,
  9293. const int nb01, const int nb02,
  9294. const int nb03, const int ne10,
  9295. const int ne11, const int ne12,
  9296. const int nb10, const int nb11,
  9297. const int nb12, const int nb13,
  9298. dpct::queue_ptr stream) {
  9299. GGML_ASSERT(ne % QK8_0 == 0);
  9300. const int num_blocks = ne / QK8_0;
  9301. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  9302. sycl::range<3>(1, 1, 1)),
  9303. [=](sycl::nd_item<3> item_ct1) {
  9304. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0>(
  9305. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  9306. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  9307. item_ct1);
  9308. });
  9309. }
  9310. static void ggml_cpy_f32_q4_0_sycl(const char *cx, char *cdst, const int ne,
  9311. const int ne00, const int ne01,
  9312. const int ne02, const int nb00,
  9313. const int nb01, const int nb02,
  9314. const int nb03, const int ne10,
  9315. const int ne11, const int ne12,
  9316. const int nb10, const int nb11,
  9317. const int nb12, const int nb13,
  9318. dpct::queue_ptr stream) {
  9319. GGML_ASSERT(ne % QK4_0 == 0);
  9320. const int num_blocks = ne / QK4_0;
  9321. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  9322. sycl::range<3>(1, 1, 1)),
  9323. [=](sycl::nd_item<3> item_ct1) {
  9324. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0>(
  9325. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  9326. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  9327. item_ct1);
  9328. });
  9329. }
  9330. static void ggml_cpy_f32_q4_1_sycl(const char *cx, char *cdst, const int ne,
  9331. const int ne00, const int ne01,
  9332. const int ne02, const int nb00,
  9333. const int nb01, const int nb02,
  9334. const int nb03, const int ne10,
  9335. const int ne11, const int ne12,
  9336. const int nb10, const int nb11,
  9337. const int nb12, const int nb13,
  9338. dpct::queue_ptr stream) {
  9339. GGML_ASSERT(ne % QK4_1 == 0);
  9340. const int num_blocks = ne / QK4_1;
  9341. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  9342. sycl::range<3>(1, 1, 1)),
  9343. [=](sycl::nd_item<3> item_ct1) {
  9344. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1>(
  9345. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  9346. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  9347. item_ct1);
  9348. });
  9349. }
  9350. static void ggml_cpy_f16_f16_sycl(const char *cx, char *cdst, const int ne,
  9351. const int ne00, const int ne01,
  9352. const int ne02, const int nb00,
  9353. const int nb01, const int nb02,
  9354. const int nb03, const int ne10,
  9355. const int ne11, const int ne12,
  9356. const int nb10, const int nb11,
  9357. const int nb12, const int nb13,
  9358. dpct::queue_ptr stream) {
  9359. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9360. {
  9361. dpct::has_capability_or_fail(stream->get_device(),
  9362. {sycl::aspect::fp16});
  9363. stream->parallel_for(
  9364. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9365. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9366. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9367. [=](sycl::nd_item<3> item_ct1) {
  9368. cpy_f32_f16<cpy_1_f16_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  9369. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  9370. item_ct1);
  9371. });
  9372. }
  9373. }
  9374. static void ggml_cpy_i16_i16_sycl(const char *cx, char *cdst, const int ne,
  9375. const int ne00, const int ne01,
  9376. const int ne02, const int nb00,
  9377. const int nb01, const int nb02,
  9378. const int nb03, const int ne10,
  9379. const int ne11, const int ne12,
  9380. const int nb10, const int nb11,
  9381. const int nb12, const int nb13,
  9382. dpct::queue_ptr stream) {
  9383. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9384. {
  9385. // dpct::has_capability_or_fail(stream->get_device(),
  9386. // {sycl::aspect::fp16});
  9387. stream->parallel_for(
  9388. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9389. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9390. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9391. [=](sycl::nd_item<3> item_ct1) {
  9392. cpy_f32_f16<cpy_1_i16_i16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  9393. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  9394. item_ct1);
  9395. });
  9396. }
  9397. }
  9398. static void ggml_cpy_i32_i32_sycl(const char *cx, char *cdst, const int ne,
  9399. const int ne00, const int ne01,
  9400. const int ne02, const int nb00,
  9401. const int nb01, const int nb02,
  9402. const int nb03, const int ne10,
  9403. const int ne11, const int ne12,
  9404. const int nb10, const int nb11,
  9405. const int nb12, const int nb13,
  9406. dpct::queue_ptr stream) {
  9407. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  9408. {
  9409. // dpct::has_capability_or_fail(stream->get_device(),
  9410. // {sycl::aspect::fp16});
  9411. stream->parallel_for(
  9412. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9413. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  9414. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  9415. [=](sycl::nd_item<3> item_ct1) {
  9416. cpy_f32_f16<cpy_1_i32_i32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  9417. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  9418. item_ct1);
  9419. });
  9420. }
  9421. }
  9422. static void scale_f32_sycl(const float *x, float *dst, const float scale,
  9423. const int k, dpct::queue_ptr stream) {
  9424. const int num_blocks = (k + SYCL_SCALE_BLOCK_SIZE - 1) / SYCL_SCALE_BLOCK_SIZE;
  9425. stream->parallel_for(
  9426. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9427. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE),
  9428. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE)),
  9429. [=](sycl::nd_item<3> item_ct1) {
  9430. scale_f32(x, dst, scale, k, item_ct1);
  9431. });
  9432. }
  9433. static void clamp_f32_sycl(const float *x, float *dst, const float min,
  9434. const float max, const int k,
  9435. dpct::queue_ptr stream) {
  9436. const int num_blocks = (k + SYCL_CLAMP_BLOCK_SIZE - 1) / SYCL_CLAMP_BLOCK_SIZE;
  9437. stream->parallel_for(
  9438. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  9439. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE),
  9440. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE)),
  9441. [=](sycl::nd_item<3> item_ct1) {
  9442. clamp_f32(x, dst, min, max, k, item_ct1);
  9443. });
  9444. }
  9445. template <typename T>
  9446. static void rope_sycl(const T *x, T *dst, int ncols, int nrows,
  9447. const int32_t *pos, float freq_scale, int p_delta_rows,
  9448. float freq_base, float ext_factor, float attn_factor,
  9449. rope_corr_dims corr_dims, dpct::queue_ptr stream) {
  9450. GGML_ASSERT(ncols % 2 == 0);
  9451. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  9452. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  9453. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  9454. if (pos == nullptr) {
  9455. /*
  9456. DPCT1049:40: The work-group size passed to the SYCL kernel may exceed
  9457. the limit. To get the device limit, query
  9458. info::device::max_work_group_size. Adjust the work-group size if needed.
  9459. */
  9460. dpct::has_capability_or_fail(stream->get_device(),
  9461. {sycl::aspect::fp16});
  9462. stream->parallel_for(
  9463. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9464. [=](sycl::nd_item<3> item_ct1) {
  9465. rope<T, false>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  9466. freq_base, ext_factor, attn_factor, corr_dims,
  9467. item_ct1);
  9468. });
  9469. } else {
  9470. /*
  9471. DPCT1049:41: The work-group size passed to the SYCL kernel may exceed
  9472. the limit. To get the device limit, query
  9473. info::device::max_work_group_size. Adjust the work-group size if needed.
  9474. */
  9475. dpct::has_capability_or_fail(stream->get_device(),
  9476. {sycl::aspect::fp16});
  9477. stream->parallel_for(
  9478. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9479. [=](sycl::nd_item<3> item_ct1) {
  9480. rope<T, true>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  9481. freq_base, ext_factor, attn_factor, corr_dims,
  9482. item_ct1);
  9483. });
  9484. }
  9485. }
  9486. template <typename T>
  9487. static void rope_neox_sycl(const T *x, T *dst, int ncols, int n_dims, int nrows,
  9488. const int32_t *pos, float freq_scale,
  9489. int p_delta_rows, float freq_base, float ext_factor,
  9490. float attn_factor, rope_corr_dims corr_dims,
  9491. dpct::queue_ptr stream) {
  9492. GGML_ASSERT(ncols % 2 == 0);
  9493. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  9494. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  9495. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  9496. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  9497. const float inv_ndims = -1.0f / n_dims;
  9498. if (pos == nullptr) {
  9499. /*
  9500. DPCT1049:42: The work-group size passed to the SYCL kernel may exceed
  9501. the limit. To get the device limit, query
  9502. info::device::max_work_group_size. Adjust the work-group size if needed.
  9503. */
  9504. dpct::has_capability_or_fail(stream->get_device(),
  9505. {sycl::aspect::fp16});
  9506. stream->parallel_for(
  9507. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9508. [=](sycl::nd_item<3> item_ct1) {
  9509. rope_neox<T, false>(x, dst, ncols, n_dims, pos, freq_scale,
  9510. p_delta_rows, ext_factor, attn_factor,
  9511. corr_dims, theta_scale, inv_ndims,
  9512. item_ct1);
  9513. });
  9514. } else {
  9515. /*
  9516. DPCT1049:43: The work-group size passed to the SYCL kernel may exceed
  9517. the limit. To get the device limit, query
  9518. info::device::max_work_group_size. Adjust the work-group size if needed.
  9519. */
  9520. dpct::has_capability_or_fail(stream->get_device(),
  9521. {sycl::aspect::fp16});
  9522. stream->parallel_for(
  9523. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9524. [=](sycl::nd_item<3> item_ct1) {
  9525. rope_neox<T, true>(x, dst, ncols, n_dims, pos, freq_scale,
  9526. p_delta_rows, ext_factor, attn_factor,
  9527. corr_dims, theta_scale, inv_ndims, item_ct1);
  9528. });
  9529. }
  9530. }
  9531. static void rope_glm_f32_sycl(const float *x, float *dst, int ncols, int nrows,
  9532. const int32_t *pos, float freq_scale,
  9533. int p_delta_rows, float freq_base, int n_ctx,
  9534. dpct::queue_ptr stream) {
  9535. GGML_ASSERT(ncols % 4 == 0);
  9536. const sycl::range<3> block_dims(1, 1, SYCL_ROPE_BLOCK_SIZE / 4);
  9537. const int num_blocks_x = (ncols + SYCL_ROPE_BLOCK_SIZE - 1) / SYCL_ROPE_BLOCK_SIZE;
  9538. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  9539. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9540. [=](sycl::nd_item<3> item_ct1) {
  9541. rope_glm_f32(x, dst, ncols, pos, freq_scale,
  9542. p_delta_rows, freq_base, n_ctx,
  9543. item_ct1);
  9544. });
  9545. }
  9546. static void alibi_f32_sycl(const float *x, float *dst, const int ncols,
  9547. const int nrows, const int k_rows,
  9548. const int n_heads_log2_floor, const float m0,
  9549. const float m1, dpct::queue_ptr stream) {
  9550. const sycl::range<3> block_dims(1, 1, SYCL_ALIBI_BLOCK_SIZE);
  9551. const int num_blocks_x = (ncols + SYCL_ALIBI_BLOCK_SIZE - 1) / (SYCL_ALIBI_BLOCK_SIZE);
  9552. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  9553. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9554. [=](sycl::nd_item<3> item_ct1) {
  9555. alibi_f32(x, dst, ncols, k_rows,
  9556. n_heads_log2_floor, m0, m1, item_ct1);
  9557. });
  9558. }
  9559. static void sum_rows_f32_sycl(const float *x, float *dst, const int ncols,
  9560. const int nrows, dpct::queue_ptr stream) {
  9561. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  9562. const sycl::range<3> block_nums(1, nrows, 1);
  9563. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9564. [=](sycl::nd_item<3> item_ct1)
  9565. [[intel::reqd_sub_group_size(32)]] {
  9566. k_sum_rows_f32(x, dst, ncols, item_ct1);
  9567. });
  9568. }
  9569. static void argsort_f32_i32_sycl(const float *x, int *dst, const int ncols,
  9570. const int nrows, ggml_sort_order order,
  9571. dpct::queue_ptr stream) {
  9572. // bitonic sort requires ncols to be power of 2
  9573. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  9574. const sycl::range<3> block_dims(1, 1, ncols);
  9575. const sycl::range<3> block_nums(1, nrows, 1);
  9576. if (order == GGML_SORT_ORDER_ASC) {
  9577. /*
  9578. DPCT1049:44: The work-group size passed to the SYCL kernel may exceed
  9579. the limit. To get the device limit, query
  9580. info::device::max_work_group_size. Adjust the work-group size if needed.
  9581. */
  9582. stream->parallel_for(
  9583. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9584. [=](sycl::nd_item<3> item_ct1) {
  9585. k_argsort_f32_i32<GGML_SORT_ORDER_ASC>(x, dst, ncols, item_ct1);
  9586. });
  9587. } else if (order == GGML_SORT_ORDER_DESC) {
  9588. /*
  9589. DPCT1049:45: The work-group size passed to the SYCL kernel may exceed
  9590. the limit. To get the device limit, query
  9591. info::device::max_work_group_size. Adjust the work-group size if needed.
  9592. */
  9593. stream->parallel_for(
  9594. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9595. [=](sycl::nd_item<3> item_ct1) {
  9596. k_argsort_f32_i32<GGML_SORT_ORDER_DESC>(x, dst, ncols, item_ct1);
  9597. });
  9598. } else {
  9599. GGML_ASSERT(false);
  9600. }
  9601. }
  9602. static void diag_mask_inf_f32_sycl(const float *x, float *dst,
  9603. const int ncols_x, const int nrows_x,
  9604. const int rows_per_channel, const int n_past,
  9605. dpct::queue_ptr stream) {
  9606. const sycl::range<3> block_dims(1, SYCL_DIAG_MASK_INF_BLOCK_SIZE, 1);
  9607. const int block_num_x = (ncols_x + SYCL_DIAG_MASK_INF_BLOCK_SIZE - 1) / SYCL_DIAG_MASK_INF_BLOCK_SIZE;
  9608. const sycl::range<3> block_nums(1, block_num_x, nrows_x);
  9609. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9610. [=](sycl::nd_item<3> item_ct1) {
  9611. diag_mask_inf_f32(x, dst, ncols_x,
  9612. rows_per_channel, n_past,
  9613. item_ct1);
  9614. });
  9615. }
  9616. template <bool vals_smem, int ncols_template, int block_size_template>
  9617. static void soft_max_f32_submitter(const float * x, const float * mask, const float *pos, float * dst, const int ncols_par,
  9618. const int nrows_y, const float scale, const float max_bias, const float m0,
  9619. const float m1, uint32_t n_head_log2, sycl::range<3> block_nums, sycl::range<3> block_dims,
  9620. const size_t n_local_scratch, dpct::queue_ptr stream) {
  9621. stream->submit([&](sycl::handler &cgh) {
  9622. sycl::local_accessor<float, 1> local_buf_acc(n_local_scratch, cgh);
  9623. cgh.parallel_for(
  9624. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9625. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  9626. soft_max_f32<vals_smem, ncols_template, block_size_template>(x, mask, pos, dst, ncols_par,
  9627. nrows_y, scale, max_bias, m0,
  9628. m1, n_head_log2, item_ct1,
  9629. local_buf_acc.get_pointer());
  9630. });
  9631. });
  9632. }
  9633. static void soft_max_f32_sycl(const float * x, const float * mask, const float * pos,
  9634. float * dst, const int ncols_x, const int nrows_x,
  9635. const int nrows_y, const float scale, const float max_bias,
  9636. dpct::queue_ptr stream) {
  9637. int nth = WARP_SIZE;
  9638. while (nth < ncols_x && nth < SYCL_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  9639. const sycl::range<3> block_dims(1, 1, nth);
  9640. const sycl::range<3> block_nums(1, 1, nrows_x);
  9641. const size_t n_local_scratch = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE);
  9642. static_assert(SYCL_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  9643. const uint32_t n_head_kv = nrows_x/nrows_y;
  9644. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  9645. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  9646. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  9647. const size_t local_mem_size = stream->get_device().get_info<sycl::info::device::local_mem_size>();
  9648. if (n_local_scratch*sizeof(float) < local_mem_size) {
  9649. switch (ncols_x) {
  9650. case 32:
  9651. soft_max_f32_submitter<true, 32, 32>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  9652. max_bias, m0, m1, n_head_log2, block_nums,
  9653. block_dims, n_local_scratch, stream);
  9654. break;
  9655. case 64:
  9656. soft_max_f32_submitter<true, 64, 64>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  9657. max_bias, m0, m1, n_head_log2, block_nums,
  9658. block_dims, n_local_scratch, stream);
  9659. break;
  9660. case 128:
  9661. soft_max_f32_submitter<true, 128, 128>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  9662. max_bias, m0, m1, n_head_log2, block_nums,
  9663. block_dims, n_local_scratch, stream);
  9664. break;
  9665. case 256:
  9666. soft_max_f32_submitter<true, 256, 256>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  9667. max_bias, m0, m1, n_head_log2, block_nums,
  9668. block_dims, n_local_scratch, stream);
  9669. break;
  9670. case 512:
  9671. soft_max_f32_submitter<true, 512, 512>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  9672. max_bias, m0, m1, n_head_log2, block_nums,
  9673. block_dims, n_local_scratch, stream);
  9674. break;
  9675. case 1024:
  9676. soft_max_f32_submitter<true, 1024, 1024>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  9677. max_bias, m0, m1, n_head_log2, block_nums,
  9678. block_dims, n_local_scratch, stream);
  9679. break;
  9680. case 2048:
  9681. soft_max_f32_submitter<true, 2048, 1024>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  9682. max_bias, m0, m1, n_head_log2, block_nums,
  9683. block_dims, n_local_scratch, stream);
  9684. break;
  9685. case 4096:
  9686. soft_max_f32_submitter<true, 4096, 1024>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  9687. max_bias, m0, m1, n_head_log2, block_nums,
  9688. block_dims, n_local_scratch, stream);
  9689. break;
  9690. default:
  9691. soft_max_f32_submitter<true, 0, 0>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  9692. max_bias, m0, m1, n_head_log2, block_nums,
  9693. block_dims, n_local_scratch, stream);
  9694. break;
  9695. }
  9696. } else {
  9697. soft_max_f32_submitter<false, 0, 0>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  9698. max_bias, m0, m1, n_head_log2, block_nums,
  9699. block_dims, WARP_SIZE, stream);
  9700. }
  9701. }
  9702. template <typename T>
  9703. static void im2col_sycl(const float *x, T *dst, int IW, int IH,
  9704. int OW, int OH, int KW, int KH, int IC,
  9705. int offset_delta, int s0, int s1, int p0,
  9706. int p1, int d0, int d1,
  9707. dpct::queue_ptr stream) {
  9708. const int parallel_elements = OW * KW * KH;
  9709. const int num_blocks = (parallel_elements + SYCL_IM2COL_BLOCK_SIZE - 1) / SYCL_IM2COL_BLOCK_SIZE;
  9710. sycl::range<3> block_nums(IC, OH, num_blocks);
  9711. {
  9712. dpct::has_capability_or_fail(stream->get_device(),
  9713. {sycl::aspect::fp16});
  9714. stream->parallel_for(
  9715. sycl::nd_range<3>(block_nums *
  9716. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  9717. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  9718. [=](sycl::nd_item<3> item_ct1) {
  9719. im2col_kernel(x, dst, offset_delta, IW, IH, OW, KW, KH,
  9720. parallel_elements, (IC * KH * KW), s0, s1, p0,
  9721. p1, d0, d1, item_ct1);
  9722. });
  9723. }
  9724. }
  9725. // buffer pool for sycl
  9726. #define MAX_SYCL_BUFFERS 256
  9727. struct scoped_spin_lock {
  9728. std::atomic_flag& lock;
  9729. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  9730. while (lock.test_and_set(std::memory_order_acquire)) {
  9731. ; // spin
  9732. }
  9733. }
  9734. ~scoped_spin_lock() {
  9735. lock.clear(std::memory_order_release);
  9736. }
  9737. scoped_spin_lock(const scoped_spin_lock&) = delete;
  9738. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  9739. };
  9740. static std::atomic_flag g_sycl_pool_lock = ATOMIC_FLAG_INIT;
  9741. // #define DEBUG_SYCL_MALLOC
  9742. struct sycl_buffer {
  9743. void * ptr = nullptr;
  9744. size_t size = 0;
  9745. };
  9746. static sycl_buffer g_sycl_buffer_pool[GGML_SYCL_MAX_DEVICES][MAX_SYCL_BUFFERS];
  9747. static size_t g_sycl_pool_size[GGML_SYCL_MAX_DEVICES] = {0};
  9748. static void *ggml_sycl_pool_malloc_leg(int device_index, size_t size, size_t *actual_size) try {
  9749. scoped_spin_lock lock(g_sycl_pool_lock);
  9750. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg device_index %d size=%lu\n", device_index, size);
  9751. #ifdef DEBUG_SYCL_MALLOC
  9752. int nnz = 0;
  9753. size_t max_size = 0;
  9754. #endif
  9755. size_t best_diff = 1ull << 36;
  9756. int ibest = -1;
  9757. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  9758. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  9759. if (b.ptr != nullptr) {
  9760. #ifdef DEBUG_SYCL_MALLOC
  9761. ++nnz;
  9762. if (b.size > max_size) max_size = b.size;
  9763. #endif
  9764. if (b.size >= size) {
  9765. size_t diff = b.size - size;
  9766. if (diff < best_diff) {
  9767. best_diff = diff;
  9768. ibest = i;
  9769. if (!best_diff) {
  9770. void * ptr = b.ptr;
  9771. *actual_size = b.size;
  9772. b.ptr = nullptr;
  9773. b.size = 0;
  9774. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 1 %p and rm in pool\n", ptr);
  9775. return ptr;
  9776. }
  9777. }
  9778. }
  9779. }
  9780. }
  9781. if (ibest >= 0) {
  9782. sycl_buffer& b = g_sycl_buffer_pool[device_index][ibest];
  9783. void * ptr = b.ptr;
  9784. *actual_size = b.size;
  9785. b.ptr = nullptr;
  9786. b.size = 0;
  9787. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 2 %p and rm in pool\n", ptr);
  9788. return ptr;
  9789. }
  9790. void * ptr;
  9791. size_t look_ahead_size = (size_t) (1.05 * size);
  9792. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  9793. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  9794. SYCL_CHECK(
  9795. CHECK_TRY_ERROR(ptr = (void *)sycl::malloc_device(
  9796. look_ahead_size, *stream)));
  9797. *actual_size = look_ahead_size;
  9798. g_sycl_pool_size[device_index] += look_ahead_size;
  9799. #ifdef DEBUG_SYCL_MALLOC
  9800. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  9801. (uint32_t)(max_size/1024/1024), (uint32_t)(g_sycl_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  9802. #endif
  9803. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg look_ahead_size=%lu, return %p\n", look_ahead_size, ptr);
  9804. return ptr;
  9805. }
  9806. catch (sycl::exception const &exc) {
  9807. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9808. << ", line:" << __LINE__ << std::endl;
  9809. std::exit(1);
  9810. }
  9811. static void ggml_sycl_pool_free_leg(int device_index, void *ptr, size_t size) try {
  9812. scoped_spin_lock lock(g_sycl_pool_lock);
  9813. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  9814. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  9815. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  9816. if (b.ptr == nullptr) {
  9817. b.ptr = ptr;
  9818. b.size = size;
  9819. return;
  9820. }
  9821. }
  9822. fprintf(stderr, "WARNING: sycl buffer pool full, increase MAX_SYCL_BUFFERS\n");
  9823. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *stream)));
  9824. g_sycl_pool_size[device_index] -= size;
  9825. }
  9826. catch (sycl::exception const &exc) {
  9827. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9828. << ", line:" << __LINE__ << std::endl;
  9829. std::exit(1);
  9830. }
  9831. // pool with virtual memory
  9832. /*
  9833. DPCT1082:64: Migration of CUmemGenericAllocationHandle type is not supported.
  9834. */
  9835. // static std::vector<CUmemGenericAllocationHandle>
  9836. // g_sycl_pool_handles[GGML_SYCL_MAX_DEVICES];
  9837. static dpct::device_ptr g_sycl_pool_addr[GGML_SYCL_MAX_DEVICES] = {0};
  9838. static size_t g_sycl_pool_used[GGML_SYCL_MAX_DEVICES] = {0};
  9839. static void *ggml_sycl_pool_malloc_vmm(int device_index, size_t size, size_t *actual_size) try {
  9840. GGML_UNUSED(device_index);
  9841. GGML_UNUSED(size);
  9842. GGML_UNUSED(actual_size);
  9843. return NULL;
  9844. }
  9845. catch (sycl::exception const &exc) {
  9846. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9847. << ", line:" << __LINE__ << std::endl;
  9848. std::exit(1);
  9849. }
  9850. static void ggml_sycl_pool_free_vmm(int device_index, void *ptr, size_t size) try {
  9851. scoped_spin_lock lock(g_sycl_pool_lock);
  9852. #ifdef DEBUG_SYCL_MALLOC
  9853. printf("sycl pool[%d]: freed %llu bytes at %llx\n", device_index, (unsigned long long) size, ptr);
  9854. #endif
  9855. g_sycl_pool_used[device_index] -= size;
  9856. // all deallocations must be in reverse order of the allocations
  9857. GGML_ASSERT(ptr == (void *) (g_sycl_pool_addr[device_index] + g_sycl_pool_used[device_index]));
  9858. }
  9859. catch (sycl::exception const &exc) {
  9860. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9861. << ", line:" << __LINE__ << std::endl;
  9862. std::exit(1);
  9863. }
  9864. static void *ggml_sycl_pool_malloc(int device_index, size_t size, size_t *actual_size) try {
  9865. if (g_device_caps[device_index].vmm) {
  9866. return ggml_sycl_pool_malloc_vmm(device_index, size, actual_size);
  9867. } else {
  9868. return ggml_sycl_pool_malloc_leg(device_index, size, actual_size);
  9869. }
  9870. }
  9871. catch (sycl::exception const &exc) {
  9872. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9873. << ", line:" << __LINE__ << std::endl;
  9874. std::exit(1);
  9875. }
  9876. static void ggml_sycl_pool_free(int device_index, void *ptr, size_t size) try {
  9877. if (g_device_caps[device_index].vmm) {
  9878. ggml_sycl_pool_free_vmm(device_index, ptr, size);
  9879. } else {
  9880. ggml_sycl_pool_free_leg(device_index, ptr, size);
  9881. }
  9882. }
  9883. catch (sycl::exception const &exc) {
  9884. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9885. << ", line:" << __LINE__ << std::endl;
  9886. std::exit(1);
  9887. }
  9888. template<typename T>
  9889. struct sycl_pool_alloc {
  9890. int device_index = -1;
  9891. int device_id = -1;
  9892. T * ptr = nullptr;
  9893. size_t actual_size = 0;
  9894. // size is in number of elements
  9895. T * alloc(size_t size) {
  9896. GGML_ASSERT(ptr == nullptr);
  9897. device_id = get_current_device_id();
  9898. device_index = g_sycl_gpu_mgr->get_index(device_id);
  9899. ptr = (T *) ggml_sycl_pool_malloc(device_index, size * sizeof(T), &this->actual_size);
  9900. // GGML_SYCL_DEBUG("alloc %lu return %p actual size=%lu\n", size * sizeof(T), ptr, this->actual_size);
  9901. return ptr;
  9902. }
  9903. sycl_pool_alloc(size_t size) {
  9904. alloc(size);
  9905. }
  9906. ~sycl_pool_alloc() {
  9907. if (ptr != nullptr) {
  9908. ggml_sycl_pool_free(device_index, ptr, actual_size);
  9909. }
  9910. }
  9911. T * get() {
  9912. return ptr;
  9913. }
  9914. sycl_pool_alloc() = default;
  9915. sycl_pool_alloc(const sycl_pool_alloc &) = delete;
  9916. sycl_pool_alloc(sycl_pool_alloc &&) = delete;
  9917. sycl_pool_alloc& operator=(const sycl_pool_alloc &) = delete;
  9918. sycl_pool_alloc& operator=(sycl_pool_alloc &&) = delete;
  9919. };
  9920. static bool g_sycl_loaded = false;
  9921. bool ggml_sycl_loaded(void) {
  9922. return g_sycl_loaded;
  9923. }
  9924. void print_device_detail(int id) {
  9925. dpct::device_info prop;
  9926. SYCL_CHECK(CHECK_TRY_ERROR(
  9927. dpct::get_device_info(prop, dpct::dev_mgr::instance().get_device(id))));
  9928. sycl::device cur_device = dpct::dev_mgr::instance().get_device(id);
  9929. std::string version;
  9930. version += std::to_string(prop.get_major_version());
  9931. version += ".";
  9932. version += std::to_string(prop.get_minor_version());
  9933. fprintf(stderr, "|%2d|%45s|%18s|%17d|%14d|%13d|%15lu|\n", id,
  9934. prop.get_name(), version.c_str(), prop.get_max_compute_units(),
  9935. prop.get_max_work_group_size(), prop.get_max_sub_group_size(),
  9936. prop.get_global_mem_size());
  9937. }
  9938. void ggml_backend_sycl_print_sycl_devices() {
  9939. int device_count = dpct::dev_mgr::instance().device_count();
  9940. fprintf(stderr, "found %d SYCL devices:\n", device_count);
  9941. fprintf(stderr, "|ID| Name |compute capability|Max compute units|Max work group|Max sub group|Global mem size|\n");
  9942. fprintf(stderr, "|--|---------------------------------------------|------------------|-----------------|--------------|-------------|---------------|\n");
  9943. for (int id = 0; id < device_count; ++id) {
  9944. print_device_detail(id);
  9945. }
  9946. }
  9947. void print_gpu_device_list() {
  9948. fprintf(stderr, "detect %d SYCL GPUs: [%s] with Max compute units:%d\n",
  9949. g_sycl_gpu_mgr->get_gpu_count(),
  9950. g_sycl_gpu_mgr->gpus_list.c_str(),
  9951. g_sycl_gpu_mgr->max_compute_units);
  9952. }
  9953. int get_sycl_env(const char *env_name, int default_val) {
  9954. char *user_device_string = getenv(env_name);
  9955. int user_number = default_val;
  9956. unsigned n;
  9957. if (user_device_string != NULL &&
  9958. sscanf(user_device_string, " %u", &n) == 1) {
  9959. user_number = (int)n;
  9960. } else {
  9961. user_number = default_val;
  9962. }
  9963. return user_number;
  9964. }
  9965. int get_work_group_size(int user_device_id) {
  9966. dpct::device_info prop;
  9967. dpct::get_device_info(prop,
  9968. dpct::dev_mgr::instance().get_device(user_device_id));
  9969. return prop.get_max_work_group_size();
  9970. }
  9971. void ggml_init_sycl() try {
  9972. static bool initialized = false;
  9973. if (!initialized) {
  9974. g_ggml_sycl_debug = get_sycl_env("GGML_SYCL_DEBUG", 0);
  9975. fprintf(stderr, "%s: GGML_SYCL_DEBUG: %d\n", __func__, g_ggml_sycl_debug);
  9976. #if defined(GGML_SYCL_F16)
  9977. fprintf(stderr, "%s: GGML_SYCL_F16: yes\n", __func__);
  9978. #else
  9979. fprintf(stderr, "%s: GGML_SYCL_F16: no\n", __func__);
  9980. #endif
  9981. if (CHECK_TRY_ERROR(g_all_sycl_device_count =
  9982. dpct::dev_mgr::instance().device_count()) != 0) {
  9983. initialized = true;
  9984. g_sycl_loaded = false;
  9985. return;
  9986. }
  9987. GGML_ASSERT(g_all_sycl_device_count <= GGML_SYCL_MAX_DEVICES);
  9988. ggml_backend_sycl_print_sycl_devices();
  9989. if (!g_sycl_gpu_mgr) g_sycl_gpu_mgr = new sycl_gpu_mgr();
  9990. g_device_count = g_sycl_gpu_mgr->get_gpu_count();
  9991. g_work_group_size = g_sycl_gpu_mgr->work_group_size;
  9992. print_gpu_device_list();
  9993. int64_t total_vram = 0;
  9994. /* NOT REMOVE, keep it for next optimize for XMX.
  9995. #if defined(SYCL_USE_XMX)
  9996. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  9997. #else
  9998. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  9999. #endif
  10000. */
  10001. for (int id = 0; id < GGML_SYCL_MAX_DEVICES; ++id) {
  10002. g_device_caps[id].vmm = 0;
  10003. g_device_caps[id].device_id = -1;
  10004. g_device_caps[id].cc = 0;
  10005. g_tensor_split[id] = 0;
  10006. g_default_tensor_split[id] = 0;
  10007. }
  10008. for (int i = 0; i < g_device_count; ++i) {
  10009. int device_id = g_sycl_gpu_mgr->gpus[i];
  10010. g_device_caps[i].vmm = 0;
  10011. dpct::device_info prop;
  10012. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  10013. prop, dpct::dev_mgr::instance().get_device(device_id))));
  10014. g_default_tensor_split[i] = total_vram;
  10015. total_vram += prop.get_global_mem_size();
  10016. g_device_caps[i].cc =
  10017. 100 * prop.get_major_version() + 10 * prop.get_minor_version();
  10018. }
  10019. for (int i = 0; i < g_device_count; ++i) {
  10020. g_default_tensor_split[i] /= total_vram;
  10021. }
  10022. for (int i = 0; i < g_device_count; ++i) {
  10023. SYCL_CHECK(ggml_sycl_set_device(i));
  10024. // create sycl streams
  10025. for (int is = 0; is < MAX_STREAMS; ++is) {
  10026. SYCL_CHECK(CHECK_TRY_ERROR(
  10027. g_syclStreams[i][is] =
  10028. dpct::get_current_device().create_queue(
  10029. g_sycl_gpu_mgr->get_co_ctx(), dpct::get_current_device())));
  10030. }
  10031. const dpct::queue_ptr stream = g_syclStreams[i][0];
  10032. // create sycl handle
  10033. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[i] = stream));
  10034. }
  10035. initialized = true;
  10036. g_sycl_loaded = true;
  10037. }
  10038. }
  10039. catch (sycl::exception const &exc) {
  10040. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10041. << ", line:" << __LINE__ << std::endl;
  10042. std::exit(1);
  10043. }
  10044. void *ggml_sycl_host_malloc(size_t size) try {
  10045. if (getenv("GGML_SYCL_NO_PINNED") != nullptr) {
  10046. return nullptr;
  10047. }
  10048. void * ptr = nullptr;
  10049. //allow to use dpct::get_in_order_queue() for host malloc
  10050. dpct::err0 err = CHECK_TRY_ERROR(
  10051. ptr = (void *)sycl::malloc_host(size, dpct::get_in_order_queue()));
  10052. if (err != 0) {
  10053. // clear the error
  10054. fprintf(
  10055. stderr,
  10056. "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  10057. size / 1024.0 / 1024.0,
  10058. "syclGetErrorString is not supported");
  10059. return nullptr;
  10060. }
  10061. return ptr;
  10062. }
  10063. catch (sycl::exception const &exc) {
  10064. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10065. << ", line:" << __LINE__ << std::endl;
  10066. std::exit(1);
  10067. }
  10068. void ggml_sycl_host_free(void *ptr) try {
  10069. //allow to use dpct::get_in_order_queue() for host malloc
  10070. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, dpct::get_in_order_queue())));
  10071. }
  10072. catch (sycl::exception const &exc) {
  10073. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10074. << ", line:" << __LINE__ << std::endl;
  10075. std::exit(1);
  10076. }
  10077. static dpct::err0 ggml_sycl_cpy_tensor_2d(void *dst,
  10078. const struct ggml_tensor *src,
  10079. int64_t i3, int64_t i2,
  10080. int64_t i1_low, int64_t i1_high,
  10081. dpct::queue_ptr stream) try {
  10082. dpct::memcpy_direction kind;
  10083. char * src_ptr;
  10084. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  10085. kind = dpct::host_to_device;
  10086. src_ptr = (char *) src->data;
  10087. // GGML_SYCL_DEBUG("ggml_sycl_cpy_tensor_2d GGML_BACKEND_TYPE_CPU src_ptr %p\n", src_ptr);
  10088. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  10089. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  10090. kind = dpct::device_to_device;
  10091. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  10092. int id;
  10093. SYCL_CHECK(CHECK_TRY_ERROR(
  10094. id = get_current_device_id()));
  10095. // GGML_SYCL_DEBUG("current device index %d\n", id);
  10096. src_ptr = (char *) extra->data_device[id];
  10097. } else {
  10098. // GGML_SYCL_DEBUG("GGML_ASSERT(false)\n");
  10099. GGML_ASSERT(false);
  10100. }
  10101. char * dst_ptr = (char *) dst;
  10102. GGML_TENSOR_LOCALS_1(int64_t, ne, src, ne);
  10103. GGML_TENSOR_LOCALS(int64_t, nb, src, nb);
  10104. const enum ggml_type type = src->type;
  10105. const int64_t ts = ggml_type_size(type);
  10106. const int64_t bs = ggml_blck_size(type);
  10107. int64_t i1_diff = i1_high - i1_low;
  10108. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  10109. if (nb0 == ts && nb1 == ts*ne0/bs) {
  10110. // GGML_SYCL_DEBUG("stream->memcpy: dst_ptr=%p, x=%p, size=%lu\n", dst_ptr, x, i1_diff * nb1);
  10111. // return CHECK_TRY_ERROR(stream->memcpy(dst_ptr, x, i1_diff * nb1));
  10112. return CHECK_TRY_ERROR(dpct::async_dpct_memcpy(dst_ptr, x, i1_diff * nb1,
  10113. kind, *stream));
  10114. } else if (nb0 == ts) {
  10115. return CHECK_TRY_ERROR(
  10116. dpct::async_dpct_memcpy(dst_ptr, ts * ne0 / bs, x, nb1,
  10117. ts * ne0 / bs, i1_diff, kind, *stream));
  10118. } else {
  10119. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  10120. const void * rx = (const void *) ((const char *) x + i1*nb1);
  10121. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  10122. // pretend the row is a matrix with cols=1
  10123. dpct::err0 r = CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  10124. rd, ts / bs, rx, nb0, ts / bs, ne0, kind, *stream));
  10125. /*
  10126. DPCT1001:85: The statement could not be removed.
  10127. */
  10128. /*
  10129. DPCT1000:86: Error handling if-stmt was detected but could not be
  10130. rewritten.
  10131. */
  10132. if (r != 0) return r;
  10133. }
  10134. return 0;
  10135. }
  10136. }
  10137. catch (sycl::exception const &exc) {
  10138. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10139. << ", line:" << __LINE__ << std::endl;
  10140. std::exit(1);
  10141. }
  10142. static void ggml_sycl_op_get_rows(const ggml_tensor *src0,
  10143. const ggml_tensor *src1, ggml_tensor *dst,
  10144. const float *src0_d, const float *src1_d,
  10145. float *dst_d, const dpct::queue_ptr &stream) {
  10146. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  10147. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10148. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  10149. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  10150. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  10151. const int32_t * src1_i32 = (const int32_t *) src1_d;
  10152. switch (src0->type) {
  10153. case GGML_TYPE_F16:
  10154. get_rows_sycl_float(src0, src1, dst, (const sycl::half *)src0_d,
  10155. src1_i32, dst_d, stream);
  10156. break;
  10157. case GGML_TYPE_F32:
  10158. get_rows_sycl_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10159. break;
  10160. case GGML_TYPE_Q4_0:
  10161. get_rows_sycl<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10162. break;
  10163. case GGML_TYPE_Q4_1:
  10164. get_rows_sycl<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10165. break;
  10166. case GGML_TYPE_Q5_0:
  10167. get_rows_sycl<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10168. break;
  10169. case GGML_TYPE_Q5_1:
  10170. get_rows_sycl<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10171. break;
  10172. case GGML_TYPE_Q8_0:
  10173. get_rows_sycl<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  10174. break;
  10175. default:
  10176. // TODO: k-quants
  10177. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  10178. GGML_ASSERT(false);
  10179. break;
  10180. }
  10181. }
  10182. template <class op>
  10183. inline void ggml_sycl_op_bin_bcast(const ggml_tensor *src0,
  10184. const ggml_tensor *src1, ggml_tensor *dst,
  10185. const float *src0_dd, const float *src1_dd,
  10186. float *dst_dd,
  10187. const dpct::queue_ptr &main_stream) {
  10188. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  10189. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  10190. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  10191. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd,
  10192. (sycl::half *)dst_dd, main_stream);
  10193. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  10194. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd, dst_dd,
  10195. main_stream);
  10196. } else if (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  10197. op()(src0, src1, dst, (const int32_t *)src0_dd, (const int32_t *)src1_dd, (int32_t *)dst_dd,
  10198. main_stream);
  10199. } else if (src0->type == GGML_TYPE_I16 && dst->type == GGML_TYPE_I16) {
  10200. op()(src0, src1, dst, (const int16_t *)src0_dd, (const int16_t *)src1_dd, (int16_t *)dst_dd,
  10201. main_stream);
  10202. } else {
  10203. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  10204. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  10205. GGML_ASSERT(false);
  10206. }
  10207. }
  10208. static void ggml_sycl_op_repeat(const ggml_tensor *src0,
  10209. const ggml_tensor *src1, ggml_tensor *dst,
  10210. const float *src0_d, const float *src1_d,
  10211. float *dst_d,
  10212. const dpct::queue_ptr &main_stream) {
  10213. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  10214. (void) src1;
  10215. (void) src1_d;
  10216. }
  10217. inline void ggml_sycl_op_add(const ggml_tensor *src0, const ggml_tensor *src1,
  10218. ggml_tensor *dst, const float *src0_dd,
  10219. const float *src1_dd, float *dst_dd,
  10220. const dpct::queue_ptr &main_stream) {
  10221. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  10222. }
  10223. inline void ggml_sycl_op_acc(const ggml_tensor *src0, const ggml_tensor *src1,
  10224. ggml_tensor *dst, const float *src0_dd,
  10225. const float *src1_dd, float *dst_dd,
  10226. const dpct::queue_ptr &main_stream) {
  10227. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10228. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10229. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10230. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  10231. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  10232. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  10233. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  10234. int offset = dst->op_params[3] / 4; // offset in bytes
  10235. 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);
  10236. (void) dst;
  10237. }
  10238. inline void ggml_sycl_op_mul(const ggml_tensor *src0, const ggml_tensor *src1,
  10239. ggml_tensor *dst, const float *src0_dd,
  10240. const float *src1_dd, float *dst_dd,
  10241. const dpct::queue_ptr &main_stream) {
  10242. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  10243. }
  10244. inline void ggml_sycl_op_div(const ggml_tensor *src0, const ggml_tensor *src1,
  10245. ggml_tensor *dst, const float *src0_dd,
  10246. const float *src1_dd, float *dst_dd,
  10247. const dpct::queue_ptr &main_stream) {
  10248. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  10249. }
  10250. inline void ggml_sycl_op_gelu(const ggml_tensor *src0, const ggml_tensor *src1,
  10251. ggml_tensor *dst, const float *src0_dd,
  10252. const float *src1_dd, float *dst_dd,
  10253. const dpct::queue_ptr &main_stream) {
  10254. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10255. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10256. gelu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10257. (void) src1;
  10258. (void) dst;
  10259. (void) src1_dd;
  10260. }
  10261. inline void ggml_sycl_op_silu(const ggml_tensor *src0, const ggml_tensor *src1,
  10262. ggml_tensor *dst, const float *src0_dd,
  10263. const float *src1_dd, float *dst_dd,
  10264. const dpct::queue_ptr &main_stream) {
  10265. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10266. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10267. silu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10268. (void) src1;
  10269. (void) dst;
  10270. (void) src1_dd;
  10271. }
  10272. inline void ggml_sycl_op_gelu_quick(const ggml_tensor *src0,
  10273. const ggml_tensor *src1, ggml_tensor *dst,
  10274. const float *src0_dd, const float *src1_dd,
  10275. float *dst_dd,
  10276. const dpct::queue_ptr &main_stream) {
  10277. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10278. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10279. gelu_quick_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10280. (void) src1;
  10281. (void) dst;
  10282. (void) src1_dd;
  10283. }
  10284. inline void ggml_sycl_op_tanh(const ggml_tensor *src0, const ggml_tensor *src1,
  10285. ggml_tensor *dst, const float *src0_dd,
  10286. const float *src1_dd, float *dst_dd,
  10287. const dpct::queue_ptr &main_stream) {
  10288. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10289. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10290. tanh_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10291. (void) src1;
  10292. (void) dst;
  10293. (void) src1_dd;
  10294. }
  10295. inline void ggml_sycl_op_relu(const ggml_tensor *src0, const ggml_tensor *src1,
  10296. ggml_tensor *dst, const float *src0_dd,
  10297. const float *src1_dd, float *dst_dd,
  10298. const dpct::queue_ptr &main_stream) {
  10299. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10300. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10301. relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10302. (void) src1;
  10303. (void) dst;
  10304. (void) src1_dd;
  10305. }
  10306. static void ggml_sycl_op_hardsigmoid(const ggml_tensor *src0,
  10307. const ggml_tensor *src1, ggml_tensor *dst,
  10308. const float *src0_dd, const float *src1_dd,
  10309. float *dst_dd,
  10310. const dpct::queue_ptr &main_stream) {
  10311. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10312. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10313. hardsigmoid_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10314. (void) src1;
  10315. (void) dst;
  10316. (void) src1_dd;
  10317. }
  10318. static void ggml_sycl_op_hardswish(const ggml_tensor *src0,
  10319. const ggml_tensor *src1, ggml_tensor *dst,
  10320. const float *src0_dd, const float *src1_dd,
  10321. float *dst_dd, const dpct::queue_ptr &main_stream) {
  10322. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10323. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10324. hardswish_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10325. (void) src1;
  10326. (void) dst;
  10327. (void) src1_dd;
  10328. }
  10329. inline void ggml_sycl_op_leaky_relu(const ggml_tensor *src0,
  10330. const ggml_tensor *src1, ggml_tensor *dst,
  10331. const float *src0_dd, const float *src1_dd,
  10332. float *dst_dd,
  10333. const dpct::queue_ptr &main_stream) {
  10334. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10335. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10336. float negative_slope;
  10337. memcpy(&negative_slope, dst->op_params, sizeof(float));
  10338. leaky_relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  10339. (void) src1;
  10340. (void) dst;
  10341. (void) src1_dd;
  10342. }
  10343. inline void ggml_sycl_op_sqr(const ggml_tensor *src0, const ggml_tensor *src1,
  10344. ggml_tensor *dst, const float *src0_dd,
  10345. const float *src1_dd, float *dst_dd,
  10346. const dpct::queue_ptr &main_stream) {
  10347. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10348. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10349. sqr_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  10350. (void) src1;
  10351. (void) dst;
  10352. (void) src1_dd;
  10353. }
  10354. inline void ggml_sycl_op_norm(const ggml_tensor *src0, const ggml_tensor *src1,
  10355. ggml_tensor *dst, const float *src0_dd,
  10356. const float *src1_dd, float *dst_dd,
  10357. const dpct::queue_ptr &main_stream) {
  10358. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10359. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10360. const int64_t ne00 = src0->ne[0];
  10361. const int64_t nrows = ggml_nrows(src0);
  10362. float eps;
  10363. memcpy(&eps, dst->op_params, sizeof(float));
  10364. norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  10365. (void) src1;
  10366. (void) dst;
  10367. (void) src1_dd;
  10368. }
  10369. inline void ggml_sycl_op_group_norm(const ggml_tensor *src0,
  10370. const ggml_tensor *src1, ggml_tensor *dst,
  10371. const float *src0_dd, const float *src1_dd,
  10372. float *dst_dd,
  10373. const dpct::queue_ptr &main_stream) {
  10374. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10375. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10376. int num_groups = dst->op_params[0];
  10377. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  10378. group_norm_f32_sycl(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  10379. (void) src1;
  10380. (void) dst;
  10381. (void) src1_dd;
  10382. }
  10383. inline void ggml_sycl_op_concat(const ggml_tensor *src0,
  10384. const ggml_tensor *src1, ggml_tensor *dst,
  10385. const float *src0_dd, const float *src1_dd,
  10386. float *dst_dd,
  10387. const dpct::queue_ptr &main_stream) {
  10388. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10389. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10390. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10391. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  10392. 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);
  10393. }
  10394. (void) src1;
  10395. (void) dst;
  10396. }
  10397. inline void ggml_sycl_op_upscale(const ggml_tensor *src0,
  10398. const ggml_tensor *src1, ggml_tensor *dst,
  10399. const float *src0_dd, const float *src1_dd,
  10400. float *dst_dd,
  10401. const dpct::queue_ptr &main_stream) {
  10402. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10403. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10404. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  10405. const int scale_factor = dst->op_params[0];
  10406. upscale_f32_sycl(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  10407. (void) src1;
  10408. (void) dst;
  10409. (void) src1_dd;
  10410. }
  10411. inline void ggml_sycl_op_pad(const ggml_tensor *src0, const ggml_tensor *src1,
  10412. ggml_tensor *dst, const float *src0_dd,
  10413. const float *src1_dd, float *dst_dd,
  10414. const dpct::queue_ptr &main_stream) {
  10415. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10416. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10417. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  10418. pad_f32_sycl(src0_dd, dst_dd,
  10419. src0->ne[0], src0->ne[1], src0->ne[2],
  10420. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  10421. (void) src1;
  10422. (void) dst;
  10423. (void) src1_dd;
  10424. }
  10425. inline void ggml_sycl_op_rms_norm(const ggml_tensor *src0,
  10426. const ggml_tensor *src1, ggml_tensor *dst,
  10427. const float *src0_dd, const float *src1_dd,
  10428. float *dst_dd,
  10429. const dpct::queue_ptr &main_stream) {
  10430. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10431. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10432. const int64_t ne00 = src0->ne[0];
  10433. const int64_t nrows = ggml_nrows(src0);
  10434. float eps;
  10435. memcpy(&eps, dst->op_params, sizeof(float));
  10436. rms_norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  10437. (void) src1;
  10438. (void) dst;
  10439. (void) src1_dd;
  10440. }
  10441. inline void ggml_sycl_op_mul_mat_q(
  10442. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  10443. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  10444. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  10445. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  10446. const dpct::queue_ptr &stream) try {
  10447. const int64_t ne00 = src0->ne[0];
  10448. const int64_t ne10 = src1->ne[0];
  10449. GGML_ASSERT(ne10 % QK8_1 == 0);
  10450. const int64_t ne0 = dst->ne[0];
  10451. const int64_t row_diff = row_high - row_low;
  10452. int device_id;
  10453. SYCL_CHECK(
  10454. CHECK_TRY_ERROR(device_id = get_current_device_id()));
  10455. // the main device has a larger memory buffer to hold the results from all GPUs
  10456. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  10457. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && device_id == g_main_device ? ne0 : row_diff;
  10458. switch (src0->type) {
  10459. case GGML_TYPE_Q4_0:
  10460. 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);
  10461. break;
  10462. case GGML_TYPE_Q4_1:
  10463. 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);
  10464. break;
  10465. case GGML_TYPE_Q5_0:
  10466. 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);
  10467. break;
  10468. case GGML_TYPE_Q5_1:
  10469. 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);
  10470. break;
  10471. case GGML_TYPE_Q8_0:
  10472. 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);
  10473. break;
  10474. case GGML_TYPE_Q2_K:
  10475. 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);
  10476. break;
  10477. case GGML_TYPE_Q3_K:
  10478. 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);
  10479. break;
  10480. case GGML_TYPE_Q4_K:
  10481. 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);
  10482. break;
  10483. case GGML_TYPE_Q5_K:
  10484. 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);
  10485. break;
  10486. case GGML_TYPE_Q6_K:
  10487. 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);
  10488. break;
  10489. default:
  10490. GGML_ASSERT(false);
  10491. break;
  10492. }
  10493. (void) src1;
  10494. (void) dst;
  10495. (void) src1_ddf_i;
  10496. }
  10497. catch (sycl::exception const &exc) {
  10498. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10499. << ", line:" << __LINE__ << std::endl;
  10500. std::exit(1);
  10501. }
  10502. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split) {
  10503. int64_t min_compute_capability = INT_MAX;
  10504. int64_t max_compute_capability = INT_MIN;
  10505. for (int i = 0; i < g_device_count; ++i) {
  10506. if (tensor_split[i] < (i + 1 < g_device_count ? tensor_split[i + 1] : 1.0f)) {
  10507. if (min_compute_capability > g_device_caps[i].cc) {
  10508. min_compute_capability = g_device_caps[i].cc;
  10509. }
  10510. if (max_compute_capability < g_device_caps[i].cc) {
  10511. max_compute_capability = g_device_caps[i].cc;
  10512. }
  10513. }
  10514. }
  10515. switch(type) {
  10516. case GGML_TYPE_Q4_0:
  10517. case GGML_TYPE_Q4_1:
  10518. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  10519. case GGML_TYPE_Q5_0:
  10520. case GGML_TYPE_Q5_1:
  10521. case GGML_TYPE_Q8_0:
  10522. return 64;
  10523. case GGML_TYPE_F16:
  10524. case GGML_TYPE_F32:
  10525. return 1;
  10526. case GGML_TYPE_Q2_K:
  10527. case GGML_TYPE_Q3_K:
  10528. case GGML_TYPE_Q4_K:
  10529. case GGML_TYPE_Q5_K:
  10530. case GGML_TYPE_IQ2_XXS:
  10531. case GGML_TYPE_IQ2_XS:
  10532. case GGML_TYPE_IQ3_XXS:
  10533. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  10534. case GGML_TYPE_Q6_K:
  10535. return 64;
  10536. default:
  10537. GGML_ASSERT(false);
  10538. }
  10539. }
  10540. inline void ggml_sycl_op_mul_mat_vec_q(
  10541. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  10542. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  10543. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  10544. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  10545. const dpct::queue_ptr &stream) {
  10546. GGML_ASSERT(ggml_nrows(src1) == 1);
  10547. const int64_t ne00 = src0->ne[0];
  10548. const int64_t row_diff = row_high - row_low;
  10549. // TODO: support these quantization types
  10550. GGML_ASSERT(!(src0->type == GGML_TYPE_IQ2_XXS ||
  10551. src0->type == GGML_TYPE_IQ2_XS ||
  10552. src0->type == GGML_TYPE_IQ3_XXS ||
  10553. src0->type == GGML_TYPE_IQ1_S));
  10554. switch (src0->type) {
  10555. case GGML_TYPE_Q4_0:
  10556. mul_mat_vec_q_sycl_submitter<QK4_0, QI4_0, block_q4_0,
  10557. VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>(
  10558. src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10559. break;
  10560. case GGML_TYPE_Q4_1:
  10561. mul_mat_vec_q_sycl_submitter<QK4_1, QI4_1, block_q4_1,
  10562. VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>(
  10563. src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10564. break;
  10565. case GGML_TYPE_Q5_0:
  10566. mul_mat_vec_q_sycl_submitter<QK5_0, QI5_0, block_q5_0,
  10567. VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>(
  10568. src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10569. break;
  10570. case GGML_TYPE_Q5_1:
  10571. mul_mat_vec_q_sycl_submitter<QK5_1, QI5_1, block_q5_1,
  10572. VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>(
  10573. src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10574. break;
  10575. case GGML_TYPE_Q8_0:
  10576. mul_mat_vec_q_sycl_submitter<QK8_0, QI8_0, block_q8_0,
  10577. VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>(
  10578. src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10579. break;
  10580. case GGML_TYPE_Q2_K:
  10581. mul_mat_vec_q_sycl_submitter<QK_K, QI2_K, block_q2_K,
  10582. VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>(
  10583. src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10584. break;
  10585. case GGML_TYPE_Q3_K:
  10586. mul_mat_vec_q_sycl_submitter<QK_K, QI3_K, block_q3_K,
  10587. VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>(
  10588. src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10589. break;
  10590. case GGML_TYPE_Q4_K:
  10591. mul_mat_vec_q_sycl_submitter<QK_K, QI4_K, block_q4_K,
  10592. VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>(
  10593. src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10594. break;
  10595. case GGML_TYPE_Q5_K:
  10596. mul_mat_vec_q_sycl_submitter<QK_K, QI5_K, block_q5_K,
  10597. VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>(
  10598. src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10599. break;
  10600. case GGML_TYPE_Q6_K:
  10601. mul_mat_vec_q_sycl_submitter<QK_K, QI6_K, block_q6_K,
  10602. VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>(
  10603. src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  10604. break;
  10605. default:
  10606. GGML_ASSERT(false);
  10607. break;
  10608. }
  10609. (void) src1;
  10610. (void) dst;
  10611. (void) src1_ddf_i;
  10612. (void) src1_ncols;
  10613. (void) src1_padded_row_size;
  10614. }
  10615. inline void ggml_sycl_op_dequantize_mul_mat_vec(
  10616. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  10617. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  10618. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  10619. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  10620. const dpct::queue_ptr &stream) {
  10621. GGML_TENSOR_BINARY_OP_LOCALS;
  10622. const int64_t row_diff = row_high - row_low;
  10623. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  10624. #ifdef GGML_SYCL_F16
  10625. sycl_pool_alloc<sycl::half> src1_dfloat_a;
  10626. sycl::half *src1_dfloat = nullptr; // dfloat == half
  10627. bool src1_convert_f16 =
  10628. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  10629. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  10630. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  10631. if (src1_convert_f16) {
  10632. if (src1->type == GGML_TYPE_F16) {
  10633. src1_dfloat = (sycl::half *)src1->data + src1_padded_row_size;
  10634. } else {
  10635. src1_dfloat = src1_dfloat_a.alloc(ne00);
  10636. ggml_cpy_f32_f16_sycl((const char *)src1_ddf_i, (char *)src1_dfloat,
  10637. ne00, ne00, ne01, ne02, nb00, nb01, nb02,
  10638. nb03, ne10, ne11, ne12, nb10, nb11, nb12,
  10639. nb13, stream);
  10640. }
  10641. }
  10642. #else
  10643. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  10644. #endif // GGML_SYCL_F16
  10645. switch (src0->type) {
  10646. case GGML_TYPE_Q4_0:
  10647. dequantize_mul_mat_vec_q4_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10648. break;
  10649. case GGML_TYPE_Q4_1:
  10650. dequantize_mul_mat_vec_q4_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10651. break;
  10652. case GGML_TYPE_Q5_0:
  10653. dequantize_mul_mat_vec_q5_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10654. break;
  10655. case GGML_TYPE_Q5_1:
  10656. dequantize_mul_mat_vec_q5_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10657. break;
  10658. case GGML_TYPE_Q8_0:
  10659. dequantize_mul_mat_vec_q8_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10660. break;
  10661. case GGML_TYPE_Q2_K:
  10662. dequantize_mul_mat_vec_q2_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10663. break;
  10664. case GGML_TYPE_Q3_K:
  10665. dequantize_mul_mat_vec_q3_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10666. break;
  10667. case GGML_TYPE_Q4_K:
  10668. dequantize_mul_mat_vec_q4_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10669. break;
  10670. case GGML_TYPE_Q5_K:
  10671. dequantize_mul_mat_vec_q5_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10672. break;
  10673. case GGML_TYPE_Q6_K:
  10674. dequantize_mul_mat_vec_q6_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  10675. break;
  10676. case GGML_TYPE_F16:
  10677. convert_mul_mat_vec_f16_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  10678. break;
  10679. default:
  10680. GGML_ASSERT(false);
  10681. break;
  10682. }
  10683. (void) src1;
  10684. (void) dst;
  10685. (void) src1_ddq_i;
  10686. (void) src1_ncols;
  10687. (void) src1_padded_row_size;
  10688. }
  10689. inline void ggml_sycl_op_mul_mat_sycl(
  10690. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  10691. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  10692. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  10693. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  10694. const dpct::queue_ptr &stream) try {
  10695. GGML_ASSERT(src0_dd_i != nullptr);
  10696. GGML_ASSERT(src1_ddf_i != nullptr);
  10697. GGML_ASSERT(dst_dd_i != nullptr);
  10698. const int64_t ne00 = src0->ne[0];
  10699. const int64_t ne10 = src1->ne[0];
  10700. const int64_t ne0 = dst->ne[0];
  10701. const int64_t row_diff = row_high - row_low;
  10702. int id;
  10703. SYCL_CHECK(
  10704. CHECK_TRY_ERROR(id = get_current_device_id()));
  10705. // the main device has a larger memory buffer to hold the results from all GPUs
  10706. // ldc == nrows of the matrix that cuBLAS writes into
  10707. int ldc = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  10708. #ifdef GGML_SYCL_F16
  10709. bool use_fp16 = true; // TODO(Yu) SYCL capability check
  10710. #else
  10711. bool use_fp16 = false;
  10712. #endif
  10713. if ((src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  10714. use_fp16 && ggml_is_contiguous(src0) && row_diff == src0->ne[1] &&
  10715. dst->op_params[0] == GGML_PREC_DEFAULT) {
  10716. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp16 path\n");
  10717. sycl_pool_alloc<sycl::half> src0_as_f16;
  10718. if (src0->type != GGML_TYPE_F16) {
  10719. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src0->type);
  10720. GGML_ASSERT(to_fp16_sycl != nullptr);
  10721. size_t ne = row_diff*ne00;
  10722. src0_as_f16.alloc(ne);
  10723. to_fp16_sycl(src0_dd_i, src0_as_f16.get(), ne, stream);
  10724. }
  10725. const sycl::half *src0_ptr = src0->type == GGML_TYPE_F16
  10726. ? (const sycl::half *)src0_dd_i
  10727. : src0_as_f16.get();
  10728. sycl_pool_alloc<sycl::half> src1_as_f16;
  10729. if (src1->type != GGML_TYPE_F16) {
  10730. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  10731. GGML_ASSERT(to_fp16_sycl != nullptr);
  10732. size_t ne = src1_ncols*ne10;
  10733. src1_as_f16.alloc(ne);
  10734. to_fp16_sycl(src1_ddf_i, src1_as_f16.get(), ne, stream);
  10735. }
  10736. const sycl::half *src1_ptr = src1->type == GGML_TYPE_F16
  10737. ? (const sycl::half *)src1->data + src1_padded_row_size
  10738. : src1_as_f16.get();
  10739. sycl_pool_alloc<sycl::half> dst_f16(row_diff * src1_ncols);
  10740. const sycl::half alpha_f16 = 1.0f;
  10741. const sycl::half beta_f16 = 0.0f;
  10742. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  10743. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
  10744. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  10745. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  10746. &alpha_f16, src0_ptr, dpct::library_data_t::real_half, ne00,
  10747. src1_ptr, dpct::library_data_t::real_half, ne10, &beta_f16,
  10748. dst_f16.get(), dpct::library_data_t::real_half, ldc,
  10749. dpct::library_data_t::real_half)));
  10750. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  10751. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  10752. }
  10753. else {
  10754. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
  10755. sycl_pool_alloc<float> src0_ddq_as_f32;
  10756. sycl_pool_alloc<float> src1_ddq_as_f32;
  10757. if (src0->type != GGML_TYPE_F32) {
  10758. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src0->type);
  10759. GGML_ASSERT(to_fp32_sycl != nullptr);
  10760. src0_ddq_as_f32.alloc(row_diff*ne00);
  10761. to_fp32_sycl(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  10762. }
  10763. if (src1->type != GGML_TYPE_F32) {
  10764. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src1->type);
  10765. GGML_ASSERT(to_fp32_sycl != nullptr);
  10766. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  10767. to_fp32_sycl(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  10768. }
  10769. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  10770. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  10771. const float alpha = 1.0f;
  10772. const float beta = 0.0f;
  10773. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  10774. SYCL_CHECK(CHECK_TRY_ERROR(oneapi::mkl::blas::column_major::gemm(
  10775. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  10776. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  10777. dpct::get_value(&alpha, *g_sycl_handles[id]), src0_ddf_i, ne00,
  10778. src1_ddf_i, ne10, dpct::get_value(&beta, *g_sycl_handles[id]),
  10779. dst_dd_i, ldc)));
  10780. }
  10781. (void) dst;
  10782. (void) src1_ddq_i;
  10783. (void) src1_padded_row_size;
  10784. }
  10785. catch (sycl::exception const &exc) {
  10786. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10787. << ", line:" << __LINE__ << std::endl;
  10788. std::exit(1);
  10789. }
  10790. inline void ggml_sycl_op_rope(const ggml_tensor *src0, const ggml_tensor *src1,
  10791. ggml_tensor *dst, const float *src0_dd,
  10792. const float *src1_dd, float *dst_dd,
  10793. const dpct::queue_ptr &main_stream) {
  10794. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  10795. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  10796. GGML_ASSERT(src0->type == dst->type);
  10797. const int64_t ne00 = src0->ne[0];
  10798. const int64_t ne01 = src0->ne[1];
  10799. const int64_t ne2 = dst->ne[2];
  10800. const int64_t nrows = ggml_nrows(src0);
  10801. //const int n_past = ((int32_t *) dst->op_params)[0];
  10802. const int n_dims = ((int32_t *) dst->op_params)[1];
  10803. const int mode = ((int32_t *) dst->op_params)[2];
  10804. const int n_ctx = ((int32_t *) dst->op_params)[3];
  10805. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  10806. // RoPE alteration for extended context
  10807. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  10808. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  10809. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  10810. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  10811. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  10812. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  10813. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  10814. const int32_t * pos = nullptr;
  10815. if ((mode & 1) == 0) {
  10816. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  10817. GGML_ASSERT(src1->ne[0] == ne2);
  10818. pos = (const int32_t *) src1_dd;
  10819. }
  10820. const bool is_neox = mode & 2;
  10821. const bool is_glm = mode & 4;
  10822. rope_corr_dims corr_dims;
  10823. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  10824. // compute
  10825. if (is_glm) {
  10826. GGML_ASSERT(false);
  10827. rope_glm_f32_sycl(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  10828. } else if (is_neox) {
  10829. if (src0->type == GGML_TYPE_F32) {
  10830. rope_neox_sycl(
  10831. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  10832. attn_factor, corr_dims, main_stream
  10833. );
  10834. } else if (src0->type == GGML_TYPE_F16) {
  10835. rope_neox_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd,
  10836. ne00, n_dims, nrows, pos, freq_scale, ne01,
  10837. freq_base, ext_factor, attn_factor, corr_dims,
  10838. main_stream);
  10839. } else {
  10840. GGML_ASSERT(false);
  10841. }
  10842. } else {
  10843. if (src0->type == GGML_TYPE_F32) {
  10844. rope_sycl(
  10845. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  10846. attn_factor, corr_dims, main_stream
  10847. );
  10848. } else if (src0->type == GGML_TYPE_F16) {
  10849. rope_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd, ne00,
  10850. nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  10851. attn_factor, corr_dims, main_stream);
  10852. } else {
  10853. GGML_ASSERT(false);
  10854. }
  10855. }
  10856. (void) src1;
  10857. (void) dst;
  10858. (void) src1_dd;
  10859. }
  10860. inline void ggml_sycl_op_alibi(const ggml_tensor *src0, const ggml_tensor *src1,
  10861. ggml_tensor *dst, const float *src0_dd,
  10862. const float *src1_dd, float *dst_dd,
  10863. const dpct::queue_ptr &main_stream) {
  10864. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10865. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10866. GGML_TENSOR_LOCALS_3(int64_t, ne0, src0, ne);
  10867. const int64_t nrows = ggml_nrows(src0);
  10868. //const int n_past = ((int32_t *) dst->op_params)[0];
  10869. const int n_head = ((int32_t *) dst->op_params)[1];
  10870. float max_bias;
  10871. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  10872. //GGML_ASSERT(ne01 + n_past == ne00);
  10873. GGML_ASSERT(n_head == ne02);
  10874. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  10875. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  10876. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  10877. alibi_f32_sycl(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  10878. (void) src1;
  10879. (void) src1_dd;
  10880. }
  10881. static void ggml_sycl_op_pool2d(const ggml_tensor *src0,
  10882. const ggml_tensor *src1, ggml_tensor *dst,
  10883. const float *src0_dd, const float *src1_dd,
  10884. float *dst_dd, const dpct::queue_ptr &main_stream) {
  10885. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10886. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10887. const int32_t * opts = (const int32_t *)dst->op_params;
  10888. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  10889. const int k0 = opts[1];
  10890. const int k1 = opts[2];
  10891. const int s0 = opts[3];
  10892. const int s1 = opts[4];
  10893. const int p0 = opts[5];
  10894. const int p1 = opts[6];
  10895. const int64_t IH = src0->ne[1];
  10896. const int64_t IW = src0->ne[0];
  10897. const int64_t N = dst->ne[3];
  10898. const int64_t OC = dst->ne[2];
  10899. const int64_t OH = dst->ne[1];
  10900. const int64_t OW = dst->ne[0];
  10901. const int parallel_elements = N * OC * OH * OW;
  10902. const int num_blocks = (parallel_elements + SYCL_POOL2D_BLOCK_SIZE - 1) / SYCL_POOL2D_BLOCK_SIZE;
  10903. sycl::range<3> block_nums(1, 1, num_blocks);
  10904. main_stream->parallel_for(
  10905. sycl::nd_range<3>(block_nums *
  10906. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  10907. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  10908. [=](sycl::nd_item<3> item_ct1) {
  10909. pool2d_nchw_kernel(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0,
  10910. parallel_elements, src0_dd, dst_dd, op,
  10911. item_ct1);
  10912. });
  10913. (void) src1;
  10914. (void) src1_dd;
  10915. }
  10916. inline void ggml_sycl_op_im2col(const ggml_tensor *src0,
  10917. const ggml_tensor *src1, ggml_tensor *dst,
  10918. const float *src0_dd, const float *src1_dd,
  10919. float *dst_dd,
  10920. const dpct::queue_ptr &main_stream) {
  10921. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  10922. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10923. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  10924. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  10925. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  10926. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  10927. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  10928. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  10929. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  10930. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  10931. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  10932. const int64_t IH = is_2D ? src1->ne[1] : 1;
  10933. const int64_t IW = src1->ne[0];
  10934. const int64_t KH = is_2D ? src0->ne[1] : 1;
  10935. const int64_t KW = src0->ne[0];
  10936. const int64_t OH = is_2D ? dst->ne[2] : 1;
  10937. const int64_t OW = dst->ne[1];
  10938. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  10939. if (dst->type == GGML_TYPE_F16) {
  10940. im2col_sycl(src1_dd, (sycl::half *)dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  10941. } else {
  10942. im2col_sycl(src1_dd, (float *)dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  10943. }
  10944. (void) src0;
  10945. (void) src0_dd;
  10946. }
  10947. inline void ggml_sycl_op_sum_rows(const ggml_tensor *src0,
  10948. const ggml_tensor *src1, ggml_tensor *dst,
  10949. const float *src0_dd, const float *src1_dd,
  10950. float *dst_dd,
  10951. const dpct::queue_ptr &main_stream) {
  10952. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10953. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10954. const int64_t ncols = src0->ne[0];
  10955. const int64_t nrows = ggml_nrows(src0);
  10956. sum_rows_f32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
  10957. (void) src1;
  10958. (void) dst;
  10959. (void) src1_dd;
  10960. }
  10961. inline void ggml_sycl_op_argsort(const ggml_tensor *src0,
  10962. const ggml_tensor *src1, ggml_tensor *dst,
  10963. const float *src0_dd, const float *src1_dd,
  10964. float *dst_dd,
  10965. const dpct::queue_ptr &main_stream) {
  10966. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10967. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  10968. const int64_t ncols = src0->ne[0];
  10969. const int64_t nrows = ggml_nrows(src0);
  10970. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  10971. argsort_f32_i32_sycl(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  10972. (void) src1;
  10973. (void) dst;
  10974. (void) src1_dd;
  10975. }
  10976. inline void ggml_sycl_op_diag_mask_inf(const ggml_tensor *src0,
  10977. const ggml_tensor *src1,
  10978. ggml_tensor *dst, const float *src0_dd,
  10979. const float *src1_dd, float *dst_dd,
  10980. const dpct::queue_ptr &main_stream) {
  10981. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10982. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10983. const int64_t ne00 = src0->ne[0];
  10984. const int64_t ne01 = src0->ne[1];
  10985. const int nrows0 = ggml_nrows(src0);
  10986. const int n_past = ((int32_t *) dst->op_params)[0];
  10987. diag_mask_inf_f32_sycl(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  10988. (void) src1;
  10989. (void) dst;
  10990. (void) src1_dd;
  10991. }
  10992. inline void ggml_sycl_op_soft_max(const ggml_tensor *src0,
  10993. const ggml_tensor *src1, ggml_tensor *dst,
  10994. const float *src0_dd, const float *src1_dd,
  10995. float *dst_dd,
  10996. const dpct::queue_ptr &main_stream) {
  10997. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10998. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  10999. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  11000. const int64_t ne00 = src0->ne[0];
  11001. const int64_t nrows_x = ggml_nrows(src0);
  11002. const int64_t nrows_y = src0->ne[1];
  11003. float scale = 1.0f;
  11004. float max_bias = 0.0f;
  11005. memcpy(&scale, dst->op_params + 0, sizeof(float));
  11006. memcpy(&max_bias, dst->op_params + 1, sizeof(float));
  11007. // positions tensor
  11008. float * src2_dd = nullptr;
  11009. sycl_pool_alloc<float> src2_f;
  11010. ggml_tensor * src2 = dst->src[2];
  11011. const bool use_src2 = src2 != nullptr;
  11012. if (use_src2) {
  11013. const bool src2_on_device = src2->backend == GGML_BACKEND_TYPE_GPU;
  11014. if (src2_on_device) {
  11015. ggml_tensor_extra_gpu * src2_extra = (ggml_tensor_extra_gpu *) src2->extra;
  11016. src2_dd = (float *) src2_extra->data_device[g_main_device];
  11017. } else {
  11018. src2_dd = src2_f.alloc(ggml_nelements(src2));
  11019. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src2_dd, src2, 0, 0, 0, 1, main_stream));
  11020. }
  11021. }
  11022. soft_max_f32_sycl(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00,
  11023. nrows_x, nrows_y, scale, max_bias, main_stream);
  11024. }
  11025. inline void ggml_sycl_op_scale(const ggml_tensor *src0, const ggml_tensor *src1,
  11026. ggml_tensor *dst, const float *src0_dd,
  11027. const float *src1_dd, float *dst_dd,
  11028. const dpct::queue_ptr &main_stream) {
  11029. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11030. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11031. float scale;
  11032. memcpy(&scale, dst->op_params, sizeof(float));
  11033. scale_f32_sycl(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  11034. /*
  11035. DPCT1010:87: SYCL uses exceptions to report errors and does not use the
  11036. error codes. The call was replaced with 0. You need to rewrite this code.
  11037. */
  11038. SYCL_CHECK(0);
  11039. (void) src1;
  11040. (void) dst;
  11041. (void) src1_dd;
  11042. }
  11043. inline void ggml_sycl_op_clamp(const ggml_tensor *src0, const ggml_tensor *src1,
  11044. ggml_tensor *dst, const float *src0_dd,
  11045. const float *src1_dd, float *dst_dd,
  11046. const dpct::queue_ptr &main_stream) {
  11047. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11048. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11049. float min;
  11050. float max;
  11051. memcpy(&min, dst->op_params, sizeof(float));
  11052. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  11053. clamp_f32_sycl(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  11054. /*
  11055. DPCT1010:88: SYCL uses exceptions to report errors and does not use the
  11056. error codes. The call was replaced with 0. You need to rewrite this code.
  11057. */
  11058. SYCL_CHECK(0);
  11059. (void) src1;
  11060. (void) dst;
  11061. (void) src1_dd;
  11062. }
  11063. static void ggml_sycl_op_flatten(const ggml_tensor *src0,
  11064. const ggml_tensor *src1, ggml_tensor *dst,
  11065. const ggml_sycl_op_flatten_t op) try {
  11066. const int64_t nrows0 = ggml_nrows(src0);
  11067. const bool use_src1 = src1 != nullptr;
  11068. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  11069. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  11070. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  11071. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11072. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  11073. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11074. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  11075. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_TYPE_GPU;
  11076. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  11077. // dd = data device
  11078. float * src0_ddf = nullptr;
  11079. float * src1_ddf = nullptr;
  11080. float * dst_ddf = nullptr;
  11081. sycl_pool_alloc<float> src0_f;
  11082. sycl_pool_alloc<float> src1_f;
  11083. sycl_pool_alloc<float> dst_f;
  11084. ggml_sycl_set_device(g_main_device);
  11085. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  11086. // GGML_SYCL_DEBUG("g_main_device=%d, main_stream=%p src0_on_device=%d, src1_on_device=%d, dst_on_device=%d\n",
  11087. // g_main_device, main_stream, src0_on_device, src1_on_device, dst_on_device);
  11088. if (src0_on_device) {
  11089. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  11090. } else {
  11091. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  11092. // GGML_SYCL_DEBUG("before ggml_sycl_cpy_tensor_2d src0_ddf=%p, src0=%p\n", src0_ddf, src0);
  11093. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  11094. }
  11095. if (use_src1) {
  11096. if (src1_on_device) {
  11097. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  11098. } else {
  11099. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  11100. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  11101. }
  11102. }
  11103. if (dst_on_device) {
  11104. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  11105. } else {
  11106. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  11107. }
  11108. // GGML_SYCL_DEBUG("op src0=%p, src1=%p, dst=%p, src0_ddf=%p, src1_ddf=%p, dst_ddf=%p, main_stream=%p\n",
  11109. // src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  11110. // do the computation
  11111. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  11112. /*
  11113. DPCT1010:89: SYCL uses exceptions to report errors and does not use the
  11114. error codes. The call was replaced with 0. You need to rewrite this code.
  11115. */
  11116. SYCL_CHECK(0);
  11117. // copy dst to host if necessary
  11118. if (!dst_on_device) {
  11119. SYCL_CHECK(CHECK_TRY_ERROR(
  11120. main_stream->memcpy(dst->data, dst_ddf, ggml_nbytes(dst))));
  11121. }
  11122. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  11123. SYCL_CHECK(CHECK_TRY_ERROR(
  11124. dpct::get_current_device().queues_wait_and_throw()));
  11125. }
  11126. // print_ggml_tensor("tensor", dst);
  11127. }
  11128. catch (sycl::exception const &exc) {
  11129. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11130. << ", line:" << __LINE__ << std::endl;
  11131. std::exit(1);
  11132. }
  11133. static void ggml_sycl_set_peer_access(const int n_tokens) {
  11134. static bool peer_access_enabled = false;
  11135. const bool enable_peer_access = n_tokens <= GGML_SYCL_PEER_MAX_BATCH_SIZE;
  11136. if (peer_access_enabled == enable_peer_access) {
  11137. return;
  11138. }
  11139. #ifdef NDEBUG
  11140. for (int i = 0; i < g_device_count; ++i) {
  11141. SYCL_CHECK(ggml_sycl_set_device(i));
  11142. // SYCL_CHECK(syclDeviceSynchronize());
  11143. }
  11144. for (int i = 0; i < g_device_count; ++i) {
  11145. SYCL_CHECK(ggml_sycl_set_device(i));
  11146. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  11147. if (i == id_other) {
  11148. continue;
  11149. }
  11150. if (i != g_main_device && id_other != g_main_device) {
  11151. continue;
  11152. }
  11153. // int can_access_peer;
  11154. // SYCL_CHECK(syclDeviceCanAccessPeer(&can_access_peer, id, id_other));
  11155. // if (can_access_peer) {
  11156. // if (enable_peer_access) {
  11157. // SYCL_CHECK(syclDeviceEnablePeerAccess(id_other, 0));
  11158. // } else {
  11159. // SYCL_CHECK(syclDeviceDisablePeerAccess(id_other));
  11160. // }
  11161. // }
  11162. }
  11163. }
  11164. #endif // NDEBUG
  11165. peer_access_enabled = enable_peer_access;
  11166. }
  11167. struct ggml_backend_sycl_split_buffer_type_context {
  11168. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  11169. };
  11170. static void ggml_sycl_op_mul_mat(const ggml_tensor *src0,
  11171. const ggml_tensor *src1, ggml_tensor *dst,
  11172. ggml_sycl_op_mul_mat_t op,
  11173. const bool convert_src1_to_q8_1) try {
  11174. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne);
  11175. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  11176. const int64_t nrows1 = ggml_nrows(src1);
  11177. GGML_ASSERT(ne03 == ne13);
  11178. const int64_t ne0 = dst->ne[0];
  11179. const int64_t ne1 = dst->ne[1];
  11180. const int nb2 = dst->nb[2];
  11181. const int nb3 = dst->nb[3];
  11182. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  11183. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  11184. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  11185. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  11186. const int64_t i02_divisor = ne12 / ne02;
  11187. const size_t src0_ts = ggml_type_size(src0->type);
  11188. const size_t src0_bs = ggml_blck_size(src0->type);
  11189. const size_t q8_1_ts = sizeof(block_q8_1);
  11190. const size_t q8_1_bs = QK8_1;
  11191. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11192. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11193. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11194. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  11195. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  11196. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  11197. int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  11198. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  11199. GGML_ASSERT(!(split && ne02 > 1));
  11200. GGML_ASSERT(!(split && ne03 > 1));
  11201. GGML_ASSERT(!(split && ne02 < ne12));
  11202. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  11203. if (split) {
  11204. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  11205. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  11206. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
  11207. tensor_split = buft_ctx->tensor_split;
  11208. }
  11209. struct dev_data {
  11210. sycl_pool_alloc<char> src0_dd_alloc;
  11211. sycl_pool_alloc<float> src1_ddf_alloc;
  11212. sycl_pool_alloc<char> src1_ddq_alloc;
  11213. sycl_pool_alloc<float> dst_dd_alloc;
  11214. char *src0_dd = nullptr;
  11215. float *src1_ddf = nullptr; // float
  11216. char *src1_ddq = nullptr; // q8_1
  11217. float *dst_dd = nullptr;
  11218. int64_t row_low;
  11219. int64_t row_high;
  11220. };
  11221. dev_data dev[GGML_SYCL_MAX_DEVICES];
  11222. int used_devices = 0;
  11223. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  11224. for (int i = 0; i < g_device_count; ++i) {
  11225. // by default, use all rows
  11226. dev[i].row_low = 0;
  11227. dev[i].row_high = ne01;
  11228. // for multi GPU, get the row boundaries from tensor split
  11229. // and round to mul_mat_q tile sizes
  11230. if (split) {
  11231. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  11232. if (i != 0) {
  11233. dev[i].row_low = ne01*tensor_split[i];
  11234. if (dev[i].row_low < ne01) {
  11235. dev[i].row_low -= dev[i].row_low % rounding;
  11236. }
  11237. }
  11238. if (i != g_device_count - 1) {
  11239. dev[i].row_high = ne01*tensor_split[i + 1];
  11240. if (dev[i].row_high < ne01) {
  11241. dev[i].row_high -= dev[i].row_high % rounding;
  11242. }
  11243. }
  11244. }
  11245. }
  11246. for (int i = 0; i < g_device_count; ++i) {
  11247. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  11248. continue;
  11249. }
  11250. used_devices++;
  11251. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  11252. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  11253. ggml_sycl_set_device(i);
  11254. dpct::queue_ptr stream = g_syclStreams[i][0];
  11255. if (src0_on_device && src0_is_contiguous) {
  11256. dev[i].src0_dd = (char *) src0_extra->data_device[i];
  11257. } else {
  11258. dev[i].src0_dd = dev[i].src0_dd_alloc.alloc(ggml_nbytes(src0));
  11259. }
  11260. if (src1_on_device && src1_is_contiguous) {
  11261. dev[i].src1_ddf = (float *) src1_extra->data_device[i];
  11262. } else {
  11263. dev[i].src1_ddf = dev[i].src1_ddf_alloc.alloc(ggml_nelements(src1));
  11264. }
  11265. if (convert_src1_to_q8_1) {
  11266. dev[i].src1_ddq = dev[i].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  11267. if (src1_on_device && src1_is_contiguous) {
  11268. quantize_row_q8_1_sycl(dev[i].src1_ddf, dev[i].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  11269. /*
  11270. DPCT1010:90: SYCL uses exceptions to report errors and does not
  11271. use the error codes. The call was replaced with 0. You need to
  11272. rewrite this code.
  11273. */
  11274. SYCL_CHECK(0);
  11275. }
  11276. }
  11277. if (dst_on_device) {
  11278. dev[i].dst_dd = (float *) dst_extra->data_device[i];
  11279. } else {
  11280. const size_t size_dst_ddf = split ? (dev[i].row_high - dev[i].row_low)*ne1 : ggml_nelements(dst);
  11281. dev[i].dst_dd = dev[i].dst_dd_alloc.alloc(size_dst_ddf);
  11282. }
  11283. }
  11284. // if multiple devices are used they need to wait for the main device
  11285. // here an event is recorded that signals that the main device has finished calculating the input data
  11286. if (split && used_devices > 1) {
  11287. ggml_sycl_set_device(g_main_device);
  11288. /*
  11289. DPCT1024:91: The original code returned the error code that was further
  11290. consumed by the program logic. This original code was replaced with 0.
  11291. You may need to rewrite the program logic consuming the error code.
  11292. */
  11293. SYCL_CHECK(CHECK_TRY_ERROR(
  11294. *src0_extra->events[g_main_device][0] =
  11295. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier()));
  11296. }
  11297. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  11298. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  11299. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  11300. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  11301. for (int i = 0; i < g_device_count; ++i) {
  11302. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  11303. continue;
  11304. }
  11305. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  11306. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  11307. const int64_t row_diff = dev[i].row_high - dev[i].row_low;
  11308. ggml_sycl_set_device(i);
  11309. dpct::queue_ptr stream = g_syclStreams[i][is];
  11310. // wait for main GPU data if necessary
  11311. if (split && (i != g_main_device || is != 0)) {
  11312. /*
  11313. DPCT1009:163: SYCL uses exceptions to report errors and does not
  11314. use the error codes. The original code was commented out and a
  11315. warning string was inserted. You need to rewrite this code.
  11316. */
  11317. SYCL_CHECK(CHECK_TRY_ERROR(stream->ext_oneapi_submit_barrier(
  11318. {*src0_extra->events[g_main_device][0]})));
  11319. }
  11320. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  11321. const int64_t i03 = i0 / ne12;
  11322. const int64_t i02 = i0 % ne12;
  11323. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  11324. // for split tensors the data begins at i0 == i0_offset_low
  11325. char * src0_dd_i = dev[i].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  11326. float * src1_ddf_i = dev[i].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  11327. char * src1_ddq_i = dev[i].src1_ddq + src1_ddq_i_offset;
  11328. float * dst_dd_i = dev[i].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  11329. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  11330. // in that case an offset on dst_ddf_i is needed
  11331. if (dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device) {
  11332. dst_dd_i += dev[i].row_low; // offset is 0 if no tensor split
  11333. }
  11334. // copy src0, src1 to device if necessary
  11335. if (src1->backend == GGML_BACKEND_TYPE_GPU && src1_is_contiguous) {
  11336. if (i != g_main_device) {
  11337. if (convert_src1_to_q8_1) {
  11338. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  11339. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  11340. src1_ddq_i, src1_ddq_i_source,
  11341. src1_ncols * src1_padded_col_size * q8_1_ts /
  11342. q8_1_bs)));
  11343. } else {
  11344. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  11345. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  11346. SYCL_CHECK(CHECK_TRY_ERROR(dev2dev_memcpy(*stream, *main_stream,
  11347. src1_ddf_i, src1_ddf_i_source,
  11348. src1_ncols * ne10 * sizeof(float))));
  11349. }
  11350. }
  11351. } else if (src1->backend == GGML_BACKEND_TYPE_CPU || (src1_on_device && !src1_is_contiguous)) {
  11352. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(
  11353. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  11354. } else {
  11355. GGML_ASSERT(false);
  11356. }
  11357. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_TYPE_CPU || !src1_is_contiguous)) {
  11358. quantize_row_q8_1_sycl(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  11359. /*
  11360. DPCT1010:92: SYCL uses exceptions to report errors and does
  11361. not use the error codes. The call was replaced with 0. You
  11362. need to rewrite this code.
  11363. */
  11364. SYCL_CHECK(0);
  11365. }
  11366. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  11367. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[i].row_low, dev[i].row_high, stream));
  11368. }
  11369. if (src1->type == GGML_TYPE_F16) {
  11370. src1_padded_col_size = (i0 * ne11 + src1_col_0) * ne10;
  11371. }
  11372. // do the computation
  11373. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  11374. dev[i].row_low, dev[i].row_high, src1_ncols, src1_padded_col_size, stream);
  11375. /*
  11376. DPCT1010:93: SYCL uses exceptions to report errors and does not
  11377. use the error codes. The call was replaced with 0. You need to
  11378. rewrite this code.
  11379. */
  11380. SYCL_CHECK(0);
  11381. // copy dst to host or other device if necessary
  11382. if (!dst_on_device) {
  11383. void * dst_off_device;
  11384. dpct::memcpy_direction kind;
  11385. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  11386. dst_off_device = dst->data;
  11387. kind = dpct::device_to_host;
  11388. } else if (dst->backend == GGML_BACKEND_TYPE_GPU) {
  11389. dst_off_device = dst_extra->data_device[g_main_device];
  11390. kind = dpct::device_to_device;
  11391. } else {
  11392. GGML_ASSERT(false);
  11393. }
  11394. if (split) {
  11395. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  11396. // dst is NOT transposed.
  11397. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  11398. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  11399. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  11400. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  11401. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  11402. dhf_dst_i += src1_col_0*ne0 + dev[i].row_low;
  11403. //todo, dirty solution. Need be updated when device2device memcpy() is supported.
  11404. if (kind == dpct::device_to_device) {
  11405. size_t dst_size = ggml_nbytes_pad(dst);
  11406. float *host_buf = (float *)malloc(dst_size);
  11407. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  11408. host_buf, ne0 * sizeof(float), dst_dd_i,
  11409. row_diff * sizeof(float), row_diff * sizeof(float),
  11410. src1_ncols, dpct::device_to_host, *stream)));
  11411. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[i]).queues_wait_and_throw();
  11412. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  11413. dhf_dst_i, ne0 * sizeof(float), host_buf,
  11414. row_diff * sizeof(float), row_diff * sizeof(float),
  11415. src1_ncols, dpct::host_to_device, *main_stream)));
  11416. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[g_main_device]).queues_wait_and_throw();
  11417. free(host_buf);
  11418. } else {
  11419. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  11420. dhf_dst_i, ne0 * sizeof(float), dst_dd_i,
  11421. row_diff * sizeof(float), row_diff * sizeof(float),
  11422. src1_ncols, kind, *stream)));
  11423. }
  11424. } else {
  11425. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  11426. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  11427. dhf_dst_i += src1_col_0*ne0;
  11428. SYCL_CHECK(CHECK_TRY_ERROR(
  11429. stream->memcpy(dhf_dst_i, dst_dd_i,
  11430. src1_ncols * ne0 * sizeof(float))));
  11431. }
  11432. }
  11433. // add event for the main device to wait on until other device is done
  11434. if (split && (i != g_main_device || is != 0)) {
  11435. /*
  11436. DPCT1024:94: The original code returned the error code that
  11437. was further consumed by the program logic. This original
  11438. code was replaced with 0. You may need to rewrite the
  11439. program logic consuming the error code.
  11440. */
  11441. SYCL_CHECK(CHECK_TRY_ERROR(
  11442. *src0_extra->events[i][is] =
  11443. stream->ext_oneapi_submit_barrier()));
  11444. }
  11445. }
  11446. }
  11447. }
  11448. // main device waits for all other devices to be finished
  11449. if (split && g_device_count > 1) {
  11450. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  11451. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  11452. ggml_sycl_set_device(g_main_device);
  11453. for (int i = 0; i < g_device_count; ++i) {
  11454. if (dev[i].row_low == dev[i].row_high) {
  11455. continue;
  11456. }
  11457. for (int64_t is = 0; is < is_max; ++is) {
  11458. SYCL_CHECK(CHECK_TRY_ERROR(
  11459. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier(
  11460. {*src0_extra->events[i][is]})));
  11461. }
  11462. }
  11463. }
  11464. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  11465. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11466. SYCL_CHECK(CHECK_TRY_ERROR(
  11467. dpct::get_current_device().queues_wait_and_throw()));
  11468. }
  11469. }
  11470. catch (sycl::exception const &exc) {
  11471. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11472. << ", line:" << __LINE__ << std::endl;
  11473. std::exit(1);
  11474. }
  11475. static void ggml_sycl_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11476. GGML_SYCL_DEBUG("call %s\n", __func__);
  11477. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_repeat);
  11478. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11479. }
  11480. static void ggml_sycl_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11481. GGML_SYCL_DEBUG("call %s\n", __func__);
  11482. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_get_rows);
  11483. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11484. }
  11485. static void ggml_sycl_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11486. GGML_SYCL_DEBUG("call %s\n", __func__);
  11487. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_add);
  11488. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11489. }
  11490. static void ggml_sycl_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11491. GGML_SYCL_DEBUG("call %s\n", __func__);
  11492. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_acc);
  11493. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11494. }
  11495. static void ggml_sycl_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11496. GGML_SYCL_DEBUG("call %s\n", __func__);
  11497. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_mul);
  11498. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11499. }
  11500. static void ggml_sycl_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11501. GGML_SYCL_DEBUG("call %s\n", __func__);
  11502. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_div);
  11503. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11504. }
  11505. static void ggml_sycl_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11506. GGML_SYCL_DEBUG("call %s\n", __func__);
  11507. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu);
  11508. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11509. }
  11510. static void ggml_sycl_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11511. GGML_SYCL_DEBUG("call %s\n", __func__);
  11512. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_silu);
  11513. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11514. }
  11515. static void ggml_sycl_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11516. GGML_SYCL_DEBUG("call %s\n", __func__);
  11517. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu_quick);
  11518. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11519. }
  11520. static void ggml_sycl_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11521. GGML_SYCL_DEBUG("call %s\n", __func__);
  11522. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_tanh);
  11523. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11524. }
  11525. static void ggml_sycl_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11526. GGML_SYCL_DEBUG("call %s\n", __func__);
  11527. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_relu);
  11528. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11529. }
  11530. static void ggml_sycl_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11531. GGML_SYCL_DEBUG("call %s\n", __func__);
  11532. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardsigmoid);
  11533. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11534. }
  11535. static void ggml_sycl_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11536. GGML_SYCL_DEBUG("call %s\n", __func__);
  11537. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardswish);
  11538. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11539. }
  11540. static void ggml_sycl_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11541. GGML_SYCL_DEBUG("call %s\n", __func__);
  11542. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_leaky_relu);
  11543. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11544. }
  11545. static void ggml_sycl_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11546. GGML_SYCL_DEBUG("call %s\n", __func__);
  11547. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sqr);
  11548. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11549. }
  11550. static void ggml_sycl_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11551. GGML_SYCL_DEBUG("call %s\n", __func__);
  11552. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_norm);
  11553. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11554. }
  11555. static void ggml_sycl_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11556. GGML_SYCL_DEBUG("call %s\n", __func__);
  11557. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_group_norm);
  11558. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11559. }
  11560. static void ggml_sycl_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11561. GGML_SYCL_DEBUG("call %s\n", __func__);
  11562. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_concat);
  11563. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11564. }
  11565. static void ggml_sycl_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11566. GGML_SYCL_DEBUG("call %s\n", __func__);
  11567. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_upscale);
  11568. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11569. }
  11570. static void ggml_sycl_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11571. GGML_SYCL_DEBUG("call %s\n", __func__);
  11572. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pad);
  11573. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11574. }
  11575. static void ggml_sycl_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11576. GGML_SYCL_DEBUG("call %s\n", __func__);
  11577. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rms_norm);
  11578. GGML_SYCL_DEBUG("call %s done\n", __func__);
  11579. }
  11580. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  11581. if (!g_sycl_loaded) return false;
  11582. const int64_t ne10 = src1->ne[0];
  11583. const int64_t ne0 = dst->ne[0];
  11584. const int64_t ne1 = dst->ne[1];
  11585. // TODO: find the optimal values for these
  11586. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  11587. src1->type == GGML_TYPE_F32 &&
  11588. dst->type == GGML_TYPE_F32 &&
  11589. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  11590. }
  11591. static void ggml_sycl_mul_mat_vec_p021(const ggml_tensor *src0,
  11592. const ggml_tensor *src1,
  11593. ggml_tensor *dst) try {
  11594. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  11595. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  11596. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  11597. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  11598. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  11599. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11600. const int64_t ne00 = src0->ne[0];
  11601. const int64_t ne01 = src0->ne[1];
  11602. const int64_t ne02 = src0->ne[2];
  11603. const int64_t ne12 = src1->ne[2];
  11604. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11605. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  11606. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11607. void * src0_ddq = src0_extra->data_device[g_main_device];
  11608. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11609. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  11610. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11611. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  11612. ggml_mul_mat_p021_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  11613. }
  11614. catch (sycl::exception const &exc) {
  11615. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11616. << ", line:" << __LINE__ << std::endl;
  11617. std::exit(1);
  11618. }
  11619. static void ggml_sycl_mul_mat_vec_nc(const ggml_tensor *src0,
  11620. const ggml_tensor *src1,
  11621. ggml_tensor *dst) try {
  11622. GGML_ASSERT(!ggml_is_transposed(src0));
  11623. GGML_ASSERT(!ggml_is_transposed(src1));
  11624. GGML_ASSERT(!ggml_is_permuted(src0));
  11625. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  11626. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  11627. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11628. const int64_t ne00 = src0->ne[0];
  11629. const int64_t ne01 = src0->ne[1];
  11630. const int64_t ne02 = src0->ne[2];
  11631. const int64_t nb01 = src0->nb[1];
  11632. const int64_t nb02 = src0->nb[2];
  11633. const int64_t ne12 = src1->ne[2];
  11634. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11635. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  11636. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11637. void * src0_ddq = src0_extra->data_device[g_main_device];
  11638. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11639. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  11640. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11641. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  11642. const int64_t row_stride_x = nb01 / sizeof(sycl::half);
  11643. const int64_t channel_stride_x = nb02 / sizeof(sycl::half);
  11644. 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);
  11645. }
  11646. catch (sycl::exception const &exc) {
  11647. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11648. << ", line:" << __LINE__ << std::endl;
  11649. std::exit(1);
  11650. }
  11651. static void k_compute_batched_ptrs(const sycl::half *src0_as_f16,
  11652. const sycl::half *src1_as_f16, char *dst,
  11653. const void **ptrs_src, void **ptrs_dst,
  11654. int64_t ne12, int64_t ne13, int64_t ne23,
  11655. size_t nb02, size_t nb03, size_t nb12,
  11656. size_t nb13, size_t nbd2, size_t nbd3,
  11657. int64_t r2, int64_t r3,
  11658. const sycl::nd_item<3> &item_ct1) {
  11659. int64_t i13 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  11660. item_ct1.get_local_id(2);
  11661. int64_t i12 = item_ct1.get_group(1) * item_ct1.get_local_range(1) +
  11662. item_ct1.get_local_id(1);
  11663. if (i13 >= ne13 || i12 >= ne12) {
  11664. return;
  11665. }
  11666. int64_t i03 = i13 / r3;
  11667. int64_t i02 = i12 / r2;
  11668. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  11669. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  11670. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  11671. }
  11672. static void ggml_sycl_mul_mat_batched_sycl(const ggml_tensor *src0,
  11673. const ggml_tensor *src1,
  11674. ggml_tensor *dst) try {
  11675. GGML_ASSERT(!ggml_is_transposed(src0));
  11676. GGML_ASSERT(!ggml_is_transposed(src1));
  11677. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  11678. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  11679. GGML_TENSOR_BINARY_OP_LOCALS
  11680. const int64_t ne_dst = ggml_nelements(dst);
  11681. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11682. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  11683. SYCL_CHECK(
  11684. CHECK_TRY_ERROR(g_sycl_handles[g_main_device] = main_stream));
  11685. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11686. void * src0_ddq = src0_extra->data_device[g_main_device];
  11687. sycl::half *src0_as_f16 = (sycl::half *)src0_ddq;
  11688. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11689. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  11690. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11691. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  11692. // convert src1 to fp16
  11693. sycl_pool_alloc<sycl::half> src1_f16_alloc;
  11694. if (src1->type != GGML_TYPE_F16) {
  11695. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  11696. const int64_t ne_src1 = ggml_nelements(src1);
  11697. src1_f16_alloc.alloc(ne_src1);
  11698. GGML_ASSERT(to_fp16_sycl != nullptr);
  11699. to_fp16_sycl(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  11700. }
  11701. sycl::half *src1_f16 = src1->type == GGML_TYPE_F16 ? (sycl::half *)src1_ddf
  11702. : src1_f16_alloc.get();
  11703. sycl_pool_alloc<sycl::half> dst_f16;
  11704. char * dst_t;
  11705. dpct::library_data_t cu_compute_type = dpct::library_data_t::real_half;
  11706. dpct::library_data_t cu_data_type = dpct::library_data_t::real_half;
  11707. // dst strides
  11708. size_t nbd2 = dst->nb[2];
  11709. size_t nbd3 = dst->nb[3];
  11710. const sycl::half alpha_f16 = 1.0f;
  11711. const sycl::half beta_f16 = 0.0f;
  11712. const float alpha_f32 = 1.0f;
  11713. const float beta_f32 = 0.0f;
  11714. const void * alpha = &alpha_f16;
  11715. const void * beta = &beta_f16;
  11716. // TODO: Renable (dst->op_params[0] =! GGML_PREC_DEFAULT) pathway
  11717. // once oneMKL open source supports half, half, float, float: datatypes
  11718. dst_t = (char *) dst_f16.alloc(ne_dst);
  11719. nbd2 /= sizeof(float) / sizeof(sycl::half);
  11720. nbd3 /= sizeof(float) / sizeof(sycl::half);
  11721. GGML_ASSERT(ne12 % ne02 == 0);
  11722. GGML_ASSERT(ne13 % ne03 == 0);
  11723. // broadcast factors
  11724. const int64_t r2 = ne12/ne02;
  11725. const int64_t r3 = ne13/ne03;
  11726. #if 0
  11727. // use syclGemmEx
  11728. {
  11729. for (int i13 = 0; i13 < ne13; ++i13) {
  11730. for (int i12 = 0; i12 < ne12; ++i12) {
  11731. int i03 = i13 / r3;
  11732. int i02 = i12 / r2;
  11733. SYCL_CHECK(
  11734. syclGemmEx(g_sycl_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  11735. ne01, ne11, ne10,
  11736. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , SYCL_R_16F, nb01/sizeof(half),
  11737. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, SYCL_R_16F, nb11/sizeof(float),
  11738. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  11739. cu_compute_type,
  11740. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  11741. }
  11742. }
  11743. }
  11744. #else
  11745. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  11746. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  11747. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  11748. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  11749. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  11750. (const char *)src0_as_f16, dpct::library_data_t::real_half,
  11751. nb01 / nb00, nb02 / nb00,
  11752. (const char *)src1_f16, dpct::library_data_t::real_half,
  11753. nb11 / nb10, nb12 / nb10, beta,
  11754. (char *)dst_t, cu_data_type, ne01, nb2 / nb0,
  11755. ne12 * ne13, cu_compute_type)));
  11756. } else {
  11757. const int ne23 = ne12*ne13;
  11758. sycl_pool_alloc<const void *> ptrs_src(2*ne23);
  11759. sycl_pool_alloc< void *> ptrs_dst(1*ne23);
  11760. sycl::range<3> block_dims(1, ne12, ne13);
  11761. /*
  11762. DPCT1049:47: The work-group size passed to the SYCL kernel may exceed
  11763. the limit. To get the device limit, query
  11764. info::device::max_work_group_size. Adjust the work-group size if needed.
  11765. */
  11766. {
  11767. dpct::has_capability_or_fail(main_stream->get_device(),
  11768. {sycl::aspect::fp16});
  11769. main_stream->submit([&](sycl::handler &cgh) {
  11770. const void **ptrs_src_get = ptrs_src.get();
  11771. void **ptrs_dst_get = ptrs_dst.get();
  11772. size_t nb12_scaled = src1->type == GGML_TYPE_F16 ? nb12 : nb12 / 2;
  11773. size_t nb13_scaled = src1->type == GGML_TYPE_F16 ? nb13 : nb13 / 2;
  11774. cgh.parallel_for(sycl::nd_range<3>(block_dims, block_dims),
  11775. [=](sycl::nd_item<3> item_ct1) {
  11776. k_compute_batched_ptrs(
  11777. src0_as_f16, src1_f16,
  11778. dst_t, ptrs_src_get,
  11779. ptrs_dst_get, ne12, ne13, ne23,
  11780. nb02, nb03, nb12_scaled, nb13_scaled,
  11781. nbd2, nbd3, r2, r3, item_ct1);
  11782. });
  11783. });
  11784. }
  11785. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  11786. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  11787. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  11788. (const void **)(ptrs_src.get() + 0 * ne23),
  11789. dpct::library_data_t::real_half, nb01 / nb00,
  11790. (const void **)(ptrs_src.get() + 1 * ne23),
  11791. dpct::library_data_t::real_half, nb11 / nb10, beta,
  11792. (void **)(ptrs_dst.get() + 0 * ne23), cu_data_type, ne01, ne23,
  11793. cu_compute_type)));
  11794. }
  11795. #endif
  11796. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  11797. to_fp32_sycl(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  11798. }
  11799. catch (sycl::exception const &exc) {
  11800. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11801. << ", line:" << __LINE__ << std::endl;
  11802. std::exit(1);
  11803. }
  11804. static void ggml_sycl_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  11805. const bool all_on_device =
  11806. (src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT) &&
  11807. (src1->backend == GGML_BACKEND_TYPE_GPU) &&
  11808. ( dst->backend == GGML_BACKEND_TYPE_GPU);
  11809. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  11810. int64_t min_compute_capability = INT_MAX;
  11811. for (int i = 0; i < g_device_count; ++i) {
  11812. if (min_compute_capability > g_device_caps[i].cc && g_tensor_split[i] < (i + 1 < g_device_count ? g_tensor_split[i + 1] : 1.0f)) {
  11813. min_compute_capability = g_device_caps[i].cc;
  11814. }
  11815. }
  11816. #ifdef SYCL_USE_XMX
  11817. const bool use_xmx = true;
  11818. #else
  11819. const bool use_xmx = false;
  11820. #endif
  11821. // debug helpers
  11822. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  11823. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  11824. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  11825. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  11826. //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);
  11827. //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);
  11828. if (!split && all_on_device && !use_xmx && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  11829. // KQ single-batch
  11830. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_p021\n");
  11831. ggml_sycl_mul_mat_vec_p021(src0, src1, dst);
  11832. } 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) {
  11833. // KQV single-batch
  11834. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_nc\n");
  11835. ggml_sycl_mul_mat_vec_nc(src0, src1, dst);
  11836. } else if (!split && all_on_device && use_xmx && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  11837. // KQ + KQV multi-batch
  11838. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_batched_sycl\n");
  11839. ggml_sycl_mul_mat_batched_sycl(src0, src1, dst);
  11840. } else if (src0->type == GGML_TYPE_F32) {
  11841. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat\n");
  11842. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  11843. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  11844. // GGML_SYCL_DEBUG("ggml_is_quantized or GGML_TYPE_F16\n");
  11845. if (src1->ne[1] == 1 && src0->ne[0] % GGML_SYCL_DMMV_X == 0) {
  11846. #ifdef GGML_SYCL_FORCE_DMMV
  11847. const bool use_mul_mat_vec_q = false;
  11848. #else
  11849. const bool use_mul_mat_vec_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  11850. #endif // GGML_SYCL_FORCE_DMMV
  11851. if (use_mul_mat_vec_q) {
  11852. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  11853. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_vec_q path\n");
  11854. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_vec_q, true);
  11855. } else {
  11856. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_dequantize_mul_mat_vec path\n");
  11857. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_dequantize_mul_mat_vec, false);
  11858. }
  11859. } else {
  11860. bool use_mul_mat_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type);
  11861. if (use_xmx && min_compute_capability >= VER_GEN9 && src1->ne[1] > XMX_MAX_BATCH_SIZE) {
  11862. use_mul_mat_q = false;
  11863. }
  11864. if (use_mul_mat_q) {
  11865. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_q path\n");
  11866. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_q, true);
  11867. } else {
  11868. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_sycl path\n");
  11869. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  11870. }
  11871. }
  11872. } else {
  11873. GGML_ASSERT(false);
  11874. }
  11875. }
  11876. #if 0
  11877. template<typename ... Srcs>
  11878. static __global__ void k_compute_batched_ptrs_id(
  11879. const void ** ptrs_src, void ** ptrs_dst,
  11880. int ne12, int ne13,
  11881. int ne23,
  11882. int nb02, int nb03,
  11883. int nb12, int nb13,
  11884. int nb2, int nb3,
  11885. int r2, int r3,
  11886. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  11887. const half * src1_f16, half * dst_f16,
  11888. const int32_t * ids, const int id,
  11889. Srcs... src0s) {
  11890. int i = ids[id];
  11891. half * src0_f16;
  11892. const void * srcs_ar[] = { (const half *) src0s... };
  11893. if (src0_type == GGML_TYPE_F16) {
  11894. src0_f16 = (half *) srcs_ar[i];
  11895. } else {
  11896. src0_f16 = src0_as_f16;
  11897. if (threadIdx.x == 0 && threadIdx.y == 0) {
  11898. const to_fp16_sycl_t to_fp16 = ggml_get_to_fp16_sycl(src0_type);
  11899. to_fp16(srcs_ar[i], src0_f16, src0_ne, syclStreamFireAndForget);
  11900. }
  11901. }
  11902. int i13 = blockIdx.x * blockDim.x + threadIdx.x;
  11903. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  11904. if (i13 >= ne13 || i12 >= ne12) {
  11905. return;
  11906. }
  11907. int i03 = i13 / r3;
  11908. int i02 = i12 / r2;
  11909. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  11910. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  11911. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  11912. }
  11913. static void ggml_sycl_mul_mat_id_sycl(ggml_tensor * dst) {
  11914. const struct ggml_tensor * ids = dst->src[0];
  11915. const struct ggml_tensor * src1 = dst->src[1];
  11916. const struct ggml_tensor * src00 = dst->src[2];
  11917. const int id = dst->op_params[0];
  11918. GGML_ASSERT(!ggml_is_transposed(src00));
  11919. GGML_ASSERT(!ggml_is_transposed(src1));
  11920. GGML_ASSERT(src00->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  11921. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11922. GGML_TENSOR_LOCALS(int64_t, ne0, src00, ne);
  11923. //const int64_t nb01 = src00->nb[1];
  11924. GGML_TENSOR_LOCALS(int64_t, nb0, src00, nb);
  11925. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  11926. GGML_TENSOR_LOCALS(int64_t, nb1, src1, nb);
  11927. //const int64_t nb11 = src1->nb[1];
  11928. const int64_t ne1 = ggml_nelements(src1);
  11929. const int64_t ne = ggml_nelements(dst);
  11930. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  11931. syclStream_t main_stream = g_syclStreams[g_main_device][0];
  11932. SYCL_CHECK(syclSetStream(g_sycl_handles[g_main_device], main_stream));
  11933. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  11934. //void * src0_ddq = src0_extra->data_device[g_main_device];
  11935. //half * src0_as_f16 = (half *) src0_ddq;
  11936. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  11937. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  11938. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  11939. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  11940. // convert src1 to fp16
  11941. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  11942. GGML_ASSERT(to_fp16_sycl != nullptr);
  11943. size_t src1_as = 0;
  11944. half * src1_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne1 * sizeof(half), &src1_as);
  11945. to_fp16_sycl(src1_ddf, src1_as_f16, ne1, main_stream);
  11946. size_t dst_as = 0;
  11947. half * dst_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne * sizeof(half), &dst_as);
  11948. GGML_ASSERT(ne12 % ne02 == 0);
  11949. GGML_ASSERT(ne13 % ne03 == 0);
  11950. // broadcast factors
  11951. const int64_t r2 = ne12/ne02;
  11952. const int64_t r3 = ne13/ne03;
  11953. const half alpha_f16 = 1.0f;
  11954. const half beta_f16 = 0.0f;
  11955. // use syclGemmBatchedEx
  11956. const int ne23 = ne12*ne13;
  11957. const void ** ptrs_src = nullptr;
  11958. void ** ptrs_dst = nullptr;
  11959. size_t ptrs_src_s = 0;
  11960. size_t ptrs_dst_s = 0;
  11961. ptrs_src = (const void **) ggml_sycl_pool_malloc(g_main_device, 2*ne23*sizeof(void *), &ptrs_src_s);
  11962. ptrs_dst = ( void **) ggml_sycl_pool_malloc(g_main_device, 1*ne23*sizeof(void *), &ptrs_dst_s);
  11963. int64_t src0_ne = ggml_nelements(src00);
  11964. half * src0_as_f16 = nullptr;
  11965. size_t src0_as = 0;
  11966. if (src00->type != GGML_TYPE_F16) {
  11967. src0_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, src0_ne * sizeof(half), &src0_as);
  11968. }
  11969. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  11970. dim3 block_dims(ne13, ne12);
  11971. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  11972. ptrs_src, ptrs_dst,
  11973. ne12, ne13,
  11974. ne23,
  11975. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  11976. nb12, nb13,
  11977. dst->nb[2], dst->nb[3],
  11978. r2, r3,
  11979. src00->type, src0_as_f16, src0_ne,
  11980. src1_as_f16, dst_f16,
  11981. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  11982. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  11983. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  11984. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  11985. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  11986. );
  11987. SYCL_CHECK(syclGetLastError());
  11988. SYCL_CHECK(
  11989. syclGemmBatchedEx(g_sycl_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  11990. ne01, ne11, ne10,
  11991. &alpha_f16, (const void **) (ptrs_src + 0*ne23), SYCL_R_16F, ne00,
  11992. (const void **) (ptrs_src + 1*ne23), SYCL_R_16F, ne10,
  11993. &beta_f16, ( void **) (ptrs_dst + 0*ne23), SYCL_R_16F, ne01,
  11994. ne23,
  11995. CUBLAS_COMPUTE_16F,
  11996. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  11997. if (src0_as != 0) {
  11998. ggml_sycl_pool_free(g_main_device, src0_as_f16, src0_as);
  11999. }
  12000. if (ptrs_src_s != 0) {
  12001. ggml_sycl_pool_free(g_main_device, ptrs_src, ptrs_src_s);
  12002. }
  12003. if (ptrs_dst_s != 0) {
  12004. ggml_sycl_pool_free(g_main_device, ptrs_dst, ptrs_dst_s);
  12005. }
  12006. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  12007. to_fp32_sycl(dst_f16, dst_ddf, ne, main_stream);
  12008. ggml_sycl_pool_free(g_main_device, src1_as_f16, src1_as);
  12009. ggml_sycl_pool_free(g_main_device, dst_f16, dst_as);
  12010. }
  12011. #endif
  12012. static void ggml_sycl_mul_mat_id(const ggml_tensor *src0,
  12013. const ggml_tensor *src1,
  12014. ggml_tensor *dst) try {
  12015. #if 0
  12016. ggml_sycl_mul_mat_id_sycl(dst);
  12017. // TODO: mmq/mmv support
  12018. #endif
  12019. const int64_t nb11 = src1->nb[1];
  12020. const int64_t nb1 = dst->nb[1];
  12021. const struct ggml_tensor * ids = src0;
  12022. const int32_t id = ((int32_t *) dst->op_params)[0];
  12023. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  12024. std::vector<char> ids_host(ggml_nbytes(ids));
  12025. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  12026. if (ids->backend == GGML_BACKEND_TYPE_GPU) {
  12027. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  12028. SYCL_CHECK(CHECK_TRY_ERROR(
  12029. stream->memcpy(ids_host.data(), ids_dev, ggml_nbytes(ids))));
  12030. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  12031. } else {
  12032. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  12033. }
  12034. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  12035. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  12036. ggml_tensor_extra_gpu src1_row_extra;
  12037. ggml_tensor_extra_gpu dst_row_extra;
  12038. ggml_tensor src1_row = *src1;
  12039. ggml_tensor dst_row = *dst;
  12040. src1_row.backend = GGML_BACKEND_TYPE_GPU;
  12041. dst_row.backend = GGML_BACKEND_TYPE_GPU;
  12042. src1_row.extra = &src1_row_extra;
  12043. dst_row.extra = &dst_row_extra;
  12044. char * src1_original = src1->backend == GGML_BACKEND_TYPE_CPU ?
  12045. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  12046. char * dst_original = dst->backend == GGML_BACKEND_TYPE_CPU ?
  12047. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  12048. if (src1->ne[1] == 1) {
  12049. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  12050. GGML_ASSERT(dst->backend == GGML_BACKEND_TYPE_GPU);
  12051. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  12052. //int32_t row_id;
  12053. //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]));
  12054. //SYCL_CHECK(syclStreamSynchronize(g_syclStreams[g_main_device][0]));
  12055. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  12056. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  12057. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  12058. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  12059. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  12060. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  12061. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  12062. ggml_sycl_mul_mat(src0_row, &src1_row, &dst_row);
  12063. }
  12064. } else {
  12065. sycl_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  12066. sycl_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  12067. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  12068. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  12069. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  12070. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  12071. int64_t num_src1_rows = 0;
  12072. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  12073. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  12074. if (row_id_i != row_id) {
  12075. continue;
  12076. }
  12077. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  12078. SYCL_CHECK(CHECK_TRY_ERROR(
  12079. stream->memcpy(src1_contiguous.get() + num_src1_rows * nb11,
  12080. src1_original + i01 * nb11, nb11)));
  12081. num_src1_rows++;
  12082. }
  12083. if (num_src1_rows == 0) {
  12084. continue;
  12085. }
  12086. src1_row.ne[1] = num_src1_rows;
  12087. dst_row.ne[1] = num_src1_rows;
  12088. src1_row.nb[1] = nb11;
  12089. src1_row.nb[2] = num_src1_rows*nb11;
  12090. src1_row.nb[3] = num_src1_rows*nb11;
  12091. dst_row.nb[1] = nb1;
  12092. dst_row.nb[2] = num_src1_rows*nb1;
  12093. dst_row.nb[3] = num_src1_rows*nb1;
  12094. ggml_sycl_mul_mat(src0_row, &src1_row, &dst_row);
  12095. num_src1_rows = 0;
  12096. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  12097. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  12098. if (row_id_i != row_id) {
  12099. continue;
  12100. }
  12101. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  12102. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  12103. dst_original + i01 * nb1,
  12104. dst_contiguous.get() + num_src1_rows * nb1, nb1)));
  12105. num_src1_rows++;
  12106. }
  12107. }
  12108. }
  12109. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12110. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  12111. }
  12112. }
  12113. catch (sycl::exception const &exc) {
  12114. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12115. << ", line:" << __LINE__ << std::endl;
  12116. std::exit(1);
  12117. }
  12118. static void ggml_sycl_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12119. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_scale);
  12120. }
  12121. static void ggml_sycl_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12122. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_clamp);
  12123. }
  12124. static void ggml_sycl_cpy(const ggml_tensor *src0, const ggml_tensor *src1,
  12125. ggml_tensor *dst) try {
  12126. const int64_t ne = ggml_nelements(src0);
  12127. GGML_ASSERT(ne == ggml_nelements(src1));
  12128. GGML_ASSERT(src0->backend == GGML_BACKEND_TYPE_GPU);
  12129. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  12130. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  12131. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  12132. GGML_TENSOR_BINARY_OP_LOCALS;
  12133. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12134. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12135. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12136. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12137. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  12138. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  12139. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  12140. ggml_cpy_f32_f32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  12141. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  12142. ggml_cpy_f32_f16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  12143. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  12144. ggml_cpy_f32_q8_0_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  12145. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  12146. ggml_cpy_f32_q4_0_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  12147. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  12148. ggml_cpy_f32_q4_1_sycl(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  12149. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  12150. ggml_cpy_f16_f32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  12151. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  12152. ggml_cpy_f16_f16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  12153. } else if (src0->type == GGML_TYPE_I16 && src1->type == GGML_TYPE_I16) {
  12154. ggml_cpy_i16_i16_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  12155. } else if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32) {
  12156. ggml_cpy_i32_i32_sycl (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
  12157. } else {
  12158. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  12159. ggml_type_name(src0->type), ggml_type_name(src1->type));
  12160. GGML_ASSERT(false);
  12161. }
  12162. (void) dst;
  12163. }
  12164. catch (sycl::exception const &exc) {
  12165. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12166. << ", line:" << __LINE__ << std::endl;
  12167. std::exit(1);
  12168. }
  12169. static void ggml_sycl_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12170. // TODO: why do we pass dst as src1 here?
  12171. ggml_sycl_cpy(src0, dst, nullptr);
  12172. (void) src1;
  12173. }
  12174. static void ggml_sycl_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12175. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_diag_mask_inf);
  12176. }
  12177. static void ggml_sycl_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12178. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_soft_max);
  12179. }
  12180. static void ggml_sycl_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12181. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  12182. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rope);
  12183. }
  12184. static void ggml_sycl_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12185. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_alibi);
  12186. }
  12187. static void ggml_sycl_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12188. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pool2d);
  12189. }
  12190. static void ggml_sycl_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12191. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_im2col);
  12192. }
  12193. static void ggml_sycl_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12194. GGML_ASSERT(ggml_is_contiguous(src0));
  12195. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sum_rows);
  12196. }
  12197. static void ggml_sycl_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12198. GGML_ASSERT(ggml_is_contiguous(src0));
  12199. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_argsort);
  12200. }
  12201. static void ggml_sycl_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12202. (void) src0;
  12203. (void) src1;
  12204. (void) dst;
  12205. }
  12206. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  12207. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  12208. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  12209. }
  12210. void ggml_sycl_free_data(struct ggml_tensor *tensor) try {
  12211. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_TYPE_GPU && tensor->backend != GGML_BACKEND_TYPE_GPU_SPLIT) ) {
  12212. return;
  12213. }
  12214. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  12215. for (int i = 0; i < g_device_count; ++i) {
  12216. const dpct::queue_ptr stream = g_syclStreams[i][0];
  12217. if (extra->data_device[i] != nullptr) {
  12218. SYCL_CHECK(ggml_sycl_set_device(i));
  12219. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(extra->data_device[i], *stream)));
  12220. }
  12221. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  12222. if (extra->events[i][is] != nullptr) {
  12223. SYCL_CHECK(ggml_sycl_set_device(i));
  12224. SYCL_CHECK(CHECK_TRY_ERROR(
  12225. dpct::destroy_event(extra->events[i][is])));
  12226. }
  12227. }
  12228. }
  12229. delete extra;
  12230. }
  12231. catch (sycl::exception const &exc) {
  12232. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12233. << ", line:" << __LINE__ << std::endl;
  12234. std::exit(1);
  12235. }
  12236. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  12237. static size_t g_temp_tensor_extra_index = 0;
  12238. static ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  12239. if (g_temp_tensor_extras == nullptr) {
  12240. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  12241. }
  12242. size_t alloc_index = g_temp_tensor_extra_index;
  12243. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  12244. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  12245. memset(extra, 0, sizeof(*extra));
  12246. return extra;
  12247. }
  12248. static void ggml_sycl_assign_buffers_impl(struct ggml_tensor *tensor,
  12249. bool scratch, bool force_inplace,
  12250. bool no_alloc) try {
  12251. if (scratch && g_scratch_size == 0) {
  12252. return;
  12253. }
  12254. tensor->backend = GGML_BACKEND_TYPE_GPU;
  12255. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_CPU) {
  12256. const ggml_op src0_op = tensor->src[0]->op;
  12257. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  12258. ggml_sycl_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  12259. }
  12260. }
  12261. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_TYPE_CPU) {
  12262. ggml_sycl_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  12263. }
  12264. if (scratch && no_alloc) {
  12265. return;
  12266. }
  12267. ggml_tensor_extra_gpu * extra;
  12268. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  12269. tensor->op == GGML_OP_VIEW ||
  12270. force_inplace;
  12271. const size_t size = ggml_nbytes(tensor);
  12272. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12273. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  12274. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT)) {
  12275. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  12276. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  12277. size_t offset = 0;
  12278. if (tensor->op == GGML_OP_VIEW) {
  12279. memcpy(&offset, tensor->op_params, sizeof(size_t));
  12280. }
  12281. extra = ggml_sycl_alloc_temp_tensor_extra();
  12282. extra->data_device[g_main_device] = src0_ddc + offset;
  12283. } else if (tensor->op == GGML_OP_CPY) {
  12284. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  12285. void * src1_ddv = src1_extra->data_device[g_main_device];
  12286. extra = ggml_sycl_alloc_temp_tensor_extra();
  12287. extra->data_device[g_main_device] = src1_ddv;
  12288. } else if (scratch) {
  12289. GGML_ASSERT(size <= g_scratch_size);
  12290. if (g_scratch_offset + size > g_scratch_size) {
  12291. g_scratch_offset = 0;
  12292. }
  12293. char * data = (char *) g_scratch_buffer;
  12294. if (data == nullptr) {
  12295. SYCL_CHECK(CHECK_TRY_ERROR(
  12296. data = (char *)sycl::malloc_device(
  12297. g_scratch_size, *stream)));
  12298. g_scratch_buffer = data;
  12299. }
  12300. extra = ggml_sycl_alloc_temp_tensor_extra();
  12301. extra->data_device[g_main_device] = data + g_scratch_offset;
  12302. g_scratch_offset += size;
  12303. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  12304. } else { // allocate new buffers outside of scratch
  12305. void * data;
  12306. SYCL_CHECK(CHECK_TRY_ERROR(data = (void *)sycl::malloc_device(
  12307. size, *stream)));
  12308. SYCL_CHECK(CHECK_TRY_ERROR(
  12309. (*stream).memset(data, 0, size).wait()));
  12310. extra = new ggml_tensor_extra_gpu;
  12311. memset(extra, 0, sizeof(*extra));
  12312. extra->data_device[g_main_device] = data;
  12313. }
  12314. tensor->extra = extra;
  12315. }
  12316. catch (sycl::exception const &exc) {
  12317. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12318. << ", line:" << __LINE__ << std::endl;
  12319. std::exit(1);
  12320. }
  12321. void ggml_sycl_copy_to_device(struct ggml_tensor *tensor) try {
  12322. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  12323. GGML_ASSERT(ggml_is_contiguous(tensor));
  12324. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  12325. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12326. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  12327. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  12328. .memcpy(extra->data_device[g_main_device],
  12329. tensor->data, ggml_nbytes(tensor))
  12330. .wait()));
  12331. }
  12332. catch (sycl::exception const &exc) {
  12333. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12334. << ", line:" << __LINE__ << std::endl;
  12335. std::exit(1);
  12336. }
  12337. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor) {
  12338. ggml_sycl_assign_buffers_impl(tensor, true, false, false);
  12339. }
  12340. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  12341. ggml_sycl_assign_buffers_impl(tensor, true, false, true);
  12342. }
  12343. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  12344. ggml_sycl_assign_buffers_impl(tensor, false, false, false);
  12345. }
  12346. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  12347. ggml_sycl_assign_buffers_impl(tensor, false, true, false);
  12348. }
  12349. void ggml_sycl_set_main_device(const int main_device) try {
  12350. if (g_main_device == main_device) return;
  12351. check_allow_gpu_index(main_device);
  12352. g_main_device = main_device;
  12353. g_main_device_id = g_sycl_gpu_mgr->gpus[main_device];
  12354. if (g_ggml_sycl_debug) {
  12355. dpct::device_info prop;
  12356. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  12357. prop, dpct::dev_mgr::instance().get_device(g_main_device_id))));
  12358. fprintf(stderr, "Using device %d (%s) as main device\n",
  12359. g_main_device_id, prop.get_name());
  12360. }
  12361. }
  12362. catch (sycl::exception const &exc) {
  12363. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12364. << ", line:" << __LINE__ << std::endl;
  12365. std::exit(1);
  12366. }
  12367. void ggml_sycl_set_scratch_size(const size_t scratch_size) {
  12368. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  12369. // it still won't always work as expected, but it's better than nothing
  12370. if (scratch_size > g_scratch_size) {
  12371. ggml_sycl_free_scratch();
  12372. }
  12373. g_scratch_size = std::max(g_scratch_size, scratch_size);
  12374. }
  12375. void ggml_sycl_free_scratch() try {
  12376. if (g_scratch_buffer == nullptr) {
  12377. return;
  12378. }
  12379. ggml_sycl_set_device(g_main_device);
  12380. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  12381. SYCL_CHECK(CHECK_TRY_ERROR(
  12382. sycl::free(g_scratch_buffer, *stream)));
  12383. g_scratch_buffer = nullptr;
  12384. }
  12385. catch (sycl::exception const &exc) {
  12386. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12387. << ", line:" << __LINE__ << std::endl;
  12388. std::exit(1);
  12389. }
  12390. bool ggml_sycl_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  12391. if (!g_sycl_loaded) return false;
  12392. ggml_sycl_func_t func;
  12393. const bool any_on_device = tensor->backend == GGML_BACKEND_TYPE_GPU
  12394. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT))
  12395. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_TYPE_GPU);
  12396. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  12397. return false;
  12398. }
  12399. if (tensor->op == GGML_OP_MUL_MAT) {
  12400. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  12401. #ifndef NDEBUG
  12402. 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]);
  12403. #endif
  12404. return false;
  12405. }
  12406. }
  12407. switch (tensor->op) {
  12408. case GGML_OP_REPEAT:
  12409. func = ggml_sycl_repeat;
  12410. break;
  12411. case GGML_OP_GET_ROWS:
  12412. func = ggml_sycl_get_rows;
  12413. break;
  12414. case GGML_OP_DUP:
  12415. func = ggml_sycl_dup;
  12416. break;
  12417. case GGML_OP_ADD:
  12418. func = ggml_sycl_add;
  12419. break;
  12420. case GGML_OP_ACC:
  12421. func = ggml_sycl_acc;
  12422. break;
  12423. case GGML_OP_MUL:
  12424. func = ggml_sycl_mul;
  12425. break;
  12426. case GGML_OP_DIV:
  12427. func = ggml_sycl_div;
  12428. break;
  12429. case GGML_OP_UNARY:
  12430. switch (ggml_get_unary_op(tensor)) {
  12431. case GGML_UNARY_OP_GELU:
  12432. func = ggml_sycl_gelu;
  12433. break;
  12434. case GGML_UNARY_OP_SILU:
  12435. func = ggml_sycl_silu;
  12436. break;
  12437. case GGML_UNARY_OP_GELU_QUICK:
  12438. func = ggml_sycl_gelu_quick;
  12439. break;
  12440. case GGML_UNARY_OP_TANH:
  12441. func = ggml_sycl_tanh;
  12442. break;
  12443. case GGML_UNARY_OP_RELU:
  12444. func = ggml_sycl_relu;
  12445. break;
  12446. case GGML_UNARY_OP_HARDSIGMOID:
  12447. func = ggml_sycl_hardsigmoid;
  12448. break;
  12449. case GGML_UNARY_OP_HARDSWISH:
  12450. func = ggml_sycl_hardswish;
  12451. break;
  12452. default:
  12453. return false;
  12454. }
  12455. break;
  12456. case GGML_OP_NORM:
  12457. func = ggml_sycl_norm;
  12458. break;
  12459. case GGML_OP_GROUP_NORM:
  12460. func = ggml_sycl_group_norm;
  12461. break;
  12462. case GGML_OP_CONCAT:
  12463. func = ggml_sycl_concat;
  12464. break;
  12465. case GGML_OP_UPSCALE:
  12466. func = ggml_sycl_upscale;
  12467. break;
  12468. case GGML_OP_PAD:
  12469. func = ggml_sycl_pad;
  12470. break;
  12471. case GGML_OP_LEAKY_RELU:
  12472. func = ggml_sycl_leaky_relu;
  12473. break;
  12474. case GGML_OP_RMS_NORM:
  12475. func = ggml_sycl_rms_norm;
  12476. break;
  12477. case GGML_OP_MUL_MAT:
  12478. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  12479. return false;
  12480. }
  12481. func = ggml_sycl_mul_mat;
  12482. break;
  12483. case GGML_OP_MUL_MAT_ID:
  12484. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  12485. return false;
  12486. }
  12487. func = ggml_sycl_mul_mat_id;
  12488. break;
  12489. case GGML_OP_SCALE:
  12490. func = ggml_sycl_scale;
  12491. break;
  12492. case GGML_OP_SQR:
  12493. func = ggml_sycl_sqr;
  12494. break;
  12495. case GGML_OP_CLAMP:
  12496. func = ggml_sycl_clamp;
  12497. break;
  12498. case GGML_OP_CPY:
  12499. func = ggml_sycl_cpy;
  12500. break;
  12501. case GGML_OP_CONT:
  12502. func = ggml_sycl_dup;
  12503. break;
  12504. case GGML_OP_NONE:
  12505. case GGML_OP_RESHAPE:
  12506. case GGML_OP_VIEW:
  12507. case GGML_OP_PERMUTE:
  12508. case GGML_OP_TRANSPOSE:
  12509. func = ggml_sycl_nop;
  12510. break;
  12511. case GGML_OP_DIAG_MASK_INF:
  12512. func = ggml_sycl_diag_mask_inf;
  12513. break;
  12514. case GGML_OP_SOFT_MAX:
  12515. func = ggml_sycl_soft_max;
  12516. break;
  12517. case GGML_OP_ROPE:
  12518. func = ggml_sycl_rope;
  12519. break;
  12520. case GGML_OP_ALIBI:
  12521. func = ggml_sycl_alibi;
  12522. break;
  12523. case GGML_OP_IM2COL:
  12524. func = ggml_sycl_im2col;
  12525. break;
  12526. case GGML_OP_POOL_2D:
  12527. func = ggml_sycl_pool2d;
  12528. break;
  12529. case GGML_OP_SUM_ROWS:
  12530. func = ggml_sycl_sum_rows;
  12531. break;
  12532. case GGML_OP_ARGSORT:
  12533. func = ggml_sycl_argsort;
  12534. break;
  12535. default:
  12536. return false;
  12537. }
  12538. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  12539. ggml_sycl_set_peer_access(tensor->src[1]->ne[1]);
  12540. }
  12541. if (params->ith != 0) {
  12542. return true;
  12543. }
  12544. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  12545. return true;
  12546. }
  12547. func(tensor->src[0], tensor->src[1], tensor);
  12548. return true;
  12549. }
  12550. GGML_API GGML_CALL void ggml_sycl_get_gpu_list(int *id_list, int max_len) try {
  12551. for(int i=0;i<max_len;i++) id_list[i] = -1;
  12552. if (!g_sycl_gpu_mgr) {
  12553. g_sycl_gpu_mgr = new sycl_gpu_mgr();
  12554. }
  12555. for (int i=0;i< g_sycl_gpu_mgr->gpus.size();i++){
  12556. if (i>=max_len) break;
  12557. id_list[i] = g_sycl_gpu_mgr->gpus[i];
  12558. }
  12559. return;
  12560. }
  12561. catch (sycl::exception const &exc) {
  12562. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12563. << ", line:" << __LINE__ << std::endl;
  12564. std::exit(1);
  12565. }
  12566. int ggml_sycl_get_device_count() try {
  12567. int device_count;
  12568. if (CHECK_TRY_ERROR(device_count =
  12569. dpct::dev_mgr::instance().device_count()) != 0) {
  12570. return 0;
  12571. }
  12572. return device_count;
  12573. }
  12574. catch (sycl::exception const &exc) {
  12575. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12576. << ", line:" << __LINE__ << std::endl;
  12577. std::exit(1);
  12578. }
  12579. GGML_API GGML_CALL void ggml_sycl_get_device_description(int device, char *description,
  12580. size_t description_size) try {
  12581. dpct::device_info prop;
  12582. int device_id = g_sycl_gpu_mgr->gpus[device];
  12583. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  12584. prop, dpct::dev_mgr::instance().get_device(device_id))));
  12585. snprintf(description, description_size, "%s", prop.get_name());
  12586. }
  12587. catch (sycl::exception const &exc) {
  12588. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12589. << ", line:" << __LINE__ << std::endl;
  12590. std::exit(1);
  12591. }
  12592. GGML_CALL void ggml_backend_sycl_get_device_memory(int device, size_t *free,
  12593. size_t *total) try {
  12594. ggml_sycl_set_device(device);
  12595. /*
  12596. DPCT1009:218: SYCL uses exceptions to report errors and does not use the
  12597. error codes. The original code was commented out and a warning string was
  12598. inserted. You need to rewrite this code.
  12599. */
  12600. /*
  12601. DPCT1106:217: 'cudaMemGetInfo' was migrated with the Intel extensions for
  12602. device information which may not be supported by all compilers or runtimes.
  12603. You may need to adjust the code.
  12604. */
  12605. int device_id = g_sycl_gpu_mgr->gpus[device];
  12606. SYCL_CHECK(CHECK_TRY_ERROR(
  12607. dpct::dev_mgr::instance().get_device(device_id).get_memory_info(*free, *total)));
  12608. }
  12609. catch (sycl::exception const &exc) {
  12610. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12611. << ", line:" << __LINE__ << std::endl;
  12612. std::exit(1);
  12613. }
  12614. ////////////////////////////////////////////////////////////////////////////////
  12615. // backend interface
  12616. #define UNUSED GGML_UNUSED
  12617. // sycl buffer
  12618. struct ggml_backend_sycl_buffer_context {
  12619. int device;
  12620. void * dev_ptr = nullptr;
  12621. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  12622. size_t temp_tensor_extra_index = 0;
  12623. std::string name;
  12624. ggml_backend_sycl_buffer_context(int device, void * dev_ptr) :
  12625. device(device), dev_ptr(dev_ptr) {
  12626. check_allow_gpu_index(device);
  12627. int id = g_sycl_gpu_mgr->gpus[device];
  12628. name = (GGML_SYCL_NAME + std::to_string(id));
  12629. }
  12630. ~ ggml_backend_sycl_buffer_context() {
  12631. delete[] temp_tensor_extras;
  12632. }
  12633. ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  12634. if (temp_tensor_extras == nullptr) {
  12635. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  12636. }
  12637. size_t alloc_index = temp_tensor_extra_index;
  12638. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  12639. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  12640. memset(extra, 0, sizeof(*extra));
  12641. return extra;
  12642. }
  12643. };
  12644. GGML_CALL static const char * ggml_backend_sycl_buffer_get_name(ggml_backend_buffer_t buffer) {
  12645. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  12646. return ctx->name.c_str();
  12647. }
  12648. GGML_CALL static bool ggml_backend_buffer_is_sycl(ggml_backend_buffer_t buffer) {
  12649. return buffer->iface.get_name == ggml_backend_sycl_buffer_get_name;
  12650. }
  12651. static void
  12652. ggml_backend_sycl_buffer_free_buffer(ggml_backend_buffer_t buffer) try {
  12653. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12654. ggml_sycl_set_device(ctx->device);
  12655. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  12656. SYCL_CHECK(
  12657. CHECK_TRY_ERROR(sycl::free(ctx->dev_ptr, *stream)));
  12658. delete ctx;
  12659. }
  12660. catch (sycl::exception const &exc) {
  12661. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12662. << ", line:" << __LINE__ << std::endl;
  12663. std::exit(1);
  12664. }
  12665. static void * ggml_backend_sycl_buffer_get_base(ggml_backend_buffer_t buffer) {
  12666. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12667. return ctx->dev_ptr;
  12668. }
  12669. GGML_CALL static void
  12670. ggml_backend_sycl_buffer_init_tensor(ggml_backend_buffer_t buffer,
  12671. ggml_tensor *tensor) try {
  12672. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  12673. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  12674. assert(tensor->view_src->buffer->buft == buffer->buft);
  12675. tensor->backend = tensor->view_src->backend;
  12676. tensor->extra = tensor->view_src->extra;
  12677. return;
  12678. }
  12679. ggml_tensor_extra_gpu * extra = ctx->ggml_sycl_alloc_temp_tensor_extra();
  12680. extra->data_device[ctx->device] = tensor->data;
  12681. tensor->backend = GGML_BACKEND_TYPE_GPU;
  12682. tensor->extra = extra;
  12683. if (ggml_is_quantized(tensor->type)) {
  12684. // initialize padding to 0 to avoid possible NaN values
  12685. size_t original_size = ggml_nbytes(tensor);
  12686. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  12687. if (padded_size > original_size && tensor->view_src == nullptr) {
  12688. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[ctx->device][0]->memset(
  12689. (char *)tensor->data + original_size, 0,
  12690. padded_size - original_size).wait()));
  12691. }
  12692. }
  12693. }
  12694. catch (sycl::exception const &exc) {
  12695. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12696. << ", line:" << __LINE__ << std::endl;
  12697. std::exit(1);
  12698. }
  12699. static void ggml_backend_sycl_buffer_set_tensor(ggml_backend_buffer_t buffer,
  12700. ggml_tensor *tensor,
  12701. const void *data, size_t offset,
  12702. size_t size) try {
  12703. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  12704. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12705. ggml_sycl_set_device(ctx->device);
  12706. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  12707. SYCL_CHECK(
  12708. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  12709. SYCL_CHECK(
  12710. CHECK_TRY_ERROR((*stream)
  12711. .memcpy((char *)tensor->data + offset, data, size)
  12712. .wait()));
  12713. }
  12714. catch (sycl::exception const &exc) {
  12715. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12716. << ", line:" << __LINE__ << std::endl;
  12717. std::exit(1);
  12718. }
  12719. static void ggml_backend_sycl_buffer_get_tensor(ggml_backend_buffer_t buffer,
  12720. const ggml_tensor *tensor,
  12721. void *data, size_t offset,
  12722. size_t size) try {
  12723. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  12724. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12725. ggml_sycl_set_device(ctx->device);
  12726. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  12727. SYCL_CHECK(
  12728. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  12729. SYCL_CHECK(CHECK_TRY_ERROR(
  12730. (*stream)
  12731. .memcpy(data, (const char *)tensor->data + offset, size)
  12732. .wait()));
  12733. }
  12734. catch (sycl::exception const &exc) {
  12735. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12736. << ", line:" << __LINE__ << std::endl;
  12737. std::exit(1);
  12738. }
  12739. GGML_CALL static bool
  12740. ggml_backend_sycl_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
  12741. const ggml_tensor *src,
  12742. ggml_tensor *dst) try {
  12743. if (ggml_backend_buffer_is_sycl(src->buffer)) {
  12744. ggml_backend_sycl_buffer_context * src_ctx = (ggml_backend_sycl_buffer_context *)src->buffer->context;
  12745. ggml_backend_sycl_buffer_context * dst_ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  12746. ggml_sycl_set_device(src_ctx->device);
  12747. /*
  12748. DPCT1009:198: SYCL uses exceptions to report errors and does not use the
  12749. error codes. The original code was commented out and a warning string
  12750. was inserted. You need to rewrite this code.
  12751. */
  12752. SYCL_CHECK(CHECK_TRY_ERROR(
  12753. dpct::dev_mgr::instance().get_device(src_ctx->device).queues_wait_and_throw()));
  12754. ggml_sycl_set_device(dst_ctx->device);
  12755. /*
  12756. DPCT1009:199: SYCL uses exceptions to report errors and does not use the
  12757. error codes. The original code was commented out and a warning string
  12758. was inserted. You need to rewrite this code.
  12759. */
  12760. SYCL_CHECK(CHECK_TRY_ERROR(
  12761. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  12762. /*
  12763. DPCT1009:200: SYCL uses exceptions to report errors and does not use the
  12764. error codes. The original code was commented out and a warning string
  12765. was inserted. You need to rewrite this code.
  12766. */
  12767. dpct::queue_ptr stream_dst = g_syclStreams[dst_ctx->device][0];
  12768. dpct::queue_ptr stream_src = g_syclStreams[src_ctx->device][0];
  12769. size_t size = ggml_nbytes(src);
  12770. //todo. it's dirty solutino to walkaroud known issue:device2device cross GPUs.
  12771. dev2dev_memcpy(*stream_dst, *stream_src, dst->data, src->data, size);
  12772. //todo, it's known issue:error in device2device cross GPUs. reused when the issue is fixed. DON"T remove
  12773. #if 0
  12774. SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy(
  12775. (char *)dst->data, (const char *)src->data, size).wait()));
  12776. /*
  12777. DPCT1009:201: SYCL uses exceptions to report errors and does not use the
  12778. error codes. The original code was commented out and a warning string
  12779. was inserted. You need to rewrite this code.
  12780. */
  12781. SYCL_CHECK(CHECK_TRY_ERROR(
  12782. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  12783. #endif
  12784. return true;
  12785. }
  12786. return false;
  12787. }
  12788. catch (sycl::exception const &exc) {
  12789. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12790. << ", line:" << __LINE__ << std::endl;
  12791. std::exit(1);
  12792. }
  12793. static void ggml_backend_sycl_buffer_clear(ggml_backend_buffer_t buffer,
  12794. uint8_t value) try {
  12795. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  12796. ggml_sycl_set_device(ctx->device);
  12797. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  12798. SYCL_CHECK(
  12799. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  12800. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  12801. .memset(ctx->dev_ptr, value, buffer->size)
  12802. .wait()));
  12803. }
  12804. catch (sycl::exception const &exc) {
  12805. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12806. << ", line:" << __LINE__ << std::endl;
  12807. std::exit(1);
  12808. }
  12809. static struct ggml_backend_buffer_i ggml_backend_sycl_buffer_interface = {
  12810. /* .get_name = */ ggml_backend_sycl_buffer_get_name,
  12811. /* .free_buffer = */ ggml_backend_sycl_buffer_free_buffer,
  12812. /* .get_base = */ ggml_backend_sycl_buffer_get_base,
  12813. /* .init_tensor = */ ggml_backend_sycl_buffer_init_tensor,
  12814. /* .set_tensor = */ ggml_backend_sycl_buffer_set_tensor,
  12815. /* .get_tensor = */ ggml_backend_sycl_buffer_get_tensor,
  12816. /* .cpy_tensor = */ ggml_backend_sycl_buffer_cpy_tensor,
  12817. /* .clear = */ ggml_backend_sycl_buffer_clear,
  12818. /* .reset = */ NULL,
  12819. };
  12820. // sycl buffer type
  12821. struct ggml_backend_sycl_buffer_type_context {
  12822. int device;
  12823. std::string name;
  12824. };
  12825. struct ggml_backend_sycl_context {
  12826. int device;
  12827. std::string name;
  12828. };
  12829. GGML_CALL static const char * ggml_backend_sycl_buffer_type_name(ggml_backend_buffer_type_t buft) {
  12830. ggml_backend_sycl_buffer_type_context * ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  12831. return ctx->name.c_str();
  12832. }
  12833. GGML_CALL static ggml_backend_buffer_t
  12834. ggml_backend_sycl_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
  12835. size_t size) try {
  12836. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  12837. ggml_sycl_set_device(buft_ctx->device);
  12838. const dpct::queue_ptr stream = g_syclStreams[buft_ctx->device][0];
  12839. size = std::max(size, (size_t)1); // syclMalloc returns null for size 0
  12840. void * dev_ptr;
  12841. SYCL_CHECK(CHECK_TRY_ERROR(dev_ptr = (void *)sycl::malloc_device(
  12842. size, *stream)));
  12843. ggml_backend_sycl_buffer_context * ctx = new ggml_backend_sycl_buffer_context(buft_ctx->device, dev_ptr);
  12844. return ggml_backend_buffer_init(buft, ggml_backend_sycl_buffer_interface, ctx, size);
  12845. }
  12846. catch (sycl::exception const &exc) {
  12847. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12848. << ", line:" << __LINE__ << std::endl;
  12849. std::exit(1);
  12850. }
  12851. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  12852. return 128;
  12853. UNUSED(buft);
  12854. }
  12855. static size_t ggml_backend_sycl_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  12856. return dpct::get_current_device().get_max_mem_alloc_size();
  12857. UNUSED(buft);
  12858. }
  12859. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  12860. size_t size = ggml_nbytes(tensor);
  12861. int64_t ne0 = tensor->ne[0];
  12862. if (ggml_is_quantized(tensor->type)) {
  12863. if (ne0 % MATRIX_ROW_PADDING != 0) {
  12864. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  12865. }
  12866. }
  12867. return size;
  12868. UNUSED(buft);
  12869. }
  12870. GGML_CALL static bool ggml_backend_sycl_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  12871. if (!ggml_backend_is_sycl(backend)) {
  12872. return false;
  12873. }
  12874. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  12875. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  12876. return buft_ctx->device == sycl_ctx->device;
  12877. }
  12878. static ggml_backend_buffer_type_i ggml_backend_sycl_buffer_type_interface = {
  12879. /* .get_name = */ ggml_backend_sycl_buffer_type_name,
  12880. /* .alloc_buffer = */ ggml_backend_sycl_buffer_type_alloc_buffer,
  12881. /* .get_alignment = */ ggml_backend_sycl_buffer_type_get_alignment,
  12882. /* .get_max_size = */ ggml_backend_sycl_buffer_type_get_max_size,
  12883. /* .get_alloc_size = */ ggml_backend_sycl_buffer_type_get_alloc_size,
  12884. /* .supports_backend = */ ggml_backend_sycl_buffer_type_supports_backend,
  12885. /* .is_host = */ nullptr,
  12886. };
  12887. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(int device) {
  12888. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  12889. static bool ggml_backend_sycl_buffer_type_initialized = false;
  12890. if (!ggml_backend_sycl_buffer_type_initialized) {
  12891. for (int i = 0; i < g_device_count; i++) {
  12892. ggml_backend_sycl_buffer_types[i] = {
  12893. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  12894. /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(g_sycl_gpu_mgr->gpus[i])},
  12895. };
  12896. }
  12897. ggml_backend_sycl_buffer_type_initialized = true;
  12898. }
  12899. return &ggml_backend_sycl_buffer_types[device];
  12900. }
  12901. // sycl split buffer type
  12902. static void get_row_split(int64_t * row_low, int64_t * row_high, const ggml_tensor * tensor, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split, int id) {
  12903. const int64_t nrows = ggml_nrows(tensor);
  12904. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  12905. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  12906. *row_low -= *row_low % rounding;
  12907. if (id == g_device_count - 1) {
  12908. *row_high = nrows;
  12909. } else {
  12910. *row_high = nrows*tensor_split[id + 1];
  12911. *row_high -= *row_high % rounding;
  12912. }
  12913. }
  12914. struct ggml_backend_sycl_split_buffer_context {
  12915. ~ggml_backend_sycl_split_buffer_context() try {
  12916. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  12917. for (int i = 0; i < g_device_count; ++i) {
  12918. // int id = g_sycl_gpu_mgr->gpus[i];
  12919. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  12920. if (extra->events[i][is] != nullptr) {
  12921. /*
  12922. DPCT1009:206: SYCL uses exceptions to report errors and
  12923. does not use the error codes. The original code was
  12924. commented out and a warning string was inserted. You
  12925. need to rewrite this code.
  12926. */
  12927. SYCL_CHECK(CHECK_TRY_ERROR(
  12928. dpct::destroy_event(extra->events[i][is])));
  12929. }
  12930. }
  12931. if (extra->data_device[i] != nullptr) {
  12932. /*
  12933. DPCT1009:207: SYCL uses exceptions to report errors and does
  12934. not use the error codes. The original code was commented out
  12935. and a warning string was inserted. You need to rewrite this
  12936. code.
  12937. */
  12938. ggml_sycl_set_device(i);
  12939. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(
  12940. extra->data_device[i], *g_syclStreams[i][0])));
  12941. }
  12942. }
  12943. delete extra;
  12944. }
  12945. }
  12946. catch (sycl::exception const &exc) {
  12947. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12948. << ", line:" << __LINE__ << std::endl;
  12949. std::exit(1);
  12950. }
  12951. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  12952. };
  12953. GGML_CALL static const char * ggml_backend_sycl_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  12954. return GGML_SYCL_NAME "_Split";
  12955. UNUSED(buffer);
  12956. }
  12957. // unused at the moment
  12958. //static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer) {
  12959. // return buffer->iface.get_name == ggml_backend_sycl_split_buffer_get_name;
  12960. //}
  12961. GGML_CALL static void ggml_backend_sycl_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  12962. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  12963. delete ctx;
  12964. }
  12965. GGML_CALL static void * ggml_backend_sycl_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  12966. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  12967. return (void *)0x1000;
  12968. UNUSED(buffer);
  12969. }
  12970. GGML_CALL static void
  12971. ggml_backend_sycl_split_buffer_init_tensor(ggml_backend_buffer_t buffer,
  12972. ggml_tensor *tensor) try {
  12973. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  12974. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  12975. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  12976. const int64_t ne0 = tensor->ne[0];
  12977. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  12978. ctx->tensor_extras.push_back(extra);
  12979. for (int i = 0; i < g_device_count; ++i) {
  12980. // int id = g_sycl_gpu_mgr->gpus[i];
  12981. int64_t row_low, row_high;
  12982. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  12983. int64_t nrows_split = row_high - row_low;
  12984. if (nrows_split == 0) {
  12985. continue;
  12986. }
  12987. size_t size = ggml_nbytes_split(tensor, nrows_split);
  12988. const size_t original_size = size;
  12989. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  12990. if (ne0 % MATRIX_ROW_PADDING != 0) {
  12991. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  12992. }
  12993. // FIXME: do not crash if cudaMalloc fails
  12994. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  12995. ggml_sycl_set_device(i);
  12996. char * buf;
  12997. /*
  12998. DPCT1009:208: SYCL uses exceptions to report errors and does not use the
  12999. error codes. The original code was commented out and a warning string
  13000. was inserted. You need to rewrite this code.
  13001. */
  13002. SYCL_CHECK(CHECK_TRY_ERROR(buf = (char *)sycl::malloc_device(
  13003. size, *g_syclStreams[i][0])));
  13004. // set padding to 0 to avoid possible NaN values
  13005. if (size > original_size) {
  13006. /*
  13007. DPCT1009:209: SYCL uses exceptions to report errors and does not use
  13008. the error codes. The original code was commented out and a warning
  13009. string was inserted. You need to rewrite this code.
  13010. */
  13011. SYCL_CHECK(CHECK_TRY_ERROR(
  13012. (*g_syclStreams[i][0])
  13013. .memset(buf + original_size, 0, size - original_size)
  13014. .wait()));
  13015. }
  13016. extra->data_device[i] = buf;
  13017. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  13018. /*
  13019. DPCT1009:210: SYCL uses exceptions to report errors and does not use
  13020. the error codes. The original code was commented out and a warning
  13021. string was inserted. You need to rewrite this code.
  13022. */
  13023. SYCL_CHECK(
  13024. CHECK_TRY_ERROR(extra->events[i][is] = new sycl::event()));
  13025. }
  13026. }
  13027. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  13028. tensor->extra = extra;
  13029. }
  13030. catch (sycl::exception const &exc) {
  13031. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13032. << ", line:" << __LINE__ << std::endl;
  13033. std::exit(1);
  13034. }
  13035. GGML_CALL static void
  13036. ggml_backend_sycl_split_buffer_set_tensor(ggml_backend_buffer_t buffer,
  13037. ggml_tensor *tensor, const void *data,
  13038. size_t offset, size_t size) try {
  13039. // split tensors must always be set in their entirety at once
  13040. GGML_ASSERT(offset == 0);
  13041. GGML_ASSERT(size == ggml_nbytes(tensor));
  13042. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  13043. const int64_t ne0 = tensor->ne[0];
  13044. const size_t nb1 = tensor->nb[1];
  13045. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  13046. for (int i = 0; i < g_device_count; ++i) {
  13047. // int id = g_sycl_gpu_mgr->gpus[i];
  13048. int64_t row_low, row_high;
  13049. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  13050. int64_t nrows_split = row_high - row_low;
  13051. if (nrows_split == 0) {
  13052. continue;
  13053. }
  13054. const size_t offset_split = row_low*nb1;
  13055. size_t size = ggml_nbytes_split(tensor, nrows_split);
  13056. const size_t original_size = size;
  13057. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  13058. if (ne0 % MATRIX_ROW_PADDING != 0) {
  13059. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  13060. }
  13061. const char * buf_host = (const char *)data + offset_split;
  13062. /*
  13063. DPCT1009:211: SYCL uses exceptions to report errors and does not use the
  13064. error codes. The original code was commented out and a warning string
  13065. was inserted. You need to rewrite this code.
  13066. */
  13067. ggml_sycl_set_device(i);
  13068. SYCL_CHECK(CHECK_TRY_ERROR(
  13069. (*g_syclStreams[i][0])
  13070. .memcpy(extra->data_device[i], buf_host, original_size)
  13071. .wait()));
  13072. }
  13073. }
  13074. catch (sycl::exception const &exc) {
  13075. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13076. << ", line:" << __LINE__ << std::endl;
  13077. std::exit(1);
  13078. }
  13079. GGML_CALL static void
  13080. ggml_backend_sycl_split_buffer_get_tensor(ggml_backend_buffer_t buffer,
  13081. const ggml_tensor *tensor, void *data,
  13082. size_t offset, size_t size) try {
  13083. // split tensors must always be set in their entirety at once
  13084. GGML_ASSERT(offset == 0);
  13085. GGML_ASSERT(size == ggml_nbytes(tensor));
  13086. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  13087. const int64_t ne0 = tensor->ne[0];
  13088. const size_t nb1 = tensor->nb[1];
  13089. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  13090. for (int i = 0; i < g_device_count; ++i) {
  13091. // int id = g_sycl_gpu_mgr->gpus[i];
  13092. int64_t row_low, row_high;
  13093. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  13094. int64_t nrows_split = row_high - row_low;
  13095. if (nrows_split == 0) {
  13096. continue;
  13097. }
  13098. const size_t offset_split = row_low*nb1;
  13099. size_t size = ggml_nbytes_split(tensor, nrows_split);
  13100. const size_t original_size = size;
  13101. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  13102. if (ne0 % MATRIX_ROW_PADDING != 0) {
  13103. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  13104. }
  13105. char * buf_host = (char *)data + offset_split;
  13106. /*
  13107. DPCT1009:212: SYCL uses exceptions to report errors and does not use the
  13108. error codes. The original code was commented out and a warning string
  13109. was inserted. You need to rewrite this code.
  13110. */
  13111. ggml_sycl_set_device(i);
  13112. SYCL_CHECK(CHECK_TRY_ERROR(
  13113. (*g_syclStreams[i][0])
  13114. .memcpy(buf_host, extra->data_device[i], original_size)
  13115. .wait()));
  13116. }
  13117. }
  13118. catch (sycl::exception const &exc) {
  13119. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13120. << ", line:" << __LINE__ << std::endl;
  13121. std::exit(1);
  13122. }
  13123. GGML_CALL static void ggml_backend_sycl_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  13124. UNUSED(buffer);
  13125. UNUSED(value);
  13126. }
  13127. static struct ggml_backend_buffer_i ggml_backend_sycl_split_buffer_interface = {
  13128. /* .get_name = */ ggml_backend_sycl_split_buffer_get_name,
  13129. /* .free_buffer = */ ggml_backend_sycl_split_buffer_free_buffer,
  13130. /* .get_base = */ ggml_backend_sycl_split_buffer_get_base,
  13131. /* .init_tensor = */ ggml_backend_sycl_split_buffer_init_tensor,
  13132. /* .set_tensor = */ ggml_backend_sycl_split_buffer_set_tensor,
  13133. /* .get_tensor = */ ggml_backend_sycl_split_buffer_get_tensor,
  13134. /* .cpy_tensor = */ NULL,
  13135. /* .clear = */ ggml_backend_sycl_split_buffer_clear,
  13136. /* .reset = */ NULL,
  13137. };
  13138. GGML_CALL static const char * ggml_backend_sycl_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  13139. return GGML_SYCL_NAME "_Split";
  13140. UNUSED(buft);
  13141. }
  13142. GGML_CALL static ggml_backend_buffer_t ggml_backend_sycl_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  13143. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  13144. // instead, we allocate them for each tensor separately in init_tensor
  13145. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  13146. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  13147. ggml_backend_sycl_split_buffer_context * ctx = new ggml_backend_sycl_split_buffer_context();
  13148. return ggml_backend_buffer_init(buft, ggml_backend_sycl_split_buffer_interface, ctx, size);
  13149. }
  13150. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  13151. return 128;
  13152. UNUSED(buft);
  13153. }
  13154. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  13155. ggml_backend_sycl_split_buffer_type_context * ctx = (ggml_backend_sycl_split_buffer_type_context *)buft->context;
  13156. size_t total_size = 0;
  13157. const int64_t ne0 = tensor->ne[0];
  13158. for (int i = 0; i < g_device_count; ++i) {
  13159. // int id = g_sycl_gpu_mgr->gpus[i];
  13160. int64_t row_low, row_high;
  13161. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, i);
  13162. int64_t nrows_split = row_high - row_low;
  13163. if (nrows_split == 0) {
  13164. continue;
  13165. }
  13166. total_size += ggml_nbytes_split(tensor, nrows_split);
  13167. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  13168. if (ne0 % MATRIX_ROW_PADDING != 0) {
  13169. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  13170. }
  13171. }
  13172. return total_size;
  13173. }
  13174. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  13175. return ggml_backend_is_sycl(backend);
  13176. UNUSED(buft);
  13177. }
  13178. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  13179. return false;
  13180. UNUSED(buft);
  13181. }
  13182. static ggml_backend_buffer_type_i ggml_backend_sycl_split_buffer_type_interface = {
  13183. /* .get_name = */ ggml_backend_sycl_split_buffer_type_name,
  13184. /* .alloc_buffer = */ ggml_backend_sycl_split_buffer_type_alloc_buffer,
  13185. /* .get_alignment = */ ggml_backend_sycl_split_buffer_type_get_alignment,
  13186. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  13187. /* .get_alloc_size = */ ggml_backend_sycl_split_buffer_type_get_alloc_size,
  13188. /* .supports_backend = */ ggml_backend_sycl_split_buffer_type_supports_backend,
  13189. /* .is_host = */ ggml_backend_sycl_split_buffer_type_is_host,
  13190. };
  13191. GGML_CALL ggml_backend_buffer_type_t ggml_backend_sycl_split_buffer_type(const float * tensor_split) {
  13192. // FIXME: this is not thread safe
  13193. static std::map<std::array<float, GGML_SYCL_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  13194. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split_arr = {};
  13195. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_SYCL_MAX_DEVICES, [](float x) { return x == 0.0f; });
  13196. if (all_zero) {
  13197. tensor_split_arr = g_default_tensor_split;
  13198. } else {
  13199. float split_sum = 0.0f;
  13200. for (int i = 0; i < g_device_count; ++i) {
  13201. // int id = g_sycl_gpu_mgr->gpus[i];
  13202. tensor_split_arr[i] = split_sum;
  13203. split_sum += tensor_split[i];
  13204. }
  13205. for (int i = 0; i < g_device_count; ++i) {
  13206. // int id = g_sycl_gpu_mgr->gpus[i];
  13207. tensor_split_arr[i] /= split_sum;
  13208. }
  13209. }
  13210. auto it = buft_map.find(tensor_split_arr);
  13211. if (it != buft_map.end()) {
  13212. return &it->second;
  13213. }
  13214. struct ggml_backend_buffer_type buft {
  13215. /* .iface = */ ggml_backend_sycl_split_buffer_type_interface,
  13216. /* .context = */ new ggml_backend_sycl_split_buffer_type_context{tensor_split_arr},
  13217. };
  13218. auto result = buft_map.emplace(tensor_split_arr, buft);
  13219. return &result.first->second;
  13220. }
  13221. // host buffer type
  13222. GGML_CALL static const char * ggml_backend_sycl_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  13223. return GGML_SYCL_NAME "_Host";
  13224. UNUSED(buft);
  13225. }
  13226. GGML_CALL static const char * ggml_backend_sycl_host_buffer_name(ggml_backend_buffer_t buffer) {
  13227. return GGML_SYCL_NAME "_Host";
  13228. UNUSED(buffer);
  13229. }
  13230. static void ggml_backend_sycl_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  13231. ggml_sycl_host_free(buffer->context);
  13232. }
  13233. static ggml_backend_buffer_t ggml_backend_sycl_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  13234. void * ptr = ggml_sycl_host_malloc(size);
  13235. if (ptr == nullptr) {
  13236. // fallback to cpu buffer
  13237. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  13238. }
  13239. // FIXME: this is a hack to avoid having to implement a new buffer type
  13240. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  13241. buffer->buft = buft;
  13242. buffer->iface.get_name = ggml_backend_sycl_host_buffer_name;
  13243. buffer->iface.free_buffer = ggml_backend_sycl_host_buffer_free_buffer;
  13244. return buffer;
  13245. }
  13246. ggml_backend_buffer_type_t ggml_backend_sycl_host_buffer_type() {
  13247. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_type_host = {
  13248. /* .iface = */ {
  13249. /* .get_name = */ ggml_backend_sycl_host_buffer_type_name,
  13250. /* .alloc_buffer = */ ggml_backend_sycl_host_buffer_type_alloc_buffer,
  13251. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  13252. /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
  13253. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  13254. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  13255. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  13256. },
  13257. /* .context = */ nullptr,
  13258. };
  13259. return &ggml_backend_sycl_buffer_type_host;
  13260. }
  13261. // backend
  13262. GGML_CALL static const char * ggml_backend_sycl_name(ggml_backend_t backend) {
  13263. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  13264. return sycl_ctx->name.c_str();
  13265. }
  13266. GGML_CALL static void ggml_backend_sycl_free(ggml_backend_t backend) {
  13267. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  13268. delete sycl_ctx;
  13269. delete backend;
  13270. }
  13271. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_sycl_get_default_buffer_type(ggml_backend_t backend) {
  13272. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  13273. return ggml_backend_sycl_buffer_type(sycl_ctx->device);
  13274. }
  13275. GGML_CALL static void ggml_backend_sycl_set_tensor_async(ggml_backend_t backend,
  13276. ggml_tensor *tensor,
  13277. const void *data, size_t offset,
  13278. size_t size) try {
  13279. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  13280. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  13281. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  13282. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  13283. (char *)tensor->data + offset, data, size)));
  13284. }
  13285. catch (sycl::exception const &exc) {
  13286. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13287. << ", line:" << __LINE__ << std::endl;
  13288. std::exit(1);
  13289. }
  13290. GGML_CALL static void ggml_backend_sycl_get_tensor_async(ggml_backend_t backend,
  13291. const ggml_tensor *tensor,
  13292. void *data, size_t offset,
  13293. size_t size) try {
  13294. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  13295. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  13296. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  13297. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  13298. data, (const char *)tensor->data + offset, size)));
  13299. }
  13300. catch (sycl::exception const &exc) {
  13301. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13302. << ", line:" << __LINE__ << std::endl;
  13303. std::exit(1);
  13304. }
  13305. GGML_CALL static bool ggml_backend_sycl_cpy_tensor_async(ggml_backend_t backend,
  13306. const ggml_tensor *src,
  13307. ggml_tensor *dst) try {
  13308. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  13309. if (dst->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && ggml_backend_buffer_is_sycl(src->buffer)) {
  13310. /*
  13311. DPCT1009:215: SYCL uses exceptions to report errors and does not use the
  13312. error codes. The original code was commented out and a warning string
  13313. was inserted. You need to rewrite this code.
  13314. */
  13315. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  13316. dst->data, src->data, ggml_nbytes(dst))));
  13317. return true;
  13318. }
  13319. return false;
  13320. }
  13321. catch (sycl::exception const &exc) {
  13322. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13323. << ", line:" << __LINE__ << std::endl;
  13324. std::exit(1);
  13325. }
  13326. static void ggml_backend_sycl_synchronize(ggml_backend_t backend) try {
  13327. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  13328. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->wait()));
  13329. UNUSED(backend);
  13330. }
  13331. catch (sycl::exception const &exc) {
  13332. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13333. << ", line:" << __LINE__ << std::endl;
  13334. std::exit(1);
  13335. }
  13336. GGML_CALL static bool ggml_backend_sycl_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  13337. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  13338. ggml_sycl_set_main_device(sycl_ctx->device);
  13339. ggml_compute_params params = {};
  13340. params.type = GGML_TASK_TYPE_COMPUTE;
  13341. params.ith = 0;
  13342. for (int i = 0; i < cgraph->n_nodes; i++) {
  13343. ggml_tensor * node = cgraph->nodes[i];
  13344. if (node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
  13345. continue;
  13346. }
  13347. #ifndef NDEBUG
  13348. assert(node->backend == GGML_BACKEND_TYPE_GPU || node->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  13349. assert(node->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  13350. assert(node->extra != nullptr);
  13351. for (int j = 0; j < GGML_MAX_SRC; j++) {
  13352. if (node->src[j] != nullptr) {
  13353. assert(node->src[j]->backend == GGML_BACKEND_TYPE_GPU || node->src[j]->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  13354. assert(node->src[j]->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  13355. assert(node->src[j]->extra != nullptr);
  13356. }
  13357. }
  13358. #endif
  13359. bool ok = ggml_sycl_compute_forward(&params, node);
  13360. if (!ok) {
  13361. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  13362. }
  13363. GGML_ASSERT(ok);
  13364. }
  13365. return true;
  13366. }
  13367. GGML_CALL static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  13368. switch (op->op) {
  13369. case GGML_OP_UNARY:
  13370. switch (ggml_get_unary_op(op)) {
  13371. case GGML_UNARY_OP_GELU:
  13372. case GGML_UNARY_OP_SILU:
  13373. case GGML_UNARY_OP_RELU:
  13374. case GGML_UNARY_OP_HARDSIGMOID:
  13375. case GGML_UNARY_OP_HARDSWISH:
  13376. case GGML_UNARY_OP_GELU_QUICK:
  13377. case GGML_UNARY_OP_TANH:
  13378. return true;
  13379. default:
  13380. return false;
  13381. }
  13382. break;
  13383. case GGML_OP_MUL_MAT:
  13384. case GGML_OP_MUL_MAT_ID:
  13385. {
  13386. struct ggml_tensor * a;
  13387. struct ggml_tensor * b;
  13388. if (op->op == GGML_OP_MUL_MAT) {
  13389. a = op->src[0];
  13390. b = op->src[1];
  13391. } else {
  13392. a = op->src[2];
  13393. b = op->src[1];
  13394. }
  13395. if (a->ne[3] != b->ne[3]) {
  13396. return false;
  13397. }
  13398. if (a->type == GGML_TYPE_IQ1_S) {
  13399. return false;
  13400. }
  13401. if (a->type == GGML_TYPE_IQ3_XXS) {
  13402. return false;
  13403. }
  13404. if (a->type == GGML_TYPE_IQ2_XXS) {
  13405. return false;
  13406. }
  13407. if (a->type == GGML_TYPE_IQ2_XS) {
  13408. return false;
  13409. }
  13410. return true;
  13411. } break;
  13412. case GGML_OP_GET_ROWS:
  13413. {
  13414. switch (op->src[0]->type) {
  13415. case GGML_TYPE_F16:
  13416. case GGML_TYPE_F32:
  13417. case GGML_TYPE_Q4_0:
  13418. case GGML_TYPE_Q4_1:
  13419. case GGML_TYPE_Q5_0:
  13420. case GGML_TYPE_Q5_1:
  13421. case GGML_TYPE_Q8_0:
  13422. return true;
  13423. default:
  13424. return false;
  13425. }
  13426. } break;
  13427. case GGML_OP_CPY:
  13428. {
  13429. ggml_type src0_type = op->src[0]->type;
  13430. ggml_type src1_type = op->src[1]->type;
  13431. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  13432. return true;
  13433. }
  13434. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  13435. return true;
  13436. }
  13437. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  13438. return true;
  13439. }
  13440. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  13441. return true;
  13442. }
  13443. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  13444. return true;
  13445. }
  13446. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  13447. return true;
  13448. }
  13449. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  13450. return true;
  13451. }
  13452. return false;
  13453. } break;
  13454. case GGML_OP_DUP:
  13455. case GGML_OP_REPEAT:
  13456. case GGML_OP_CONCAT:
  13457. {
  13458. ggml_type src0_type = op->src[0]->type;
  13459. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  13460. } break;
  13461. case GGML_OP_NONE:
  13462. case GGML_OP_RESHAPE:
  13463. case GGML_OP_VIEW:
  13464. case GGML_OP_PERMUTE:
  13465. case GGML_OP_TRANSPOSE:
  13466. case GGML_OP_NORM:
  13467. case GGML_OP_ADD:
  13468. case GGML_OP_MUL:
  13469. case GGML_OP_DIV:
  13470. case GGML_OP_RMS_NORM:
  13471. case GGML_OP_SCALE:
  13472. case GGML_OP_SQR:
  13473. case GGML_OP_CLAMP:
  13474. case GGML_OP_CONT:
  13475. case GGML_OP_DIAG_MASK_INF:
  13476. case GGML_OP_SOFT_MAX:
  13477. case GGML_OP_ROPE:
  13478. case GGML_OP_ALIBI:
  13479. case GGML_OP_IM2COL:
  13480. case GGML_OP_POOL_2D:
  13481. case GGML_OP_SUM_ROWS:
  13482. case GGML_OP_ARGSORT:
  13483. case GGML_OP_ACC:
  13484. case GGML_OP_GROUP_NORM:
  13485. case GGML_OP_UPSCALE:
  13486. case GGML_OP_PAD:
  13487. case GGML_OP_LEAKY_RELU:
  13488. return true;
  13489. default:
  13490. return false;
  13491. }
  13492. UNUSED(backend);
  13493. }
  13494. static ggml_backend_i ggml_backend_sycl_interface = {
  13495. /* .get_name = */ ggml_backend_sycl_name,
  13496. /* .free = */ ggml_backend_sycl_free,
  13497. /* .get_default_buffer_type = */ ggml_backend_sycl_get_default_buffer_type,
  13498. /* .set_tensor_async = */ ggml_backend_sycl_set_tensor_async,
  13499. /* .get_tensor_async = */ ggml_backend_sycl_get_tensor_async,
  13500. /* .cpy_tensor_async = */ ggml_backend_sycl_cpy_tensor_async,
  13501. /* .synchronize = */ ggml_backend_sycl_synchronize,
  13502. /* .graph_plan_create = */ NULL,
  13503. /* .graph_plan_free = */ NULL,
  13504. /* .graph_plan_compute = */ NULL,
  13505. /* .graph_compute = */ ggml_backend_sycl_graph_compute,
  13506. /* .supports_op = */ ggml_backend_sycl_supports_op,
  13507. };
  13508. static ggml_guid_t ggml_backend_sycl_guid() {
  13509. static ggml_guid guid = { 0x58, 0x05, 0x13, 0x8f, 0xcd, 0x3a, 0x61, 0x9d, 0xe7, 0xcd, 0x98, 0xa9, 0x03, 0xfd, 0x7c, 0x53 };
  13510. return &guid;
  13511. }
  13512. GGML_CALL ggml_backend_t ggml_backend_sycl_init(int device) {
  13513. ggml_init_sycl(); // TODO: remove from ggml.c
  13514. check_allow_gpu_index(device);
  13515. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  13516. ggml_sycl_set_main_device(device);
  13517. int id = g_sycl_gpu_mgr->gpus[device];
  13518. ggml_backend_sycl_context * ctx = new ggml_backend_sycl_context {
  13519. /* .device = */ device,
  13520. /* .name = */ GGML_SYCL_NAME + std::to_string(id),
  13521. };
  13522. ggml_backend_t sycl_backend = new ggml_backend {
  13523. /* .guid = */ ggml_backend_sycl_guid(),
  13524. /* .interface = */ ggml_backend_sycl_interface,
  13525. /* .context = */ ctx
  13526. };
  13527. return sycl_backend;
  13528. }
  13529. bool ggml_backend_is_sycl(ggml_backend_t backend) {
  13530. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_sycl_guid());
  13531. }
  13532. GGML_CALL int ggml_backend_sycl_get_device_count() {
  13533. if (!g_sycl_gpu_mgr) g_sycl_gpu_mgr = new sycl_gpu_mgr();
  13534. return g_sycl_gpu_mgr->get_gpu_count();
  13535. }
  13536. GGML_CALL static ggml_backend_t ggml_backend_reg_sycl_init(const char * params, void * user_data) {
  13537. ggml_backend_t sycl_backend = ggml_backend_sycl_init((int) (intptr_t) user_data);
  13538. return sycl_backend;
  13539. UNUSED(params);
  13540. }
  13541. GGML_API GGML_CALL int ggml_backend_sycl_get_device_index(int device_id) {
  13542. return g_sycl_gpu_mgr->get_index(device_id);
  13543. }
  13544. extern "C" int ggml_backend_sycl_reg_devices();
  13545. int ggml_backend_sycl_reg_devices() {
  13546. if (!g_sycl_gpu_mgr) g_sycl_gpu_mgr = new sycl_gpu_mgr();
  13547. g_device_count = g_sycl_gpu_mgr->get_gpu_count();
  13548. assert(g_device_count>0);
  13549. for (int i = 0; i < g_device_count; i++) {
  13550. int id = g_sycl_gpu_mgr->gpus[i];
  13551. char name[128];
  13552. snprintf(name, sizeof(name), "%s%d", GGML_SYCL_NAME, id);
  13553. ggml_backend_register(name, ggml_backend_reg_sycl_init, ggml_backend_sycl_buffer_type(i), (void *) (intptr_t) i);
  13554. }
  13555. return g_device_count;
  13556. }