1
0

ggml-sycl.cpp 666 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283
  1. //
  2. // MIT license
  3. // Copyright (C) 2024 Intel Corporation
  4. // SPDX-License-Identifier: MIT
  5. //
  6. //
  7. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  8. // See https://llvm.org/LICENSE.txt for license information.
  9. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  10. //
  11. #include <algorithm>
  12. #include <assert.h>
  13. #include <atomic>
  14. #include <cinttypes>
  15. #include <cstddef>
  16. #include <cstdint>
  17. #include <cstdlib>
  18. #include <float.h>
  19. #include <limits>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <vector>
  23. #include <cmath>
  24. #include <iostream>
  25. #include <fstream>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <regex>
  29. #include <sycl/sycl.hpp>
  30. #include <sycl/half_type.hpp>
  31. #include "ggml-sycl.h"
  32. #include "ggml.h"
  33. #include "ggml-backend-impl.h"
  34. /*
  35. Following definition copied from DPCT head files, which are used by ggml-sycl.cpp
  36. */
  37. // COPY from DPCT head files
  38. #include <sycl/sycl.hpp>
  39. #include <oneapi/mkl.hpp>
  40. #include <map>
  41. #if defined(__linux__)
  42. #include <sys/mman.h>
  43. #elif defined(_WIN64)
  44. #ifndef NOMINMAX
  45. #define NOMINMAX
  46. #endif
  47. #include <windows.h>
  48. #else
  49. #error "Only support Windows and Linux."
  50. #endif
  51. #if defined(__linux__)
  52. #include <unistd.h>
  53. #include <sys/syscall.h>
  54. #endif
  55. #if defined(_WIN64)
  56. #ifndef NOMINMAX
  57. #define NOMINMAX
  58. #endif
  59. #include <windows.h>
  60. #endif
  61. #define DPCT_COMPATIBILITY_TEMP (900)
  62. #if defined(_MSC_VER)
  63. #define __dpct_align__(n) __declspec(align(n))
  64. #define __dpct_inline__ __forceinline
  65. #else
  66. #define __dpct_align__(n) __attribute__((aligned(n)))
  67. #define __dpct_inline__ __inline__ __attribute__((always_inline))
  68. #endif
  69. #if defined(_MSC_VER)
  70. #define __dpct_noinline__ __declspec(noinline)
  71. #else
  72. #define __dpct_noinline__ __attribute__((noinline))
  73. #endif
  74. std::string get_device_type_name(const sycl::device &Device) {
  75. auto DeviceType = Device.get_info<sycl::info::device::device_type>();
  76. switch (DeviceType) {
  77. case sycl::info::device_type::cpu:
  78. return "cpu";
  79. case sycl::info::device_type::gpu:
  80. return "gpu";
  81. case sycl::info::device_type::host:
  82. return "host";
  83. case sycl::info::device_type::accelerator:
  84. return "acc";
  85. default:
  86. return "unknown";
  87. }
  88. }
  89. std::string get_device_backend_and_type(const sycl::device &device) {
  90. std::stringstream device_type;
  91. sycl::backend backend = device.get_backend();
  92. device_type << backend << ":" << get_device_type_name(device);
  93. return device_type.str();
  94. }
  95. namespace dpct
  96. {
  97. typedef sycl::queue *queue_ptr;
  98. typedef sycl::event *event_ptr;
  99. typedef char *device_ptr;
  100. typedef uint8_t byte_t;
  101. typedef sycl::buffer<byte_t> buffer_t;
  102. /// SYCL default exception handler
  103. inline auto exception_handler = [](sycl::exception_list exceptions)
  104. {
  105. for (std::exception_ptr const &e : exceptions)
  106. {
  107. try
  108. {
  109. std::rethrow_exception(e);
  110. }
  111. catch (sycl::exception const &e)
  112. {
  113. std::cerr << "Caught asynchronous SYCL exception:" << std::endl
  114. << e.what() << std::endl
  115. << "Exception caught at file:" << __FILE__
  116. << ", line:" << __LINE__ << std::endl;
  117. }
  118. }
  119. };
  120. enum error_code
  121. {
  122. success = 0,
  123. default_error = 999
  124. };
  125. enum memcpy_direction
  126. {
  127. host_to_host,
  128. host_to_device,
  129. device_to_host,
  130. device_to_device,
  131. automatic
  132. };
  133. enum memory_region
  134. {
  135. global = 0, // device global memory
  136. constant, // device constant memory
  137. local, // device local memory
  138. shared, // memory which can be accessed by host and device
  139. };
  140. enum class library_data_t : unsigned char
  141. {
  142. real_float = 0,
  143. complex_float,
  144. real_double,
  145. complex_double,
  146. real_half,
  147. complex_half,
  148. real_bfloat16,
  149. complex_bfloat16,
  150. real_int4,
  151. complex_int4,
  152. real_uint4,
  153. complex_uint4,
  154. real_int8,
  155. complex_int8,
  156. real_uint8,
  157. complex_uint8,
  158. real_int16,
  159. complex_int16,
  160. real_uint16,
  161. complex_uint16,
  162. real_int32,
  163. complex_int32,
  164. real_uint32,
  165. complex_uint32,
  166. real_int64,
  167. complex_int64,
  168. real_uint64,
  169. complex_uint64,
  170. real_int8_4,
  171. real_int8_32,
  172. real_uint8_4,
  173. library_data_t_size
  174. };
  175. template <typename T>
  176. struct DataType
  177. {
  178. using T2 = T;
  179. };
  180. template <typename T>
  181. struct DataType<sycl::vec<T, 2>>
  182. {
  183. using T2 = std::complex<T>;
  184. };
  185. static void destroy_event(event_ptr event)
  186. {
  187. delete event;
  188. }
  189. static inline unsigned int get_tid()
  190. {
  191. #if defined(__linux__)
  192. return syscall(SYS_gettid);
  193. #elif defined(_WIN64)
  194. return GetCurrentThreadId();
  195. #else
  196. #error "Only support Windows and Linux."
  197. #endif
  198. }
  199. namespace detail
  200. {
  201. static void get_version(const sycl::device &dev, int &major, int &minor)
  202. {
  203. // Version string has the following format:
  204. // a. OpenCL<space><major.minor><space><vendor-specific-information>
  205. // b. <major.minor>
  206. // c. <AmdGcnArchName> e.g gfx1030
  207. std::string ver;
  208. ver = dev.get_info<sycl::info::device::version>();
  209. std::string::size_type i = 0;
  210. while (i < ver.size()) {
  211. if (isdigit(ver[i]))
  212. break;
  213. i++;
  214. }
  215. major = std::stoi(&(ver[i]));
  216. while (i < ver.size()) {
  217. if (ver[i] == '.')
  218. break;
  219. i++;
  220. }
  221. if (i < ver.size()) {
  222. // a. and b.
  223. i++;
  224. minor = std::stoi(&(ver[i]));
  225. } else {
  226. // c.
  227. minor = 0;
  228. }
  229. }
  230. template <typename tag, typename T>
  231. class generic_error_type
  232. {
  233. public:
  234. generic_error_type() = default;
  235. generic_error_type(T value) : value{value} {}
  236. operator T() const { return value; }
  237. private:
  238. T value;
  239. };
  240. } // namespace detail
  241. /// Pitched 2D/3D memory data.
  242. class pitched_data
  243. {
  244. public:
  245. pitched_data() : pitched_data(nullptr, 0, 0, 0) {}
  246. pitched_data(void *data, size_t pitch, size_t x, size_t y)
  247. : _data(data), _pitch(pitch), _x(x), _y(y) {}
  248. void *get_data_ptr() { return _data; }
  249. void set_data_ptr(void *data) { _data = data; }
  250. size_t get_pitch() { return _pitch; }
  251. void set_pitch(size_t pitch) { _pitch = pitch; }
  252. size_t get_x() { return _x; }
  253. void set_x(size_t x) { _x = x; };
  254. size_t get_y() { return _y; }
  255. void set_y(size_t y) { _y = y; }
  256. private:
  257. void *_data;
  258. size_t _pitch, _x, _y;
  259. };
  260. class device_info
  261. {
  262. public:
  263. // get interface
  264. const char *get_name() const { return _name; }
  265. char *get_name() { return _name; }
  266. template <typename WorkItemSizesTy = sycl::range<3>,
  267. std::enable_if_t<std::is_same_v<WorkItemSizesTy, sycl::range<3>> ||
  268. std::is_same_v<WorkItemSizesTy, int *>,
  269. int> = 0>
  270. auto get_max_work_item_sizes() const
  271. {
  272. if constexpr (std::is_same_v<WorkItemSizesTy, sycl::range<3>>)
  273. return sycl::range<3>(_max_work_item_sizes_i[0],
  274. _max_work_item_sizes_i[1],
  275. _max_work_item_sizes_i[2]);
  276. else
  277. {
  278. return _max_work_item_sizes_i;
  279. }
  280. }
  281. template <typename WorkItemSizesTy = sycl::range<3>,
  282. std::enable_if_t<std::is_same_v<WorkItemSizesTy, sycl::range<3>> ||
  283. std::is_same_v<WorkItemSizesTy, int *>,
  284. int> = 0>
  285. auto get_max_work_item_sizes()
  286. {
  287. if constexpr (std::is_same_v<WorkItemSizesTy, sycl::range<3>>)
  288. return sycl::range<3>(_max_work_item_sizes_i[0],
  289. _max_work_item_sizes_i[1],
  290. _max_work_item_sizes_i[2]);
  291. else
  292. {
  293. return _max_work_item_sizes_i;
  294. }
  295. }
  296. bool get_host_unified_memory() const { return _host_unified_memory; }
  297. int get_major_version() const { return _major; }
  298. int get_minor_version() const { return _minor; }
  299. int get_integrated() const { return _integrated; }
  300. int get_max_clock_frequency() const { return _frequency; }
  301. int get_max_compute_units() const { return _max_compute_units; }
  302. int get_max_work_group_size() const { return _max_work_group_size; }
  303. int get_max_sub_group_size() const { return _max_sub_group_size; }
  304. int get_max_work_items_per_compute_unit() const
  305. {
  306. return _max_work_items_per_compute_unit;
  307. }
  308. int get_max_register_size_per_work_group() const
  309. {
  310. return _max_register_size_per_work_group;
  311. }
  312. template <typename NDRangeSizeTy = size_t *,
  313. std::enable_if_t<std::is_same_v<NDRangeSizeTy, size_t *> ||
  314. std::is_same_v<NDRangeSizeTy, int *>,
  315. int> = 0>
  316. auto get_max_nd_range_size() const
  317. {
  318. if constexpr (std::is_same_v<NDRangeSizeTy, size_t *>)
  319. return _max_nd_range_size;
  320. else
  321. return _max_nd_range_size_i;
  322. }
  323. template <typename NDRangeSizeTy = size_t *,
  324. std::enable_if_t<std::is_same_v<NDRangeSizeTy, size_t *> ||
  325. std::is_same_v<NDRangeSizeTy, int *>,
  326. int> = 0>
  327. auto get_max_nd_range_size()
  328. {
  329. if constexpr (std::is_same_v<NDRangeSizeTy, size_t *>)
  330. return _max_nd_range_size;
  331. else
  332. return _max_nd_range_size_i;
  333. }
  334. size_t get_global_mem_size() const { return _global_mem_size; }
  335. size_t get_local_mem_size() const { return _local_mem_size; }
  336. size_t get_max_mem_alloc_size() const { return _max_mem_alloc_size; }
  337. /// Returns the maximum clock rate of device's global memory in kHz. If
  338. /// compiler does not support this API then returns default value 3200000 kHz.
  339. unsigned int get_memory_clock_rate() const { return _memory_clock_rate; }
  340. /// Returns the maximum bus width between device and memory in bits. If
  341. /// compiler does not support this API then returns default value 64 bits.
  342. unsigned int get_memory_bus_width() const { return _memory_bus_width; }
  343. uint32_t get_device_id() const { return _device_id; }
  344. std::array<unsigned char, 16> get_uuid() const { return _uuid; }
  345. /// Returns global memory cache size in bytes.
  346. unsigned int get_global_mem_cache_size() const
  347. {
  348. return _global_mem_cache_size;
  349. }
  350. // set interface
  351. void set_name(const char *name)
  352. {
  353. size_t length = strlen(name);
  354. if (length < 256)
  355. {
  356. std::memcpy(_name, name, length + 1);
  357. }
  358. else
  359. {
  360. std::memcpy(_name, name, 255);
  361. _name[255] = '\0';
  362. }
  363. }
  364. void set_max_work_item_sizes(const sycl::range<3> max_work_item_sizes)
  365. {
  366. for (int i = 0; i < 3; ++i)
  367. _max_work_item_sizes_i[i] = max_work_item_sizes[i];
  368. }
  369. [[deprecated]] void
  370. set_max_work_item_sizes(const sycl::id<3> max_work_item_sizes)
  371. {
  372. for (int i = 0; i < 3; ++i)
  373. {
  374. _max_work_item_sizes_i[i] = max_work_item_sizes[i];
  375. }
  376. }
  377. void set_host_unified_memory(bool host_unified_memory)
  378. {
  379. _host_unified_memory = host_unified_memory;
  380. }
  381. void set_major_version(int major) { _major = major; }
  382. void set_minor_version(int minor) { _minor = minor; }
  383. void set_integrated(int integrated) { _integrated = integrated; }
  384. void set_max_clock_frequency(int frequency) { _frequency = frequency; }
  385. void set_max_compute_units(int max_compute_units)
  386. {
  387. _max_compute_units = max_compute_units;
  388. }
  389. void set_global_mem_size(size_t global_mem_size)
  390. {
  391. _global_mem_size = global_mem_size;
  392. }
  393. void set_local_mem_size(size_t local_mem_size)
  394. {
  395. _local_mem_size = local_mem_size;
  396. }
  397. void set_max_mem_alloc_size(size_t max_mem_alloc_size)
  398. {
  399. _max_mem_alloc_size = max_mem_alloc_size;
  400. }
  401. void set_max_work_group_size(int max_work_group_size)
  402. {
  403. _max_work_group_size = max_work_group_size;
  404. }
  405. void set_max_sub_group_size(int max_sub_group_size)
  406. {
  407. _max_sub_group_size = max_sub_group_size;
  408. }
  409. void
  410. set_max_work_items_per_compute_unit(int max_work_items_per_compute_unit)
  411. {
  412. _max_work_items_per_compute_unit = max_work_items_per_compute_unit;
  413. }
  414. void set_max_nd_range_size(int max_nd_range_size[])
  415. {
  416. for (int i = 0; i < 3; i++)
  417. {
  418. _max_nd_range_size[i] = max_nd_range_size[i];
  419. _max_nd_range_size_i[i] = max_nd_range_size[i];
  420. }
  421. }
  422. void set_memory_clock_rate(unsigned int memory_clock_rate)
  423. {
  424. _memory_clock_rate = memory_clock_rate;
  425. }
  426. void set_memory_bus_width(unsigned int memory_bus_width)
  427. {
  428. _memory_bus_width = memory_bus_width;
  429. }
  430. void
  431. set_max_register_size_per_work_group(int max_register_size_per_work_group)
  432. {
  433. _max_register_size_per_work_group = max_register_size_per_work_group;
  434. }
  435. void set_device_id(uint32_t device_id)
  436. {
  437. _device_id = device_id;
  438. }
  439. void set_uuid(std::array<unsigned char, 16> uuid)
  440. {
  441. _uuid = std::move(uuid);
  442. }
  443. void set_global_mem_cache_size(unsigned int global_mem_cache_size)
  444. {
  445. _global_mem_cache_size = global_mem_cache_size;
  446. }
  447. private:
  448. char _name[256];
  449. int _max_work_item_sizes_i[3];
  450. bool _host_unified_memory = false;
  451. int _major;
  452. int _minor;
  453. int _integrated = 0;
  454. int _frequency;
  455. // Set estimated value 3200000 kHz as default value.
  456. unsigned int _memory_clock_rate = 3200000;
  457. // Set estimated value 64 bits as default value.
  458. unsigned int _memory_bus_width = 64;
  459. unsigned int _global_mem_cache_size;
  460. int _max_compute_units;
  461. int _max_work_group_size;
  462. int _max_sub_group_size;
  463. int _max_work_items_per_compute_unit;
  464. int _max_register_size_per_work_group;
  465. size_t _global_mem_size;
  466. size_t _local_mem_size;
  467. size_t _max_mem_alloc_size;
  468. size_t _max_nd_range_size[3];
  469. int _max_nd_range_size_i[3];
  470. uint32_t _device_id;
  471. std::array<unsigned char, 16> _uuid;
  472. };
  473. static int get_major_version(const sycl::device &dev)
  474. {
  475. int major, minor;
  476. detail::get_version(dev, major, minor);
  477. return major;
  478. }
  479. static int get_minor_version(const sycl::device &dev)
  480. {
  481. int major, minor;
  482. detail::get_version(dev, major, minor);
  483. return minor;
  484. }
  485. static void get_device_info(device_info &out, const sycl::device &dev)
  486. {
  487. device_info prop;
  488. prop.set_name(dev.get_info<sycl::info::device::name>().c_str());
  489. int major, minor;
  490. detail::get_version(dev, major, minor);
  491. prop.set_major_version(major);
  492. prop.set_minor_version(minor);
  493. prop.set_max_work_item_sizes(
  494. #if (__SYCL_COMPILER_VERSION && __SYCL_COMPILER_VERSION < 20220902)
  495. // oneAPI DPC++ compiler older than 2022/09/02, where max_work_item_sizes
  496. // is an enum class element
  497. dev.get_info<sycl::info::device::max_work_item_sizes>());
  498. #else
  499. // SYCL 2020-conformant code, max_work_item_sizes is a struct templated by
  500. // an int
  501. dev.get_info<sycl::info::device::max_work_item_sizes<3>>());
  502. #endif
  503. prop.set_host_unified_memory(dev.has(sycl::aspect::usm_host_allocations));
  504. prop.set_max_clock_frequency(
  505. dev.get_info<sycl::info::device::max_clock_frequency>() * 1000);
  506. prop.set_max_compute_units(
  507. dev.get_info<sycl::info::device::max_compute_units>());
  508. prop.set_max_work_group_size(
  509. dev.get_info<sycl::info::device::max_work_group_size>());
  510. prop.set_global_mem_size(dev.get_info<sycl::info::device::global_mem_size>());
  511. prop.set_local_mem_size(dev.get_info<sycl::info::device::local_mem_size>());
  512. prop.set_max_mem_alloc_size(dev.get_info<sycl::info::device::max_mem_alloc_size>());
  513. #if (defined(SYCL_EXT_INTEL_DEVICE_INFO) && SYCL_EXT_INTEL_DEVICE_INFO >= 6)
  514. if (dev.has(sycl::aspect::ext_intel_memory_clock_rate))
  515. {
  516. unsigned int tmp =
  517. dev.get_info<sycl::ext::intel::info::device::memory_clock_rate>();
  518. if (tmp != 0)
  519. prop.set_memory_clock_rate(1000 * tmp);
  520. }
  521. if (dev.has(sycl::aspect::ext_intel_memory_bus_width))
  522. {
  523. prop.set_memory_bus_width(
  524. dev.get_info<sycl::ext::intel::info::device::memory_bus_width>());
  525. }
  526. if (dev.has(sycl::aspect::ext_intel_device_id))
  527. {
  528. prop.set_device_id(
  529. dev.get_info<sycl::ext::intel::info::device::device_id>());
  530. }
  531. if (dev.has(sycl::aspect::ext_intel_device_info_uuid))
  532. {
  533. prop.set_uuid(dev.get_info<sycl::ext::intel::info::device::uuid>());
  534. }
  535. #elif defined(_MSC_VER) && !defined(__clang__)
  536. #pragma message("get_device_info: querying memory_clock_rate and \
  537. memory_bus_width are not supported by the compiler used. \
  538. Use 3200000 kHz as memory_clock_rate default value. \
  539. Use 64 bits as memory_bus_width default value.")
  540. #else
  541. #warning "get_device_info: querying memory_clock_rate and \
  542. memory_bus_width are not supported by the compiler used. \
  543. Use 3200000 kHz as memory_clock_rate default value. \
  544. Use 64 bits as memory_bus_width default value."
  545. #endif
  546. size_t max_sub_group_size = 1;
  547. std::vector<size_t> sub_group_sizes =
  548. dev.get_info<sycl::info::device::sub_group_sizes>();
  549. for (const auto &sub_group_size : sub_group_sizes)
  550. {
  551. if (max_sub_group_size < sub_group_size)
  552. max_sub_group_size = sub_group_size;
  553. }
  554. prop.set_max_sub_group_size(max_sub_group_size);
  555. prop.set_max_work_items_per_compute_unit(
  556. dev.get_info<sycl::info::device::max_work_group_size>());
  557. int max_nd_range_size[] = {0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF};
  558. prop.set_max_nd_range_size(max_nd_range_size);
  559. // Estimates max register size per work group, feel free to update the value
  560. // according to device properties.
  561. prop.set_max_register_size_per_work_group(65536);
  562. prop.set_global_mem_cache_size(
  563. dev.get_info<sycl::info::device::global_mem_cache_size>());
  564. out = prop;
  565. }
  566. /// dpct device extension
  567. class device_ext : public sycl::device
  568. {
  569. typedef std::mutex mutex_type;
  570. public:
  571. device_ext() : sycl::device(), _ctx(*this) {}
  572. ~device_ext()
  573. {
  574. std::lock_guard<mutex_type> lock(m_mutex);
  575. clear_queues();
  576. }
  577. device_ext(const sycl::device &base) : sycl::device(base), _ctx(*this)
  578. {
  579. std::lock_guard<mutex_type> lock(m_mutex);
  580. init_queues();
  581. }
  582. int is_native_atomic_supported() { return 0; }
  583. int get_major_version() const
  584. {
  585. return dpct::get_major_version(*this);
  586. }
  587. int get_minor_version() const
  588. {
  589. return dpct::get_minor_version(*this);
  590. }
  591. int get_max_compute_units() const
  592. {
  593. return get_device_info().get_max_compute_units();
  594. }
  595. /// Return the maximum clock frequency of this device in KHz.
  596. int get_max_clock_frequency() const
  597. {
  598. return get_device_info().get_max_clock_frequency();
  599. }
  600. int get_integrated() const { return get_device_info().get_integrated(); }
  601. int get_max_sub_group_size() const
  602. {
  603. return get_device_info().get_max_sub_group_size();
  604. }
  605. int get_max_register_size_per_work_group() const
  606. {
  607. return get_device_info().get_max_register_size_per_work_group();
  608. }
  609. int get_max_work_group_size() const
  610. {
  611. return get_device_info().get_max_work_group_size();
  612. }
  613. int get_mem_base_addr_align() const
  614. {
  615. return get_info<sycl::info::device::mem_base_addr_align>();
  616. }
  617. size_t get_global_mem_size() const
  618. {
  619. return get_device_info().get_global_mem_size();
  620. }
  621. size_t get_max_mem_alloc_size() const
  622. {
  623. return get_device_info().get_max_mem_alloc_size();
  624. }
  625. /// Get the number of bytes of free and total memory on the SYCL device.
  626. /// \param [out] free_memory The number of bytes of free memory on the SYCL device.
  627. /// \param [out] total_memory The number of bytes of total memory on the SYCL device.
  628. void get_memory_info(size_t &free_memory, size_t &total_memory)
  629. {
  630. total_memory = get_device_info().get_global_mem_size();
  631. const char *warning_info = "get_memory_info: [warning] ext_intel_free_memory is not "
  632. "supported (export/set ZES_ENABLE_SYSMAN=1 to support), "
  633. "use total memory as free memory";
  634. #if (defined(__SYCL_COMPILER_VERSION) && __SYCL_COMPILER_VERSION >= 20221105)
  635. if (!has(sycl::aspect::ext_intel_free_memory))
  636. {
  637. std::cerr << warning_info << std::endl;
  638. free_memory = total_memory;
  639. }
  640. else
  641. {
  642. free_memory = get_info<sycl::ext::intel::info::device::free_memory>();
  643. }
  644. #else
  645. std::cerr << warning_info << std::endl;
  646. free_memory = total_memory;
  647. #if defined(_MSC_VER) && !defined(__clang__)
  648. #pragma message("Querying the number of bytes of free memory is not supported")
  649. #else
  650. #warning "Querying the number of bytes of free memory is not supported"
  651. #endif
  652. #endif
  653. }
  654. void get_device_info(device_info &out) const
  655. {
  656. dpct::get_device_info(out, *this);
  657. }
  658. device_info get_device_info() const
  659. {
  660. device_info prop;
  661. dpct::get_device_info(prop, *this);
  662. return prop;
  663. }
  664. void reset()
  665. {
  666. std::lock_guard<mutex_type> lock(m_mutex);
  667. clear_queues();
  668. init_queues();
  669. }
  670. sycl::queue &in_order_queue() { return *_q_in_order; }
  671. sycl::queue &out_of_order_queue() { return *_q_out_of_order; }
  672. sycl::queue &default_queue()
  673. {
  674. return in_order_queue();
  675. }
  676. void queues_wait_and_throw()
  677. {
  678. std::unique_lock<mutex_type> lock(m_mutex);
  679. std::vector<std::shared_ptr<sycl::queue>> current_queues(
  680. _queues);
  681. lock.unlock();
  682. for (const auto &q : current_queues)
  683. {
  684. q->wait_and_throw();
  685. }
  686. // Guard the destruct of current_queues to make sure the ref count is safe.
  687. lock.lock();
  688. }
  689. sycl::queue *create_queue(bool enable_exception_handler = false)
  690. {
  691. return create_in_order_queue(enable_exception_handler);
  692. }
  693. sycl::queue *create_queue(sycl::context context, sycl::device device,
  694. bool enable_exception_handler = false) {
  695. return create_in_order_queue(context, device, enable_exception_handler);
  696. }
  697. sycl::queue *create_in_order_queue(bool enable_exception_handler = false) {
  698. std::lock_guard<mutex_type> lock(m_mutex);
  699. return create_queue_impl(enable_exception_handler,
  700. sycl::property::queue::in_order());
  701. }
  702. sycl::queue *create_in_order_queue(sycl::context context, sycl::device device,
  703. bool enable_exception_handler = false) {
  704. std::lock_guard<mutex_type> lock(m_mutex);
  705. return create_queue_impl(context, device, enable_exception_handler,
  706. sycl::property::queue::in_order());
  707. }
  708. sycl::queue *create_out_of_order_queue(bool enable_exception_handler = false) {
  709. std::lock_guard<mutex_type> lock(m_mutex);
  710. return create_queue_impl(enable_exception_handler);
  711. }
  712. void destroy_queue(sycl::queue *&queue)
  713. {
  714. std::lock_guard<mutex_type> lock(m_mutex);
  715. _queues.erase(std::remove_if(_queues.begin(), _queues.end(),
  716. [=](const std::shared_ptr<sycl::queue> &q) -> bool
  717. {
  718. return q.get() == queue;
  719. }),
  720. _queues.end());
  721. queue = nullptr;
  722. }
  723. void set_saved_queue(sycl::queue *q)
  724. {
  725. std::lock_guard<mutex_type> lock(m_mutex);
  726. _saved_queue = q;
  727. }
  728. sycl::queue *get_saved_queue() const
  729. {
  730. std::lock_guard<mutex_type> lock(m_mutex);
  731. return _saved_queue;
  732. }
  733. sycl::context get_context() const { return _ctx; }
  734. private:
  735. void clear_queues()
  736. {
  737. _queues.clear();
  738. _q_in_order = _q_out_of_order = _saved_queue = nullptr;
  739. }
  740. void init_queues()
  741. {
  742. _q_in_order = create_queue_impl(true, sycl::property::queue::in_order());
  743. _q_out_of_order = create_queue_impl(true);
  744. _saved_queue = &default_queue();
  745. }
  746. /// Caller should acquire resource \p m_mutex before calling this function.
  747. template <class... Properties>
  748. sycl::queue *create_queue_impl(bool enable_exception_handler,
  749. Properties... properties)
  750. {
  751. sycl::async_handler eh = {};
  752. if (enable_exception_handler)
  753. {
  754. eh = exception_handler;
  755. }
  756. _queues.push_back(std::make_shared<sycl::queue>(
  757. _ctx, *this, eh,
  758. sycl::property_list(
  759. #ifdef DPCT_PROFILING_ENABLED
  760. sycl::property::queue::enable_profiling(),
  761. #endif
  762. properties...)));
  763. return _queues.back().get();
  764. }
  765. template <class... Properties>
  766. sycl::queue *create_queue_impl(sycl::context context, sycl::device device,
  767. bool enable_exception_handler,
  768. Properties... properties) {
  769. sycl::async_handler eh = {};
  770. if (enable_exception_handler) {
  771. eh = exception_handler;
  772. }
  773. _queues.push_back(std::make_shared<sycl::queue>(
  774. context, device, eh,
  775. sycl::property_list(
  776. #ifdef DPCT_PROFILING_ENABLED
  777. sycl::property::queue::enable_profiling(),
  778. #endif
  779. properties...)));
  780. return _queues.back().get();
  781. }
  782. void get_version(int &major, int &minor) const
  783. {
  784. detail::get_version(*this, major, minor);
  785. }
  786. sycl::queue *_q_in_order, *_q_out_of_order;
  787. sycl::queue *_saved_queue;
  788. sycl::context _ctx;
  789. std::vector<std::shared_ptr<sycl::queue>> _queues;
  790. mutable mutex_type m_mutex;
  791. };
  792. /// device manager
  793. class dev_mgr
  794. {
  795. public:
  796. device_ext &current_device()
  797. {
  798. unsigned int dev_id = current_device_id();
  799. check_id(dev_id);
  800. return *_devs[dev_id];
  801. }
  802. device_ext &cpu_device() const
  803. {
  804. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  805. if (_cpu_device == -1)
  806. {
  807. throw std::runtime_error("no valid cpu device");
  808. }
  809. else
  810. {
  811. return *_devs[_cpu_device];
  812. }
  813. }
  814. device_ext &get_device(unsigned int id) const
  815. {
  816. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  817. check_id(id);
  818. return *_devs[id];
  819. }
  820. unsigned int current_device_id() const
  821. {
  822. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  823. auto it = _thread2dev_map.find(get_tid());
  824. if (it != _thread2dev_map.end())
  825. return it->second;
  826. return DEFAULT_DEVICE_ID;
  827. }
  828. /// Select device with a device ID.
  829. /// \param [in] id The id of the device which can
  830. /// be obtained through get_device_id(const sycl::device).
  831. void select_device(unsigned int id)
  832. {
  833. std::lock_guard<std::recursive_mutex> lock(m_mutex);
  834. check_id(id);
  835. _thread2dev_map[get_tid()] = id;
  836. }
  837. unsigned int device_count() { return _devs.size(); }
  838. unsigned int get_device_id(const sycl::device &dev)
  839. {
  840. unsigned int id = 0;
  841. for (auto dev_item : _devs)
  842. {
  843. if (*dev_item == dev)
  844. {
  845. break;
  846. }
  847. id++;
  848. }
  849. return id;
  850. }
  851. template <class DeviceSelector>
  852. std::enable_if_t<
  853. std::is_invocable_r_v<int, DeviceSelector, const sycl::device &>>
  854. select_device(const DeviceSelector &selector = sycl::gpu_selector_v)
  855. {
  856. sycl::device selected_device = sycl::device(selector);
  857. unsigned int selected_device_id = get_device_id(selected_device);
  858. select_device(selected_device_id);
  859. }
  860. /// Returns the instance of device manager singleton.
  861. static dev_mgr &instance()
  862. {
  863. static dev_mgr d_m;
  864. return d_m;
  865. }
  866. dev_mgr(const dev_mgr &) = delete;
  867. dev_mgr &operator=(const dev_mgr &) = delete;
  868. dev_mgr(dev_mgr &&) = delete;
  869. dev_mgr &operator=(dev_mgr &&) = delete;
  870. private:
  871. mutable std::recursive_mutex m_mutex;
  872. static bool compare_dev(sycl::device &device1, sycl::device &device2)
  873. {
  874. dpct::device_info prop1;
  875. dpct::get_device_info(prop1, device1);
  876. dpct::device_info prop2;
  877. dpct::get_device_info(prop2, device2);
  878. return prop1.get_max_compute_units() > prop2.get_max_compute_units();
  879. }
  880. static int convert_backend_index(std::string & backend) {
  881. if (backend == "ext_oneapi_level_zero:gpu") return 0;
  882. if (backend == "opencl:gpu") return 1;
  883. if (backend == "ext_oneapi_cuda:gpu") return 2;
  884. if (backend == "ext_oneapi_hip:gpu") return 3;
  885. if (backend == "opencl:cpu") return 4;
  886. if (backend == "opencl:acc") return 5;
  887. printf("convert_backend_index: can't handle backend=%s\n", backend.c_str());
  888. GGML_ASSERT(false);
  889. }
  890. static bool compare_backend(std::string &backend1, std::string &backend2) {
  891. return convert_backend_index(backend1) < convert_backend_index(backend2);
  892. }
  893. dev_mgr()
  894. {
  895. sycl::device default_device =
  896. sycl::device(sycl::default_selector_v);
  897. _devs.push_back(std::make_shared<device_ext>(default_device));
  898. std::vector<sycl::device> sycl_all_devs;
  899. // Collect other devices except for the default device.
  900. if (default_device.is_cpu())
  901. _cpu_device = 0;
  902. auto Platforms = sycl::platform::get_platforms();
  903. // Keep track of the number of devices per backend
  904. std::map<sycl::backend, size_t> DeviceNums;
  905. std::map<std::string, std::vector<sycl::device>> backend_devices;
  906. while (!Platforms.empty()) {
  907. auto Platform = Platforms.back();
  908. Platforms.pop_back();
  909. auto devices = Platform.get_devices();
  910. std::string backend_type = get_device_backend_and_type(devices[0]);
  911. for (const auto &device : devices) {
  912. backend_devices[backend_type].push_back(device);
  913. }
  914. }
  915. std::vector<std::string> keys;
  916. for(auto it = backend_devices.begin(); it != backend_devices.end(); ++it) {
  917. keys.push_back(it->first);
  918. }
  919. std::sort(keys.begin(), keys.end(), compare_backend);
  920. for (auto &key : keys) {
  921. std::vector<sycl::device> devs = backend_devices[key];
  922. std::sort(devs.begin(), devs.end(), compare_dev);
  923. for (const auto &dev : devs) {
  924. sycl_all_devs.push_back(dev);
  925. }
  926. }
  927. for (auto &dev : sycl_all_devs)
  928. {
  929. if (dev == default_device)
  930. {
  931. continue;
  932. }
  933. _devs.push_back(std::make_shared<device_ext>(dev));
  934. if (_cpu_device == -1 && dev.is_cpu())
  935. {
  936. _cpu_device = _devs.size() - 1;
  937. }
  938. }
  939. }
  940. void check_id(unsigned int id) const
  941. {
  942. if (id >= _devs.size())
  943. {
  944. throw std::runtime_error("invalid device id");
  945. }
  946. }
  947. std::vector<std::shared_ptr<device_ext>> _devs;
  948. /// DEFAULT_DEVICE_ID is used, if current_device_id() can not find current
  949. /// thread id in _thread2dev_map, which means default device should be used
  950. /// for the current thread.
  951. const unsigned int DEFAULT_DEVICE_ID = 0;
  952. /// thread-id to device-id map.
  953. std::map<unsigned int, unsigned int> _thread2dev_map;
  954. int _cpu_device = -1;
  955. };
  956. static inline sycl::queue &get_default_queue()
  957. {
  958. return dev_mgr::instance().current_device().default_queue();
  959. }
  960. namespace detail
  961. {
  962. enum class pointer_access_attribute
  963. {
  964. host_only = 0,
  965. device_only,
  966. host_device,
  967. end
  968. };
  969. static pointer_access_attribute get_pointer_attribute(sycl::queue &q,
  970. const void *ptr)
  971. {
  972. switch (sycl::get_pointer_type(ptr, q.get_context()))
  973. {
  974. case sycl::usm::alloc::unknown:
  975. return pointer_access_attribute::host_only;
  976. case sycl::usm::alloc::device:
  977. return pointer_access_attribute::device_only;
  978. case sycl::usm::alloc::shared:
  979. case sycl::usm::alloc::host:
  980. return pointer_access_attribute::host_device;
  981. }
  982. }
  983. template <typename ArgT>
  984. inline constexpr std::uint64_t get_type_combination_id(ArgT Val)
  985. {
  986. static_assert((unsigned char)library_data_t::library_data_t_size <=
  987. std::numeric_limits<unsigned char>::max() &&
  988. "library_data_t size exceeds limit.");
  989. static_assert(std::is_same_v<ArgT, library_data_t>, "Unsupported ArgT");
  990. return (std::uint64_t)Val;
  991. }
  992. template <typename FirstT, typename... RestT>
  993. inline constexpr std::uint64_t get_type_combination_id(FirstT FirstVal,
  994. RestT... RestVal)
  995. {
  996. static_assert((std::uint8_t)library_data_t::library_data_t_size <=
  997. std::numeric_limits<unsigned char>::max() &&
  998. "library_data_t size exceeds limit.");
  999. static_assert(sizeof...(RestT) <= 8 && "Too many parameters");
  1000. static_assert(std::is_same_v<FirstT, library_data_t>, "Unsupported FirstT");
  1001. return get_type_combination_id(RestVal...) << 8 | ((std::uint64_t)FirstVal);
  1002. }
  1003. class mem_mgr
  1004. {
  1005. mem_mgr()
  1006. {
  1007. // Reserved address space, no real memory allocation happens here.
  1008. #if defined(__linux__)
  1009. mapped_address_space =
  1010. (byte_t *)mmap(nullptr, mapped_region_size, PROT_NONE,
  1011. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  1012. #elif defined(_WIN64)
  1013. mapped_address_space = (byte_t *)VirtualAlloc(
  1014. NULL, // NULL specified as the base address parameter
  1015. mapped_region_size, // Size of allocation
  1016. MEM_RESERVE, // Allocate reserved pages
  1017. PAGE_NOACCESS); // Protection = no access
  1018. #else
  1019. #error "Only support Windows and Linux."
  1020. #endif
  1021. next_free = mapped_address_space;
  1022. };
  1023. public:
  1024. using buffer_id_t = int;
  1025. struct allocation
  1026. {
  1027. buffer_t buffer;
  1028. byte_t *alloc_ptr;
  1029. size_t size;
  1030. };
  1031. ~mem_mgr()
  1032. {
  1033. #if defined(__linux__)
  1034. munmap(mapped_address_space, mapped_region_size);
  1035. #elif defined(_WIN64)
  1036. VirtualFree(mapped_address_space, 0, MEM_RELEASE);
  1037. #else
  1038. #error "Only support Windows and Linux."
  1039. #endif
  1040. };
  1041. mem_mgr(const mem_mgr &) = delete;
  1042. mem_mgr &operator=(const mem_mgr &) = delete;
  1043. mem_mgr(mem_mgr &&) = delete;
  1044. mem_mgr &operator=(mem_mgr &&) = delete;
  1045. /// Allocate
  1046. void *mem_alloc(size_t size)
  1047. {
  1048. if (!size)
  1049. return nullptr;
  1050. std::lock_guard<std::mutex> lock(m_mutex);
  1051. if (next_free + size > mapped_address_space + mapped_region_size)
  1052. {
  1053. throw std::runtime_error("dpct_malloc: out of memory for virtual memory pool");
  1054. }
  1055. // Allocation
  1056. sycl::range<1> r(size);
  1057. buffer_t buf(r);
  1058. allocation A{buf, next_free, size};
  1059. // Map allocation to device pointer
  1060. void *result = next_free;
  1061. m_map.emplace(next_free + size, A);
  1062. // Update pointer to the next free space.
  1063. next_free += (size + extra_padding + alignment - 1) & ~(alignment - 1);
  1064. return result;
  1065. }
  1066. /// Deallocate
  1067. void mem_free(const void *ptr)
  1068. {
  1069. if (!ptr)
  1070. return;
  1071. std::lock_guard<std::mutex> lock(m_mutex);
  1072. auto it = get_map_iterator(ptr);
  1073. m_map.erase(it);
  1074. }
  1075. /// map: device pointer -> allocation(buffer, alloc_ptr, size)
  1076. allocation translate_ptr(const void *ptr)
  1077. {
  1078. std::lock_guard<std::mutex> lock(m_mutex);
  1079. auto it = get_map_iterator(ptr);
  1080. return it->second;
  1081. }
  1082. /// Check if the pointer represents device pointer or not.
  1083. bool is_device_ptr(const void *ptr) const
  1084. {
  1085. std::lock_guard<std::mutex> lock(m_mutex);
  1086. return (mapped_address_space <= ptr) &&
  1087. (ptr < mapped_address_space + mapped_region_size);
  1088. }
  1089. /// Returns the instance of memory manager singleton.
  1090. static mem_mgr &instance()
  1091. {
  1092. static mem_mgr m;
  1093. return m;
  1094. }
  1095. private:
  1096. std::map<byte_t *, allocation> m_map;
  1097. mutable std::mutex m_mutex;
  1098. byte_t *mapped_address_space;
  1099. byte_t *next_free;
  1100. const size_t mapped_region_size = 128ull * 1024 * 1024 * 1024;
  1101. const size_t alignment = 256;
  1102. /// This padding may be defined to some positive value to debug
  1103. /// out of bound accesses.
  1104. const size_t extra_padding = 0;
  1105. std::map<byte_t *, allocation>::iterator get_map_iterator(const void *ptr)
  1106. {
  1107. auto it = m_map.upper_bound((byte_t *)ptr);
  1108. if (it == m_map.end())
  1109. {
  1110. // Not a virtual pointer.
  1111. throw std::runtime_error("can not get buffer from non-virtual pointer");
  1112. }
  1113. const allocation &alloc = it->second;
  1114. if (ptr < alloc.alloc_ptr)
  1115. {
  1116. // Out of bound.
  1117. // This may happen if there's a gap between allocations due to alignment
  1118. // or extra padding and pointer points to this gap.
  1119. throw std::runtime_error("invalid virtual pointer");
  1120. }
  1121. return it;
  1122. }
  1123. };
  1124. template <class T, memory_region Memory, size_t Dimension>
  1125. class accessor;
  1126. template <memory_region Memory, class T = byte_t>
  1127. class memory_traits
  1128. {
  1129. public:
  1130. static constexpr sycl::access::target target =
  1131. sycl::access::target::device;
  1132. static constexpr sycl::access_mode mode =
  1133. (Memory == constant) ? sycl::access_mode::read
  1134. : sycl::access_mode::read_write;
  1135. static constexpr size_t type_size = sizeof(T);
  1136. using element_t =
  1137. typename std::conditional<Memory == constant, const T, T>::type;
  1138. using value_t = typename std::remove_cv<T>::type;
  1139. template <size_t Dimension = 1>
  1140. using accessor_t = typename std::conditional<
  1141. Memory == local, sycl::local_accessor<value_t, Dimension>,
  1142. sycl::accessor<T, Dimension, mode, target>>::type;
  1143. using pointer_t = T *;
  1144. };
  1145. static inline void *dpct_malloc(size_t size, sycl::queue &q)
  1146. {
  1147. return sycl::malloc_device(size, q.get_device(), q.get_context());
  1148. }
  1149. #define PITCH_DEFAULT_ALIGN(x) (((x) + 31) & ~(0x1F))
  1150. static inline void *dpct_malloc(size_t &pitch, size_t x, size_t y, size_t z,
  1151. sycl::queue &q)
  1152. {
  1153. pitch = PITCH_DEFAULT_ALIGN(x);
  1154. return dpct_malloc(pitch * y * z, q);
  1155. }
  1156. /**
  1157. * @brief Sets \p value to the first \p size elements starting from \p dev_ptr in \p q.
  1158. * @tparam valueT The type of the element to be set.
  1159. * @param [in] q The queue in which the operation is done.
  1160. * @param [in] dev_ptr Pointer to the virtual device memory address.
  1161. * @param [in] value The value to be set.
  1162. * @param [in] size Number of elements to be set to the value.
  1163. * @return An event representing the memset operation.
  1164. */
  1165. template <typename valueT>
  1166. static inline sycl::event dpct_memset(sycl::queue &q, void *dev_ptr,
  1167. valueT value, size_t size)
  1168. {
  1169. return q.fill(dev_ptr, value, size);
  1170. }
  1171. /**
  1172. * @brief Sets \p value to the 3D memory region pointed by \p data in \p q.
  1173. * @tparam valueT The type of the element to be set.
  1174. * @param [in] q The queue in which the operation is done.
  1175. * @param [in] data Pointer to the pitched device memory region.
  1176. * @param [in] value The value to be set.
  1177. * @param [in] size 3D memory region by number of elements.
  1178. * @return An event list representing the memset operations.
  1179. */
  1180. template <typename valueT>
  1181. static inline std::vector<sycl::event>
  1182. dpct_memset(sycl::queue &q, pitched_data data, valueT value,
  1183. sycl::range<3> size)
  1184. {
  1185. std::vector<sycl::event> event_list;
  1186. size_t slice = data.get_pitch() * data.get_y();
  1187. unsigned char *data_surface = (unsigned char *)data.get_data_ptr();
  1188. for (size_t z = 0; z < size.get(2); ++z)
  1189. {
  1190. unsigned char *data_ptr = data_surface;
  1191. for (size_t y = 0; y < size.get(1); ++y)
  1192. {
  1193. event_list.push_back(dpct_memset(q, data_ptr, value, size.get(0)));
  1194. data_ptr += data.get_pitch();
  1195. }
  1196. data_surface += slice;
  1197. }
  1198. return event_list;
  1199. }
  1200. /**
  1201. * @brief Sets \p val to the pitched 2D memory region pointed by \p ptr in \p q.
  1202. * @tparam valueT The type of the element to be set.
  1203. * @param [in] q The queue in which the operation is done.
  1204. * @param [in] ptr Pointer to the virtual device memory.
  1205. * @param [in] pitch The pitch size by number of elements, including padding.
  1206. * @param [in] val The value to be set.
  1207. * @param [in] x The width of memory region by number of elements.
  1208. * @param [in] y The height of memory region by number of elements.
  1209. * @return An event list representing the memset operations.
  1210. */
  1211. template <typename valueT>
  1212. static inline std::vector<sycl::event>
  1213. dpct_memset(sycl::queue &q, void *ptr, size_t pitch, valueT val, size_t x,
  1214. size_t y)
  1215. {
  1216. return dpct_memset(q, pitched_data(ptr, pitch, x, 1), val,
  1217. sycl::range<3>(x, y, 1));
  1218. }
  1219. static memcpy_direction deduce_memcpy_direction(sycl::queue &q, void *to_ptr,
  1220. const void *from_ptr,
  1221. memcpy_direction dir)
  1222. {
  1223. switch (dir)
  1224. {
  1225. case memcpy_direction::host_to_host:
  1226. case memcpy_direction::host_to_device:
  1227. case memcpy_direction::device_to_host:
  1228. case memcpy_direction::device_to_device:
  1229. return dir;
  1230. case memcpy_direction::automatic:
  1231. {
  1232. // table[to_attribute][from_attribute]
  1233. static const memcpy_direction
  1234. direction_table[static_cast<unsigned>(pointer_access_attribute::end)]
  1235. [static_cast<unsigned>(pointer_access_attribute::end)] =
  1236. {{memcpy_direction::host_to_host,
  1237. memcpy_direction::device_to_host,
  1238. memcpy_direction::host_to_host},
  1239. {memcpy_direction::host_to_device,
  1240. memcpy_direction::device_to_device,
  1241. memcpy_direction::device_to_device},
  1242. {memcpy_direction::host_to_host,
  1243. memcpy_direction::device_to_device,
  1244. memcpy_direction::device_to_device}};
  1245. return direction_table[static_cast<unsigned>(get_pointer_attribute(
  1246. q, to_ptr))][static_cast<unsigned>(get_pointer_attribute(q, from_ptr))];
  1247. }
  1248. default:
  1249. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1250. }
  1251. }
  1252. static sycl::event
  1253. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1254. memcpy_direction direction,
  1255. const std::vector<sycl::event> &dep_events = {})
  1256. {
  1257. if (!size)
  1258. return sycl::event{};
  1259. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  1260. GGML_UNUSED(direction);
  1261. }
  1262. // Get actual copy range and make sure it will not exceed range.
  1263. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  1264. size_t pitch)
  1265. {
  1266. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  1267. }
  1268. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  1269. size_t pitch)
  1270. {
  1271. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  1272. }
  1273. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  1274. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  1275. static inline std::vector<sycl::event>
  1276. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1277. sycl::range<3> to_range, sycl::range<3> from_range,
  1278. sycl::id<3> to_id, sycl::id<3> from_id,
  1279. sycl::range<3> size, memcpy_direction direction,
  1280. const std::vector<sycl::event> &dep_events = {})
  1281. {
  1282. // RAII for host pointer
  1283. class host_buffer
  1284. {
  1285. void *_buf;
  1286. size_t _size;
  1287. sycl::queue &_q;
  1288. const std::vector<sycl::event> &_deps; // free operation depends
  1289. public:
  1290. host_buffer(size_t size, sycl::queue &q,
  1291. const std::vector<sycl::event> &deps)
  1292. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  1293. void *get_ptr() const { return _buf; }
  1294. size_t get_size() const { return _size; }
  1295. ~host_buffer()
  1296. {
  1297. if (_buf)
  1298. {
  1299. _q.submit([&](sycl::handler &cgh)
  1300. {
  1301. cgh.depends_on(_deps);
  1302. cgh.host_task([buf = _buf] { std::free(buf); }); });
  1303. }
  1304. }
  1305. };
  1306. std::vector<sycl::event> event_list;
  1307. size_t to_slice = to_range.get(1) * to_range.get(0),
  1308. from_slice = from_range.get(1) * from_range.get(0);
  1309. unsigned char *to_surface =
  1310. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  1311. const unsigned char *from_surface =
  1312. (const unsigned char *)from_ptr +
  1313. get_offset(from_id, from_slice, from_range.get(0));
  1314. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  1315. {
  1316. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  1317. direction, dep_events)};
  1318. }
  1319. direction = deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1320. size_t size_slice = size.get(1) * size.get(0);
  1321. switch (direction)
  1322. {
  1323. case host_to_host:
  1324. for (size_t z = 0; z < size.get(2); ++z)
  1325. {
  1326. unsigned char *to_ptr = to_surface;
  1327. const unsigned char *from_ptr = from_surface;
  1328. if (to_range.get(0) == from_range.get(0) &&
  1329. to_range.get(0) == size.get(0))
  1330. {
  1331. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  1332. direction, dep_events));
  1333. }
  1334. else
  1335. {
  1336. for (size_t y = 0; y < size.get(1); ++y)
  1337. {
  1338. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  1339. direction, dep_events));
  1340. to_ptr += to_range.get(0);
  1341. from_ptr += from_range.get(0);
  1342. }
  1343. }
  1344. to_surface += to_slice;
  1345. from_surface += from_slice;
  1346. }
  1347. break;
  1348. case host_to_device:
  1349. {
  1350. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  1351. event_list);
  1352. std::vector<sycl::event> host_events;
  1353. if (to_slice == size_slice)
  1354. {
  1355. // Copy host data to a temp host buffer with the shape of target.
  1356. host_events =
  1357. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  1358. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  1359. host_to_host, dep_events);
  1360. }
  1361. else
  1362. {
  1363. // Copy host data to a temp host buffer with the shape of target.
  1364. host_events = dpct_memcpy(
  1365. q, buf.get_ptr(), from_surface, to_range, from_range,
  1366. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  1367. // If has padding data, not sure whether it is useless. So fill temp
  1368. // buffer with it.
  1369. std::vector<sycl::event>{
  1370. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  1371. device_to_host, dep_events)});
  1372. }
  1373. // Copy from temp host buffer to device with only one submit.
  1374. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  1375. buf.get_size(), host_to_device,
  1376. host_events));
  1377. break;
  1378. }
  1379. case device_to_host:
  1380. {
  1381. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  1382. event_list);
  1383. // Copy from host temp buffer to host target with reshaping.
  1384. event_list = dpct_memcpy(
  1385. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  1386. sycl::id<3>(0, 0, 0), size, host_to_host,
  1387. // Copy from device to temp host buffer with only one submit.
  1388. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  1389. buf.get_size(),
  1390. device_to_host, dep_events)});
  1391. break;
  1392. }
  1393. case device_to_device:
  1394. event_list.push_back(q.submit([&](sycl::handler &cgh){
  1395. cgh.depends_on(dep_events);
  1396. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  1397. size,
  1398. [=](sycl::id<3> id) {
  1399. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  1400. from_surface[get_offset(id, from_slice, from_range.get(0))];
  1401. }); }));
  1402. break;
  1403. default:
  1404. throw std::runtime_error("dpct_memcpy: invalid direction value");
  1405. }
  1406. return event_list;
  1407. }
  1408. /// memcpy 2D/3D matrix specified by pitched_data.
  1409. static inline std::vector<sycl::event>
  1410. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  1411. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  1412. memcpy_direction direction = automatic)
  1413. {
  1414. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  1415. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  1416. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  1417. size, direction);
  1418. }
  1419. /// memcpy 2D matrix with pitch.
  1420. static inline std::vector<sycl::event>
  1421. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1422. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  1423. memcpy_direction direction = automatic)
  1424. {
  1425. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  1426. sycl::range<3>(from_pitch, y, 1),
  1427. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  1428. sycl::range<3>(x, y, 1), direction);
  1429. }
  1430. namespace deprecated
  1431. {
  1432. template <typename T, sycl::usm::alloc AllocKind>
  1433. class usm_allocator
  1434. {
  1435. private:
  1436. using Alloc = sycl::usm_allocator<T, AllocKind>;
  1437. Alloc _impl;
  1438. public:
  1439. using value_type = typename std::allocator_traits<Alloc>::value_type;
  1440. using pointer = typename std::allocator_traits<Alloc>::pointer;
  1441. using const_pointer = typename std::allocator_traits<Alloc>::const_pointer;
  1442. using void_pointer = typename std::allocator_traits<Alloc>::void_pointer;
  1443. using const_void_pointer =
  1444. typename std::allocator_traits<Alloc>::const_void_pointer;
  1445. using reference = typename std::allocator_traits<Alloc>::value_type &;
  1446. using const_reference =
  1447. const typename std::allocator_traits<Alloc>::value_type &;
  1448. using difference_type =
  1449. typename std::allocator_traits<Alloc>::difference_type;
  1450. using size_type = typename std::allocator_traits<Alloc>::size_type;
  1451. using propagate_on_container_copy_assignment = typename std::allocator_traits<
  1452. Alloc>::propagate_on_container_copy_assignment;
  1453. using propagate_on_container_move_assignment = typename std::allocator_traits<
  1454. Alloc>::propagate_on_container_move_assignment;
  1455. using propagate_on_container_swap =
  1456. typename std::allocator_traits<Alloc>::propagate_on_container_swap;
  1457. using is_always_equal =
  1458. typename std::allocator_traits<Alloc>::is_always_equal;
  1459. template <typename U>
  1460. struct rebind
  1461. {
  1462. typedef usm_allocator<U, AllocKind> other;
  1463. };
  1464. usm_allocator() : _impl(dpct::get_default_queue()) {}
  1465. ~usm_allocator() {}
  1466. usm_allocator(const usm_allocator &other) : _impl(other._impl) {}
  1467. usm_allocator(usm_allocator &&other) : _impl(std::move(other._impl)) {}
  1468. pointer address(reference r) { return &r; }
  1469. const_pointer address(const_reference r) { return &r; }
  1470. pointer allocate(size_type cnt, const_void_pointer hint = nullptr)
  1471. {
  1472. return std::allocator_traits<Alloc>::allocate(_impl, cnt, hint);
  1473. }
  1474. void deallocate(pointer p, size_type cnt)
  1475. {
  1476. std::allocator_traits<Alloc>::deallocate(_impl, p, cnt);
  1477. }
  1478. size_type max_size() const
  1479. {
  1480. return std::allocator_traits<Alloc>::max_size(_impl);
  1481. }
  1482. bool operator==(const usm_allocator &other) const { return _impl == other._impl; }
  1483. bool operator!=(const usm_allocator &other) const { return _impl != other._impl; }
  1484. };
  1485. } // namespace deprecated
  1486. inline void dpct_free(void *ptr,
  1487. const sycl::queue &q)
  1488. {
  1489. if (ptr)
  1490. {
  1491. sycl::free(ptr, q.get_context());
  1492. }
  1493. }
  1494. template <typename T>
  1495. inline auto get_memory(const void *x)
  1496. {
  1497. T *new_x = reinterpret_cast<T *>(const_cast<void *>(x));
  1498. return new_x;
  1499. }
  1500. template <typename T>
  1501. inline typename DataType<T>::T2 get_value(const T *s, sycl::queue &q)
  1502. {
  1503. using Ty = typename DataType<T>::T2;
  1504. Ty s_h;
  1505. if (get_pointer_attribute(q, s) == pointer_access_attribute::device_only)
  1506. detail::dpct_memcpy(q, (void *)&s_h, (const void *)s, sizeof(T), device_to_host)
  1507. .wait();
  1508. else
  1509. s_h = *reinterpret_cast<const Ty *>(s);
  1510. return s_h;
  1511. }
  1512. } // namespace detail
  1513. template <typename T>
  1514. inline auto get_value(const T *s, sycl::queue &q)
  1515. {
  1516. return detail::get_value(s, q);
  1517. }
  1518. namespace detail
  1519. {
  1520. template <class Ta, class Tb, class Tc, class Ts>
  1521. inline void gemm_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1522. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1523. const void *alpha, const void *a, int lda, const void *b,
  1524. int ldb, const void *beta, void *c, int ldc)
  1525. {
  1526. #ifndef __INTEL_MKL__
  1527. GGML_UNUSED(q);
  1528. GGML_UNUSED(a_trans);
  1529. GGML_UNUSED(b_trans);
  1530. GGML_UNUSED(m);
  1531. GGML_UNUSED(n);
  1532. GGML_UNUSED(k);
  1533. GGML_UNUSED(alpha);
  1534. GGML_UNUSED(a);
  1535. GGML_UNUSED(lda);
  1536. GGML_UNUSED(b);
  1537. GGML_UNUSED(ldb);
  1538. GGML_UNUSED(beta);
  1539. GGML_UNUSED(c);
  1540. GGML_UNUSED(ldc);
  1541. throw std::runtime_error("The oneAPI Math Kernel Library (oneMKL) Interfaces "
  1542. "Project does not support this API.");
  1543. #else
  1544. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1545. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1546. auto data_a = get_memory<const Ta>(a);
  1547. auto data_b = get_memory<const Tb>(b);
  1548. auto data_c = get_memory<Tc>(c);
  1549. oneapi::mkl::blas::column_major::gemm(
  1550. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1551. data_b, ldb, beta_value, data_c, ldc);
  1552. #endif
  1553. }
  1554. template <typename VecT, class BinaryOperation, class = void>
  1555. class vectorized_binary
  1556. {
  1557. public:
  1558. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1559. {
  1560. VecT v4;
  1561. for (size_t i = 0; i < v4.size(); ++i)
  1562. {
  1563. v4[i] = binary_op(a[i], b[i]);
  1564. }
  1565. return v4;
  1566. }
  1567. };
  1568. template <typename VecT, class BinaryOperation>
  1569. class vectorized_binary<
  1570. VecT, BinaryOperation,
  1571. std::void_t<std::invoke_result_t<BinaryOperation, VecT, VecT>>>
  1572. {
  1573. public:
  1574. inline VecT operator()(VecT a, VecT b, const BinaryOperation binary_op)
  1575. {
  1576. return binary_op(a, b).template as<VecT>();
  1577. }
  1578. };
  1579. template <class Ta, class Tb, class Tc, class Ts>
  1580. inline void gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1581. oneapi::mkl::transpose b_trans, int m, int n, int k,
  1582. const void *alpha, const void **a, int lda,
  1583. const void **b, int ldb, const void *beta, void **c,
  1584. int ldc, int batch_size)
  1585. {
  1586. struct matrix_info_t
  1587. {
  1588. oneapi::mkl::transpose transpose_info[2];
  1589. Ts value_info[2];
  1590. std::int64_t size_info[3];
  1591. std::int64_t ld_info[3];
  1592. std::int64_t groupsize_info;
  1593. };
  1594. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1595. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1596. matrix_info_t *matrix_info =
  1597. (matrix_info_t *)std::malloc(sizeof(matrix_info_t));
  1598. matrix_info->transpose_info[0] = a_trans;
  1599. matrix_info->transpose_info[1] = b_trans;
  1600. matrix_info->value_info[0] = alpha_value;
  1601. matrix_info->value_info[1] = beta_value;
  1602. matrix_info->size_info[0] = m;
  1603. matrix_info->size_info[1] = n;
  1604. matrix_info->size_info[2] = k;
  1605. matrix_info->ld_info[0] = lda;
  1606. matrix_info->ld_info[1] = ldb;
  1607. matrix_info->ld_info[2] = ldc;
  1608. matrix_info->groupsize_info = batch_size;
  1609. sycl::event e = oneapi::mkl::blas::column_major::gemm_batch(
  1610. q, matrix_info->transpose_info, matrix_info->transpose_info + 1,
  1611. matrix_info->size_info, matrix_info->size_info + 1,
  1612. matrix_info->size_info + 2, matrix_info->value_info,
  1613. reinterpret_cast<const Ta **>(a), matrix_info->ld_info,
  1614. reinterpret_cast<const Tb **>(b), matrix_info->ld_info + 1,
  1615. matrix_info->value_info + 1, reinterpret_cast<Tc **>(c),
  1616. matrix_info->ld_info + 2, 1, &(matrix_info->groupsize_info));
  1617. q.submit([&](sycl::handler &cgh)
  1618. {
  1619. cgh.depends_on(e);
  1620. cgh.host_task([=] { std::free(matrix_info); }); });
  1621. }
  1622. template <class Ta, class Tb, class Tc, class Ts>
  1623. inline void
  1624. gemm_batch_impl(sycl::queue &q, oneapi::mkl::transpose a_trans,
  1625. oneapi::mkl::transpose b_trans, int m, int n,
  1626. int k, const void *alpha, const void *a, int lda,
  1627. long long int stride_a, const void *b, int ldb,
  1628. long long int stride_b, const void *beta, void *c,
  1629. int ldc, long long int stride_c, int batch_size)
  1630. {
  1631. Ts alpha_value = dpct::get_value(reinterpret_cast<const Ts *>(alpha), q);
  1632. Ts beta_value = dpct::get_value(reinterpret_cast<const Ts *>(beta), q);
  1633. auto data_a = get_memory<const Ta>(a);
  1634. auto data_b = get_memory<const Tb>(b);
  1635. auto data_c = get_memory<Tc>(c);
  1636. oneapi::mkl::blas::column_major::gemm_batch(
  1637. q, a_trans, b_trans, m, n, k, alpha_value, data_a, lda,
  1638. stride_a, data_b, ldb, stride_b, beta_value,
  1639. data_c, ldc, stride_c, batch_size);
  1640. }
  1641. } // namespace detail
  1642. template <typename VecT, class BinaryOperation>
  1643. inline unsigned vectorized_binary(unsigned a, unsigned b,
  1644. const BinaryOperation binary_op)
  1645. {
  1646. sycl::vec<unsigned, 1> v0{a}, v1{b};
  1647. auto v2 = v0.as<VecT>();
  1648. auto v3 = v1.as<VecT>();
  1649. auto v4 =
  1650. detail::vectorized_binary<VecT, BinaryOperation>()(v2, v3, binary_op);
  1651. v0 = v4.template as<sycl::vec<unsigned, 1>>();
  1652. return v0;
  1653. }
  1654. static void async_dpct_memcpy(void *to_ptr, const void *from_ptr, size_t size,
  1655. memcpy_direction direction = automatic,
  1656. sycl::queue &q = dpct::get_default_queue())
  1657. {
  1658. detail::dpct_memcpy(q, to_ptr, from_ptr, size, direction);
  1659. }
  1660. static inline unsigned int select_device(unsigned int id)
  1661. {
  1662. dev_mgr::instance().select_device(id);
  1663. return id;
  1664. }
  1665. template <typename T>
  1666. T permute_sub_group_by_xor(sycl::sub_group g, T x, unsigned int mask,
  1667. unsigned int logical_sub_group_size = 32)
  1668. {
  1669. unsigned int id = g.get_local_linear_id();
  1670. unsigned int start_index =
  1671. id / logical_sub_group_size * logical_sub_group_size;
  1672. unsigned int target_offset = (id % logical_sub_group_size) ^ mask;
  1673. return sycl::select_from_group(g, x,
  1674. target_offset < logical_sub_group_size
  1675. ? start_index + target_offset
  1676. : id);
  1677. }
  1678. template <typename T>
  1679. sycl::vec<T, 4> extract_and_sign_or_zero_extend4(T val)
  1680. {
  1681. return sycl::vec<T, 1>(val)
  1682. .template as<sycl::vec<
  1683. std::conditional_t<std::is_signed_v<T>, int8_t, uint8_t>, 4>>()
  1684. .template convert<T>();
  1685. }
  1686. template <typename T1, typename T2>
  1687. using dot_product_acc_t =
  1688. std::conditional_t<std::is_unsigned_v<T1> && std::is_unsigned_v<T2>,
  1689. uint32_t, int32_t>;
  1690. template <typename T1, typename T2, typename T3>
  1691. inline auto dp4a(T1 a, T2 b, T3 c)
  1692. {
  1693. dot_product_acc_t<T1, T2> res = c;
  1694. auto va = extract_and_sign_or_zero_extend4(a);
  1695. auto vb = extract_and_sign_or_zero_extend4(b);
  1696. res += va[0] * vb[0];
  1697. res += va[1] * vb[1];
  1698. res += va[2] * vb[2];
  1699. res += va[3] * vb[3];
  1700. return res;
  1701. }
  1702. struct sub_sat
  1703. {
  1704. template <typename T>
  1705. auto operator()(const T x, const T y) const
  1706. {
  1707. return sycl::sub_sat(x, y);
  1708. }
  1709. };
  1710. template <typename S, typename T>
  1711. inline T vectorized_min(T a, T b)
  1712. {
  1713. sycl::vec<T, 1> v0{a}, v1{b};
  1714. auto v2 = v0.template as<S>();
  1715. auto v3 = v1.template as<S>();
  1716. auto v4 = sycl::min(v2, v3);
  1717. v0 = v4.template as<sycl::vec<T, 1>>();
  1718. return v0;
  1719. }
  1720. inline float pow(const float a, const int b) { return sycl::pown(a, b); }
  1721. inline double pow(const double a, const int b) { return sycl::pown(a, b); }
  1722. inline float pow(const float a, const float b) { return sycl::pow(a, b); }
  1723. inline double pow(const double a, const double b) { return sycl::pow(a, b); }
  1724. template <typename T, typename U>
  1725. inline typename std::enable_if_t<std::is_floating_point_v<T>, T>
  1726. pow(const T a, const U b)
  1727. {
  1728. return sycl::pow(a, static_cast<T>(b));
  1729. }
  1730. template <typename T, typename U>
  1731. inline typename std::enable_if_t<!std::is_floating_point_v<T>, double>
  1732. pow(const T a, const U b)
  1733. {
  1734. return sycl::pow(static_cast<double>(a), static_cast<double>(b));
  1735. }
  1736. inline double min(const double a, const float b)
  1737. {
  1738. return sycl::fmin(a, static_cast<double>(b));
  1739. }
  1740. inline double min(const float a, const double b)
  1741. {
  1742. return sycl::fmin(static_cast<double>(a), b);
  1743. }
  1744. inline float min(const float a, const float b) { return sycl::fmin(a, b); }
  1745. inline double min(const double a, const double b) { return sycl::fmin(a, b); }
  1746. inline std::uint32_t min(const std::uint32_t a, const std::int32_t b)
  1747. {
  1748. return sycl::min(a, static_cast<std::uint32_t>(b));
  1749. }
  1750. inline std::uint32_t min(const std::int32_t a, const std::uint32_t b)
  1751. {
  1752. return sycl::min(static_cast<std::uint32_t>(a), b);
  1753. }
  1754. inline std::int32_t min(const std::int32_t a, const std::int32_t b)
  1755. {
  1756. return sycl::min(a, b);
  1757. }
  1758. inline std::uint32_t min(const std::uint32_t a, const std::uint32_t b)
  1759. {
  1760. return sycl::min(a, b);
  1761. }
  1762. inline std::uint64_t min(const std::uint64_t a, const std::int64_t b)
  1763. {
  1764. return sycl::min(a, static_cast<std::uint64_t>(b));
  1765. }
  1766. inline std::uint64_t min(const std::int64_t a, const std::uint64_t b)
  1767. {
  1768. return sycl::min(static_cast<std::uint64_t>(a), b);
  1769. }
  1770. inline std::int64_t min(const std::int64_t a, const std::int64_t b)
  1771. {
  1772. return sycl::min(a, b);
  1773. }
  1774. inline std::uint64_t min(const std::uint64_t a, const std::uint64_t b)
  1775. {
  1776. return sycl::min(a, b);
  1777. }
  1778. inline std::uint64_t min(const std::uint64_t a, const std::int32_t b)
  1779. {
  1780. return sycl::min(a, static_cast<std::uint64_t>(b));
  1781. }
  1782. inline std::uint64_t min(const std::int32_t a, const std::uint64_t b)
  1783. {
  1784. return sycl::min(static_cast<std::uint64_t>(a), b);
  1785. }
  1786. inline std::uint64_t min(const std::uint64_t a, const std::uint32_t b)
  1787. {
  1788. return sycl::min(a, static_cast<std::uint64_t>(b));
  1789. }
  1790. inline std::uint64_t min(const std::uint32_t a, const std::uint64_t b)
  1791. {
  1792. return sycl::min(static_cast<std::uint64_t>(a), b);
  1793. }
  1794. // max function overloads.
  1795. // For floating-point types, `float` or `double` arguments are acceptable.
  1796. // For integer types, `std::uint32_t`, `std::int32_t`, `std::uint64_t` or
  1797. // `std::int64_t` type arguments are acceptable.
  1798. inline double max(const double a, const float b)
  1799. {
  1800. return sycl::fmax(a, static_cast<double>(b));
  1801. }
  1802. inline double max(const float a, const double b)
  1803. {
  1804. return sycl::fmax(static_cast<double>(a), b);
  1805. }
  1806. inline float max(const float a, const float b) { return sycl::fmax(a, b); }
  1807. inline double max(const double a, const double b) { return sycl::fmax(a, b); }
  1808. inline std::uint32_t max(const std::uint32_t a, const std::int32_t b)
  1809. {
  1810. return sycl::max(a, static_cast<std::uint32_t>(b));
  1811. }
  1812. inline std::uint32_t max(const std::int32_t a, const std::uint32_t b)
  1813. {
  1814. return sycl::max(static_cast<std::uint32_t>(a), b);
  1815. }
  1816. inline std::int32_t max(const std::int32_t a, const std::int32_t b)
  1817. {
  1818. return sycl::max(a, b);
  1819. }
  1820. inline std::uint32_t max(const std::uint32_t a, const std::uint32_t b)
  1821. {
  1822. return sycl::max(a, b);
  1823. }
  1824. inline std::uint64_t max(const std::uint64_t a, const std::int64_t b)
  1825. {
  1826. return sycl::max(a, static_cast<std::uint64_t>(b));
  1827. }
  1828. inline std::uint64_t max(const std::int64_t a, const std::uint64_t b)
  1829. {
  1830. return sycl::max(static_cast<std::uint64_t>(a), b);
  1831. }
  1832. inline std::int64_t max(const std::int64_t a, const std::int64_t b)
  1833. {
  1834. return sycl::max(a, b);
  1835. }
  1836. inline std::uint64_t max(const std::uint64_t a, const std::uint64_t b)
  1837. {
  1838. return sycl::max(a, b);
  1839. }
  1840. inline std::uint64_t max(const std::uint64_t a, const std::int32_t b)
  1841. {
  1842. return sycl::max(a, static_cast<std::uint64_t>(b));
  1843. }
  1844. inline std::uint64_t max(const std::int32_t a, const std::uint64_t b)
  1845. {
  1846. return sycl::max(static_cast<std::uint64_t>(a), b);
  1847. }
  1848. inline std::uint64_t max(const std::uint64_t a, const std::uint32_t b)
  1849. {
  1850. return sycl::max(a, static_cast<std::uint64_t>(b));
  1851. }
  1852. inline std::uint64_t max(const std::uint32_t a, const std::uint64_t b)
  1853. {
  1854. return sycl::max(static_cast<std::uint64_t>(a), b);
  1855. }
  1856. inline void
  1857. has_capability_or_fail(const sycl::device &dev,
  1858. const std::initializer_list<sycl::aspect> &props)
  1859. {
  1860. for (const auto &it : props)
  1861. {
  1862. if (dev.has(it))
  1863. continue;
  1864. switch (it)
  1865. {
  1866. case sycl::aspect::fp64:
  1867. throw std::runtime_error("'double' is not supported in '" +
  1868. dev.get_info<sycl::info::device::name>() +
  1869. "' device");
  1870. break;
  1871. case sycl::aspect::fp16:
  1872. throw std::runtime_error("'half' is not supported in '" +
  1873. dev.get_info<sycl::info::device::name>() +
  1874. "' device");
  1875. break;
  1876. default:
  1877. #define __SYCL_ASPECT(ASPECT, ID) \
  1878. case sycl::aspect::ASPECT: \
  1879. return #ASPECT;
  1880. #define __SYCL_ASPECT_DEPRECATED(ASPECT, ID, MESSAGE) __SYCL_ASPECT(ASPECT, ID)
  1881. #define __SYCL_ASPECT_DEPRECATED_ALIAS(ASPECT, ID, MESSAGE)
  1882. auto getAspectNameStr = [](sycl::aspect AspectNum) -> std::string
  1883. {
  1884. switch (AspectNum)
  1885. {
  1886. #include <sycl/info/aspects.def>
  1887. #include <sycl/info/aspects_deprecated.def>
  1888. default:
  1889. return "unknown aspect";
  1890. }
  1891. };
  1892. #undef __SYCL_ASPECT_DEPRECATED_ALIAS
  1893. #undef __SYCL_ASPECT_DEPRECATED
  1894. #undef __SYCL_ASPECT
  1895. throw std::runtime_error(
  1896. "'" + getAspectNameStr(it) + "' is not supported in '" +
  1897. dev.get_info<sycl::info::device::name>() + "' device");
  1898. }
  1899. break;
  1900. }
  1901. }
  1902. static inline unsigned int get_current_device_id()
  1903. {
  1904. return dev_mgr::instance().current_device_id();
  1905. }
  1906. static inline device_ext &get_current_device()
  1907. {
  1908. return dev_mgr::instance().current_device();
  1909. }
  1910. static inline sycl::queue &get_in_order_queue()
  1911. {
  1912. return dev_mgr::instance().current_device().in_order_queue();
  1913. }
  1914. static sycl::event
  1915. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr, size_t size,
  1916. memcpy_direction direction,
  1917. const std::vector<sycl::event> &dep_events = {})
  1918. {
  1919. if (!size)
  1920. return sycl::event{};
  1921. return q.memcpy(to_ptr, from_ptr, size, dep_events);
  1922. GGML_UNUSED(direction);
  1923. }
  1924. // Get actual copy range and make sure it will not exceed range.
  1925. static inline size_t get_copy_range(sycl::range<3> size, size_t slice,
  1926. size_t pitch)
  1927. {
  1928. return slice * (size.get(2) - 1) + pitch * (size.get(1) - 1) + size.get(0);
  1929. }
  1930. static inline size_t get_offset(sycl::id<3> id, size_t slice,
  1931. size_t pitch)
  1932. {
  1933. return slice * id.get(2) + pitch * id.get(1) + id.get(0);
  1934. }
  1935. /// copy 3D matrix specified by \p size from 3D matrix specified by \p from_ptr
  1936. /// and \p from_range to another specified by \p to_ptr and \p to_range.
  1937. static inline std::vector<sycl::event>
  1938. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  1939. sycl::range<3> to_range, sycl::range<3> from_range,
  1940. sycl::id<3> to_id, sycl::id<3> from_id,
  1941. sycl::range<3> size, memcpy_direction direction,
  1942. const std::vector<sycl::event> &dep_events = {})
  1943. {
  1944. // RAII for host pointer
  1945. class host_buffer
  1946. {
  1947. void *_buf;
  1948. size_t _size;
  1949. sycl::queue &_q;
  1950. const std::vector<sycl::event> &_deps; // free operation depends
  1951. public:
  1952. host_buffer(size_t size, sycl::queue &q,
  1953. const std::vector<sycl::event> &deps)
  1954. : _buf(std::malloc(size)), _size(size), _q(q), _deps(deps) {}
  1955. void *get_ptr() const { return _buf; }
  1956. size_t get_size() const { return _size; }
  1957. ~host_buffer()
  1958. {
  1959. if (_buf)
  1960. {
  1961. _q.submit([&](sycl::handler &cgh)
  1962. {
  1963. cgh.depends_on(_deps);
  1964. cgh.host_task([buf = _buf] { std::free(buf); }); });
  1965. }
  1966. }
  1967. };
  1968. std::vector<sycl::event> event_list;
  1969. size_t to_slice = to_range.get(1) * to_range.get(0),
  1970. from_slice = from_range.get(1) * from_range.get(0);
  1971. unsigned char *to_surface =
  1972. (unsigned char *)to_ptr + get_offset(to_id, to_slice, to_range.get(0));
  1973. const unsigned char *from_surface =
  1974. (const unsigned char *)from_ptr +
  1975. get_offset(from_id, from_slice, from_range.get(0));
  1976. if (to_slice == from_slice && to_slice == size.get(1) * size.get(0))
  1977. {
  1978. return {dpct_memcpy(q, to_surface, from_surface, to_slice * size.get(2),
  1979. direction, dep_events)};
  1980. }
  1981. direction = detail::deduce_memcpy_direction(q, to_ptr, from_ptr, direction);
  1982. size_t size_slice = size.get(1) * size.get(0);
  1983. switch (direction)
  1984. {
  1985. case host_to_host:
  1986. for (size_t z = 0; z < size.get(2); ++z)
  1987. {
  1988. unsigned char *to_ptr = to_surface;
  1989. const unsigned char *from_ptr = from_surface;
  1990. if (to_range.get(0) == from_range.get(0) &&
  1991. to_range.get(0) == size.get(0))
  1992. {
  1993. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size_slice,
  1994. direction, dep_events));
  1995. }
  1996. else
  1997. {
  1998. for (size_t y = 0; y < size.get(1); ++y)
  1999. {
  2000. event_list.push_back(dpct_memcpy(q, to_ptr, from_ptr, size.get(0),
  2001. direction, dep_events));
  2002. to_ptr += to_range.get(0);
  2003. from_ptr += from_range.get(0);
  2004. }
  2005. }
  2006. to_surface += to_slice;
  2007. from_surface += from_slice;
  2008. }
  2009. break;
  2010. case host_to_device:
  2011. {
  2012. host_buffer buf(get_copy_range(size, to_slice, to_range.get(0)), q,
  2013. event_list);
  2014. std::vector<sycl::event> host_events;
  2015. if (to_slice == size_slice)
  2016. {
  2017. // Copy host data to a temp host buffer with the shape of target.
  2018. host_events =
  2019. dpct_memcpy(q, buf.get_ptr(), from_surface, to_range, from_range,
  2020. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size,
  2021. host_to_host, dep_events);
  2022. }
  2023. else
  2024. {
  2025. // Copy host data to a temp host buffer with the shape of target.
  2026. host_events = dpct_memcpy(
  2027. q, buf.get_ptr(), from_surface, to_range, from_range,
  2028. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0), size, host_to_host,
  2029. // If has padding data, not sure whether it is useless. So fill temp
  2030. // buffer with it.
  2031. std::vector<sycl::event>{
  2032. dpct_memcpy(q, buf.get_ptr(), to_surface, buf.get_size(),
  2033. device_to_host, dep_events)});
  2034. }
  2035. // Copy from temp host buffer to device with only one submit.
  2036. event_list.push_back(dpct_memcpy(q, to_surface, buf.get_ptr(),
  2037. buf.get_size(), host_to_device,
  2038. host_events));
  2039. break;
  2040. }
  2041. case device_to_host:
  2042. {
  2043. host_buffer buf(get_copy_range(size, from_slice, from_range.get(0)), q,
  2044. event_list);
  2045. // Copy from host temp buffer to host target with reshaping.
  2046. event_list = dpct_memcpy(
  2047. q, to_surface, buf.get_ptr(), to_range, from_range, sycl::id<3>(0, 0, 0),
  2048. sycl::id<3>(0, 0, 0), size, host_to_host,
  2049. // Copy from device to temp host buffer with only one submit.
  2050. std::vector<sycl::event>{dpct_memcpy(q, buf.get_ptr(), from_surface,
  2051. buf.get_size(),
  2052. device_to_host, dep_events)});
  2053. break;
  2054. }
  2055. case device_to_device:
  2056. event_list.push_back(q.submit([&](sycl::handler &cgh)
  2057. {
  2058. cgh.depends_on(dep_events);
  2059. cgh.parallel_for<class dpct_memcpy_3d_detail>(
  2060. size,
  2061. [=](sycl::id<3> id) {
  2062. to_surface[get_offset(id, to_slice, to_range.get(0))] =
  2063. from_surface[get_offset(id, from_slice, from_range.get(0))];
  2064. }); }));
  2065. break;
  2066. default:
  2067. throw std::runtime_error("dpct_memcpy: invalid direction value");
  2068. }
  2069. return event_list;
  2070. }
  2071. /// memcpy 2D/3D matrix specified by pitched_data.
  2072. static inline std::vector<sycl::event>
  2073. dpct_memcpy(sycl::queue &q, pitched_data to, sycl::id<3> to_id,
  2074. pitched_data from, sycl::id<3> from_id, sycl::range<3> size,
  2075. memcpy_direction direction = automatic)
  2076. {
  2077. return dpct_memcpy(q, to.get_data_ptr(), from.get_data_ptr(),
  2078. sycl::range<3>(to.get_pitch(), to.get_y(), 1),
  2079. sycl::range<3>(from.get_pitch(), from.get_y(), 1), to_id, from_id,
  2080. size, direction);
  2081. }
  2082. /// memcpy 2D matrix with pitch.
  2083. static inline std::vector<sycl::event>
  2084. dpct_memcpy(sycl::queue &q, void *to_ptr, const void *from_ptr,
  2085. size_t to_pitch, size_t from_pitch, size_t x, size_t y,
  2086. memcpy_direction direction = automatic)
  2087. {
  2088. return dpct_memcpy(q, to_ptr, from_ptr, sycl::range<3>(to_pitch, y, 1),
  2089. sycl::range<3>(from_pitch, y, 1),
  2090. sycl::id<3>(0, 0, 0), sycl::id<3>(0, 0, 0),
  2091. sycl::range<3>(x, y, 1), direction);
  2092. }
  2093. inline void gemm(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2094. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2095. const void *alpha, const void *a, library_data_t a_type,
  2096. int lda, const void *b, library_data_t b_type, int ldb,
  2097. const void *beta, void *c, library_data_t c_type, int ldc,
  2098. library_data_t scaling_type)
  2099. {
  2100. if (scaling_type == library_data_t::real_float &&
  2101. c_type == library_data_t::complex_float)
  2102. {
  2103. scaling_type = library_data_t::complex_float;
  2104. }
  2105. else if (scaling_type == library_data_t::real_double &&
  2106. c_type == library_data_t::complex_double)
  2107. {
  2108. scaling_type = library_data_t::complex_double;
  2109. }
  2110. std::uint64_t key =
  2111. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2112. switch (key)
  2113. {
  2114. case detail::get_type_combination_id(
  2115. library_data_t::real_float, library_data_t::real_float,
  2116. library_data_t::real_float, library_data_t::real_float):
  2117. {
  2118. detail::gemm_impl<float, float, float, float>(
  2119. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2120. break;
  2121. }
  2122. case detail::get_type_combination_id(
  2123. library_data_t::real_double, library_data_t::real_double,
  2124. library_data_t::real_double, library_data_t::real_double):
  2125. {
  2126. detail::gemm_impl<double, double, double, double>(
  2127. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2128. break;
  2129. }
  2130. case detail::get_type_combination_id(
  2131. library_data_t::complex_float, library_data_t::complex_float,
  2132. library_data_t::complex_float, library_data_t::complex_float):
  2133. {
  2134. detail::gemm_impl<std::complex<float>, std::complex<float>,
  2135. std::complex<float>, std::complex<float>>(
  2136. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2137. break;
  2138. }
  2139. case detail::get_type_combination_id(
  2140. library_data_t::complex_double, library_data_t::complex_double,
  2141. library_data_t::complex_double, library_data_t::complex_double):
  2142. {
  2143. detail::gemm_impl<std::complex<double>, std::complex<double>,
  2144. std::complex<double>, std::complex<double>>(
  2145. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2146. break;
  2147. }
  2148. case detail::get_type_combination_id(
  2149. library_data_t::real_half, library_data_t::real_half,
  2150. library_data_t::real_half, library_data_t::real_half):
  2151. {
  2152. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2153. sycl::half>(q, a_trans, b_trans, m, n, k, alpha, a,
  2154. lda, b, ldb, beta, c, ldc);
  2155. break;
  2156. }
  2157. case detail::get_type_combination_id(
  2158. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2159. library_data_t::real_float, library_data_t::real_float):
  2160. {
  2161. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2162. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda, b,
  2163. ldb, beta, c, ldc);
  2164. break;
  2165. }
  2166. case detail::get_type_combination_id(
  2167. library_data_t::real_half, library_data_t::real_half,
  2168. library_data_t::real_float, library_data_t::real_float):
  2169. {
  2170. detail::gemm_impl<sycl::half, sycl::half, float, float>(
  2171. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2172. break;
  2173. }
  2174. case detail::get_type_combination_id(
  2175. library_data_t::real_half, library_data_t::real_half,
  2176. library_data_t::real_half, library_data_t::real_float):
  2177. {
  2178. float alpha_value =
  2179. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2180. float beta_value =
  2181. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2182. sycl::half alpha_half(alpha_value);
  2183. sycl::half beta_half(beta_value);
  2184. detail::gemm_impl<sycl::half, sycl::half, sycl::half,
  2185. sycl::half>(q, a_trans, b_trans, m, n, k, &alpha_half,
  2186. a, lda, b, ldb, &beta_half, c, ldc);
  2187. break;
  2188. }
  2189. case detail::get_type_combination_id(
  2190. library_data_t::real_int8, library_data_t::real_int8,
  2191. library_data_t::real_float, library_data_t::real_float):
  2192. {
  2193. detail::gemm_impl<std::int8_t, std::int8_t, float, float>(
  2194. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2195. break;
  2196. }
  2197. case detail::get_type_combination_id(
  2198. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2199. library_data_t::real_bfloat16, library_data_t::real_float):
  2200. {
  2201. detail::gemm_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2202. oneapi::mkl::bfloat16, float>(
  2203. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);
  2204. break;
  2205. }
  2206. case detail::get_type_combination_id(
  2207. library_data_t::real_int8, library_data_t::real_int8,
  2208. library_data_t::real_int32, library_data_t::real_int32):
  2209. {
  2210. float alpha_float =
  2211. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2212. float beta_float =
  2213. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2214. detail::gemm_impl<std::int8_t, std::int8_t, std::int32_t, float>(
  2215. q, a_trans, b_trans, m, n, k, &alpha_float, a, lda, b, ldb, &beta_float, c, ldc);
  2216. break;
  2217. }
  2218. default:
  2219. throw std::runtime_error("the combination of data type is unsupported");
  2220. }
  2221. } // gemm()
  2222. /// Computes a batch of matrix-matrix product with general matrices.
  2223. /// \param [in] q The queue where the routine should be executed.
  2224. /// \param [in] a_trans Specifies the operation applied to A.
  2225. /// \param [in] b_trans Specifies the operation applied to B.
  2226. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2227. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2228. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2229. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2230. /// \param [in] a Input matrix A.
  2231. /// \param [in] a_type Data type of the matrix A.
  2232. /// \param [in] lda Leading dimension of A.
  2233. /// \param [in] b Input matrix B.
  2234. /// \param [in] b_type Data type of the matrix B.
  2235. /// \param [in] ldb Leading dimension of B.
  2236. /// \param [in] beta Scaling factor for matrix C.
  2237. /// \param [in, out] c Input/Output matrix C.
  2238. /// \param [in] c_type Data type of the matrix C.
  2239. /// \param [in] ldc Leading dimension of C.
  2240. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2241. /// \param [in] scaling_type Data type of the scaling factors.
  2242. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2243. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2244. const void *alpha, const void *a[],
  2245. library_data_t a_type, int lda, const void *b[],
  2246. library_data_t b_type, int ldb, const void *beta,
  2247. void *c[], library_data_t c_type, int ldc,
  2248. int batch_size, library_data_t scaling_type)
  2249. {
  2250. if (scaling_type == library_data_t::real_float &&
  2251. c_type == library_data_t::complex_float)
  2252. {
  2253. scaling_type = library_data_t::complex_float;
  2254. }
  2255. else if (scaling_type == library_data_t::real_double &&
  2256. c_type == library_data_t::complex_double)
  2257. {
  2258. scaling_type = library_data_t::complex_double;
  2259. }
  2260. std::uint64_t key =
  2261. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2262. switch (key)
  2263. {
  2264. case detail::get_type_combination_id(
  2265. library_data_t::real_float, library_data_t::real_float,
  2266. library_data_t::real_float, library_data_t::real_float):
  2267. {
  2268. detail::gemm_batch_impl<float, float, float, float>(
  2269. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2270. batch_size);
  2271. break;
  2272. }
  2273. case detail::get_type_combination_id(
  2274. library_data_t::real_double, library_data_t::real_double,
  2275. library_data_t::real_double, library_data_t::real_double):
  2276. {
  2277. detail::gemm_batch_impl<double, double, double, double>(
  2278. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2279. batch_size);
  2280. break;
  2281. }
  2282. case detail::get_type_combination_id(
  2283. library_data_t::complex_float, library_data_t::complex_float,
  2284. library_data_t::complex_float, library_data_t::complex_float):
  2285. {
  2286. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2287. std::complex<float>, std::complex<float>>(
  2288. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2289. batch_size);
  2290. break;
  2291. }
  2292. case detail::get_type_combination_id(
  2293. library_data_t::complex_double, library_data_t::complex_double,
  2294. library_data_t::complex_double, library_data_t::complex_double):
  2295. {
  2296. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2297. std::complex<double>, std::complex<double>>(
  2298. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2299. batch_size);
  2300. break;
  2301. }
  2302. case detail::get_type_combination_id(
  2303. library_data_t::real_half, library_data_t::real_half,
  2304. library_data_t::real_half, library_data_t::real_half):
  2305. {
  2306. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2307. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2308. a, lda, b, ldb, beta, c, ldc,
  2309. batch_size);
  2310. break;
  2311. }
  2312. #ifdef __INTEL_MKL__
  2313. case detail::get_type_combination_id(
  2314. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2315. library_data_t::real_bfloat16, library_data_t::real_float):
  2316. {
  2317. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2318. oneapi::mkl::bfloat16, float>(
  2319. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2320. batch_size);
  2321. break;
  2322. }
  2323. case detail::get_type_combination_id(
  2324. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2325. library_data_t::real_float, library_data_t::real_float):
  2326. {
  2327. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2328. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2329. b, ldb, beta, c, ldc, batch_size);
  2330. break;
  2331. }
  2332. case detail::get_type_combination_id(
  2333. library_data_t::real_int8, library_data_t::real_int8,
  2334. library_data_t::real_int32, library_data_t::real_int32):
  2335. {
  2336. float alpha_float =
  2337. dpct::get_value(reinterpret_cast<const std::int32_t *>(alpha), q);
  2338. float beta_float =
  2339. dpct::get_value(reinterpret_cast<const std::int32_t *>(beta), q);
  2340. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2341. float>(q, a_trans, b_trans, m, n, k, &alpha_float,
  2342. a, lda, b, ldb, &beta_float, c, ldc,
  2343. batch_size);
  2344. break;
  2345. }
  2346. case detail::get_type_combination_id(
  2347. library_data_t::real_int8, library_data_t::real_int8,
  2348. library_data_t::real_float, library_data_t::real_float):
  2349. {
  2350. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2351. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2352. batch_size);
  2353. break;
  2354. }
  2355. case detail::get_type_combination_id(
  2356. library_data_t::real_half, library_data_t::real_half,
  2357. library_data_t::real_float, library_data_t::real_float):
  2358. {
  2359. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2360. q, a_trans, b_trans, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc,
  2361. batch_size);
  2362. break;
  2363. }
  2364. #endif
  2365. case detail::get_type_combination_id(
  2366. library_data_t::real_half, library_data_t::real_half,
  2367. library_data_t::real_half, library_data_t::real_float):
  2368. {
  2369. float alpha_value =
  2370. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2371. float beta_value =
  2372. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2373. sycl::half alpha_half(alpha_value);
  2374. sycl::half beta_half(beta_value);
  2375. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2376. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, b, ldb, &beta_half, c, ldc,
  2377. batch_size);
  2378. break;
  2379. }
  2380. default:
  2381. throw std::runtime_error("the combination of data type is unsupported");
  2382. }
  2383. }
  2384. /// Computes a batch of matrix-matrix product with general matrices.
  2385. /// \param [in] q The queue where the routine should be executed.
  2386. /// \param [in] a_trans Specifies the operation applied to A.
  2387. /// \param [in] b_trans Specifies the operation applied to B.
  2388. /// \param [in] m Specifies the number of rows of the matrix op(A) and of the matrix C.
  2389. /// \param [in] n Specifies the number of columns of the matrix op(B) and of the matrix C.
  2390. /// \param [in] k Specifies the number of columns of the matrix op(A) and the number of rows of the matrix op(B).
  2391. /// \param [in] alpha Scaling factor for the matrix-matrix product.
  2392. /// \param [in] a Input matrix A.
  2393. /// \param [in] a_type Data type of the matrix A.
  2394. /// \param [in] lda Leading dimension of A.
  2395. /// \param [in] stride_a Stride between the different A matrices.
  2396. /// \param [in] b Input matrix B.
  2397. /// \param [in] b_type Data type of the matrix B.
  2398. /// \param [in] ldb Leading dimension of B.
  2399. /// \param [in] stride_b Stride between the different B matrices.
  2400. /// \param [in] beta Scaling factor for matrix C.
  2401. /// \param [in, out] c Input/Output matrix C.
  2402. /// \param [in] c_type Data type of the matrix C.
  2403. /// \param [in] ldc Leading dimension of C.
  2404. /// \param [in] stride_c Stride between the different C matrices.
  2405. /// \param [in] batch_size Specifies the number of matrix multiply operations to perform.
  2406. /// \param [in] scaling_type Data type of the scaling factors.
  2407. inline void gemm_batch(sycl::queue &q, oneapi::mkl::transpose a_trans,
  2408. oneapi::mkl::transpose b_trans, int m, int n, int k,
  2409. const void *alpha, const void *a, library_data_t a_type,
  2410. int lda, long long int stride_a, const void *b,
  2411. library_data_t b_type, int ldb, long long int stride_b,
  2412. const void *beta, void *c, library_data_t c_type,
  2413. int ldc, long long int stride_c, int batch_size,
  2414. library_data_t scaling_type)
  2415. {
  2416. if (scaling_type == library_data_t::real_float &&
  2417. c_type == library_data_t::complex_float)
  2418. {
  2419. scaling_type = library_data_t::complex_float;
  2420. }
  2421. else if (scaling_type == library_data_t::real_double &&
  2422. c_type == library_data_t::complex_double)
  2423. {
  2424. scaling_type = library_data_t::complex_double;
  2425. }
  2426. std::uint64_t key =
  2427. detail::get_type_combination_id(a_type, b_type, c_type, scaling_type);
  2428. switch (key)
  2429. {
  2430. case detail::get_type_combination_id(
  2431. library_data_t::real_float, library_data_t::real_float,
  2432. library_data_t::real_float, library_data_t::real_float):
  2433. {
  2434. detail::gemm_batch_impl<float, float, float, float>(
  2435. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2436. beta, c, ldc, stride_c, batch_size);
  2437. break;
  2438. }
  2439. case detail::get_type_combination_id(
  2440. library_data_t::real_double, library_data_t::real_double,
  2441. library_data_t::real_double, library_data_t::real_double):
  2442. {
  2443. detail::gemm_batch_impl<double, double, double, double>(
  2444. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2445. beta, c, ldc, stride_c, batch_size);
  2446. break;
  2447. }
  2448. case detail::get_type_combination_id(
  2449. library_data_t::complex_float, library_data_t::complex_float,
  2450. library_data_t::complex_float, library_data_t::complex_float):
  2451. {
  2452. detail::gemm_batch_impl<std::complex<float>, std::complex<float>,
  2453. std::complex<float>, std::complex<float>>(
  2454. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2455. beta, c, ldc, stride_c, batch_size);
  2456. break;
  2457. }
  2458. case detail::get_type_combination_id(
  2459. library_data_t::complex_double, library_data_t::complex_double,
  2460. library_data_t::complex_double, library_data_t::complex_double):
  2461. {
  2462. detail::gemm_batch_impl<std::complex<double>, std::complex<double>,
  2463. std::complex<double>, std::complex<double>>(
  2464. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2465. beta, c, ldc, stride_c, batch_size);
  2466. break;
  2467. }
  2468. case detail::get_type_combination_id(
  2469. library_data_t::real_half, library_data_t::real_half,
  2470. library_data_t::real_half, library_data_t::real_half):
  2471. {
  2472. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half,
  2473. sycl::half>(q, a_trans, b_trans, m, n, k, alpha,
  2474. a, lda, stride_a, b, ldb, stride_b,
  2475. beta, c, ldc, stride_c, batch_size);
  2476. break;
  2477. }
  2478. #ifdef __INTEL_MKL__
  2479. case detail::get_type_combination_id(
  2480. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2481. library_data_t::real_bfloat16, library_data_t::real_float):
  2482. {
  2483. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16,
  2484. oneapi::mkl::bfloat16, float>(
  2485. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2486. beta, c, ldc, stride_c, batch_size);
  2487. break;
  2488. }
  2489. case detail::get_type_combination_id(
  2490. library_data_t::real_bfloat16, library_data_t::real_bfloat16,
  2491. library_data_t::real_float, library_data_t::real_float):
  2492. {
  2493. detail::gemm_batch_impl<oneapi::mkl::bfloat16, oneapi::mkl::bfloat16, float,
  2494. float>(q, a_trans, b_trans, m, n, k, alpha, a, lda,
  2495. stride_a, b, ldb, stride_b, beta, c, ldc,
  2496. stride_c, batch_size);
  2497. break;
  2498. }
  2499. case detail::get_type_combination_id(
  2500. library_data_t::real_int8, library_data_t::real_int8,
  2501. library_data_t::real_int32, library_data_t::real_int32):
  2502. {
  2503. detail::gemm_batch_impl<std::int8_t, std::int8_t, std::int32_t,
  2504. std::int32_t>(q, a_trans, b_trans, m, n, k, alpha,
  2505. a, lda, stride_a, b, ldb, stride_b,
  2506. beta, c, ldc, stride_c, batch_size);
  2507. break;
  2508. }
  2509. case detail::get_type_combination_id(
  2510. library_data_t::real_int8, library_data_t::real_int8,
  2511. library_data_t::real_float, library_data_t::real_float):
  2512. {
  2513. detail::gemm_batch_impl<std::int8_t, std::int8_t, float, float>(
  2514. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2515. beta, c, ldc, stride_c, batch_size);
  2516. break;
  2517. }
  2518. case detail::get_type_combination_id(
  2519. library_data_t::real_half, library_data_t::real_half,
  2520. library_data_t::real_float, library_data_t::real_float):
  2521. {
  2522. detail::gemm_batch_impl<sycl::half, sycl::half, float, float>(
  2523. q, a_trans, b_trans, m, n, k, alpha, a, lda, stride_a, b, ldb, stride_b,
  2524. beta, c, ldc, stride_c, batch_size);
  2525. break;
  2526. }
  2527. #endif
  2528. case detail::get_type_combination_id(
  2529. library_data_t::real_half, library_data_t::real_half,
  2530. library_data_t::real_half, library_data_t::real_float):
  2531. {
  2532. float alpha_value =
  2533. dpct::get_value(reinterpret_cast<const float *>(alpha), q);
  2534. float beta_value =
  2535. dpct::get_value(reinterpret_cast<const float *>(beta), q);
  2536. sycl::half alpha_half(alpha_value);
  2537. sycl::half beta_half(beta_value);
  2538. detail::gemm_batch_impl<sycl::half, sycl::half, sycl::half, sycl::half>(
  2539. q, a_trans, b_trans, m, n, k, &alpha_half, a, lda, stride_a, b, ldb, stride_b,
  2540. &beta_half, c, ldc, stride_c, batch_size);
  2541. break;
  2542. }
  2543. default:
  2544. throw std::runtime_error("the combination of data type is unsupported");
  2545. }
  2546. }
  2547. static inline void
  2548. async_dpct_memcpy(void *to_ptr, size_t to_pitch, const void *from_ptr,
  2549. size_t from_pitch, size_t x, size_t y,
  2550. memcpy_direction direction = automatic,
  2551. sycl::queue &q = get_default_queue())
  2552. {
  2553. detail::dpct_memcpy(q, to_ptr, from_ptr, to_pitch, from_pitch, x, y,
  2554. direction);
  2555. }
  2556. using err0 = detail::generic_error_type<struct err0_tag, int>;
  2557. using err1 = detail::generic_error_type<struct err1_tag, int>;
  2558. static inline void dpct_free(void *ptr, sycl::queue &q = get_default_queue()) {
  2559. detail::dpct_free(ptr, q);
  2560. }
  2561. /// dpct accessor used as device function parameter.
  2562. template <class T, memory_region Memory, size_t Dimension> class accessor;
  2563. template <class T, memory_region Memory> class accessor<T, Memory, 3> {
  2564. public:
  2565. using memory_t = detail::memory_traits<Memory, T>;
  2566. using element_t = typename memory_t::element_t;
  2567. using pointer_t = typename memory_t::pointer_t;
  2568. using accessor_t = typename memory_t::template accessor_t<3>;
  2569. accessor(pointer_t data, const sycl::range<3> &in_range)
  2570. : _data(data), _range(in_range) {}
  2571. template <memory_region M = Memory>
  2572. accessor(typename std::enable_if<M != local, const accessor_t>::type &acc)
  2573. : accessor(acc, acc.get_range()) {}
  2574. accessor(const accessor_t &acc, const sycl::range<3> &in_range)
  2575. : accessor(acc.get_pointer(), in_range) {}
  2576. accessor<T, Memory, 2> operator[](size_t index) const {
  2577. sycl::range<2> sub(_range.get(1), _range.get(2));
  2578. return accessor<T, Memory, 2>(_data + index * sub.size(), sub);
  2579. }
  2580. pointer_t get_ptr() const { return _data; }
  2581. private:
  2582. pointer_t _data;
  2583. sycl::range<3> _range;
  2584. };
  2585. template <class T, memory_region Memory> class accessor<T, Memory, 2> {
  2586. public:
  2587. using memory_t = detail::memory_traits<Memory, T>;
  2588. using element_t = typename memory_t::element_t;
  2589. using pointer_t = typename memory_t::pointer_t;
  2590. using accessor_t = typename memory_t::template accessor_t<2>;
  2591. accessor(pointer_t data, const sycl::range<2> &in_range)
  2592. : _data(data), _range(in_range) {}
  2593. template <memory_region M = Memory>
  2594. accessor(typename std::enable_if<M != local, const accessor_t>::type &acc)
  2595. : accessor(acc, acc.get_range()) {}
  2596. accessor(const accessor_t &acc, const sycl::range<2> &in_range)
  2597. : accessor(acc.get_pointer(), in_range) {}
  2598. pointer_t operator[](size_t index) const {
  2599. return _data + _range.get(1) * index;
  2600. }
  2601. pointer_t get_ptr() const { return _data; }
  2602. private:
  2603. pointer_t _data;
  2604. sycl::range<2> _range;
  2605. };
  2606. namespace detail {
  2607. /// Device variable with address space of shared, global or constant.
  2608. template <class T, memory_region Memory, size_t Dimension> class device_memory {
  2609. public:
  2610. using accessor_t =
  2611. typename detail::memory_traits<Memory,
  2612. T>::template accessor_t<Dimension>;
  2613. using value_t = typename detail::memory_traits<Memory, T>::value_t;
  2614. using dpct_accessor_t = dpct::accessor<T, Memory, Dimension>;
  2615. device_memory() : device_memory(sycl::range<Dimension>(1)) {}
  2616. /// Constructor of 1-D array with initializer list
  2617. device_memory(const sycl::range<Dimension> &in_range,
  2618. std::initializer_list<value_t> &&init_list)
  2619. : device_memory(in_range) {
  2620. assert(init_list.size() <= in_range.size());
  2621. _host_ptr = (value_t *)std::malloc(_size);
  2622. std::memset(_host_ptr, 0, _size);
  2623. std::memcpy(_host_ptr, init_list.begin(), init_list.size() * sizeof(T));
  2624. }
  2625. /// Constructor of 2-D array with initializer list
  2626. template <size_t D = Dimension>
  2627. device_memory(
  2628. const typename std::enable_if<D == 2, sycl::range<2>>::type &in_range,
  2629. std::initializer_list<std::initializer_list<value_t>> &&init_list)
  2630. : device_memory(in_range) {
  2631. assert(init_list.size() <= in_range[0]);
  2632. _host_ptr = (value_t *)std::malloc(_size);
  2633. std::memset(_host_ptr, 0, _size);
  2634. auto tmp_data = _host_ptr;
  2635. for (auto sub_list : init_list) {
  2636. assert(sub_list.size() <= in_range[1]);
  2637. std::memcpy(tmp_data, sub_list.begin(),
  2638. sub_list.size() * sizeof(T));
  2639. tmp_data += in_range[1];
  2640. }
  2641. }
  2642. /// Constructor with range
  2643. device_memory(const sycl::range<Dimension> &range_in)
  2644. : _size(range_in.size() * sizeof(T)), _range(range_in),
  2645. _reference(false), _host_ptr(nullptr), _device_ptr(nullptr) {
  2646. static_assert(
  2647. (Memory == global) || (Memory == constant) || (Memory == shared),
  2648. "device memory region should be global, constant or shared");
  2649. // Make sure that singleton class mem_mgr and dev_mgr will destruct
  2650. // later than this.
  2651. detail::mem_mgr::instance();
  2652. dev_mgr::instance();
  2653. }
  2654. /// Constructor with range
  2655. template <class... Args>
  2656. device_memory(Args... Arguments)
  2657. : device_memory(sycl::range<Dimension>(Arguments...)) {}
  2658. ~device_memory() {
  2659. if (_device_ptr && !_reference)
  2660. dpct::dpct_free(_device_ptr);
  2661. if (_host_ptr)
  2662. std::free(_host_ptr);
  2663. }
  2664. /// Allocate memory with default queue, and init memory if has initial
  2665. /// value.
  2666. void init() { init(dpct::get_default_queue()); }
  2667. /// Allocate memory with specified queue, and init memory if has initial
  2668. /// value.
  2669. void init(sycl::queue &q) {
  2670. if (_device_ptr)
  2671. return;
  2672. if (!_size)
  2673. return;
  2674. allocate_device(q);
  2675. if (_host_ptr)
  2676. detail::dpct_memcpy(q, _device_ptr, _host_ptr, _size,
  2677. host_to_device);
  2678. }
  2679. /// The variable is assigned to a device pointer.
  2680. void assign(value_t *src, size_t size) {
  2681. this->~device_memory();
  2682. new (this) device_memory(src, size);
  2683. }
  2684. /// Get memory pointer of the memory object, which is virtual pointer when
  2685. /// usm is not used, and device pointer when usm is used.
  2686. value_t *get_ptr() { return get_ptr(get_default_queue()); }
  2687. /// Get memory pointer of the memory object, which is virtual pointer when
  2688. /// usm is not used, and device pointer when usm is used.
  2689. value_t *get_ptr(sycl::queue &q) {
  2690. init(q);
  2691. return _device_ptr;
  2692. }
  2693. /// Get the device memory object size in bytes.
  2694. size_t get_size() { return _size; }
  2695. template <size_t D = Dimension>
  2696. typename std::enable_if<D == 1, T>::type &operator[](size_t index) {
  2697. init();
  2698. return _device_ptr[index];
  2699. }
  2700. /// Get dpct::accessor with dimension info for the device memory object
  2701. /// when usm is used and dimension is greater than 1.
  2702. template <size_t D = Dimension>
  2703. typename std::enable_if<D != 1, dpct_accessor_t>::type
  2704. get_access(sycl::handler &cgh) {
  2705. return dpct_accessor_t((T *)_device_ptr, _range);
  2706. }
  2707. private:
  2708. device_memory(value_t *memory_ptr, size_t size)
  2709. : _size(size), _range(size / sizeof(T)), _reference(true),
  2710. _device_ptr(memory_ptr) {}
  2711. void allocate_device(sycl::queue &q) {
  2712. #ifndef DPCT_USM_LEVEL_NONE
  2713. if (Memory == shared) {
  2714. _device_ptr = (value_t *)sycl::malloc_shared(_size, q.get_device(),
  2715. q.get_context());
  2716. return;
  2717. }
  2718. #ifdef SYCL_EXT_ONEAPI_USM_DEVICE_READ_ONLY
  2719. if (Memory == constant) {
  2720. _device_ptr = (value_t *)sycl::malloc_device(
  2721. _size, q.get_device(), q.get_context(),
  2722. sycl::ext::oneapi::property::usm::device_read_only());
  2723. return;
  2724. }
  2725. #endif
  2726. #endif
  2727. _device_ptr = (value_t *)detail::dpct_malloc(_size, q);
  2728. }
  2729. size_t _size;
  2730. sycl::range<Dimension> _range;
  2731. bool _reference;
  2732. value_t *_host_ptr;
  2733. value_t *_device_ptr;
  2734. };
  2735. template <class T, memory_region Memory>
  2736. class device_memory<T, Memory, 0> : public device_memory<T, Memory, 1> {
  2737. public:
  2738. using base = device_memory<T, Memory, 1>;
  2739. using value_t = typename base::value_t;
  2740. using accessor_t =
  2741. typename detail::memory_traits<Memory, T>::template accessor_t<0>;
  2742. /// Constructor with initial value.
  2743. device_memory(const value_t &val) : base(sycl::range<1>(1), {val}) {}
  2744. /// Default constructor
  2745. device_memory() : base(1) {}
  2746. };
  2747. } // namespace detail
  2748. template <class T, size_t Dimension>
  2749. using global_memory = detail::device_memory<T, global, Dimension>;
  2750. template <class T, size_t Dimension>
  2751. using constant_memory = detail::device_memory<T, constant, Dimension>;
  2752. template <class T, size_t Dimension>
  2753. using shared_memory = detail::device_memory<T, shared, Dimension>;
  2754. } // COPY from DPCT head files
  2755. #define GGML_COMMON_DECL_SYCL
  2756. #define GGML_COMMON_IMPL_SYCL
  2757. #include "ggml-common.h"
  2758. static int g_ggml_sycl_debug=0;
  2759. #define GGML_SYCL_DEBUG(...) do{if(g_ggml_sycl_debug) fprintf(stderr, __VA_ARGS__);}while(0)
  2760. #define CHECK_TRY_ERROR(expr) \
  2761. [&]() { \
  2762. try { \
  2763. expr; \
  2764. return dpct::success; \
  2765. } catch (std::exception const &e) { \
  2766. std::cerr << e.what()<< "\nException caught at file:" << __FILE__ \
  2767. << ", line:" << __LINE__ <<", func:"<<__func__<< std::endl; \
  2768. return dpct::default_error; \
  2769. } \
  2770. }()
  2771. // #define DEBUG_SYCL_MALLOC
  2772. static int g_work_group_size = 0;
  2773. // typedef sycl::half ggml_fp16_t;
  2774. #define __SYCL_ARCH__ DPCT_COMPATIBILITY_TEMP
  2775. #define VER_4VEC 610 //todo for hardward optimize.
  2776. #define VER_GEN9 700 //todo for hardward optimize.
  2777. #define VER_GEN12 1000000 //todo for hardward optimize.
  2778. #define VER_GEN13 (VER_GEN12 + 1030) //todo for hardward optimize.
  2779. #define GGML_SYCL_MAX_NODES 8192 //TODO: adapt to hardwares
  2780. //define for XMX in Intel GPU
  2781. //TODO: currently, it's not used for XMX really.
  2782. #define SYCL_USE_XMX
  2783. // max batch size to use MMQ kernels when tensor cores are available
  2784. #define XMX_MAX_BATCH_SIZE 32
  2785. #if defined(_MSC_VER)
  2786. #pragma warning(disable: 4244 4267) // possible loss of data
  2787. #endif
  2788. // dmmv = dequantize_mul_mat_vec
  2789. #ifndef GGML_SYCL_DMMV_X
  2790. #define GGML_SYCL_DMMV_X 32
  2791. #endif
  2792. #ifndef GGML_SYCL_MMV_Y
  2793. #define GGML_SYCL_MMV_Y 1
  2794. #endif
  2795. enum ggml_sycl_backend_gpu_mode {
  2796. SYCL_UNSET_GPU_MODE = -1,
  2797. SYCL_SINGLE_GPU_MODE = 0,
  2798. SYCL_MUL_GPU_MODE
  2799. };
  2800. static_assert(sizeof(sycl::half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  2801. static void crash(){
  2802. int *ptr = NULL;
  2803. *ptr = 0;
  2804. }
  2805. static void ggml_sycl_error(const char * stmt, const char * func, const char * file, const int line, const char * msg) {
  2806. fprintf(stderr, "SYCL error: %s: %s\n", stmt, msg);
  2807. fprintf(stderr, " in function %s at %s:%d\n", func, file, line);
  2808. GGML_ASSERT(!"SYCL error");
  2809. }
  2810. #define SYCL_CHECK(err) do { \
  2811. auto err_ = (err); if (err_ != 0) ggml_sycl_error( \
  2812. #err, __func__, __FILE__, __LINE__, \
  2813. "Meet error in this line code!"); \
  2814. } while (0)
  2815. #if DPCT_COMPAT_RT_VERSION >= 11100
  2816. #define GGML_SYCL_ASSUME(x) __builtin_assume(x)
  2817. #else
  2818. #define GGML_SYCL_ASSUME(x)
  2819. #endif // DPCT_COMPAT_RT_VERSION >= 11100
  2820. #ifdef GGML_SYCL_F16
  2821. typedef sycl::half dfloat; // dequantize float
  2822. typedef sycl::half2 dfloat2;
  2823. #else
  2824. typedef float dfloat; // dequantize float
  2825. typedef sycl::float2 dfloat2;
  2826. #endif //GGML_SYCL_F16
  2827. bool ggml_sycl_loaded(void);
  2828. void * ggml_sycl_host_malloc(size_t size);
  2829. void ggml_sycl_host_free(void * ptr);
  2830. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
  2831. void ggml_sycl_free_data(struct ggml_tensor * tensor);
  2832. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor);
  2833. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor);
  2834. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor);
  2835. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor);
  2836. void ggml_sycl_copy_to_device(struct ggml_tensor * tensor);
  2837. void ggml_sycl_set_main_device(int main_device);
  2838. void ggml_sycl_set_mul_mat_q(bool mul_mat_q);
  2839. void ggml_sycl_set_scratch_size(size_t scratch_size);
  2840. void ggml_sycl_free_scratch(void);
  2841. void ggml_sycl_get_device_description(int device, char * description, size_t description_size);
  2842. bool ggml_backend_is_sycl(ggml_backend_t backend);
  2843. int ggml_backend_sycl_get_device(ggml_backend_t backend);
  2844. int get_main_device();
  2845. void print_ggml_tensor(const char*name, struct ggml_tensor *src);
  2846. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt);
  2847. void dev2dev_memcpy(sycl::queue &q_dst, sycl::queue &q_src, void *ptr_dst,
  2848. const void *ptr_src, size_t size) {
  2849. char *host_buf = (char *)malloc(size);
  2850. q_src.memcpy(host_buf, (const char *)ptr_src, size).wait();
  2851. q_dst.memcpy((char *)ptr_dst, host_buf, size).wait();
  2852. free(host_buf);
  2853. }
  2854. static __dpct_inline__ int get_int_from_int8(const int8_t *x8, const int &i32) {
  2855. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2856. int x32 = 0;
  2857. x32 |= x16[0] << 0;
  2858. x32 |= x16[1] << 16;
  2859. return x32;
  2860. }
  2861. static __dpct_inline__ int get_int_from_uint8(const uint8_t *x8,
  2862. const int &i32) {
  2863. const uint16_t * x16 = (const uint16_t *) (x8 + sizeof(int) * i32); // assume at least 2 byte alignment
  2864. int x32 = 0;
  2865. x32 |= x16[0] << 0;
  2866. x32 |= x16[1] << 16;
  2867. return x32;
  2868. }
  2869. static __dpct_inline__ int get_int_from_int8_aligned(const int8_t *x8,
  2870. const int &i32) {
  2871. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2872. }
  2873. static __dpct_inline__ int get_int_from_uint8_aligned(const uint8_t *x8,
  2874. const int &i32) {
  2875. return *((const int *) (x8 + sizeof(int) * i32)); // assume at least 4 byte alignment
  2876. }
  2877. template <typename T>
  2878. using to_t_sycl_t = void (*)(const void *__restrict__ x, T *__restrict__ y,
  2879. int k, dpct::queue_ptr stream);
  2880. typedef to_t_sycl_t<float> to_fp32_sycl_t;
  2881. typedef to_t_sycl_t<sycl::half> to_fp16_sycl_t;
  2882. typedef void (*dequantize_kernel_t)(const void * vx, const int ib, const int iqs, dfloat2 & v);
  2883. typedef void (*dot_kernel_k_t)(const void * __restrict__ vx, const int ib, const int iqs, const float * __restrict__ y, float & v);
  2884. typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
  2885. typedef void (*ggml_sycl_func_t)(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
  2886. typedef void (*ggml_sycl_op_mul_mat_t)(
  2887. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  2888. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  2889. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  2890. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  2891. const dpct::queue_ptr &stream);
  2892. typedef void (*ggml_sycl_op_flatten_t)(const ggml_tensor *src0,
  2893. const ggml_tensor *src1,
  2894. ggml_tensor *dst, const float *src0_dd,
  2895. const float *src1_dd, float *dst_dd,
  2896. const dpct::queue_ptr &main_stream);
  2897. typedef float (*vec_dot_q_sycl_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
  2898. typedef void (*allocate_tiles_sycl_t)(int **x_ql, sycl::half2 **x_dm,
  2899. int **x_qh, int **x_sc);
  2900. typedef void (*load_tiles_sycl_t)(const void *__restrict__ vx,
  2901. int *__restrict__ x_ql,
  2902. sycl::half2 *__restrict__ x_dm,
  2903. int *__restrict__ x_qh,
  2904. int *__restrict__ x_sc, const int &i_offset,
  2905. const int &i_max, const int &k,
  2906. const int &blocks_per_row);
  2907. typedef float (*vec_dot_q_mul_mat_sycl_t)(
  2908. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  2909. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  2910. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ms,
  2911. const int &i, const int &j, const int &k);
  2912. #define WARP_SIZE 32
  2913. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  2914. #define SYCL_GELU_BLOCK_SIZE 256
  2915. #define SYCL_SILU_BLOCK_SIZE 256
  2916. #define SYCL_TANH_BLOCK_SIZE 256
  2917. #define SYCL_RELU_BLOCK_SIZE 256
  2918. #define SYCL_HARDSIGMOID_BLOCK_SIZE 256
  2919. #define SYCL_HARDSWISH_BLOCK_SIZE 256
  2920. #define SYCL_SQR_BLOCK_SIZE 256
  2921. #define SYCL_CPY_BLOCK_SIZE 32
  2922. #define SYCL_SCALE_BLOCK_SIZE 256
  2923. #define SYCL_CLAMP_BLOCK_SIZE 256
  2924. #define SYCL_ROPE_BLOCK_SIZE 256
  2925. #define SYCL_SOFT_MAX_BLOCK_SIZE 1024
  2926. #define SYCL_ALIBI_BLOCK_SIZE 32
  2927. #define SYCL_DIAG_MASK_INF_BLOCK_SIZE 32
  2928. #define SYCL_QUANTIZE_BLOCK_SIZE 256
  2929. #define SYCL_DEQUANTIZE_BLOCK_SIZE 256
  2930. #define SYCL_GET_ROWS_BLOCK_SIZE 256
  2931. #define SYCL_UPSCALE_BLOCK_SIZE 256
  2932. #define SYCL_CONCAT_BLOCK_SIZE 256
  2933. #define SYCL_PAD_BLOCK_SIZE 256
  2934. #define SYCL_ACC_BLOCK_SIZE 256
  2935. #define SYCL_IM2COL_BLOCK_SIZE 256
  2936. #define SYCL_POOL2D_BLOCK_SIZE 256
  2937. // dmmv = dequantize_mul_mat_vec
  2938. #ifndef GGML_SYCL_DMMV_X
  2939. #define GGML_SYCL_DMMV_X 32
  2940. #endif
  2941. #ifndef GGML_SYCL_MMV_Y
  2942. #define GGML_SYCL_MMV_Y 1
  2943. #endif
  2944. #ifndef K_QUANTS_PER_ITERATION
  2945. #define K_QUANTS_PER_ITERATION 2
  2946. #else
  2947. static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
  2948. #endif
  2949. #ifndef GGML_SYCL_PEER_MAX_BATCH_SIZE
  2950. #define GGML_SYCL_PEER_MAX_BATCH_SIZE 128
  2951. #endif // GGML_SYCL_PEER_MAX_BATCH_SIZE
  2952. #define MUL_MAT_SRC1_COL_STRIDE 128
  2953. #define MAX_STREAMS 8
  2954. static dpct::queue_ptr g_syclStreams[GGML_SYCL_MAX_DEVICES][MAX_STREAMS] = {{0}};
  2955. struct ggml_tensor_extra_gpu {
  2956. void * data_device[GGML_SYCL_MAX_DEVICES]; // 1 pointer for each device for split tensors
  2957. dpct::event_ptr
  2958. events[GGML_SYCL_MAX_DEVICES]
  2959. [MAX_STREAMS]; // events for synchronizing multiple GPUs
  2960. };
  2961. class sycl_gpu_mgr {
  2962. public:
  2963. std::vector<int> gpus;
  2964. std::vector<sycl::device> devices;
  2965. sycl::queue *first_queue;
  2966. sycl::context co_ctx;
  2967. int max_compute_units = 0;
  2968. int work_group_size = 0;
  2969. std::string gpus_list = "";
  2970. /*
  2971. Use all GPUs with same top max compute units
  2972. */
  2973. sycl_gpu_mgr() {
  2974. detect_sycl_gpu_list_with_max_cu();
  2975. get_allow_gpus();
  2976. create_context_with_gpus();
  2977. }
  2978. /*
  2979. Only use the assigned GPU
  2980. */
  2981. sycl_gpu_mgr(int main_gpu_id) {
  2982. sycl::device device = dpct::dev_mgr::instance().get_device(main_gpu_id);
  2983. dpct::device_info prop;
  2984. dpct::get_device_info(prop, device);
  2985. gpus.push_back(main_gpu_id);
  2986. devices.push_back(device);
  2987. work_group_size = prop.get_max_work_group_size();
  2988. max_compute_units = prop.get_max_compute_units();
  2989. get_allow_gpus();
  2990. create_context_with_gpus();
  2991. }
  2992. void create_context_with_gpus() {
  2993. sycl::context ctx = sycl::context(devices);
  2994. assert(gpus.size() > 0);
  2995. first_queue = dpct::get_current_device().create_queue(ctx, devices[0]);
  2996. co_ctx = first_queue->get_context();
  2997. }
  2998. sycl::context &get_co_ctx() { return co_ctx; }
  2999. void get_allow_gpus() {
  3000. gpus_list = "";
  3001. for (size_t i = 0; i < gpus.size(); ++i) {
  3002. gpus_list += std::to_string(gpus[i]);
  3003. gpus_list += ",";
  3004. }
  3005. if (gpus_list.length() > 1) {
  3006. gpus_list.pop_back();
  3007. }
  3008. }
  3009. bool is_allowed_gpu(int device_id) {
  3010. return std::find(gpus.begin(), gpus.end(), device_id) != gpus.end();
  3011. }
  3012. void detect_sycl_gpu_list_with_max_cu() try {
  3013. int device_count = dpct::dev_mgr::instance().device_count();
  3014. for (int id = 0; id < device_count; id++) {
  3015. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  3016. if (!device.is_gpu())
  3017. continue;
  3018. dpct::device_info prop;
  3019. dpct::get_device_info(prop, device);
  3020. if (max_compute_units < prop.get_max_compute_units())
  3021. max_compute_units = prop.get_max_compute_units();
  3022. }
  3023. for (int id = 0; id < device_count; id++) {
  3024. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  3025. if (!device.is_gpu())
  3026. continue;
  3027. dpct::device_info prop;
  3028. dpct::get_device_info(prop, device);
  3029. if (max_compute_units == prop.get_max_compute_units() &&
  3030. is_ext_oneapi_device(device)) {
  3031. gpus.push_back(id);
  3032. devices.push_back(device);
  3033. work_group_size = prop.get_max_work_group_size();
  3034. }
  3035. }
  3036. return;
  3037. } catch (sycl::exception const &exc) {
  3038. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3039. << ", line:" << __LINE__ << std::endl;
  3040. std::exit(1);
  3041. }
  3042. int get_gpu_count() { return (int)gpus.size(); }
  3043. int get_index(int id) {
  3044. for (int i = 0; i < (int)gpus.size(); i++) {
  3045. if (gpus[i] == id)
  3046. return i;
  3047. }
  3048. printf("miss to get device index by id=%d\n", id);
  3049. GGML_ASSERT(false);
  3050. }
  3051. int get_next_index(int id) {
  3052. int cur_index = get_index(id);
  3053. for (int i = cur_index + 1; i < (int)gpus.size(); i++) {
  3054. if (gpus[i] == id)
  3055. return i;
  3056. }
  3057. GGML_ASSERT(false);
  3058. }
  3059. bool is_ext_oneapi_device(const sycl::device &dev) {
  3060. sycl::backend dev_backend = dev.get_backend();
  3061. if (dev_backend == sycl::backend::ext_oneapi_level_zero ||
  3062. dev_backend == sycl::backend::ext_oneapi_cuda ||
  3063. dev_backend == sycl::backend::ext_oneapi_hip)
  3064. return true;
  3065. return false;
  3066. }
  3067. };
  3068. static sycl_gpu_mgr *g_sycl_gpu_mgr = NULL;
  3069. static int g_device_count = -1;
  3070. static int g_all_sycl_device_count = -1;
  3071. static int g_main_device = -1;
  3072. static int g_main_device_id = -1;
  3073. static bool g_ggml_backend_sycl_buffer_type_initialized = false;
  3074. static std::array<float, GGML_SYCL_MAX_DEVICES> g_default_tensor_split = {};
  3075. static float g_tensor_split[GGML_SYCL_MAX_DEVICES] = {0};
  3076. static ggml_sycl_backend_gpu_mode g_ggml_sycl_backend_gpu_mode = SYCL_UNSET_GPU_MODE;
  3077. struct sycl_device_capabilities {
  3078. int cc; // compute capability
  3079. bool vmm; // virtual memory support
  3080. size_t vmm_granularity; // granularity of virtual memory
  3081. int device_id;
  3082. };
  3083. static sycl_device_capabilities g_device_caps[GGML_SYCL_MAX_DEVICES] = { {0, false, 0, -1} };
  3084. struct sycl_device_id2index {
  3085. int index;
  3086. };
  3087. static void * g_scratch_buffer = nullptr;
  3088. static size_t g_scratch_size = 0; // disabled by default
  3089. static size_t g_scratch_offset = 0;
  3090. static dpct::queue_ptr g_sycl_handles[GGML_SYCL_MAX_DEVICES] = {nullptr};
  3091. int get_main_device(){
  3092. return g_main_device;
  3093. }
  3094. [[noreturn]]
  3095. static void bad_arch(const sycl::stream &stream_ct1) {
  3096. stream_ct1 << "ERROR: ggml-sycl was compiled without support for the "
  3097. "current GPU architecture.\n";
  3098. // __trap();
  3099. std::exit(1);
  3100. (void) bad_arch; // suppress unused function warning
  3101. }
  3102. /*
  3103. device_index: device index from 0 to n (continue numbers).
  3104. It is used for device select/set in SYCL backend internal data structure.
  3105. */
  3106. void check_allow_gpu_index(const int device_index) {
  3107. if (device_index >= g_device_count) {
  3108. char error_buf[256];
  3109. snprintf(error_buf, sizeof(error_buf),
  3110. "%s error: device_index:%d is out of range: [0-%d]", __func__,
  3111. device_index, g_device_count - 1);
  3112. fprintf(stderr, "%s\n", error_buf);
  3113. assert(false);
  3114. }
  3115. }
  3116. /*
  3117. device_id: device ID is shown by ggml_backend_sycl_print_sycl_devices().
  3118. It is only used to set current working device.
  3119. */
  3120. void check_allow_gpu_id(const int device_id) {
  3121. if (!g_sycl_gpu_mgr->is_allowed_gpu(device_id)) {
  3122. char error_buf[256];
  3123. snprintf(error_buf, sizeof(error_buf),
  3124. "error: cannot set device=%d, which is not allowed. Please "
  3125. "set GPU ID in: [%s]",
  3126. device_id, g_sycl_gpu_mgr->gpus_list.c_str());
  3127. fprintf(stderr, "%s\n", error_buf);
  3128. throw std::invalid_argument(error_buf);
  3129. }
  3130. }
  3131. int get_current_device_id() {
  3132. return dpct::dev_mgr::instance().current_device_id();
  3133. }
  3134. inline dpct::err0 ggml_sycl_set_device(const int device) try {
  3135. int device_id = g_sycl_gpu_mgr->gpus[device];
  3136. check_allow_gpu_id(device_id);
  3137. int current_device_id;
  3138. SYCL_CHECK(CHECK_TRY_ERROR(current_device_id = get_current_device_id()));
  3139. // GGML_SYCL_DEBUG("ggml_sycl_set_device device_id=%d,
  3140. // current_device_id=%d\n", device, current_device);
  3141. if (device_id == current_device_id) {
  3142. return 0;
  3143. }
  3144. return CHECK_TRY_ERROR(dpct::select_device(device_id));
  3145. } catch (sycl::exception const &exc) {
  3146. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  3147. << ", line:" << __LINE__ << std::endl;
  3148. crash();
  3149. std::exit(1);
  3150. }
  3151. void log_ggml_var_device(const char*name, float *src, size_t total_elements, bool src_on_device){
  3152. if(!g_ggml_sycl_debug) return;
  3153. if(!src){
  3154. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3155. return;
  3156. }
  3157. char filename[1024];
  3158. sprintf(filename, "%s.txt", name);
  3159. printf("GGML Tensor:%s save to %s\n", name, filename);
  3160. size_t total_size = total_elements*sizeof(float);
  3161. float *local_buf = NULL;
  3162. if(src_on_device) {
  3163. local_buf = (float *) ggml_sycl_host_malloc(total_size);
  3164. ggml_sycl_set_device(g_main_device);
  3165. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3166. main_stream->memcpy(local_buf, src, total_size).wait();
  3167. }
  3168. else {
  3169. local_buf = (float *)src;
  3170. }
  3171. std::ofstream logfile;
  3172. logfile.open(filename);
  3173. for(size_t i=0; i<total_elements; i++){
  3174. logfile << local_buf[i] <<" ";
  3175. if((i+1)%20 ==0) logfile <<std::endl;
  3176. }
  3177. logfile <<std::endl;
  3178. logfile.close();
  3179. if(src_on_device) ggml_sycl_host_free(local_buf);
  3180. }
  3181. void log_ggml_var_device_fp16(const char*name, sycl::half *src, size_t total_elements, bool src_on_device){
  3182. if(!g_ggml_sycl_debug) return;
  3183. if(!src){
  3184. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3185. return;
  3186. }
  3187. char filename[1024];
  3188. sprintf(filename, "%s.txt", name);
  3189. printf("GGML Tensor:%s save to %s\n", name, filename);
  3190. size_t total_size = total_elements*sizeof(sycl::half);
  3191. sycl::half *local_buf = NULL;
  3192. if(src_on_device) {
  3193. local_buf = (sycl::half *) ggml_sycl_host_malloc(total_size);
  3194. ggml_sycl_set_device(g_main_device);
  3195. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  3196. main_stream->memcpy(local_buf, src, total_size).wait();
  3197. }
  3198. else {
  3199. local_buf = (sycl::half *)src;
  3200. }
  3201. std::ofstream logfile;
  3202. logfile.open(filename);
  3203. for(size_t i=0; i<total_elements; i++){
  3204. logfile << local_buf[i] <<" ";
  3205. if((i+1)%20 ==0) logfile <<std::endl;
  3206. }
  3207. logfile <<std::endl;
  3208. logfile.close();
  3209. if(src_on_device) ggml_sycl_host_free(local_buf);
  3210. }
  3211. //todo: debug for crash in some case
  3212. void print_ggml_tensor(const char*name, struct ggml_tensor *src){
  3213. if(!g_ggml_sycl_debug) return;
  3214. if(!src){
  3215. printf("GGML Tensor:%s skip to save for NULL pointer\n", name);
  3216. return;
  3217. }
  3218. size_t total_elements = ggml_nelements(src);
  3219. const bool src_on_device = src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  3220. float *src_data =NULL;
  3221. if(src_on_device) {
  3222. ggml_tensor_extra_gpu * src_extra = (ggml_tensor_extra_gpu *) src->extra;
  3223. src_data = (float*)src_extra->data_device[g_main_device];
  3224. }
  3225. else {
  3226. src_data = (float *)src->data;
  3227. }
  3228. log_ggml_var_device(name, src_data, total_elements, src_on_device);
  3229. }
  3230. static int log_file_name_idx=0;
  3231. void log_tensor_with_cnt(const char* name, struct ggml_tensor * src, int stop_cnt) {
  3232. stop_cnt = 4;
  3233. if(log_file_name_idx>=stop_cnt) return;
  3234. char filename[1280];
  3235. sprintf(filename, "%s_%07d", name, log_file_name_idx);
  3236. log_file_name_idx++;
  3237. print_ggml_tensor(filename, src);
  3238. }
  3239. static __dpct_inline__ float warp_reduce_sum(float x,
  3240. const sycl::nd_item<3> &item_ct1) {
  3241. #pragma unroll
  3242. for (int mask = 16; mask > 0; mask >>= 1) {
  3243. /*
  3244. DPCT1096:98: The right-most dimension of the work-group used in the SYCL
  3245. kernel that calls this function may be less than "32". The function
  3246. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3247. CPU device. Modify the size of the work-group to ensure that the value
  3248. of the right-most dimension is a multiple of "32".
  3249. */
  3250. x += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), x, mask);
  3251. }
  3252. return x;
  3253. }
  3254. static __dpct_inline__ sycl::float2
  3255. warp_reduce_sum(sycl::float2 a, const sycl::nd_item<3> &item_ct1) {
  3256. #pragma unroll
  3257. for (int mask = 16; mask > 0; mask >>= 1) {
  3258. a.x() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.x(),
  3259. mask);
  3260. a.y() += dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), a.y(),
  3261. mask);
  3262. }
  3263. return a;
  3264. }
  3265. static __dpct_inline__ float warp_reduce_max(float x,
  3266. const sycl::nd_item<3> &item_ct1) {
  3267. #pragma unroll
  3268. for (int mask = 16; mask > 0; mask >>= 1) {
  3269. /*
  3270. DPCT1096:97: The right-most dimension of the work-group used in the SYCL
  3271. kernel that calls this function may be less than "32". The function
  3272. "dpct::permute_sub_group_by_xor" may return an unexpected result on the
  3273. CPU device. Modify the size of the work-group to ensure that the value
  3274. of the right-most dimension is a multiple of "32".
  3275. */
  3276. x = sycl::fmax(x, dpct::permute_sub_group_by_xor(
  3277. item_ct1.get_sub_group(), x, mask));
  3278. }
  3279. return x;
  3280. }
  3281. static __dpct_inline__ float op_repeat(const float a, const float b) {
  3282. return b;
  3283. GGML_UNUSED(a);
  3284. }
  3285. static __dpct_inline__ float op_add(const float a, const float b) {
  3286. return a + b;
  3287. }
  3288. static __dpct_inline__ float op_mul(const float a, const float b) {
  3289. return a * b;
  3290. }
  3291. static __dpct_inline__ float op_div(const float a, const float b) {
  3292. return a / b;
  3293. }
  3294. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3295. static void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3296. int ne0, int ne1, int ne2, int ne3,
  3297. int ne10, int ne11, int ne12, int ne13,
  3298. /*int s0, */ int s1, int s2, int s3,
  3299. /*int s10,*/ int s11, int s12, int s13,
  3300. const sycl::nd_item<3> &item_ct1) {
  3301. const int i0s = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3302. item_ct1.get_local_id(2);
  3303. const int i1 = (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  3304. item_ct1.get_local_id(1));
  3305. const int i2 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3306. item_ct1.get_local_id(0)) /
  3307. ne3;
  3308. const int i3 = (item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  3309. item_ct1.get_local_id(0)) %
  3310. ne3;
  3311. if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3312. return;
  3313. }
  3314. const int i11 = i1 % ne11;
  3315. const int i12 = i2 % ne12;
  3316. const int i13 = i3 % ne13;
  3317. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3318. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3319. const size_t i_dst = i_src0;
  3320. const src0_t * src0_row = src0 + i_src0;
  3321. const src1_t * src1_row = src1 + i_src1;
  3322. dst_t * dst_row = dst + i_dst;
  3323. for (int i0 = i0s; i0 < ne0;
  3324. i0 += item_ct1.get_local_range(2) * item_ct1.get_group_range(2)) {
  3325. const int i10 = i0 % ne10;
  3326. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3327. }
  3328. }
  3329. template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
  3330. static void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
  3331. int ne0, int ne1, int ne2, int ne3,
  3332. int ne10, int ne11, int ne12, int ne13,
  3333. /*int s0, */ int s1, int s2, int s3,
  3334. /*int s10,*/ int s11, int s12, int s13,
  3335. const sycl::nd_item<3> &item_ct1) {
  3336. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3337. item_ct1.get_local_id(2);
  3338. const int i3 = i/(ne2*ne1*ne0);
  3339. const int i2 = (i/(ne1*ne0)) % ne2;
  3340. const int i1 = (i/ne0) % ne1;
  3341. const int i0 = i % ne0;
  3342. if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
  3343. return;
  3344. }
  3345. const int i11 = i1 % ne11;
  3346. const int i12 = i2 % ne12;
  3347. const int i13 = i3 % ne13;
  3348. const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
  3349. const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
  3350. const size_t i_dst = i_src0;
  3351. const src0_t * src0_row = src0 + i_src0;
  3352. const src1_t * src1_row = src1 + i_src1;
  3353. dst_t * dst_row = dst + i_dst;
  3354. const int i10 = i0 % ne10;
  3355. dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
  3356. }
  3357. static void acc_f32(const float * x, const float * y, float * dst, const int ne,
  3358. const int ne10, const int ne11, const int ne12,
  3359. const int nb1, const int nb2, int offset, const sycl::nd_item<3> &item_ct1) {
  3360. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3361. item_ct1.get_local_id(2);
  3362. if (i >= ne) {
  3363. return;
  3364. }
  3365. int src1_idx = i - offset;
  3366. int oz = src1_idx / nb2;
  3367. int oy = (src1_idx - (oz * nb2)) / nb1;
  3368. int ox = src1_idx % nb1;
  3369. if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
  3370. dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
  3371. } else {
  3372. dst[i] = x[i];
  3373. }
  3374. }
  3375. static void gelu_f32(const float * x, float * dst, const int k,
  3376. const sycl::nd_item<3> &item_ct1) {
  3377. const float GELU_COEF_A = 0.044715f;
  3378. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  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 >= k) {
  3382. return;
  3383. }
  3384. float xi = x[i];
  3385. dst[i] = 0.5f * xi *
  3386. (1.0f +
  3387. sycl::tanh(SQRT_2_OVER_PI * xi * (1.0f + GELU_COEF_A * xi * xi)));
  3388. }
  3389. static void silu_f32(const float * x, float * dst, const int k,
  3390. const sycl::nd_item<3> &item_ct1) {
  3391. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3392. item_ct1.get_local_id(2);
  3393. if (i >= k) {
  3394. return;
  3395. }
  3396. dst[i] = x[i] / (1.0f + sycl::native::exp(-x[i]));
  3397. }
  3398. static void gelu_quick_f32(const float *x, float *dst, int k,
  3399. const sycl::nd_item<3> &item_ct1) {
  3400. const float GELU_QUICK_COEF = -1.702f;
  3401. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3402. item_ct1.get_local_id(2);
  3403. if (i >= k) {
  3404. return;
  3405. }
  3406. dst[i] = x[i] * (1.0f / (1.0f + sycl::native::exp(GELU_QUICK_COEF * x[i])));
  3407. }
  3408. static void tanh_f32(const float *x, float *dst, 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] = sycl::tanh((float)(x[i]));
  3416. }
  3417. static void relu_f32(const float * x, float * dst, const int k,
  3418. const sycl::nd_item<3> &item_ct1) {
  3419. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3420. item_ct1.get_local_id(2);
  3421. if (i >= k) {
  3422. return;
  3423. }
  3424. dst[i] = sycl::fmax((float)(x[i]), (float)0);
  3425. }
  3426. static void hardsigmoid_f32(const float * x, float * dst, const int k,
  3427. const sycl::nd_item<3> &item_ct1) {
  3428. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3429. item_ct1.get_local_id(2);
  3430. if (i >= k) {
  3431. return;
  3432. }
  3433. dst[i] = sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3434. }
  3435. static void hardswish_f32(const float * x, float * dst, const int k,
  3436. const sycl::nd_item<3> &item_ct1) {
  3437. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3438. item_ct1.get_local_id(2);
  3439. if (i >= k) {
  3440. return;
  3441. }
  3442. dst[i] = x[i] * sycl::fmin(1.0f, sycl::fmax(0.0f, (x[i] + 3.0f) / 6.0f));
  3443. }
  3444. static void leaky_relu_f32(const float *x, float *dst, const int k, const float negative_slope,
  3445. const sycl::nd_item<3> &item_ct1) {
  3446. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  3447. item_ct1.get_local_id(2);
  3448. if (i >= k) {
  3449. return;
  3450. }
  3451. dst[i] = sycl::fmax((float)(x[i]), (float)0) +
  3452. sycl::fmin((float)(x[i]), 0.0f) * negative_slope;
  3453. }
  3454. static void sqr_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] * x[i];
  3462. }
  3463. static void norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3464. const sycl::nd_item<3> &item_ct1, sycl::float2 *s_sum, int block_size) {
  3465. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3466. item_ct1.get_local_id(1);
  3467. const int tid = item_ct1.get_local_id(2);
  3468. sycl::float2 mean_var = sycl::float2(0.f, 0.f);
  3469. for (int col = tid; col < ncols; col += block_size) {
  3470. const float xi = x[row*ncols + col];
  3471. mean_var.x() += xi;
  3472. mean_var.y() += xi * xi;
  3473. }
  3474. // sum up partial sums
  3475. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3476. if (block_size > WARP_SIZE) {
  3477. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3478. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3479. if (lane_id == 0) {
  3480. s_sum[warp_id] = mean_var;
  3481. }
  3482. /*
  3483. DPCT1118:0: SYCL group functions and algorithms must be encountered in
  3484. converged control flow. You may need to adjust the code.
  3485. */
  3486. item_ct1.barrier(sycl::access::fence_space::local_space);
  3487. mean_var = s_sum[lane_id];
  3488. mean_var = warp_reduce_sum(mean_var, item_ct1);
  3489. }
  3490. const float mean = mean_var.x() / ncols;
  3491. const float var = mean_var.y() / ncols - mean * mean;
  3492. const float inv_std = sycl::rsqrt(var + eps);
  3493. for (int col = tid; col < ncols; col += block_size) {
  3494. dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
  3495. }
  3496. }
  3497. static void concat_f32(const float *x,const float *y, float *dst, const int ne0, const int ne02,
  3498. const sycl::nd_item<3> &item_ct1) {
  3499. int nidx = item_ct1.get_local_id(2) +
  3500. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3501. if (nidx >= ne0) {
  3502. return;
  3503. }
  3504. // operation
  3505. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3506. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3507. if (item_ct1.get_group(0) < ne02) { // src0
  3508. int offset_src =
  3509. nidx + item_ct1.get_group(1) * ne0 +
  3510. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3511. dst[offset_dst] = x[offset_src];
  3512. } else {
  3513. int offset_src =
  3514. nidx + item_ct1.get_group(1) * ne0 +
  3515. (item_ct1.get_group(0) - ne02) * ne0 * item_ct1.get_group_range(1);
  3516. dst[offset_dst] = y[offset_src];
  3517. }
  3518. }
  3519. static void upscale_f32(const float *x, float *dst, const int ne00, const int nb02, const int scale_factor,
  3520. const sycl::nd_item<3> &item_ct1) {
  3521. int ne0 = ne00 * scale_factor;
  3522. int nidx = item_ct1.get_local_id(2) +
  3523. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3524. if (nidx >= ne0) {
  3525. return;
  3526. }
  3527. // operation
  3528. int i00 = nidx / scale_factor;
  3529. int i01 = item_ct1.get_group(1) / scale_factor;
  3530. int offset_src = i00 + i01 * ne00 + item_ct1.get_group(0) * nb02;
  3531. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3532. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3533. dst[offset_dst] = x[offset_src];
  3534. }
  3535. static void pad_f32(const float *x, float *dst, const int ne0, const int ne00, const int ne01, const int ne02,
  3536. const sycl::nd_item<3> &item_ct1) {
  3537. int nidx = item_ct1.get_local_id(2) +
  3538. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  3539. if (nidx >= ne0) {
  3540. return;
  3541. }
  3542. // operation
  3543. int offset_dst = nidx + item_ct1.get_group(1) * ne0 +
  3544. item_ct1.get_group(0) * ne0 * item_ct1.get_group_range(1);
  3545. if (nidx < ne00 && item_ct1.get_group(1) < ne01 &&
  3546. item_ct1.get_group(0) < ne02) {
  3547. int offset_src = nidx + item_ct1.get_group(1) * ne00 +
  3548. item_ct1.get_group(0) * ne00 * ne01;
  3549. dst[offset_dst] = x[offset_src];
  3550. } else {
  3551. dst[offset_dst] = 0.0f;
  3552. }
  3553. }
  3554. static void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps,
  3555. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3556. int start = item_ct1.get_group(2) * group_size;
  3557. int end = start + group_size;
  3558. start += item_ct1.get_local_id(2);
  3559. if (end >= ne_elements) {
  3560. end = ne_elements;
  3561. }
  3562. float tmp = 0.0f; // partial sum for thread in warp
  3563. for (int j = start; j < end; j += block_size) {
  3564. tmp += x[j];
  3565. }
  3566. tmp = warp_reduce_sum(tmp, item_ct1);
  3567. if (block_size > WARP_SIZE) {
  3568. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3569. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3570. if (lane_id == 0) {
  3571. s_sum[warp_id] = tmp;
  3572. }
  3573. /*
  3574. DPCT1118:1: SYCL group functions and algorithms must be encountered in
  3575. converged control flow. You may need to adjust the code.
  3576. */
  3577. /*
  3578. DPCT1065:54: Consider replacing sycl::nd_item::barrier() with
  3579. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3580. better performance if there is no access to global memory.
  3581. */
  3582. item_ct1.barrier();
  3583. tmp = s_sum[lane_id];
  3584. tmp = warp_reduce_sum(tmp, item_ct1);
  3585. }
  3586. float mean = tmp / group_size;
  3587. tmp = 0.0f;
  3588. for (int j = start; j < end; j += block_size) {
  3589. float xi = x[j] - mean;
  3590. dst[j] = xi;
  3591. tmp += xi * xi;
  3592. }
  3593. tmp = warp_reduce_sum(tmp, item_ct1);
  3594. if (block_size > WARP_SIZE) {
  3595. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3596. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3597. if (lane_id == 0) {
  3598. s_sum[warp_id] = tmp;
  3599. }
  3600. /*
  3601. DPCT1118:2: SYCL group functions and algorithms must be encountered in
  3602. converged control flow. You may need to adjust the code.
  3603. */
  3604. /*
  3605. DPCT1065:55: Consider replacing sycl::nd_item::barrier() with
  3606. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  3607. better performance if there is no access to global memory.
  3608. */
  3609. item_ct1.barrier();
  3610. tmp = s_sum[lane_id];
  3611. tmp = warp_reduce_sum(tmp, item_ct1);
  3612. }
  3613. float variance = tmp / group_size;
  3614. float scale = sycl::rsqrt(variance + eps);
  3615. for (int j = start; j < end; j += block_size) {
  3616. dst[j] *= scale;
  3617. }
  3618. }
  3619. static void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps,
  3620. const sycl::nd_item<3> &item_ct1, float *s_sum, int block_size) {
  3621. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  3622. item_ct1.get_local_id(1);
  3623. const int tid = item_ct1.get_local_id(2);
  3624. float tmp = 0.0f; // partial sum for thread in warp
  3625. for (int col = tid; col < ncols; col += block_size) {
  3626. const float xi = x[row*ncols + col];
  3627. tmp += xi * xi;
  3628. }
  3629. // sum up partial sums
  3630. tmp = warp_reduce_sum(tmp, item_ct1);
  3631. if (block_size > WARP_SIZE) {
  3632. int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  3633. int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  3634. if (lane_id == 0) {
  3635. s_sum[warp_id] = tmp;
  3636. }
  3637. /*
  3638. DPCT1118:3: SYCL group functions and algorithms must be encountered in
  3639. converged control flow. You may need to adjust the code.
  3640. */
  3641. item_ct1.barrier(sycl::access::fence_space::local_space);
  3642. tmp = s_sum[lane_id];
  3643. tmp = warp_reduce_sum(tmp, item_ct1);
  3644. }
  3645. const float mean = tmp / ncols;
  3646. const float scale = sycl::rsqrt(mean + eps);
  3647. for (int col = tid; col < ncols; col += block_size) {
  3648. dst[row*ncols + col] = scale * x[row*ncols + col];
  3649. }
  3650. }
  3651. static __dpct_inline__ void dequantize_q4_0(const void *vx, const int ib,
  3652. const int iqs, dfloat2 &v) {
  3653. const block_q4_0 * x = (const block_q4_0 *) vx;
  3654. const dfloat d = x[ib].d;
  3655. const int vui = x[ib].qs[iqs];
  3656. v.x() = vui & 0xF;
  3657. v.y() = vui >> 4;
  3658. #ifdef GGML_SYCL_F16
  3659. // v = v - {8.0f, 8.0f};
  3660. // v = v * {d, d};
  3661. v.s0() = (v.s0() - 8.0f) * d;
  3662. v.s1() = (v.s1() - 8.0f) * d;
  3663. #else
  3664. v.x() = (v.x() - 8.0f) * d;
  3665. v.y() = (v.y() - 8.0f) * d;
  3666. #endif // GGML_SYCL_F16
  3667. }
  3668. static __dpct_inline__ void dequantize_q4_1(const void *vx, const int ib,
  3669. const int iqs, dfloat2 &v) {
  3670. const block_q4_1 * x = (const block_q4_1 *) vx;
  3671. const dfloat d = x[ib].dm[0];
  3672. const dfloat m = x[ib].dm[1];
  3673. const int vui = x[ib].qs[iqs];
  3674. v.x() = vui & 0xF;
  3675. v.y() = vui >> 4;
  3676. #ifdef GGML_SYCL_F16
  3677. // v = v * {d, d};
  3678. // v = v + {m, m};
  3679. v.s0() = (v.s0() * d) + m;
  3680. v.s1() = (v.s1() * d) + m;
  3681. #else
  3682. v.x() = (v.x() * d) + m;
  3683. v.y() = (v.y() * d) + m;
  3684. #endif // GGML_SYCL_F16
  3685. }
  3686. static __dpct_inline__ void dequantize_q5_0(const void *vx, const int ib,
  3687. const int iqs, dfloat2 &v) {
  3688. const block_q5_0 * x = (const block_q5_0 *) vx;
  3689. const dfloat d = x[ib].d;
  3690. uint32_t qh;
  3691. memcpy(&qh, x[ib].qh, sizeof(qh));
  3692. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3693. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3694. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3695. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3696. #ifdef GGML_SYCL_F16
  3697. // v = v - {16.0f, 16.0f};
  3698. // v = v * {d, d};
  3699. v.s0() = (v.s0() - 16.0f) * d;
  3700. v.s1() = (v.s1() - 16.0f) * d;
  3701. #else
  3702. v.x() = (v.x() - 16.0f) * d;
  3703. v.y() = (v.y() - 16.0f) * d;
  3704. #endif // GGML_SYCL_F16
  3705. }
  3706. static __dpct_inline__ void dequantize_q5_1(const void *vx, const int ib,
  3707. const int iqs, dfloat2 &v) {
  3708. const block_q5_1 * x = (const block_q5_1 *) vx;
  3709. const dfloat d = x[ib].dm[0];
  3710. const dfloat m = x[ib].dm[1];
  3711. uint32_t qh;
  3712. memcpy(&qh, x[ib].qh, sizeof(qh));
  3713. const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
  3714. const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
  3715. v.x() = ((x[ib].qs[iqs] & 0xf) | xh_0);
  3716. v.y() = ((x[ib].qs[iqs] >> 4) | xh_1);
  3717. #ifdef GGML_SYCL_F16
  3718. // v = v * {d, d};
  3719. // v = v + {m, m};
  3720. v.s0() = (v.s0() * d) + m;
  3721. v.s1() = (v.s1() * d) + m;
  3722. #else
  3723. v.x() = (v.x() * d) + m;
  3724. v.y() = (v.y() * d) + m;
  3725. #endif // GGML_SYCL_F16
  3726. }
  3727. static __dpct_inline__ void dequantize_q8_0(const void *vx, const int ib,
  3728. const int iqs, dfloat2 &v) {
  3729. const block_q8_0 * x = (const block_q8_0 *) vx;
  3730. const dfloat d = x[ib].d;
  3731. v.x() = x[ib].qs[iqs + 0];
  3732. v.y() = x[ib].qs[iqs + 1];
  3733. #ifdef GGML_SYCL_F16
  3734. // v = v * {d, d};
  3735. v.s0() *= d;
  3736. v.s1() *= d;
  3737. #else
  3738. v.x() *= d;
  3739. v.y() *= d;
  3740. #endif // GGML_SYCL_F16
  3741. }
  3742. template<typename dst_t>
  3743. static void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32,
  3744. const sycl::nd_item<3> &item_ct1) {
  3745. const int i = item_ct1.get_group(2);
  3746. // assume 32 threads
  3747. const int tid = item_ct1.get_local_id(2);
  3748. const int il = tid/8;
  3749. const int ir = tid%8;
  3750. const int ib = 8*i + ir;
  3751. if (ib >= nb32) {
  3752. return;
  3753. }
  3754. dst_t * y = yy + 256*i + 32*ir + 4*il;
  3755. const block_q4_0 * x = (const block_q4_0 *)vx + ib;
  3756. const float d = sycl::vec<sycl::half, 1>(x->d)
  3757. .convert<float, sycl::rounding_mode::automatic>()[0];
  3758. const float dm = -8*d;
  3759. const uint8_t * q = x->qs + 4*il;
  3760. for (int l = 0; l < 4; ++l) {
  3761. y[l+ 0] = d * (q[l] & 0xF) + dm;
  3762. y[l+16] = d * (q[l] >> 4) + dm;
  3763. }
  3764. }
  3765. template<typename dst_t>
  3766. static void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32,
  3767. const sycl::nd_item<3> &item_ct1) {
  3768. const int i = item_ct1.get_group(2);
  3769. // assume 32 threads
  3770. const int tid = item_ct1.get_local_id(2);
  3771. const int il = tid/8;
  3772. const int ir = tid%8;
  3773. const int ib = 8*i + ir;
  3774. if (ib >= nb32) {
  3775. return;
  3776. }
  3777. dst_t * y = yy + 256*i + 32*ir + 4*il;
  3778. const block_q4_1 * x = (const block_q4_1 *)vx + ib;
  3779. const sycl::float2 d =
  3780. x->dm.convert<float, sycl::rounding_mode::automatic>();
  3781. const uint8_t * q = x->qs + 4*il;
  3782. for (int l = 0; l < 4; ++l) {
  3783. y[l + 0] = d.x() * (q[l] & 0xF) + d.y();
  3784. y[l + 16] = d.x() * (q[l] >> 4) + d.y();
  3785. }
  3786. }
  3787. //================================== k-quants
  3788. template<typename dst_t>
  3789. static void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3790. const sycl::nd_item<3> &item_ct1) {
  3791. const int i = item_ct1.get_group(2);
  3792. const block_q2_K * x = (const block_q2_K *) vx;
  3793. const int tid = item_ct1.get_local_id(2);
  3794. #if QK_K == 256
  3795. const int n = tid/32;
  3796. const int l = tid - 32*n;
  3797. const int is = 8*n + l/16;
  3798. const uint8_t q = x[i].qs[32*n + l];
  3799. dst_t * y = yy + i*QK_K + 128*n;
  3800. float dall = x[i].dm[0];
  3801. float dmin = x[i].dm[1];
  3802. y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3803. y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3804. y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
  3805. y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
  3806. #else
  3807. const int is = tid/16; // 0 or 1
  3808. const int il = tid%16; // 0...15
  3809. const uint8_t q = x[i].qs[il] >> (2*is);
  3810. dst_t * y = yy + i*QK_K + 16*is + il;
  3811. float dall = x[i].dm[0];
  3812. float dmin = x[i].dm[1];
  3813. y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
  3814. y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
  3815. #endif
  3816. }
  3817. template<typename dst_t>
  3818. static void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3819. const sycl::nd_item<3> &item_ct1) {
  3820. const int i = item_ct1.get_group(2);
  3821. const block_q3_K * x = (const block_q3_K *) vx;
  3822. #if QK_K == 256
  3823. const int r = item_ct1.get_local_id(2) / 4;
  3824. const int tid = r/2;
  3825. const int is0 = r%2;
  3826. const int l0 = 16 * is0 + 4 * (item_ct1.get_local_id(2) % 4);
  3827. const int n = tid / 4;
  3828. const int j = tid - 4*n;
  3829. uint8_t m = 1 << (4*n + j);
  3830. int is = 8*n + 2*j + is0;
  3831. int shift = 2*j;
  3832. int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
  3833. is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
  3834. is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
  3835. (x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
  3836. float d_all = x[i].d;
  3837. float dl = d_all * (us - 32);
  3838. dst_t * y = yy + i*QK_K + 128*n + 32*j;
  3839. const uint8_t * q = x[i].qs + 32*n;
  3840. const uint8_t * hm = x[i].hmask;
  3841. for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
  3842. #else
  3843. const int tid = item_ct1.get_local_id(2);
  3844. const int is = tid/16; // 0 or 1
  3845. const int il = tid%16; // 0...15
  3846. const int im = il/8; // 0...1
  3847. const int in = il%8; // 0...7
  3848. dst_t * y = yy + i*QK_K + 16*is + il;
  3849. const uint8_t q = x[i].qs[il] >> (2*is);
  3850. const uint8_t h = x[i].hmask[in] >> (2*is + im);
  3851. const float d = (float)x[i].d;
  3852. if (is == 0) {
  3853. y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  3854. y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  3855. } else {
  3856. y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
  3857. y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
  3858. }
  3859. #endif
  3860. }
  3861. #if QK_K == 256
  3862. static inline void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
  3863. if (j < 4) {
  3864. d = q[j] & 63; m = q[j + 4] & 63;
  3865. } else {
  3866. d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  3867. m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  3868. }
  3869. }
  3870. #endif
  3871. template<typename dst_t>
  3872. static void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3873. const sycl::nd_item<3> &item_ct1) {
  3874. const block_q4_K * x = (const block_q4_K *) vx;
  3875. const int i = item_ct1.get_group(2);
  3876. #if QK_K == 256
  3877. // assume 32 threads
  3878. const int tid = item_ct1.get_local_id(2);
  3879. const int il = tid/8;
  3880. const int ir = tid%8;
  3881. const int is = 2*il;
  3882. const int n = 4;
  3883. dst_t * y = yy + i*QK_K + 64*il + n*ir;
  3884. const float dall = x[i].dm[0];
  3885. const float dmin = x[i].dm[1];
  3886. const uint8_t * q = x[i].qs + 32*il + n*ir;
  3887. uint8_t sc, m;
  3888. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3889. const float d1 = dall * sc; const float m1 = dmin * m;
  3890. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3891. const float d2 = dall * sc; const float m2 = dmin * m;
  3892. for (int l = 0; l < n; ++l) {
  3893. y[l + 0] = d1 * (q[l] & 0xF) - m1;
  3894. y[l +32] = d2 * (q[l] >> 4) - m2;
  3895. }
  3896. #else
  3897. const int tid = item_ct1.get_local_id(2);
  3898. const uint8_t * q = x[i].qs;
  3899. dst_t * y = yy + i*QK_K;
  3900. const float d = (float)x[i].dm[0];
  3901. const float m = (float)x[i].dm[1];
  3902. y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
  3903. y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
  3904. #endif
  3905. }
  3906. template<typename dst_t>
  3907. static void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3908. const sycl::nd_item<3> &item_ct1) {
  3909. const block_q5_K * x = (const block_q5_K *) vx;
  3910. const int i = item_ct1.get_group(2);
  3911. #if QK_K == 256
  3912. // assume 64 threads - this is very slightly better than the one below
  3913. const int tid = item_ct1.get_local_id(2);
  3914. const int il = tid/16; // il is in 0...3
  3915. const int ir = tid%16; // ir is in 0...15
  3916. const int is = 2*il; // is is in 0...6
  3917. dst_t * y = yy + i*QK_K + 64*il + 2*ir;
  3918. const float dall = x[i].dm[0];
  3919. const float dmin = x[i].dm[1];
  3920. const uint8_t * ql = x[i].qs + 32*il + 2*ir;
  3921. const uint8_t * qh = x[i].qh + 2*ir;
  3922. uint8_t sc, m;
  3923. get_scale_min_k4(is + 0, x[i].scales, sc, m);
  3924. const float d1 = dall * sc; const float m1 = dmin * m;
  3925. get_scale_min_k4(is + 1, x[i].scales, sc, m);
  3926. const float d2 = dall * sc; const float m2 = dmin * m;
  3927. uint8_t hm = 1 << (2*il);
  3928. y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
  3929. y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
  3930. hm <<= 1;
  3931. y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
  3932. y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
  3933. #else
  3934. const int tid = item_ct1.get_local_id(2);
  3935. const uint8_t q = x[i].qs[tid];
  3936. const int im = tid/8; // 0...3
  3937. const int in = tid%8; // 0...7
  3938. const int is = tid/16; // 0 or 1
  3939. const uint8_t h = x[i].qh[in] >> im;
  3940. const float d = x[i].d;
  3941. dst_t * y = yy + i*QK_K + tid;
  3942. y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
  3943. y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
  3944. #endif
  3945. }
  3946. template<typename dst_t>
  3947. static void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3948. const sycl::nd_item<3> &item_ct1) {
  3949. const block_q6_K * x = (const block_q6_K *) vx;
  3950. const int i = item_ct1.get_group(2);
  3951. #if QK_K == 256
  3952. // assume 64 threads - this is very slightly better than the one below
  3953. const int tid = item_ct1.get_local_id(2);
  3954. const int ip = tid/32; // ip is 0 or 1
  3955. const int il = tid - 32*ip; // 0...32
  3956. const int is = 8*ip + il/16;
  3957. dst_t * y = yy + i*QK_K + 128*ip + il;
  3958. const float d = x[i].d;
  3959. const uint8_t * ql = x[i].ql + 64*ip + il;
  3960. const uint8_t qh = x[i].qh[32*ip + il];
  3961. const int8_t * sc = x[i].scales + is;
  3962. y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3963. y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
  3964. y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3965. y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
  3966. #else
  3967. // assume 32 threads
  3968. const int tid = item_ct1.get_local_id(2);
  3969. const int ip = tid/16; // 0 or 1
  3970. const int il = tid - 16*ip; // 0...15
  3971. dst_t * y = yy + i*QK_K + 16*ip + il;
  3972. const float d = x[i].d;
  3973. const uint8_t ql = x[i].ql[16*ip + il];
  3974. const uint8_t qh = x[i].qh[il] >> (2*ip);
  3975. const int8_t * sc = x[i].scales;
  3976. y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
  3977. y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
  3978. #endif
  3979. }
  3980. template<typename dst_t>
  3981. static void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  3982. const sycl::nd_item<3> &item_ct1,
  3983. const uint64_t *iq2xxs_grid_ptr,
  3984. const uint8_t *ksigns_iq2xs_ptr,
  3985. const uint8_t *kmask_iq2xs_ptr) {
  3986. const int i = item_ct1.get_group(2);
  3987. const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
  3988. const int tid = item_ct1.get_local_id(2);
  3989. #if QK_K == 256
  3990. const int il = tid/8; // 0...3
  3991. const int ib = tid%8; // 0...7
  3992. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  3993. const uint16_t * q2 = x[i].qs + 4*ib;
  3994. const uint8_t * aux8 = (const uint8_t *)q2;
  3995. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid_ptr + aux8[il]);
  3996. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  3997. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
  3998. const uint8_t signs = ksigns_iq2xs_ptr[(aux32 >> 7*il) & 127];
  3999. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs_ptr[j] ? -1.f : 1.f);
  4000. #else
  4001. assert(false);
  4002. #endif
  4003. }
  4004. template<typename dst_t>
  4005. static void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4006. const sycl::nd_item<3> &item_ct1,
  4007. const uint64_t *iq2xs_grid,
  4008. const uint8_t *ksigns_iq2xs,
  4009. const uint8_t *kmask_iq2xs) {
  4010. const int i = item_ct1.get_group(2);
  4011. const block_iq2_xs * x = (const block_iq2_xs *) vx;
  4012. const int tid = item_ct1.get_local_id(2);
  4013. #if QK_K == 256
  4014. const int il = tid/8; // 0...3
  4015. const int ib = tid%8; // 0...7
  4016. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4017. const uint16_t * q2 = x[i].qs + 4*ib;
  4018. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
  4019. const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
  4020. const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
  4021. for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  4022. #else
  4023. assert(false);
  4024. #endif
  4025. }
  4026. template<typename dst_t>
  4027. static void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4028. const sycl::nd_item<3> &item_ct1,
  4029. const uint32_t *iq3xxs_grid,
  4030. const uint8_t *ksigns_iq2xs,
  4031. const uint8_t *kmask_iq2xs) {
  4032. const int i = item_ct1.get_group(2);
  4033. const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
  4034. const int tid = item_ct1.get_local_id(2);
  4035. #if QK_K == 256
  4036. const int il = tid/8; // 0...3
  4037. const int ib = tid%8; // 0...7
  4038. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4039. const uint8_t * q3 = x[i].qs + 8*ib;
  4040. const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
  4041. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
  4042. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
  4043. const uint32_t aux32 = gas[0] | (gas[1] << 16);
  4044. const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
  4045. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
  4046. for (int j = 0; j < 4; ++j) {
  4047. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4048. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4049. }
  4050. #else
  4051. assert(false);
  4052. #endif
  4053. }
  4054. template<typename dst_t>
  4055. static void dequantize_block_iq3_s(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4056. const sycl::nd_item<3> &item_ct1,
  4057. const uint32_t *iq3s_grid,
  4058. const uint8_t *ksigns_iq2xs,
  4059. const uint8_t *kmask_iq2xs) {
  4060. const int i = item_ct1.get_group(2);
  4061. const block_iq3_s * x = (const block_iq3_s *) vx;
  4062. const int tid = item_ct1.get_local_id(2);
  4063. #if QK_K == 256
  4064. const int il = tid/8; // 0...3
  4065. const int ib = tid%8; // 0...7
  4066. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4067. const uint8_t * qs = x[i].qs + 8*ib;
  4068. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + qs[2*il+0]);
  4069. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + qs[2*il+1]);
  4070. const float d = (float)x[i].d * (1 + 2*((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf));
  4071. const uint8_t signs = x[i].signs[4*ib + il];
  4072. for (int j = 0; j < 4; ++j) {
  4073. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4074. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4075. }
  4076. #else
  4077. assert(false);
  4078. #endif
  4079. }
  4080. template<typename dst_t>
  4081. static void dequantize_block_iq1_s(const void * __restrict__ vx, dst_t * __restrict__ yy,
  4082. const sycl::nd_item<3> &item_ct1,
  4083. const uint32_t *iq1s_grid,
  4084. const uint8_t *ksigns_iq2xs,
  4085. const uint8_t *kmask_iq2xs) {
  4086. const int i = item_ct1.get_group(2);
  4087. const block_iq1_s * x = (const block_iq1_s *) vx;
  4088. const int tid = item_ct1.get_local_id(2);
  4089. #if QK_K == 256
  4090. const int il = tid/8; // 0...3
  4091. const int ib = tid%8; // 0...7
  4092. dst_t * y = yy + i*QK_K + 32*ib + 8*il;
  4093. const uint8_t * qs = x[i].qs + 8*ib;
  4094. const uint8_t * grid1 = (const uint8_t *)(iq1s_grid + qs[2*il+0]);
  4095. const uint8_t * grid2 = (const uint8_t *)(iq1s_grid + qs[2*il+1]);
  4096. const float d = (float)x[i].d * (2*((x[i].qh[ib] >> 12) & 0xf) + 1);
  4097. const uint8_t signs = ksigns_iq2xs[(x[i].qh[ib] >> 3*il) & 7];
  4098. for (int j = 0; j < 4; ++j) {
  4099. y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  4100. y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  4101. }
  4102. #else
  4103. assert(false);
  4104. #endif
  4105. }
  4106. /*
  4107. DPCT1110:4: The total declared local variable size in device function
  4108. dequantize_mul_mat_vec_q2_k exceeds 128 bytes and may cause high register
  4109. pressure. Consult with your hardware vendor to find the total register size
  4110. available and adjust the code, or use smaller sub-group size to avoid high
  4111. register pressure.
  4112. */
  4113. static void dequantize_mul_mat_vec_q2_k(const void *__restrict__ vx,
  4114. const float *__restrict__ yy,
  4115. float *__restrict__ dst,
  4116. const int ncols, int nrows,
  4117. const sycl::nd_item<3> &item_ct1) {
  4118. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4119. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4120. item_ct1.get_local_id(1);
  4121. if (row > nrows) return;
  4122. const int num_blocks_per_row = ncols / QK_K;
  4123. const int ib0 = row*num_blocks_per_row;
  4124. const block_q2_K * x = (const block_q2_K *)vx + ib0;
  4125. float tmp = 0; // partial sum for thread in warp
  4126. #if QK_K == 256
  4127. const int tid =
  4128. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...15
  4129. const int ix =
  4130. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4131. const int step = 16/K_QUANTS_PER_ITERATION;
  4132. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4133. const int in = tid - step*im; // 0...15 or 0...7
  4134. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
  4135. const int q_offset = 32*im + l0;
  4136. const int s_offset = 8*im;
  4137. const int y_offset = 128*im + l0;
  4138. uint32_t aux[4];
  4139. const uint8_t * d = (const uint8_t *)aux;
  4140. const uint8_t * m = (const uint8_t *)(aux + 2);
  4141. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4142. const float * y = yy + i * QK_K + y_offset;
  4143. const uint8_t * q = x[i].qs + q_offset;
  4144. const float dall = x[i].dm[0];
  4145. const float dmin = x[i].dm[1];
  4146. const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
  4147. aux[0] = a[0] & 0x0f0f0f0f;
  4148. aux[1] = a[1] & 0x0f0f0f0f;
  4149. aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
  4150. aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
  4151. float sum1 = 0, sum2 = 0;
  4152. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4153. sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
  4154. + y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
  4155. + y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
  4156. + y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
  4157. + y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
  4158. + y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
  4159. + y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
  4160. +y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
  4161. sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
  4162. + y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
  4163. }
  4164. tmp += dall * sum1 - dmin * sum2;
  4165. }
  4166. #else
  4167. const int tid = item_ct1.get_local_id(2) /
  4168. (2 * K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  4169. const int ix = item_ct1.get_local_id(2) %
  4170. (2 * K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  4171. const int offset = tid * K_QUANTS_PER_ITERATION;
  4172. uint32_t uaux[2];
  4173. const uint8_t * d = (const uint8_t *)uaux;
  4174. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4175. const float * y = yy + i * QK_K + offset;
  4176. const uint8_t * q = x[i].qs + offset;
  4177. const uint32_t * s = (const uint32_t *)x[i].scales;
  4178. uaux[0] = s[0] & 0x0f0f0f0f;
  4179. uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
  4180. const sycl::float2 dall =
  4181. x[i].dm.convert<float, sycl::rounding_mode::automatic>();
  4182. float sum1 = 0, sum2 = 0;
  4183. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4184. const uint8_t ql = q[l];
  4185. sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
  4186. + y[l+16] * d[1] * ((ql >> 2) & 3)
  4187. + y[l+32] * d[2] * ((ql >> 4) & 3)
  4188. + y[l+48] * d[3] * ((ql >> 6) & 3);
  4189. sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
  4190. }
  4191. tmp += dall.x() * sum1 - dall.y() * sum2;
  4192. }
  4193. #endif
  4194. // sum up partial sums and write back result
  4195. #pragma unroll
  4196. for (int mask = 16; mask > 0; mask >>= 1) {
  4197. tmp +=
  4198. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4199. }
  4200. if (item_ct1.get_local_id(2) == 0) {
  4201. dst[row] = tmp;
  4202. }
  4203. }
  4204. /*
  4205. DPCT1110:5: The total declared local variable size in device function
  4206. dequantize_mul_mat_vec_q3_k exceeds 128 bytes and may cause high register
  4207. pressure. Consult with your hardware vendor to find the total register size
  4208. available and adjust the code, or use smaller sub-group size to avoid high
  4209. register pressure.
  4210. */
  4211. static void dequantize_mul_mat_vec_q3_k(const void *__restrict__ vx,
  4212. const float *__restrict__ yy,
  4213. float *__restrict__ dst,
  4214. const int ncols, int nrows,
  4215. const sycl::nd_item<3> &item_ct1) {
  4216. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4217. item_ct1.get_local_id(1);
  4218. if (row > nrows) return;
  4219. const int num_blocks_per_row = ncols / QK_K;
  4220. const int ib0 = row*num_blocks_per_row;
  4221. const block_q3_K * x = (const block_q3_K *)vx + ib0;
  4222. float tmp = 0; // partial sum for thread in warp
  4223. #if QK_K == 256
  4224. const uint16_t kmask1 = 0x0303;
  4225. const uint16_t kmask2 = 0x0f0f;
  4226. const int tid =
  4227. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4228. const int ix =
  4229. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4230. const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
  4231. const int step = 16/K_QUANTS_PER_ITERATION;
  4232. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4233. const int in = tid - step*im; // 0....15 or 0...7
  4234. const uint8_t m = 1 << (4*im);
  4235. const int l0 = n*in; // 0...15 or 0...14 in steps of 2
  4236. const int q_offset = 32*im + l0;
  4237. const int y_offset = 128*im + l0;
  4238. uint16_t utmp[4];
  4239. const int8_t * s = (const int8_t *)utmp;
  4240. const uint16_t s_shift = 4*im;
  4241. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4242. const float * y = yy + i * QK_K + y_offset;
  4243. const uint8_t * q = x[i].qs + q_offset;
  4244. const uint8_t * h = x[i].hmask + l0;
  4245. const uint16_t * a = (const uint16_t *)x[i].scales;
  4246. utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
  4247. utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
  4248. utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
  4249. utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
  4250. const float d = x[i].d;
  4251. float sum = 0;
  4252. for (int l = 0; l < n; ++l) {
  4253. sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
  4254. + y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
  4255. + y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
  4256. + y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
  4257. sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
  4258. + y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
  4259. + y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
  4260. + y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
  4261. }
  4262. tmp += d * sum;
  4263. }
  4264. #else
  4265. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
  4266. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
  4267. const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
  4268. const int in = offset/8; // 0 or 1
  4269. const int im = offset%8; // 0...7
  4270. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4271. const float * y = yy + i * QK_K + offset;
  4272. const uint8_t * q = x[i].qs + offset;
  4273. const uint8_t * s = x[i].scales;
  4274. const float dall = (float)x[i].d;
  4275. float sum = 0;
  4276. for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
  4277. const uint8_t hl = x[i].hmask[im+l] >> in;
  4278. const uint8_t ql = q[l];
  4279. sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
  4280. + y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
  4281. + y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
  4282. + y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
  4283. }
  4284. tmp += sum;
  4285. }
  4286. #endif
  4287. // sum up partial sums and write back result
  4288. #pragma unroll
  4289. for (int mask = 16; mask > 0; mask >>= 1) {
  4290. tmp +=
  4291. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4292. }
  4293. if (item_ct1.get_local_id(2) == 0) {
  4294. dst[row] = tmp;
  4295. }
  4296. }
  4297. /*
  4298. DPCT1110:6: The total declared local variable size in device function
  4299. dequantize_mul_mat_vec_q4_k exceeds 128 bytes and may cause high register
  4300. pressure. Consult with your hardware vendor to find the total register size
  4301. available and adjust the code, or use smaller sub-group size to avoid high
  4302. register pressure.
  4303. */
  4304. static void dequantize_mul_mat_vec_q4_k(const void *__restrict__ vx,
  4305. const float *__restrict__ yy,
  4306. float *__restrict__ dst,
  4307. const int ncols, int nrows,
  4308. const sycl::nd_item<3> &item_ct1) {
  4309. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4310. item_ct1.get_local_id(1);
  4311. if (row > nrows) return;
  4312. const int num_blocks_per_row = ncols / QK_K;
  4313. const int ib0 = row*num_blocks_per_row;
  4314. const block_q4_K * x = (const block_q4_K *)vx + ib0;
  4315. #if QK_K == 256
  4316. const uint16_t kmask1 = 0x3f3f;
  4317. const uint16_t kmask2 = 0x0f0f;
  4318. const uint16_t kmask3 = 0xc0c0;
  4319. const int tid =
  4320. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4321. const int ix =
  4322. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0,1
  4323. const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
  4324. const int il = tid/step; // 0...3
  4325. const int ir = tid - step*il; // 0...7 or 0...3
  4326. const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
  4327. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4328. const int in = il%2;
  4329. const int l0 = n*(2*ir + in);
  4330. const int q_offset = 32*im + l0;
  4331. const int y_offset = 64*im + l0;
  4332. uint16_t aux[4];
  4333. const uint8_t * sc = (const uint8_t *)aux;
  4334. #if K_QUANTS_PER_ITERATION == 2
  4335. uint32_t q32[4];
  4336. const uint8_t * q4 = (const uint8_t *)q32;
  4337. #else
  4338. uint16_t q16[4];
  4339. const uint8_t * q4 = (const uint8_t *)q16;
  4340. #endif
  4341. float tmp = 0; // partial sum for thread in warp
  4342. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4343. const float * y1 = yy + i*QK_K + y_offset;
  4344. const float * y2 = y1 + 128;
  4345. const float dall = x[i].dm[0];
  4346. const float dmin = x[i].dm[1];
  4347. const uint16_t * a = (const uint16_t *)x[i].scales;
  4348. aux[0] = a[im+0] & kmask1;
  4349. aux[1] = a[im+2] & kmask1;
  4350. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4351. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4352. #if K_QUANTS_PER_ITERATION == 2
  4353. const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
  4354. const uint32_t * q2 = q1 + 16;
  4355. q32[0] = q1[0] & 0x0f0f0f0f;
  4356. q32[1] = q1[0] & 0xf0f0f0f0;
  4357. q32[2] = q2[0] & 0x0f0f0f0f;
  4358. q32[3] = q2[0] & 0xf0f0f0f0;
  4359. sycl::float4 s = {0.f, 0.f, 0.f, 0.f};
  4360. float smin = 0;
  4361. for (int l = 0; l < 4; ++l) {
  4362. s.x() += y1[l] * q4[l + 0]; s.y() += y1[l + 32] * q4[l + 4];
  4363. s.z() += y2[l] * q4[l + 8]; s.w() += y2[l + 32] * q4[l + 12];
  4364. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4365. }
  4366. tmp += dall * (s.x() * sc[0] + s.y() * sc[1] * 1.f / 16.f +
  4367. s.z() * sc[4] + s.w() * sc[5] * 1.f / 16.f) -
  4368. dmin * smin;
  4369. #else
  4370. const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
  4371. const uint16_t * q2 = q1 + 32;
  4372. q16[0] = q1[0] & 0x0f0f;
  4373. q16[1] = q1[0] & 0xf0f0;
  4374. q16[2] = q2[0] & 0x0f0f;
  4375. q16[3] = q2[0] & 0xf0f0;
  4376. float4 s = {0.f, 0.f, 0.f, 0.f};
  4377. float smin = 0;
  4378. for (int l = 0; l < 2; ++l) {
  4379. s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
  4380. s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
  4381. smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
  4382. }
  4383. 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;
  4384. #endif
  4385. }
  4386. #else
  4387. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15
  4388. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION);
  4389. const int step = tid * K_QUANTS_PER_ITERATION;
  4390. uint16_t aux16[2];
  4391. const uint8_t * s = (const uint8_t *)aux16;
  4392. float tmp = 0;
  4393. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4394. const uint8_t * q = x[i].qs + step;
  4395. const float * y = yy + i*QK_K + step;
  4396. const uint16_t * a = (const uint16_t *)x[i].scales;
  4397. aux16[0] = a[0] & 0x0f0f;
  4398. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4399. const float d = (float)x[i].dm[0];
  4400. const float m = (float)x[i].dm[1];
  4401. float sum = 0.f;
  4402. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4403. sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
  4404. + y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
  4405. + y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
  4406. + y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
  4407. }
  4408. tmp += sum;
  4409. }
  4410. #endif
  4411. // sum up partial sums and write back result
  4412. #pragma unroll
  4413. for (int mask = 16; mask > 0; mask >>= 1) {
  4414. tmp +=
  4415. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4416. }
  4417. if (tid == 0) {
  4418. dst[row] = tmp;
  4419. }
  4420. }
  4421. /*
  4422. DPCT1110:7: The total declared local variable size in device function
  4423. dequantize_mul_mat_vec_q5_k exceeds 128 bytes and may cause high register
  4424. pressure. Consult with your hardware vendor to find the total register size
  4425. available and adjust the code, or use smaller sub-group size to avoid high
  4426. register pressure.
  4427. */
  4428. static void dequantize_mul_mat_vec_q5_k(const void *__restrict__ vx,
  4429. const float *__restrict__ yy,
  4430. float *__restrict__ dst,
  4431. const int ncols,
  4432. const sycl::nd_item<3> &item_ct1) {
  4433. const int row = item_ct1.get_group(2);
  4434. const int num_blocks_per_row = ncols / QK_K;
  4435. const int ib0 = row*num_blocks_per_row;
  4436. const block_q5_K * x = (const block_q5_K *)vx + ib0;
  4437. float tmp = 0; // partial sum for thread in warp
  4438. #if QK_K == 256
  4439. const uint16_t kmask1 = 0x3f3f;
  4440. const uint16_t kmask2 = 0x0f0f;
  4441. const uint16_t kmask3 = 0xc0c0;
  4442. const int tid = item_ct1.get_local_id(2) / 2; // 0...15
  4443. const int ix = item_ct1.get_local_id(2) % 2;
  4444. const int il = tid/4; // 0...3
  4445. const int ir = tid - 4*il;// 0...3
  4446. const int n = 2;
  4447. const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
  4448. const int in = il%2;
  4449. const int l0 = n*(2*ir + in);
  4450. const int q_offset = 32*im + l0;
  4451. const int y_offset = 64*im + l0;
  4452. const uint8_t hm1 = 1 << (2*im);
  4453. const uint8_t hm2 = hm1 << 4;
  4454. uint16_t aux[4];
  4455. const uint8_t * sc = (const uint8_t *)aux;
  4456. uint16_t q16[8];
  4457. const uint8_t * q4 = (const uint8_t *)q16;
  4458. for (int i = ix; i < num_blocks_per_row; i += 2) {
  4459. const uint8_t * ql1 = x[i].qs + q_offset;
  4460. const uint8_t * qh = x[i].qh + l0;
  4461. const float * y1 = yy + i*QK_K + y_offset;
  4462. const float * y2 = y1 + 128;
  4463. const float dall = x[i].dm[0];
  4464. const float dmin = x[i].dm[1];
  4465. const uint16_t * a = (const uint16_t *)x[i].scales;
  4466. aux[0] = a[im+0] & kmask1;
  4467. aux[1] = a[im+2] & kmask1;
  4468. aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
  4469. aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
  4470. sycl::float4 sum = {0.f, 0.f, 0.f, 0.f};
  4471. float smin = 0;
  4472. const uint16_t * q1 = (const uint16_t *)ql1;
  4473. const uint16_t * q2 = q1 + 32;
  4474. q16[0] = q1[0] & 0x0f0f;
  4475. q16[1] = q1[8] & 0x0f0f;
  4476. q16[2] = (q1[0] >> 4) & 0x0f0f;
  4477. q16[3] = (q1[8] >> 4) & 0x0f0f;
  4478. q16[4] = q2[0] & 0x0f0f;
  4479. q16[5] = q2[8] & 0x0f0f;
  4480. q16[6] = (q2[0] >> 4) & 0x0f0f;
  4481. q16[7] = (q2[8] >> 4) & 0x0f0f;
  4482. for (int l = 0; l < n; ++l) {
  4483. sum.x() +=
  4484. y1[l + 0] * (q4[l + 0] + (qh[l + 0] & (hm1 << 0) ? 16 : 0)) +
  4485. y1[l + 16] * (q4[l + 2] + (qh[l + 16] & (hm1 << 0) ? 16 : 0));
  4486. sum.y() +=
  4487. y1[l + 32] * (q4[l + 4] + (qh[l + 0] & (hm1 << 1) ? 16 : 0)) +
  4488. y1[l + 48] * (q4[l + 6] + (qh[l + 16] & (hm1 << 1) ? 16 : 0));
  4489. sum.z() +=
  4490. y2[l + 0] * (q4[l + 8] + (qh[l + 0] & (hm2 << 0) ? 16 : 0)) +
  4491. y2[l + 16] * (q4[l + 10] + (qh[l + 16] & (hm2 << 0) ? 16 : 0));
  4492. sum.w() +=
  4493. y2[l + 32] * (q4[l + 12] + (qh[l + 0] & (hm2 << 1) ? 16 : 0)) +
  4494. y2[l + 48] * (q4[l + 14] + (qh[l + 16] & (hm2 << 1) ? 16 : 0));
  4495. smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
  4496. + (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
  4497. }
  4498. tmp += dall * (sum.x() * sc[0] + sum.y() * sc[1] + sum.z() * sc[4] +
  4499. sum.w() * sc[5]) -
  4500. dmin * smin;
  4501. }
  4502. #else
  4503. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...15
  4504. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION);
  4505. const int step = tid * K_QUANTS_PER_ITERATION;
  4506. const int im = step/8;
  4507. const int in = step%8;
  4508. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4509. const uint8_t * q = x[i].qs + step;
  4510. const int8_t * s = x[i].scales;
  4511. const float * y = yy + i*QK_K + step;
  4512. const float d = x[i].d;
  4513. float sum = 0.f;
  4514. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4515. const uint8_t h = x[i].qh[in+j] >> im;
  4516. sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
  4517. + y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
  4518. + y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
  4519. + y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
  4520. }
  4521. tmp += sum;
  4522. }
  4523. #endif
  4524. // sum up partial sums and write back result
  4525. #pragma unroll
  4526. for (int mask = 16; mask > 0; mask >>= 1) {
  4527. tmp +=
  4528. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4529. }
  4530. if (item_ct1.get_local_id(2) == 0) {
  4531. dst[row] = tmp;
  4532. }
  4533. }
  4534. static void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows,
  4535. const sycl::nd_item<3> &item_ct1) {
  4536. static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
  4537. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  4538. item_ct1.get_local_id(1);
  4539. if (row > nrows) return;
  4540. const int num_blocks_per_row = ncols / QK_K;
  4541. const int ib0 = row*num_blocks_per_row;
  4542. const block_q6_K * x = (const block_q6_K *)vx + ib0;
  4543. #if QK_K == 256
  4544. const int tid =
  4545. item_ct1.get_local_id(2) / K_QUANTS_PER_ITERATION; // 0...31 or 0...16
  4546. const int ix =
  4547. item_ct1.get_local_id(2) % K_QUANTS_PER_ITERATION; // 0 or 0, 1
  4548. const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
  4549. const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
  4550. const int in = tid - step*im; // 0...15 or 0...7
  4551. #if K_QUANTS_PER_ITERATION == 1
  4552. const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
  4553. const int is = 0;
  4554. #else
  4555. const int l0 = 4 * in; // 0, 4, 8, ..., 28
  4556. const int is = in / 4;
  4557. #endif
  4558. const int ql_offset = 64*im + l0;
  4559. const int qh_offset = 32*im + l0;
  4560. const int s_offset = 8*im + is;
  4561. const int y_offset = 128*im + l0;
  4562. float tmp = 0; // partial sum for thread in warp
  4563. for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
  4564. const float * y = yy + i * QK_K + y_offset;
  4565. const uint8_t * ql = x[i].ql + ql_offset;
  4566. const uint8_t * qh = x[i].qh + qh_offset;
  4567. const int8_t * s = x[i].scales + s_offset;
  4568. const float d = x[i].d;
  4569. #if K_QUANTS_PER_ITERATION == 1
  4570. float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
  4571. + y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
  4572. + y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
  4573. + y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
  4574. + y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
  4575. + y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
  4576. + y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
  4577. +y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
  4578. tmp += sum;
  4579. #else
  4580. float sum = 0;
  4581. for (int l = 0; l < 4; ++l) {
  4582. sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
  4583. + y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
  4584. + y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
  4585. + y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
  4586. }
  4587. tmp += sum;
  4588. #endif
  4589. }
  4590. #else
  4591. const int tid = item_ct1.get_local_id(2)/(2*K_QUANTS_PER_ITERATION); // 0...7
  4592. const int ix = item_ct1.get_local_id(2)%(2*K_QUANTS_PER_ITERATION); // 0...3
  4593. const int step = tid * K_QUANTS_PER_ITERATION;
  4594. float tmp = 0; // partial sum for thread in warp
  4595. for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
  4596. const float * y = yy + i * QK_K + step;
  4597. const uint8_t * ql = x[i].ql + step;
  4598. const uint8_t * qh = x[i].qh + step;
  4599. const int8_t * s = x[i].scales;
  4600. const float d = x[i+0].d;
  4601. float sum = 0;
  4602. for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
  4603. sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
  4604. + y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
  4605. + y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
  4606. + y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
  4607. }
  4608. tmp += sum;
  4609. }
  4610. #endif
  4611. // sum up partial sums and write back result
  4612. #pragma unroll
  4613. for (int mask = 16; mask > 0; mask >>= 1) {
  4614. tmp +=
  4615. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  4616. }
  4617. if (tid == 0) {
  4618. dst[row] = tmp;
  4619. }
  4620. }
  4621. static void convert_f16(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4622. const sycl::half *x = (const sycl::half *)vx;
  4623. // automatic half -> float type cast if dfloat == float
  4624. v.x() = x[ib + iqs + 0];
  4625. v.y() = x[ib + iqs + 1];
  4626. }
  4627. static void convert_f32(const void * vx, const int ib, const int iqs, dfloat2 & v){
  4628. const float * x = (const float *) vx;
  4629. // automatic half -> float type cast if dfloat == float
  4630. v.x() = x[ib + iqs + 0];
  4631. v.y() = x[ib + iqs + 1];
  4632. }
  4633. static void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int kx, const int kx_padded,
  4634. const sycl::nd_item<3> &item_ct1) {
  4635. const int ix = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4636. item_ct1.get_local_id(2);
  4637. if (ix >= kx_padded) {
  4638. return;
  4639. }
  4640. const int iy = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4641. item_ct1.get_local_id(1);
  4642. const int i_padded = iy*kx_padded + ix;
  4643. block_q8_1 * y = (block_q8_1 *) vy;
  4644. const int ib = i_padded / QK8_1; // block index
  4645. const int iqs = i_padded % QK8_1; // quant index
  4646. const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
  4647. float amax = sycl::fabs((float)xi);
  4648. float sum = xi;
  4649. #pragma unroll
  4650. for (int mask = 16; mask > 0; mask >>= 1) {
  4651. amax = sycl::fmax(amax, dpct::permute_sub_group_by_xor(
  4652. item_ct1.get_sub_group(), amax, mask));
  4653. sum +=
  4654. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), sum, mask);
  4655. }
  4656. const float d = amax / 127;
  4657. const int8_t q = amax == 0.0f ? 0 : sycl::round(xi / d);
  4658. y[ib].qs[iqs] = q;
  4659. if (iqs > 0) {
  4660. return;
  4661. }
  4662. reinterpret_cast<sycl::half &>(y[ib].ds.x()) = d;
  4663. reinterpret_cast<sycl::half &>(y[ib].ds.y()) = sum;
  4664. }
  4665. template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4666. static void k_get_rows(
  4667. const void * src0, const int32_t * src1, dst_t * dst,
  4668. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4669. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4670. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4671. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4672. size_t s10, size_t s11, size_t s12,
  4673. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4674. const int i00 = (item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4675. item_ct1.get_local_id(2)) *
  4676. 2;
  4677. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4678. item_ct1.get_local_id(1);
  4679. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4680. item_ct1.get_local_id(0)) /
  4681. ne12;
  4682. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4683. item_ct1.get_local_id(0)) %
  4684. ne12;
  4685. if (i00 >= ne00) {
  4686. return;
  4687. }
  4688. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4689. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4690. const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
  4691. const int ib = i00/qk; // block index
  4692. const int iqs = (i00%qk)/qr; // quant index
  4693. const int iybs = i00 - i00%qk; // dst block start index
  4694. const int y_offset = qr == 1 ? 1 : qk/2;
  4695. // dequantize
  4696. dfloat2 v;
  4697. dequantize_kernel(src0_row, ib, iqs, v);
  4698. dst_row[iybs + iqs + 0] = v.x();
  4699. dst_row[iybs + iqs + y_offset] = v.y();
  4700. }
  4701. template<typename src0_t, typename dst_t>
  4702. static void k_get_rows_float(
  4703. const src0_t * src0, const int32_t * src1, dst_t * dst,
  4704. int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
  4705. /*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
  4706. /*size_t s0,*/ size_t s1, size_t s2, size_t s3,
  4707. /*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
  4708. size_t s10, size_t s11, size_t s12,
  4709. const sycl::nd_item<3> &item_ct1/*, size_t s13*/) {
  4710. const int i00 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  4711. item_ct1.get_local_id(2);
  4712. const int i10 = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  4713. item_ct1.get_local_id(1);
  4714. const int i11 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4715. item_ct1.get_local_id(0)) /
  4716. ne12;
  4717. const int i12 = (item_ct1.get_group(0) * item_ct1.get_local_range(0) +
  4718. item_ct1.get_local_id(0)) %
  4719. ne12;
  4720. if (i00 >= ne00) {
  4721. return;
  4722. }
  4723. const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
  4724. dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
  4725. const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
  4726. dst_row[i00] = src0_row[i00];
  4727. }
  4728. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  4729. static void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4730. const sycl::nd_item<3> &item_ct1) {
  4731. const int i = 2 * (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4732. item_ct1.get_local_id(2));
  4733. if (i >= k) {
  4734. return;
  4735. }
  4736. const int ib = i/qk; // block index
  4737. const int iqs = (i%qk)/qr; // quant index
  4738. const int iybs = i - i%qk; // y block start index
  4739. const int y_offset = qr == 1 ? 1 : qk/2;
  4740. // dequantize
  4741. dfloat2 v;
  4742. dequantize_kernel(vx, ib, iqs, v);
  4743. y[iybs + iqs + 0] = v.x();
  4744. y[iybs + iqs + y_offset] = v.y();
  4745. }
  4746. template <typename src_t, typename dst_t>
  4747. static void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int k,
  4748. const sycl::nd_item<3> &item_ct1) {
  4749. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  4750. item_ct1.get_local_id(2);
  4751. if (i >= k) {
  4752. return;
  4753. }
  4754. const src_t * x = (src_t *) vx;
  4755. y[i] = x[i];
  4756. }
  4757. // VDR = vec dot ratio, how many contiguous integers each thread processes when the vec dot kernel is called
  4758. // MMVQ = mul_mat_vec_q, MMQ = mul_mat_q
  4759. #define VDR_Q4_0_Q8_1_MMVQ 2
  4760. #define VDR_Q4_0_Q8_1_MMQ 4
  4761. template <int vdr>
  4762. static __dpct_inline__ float vec_dot_q4_0_q8_1_impl(const int *v, const int *u,
  4763. const float &d4,
  4764. const sycl::half2 &ds8) {
  4765. int sumi = 0;
  4766. #pragma unroll
  4767. for (int i = 0; i < vdr; ++i) {
  4768. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4769. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4770. // SIMD dot product of quantized values
  4771. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4772. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4773. }
  4774. const sycl::float2 ds8f =
  4775. ds8.convert<float, sycl::rounding_mode::automatic>();
  4776. // second part effectively subtracts 8 from each quant value
  4777. return d4 * (sumi * ds8f.x() - (8 * vdr / QI4_0) * ds8f.y());
  4778. }
  4779. #define VDR_Q4_1_Q8_1_MMVQ 2
  4780. #define VDR_Q4_1_Q8_1_MMQ 4
  4781. template <int vdr>
  4782. static __dpct_inline__ float vec_dot_q4_1_q8_1_impl(const int *v, const int *u,
  4783. const sycl::half2 &dm4,
  4784. const sycl::half2 &ds8) {
  4785. int sumi = 0;
  4786. #pragma unroll
  4787. for (int i = 0; i < vdr; ++i) {
  4788. const int vi0 = (v[i] >> 0) & 0x0F0F0F0F;
  4789. const int vi1 = (v[i] >> 4) & 0x0F0F0F0F;
  4790. // SIMD dot product of quantized values
  4791. sumi = dpct::dp4a(vi0, u[2 * i + 0], sumi);
  4792. sumi = dpct::dp4a(vi1, u[2 * i + 1], sumi);
  4793. }
  4794. #ifdef GGML_SYCL_F16
  4795. const sycl::float2 tmp =
  4796. (dm4 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4797. const float d4d8 = tmp.x();
  4798. const float m4s8 = tmp.y();
  4799. #else
  4800. const sycl::float2 dm4f =
  4801. dm4.convert<float, sycl::rounding_mode::automatic>();
  4802. const sycl::float2 ds8f =
  4803. ds8.convert<float, sycl::rounding_mode::automatic>();
  4804. const float d4d8 = dm4f.x() * ds8f.x();
  4805. const float m4s8 = dm4f.y() * ds8f.y();
  4806. #endif // GGML_SYCL_F16
  4807. // scale second part of sum by QI8_1/(vdr * QR4_1) to compensate for multiple threads adding it
  4808. return sumi * d4d8 + m4s8 / (QI8_1 / (vdr * QR4_1));
  4809. }
  4810. #define VDR_Q5_0_Q8_1_MMVQ 2
  4811. #define VDR_Q5_0_Q8_1_MMQ 4
  4812. template <int vdr>
  4813. static __dpct_inline__ float
  4814. vec_dot_q5_0_q8_1_impl(const int *vl, const int *vh, const int *u,
  4815. const float &d5, const sycl::half2 &ds8) {
  4816. int sumi = 0;
  4817. #pragma unroll
  4818. for (int i = 0; i < vdr; ++i) {
  4819. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4820. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4821. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4822. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4823. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4824. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4825. sumi); // SIMD dot product of quantized values
  4826. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4827. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4828. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4829. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4830. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4831. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4832. sumi); // SIMD dot product of quantized values
  4833. }
  4834. const sycl::float2 ds8f =
  4835. ds8.convert<float, sycl::rounding_mode::automatic>();
  4836. // second part effectively subtracts 16 from each quant value
  4837. return d5 * (sumi * ds8f.x() - (16 * vdr / QI5_0) * ds8f.y());
  4838. }
  4839. #define VDR_Q5_1_Q8_1_MMVQ 2
  4840. #define VDR_Q5_1_Q8_1_MMQ 4
  4841. template <int vdr>
  4842. static __dpct_inline__ float
  4843. vec_dot_q5_1_q8_1_impl(const int *vl, const int *vh, const int *u,
  4844. const sycl::half2 &dm5, const sycl::half2 &ds8) {
  4845. int sumi = 0;
  4846. #pragma unroll
  4847. for (int i = 0; i < vdr; ++i) {
  4848. int vi0 = (vl[i] >> 0) & 0x0F0F0F0F; // lower 4 qs bits, still need qh as 5th bits
  4849. vi0 |= (vh[i] << 4) & 0x00000010; // 0 -> 4
  4850. vi0 |= (vh[i] << 11) & 0x00001000; // 1 -> 12
  4851. vi0 |= (vh[i] << 18) & 0x00100000; // 2 -> 20
  4852. vi0 |= (vh[i] << 25) & 0x10000000; // 3 -> 28
  4853. sumi = dpct::dp4a(vi0, u[2 * i + 0],
  4854. sumi); // SIMD dot product of quantized values
  4855. int vi1 = (vl[i] >> 4) & 0x0F0F0F0F; // upper 4 qs bits, still need qh as 5th bits
  4856. vi1 |= (vh[i] >> 12) & 0x00000010; // 16 -> 4
  4857. vi1 |= (vh[i] >> 5) & 0x00001000; // 17 -> 12
  4858. vi1 |= (vh[i] << 2) & 0x00100000; // 18 -> 20
  4859. vi1 |= (vh[i] << 9) & 0x10000000; // 19 -> 28
  4860. sumi = dpct::dp4a(vi1, u[2 * i + 1],
  4861. sumi); // SIMD dot product of quantized values
  4862. }
  4863. #ifdef GGML_SYCL_F16
  4864. const sycl::float2 tmp =
  4865. (dm5 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4866. const float d5d8 = tmp.x();
  4867. const float m5s8 = tmp.y();
  4868. #else
  4869. const sycl::float2 dm5f =
  4870. dm5.convert<float, sycl::rounding_mode::automatic>();
  4871. const sycl::float2 ds8f =
  4872. ds8.convert<float, sycl::rounding_mode::automatic>();
  4873. const float d5d8 = dm5f.x() * ds8f.x();
  4874. const float m5s8 = dm5f.y() * ds8f.y();
  4875. #endif // GGML_SYCL_F16
  4876. // scale second part of sum by QI5_1 / vdr to compensate for multiple threads adding it
  4877. return sumi*d5d8 + m5s8 / (QI5_1 / vdr);
  4878. }
  4879. #define VDR_Q8_0_Q8_1_MMVQ 2
  4880. #define VDR_Q8_0_Q8_1_MMQ 8
  4881. template <int vdr>
  4882. static __dpct_inline__ float vec_dot_q8_0_q8_1_impl(const int *v, const int *u,
  4883. const float &d8_0,
  4884. const float &d8_1) {
  4885. int sumi = 0;
  4886. #pragma unroll
  4887. for (int i = 0; i < vdr; ++i) {
  4888. // SIMD dot product of quantized values
  4889. sumi = dpct::dp4a(v[i], u[i], sumi);
  4890. }
  4891. return d8_0*d8_1 * sumi;
  4892. }
  4893. template <int vdr>
  4894. static __dpct_inline__ float vec_dot_q8_1_q8_1_impl(const int *v, const int *u,
  4895. const sycl::half2 &dm8,
  4896. const sycl::half2 &ds8) {
  4897. int sumi = 0;
  4898. #pragma unroll
  4899. for (int i = 0; i < vdr; ++i) {
  4900. // SIMD dot product of quantized values
  4901. sumi = dpct::dp4a(v[i], u[i], sumi);
  4902. }
  4903. #ifdef GGML_SYCL_F16
  4904. const sycl::float2 tmp =
  4905. (dm8 * ds8).convert<float, sycl::rounding_mode::automatic>();
  4906. const float d8d8 = tmp.x();
  4907. const float m8s8 = tmp.y();
  4908. #else
  4909. const sycl::float2 dm8f =
  4910. dm8.convert<float, sycl::rounding_mode::automatic>();
  4911. const sycl::float2 ds8f =
  4912. ds8.convert<float, sycl::rounding_mode::automatic>();
  4913. const float d8d8 = dm8f.x() * ds8f.x();
  4914. const float m8s8 = dm8f.y() * ds8f.y();
  4915. #endif // GGML_SYCL_F16
  4916. // scale second part of sum by QI8_1/ vdr to compensate for multiple threads adding it
  4917. return sumi*d8d8 + m8s8 / (QI8_1 / vdr);
  4918. }
  4919. #define VDR_Q2_K_Q8_1_MMVQ 1
  4920. #define VDR_Q2_K_Q8_1_MMQ 2
  4921. // contiguous v/x values
  4922. static __dpct_inline__ float vec_dot_q2_K_q8_1_impl_mmvq(
  4923. const int &v, const int *__restrict__ u, const uint8_t *__restrict__ scales,
  4924. const sycl::half2 &dm2, const float *__restrict__ d8) {
  4925. float sumf_d = 0.0f;
  4926. float sumf_m = 0.0f;
  4927. #pragma unroll
  4928. for (int i = 0; i < QR2_K; ++i) {
  4929. const int sc = scales[2*i];
  4930. const int vi = (v >> (2*i)) & 0x03030303;
  4931. sumf_d +=
  4932. d8[i] * (dpct::dp4a(vi, u[i], 0) * (sc & 0xF)); // SIMD dot product
  4933. // fill int with 4x m
  4934. int m = sc >> 4;
  4935. m |= m << 8;
  4936. m |= m << 16;
  4937. sumf_m += d8[i] *
  4938. dpct::dp4a(
  4939. m, u[i],
  4940. 0); // multiply constant q2_K part with sum of q8_1 values
  4941. }
  4942. const sycl::float2 dm2f =
  4943. dm2.convert<float, sycl::rounding_mode::automatic>();
  4944. return dm2f.x() * sumf_d - dm2f.y() * sumf_m;
  4945. }
  4946. // contiguous u/y values
  4947. static __dpct_inline__ float
  4948. vec_dot_q2_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  4949. const uint8_t *__restrict__ scales,
  4950. const sycl::half2 &dm2, const float &d8) {
  4951. int sumi_d = 0;
  4952. int sumi_m = 0;
  4953. #pragma unroll
  4954. for (int i0 = 0; i0 < QI8_1; i0 += QI8_1/2) {
  4955. int sumi_d_sc = 0;
  4956. const int sc = scales[i0 / (QI8_1/2)];
  4957. // fill int with 4x m
  4958. int m = sc >> 4;
  4959. m |= m << 8;
  4960. m |= m << 16;
  4961. #pragma unroll
  4962. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  4963. sumi_d_sc = dpct::dp4a(v[i], u[i], sumi_d_sc); // SIMD dot product
  4964. sumi_m = dpct::dp4a(m, u[i],
  4965. sumi_m); // multiply sum of q8_1 values with m
  4966. }
  4967. sumi_d += sumi_d_sc * (sc & 0xF);
  4968. }
  4969. const sycl::float2 dm2f =
  4970. dm2.convert<float, sycl::rounding_mode::automatic>();
  4971. return d8 * (dm2f.x() * sumi_d - dm2f.y() * sumi_m);
  4972. }
  4973. #define VDR_Q3_K_Q8_1_MMVQ 1
  4974. #define VDR_Q3_K_Q8_1_MMQ 2
  4975. // contiguous v/x values
  4976. static __dpct_inline__ float vec_dot_q3_K_q8_1_impl_mmvq(
  4977. const int &vl, const int &vh, const int *__restrict__ u,
  4978. const uint8_t *__restrict__ scales, const int &scale_offset,
  4979. const float &d3, const float *__restrict__ d8) {
  4980. float sumf = 0.0f;
  4981. #pragma unroll
  4982. for (int i = 0; i < QR3_K; ++i) {
  4983. const int isc = scale_offset + 2*i;
  4984. const int isc_low = isc % (QK_K/32);
  4985. const int sc_shift_low = 4 * (isc / (QK_K/32));
  4986. const int sc_low = (scales[isc_low] >> sc_shift_low) & 0xF;
  4987. const int isc_high = isc % (QK_K/64);
  4988. const int sc_shift_high = 2 * (isc / (QK_K/64));
  4989. const int sc_high = ((scales[(QK_K/32) + isc_high] >> sc_shift_high) & 3) << 4;
  4990. const int sc = (sc_low | sc_high) - 32;
  4991. const int vil = (vl >> (2*i)) & 0x03030303;
  4992. const int vih = ((vh >> i) << 2) & 0x04040404;
  4993. const int vi =
  4994. dpct::vectorized_binary<sycl::char4>(vil, vih, dpct::sub_sat());
  4995. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  4996. }
  4997. return d3 * sumf;
  4998. }
  4999. // contiguous u/y values
  5000. static __dpct_inline__ float
  5001. vec_dot_q3_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  5002. const int8_t *__restrict__ scales, const float &d3,
  5003. const float &d8) {
  5004. int sumi = 0;
  5005. #pragma unroll
  5006. for (int i0 = 0; i0 < QR3_K*VDR_Q3_K_Q8_1_MMQ; i0 += QI8_1/2) {
  5007. int sumi_sc = 0;
  5008. for (int i = i0; i < i0 + QI8_1/2; ++i) {
  5009. sumi_sc = dpct::dp4a(v[i], u[i], sumi_sc); // SIMD dot product
  5010. }
  5011. sumi += sumi_sc * scales[i0 / (QI8_1/2)];
  5012. }
  5013. return d3*d8 * sumi;
  5014. }
  5015. #define VDR_Q4_K_Q8_1_MMVQ 2
  5016. #define VDR_Q4_K_Q8_1_MMQ 8
  5017. // contiguous v/x values
  5018. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_vmmq(
  5019. const int *__restrict__ v, const int *__restrict__ u,
  5020. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5021. const sycl::half2 &dm4, const float *__restrict__ d8) {
  5022. float sumf_d = 0.0f;
  5023. float sumf_m = 0.0f;
  5024. #pragma unroll
  5025. for (int i = 0; i < QR4_K; ++i) {
  5026. const int v0i = (v[0] >> (4*i)) & 0x0F0F0F0F;
  5027. const int v1i = (v[1] >> (4*i)) & 0x0F0F0F0F;
  5028. const int dot1 =
  5029. dpct::dp4a(v1i, u[2 * i + 1],
  5030. dpct::dp4a(v0i, u[2 * i + 0], 0)); // SIMD dot product
  5031. const int dot2 =
  5032. dpct::dp4a(0x01010101, u[2 * i + 1],
  5033. dpct::dp4a(0x01010101, u[2 * i + 0], 0)); // sum of u
  5034. sumf_d += d8[i] * (dot1 * sc[i]);
  5035. sumf_m += d8[i] * (dot2 * m[i]); // multiply constant part of q4_K with sum of q8_1 values
  5036. }
  5037. const sycl::float2 dm4f =
  5038. dm4.convert<float, sycl::rounding_mode::automatic>();
  5039. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5040. }
  5041. // contiguous u/y values
  5042. static __dpct_inline__ float vec_dot_q4_K_q8_1_impl_mmq(
  5043. const int *__restrict__ v, const int *__restrict__ u,
  5044. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5045. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  5046. float sumf_d = 0.0f;
  5047. float sumf_m = 0.0f;
  5048. #pragma unroll
  5049. for (int i = 0; i < QR4_K*VDR_Q4_K_Q8_1_MMQ/QI8_1; ++i) {
  5050. int sumi_d = 0;
  5051. #pragma unroll
  5052. for (int j = 0; j < QI8_1; ++j) {
  5053. sumi_d = dpct::dp4a((v[j] >> (4 * i)) & 0x0F0F0F0F,
  5054. u[i * QI8_1 + j], sumi_d); // SIMD dot product
  5055. }
  5056. const sycl::float2 ds8f =
  5057. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  5058. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  5059. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  5060. }
  5061. const sycl::float2 dm4f =
  5062. dm4.convert<float, sycl::rounding_mode::automatic>();
  5063. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5064. }
  5065. #define VDR_Q5_K_Q8_1_MMVQ 2
  5066. #define VDR_Q5_K_Q8_1_MMQ 8
  5067. // contiguous v/x values
  5068. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_vmmq(
  5069. const int *__restrict__ vl, const int *__restrict__ vh,
  5070. const int *__restrict__ u, const uint8_t *__restrict__ sc,
  5071. const uint8_t *__restrict__ m, const sycl::half2 &dm5,
  5072. const float *__restrict__ d8) {
  5073. float sumf_d = 0.0f;
  5074. float sumf_m = 0.0f;
  5075. #pragma unroll
  5076. for (int i = 0; i < QR5_K; ++i) {
  5077. const int vl0i = (vl[0] >> (4*i)) & 0x0F0F0F0F;
  5078. const int vl1i = (vl[1] >> (4*i)) & 0x0F0F0F0F;
  5079. const int vh0i = ((vh[0] >> i) << 4) & 0x10101010;
  5080. const int vh1i = ((vh[1] >> i) << 4) & 0x10101010;
  5081. const int v0i = vl0i | vh0i;
  5082. const int v1i = vl1i | vh1i;
  5083. const int dot1 =
  5084. dpct::dp4a(v0i, u[2 * i + 0],
  5085. dpct::dp4a(v1i, u[2 * i + 1], 0)); // SIMD dot product
  5086. const int dot2 =
  5087. dpct::dp4a(0x01010101, u[2 * i + 0],
  5088. dpct::dp4a(0x01010101, u[2 * i + 1], 0)); // sum of u
  5089. sumf_d += d8[i] * (dot1 * sc[i]);
  5090. sumf_m += d8[i] * (dot2 * m[i]);
  5091. }
  5092. const sycl::float2 dm5f =
  5093. dm5.convert<float, sycl::rounding_mode::automatic>();
  5094. return dm5f.x() * sumf_d - dm5f.y() * sumf_m;
  5095. }
  5096. // contiguous u/y values
  5097. static __dpct_inline__ float vec_dot_q5_K_q8_1_impl_mmq(
  5098. const int *__restrict__ v, const int *__restrict__ u,
  5099. const uint8_t *__restrict__ sc, const uint8_t *__restrict__ m,
  5100. const sycl::half2 &dm4, const sycl::half2 *__restrict__ ds8) {
  5101. float sumf_d = 0.0f;
  5102. float sumf_m = 0.0f;
  5103. #pragma unroll
  5104. for (int i = 0; i < QR5_K*VDR_Q5_K_Q8_1_MMQ/QI8_1; ++i) {
  5105. int sumi_d = 0;
  5106. #pragma unroll
  5107. for (int j = 0; j < QI8_1; ++j) {
  5108. sumi_d = dpct::dp4a(v[i * QI8_1 + j], u[i * QI8_1 + j],
  5109. sumi_d); // SIMD dot product
  5110. }
  5111. const sycl::float2 ds8f =
  5112. ds8[i].convert<float, sycl::rounding_mode::automatic>();
  5113. sumf_d += ds8f.x() * (sc[i] * sumi_d);
  5114. sumf_m += ds8f.y() * m[i]; // sum of q8_1 block * q4_K min val
  5115. }
  5116. const sycl::float2 dm4f =
  5117. dm4.convert<float, sycl::rounding_mode::automatic>();
  5118. return dm4f.x() * sumf_d - dm4f.y() * sumf_m;
  5119. }
  5120. #define VDR_Q6_K_Q8_1_MMVQ 1
  5121. #define VDR_Q6_K_Q8_1_MMQ 8
  5122. // contiguous v/x values
  5123. static __dpct_inline__ float
  5124. vec_dot_q6_K_q8_1_impl_mmvq(const int &vl, const int &vh,
  5125. const int *__restrict__ u,
  5126. const int8_t *__restrict__ scales, const float &d,
  5127. const float *__restrict__ d8) {
  5128. float sumf = 0.0f;
  5129. #pragma unroll
  5130. for (int i = 0; i < QR6_K; ++i) {
  5131. const int sc = scales[4*i];
  5132. const int vil = (vl >> (4*i)) & 0x0F0F0F0F;
  5133. const int vih = ((vh >> (4*i)) << 4) & 0x30303030;
  5134. const int vi = dpct::vectorized_binary<sycl::char4>(
  5135. (vil | vih), 0x20202020, dpct::sub_sat()); // vi = (vil | vih) - 32
  5136. sumf += d8[i] * (dpct::dp4a(vi, u[i], 0) * sc); // SIMD dot product
  5137. }
  5138. return d*sumf;
  5139. }
  5140. // contiguous u/y values
  5141. static __dpct_inline__ float
  5142. vec_dot_q6_K_q8_1_impl_mmq(const int *__restrict__ v, const int *__restrict__ u,
  5143. const int8_t *__restrict__ sc, const float &d6,
  5144. const float *__restrict__ d8) {
  5145. float sumf_d = 0.0f;
  5146. #pragma unroll
  5147. for (int i0 = 0; i0 < VDR_Q6_K_Q8_1_MMQ; i0 += 4) {
  5148. sycl::int2 sumi_d = {0, 0}; // 2 q6_K scales per q8_1 scale
  5149. #pragma unroll
  5150. for (int i = i0; i < i0 + 2; ++i) {
  5151. sumi_d.x() = dpct::dp4a(v[2 * i + 0], u[2 * i + 0],
  5152. sumi_d.x()); // SIMD dot product
  5153. sumi_d.x() = dpct::dp4a(v[2 * i + 1], u[2 * i + 1],
  5154. sumi_d.x()); // SIMD dot product
  5155. sumi_d.y() = dpct::dp4a(v[2 * i + 4], u[2 * i + 4],
  5156. sumi_d.y()); // SIMD dot product
  5157. sumi_d.y() = dpct::dp4a(v[2 * i + 5], u[2 * i + 5],
  5158. sumi_d.y()); // SIMD dot product
  5159. }
  5160. sumf_d += d8[i0 / 4] *
  5161. (sc[i0 / 2 + 0] * sumi_d.x() + sc[i0 / 2 + 1] * sumi_d.y());
  5162. }
  5163. return d6 * sumf_d;
  5164. }
  5165. static __dpct_inline__ float
  5166. vec_dot_q4_0_q8_1(const void *__restrict__ vbq,
  5167. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5168. const block_q4_0 * bq4_0 = (const block_q4_0 *) vbq;
  5169. int v[VDR_Q4_0_Q8_1_MMVQ];
  5170. int u[2*VDR_Q4_0_Q8_1_MMVQ];
  5171. #pragma unroll
  5172. for (int i = 0; i < VDR_Q4_0_Q8_1_MMVQ; ++i) {
  5173. v[i] = get_int_from_uint8(bq4_0->qs, iqs + i);
  5174. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5175. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_0);
  5176. }
  5177. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMVQ>(v, u, bq4_0->d, bq8_1->ds);
  5178. }
  5179. template <int mmq_y>
  5180. static __dpct_inline__ void
  5181. allocate_tiles_q4_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5182. int *tile_x_qs_q4_0, float *tile_x_d_q4_0) {
  5183. (void)x_qh; (void)x_sc;
  5184. *x_ql = tile_x_qs_q4_0;
  5185. *x_dm = (sycl::half2 *)tile_x_d_q4_0;
  5186. }
  5187. template <int mmq_y, int nwarps, bool need_check>
  5188. static __dpct_inline__ void
  5189. load_tiles_q4_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5190. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5191. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5192. const int &k, const int &blocks_per_row) {
  5193. (void)x_qh; (void)x_sc;
  5194. GGML_SYCL_ASSUME(i_offset >= 0);
  5195. GGML_SYCL_ASSUME(i_offset < nwarps);
  5196. GGML_SYCL_ASSUME(k >= 0);
  5197. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5198. const int kbx = k / QI4_0;
  5199. const int kqsx = k % QI4_0;
  5200. const block_q4_0 * bx0 = (const block_q4_0 *) vx;
  5201. float * x_dmf = (float *) x_dm;
  5202. #pragma unroll
  5203. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5204. int i = i0 + i_offset;
  5205. if (need_check) {
  5206. i = sycl::min(i, i_max);
  5207. }
  5208. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5209. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5210. // x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbx] = bxi->d;
  5211. }
  5212. const int blocks_per_tile_x_row = WARP_SIZE / QI4_0;
  5213. const int kbxd = k % blocks_per_tile_x_row;
  5214. #pragma unroll
  5215. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_0) {
  5216. int i = i0 + i_offset * QI4_0 + k / blocks_per_tile_x_row;
  5217. if (need_check) {
  5218. i = sycl::min(i, i_max);
  5219. }
  5220. const block_q4_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5221. x_dmf[i * (WARP_SIZE/QI4_0) + i / QI4_0 + kbxd] = bxi->d;
  5222. }
  5223. }
  5224. static __dpct_inline__ float vec_dot_q4_0_q8_1_mul_mat(
  5225. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5226. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5227. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5228. const int &i, const int &j, const int &k) {
  5229. (void)x_qh; (void)x_sc;
  5230. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5231. const float * x_dmf = (const float *) x_dm;
  5232. int u[2*VDR_Q4_0_Q8_1_MMQ];
  5233. #pragma unroll
  5234. for (int l = 0; l < VDR_Q4_0_Q8_1_MMQ; ++l) {
  5235. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5236. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_0) % WARP_SIZE];
  5237. }
  5238. return vec_dot_q4_0_q8_1_impl<VDR_Q4_0_Q8_1_MMQ>
  5239. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dmf[i * (WARP_SIZE/QI4_0) + i/QI4_0 + k/QI4_0],
  5240. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5241. }
  5242. static __dpct_inline__ float
  5243. vec_dot_q4_1_q8_1(const void *__restrict__ vbq,
  5244. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5245. const block_q4_1 * bq4_1 = (const block_q4_1 *) vbq;
  5246. int v[VDR_Q4_1_Q8_1_MMVQ];
  5247. int u[2*VDR_Q4_1_Q8_1_MMVQ];
  5248. #pragma unroll
  5249. for (int i = 0; i < VDR_Q4_1_Q8_1_MMVQ; ++i) {
  5250. v[i] = get_int_from_uint8_aligned(bq4_1->qs, iqs + i);
  5251. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5252. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI4_1);
  5253. }
  5254. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMVQ>(v, u, bq4_1->dm, bq8_1->ds);
  5255. }
  5256. template <int mmq_y>
  5257. static __dpct_inline__ void
  5258. allocate_tiles_q4_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5259. int *tile_x_qs_q4_1, sycl::half2 *tile_x_dm_q4_1) {
  5260. (void)x_qh; (void)x_sc;
  5261. *x_ql = tile_x_qs_q4_1;
  5262. *x_dm = tile_x_dm_q4_1;
  5263. }
  5264. template <int mmq_y, int nwarps, bool need_check>
  5265. static __dpct_inline__ void
  5266. load_tiles_q4_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5267. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5268. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5269. const int &k, const int &blocks_per_row) {
  5270. (void)x_qh; (void)x_sc;
  5271. GGML_SYCL_ASSUME(i_offset >= 0);
  5272. GGML_SYCL_ASSUME(i_offset < nwarps);
  5273. GGML_SYCL_ASSUME(k >= 0);
  5274. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5275. const int kbx = k / QI4_1;
  5276. const int kqsx = k % QI4_1;
  5277. const block_q4_1 * bx0 = (const block_q4_1 *) vx;
  5278. #pragma unroll
  5279. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5280. int i = i0 + i_offset;
  5281. if (need_check) {
  5282. i = sycl::min(i, i_max);
  5283. }
  5284. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5285. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5286. }
  5287. const int blocks_per_tile_x_row = WARP_SIZE / QI4_1;
  5288. const int kbxd = k % blocks_per_tile_x_row;
  5289. #pragma unroll
  5290. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_1) {
  5291. int i = i0 + i_offset * QI4_1 + k / blocks_per_tile_x_row;
  5292. if (need_check) {
  5293. i = sycl::min(i, i_max);
  5294. }
  5295. const block_q4_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5296. x_dm[i * (WARP_SIZE/QI4_1) + i / QI4_1 + kbxd] = bxi->dm;
  5297. }
  5298. }
  5299. static __dpct_inline__ float vec_dot_q4_1_q8_1_mul_mat(
  5300. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5301. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5302. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5303. const int &i, const int &j, const int &k) {
  5304. (void)x_qh; (void)x_sc;
  5305. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5306. int u[2*VDR_Q4_1_Q8_1_MMQ];
  5307. #pragma unroll
  5308. for (int l = 0; l < VDR_Q4_1_Q8_1_MMQ; ++l) {
  5309. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5310. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI4_1) % WARP_SIZE];
  5311. }
  5312. return vec_dot_q4_1_q8_1_impl<VDR_Q4_1_Q8_1_MMQ>
  5313. (&x_ql[i * (WARP_SIZE + 1) + k], u, x_dm[i * (WARP_SIZE/QI4_1) + i/QI4_1 + k/QI4_1],
  5314. y_ds[j * (WARP_SIZE/QI8_1) + (2*k/QI8_1) % (WARP_SIZE/QI8_1)]);
  5315. }
  5316. static __dpct_inline__ float
  5317. vec_dot_q5_0_q8_1(const void *__restrict__ vbq,
  5318. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5319. const block_q5_0 * bq5_0 = (const block_q5_0 *) vbq;
  5320. int vl[VDR_Q5_0_Q8_1_MMVQ];
  5321. int vh[VDR_Q5_0_Q8_1_MMVQ];
  5322. int u[2*VDR_Q5_0_Q8_1_MMVQ];
  5323. #pragma unroll
  5324. for (int i = 0; i < VDR_Q5_0_Q8_1_MMVQ; ++i) {
  5325. vl[i] = get_int_from_uint8(bq5_0->qs, iqs + i);
  5326. vh[i] = get_int_from_uint8(bq5_0->qh, 0) >> (4 * (iqs + i));
  5327. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5328. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_0);
  5329. }
  5330. return vec_dot_q5_0_q8_1_impl<VDR_Q5_0_Q8_1_MMVQ>(vl, vh, u, bq5_0->d, bq8_1->ds);
  5331. }
  5332. template <int mmq_y>
  5333. static __dpct_inline__ void
  5334. allocate_tiles_q5_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5335. int *tile_x_ql_q5_0, float *tile_x_d_q5_0) {
  5336. (void)x_qh; (void)x_sc;
  5337. *x_ql = tile_x_ql_q5_0;
  5338. *x_dm = (sycl::half2 *)tile_x_d_q5_0;
  5339. }
  5340. template <int mmq_y, int nwarps, bool need_check>
  5341. static __dpct_inline__ void
  5342. load_tiles_q5_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5343. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5344. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5345. const int &k, const int &blocks_per_row) {
  5346. (void)x_qh; (void)x_sc;
  5347. GGML_SYCL_ASSUME(i_offset >= 0);
  5348. GGML_SYCL_ASSUME(i_offset < nwarps);
  5349. GGML_SYCL_ASSUME(k >= 0);
  5350. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5351. const int kbx = k / QI5_0;
  5352. const int kqsx = k % QI5_0;
  5353. const block_q5_0 * bx0 = (const block_q5_0 *) vx;
  5354. #pragma unroll
  5355. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5356. int i = i0 + i_offset;
  5357. if (need_check) {
  5358. i = sycl::min(i, i_max);
  5359. }
  5360. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5361. const int ql = get_int_from_uint8(bxi->qs, kqsx);
  5362. const int qh = get_int_from_uint8(bxi->qh, 0) >> (4 * (k % QI5_0));
  5363. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5364. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5365. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5366. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5367. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5368. qs0 = dpct::vectorized_binary<sycl::char4>(
  5369. qs0, 0x10101010, dpct::sub_sat()); // subtract 16
  5370. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5371. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5372. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5373. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5374. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5375. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5376. qs1 = dpct::vectorized_binary<sycl::char4>(
  5377. qs1, 0x10101010, dpct::sub_sat()); // subtract 16
  5378. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5379. }
  5380. const int blocks_per_tile_x_row = WARP_SIZE / QI5_0;
  5381. const int kbxd = k % blocks_per_tile_x_row;
  5382. float * x_dmf = (float *) x_dm;
  5383. #pragma unroll
  5384. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_0) {
  5385. int i = i0 + i_offset * QI5_0 + k / blocks_per_tile_x_row;
  5386. if (need_check) {
  5387. i = sycl::min(i, i_max);
  5388. }
  5389. const block_q5_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5390. x_dmf[i * (WARP_SIZE/QI5_0) + i / QI5_0 + kbxd] = bxi->d;
  5391. }
  5392. }
  5393. static __dpct_inline__ float vec_dot_q5_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 int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5400. const int index_bx = i * (WARP_SIZE/QI5_0) + i/QI5_0 + k/QI5_0;
  5401. const float * x_dmf = (const float *) x_dm;
  5402. const float * y_df = (const float *) y_ds;
  5403. int u[2*VDR_Q5_0_Q8_1_MMQ];
  5404. #pragma unroll
  5405. for (int l = 0; l < VDR_Q5_0_Q8_1_MMQ; ++l) {
  5406. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5407. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_0) % WARP_SIZE];
  5408. }
  5409. return vec_dot_q8_0_q8_1_impl<QR5_0*VDR_Q5_0_Q8_1_MMQ>
  5410. (&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)]);
  5411. }
  5412. static __dpct_inline__ float
  5413. vec_dot_q5_1_q8_1(const void *__restrict__ vbq,
  5414. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5415. const block_q5_1 * bq5_1 = (const block_q5_1 *) vbq;
  5416. int vl[VDR_Q5_1_Q8_1_MMVQ];
  5417. int vh[VDR_Q5_1_Q8_1_MMVQ];
  5418. int u[2*VDR_Q5_1_Q8_1_MMVQ];
  5419. #pragma unroll
  5420. for (int i = 0; i < VDR_Q5_1_Q8_1_MMVQ; ++i) {
  5421. vl[i] = get_int_from_uint8_aligned(bq5_1->qs, iqs + i);
  5422. vh[i] = get_int_from_uint8_aligned(bq5_1->qh, 0) >> (4 * (iqs + i));
  5423. u[2*i+0] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5424. u[2*i+1] = get_int_from_int8_aligned(bq8_1->qs, iqs + i + QI5_1);
  5425. }
  5426. return vec_dot_q5_1_q8_1_impl<VDR_Q5_1_Q8_1_MMVQ>(vl, vh, u, bq5_1->dm, bq8_1->ds);
  5427. }
  5428. template <int mmq_y>
  5429. static __dpct_inline__ void
  5430. allocate_tiles_q5_1(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5431. int *tile_x_ql_q5_1, sycl::half2 *tile_x_dm_q5_1) {
  5432. (void)x_qh; (void)x_sc;
  5433. *x_ql = tile_x_ql_q5_1;
  5434. *x_dm = tile_x_dm_q5_1;
  5435. }
  5436. template <int mmq_y, int nwarps, bool need_check>
  5437. static __dpct_inline__ void
  5438. load_tiles_q5_1(const void *__restrict__ vx, int *__restrict__ x_ql,
  5439. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5440. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5441. const int &k, const int &blocks_per_row) {
  5442. (void)x_qh; (void)x_sc;
  5443. GGML_SYCL_ASSUME(i_offset >= 0);
  5444. GGML_SYCL_ASSUME(i_offset < nwarps);
  5445. GGML_SYCL_ASSUME(k >= 0);
  5446. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5447. const int kbx = k / QI5_1;
  5448. const int kqsx = k % QI5_1;
  5449. const block_q5_1 * bx0 = (const block_q5_1 *) vx;
  5450. #pragma unroll
  5451. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5452. int i = i0 + i_offset;
  5453. if (need_check) {
  5454. i = sycl::min(i, i_max);
  5455. }
  5456. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbx;
  5457. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5458. const int qh = get_int_from_uint8_aligned(bxi->qh, 0) >> (4 * (k % QI5_1));
  5459. int qs0 = (ql >> 0) & 0x0F0F0F0F;
  5460. qs0 |= (qh << 4) & 0x00000010; // 0 -> 4
  5461. qs0 |= (qh << 11) & 0x00001000; // 1 -> 12
  5462. qs0 |= (qh << 18) & 0x00100000; // 2 -> 20
  5463. qs0 |= (qh << 25) & 0x10000000; // 3 -> 28
  5464. x_ql[i * (2*WARP_SIZE + 1) + 2*k+0] = qs0;
  5465. int qs1 = (ql >> 4) & 0x0F0F0F0F;
  5466. qs1 |= (qh >> 12) & 0x00000010; // 16 -> 4
  5467. qs1 |= (qh >> 5) & 0x00001000; // 17 -> 12
  5468. qs1 |= (qh << 2) & 0x00100000; // 18 -> 20
  5469. qs1 |= (qh << 9) & 0x10000000; // 19 -> 28
  5470. x_ql[i * (2*WARP_SIZE + 1) + 2*k+1] = qs1;
  5471. }
  5472. const int blocks_per_tile_x_row = WARP_SIZE / QI5_1;
  5473. const int kbxd = k % blocks_per_tile_x_row;
  5474. #pragma unroll
  5475. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_1) {
  5476. int i = i0 + i_offset * QI5_1 + k / blocks_per_tile_x_row;
  5477. if (need_check) {
  5478. i = sycl::min(i, i_max);
  5479. }
  5480. const block_q5_1 * bxi = bx0 + i*blocks_per_row + kbxd;
  5481. x_dm[i * (WARP_SIZE/QI5_1) + i / QI5_1 + kbxd] = bxi->dm;
  5482. }
  5483. }
  5484. static __dpct_inline__ float vec_dot_q5_1_q8_1_mul_mat(
  5485. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5486. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5487. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5488. const int &i, const int &j, const int &k) {
  5489. (void)x_qh; (void)x_sc;
  5490. const int kyqs = k % (QI8_1/2) + QI8_1 * (k / (QI8_1/2));
  5491. const int index_bx = i * (WARP_SIZE/QI5_1) + + i/QI5_1 + k/QI5_1;
  5492. int u[2*VDR_Q5_1_Q8_1_MMQ];
  5493. #pragma unroll
  5494. for (int l = 0; l < VDR_Q5_1_Q8_1_MMQ; ++l) {
  5495. u[2*l+0] = y_qs[j * WARP_SIZE + (kyqs + l) % WARP_SIZE];
  5496. u[2*l+1] = y_qs[j * WARP_SIZE + (kyqs + l + QI5_1) % WARP_SIZE];
  5497. }
  5498. return vec_dot_q8_1_q8_1_impl<QR5_1*VDR_Q5_1_Q8_1_MMQ>
  5499. (&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)]);
  5500. }
  5501. static __dpct_inline__ float
  5502. vec_dot_q8_0_q8_1(const void *__restrict__ vbq,
  5503. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5504. const block_q8_0 * bq8_0 = (const block_q8_0 *) vbq;
  5505. int v[VDR_Q8_0_Q8_1_MMVQ];
  5506. int u[VDR_Q8_0_Q8_1_MMVQ];
  5507. #pragma unroll
  5508. for (int i = 0; i < VDR_Q8_0_Q8_1_MMVQ; ++i) {
  5509. v[i] = get_int_from_int8(bq8_0->qs, iqs + i);
  5510. u[i] = get_int_from_int8_aligned(bq8_1->qs, iqs + i);
  5511. }
  5512. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMVQ>(v, u, bq8_0->d,
  5513. bq8_1->ds[0]);
  5514. }
  5515. template <int mmq_y>
  5516. static __dpct_inline__ void
  5517. allocate_tiles_q8_0(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5518. int *tile_x_qs_q8_0, float *tile_x_d_q8_0) {
  5519. (void)x_qh; (void)x_sc;
  5520. *x_ql = tile_x_qs_q8_0;
  5521. *x_dm = (sycl::half2 *)tile_x_d_q8_0;
  5522. }
  5523. template <int mmq_y, int nwarps, bool need_check>
  5524. static __dpct_inline__ void
  5525. load_tiles_q8_0(const void *__restrict__ vx, int *__restrict__ x_ql,
  5526. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5527. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5528. const int &k, const int &blocks_per_row) {
  5529. (void)x_qh; (void)x_sc;
  5530. GGML_SYCL_ASSUME(i_offset >= 0);
  5531. GGML_SYCL_ASSUME(i_offset < nwarps);
  5532. GGML_SYCL_ASSUME(k >= 0);
  5533. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5534. const int kbx = k / QI8_0;
  5535. const int kqsx = k % QI8_0;
  5536. float * x_dmf = (float *) x_dm;
  5537. const block_q8_0 * bx0 = (const block_q8_0 *) 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_q8_0 * bxi = bx0 + i*blocks_per_row + kbx;
  5545. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_int8(bxi->qs, kqsx);
  5546. }
  5547. const int blocks_per_tile_x_row = WARP_SIZE / QI8_0;
  5548. const int kbxd = k % blocks_per_tile_x_row;
  5549. #pragma unroll
  5550. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI8_0) {
  5551. int i = i0 + i_offset * QI8_0 + k / blocks_per_tile_x_row;
  5552. if (need_check) {
  5553. i = sycl::min(i, i_max);
  5554. }
  5555. const block_q8_0 * bxi = bx0 + i*blocks_per_row + kbxd;
  5556. x_dmf[i * (WARP_SIZE/QI8_0) + i / QI8_0 + kbxd] = bxi->d;
  5557. }
  5558. }
  5559. static __dpct_inline__ float vec_dot_q8_0_q8_1_mul_mat(
  5560. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5561. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5562. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5563. const int &i, const int &j, const int &k) {
  5564. (void)x_qh; (void)x_sc;
  5565. const float * x_dmf = (const float *) x_dm;
  5566. const float * y_df = (const float *) y_ds;
  5567. return vec_dot_q8_0_q8_1_impl<VDR_Q8_0_Q8_1_MMQ>
  5568. (&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],
  5569. y_df[j * (WARP_SIZE/QI8_1) + k/QI8_1]);
  5570. }
  5571. static __dpct_inline__ float
  5572. vec_dot_q2_K_q8_1(const void *__restrict__ vbq,
  5573. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5574. const block_q2_K * bq2_K = (const block_q2_K *) vbq;
  5575. const int bq8_offset = QR2_K * (iqs / QI8_1);
  5576. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5577. const uint8_t * scales = bq2_K->scales + scale_offset;
  5578. const int v = get_int_from_uint8_aligned(bq2_K->qs, iqs);
  5579. int u[QR2_K];
  5580. float d8[QR2_K];
  5581. #pragma unroll
  5582. for (int i = 0; i < QR2_K; ++ i) {
  5583. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5584. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5585. }
  5586. return vec_dot_q2_K_q8_1_impl_mmvq(v, u, scales, bq2_K->dm, d8);
  5587. }
  5588. template <int mmq_y>
  5589. static __dpct_inline__ void
  5590. allocate_tiles_q2_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5591. int *tile_x_ql_q2_K, sycl::half2 *tile_x_dm_q2_K,
  5592. int *tile_x_sc_q2_K) {
  5593. (void)x_qh;
  5594. *x_ql = tile_x_ql_q2_K;
  5595. *x_dm = tile_x_dm_q2_K;
  5596. *x_sc = tile_x_sc_q2_K;
  5597. }
  5598. template <int mmq_y, int nwarps, bool need_check>
  5599. static __dpct_inline__ void
  5600. load_tiles_q2_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5601. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5602. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5603. const int &k, const int &blocks_per_row) {
  5604. (void)x_qh;
  5605. GGML_SYCL_ASSUME(i_offset >= 0);
  5606. GGML_SYCL_ASSUME(i_offset < nwarps);
  5607. GGML_SYCL_ASSUME(k >= 0);
  5608. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5609. const int kbx = k / QI2_K;
  5610. const int kqsx = k % QI2_K;
  5611. const block_q2_K * bx0 = (const block_q2_K *) vx;
  5612. #pragma unroll
  5613. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5614. int i = i0 + i_offset;
  5615. if (need_check) {
  5616. i = sycl::min(i, i_max);
  5617. }
  5618. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbx;
  5619. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5620. }
  5621. const int blocks_per_tile_x_row = WARP_SIZE / QI2_K;
  5622. const int kbxd = k % blocks_per_tile_x_row;
  5623. #pragma unroll
  5624. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI2_K) {
  5625. int i = (i0 + i_offset * QI2_K + k / blocks_per_tile_x_row) % mmq_y;
  5626. if (need_check) {
  5627. i = sycl::min(i, i_max);
  5628. }
  5629. const block_q2_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5630. x_dm[i * (WARP_SIZE/QI2_K) + i / QI2_K + kbxd] = bxi->dm;
  5631. }
  5632. #pragma unroll
  5633. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5634. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5635. if (need_check) {
  5636. i = sycl::min(i, i_max);
  5637. }
  5638. const block_q2_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI2_K/4);
  5639. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = get_int_from_uint8_aligned(bxi->scales, k % (QI2_K/4));
  5640. }
  5641. }
  5642. static __dpct_inline__ float vec_dot_q2_K_q8_1_mul_mat(
  5643. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5644. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5645. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5646. const int &i, const int &j, const int &k) {
  5647. (void)x_qh;
  5648. const int kbx = k / QI2_K;
  5649. const int ky = (k % QI2_K) * QR2_K;
  5650. const float * y_df = (const float *) y_ds;
  5651. int v[QR2_K*VDR_Q2_K_Q8_1_MMQ];
  5652. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI2_K + (QI2_K/2) * (ky/(2*QI2_K)) + ky % (QI2_K/2);
  5653. const int shift = 2 * ((ky % (2*QI2_K)) / (QI2_K/2));
  5654. #pragma unroll
  5655. for (int l = 0; l < QR2_K*VDR_Q2_K_Q8_1_MMQ; ++l) {
  5656. v[l] = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5657. }
  5658. const uint8_t * scales = ((const uint8_t *) &x_sc[i * (WARP_SIZE/4) + i/4 + kbx*4]) + ky/4;
  5659. const int index_y = j * WARP_SIZE + (QR2_K*k) % WARP_SIZE;
  5660. 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]);
  5661. }
  5662. static __dpct_inline__ float
  5663. vec_dot_q3_K_q8_1(const void *__restrict__ vbq,
  5664. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5665. const block_q3_K * bq3_K = (const block_q3_K *) vbq;
  5666. const int bq8_offset = QR3_K * (iqs / (QI3_K/2));
  5667. const int scale_offset = iqs - iqs % QI8_1 + (iqs % QI8_1) / (QI8_1/2);
  5668. const float d = bq3_K->d;
  5669. const int vl = get_int_from_uint8(bq3_K->qs, iqs);
  5670. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5671. const int vh = ~get_int_from_uint8(bq3_K->hmask, iqs % (QI3_K/2)) >> bq8_offset;
  5672. int u[QR3_K];
  5673. float d8[QR3_K];
  5674. #pragma unroll
  5675. for (int i = 0; i < QR3_K; ++i) {
  5676. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + i].qs, iqs % QI8_1);
  5677. d8[i] = bq8_1[bq8_offset + i].ds[0];
  5678. }
  5679. return vec_dot_q3_K_q8_1_impl_mmvq(vl, vh, u, bq3_K->scales, scale_offset, d, d8);
  5680. }
  5681. template <int mmq_y>
  5682. static __dpct_inline__ void
  5683. allocate_tiles_q3_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5684. int *tile_x_ql_q3_K, sycl::half2 *tile_x_dm_q3_K,
  5685. int *tile_x_qh_q3_K, int *tile_x_sc_q3_K) {
  5686. *x_ql = tile_x_ql_q3_K;
  5687. *x_dm = tile_x_dm_q3_K;
  5688. *x_qh = tile_x_qh_q3_K;
  5689. *x_sc = tile_x_sc_q3_K;
  5690. }
  5691. template <int mmq_y, int nwarps, bool need_check>
  5692. static __dpct_inline__ void
  5693. load_tiles_q3_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. GGML_SYCL_ASSUME(i_offset >= 0);
  5698. GGML_SYCL_ASSUME(i_offset < nwarps);
  5699. GGML_SYCL_ASSUME(k >= 0);
  5700. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5701. const int kbx = k / QI3_K;
  5702. const int kqsx = k % QI3_K;
  5703. const block_q3_K * bx0 = (const block_q3_K *) vx;
  5704. #pragma unroll
  5705. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5706. int i = i0 + i_offset;
  5707. if (need_check) {
  5708. i = sycl::min(i, i_max);
  5709. }
  5710. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbx;
  5711. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8(bxi->qs, kqsx);
  5712. }
  5713. const int blocks_per_tile_x_row = WARP_SIZE / QI3_K;
  5714. const int kbxd = k % blocks_per_tile_x_row;
  5715. float * x_dmf = (float *) x_dm;
  5716. #pragma unroll
  5717. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI3_K) {
  5718. int i = (i0 + i_offset * QI3_K + k / blocks_per_tile_x_row) % mmq_y;
  5719. if (need_check) {
  5720. i = sycl::min(i, i_max);
  5721. }
  5722. const block_q3_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5723. x_dmf[i * (WARP_SIZE/QI3_K) + i / QI3_K + kbxd] = bxi->d;
  5724. }
  5725. #pragma unroll
  5726. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 2) {
  5727. int i = i0 + i_offset * 2 + k / (WARP_SIZE/2);
  5728. if (need_check) {
  5729. i = sycl::min(i, i_max);
  5730. }
  5731. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/2)) / (QI3_K/2);
  5732. // invert the mask with ~ so that a 0/1 results in 4/0 being subtracted
  5733. x_qh[i * (WARP_SIZE/2) + i / 2 + k % (WARP_SIZE/2)] = ~get_int_from_uint8(bxi->hmask, k % (QI3_K/2));
  5734. }
  5735. #pragma unroll
  5736. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 4) {
  5737. int i = i0 + i_offset * 4 + k / (WARP_SIZE/4);
  5738. if (need_check) {
  5739. i = sycl::min(i, i_max);
  5740. }
  5741. const block_q3_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/4)) / (QI3_K/4);
  5742. const int ksc = k % (QI3_K/4);
  5743. const int ksc_low = ksc % (QI3_K/8);
  5744. const int shift_low = 4 * (ksc / (QI3_K/8));
  5745. const int sc_low = (get_int_from_uint8(bxi->scales, ksc_low) >> shift_low) & 0x0F0F0F0F;
  5746. const int ksc_high = QI3_K/8;
  5747. const int shift_high = 2 * ksc;
  5748. const int sc_high = ((get_int_from_uint8(bxi->scales, ksc_high) >> shift_high) << 4) & 0x30303030;
  5749. const int sc = dpct::vectorized_binary<sycl::char4>(
  5750. sc_low | sc_high, 0x20202020, dpct::sub_sat());
  5751. x_sc[i * (WARP_SIZE/4) + i / 4 + k % (WARP_SIZE/4)] = sc;
  5752. }
  5753. }
  5754. static __dpct_inline__ float vec_dot_q3_K_q8_1_mul_mat(
  5755. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5756. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5757. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5758. const int &i, const int &j, const int &k) {
  5759. const int kbx = k / QI3_K;
  5760. const int ky = (k % QI3_K) * QR3_K;
  5761. const float * x_dmf = (const float *) x_dm;
  5762. const float * y_df = (const float *) y_ds;
  5763. const int8_t * scales = ((const int8_t *) (x_sc + i * (WARP_SIZE/4) + i/4 + kbx*4)) + ky/4;
  5764. int v[QR3_K*VDR_Q3_K_Q8_1_MMQ];
  5765. #pragma unroll
  5766. for (int l = 0; l < QR3_K*VDR_Q3_K_Q8_1_MMQ; ++l) {
  5767. const int kqsx = i * (WARP_SIZE + 1) + kbx*QI3_K + (QI3_K/2) * (ky/(2*QI3_K)) + ky % (QI3_K/2);
  5768. const int shift = 2 * ((ky % 32) / 8);
  5769. const int vll = (x_ql[kqsx + l] >> shift) & 0x03030303;
  5770. const int vh = x_qh[i * (WARP_SIZE/2) + i/2 + kbx * (QI3_K/2) + (ky+l)%8] >> ((ky+l) / 8);
  5771. const int vlh = (vh << 2) & 0x04040404;
  5772. v[l] = dpct::vectorized_binary<sycl::char4>(vll, vlh, dpct::sub_sat());
  5773. }
  5774. const int index_y = j * WARP_SIZE + (k*QR3_K) % WARP_SIZE;
  5775. 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]);
  5776. }
  5777. static __dpct_inline__ float
  5778. vec_dot_q4_K_q8_1(const void *__restrict__ vbq,
  5779. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5780. #ifndef GGML_QKK_64
  5781. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5782. int v[2];
  5783. int u[2*QR4_K];
  5784. float d8[QR4_K];
  5785. // iqs is in 0,2..30. bq8_offset = iqs/4 -> bq8_offset = 0, 2, 4, 6
  5786. const int bq8_offset = QR4_K * ((iqs/2) / (QI8_1/2));
  5787. // iqs = 0....3 -> bq8_offset = 0, want q4_offset = 0, 4, 8, 12
  5788. // iqs = 4....7 -> bq8_offset = 2, want q4_offset = 32, 36, 40, 44
  5789. // iqs = 8...11 -> bq8_offset = 4, want q4_offset = 64, 68, 72, 76
  5790. // iqs = 12..15 -> bq8_offset = 6, want q4_offset = 96, 100, 104, 108
  5791. const int * q4 = (const int *)(bq4_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5792. v[0] = q4[0];
  5793. v[1] = q4[4];
  5794. const uint16_t * scales = (const uint16_t *)bq4_K->scales;
  5795. uint16_t aux[2];
  5796. const int j = bq8_offset/2;
  5797. if (j < 2) {
  5798. aux[0] = scales[j+0] & 0x3f3f;
  5799. aux[1] = scales[j+2] & 0x3f3f;
  5800. } else {
  5801. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5802. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5803. }
  5804. const uint8_t * sc = (const uint8_t *)aux;
  5805. const uint8_t * m = sc + 2;
  5806. for (int i = 0; i < QR4_K; ++i) {
  5807. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5808. d8[i] = bq8i->ds[0];
  5809. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5810. u[2*i+0] = q8[0];
  5811. u[2*i+1] = q8[4];
  5812. }
  5813. return vec_dot_q4_K_q8_1_impl_vmmq(v, u, sc, m, bq4_K->dm, d8);
  5814. #else
  5815. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  5816. const block_q4_K * bq4_K = (const block_q4_K *) vbq;
  5817. float sumf_d = 0.0f;
  5818. float sumf_m = 0.0f;
  5819. uint16_t aux16[2];
  5820. const uint8_t * s = (const uint8_t *)aux16;
  5821. const uint16_t * a = (const uint16_t *)bq4_K->scales;
  5822. aux16[0] = a[0] & 0x0f0f;
  5823. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5824. const float dall = bq4_K->dm[0];
  5825. const float dmin = bq4_K->dm[1];
  5826. const float d8_1 = bq8_1[0].ds[0];
  5827. const float d8_2 = bq8_1[1].ds[1];
  5828. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  5829. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  5830. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  5831. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  5832. const int * q4 = (const int *)bq4_K->qs + (iqs/2);
  5833. const int v1 = q4[0];
  5834. const int v2 = q4[4];
  5835. const int dot1 = dpct::dp4a(ui2, v2 & 0x0f0f0f0f, dpct::dp4a(ui1, v1 & 0x0f0f0f0f, 0));
  5836. const int dot2 = dpct::dp4a(ui4, (v2 >> 4) & 0x0f0f0f0f, dpct::dp4a(ui3, (v1 >> 4) & 0x0f0f0f0f, 0));
  5837. const int dot3 = dpct::dp4a(0x01010101, ui2, dpct::dp4a(0x01010101, ui1, 0));
  5838. const int dot4 = dpct::dp4a(0x01010101, ui4, dpct::dp4a(0x01010101, ui3, 0));
  5839. sumf_d += d8_1 * (dot1 * s[0]) + d8_2 * (dot2 * s[1]);
  5840. sumf_m += d8_1 * (dot3 * s[2]) + d8_2 * (dot4 * s[3]);
  5841. return dall * sumf_d - dmin * sumf_m;
  5842. #else
  5843. bad_arch();
  5844. #endif // __SYCL_ARCH__ >= VER_4VEC
  5845. #endif
  5846. }
  5847. template <int mmq_y>
  5848. static __dpct_inline__ void
  5849. allocate_tiles_q4_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5850. int *tile_x_ql_q4_K, sycl::half2 *tile_x_dm_q4_K,
  5851. int *tile_x_sc_q4_K) {
  5852. (void)x_qh;
  5853. *x_ql = tile_x_ql_q4_K;
  5854. *x_dm = tile_x_dm_q4_K;
  5855. *x_sc = tile_x_sc_q4_K;
  5856. }
  5857. template <int mmq_y, int nwarps, bool need_check>
  5858. static __dpct_inline__ void
  5859. load_tiles_q4_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  5860. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  5861. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  5862. const int &k, const int &blocks_per_row) {
  5863. (void)x_qh;
  5864. GGML_SYCL_ASSUME(i_offset >= 0);
  5865. GGML_SYCL_ASSUME(i_offset < nwarps);
  5866. GGML_SYCL_ASSUME(k >= 0);
  5867. GGML_SYCL_ASSUME(k < WARP_SIZE);
  5868. const int kbx = k / QI4_K; // == 0 if QK_K == 256
  5869. const int kqsx = k % QI4_K; // == k if QK_K == 256
  5870. const block_q4_K * bx0 = (const block_q4_K *) vx;
  5871. #pragma unroll
  5872. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  5873. int i = i0 + i_offset;
  5874. if (need_check) {
  5875. i = sycl::min(i, i_max);
  5876. }
  5877. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbx;
  5878. x_ql[i * (WARP_SIZE + 1) + k] = get_int_from_uint8_aligned(bxi->qs, kqsx);
  5879. }
  5880. const int blocks_per_tile_x_row = WARP_SIZE / QI4_K; // == 1 if QK_K == 256
  5881. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  5882. #pragma unroll
  5883. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI4_K) {
  5884. int i = (i0 + i_offset * QI4_K + k / blocks_per_tile_x_row) % mmq_y;
  5885. if (need_check) {
  5886. i = sycl::min(i, i_max);
  5887. }
  5888. const block_q4_K * bxi = bx0 + i*blocks_per_row + kbxd;
  5889. #if QK_K == 256
  5890. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = bxi->dm;
  5891. #else
  5892. x_dm[i * (WARP_SIZE/QI4_K) + i / QI4_K + kbxd] = {bxi->dm[0], bxi->dm[1]};
  5893. #endif
  5894. }
  5895. #pragma unroll
  5896. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  5897. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  5898. if (need_check) {
  5899. i = sycl::min(i, i_max);
  5900. }
  5901. const block_q4_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI4_K/8);
  5902. const int * scales = (const int *) bxi->scales;
  5903. const int ksc = k % (WARP_SIZE/8);
  5904. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  5905. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  5906. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  5907. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  5908. }
  5909. }
  5910. static __dpct_inline__ float vec_dot_q4_K_q8_1_mul_mat(
  5911. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  5912. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  5913. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  5914. const int &i, const int &j, const int &k) {
  5915. (void)x_qh;
  5916. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2*((k % 16) / 8);
  5917. const int index_y = j * WARP_SIZE + (QR4_K*k) % WARP_SIZE;
  5918. return vec_dot_q4_K_q8_1_impl_mmq(&x_ql[i * (WARP_SIZE + 1) + k], &y_qs[index_y], sc, sc+8,
  5919. x_dm[i * (WARP_SIZE/QI4_K) + i/QI4_K], &y_ds[index_y/QI8_1]);
  5920. }
  5921. static __dpct_inline__ float
  5922. vec_dot_q5_K_q8_1(const void *__restrict__ vbq,
  5923. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  5924. #ifndef GGML_QKK_64
  5925. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  5926. int vl[2];
  5927. int vh[2];
  5928. int u[2*QR5_K];
  5929. float d8[QR5_K];
  5930. const int bq8_offset = QR5_K * ((iqs/2) / (QI8_1/2));
  5931. const int * ql = (const int *)(bq5_K->qs + 16 * bq8_offset + 4 * ((iqs/2)%4));
  5932. const int * qh = (const int *)(bq5_K->qh + 4 * ((iqs/2)%4));
  5933. vl[0] = ql[0];
  5934. vl[1] = ql[4];
  5935. vh[0] = qh[0] >> bq8_offset;
  5936. vh[1] = qh[4] >> bq8_offset;
  5937. const uint16_t * scales = (const uint16_t *)bq5_K->scales;
  5938. uint16_t aux[2];
  5939. const int j = bq8_offset/2;
  5940. if (j < 2) {
  5941. aux[0] = scales[j+0] & 0x3f3f;
  5942. aux[1] = scales[j+2] & 0x3f3f;
  5943. } else {
  5944. aux[0] = ((scales[j+2] >> 0) & 0x0f0f) | ((scales[j-2] & 0xc0c0) >> 2);
  5945. aux[1] = ((scales[j+2] >> 4) & 0x0f0f) | ((scales[j-0] & 0xc0c0) >> 2);
  5946. }
  5947. const uint8_t * sc = (const uint8_t *)aux;
  5948. const uint8_t * m = sc + 2;
  5949. #pragma unroll
  5950. for (int i = 0; i < QR5_K; ++i) {
  5951. const block_q8_1 * bq8i = bq8_1 + bq8_offset + i;
  5952. d8[i] = bq8i->ds[0];
  5953. const int * q8 = (const int *)bq8i->qs + ((iqs/2)%4);
  5954. u[2*i+0] = q8[0];
  5955. u[2*i+1] = q8[4];
  5956. }
  5957. return vec_dot_q5_K_q8_1_impl_vmmq(vl, vh, u, sc, m, bq5_K->dm, d8);
  5958. #else
  5959. #if __SYCL_ARCH__ >= VER_4VEC // lowest compute capability for integer intrinsics
  5960. const block_q5_K * bq5_K = (const block_q5_K *) vbq;
  5961. const int8_t * s = bq5_K->scales;
  5962. const float d = bq5_K->d;
  5963. const float d8_1 = bq8_1[0].ds[0];
  5964. const float d8_2 = bq8_1[1].ds[1];
  5965. const int ui1 = *((const int *)bq8_1[0].qs + (iqs/2));
  5966. const int ui2 = *((const int *)bq8_1[0].qs + (iqs/2) + 4);
  5967. const int ui3 = *((const int *)bq8_1[1].qs + (iqs/2));
  5968. const int ui4 = *((const int *)bq8_1[1].qs + (iqs/2) + 4);
  5969. const int * ql = (const int *)bq5_K->qs + (iqs/2);
  5970. const int vl1 = ql[0];
  5971. const int vl2 = ql[4];
  5972. const int step = 4 * (iqs/2); // 0, 4, 8, 12
  5973. const int im = step/8; // = 0 for iqs = 0, 2, = 1 for iqs = 4, 6
  5974. const int in = step%8; // 0, 4, 0, 4
  5975. const int vh = (*((const int *)(bq5_K->qh + in))) >> im;
  5976. const int v1 = (((vh << 4) & 0x10101010) ^ 0x10101010) | ((vl1 >> 0) & 0x0f0f0f0f);
  5977. const int v2 = (((vh << 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 0) & 0x0f0f0f0f);
  5978. const int v3 = (((vh >> 0) & 0x10101010) ^ 0x10101010) | ((vl1 >> 4) & 0x0f0f0f0f);
  5979. const int v4 = (((vh >> 2) & 0x10101010) ^ 0x10101010) | ((vl2 >> 4) & 0x0f0f0f0f);
  5980. const float sumf_d = d8_1 * (dpct::dp4a(ui1, v1, 0) * s[0] + dpct::dp4a(ui2, v2, 0) * s[1])
  5981. + d8_2 * (dpct::dp4a(ui3, v3, 0) * s[2] + dpct::dp4a(ui4, v4, 0) * s[3]);
  5982. return d * sumf_d;
  5983. #else
  5984. bad_arch();
  5985. #endif // __SYCL_ARCH__ >= VER_4VEC
  5986. #endif
  5987. }
  5988. template <int mmq_y>
  5989. static __dpct_inline__ void
  5990. allocate_tiles_q5_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  5991. int *tile_x_ql_q5_K, sycl::half2 *tile_x_dm_q5_K,
  5992. int *tile_x_sc_q5_K) {
  5993. (void)x_qh;
  5994. *x_ql = tile_x_ql_q5_K;
  5995. *x_dm = tile_x_dm_q5_K;
  5996. *x_sc = tile_x_sc_q5_K;
  5997. }
  5998. template <int mmq_y, int nwarps, bool need_check>
  5999. static __dpct_inline__ void
  6000. load_tiles_q5_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  6001. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6002. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6003. const int &k, const int &blocks_per_row) {
  6004. (void)x_qh;
  6005. GGML_SYCL_ASSUME(i_offset >= 0);
  6006. GGML_SYCL_ASSUME(i_offset < nwarps);
  6007. GGML_SYCL_ASSUME(k >= 0);
  6008. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6009. const int kbx = k / QI5_K; // == 0 if QK_K == 256
  6010. const int kqsx = k % QI5_K; // == k if QK_K == 256
  6011. const block_q5_K * bx0 = (const block_q5_K *) vx;
  6012. #pragma unroll
  6013. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6014. int i = i0 + i_offset;
  6015. if (need_check) {
  6016. i = sycl::min(i, i_max);
  6017. }
  6018. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbx;
  6019. const int ky = QR5_K*kqsx;
  6020. const int ql = get_int_from_uint8_aligned(bxi->qs, kqsx);
  6021. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  6022. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  6023. const int qh = get_int_from_uint8_aligned(bxi->qh, kqsx % (QI5_K/4));
  6024. const int qh0 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 0)) << 4) & 0x10101010;
  6025. const int qh1 = ((qh >> (2 * (kqsx / (QI5_K/4)) + 1)) << 4) & 0x10101010;
  6026. const int kq0 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + 0;
  6027. const int kq1 = ky - ky % (QI5_K/2) + k % (QI5_K/4) + (QI5_K/4);
  6028. x_ql[i * (2*WARP_SIZE + 1) + kq0] = ql0 | qh0;
  6029. x_ql[i * (2*WARP_SIZE + 1) + kq1] = ql1 | qh1;
  6030. }
  6031. const int blocks_per_tile_x_row = WARP_SIZE / QI5_K; // == 1 if QK_K == 256
  6032. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  6033. #pragma unroll
  6034. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI5_K) {
  6035. int i = (i0 + i_offset * QI5_K + k / blocks_per_tile_x_row) % mmq_y;
  6036. if (need_check) {
  6037. i = sycl::min(i, i_max);
  6038. }
  6039. const block_q5_K * bxi = bx0 + i*blocks_per_row + kbxd;
  6040. #if QK_K == 256
  6041. x_dm[i * (WARP_SIZE/QI5_K) + i / QI5_K + kbxd] = bxi->dm;
  6042. #endif
  6043. }
  6044. #pragma unroll
  6045. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  6046. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  6047. if (need_check) {
  6048. i = sycl::min(i, i_max);
  6049. }
  6050. const block_q5_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / (QI5_K/8);
  6051. const int * scales = (const int *) bxi->scales;
  6052. const int ksc = k % (WARP_SIZE/8);
  6053. // scale arrangement after the following two lines: sc0,...,sc3, sc4,...,sc7, m0,...,m3, m4,...,m8
  6054. int scales8 = (scales[(ksc%2) + (ksc!=0)] >> (4 * (ksc & (ksc/2)))) & 0x0F0F0F0F; // lower 4 bits
  6055. scales8 |= (scales[ksc/2] >> (2 * (ksc % 2))) & 0x30303030; // upper 2 bits
  6056. x_sc[i * (WARP_SIZE/8) + i / 8 + ksc] = scales8;
  6057. }
  6058. }
  6059. static __dpct_inline__ float vec_dot_q5_K_q8_1_mul_mat(
  6060. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6061. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6062. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6063. const int &i, const int &j, const int &k) {
  6064. (void)x_qh;
  6065. const uint8_t * sc = ((const uint8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/16]) + 2 * ((k % 16) / 8);
  6066. const int index_x = i * (QR5_K*WARP_SIZE + 1) + QR5_K*k;
  6067. const int index_y = j * WARP_SIZE + (QR5_K*k) % WARP_SIZE;
  6068. return vec_dot_q5_K_q8_1_impl_mmq(&x_ql[index_x], &y_qs[index_y], sc, sc+8,
  6069. x_dm[i * (WARP_SIZE/QI5_K) + i/QI5_K], &y_ds[index_y/QI8_1]);
  6070. }
  6071. static __dpct_inline__ float
  6072. vec_dot_q6_K_q8_1(const void *__restrict__ vbq,
  6073. const block_q8_1 *__restrict__ bq8_1, const int &iqs) {
  6074. const block_q6_K * bq6_K = (const block_q6_K *) vbq;
  6075. const int bq8_offset = 2 * QR6_K * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/4);
  6076. const int scale_offset = (QI6_K/4) * (iqs / (QI6_K/2)) + (iqs % (QI6_K/2)) / (QI6_K/8);
  6077. const int vh_shift = 2 * ((iqs % (QI6_K/2)) / (QI6_K/4));
  6078. const int vl = get_int_from_uint8(bq6_K->ql, iqs);
  6079. const int vh = get_int_from_uint8(bq6_K->qh, (QI6_K/4) * (iqs / (QI6_K/2)) + iqs % (QI6_K/4)) >> vh_shift;
  6080. const int8_t * scales = bq6_K->scales + scale_offset;
  6081. int u[QR6_K];
  6082. float d8[QR6_K];
  6083. #pragma unroll
  6084. for (int i = 0; i < QR6_K; ++i) {
  6085. u[i] = get_int_from_int8_aligned(bq8_1[bq8_offset + 2*i].qs, iqs % QI8_1);
  6086. d8[i] = bq8_1[bq8_offset + 2 * i].ds[0];
  6087. }
  6088. return vec_dot_q6_K_q8_1_impl_mmvq(vl, vh, u, scales, bq6_K->d, d8);
  6089. }
  6090. template <int mmq_y>
  6091. static __dpct_inline__ void
  6092. allocate_tiles_q6_K(int **x_ql, sycl::half2 **x_dm, int **x_qh, int **x_sc,
  6093. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_sc) {
  6094. (void)x_qh;
  6095. *x_ql = tile_x_ql;
  6096. *x_dm = tile_x_dm;
  6097. *x_sc = tile_x_sc;
  6098. }
  6099. template <int mmq_y, int nwarps, bool need_check>
  6100. static __dpct_inline__ void
  6101. load_tiles_q6_K(const void *__restrict__ vx, int *__restrict__ x_ql,
  6102. sycl::half2 *__restrict__ x_dm, int *__restrict__ x_qh,
  6103. int *__restrict__ x_sc, const int &i_offset, const int &i_max,
  6104. const int &k, const int &blocks_per_row) {
  6105. (void)x_qh;
  6106. GGML_SYCL_ASSUME(i_offset >= 0);
  6107. GGML_SYCL_ASSUME(i_offset < nwarps);
  6108. GGML_SYCL_ASSUME(k >= 0);
  6109. GGML_SYCL_ASSUME(k < WARP_SIZE);
  6110. const int kbx = k / QI6_K; // == 0 if QK_K == 256
  6111. const int kqsx = k % QI6_K; // == k if QK_K == 256
  6112. const block_q6_K * bx0 = (const block_q6_K *) vx;
  6113. #pragma unroll
  6114. for (int i0 = 0; i0 < mmq_y; i0 += nwarps) {
  6115. int i = i0 + i_offset;
  6116. if (need_check) {
  6117. i = sycl::min(i, i_max);
  6118. }
  6119. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbx;
  6120. const int ky = QR6_K*kqsx;
  6121. const int ql = get_int_from_uint8(bxi->ql, kqsx);
  6122. const int ql0 = (ql >> 0) & 0x0F0F0F0F;
  6123. const int ql1 = (ql >> 4) & 0x0F0F0F0F;
  6124. const int qh = get_int_from_uint8(bxi->qh, (QI6_K/4) * (kqsx / (QI6_K/2)) + kqsx % (QI6_K/4));
  6125. const int qh0 = ((qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) << 4) & 0x30303030;
  6126. const int qh1 = (qh >> (2 * ((kqsx % (QI6_K/2)) / (QI6_K/4)))) & 0x30303030;
  6127. const int kq0 = ky - ky % QI6_K + k % (QI6_K/2) + 0;
  6128. const int kq1 = ky - ky % QI6_K + k % (QI6_K/2) + (QI6_K/2);
  6129. x_ql[i * (2 * WARP_SIZE + 1) + kq0] =
  6130. dpct::vectorized_binary<sycl::char4>(ql0 | qh0, 0x20202020,
  6131. dpct::sub_sat());
  6132. x_ql[i * (2 * WARP_SIZE + 1) + kq1] =
  6133. dpct::vectorized_binary<sycl::char4>(ql1 | qh1, 0x20202020,
  6134. dpct::sub_sat());
  6135. }
  6136. const int blocks_per_tile_x_row = WARP_SIZE / QI6_K; // == 1 if QK_K == 256
  6137. const int kbxd = k % blocks_per_tile_x_row; // == 0 if QK_K == 256
  6138. float * x_dmf = (float *) x_dm;
  6139. #pragma unroll
  6140. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * QI6_K) {
  6141. int i = (i0 + i_offset * QI6_K + k / blocks_per_tile_x_row) % mmq_y;
  6142. if (need_check) {
  6143. i = sycl::min(i, i_max);
  6144. }
  6145. const block_q6_K * bxi = bx0 + i*blocks_per_row + kbxd;
  6146. x_dmf[i * (WARP_SIZE/QI6_K) + i / QI6_K + kbxd] = bxi->d;
  6147. }
  6148. #pragma unroll
  6149. for (int i0 = 0; i0 < mmq_y; i0 += nwarps * 8) {
  6150. int i = (i0 + i_offset * 8 + k / (WARP_SIZE/8)) % mmq_y;
  6151. if (need_check) {
  6152. i = sycl::min(i, i_max);
  6153. }
  6154. const block_q6_K * bxi = bx0 + i*blocks_per_row + (k % (WARP_SIZE/8)) / 4;
  6155. x_sc[i * (WARP_SIZE/8) + i / 8 + k % (WARP_SIZE/8)] = get_int_from_int8(bxi->scales, k % (QI6_K/8));
  6156. }
  6157. }
  6158. static __dpct_inline__ float vec_dot_q6_K_q8_1_mul_mat(
  6159. const int *__restrict__ x_ql, const sycl::half2 *__restrict__ x_dm,
  6160. const int *__restrict__ x_qh, const int *__restrict__ x_sc,
  6161. const int *__restrict__ y_qs, const sycl::half2 *__restrict__ y_ds,
  6162. const int &i, const int &j, const int &k) {
  6163. (void)x_qh;
  6164. const float * x_dmf = (const float *) x_dm;
  6165. const float * y_df = (const float *) y_ds;
  6166. const int8_t * sc = ((const int8_t *) &x_sc[i * (WARP_SIZE/8) + i/8 + k/8]);
  6167. const int index_x = i * (QR6_K*WARP_SIZE + 1) + QR6_K*k;
  6168. const int index_y = j * WARP_SIZE + (QR6_K*k) % WARP_SIZE;
  6169. 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]);
  6170. }
  6171. static __dpct_inline__ float
  6172. vec_dot_iq2_xxs_q8_1(const void *__restrict__ vbq,
  6173. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6174. const uint64_t *iq2xxs_grid, const uint8_t *ksigns_iq2xs,
  6175. const uint8_t *kmask_iq2xs) {
  6176. #if QK_K == 256
  6177. const block_iq2_xxs * bq2 = (const block_iq2_xxs *) vbq;
  6178. #if QR2_XXS == 8
  6179. const int ib32 = iqs;
  6180. const uint16_t * q2 = bq2->qs + 4*ib32;
  6181. const uint8_t * aux8 = (const uint8_t *)q2;
  6182. const int8_t * q8 = bq8_1[ib32].qs;
  6183. uint32_t aux32 = q2[2] | (q2[3] << 16);
  6184. int sumi = 0;
  6185. for (int l = 0; l < 4; ++l) {
  6186. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6187. const uint8_t signs = ksigns_iq2xs[aux32 & 127];
  6188. for (int j = 0; j < 8; ++j) {
  6189. sumi += q8[j] * grid[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6190. }
  6191. q8 += 8;
  6192. aux32 >>= 7;
  6193. }
  6194. const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.25f;
  6195. return d * sumi;
  6196. #else
  6197. // iqs is 0...15
  6198. const int ib32 = iqs/2;
  6199. const int il = iqs%2;
  6200. const uint16_t * q2 = bq2->qs + 4*ib32;
  6201. const uint8_t * aux8 = (const uint8_t *)q2;
  6202. const uint8_t * grid1 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+0]);
  6203. const uint8_t * grid2 = (const uint8_t *)(iq2xxs_grid + aux8[2*il+1]);
  6204. const uint32_t aux32 = q2[2] | (q2[3] << 16);
  6205. const float d = (float)bq2->d * (0.5f + (aux32 >> 28)) * bq8_1[ib32].ds[0] * 0.25f;
  6206. const uint8_t signs1 = ksigns_iq2xs[(aux32 >> 14*il) & 127];
  6207. const uint8_t signs2 = ksigns_iq2xs[(aux32 >> (14*il + 7)) & 127];
  6208. const int8_t * q8 = bq8_1[ib32].qs + 16*il;
  6209. int sumi1 = 0, sumi2 = 0;
  6210. for (int j = 0; j < 8; ++j) {
  6211. sumi1 += q8[j+0] * grid1[j] * (signs1 & kmask_iq2xs[j] ? -1 : 1);
  6212. sumi2 += q8[j+8] * grid2[j] * (signs2 & kmask_iq2xs[j] ? -1 : 1);
  6213. }
  6214. return d * (sumi1 + sumi2);
  6215. #endif
  6216. #else
  6217. assert(false);
  6218. return 0.f;
  6219. #endif
  6220. }
  6221. static __dpct_inline__ float
  6222. vec_dot_iq2_xs_q8_1(const void *__restrict__ vbq,
  6223. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6224. const uint64_t *iq2xs_grid, const uint64_t *ksigns64) {
  6225. #if DPCT_COMPATIBILITY_TEMP >= \
  6226. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6227. #if QK_K == 256
  6228. const block_iq2_xs * bq2 = (const block_iq2_xs *) vbq;
  6229. const int ib32 = iqs;
  6230. const uint16_t * q2 = bq2->qs + 4*ib32;
  6231. const int8_t * q8 = bq8_1[ib32].qs;
  6232. const uint8_t ls1 = bq2->scales[ib32] & 0xf;
  6233. const uint8_t ls2 = bq2->scales[ib32] >> 4;
  6234. int sumi1 = 0;
  6235. for (int l = 0; l < 2; ++l) {
  6236. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  6237. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  6238. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6239. grid[0] ^ signs[0], signs[0], std::minus<>());
  6240. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6241. grid[1] ^ signs[1], signs[1], std::minus<>());
  6242. sumi1 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi1);
  6243. sumi1 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi1);
  6244. q8 += 8;
  6245. }
  6246. int sumi2 = 0;
  6247. for (int l = 2; l < 4; ++l) {
  6248. const uint32_t * grid = (const uint32_t *)(iq2xs_grid + (q2[l] & 511));
  6249. const uint32_t * signs = (const uint32_t *)(ksigns64 + (q2[l] >> 9));
  6250. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6251. grid[0] ^ signs[0], signs[0], std::minus<>());
  6252. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6253. grid[1] ^ signs[1], signs[1], std::minus<>());
  6254. sumi2 = dpct::dp4a(grid_l, *((const int *)q8 + 0), sumi2);
  6255. sumi2 = dpct::dp4a(grid_h, *((const int *)q8 + 1), sumi2);
  6256. q8 += 8;
  6257. }
  6258. const float d = (float)bq2->d * bq8_1[ib32].ds[0] * 0.25f;
  6259. return d * ((0.5f + ls1) * sumi1 + (0.5f + ls2) * sumi2);
  6260. #else
  6261. assert(false);
  6262. return 0.f;
  6263. #endif
  6264. #else
  6265. assert(false);
  6266. return 0.f;
  6267. #endif
  6268. }
  6269. static __dpct_inline__ float
  6270. vec_dot_iq3_xxs_q8_1(const void *__restrict__ vbq,
  6271. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6272. const uint32_t *iq3xxs_grid, const uint64_t *ksigns64) {
  6273. #if DPCT_COMPATIBILITY_TEMP >= \
  6274. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6275. #if QK_K == 256
  6276. const block_iq3_xxs * bq2 = (const block_iq3_xxs *) vbq;
  6277. const int ib32 = iqs;
  6278. const uint8_t * q3 = bq2->qs + 8*ib32;
  6279. const uint16_t * gas = (const uint16_t *)(bq2->qs + QK_K/4) + 2*ib32;
  6280. const int8_t * q8 = bq8_1[ib32].qs;
  6281. uint32_t aux32 = gas[0] | (gas[1] << 16);
  6282. int sumi = 0;
  6283. for (int l = 0; l < 4; ++l) {
  6284. const uint32_t * grid1 = iq3xxs_grid + q3[2*l+0];
  6285. const uint32_t * grid2 = iq3xxs_grid + q3[2*l+1];
  6286. const uint32_t * signs = (const uint32_t *)(ksigns64 + (aux32 & 127));
  6287. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6288. grid1[0] ^ signs[0], signs[0], std::minus<>());
  6289. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6290. grid2[0] ^ signs[1], signs[1], std::minus<>());
  6291. sumi = dpct::dp4a(grid_l, *((int *)q8 + 0), sumi);
  6292. sumi = dpct::dp4a(grid_h, *((int *)q8 + 1), sumi);
  6293. q8 += 8;
  6294. aux32 >>= 7;
  6295. }
  6296. const float d = (float)bq2->d * (0.5f + aux32) * bq8_1[ib32].ds[0] * 0.5f;
  6297. return d * sumi;
  6298. #else
  6299. assert(false);
  6300. return 0.f;
  6301. #endif
  6302. #else
  6303. assert(false);
  6304. return 0.f;
  6305. #endif
  6306. }
  6307. static __dpct_inline__ float
  6308. vec_dot_iq3_s_q8_1(const void *__restrict__ vbq,
  6309. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6310. const uint32_t *iq3s_grid, const uint64_t *ksigns64) {
  6311. #if DPCT_COMPATIBILITY_TEMP >= \
  6312. MIN_CC_DP4A // lowest compute capability for integer intrinsics
  6313. #if QK_K == 256
  6314. const block_iq3_s * bq2 = (const block_iq3_s *) vbq;
  6315. const int ib32 = iqs;
  6316. const uint8_t * qs = bq2->qs + 8*ib32;
  6317. const int8_t * q8 = bq8_1[ib32].qs;
  6318. int sumi = 0;
  6319. for (int l = 0; l < 4; ++l) {
  6320. const uint32_t * grid1 = iq3s_grid + (qs[2*l+0] | ((bq2->qh[ib32] << (8 - 2*l)) & 256));
  6321. const uint32_t * grid2 = iq3s_grid + (qs[2*l+1] | ((bq2->qh[ib32] << (7 - 2*l)) & 256));
  6322. uint32_t signs0 = dpct::vectorized_binary<sycl::uchar4>(
  6323. ((bq2->signs[4*ib32+l] & 0xf) * 0x01010101) & 0x08040201, 0x08040201, std::equal_to<>());
  6324. uint32_t signs1 = dpct::vectorized_binary<sycl::uchar4>(
  6325. ((bq2->signs[4*ib32+l] >> 4) * 0x01010101) & 0x08040201, 0x08040201, std::equal_to<>());
  6326. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6327. grid1[0] ^ signs0, signs0, std::minus<>());
  6328. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6329. grid2[0] ^ signs1, signs1, std::minus<>());
  6330. sumi = dpct::dp4a(grid_l, *((int *)q8 + 0), sumi);
  6331. sumi = dpct::dp4a(grid_h, *((int *)q8 + 1), sumi);
  6332. q8 += 8;
  6333. }
  6334. const float d = (float)bq2->d * (1 + 2*((bq2->scales[ib32/2] >> 4*(ib32%2)) & 0xf)) * bq8_1[ib32].ds[0];
  6335. return d * sumi;
  6336. #else
  6337. assert(false);
  6338. return 0.f;
  6339. #endif
  6340. #else
  6341. assert(false);
  6342. return 0.f;
  6343. #endif
  6344. }
  6345. static __dpct_inline__ float
  6346. vec_dot_iq1_s_q8_1(const void *__restrict__ vbq,
  6347. const block_q8_1 *__restrict__ bq8_1, const int &iqs,
  6348. const uint32_t *iq1s_grid, const uint64_t *ksigns64) {
  6349. #if QK_K == 256
  6350. const block_iq1_s * bq1 = (const block_iq1_s *) vbq;
  6351. const int ib32 = iqs;
  6352. const uint8_t * qs = bq1->qs + 4*ib32;
  6353. const int8_t * q8 = bq8_1[ib32].qs;
  6354. int sumi = 0;
  6355. for (int l = 0; l < 4; ++l) {
  6356. const uint32_t * grid = (const uint32_t *)(iq1s_grid + qs[l]);
  6357. const uint32_t * signs = (const uint32_t *)(ksigns64 + (qs[l] >> 8));
  6358. const int grid_l = dpct::vectorized_binary<sycl::uchar4>(
  6359. grid[0] ^ signs[0], signs[0], std::minus<>());
  6360. const int grid_h = dpct::vectorized_binary<sycl::uchar4>(
  6361. grid[1] ^ signs[1], signs[1], std::minus<>());
  6362. sumi = dpct::dp4a(grid_l, *((int *)q8 + 0), sumi);
  6363. sumi = dpct::dp4a(grid_h, *((int *)q8 + 1), sumi);
  6364. q8 += 8;
  6365. }
  6366. const float d = (float)bq1->d * bq8_1[ib32].ds[0] * 0.25f;
  6367. return d * sumi;
  6368. #else
  6369. assert(false);
  6370. return 0.f;
  6371. #endif
  6372. }
  6373. template <int qk, int qr, int qi, bool need_sum, typename block_q_t, int mmq_x,
  6374. int mmq_y, int nwarps, load_tiles_sycl_t load_tiles, int vdr,
  6375. vec_dot_q_mul_mat_sycl_t vec_dot>
  6376. /*
  6377. DPCT1110:8: The total declared local variable size in device function mul_mat_q
  6378. exceeds 128 bytes and may cause high register pressure. Consult with your
  6379. hardware vendor to find the total register size available and adjust the code,
  6380. or use smaller sub-group size to avoid high register pressure.
  6381. */
  6382. static __dpct_inline__ void
  6383. mul_mat_q(const void *__restrict__ vx, const void *__restrict__ vy,
  6384. float *__restrict__ dst, const int ncols_x, const int nrows_x,
  6385. const int ncols_y, const int nrows_y, const int nrows_dst,
  6386. int *tile_x_ql, sycl::half2 *tile_x_dm, int *tile_x_qh,
  6387. int *tile_x_sc, const sycl::nd_item<3> &item_ct1, int *tile_y_qs,
  6388. sycl::half2 *tile_y_ds) {
  6389. const block_q_t * x = (const block_q_t *) vx;
  6390. const block_q8_1 * y = (const block_q8_1 *) vy;
  6391. const int blocks_per_row_x = ncols_x / qk;
  6392. const int blocks_per_col_y = nrows_y / QK8_1;
  6393. const int blocks_per_warp = WARP_SIZE / qi;
  6394. const int & ncols_dst = ncols_y;
  6395. const int row_dst_0 = item_ct1.get_group(2) * mmq_y;
  6396. const int & row_x_0 = row_dst_0;
  6397. const int col_dst_0 = item_ct1.get_group(1) * mmq_x;
  6398. const int & col_y_0 = col_dst_0;
  6399. float sum[mmq_y/WARP_SIZE][mmq_x/nwarps] = {{0.0f}};
  6400. for (int ib0 = 0; ib0 < blocks_per_row_x; ib0 += blocks_per_warp) {
  6401. load_tiles(x + row_x_0 * blocks_per_row_x + ib0, tile_x_ql, tile_x_dm,
  6402. tile_x_qh, tile_x_sc, item_ct1.get_local_id(1),
  6403. nrows_x - row_x_0 - 1, item_ct1.get_local_id(2),
  6404. blocks_per_row_x);
  6405. #pragma unroll
  6406. for (int ir = 0; ir < qr; ++ir) {
  6407. const int kqs = ir * WARP_SIZE + item_ct1.get_local_id(2);
  6408. const int kbxd = kqs / QI8_1;
  6409. #pragma unroll
  6410. for (int i = 0; i < mmq_x; i += nwarps) {
  6411. const int col_y_eff = dpct::min(
  6412. (unsigned int)(col_y_0 + item_ct1.get_local_id(1) + i),
  6413. ncols_y - 1); // to prevent out-of-bounds memory accesses
  6414. const block_q8_1 * by0 = &y[col_y_eff*blocks_per_col_y + ib0 * (qk/QK8_1) + kbxd];
  6415. const int index_y = (item_ct1.get_local_id(1) + i) * WARP_SIZE +
  6416. kqs % WARP_SIZE;
  6417. tile_y_qs[index_y] = get_int_from_int8_aligned(
  6418. by0->qs, item_ct1.get_local_id(2) % QI8_1);
  6419. }
  6420. #pragma unroll
  6421. for (int ids0 = 0; ids0 < mmq_x; ids0 += nwarps * QI8_1) {
  6422. const int ids =
  6423. (ids0 + item_ct1.get_local_id(1) * QI8_1 +
  6424. item_ct1.get_local_id(2) / (WARP_SIZE / QI8_1)) %
  6425. mmq_x;
  6426. const int kby = item_ct1.get_local_id(2) % (WARP_SIZE / QI8_1);
  6427. const int col_y_eff = sycl::min(col_y_0 + ids, ncols_y - 1);
  6428. // if the sum is not needed it's faster to transform the scale to f32 ahead of time
  6429. const sycl::half2 *dsi_src =
  6430. &y[col_y_eff * blocks_per_col_y + ib0 * (qk / QK8_1) +
  6431. ir * (WARP_SIZE / QI8_1) + kby]
  6432. .ds;
  6433. sycl::half2 *dsi_dst =
  6434. &tile_y_ds[ids * (WARP_SIZE / QI8_1) + kby];
  6435. if (need_sum) {
  6436. *dsi_dst = *dsi_src;
  6437. } else {
  6438. float * dfi_dst = (float *) dsi_dst;
  6439. *dfi_dst = (*dsi_src)[0];
  6440. }
  6441. }
  6442. /*
  6443. DPCT1118:9: SYCL group functions and algorithms must be encountered
  6444. in converged control flow. You may need to adjust the code.
  6445. */
  6446. /*
  6447. DPCT1065:56: Consider replacing sycl::nd_item::barrier() with
  6448. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6449. better performance if there is no access to global memory.
  6450. */
  6451. item_ct1.barrier();
  6452. // #pragma unroll // unrolling this loop causes too much register pressure
  6453. for (int k = ir*WARP_SIZE/qr; k < (ir+1)*WARP_SIZE/qr; k += vdr) {
  6454. #pragma unroll
  6455. for (int j = 0; j < mmq_x; j += nwarps) {
  6456. #pragma unroll
  6457. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  6458. sum[i / WARP_SIZE][j / nwarps] += vec_dot(
  6459. tile_x_ql, tile_x_dm, tile_x_qh, tile_x_sc,
  6460. tile_y_qs, tile_y_ds, item_ct1.get_local_id(2) + i,
  6461. item_ct1.get_local_id(1) + j, k);
  6462. }
  6463. }
  6464. }
  6465. /*
  6466. DPCT1118:10: SYCL group functions and algorithms must be encountered
  6467. in converged control flow. You may need to adjust the code.
  6468. */
  6469. /*
  6470. DPCT1065:57: Consider replacing sycl::nd_item::barrier() with
  6471. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  6472. better performance if there is no access to global memory.
  6473. */
  6474. item_ct1.barrier();
  6475. }
  6476. }
  6477. #pragma unroll
  6478. for (int j = 0; j < mmq_x; j += nwarps) {
  6479. const int col_dst = col_dst_0 + j + item_ct1.get_local_id(1);
  6480. if (col_dst >= ncols_dst) {
  6481. return;
  6482. }
  6483. #pragma unroll
  6484. for (int i = 0; i < mmq_y; i += WARP_SIZE) {
  6485. const int row_dst = row_dst_0 + item_ct1.get_local_id(2) + i;
  6486. if (row_dst >= nrows_dst) {
  6487. continue;
  6488. }
  6489. dst[col_dst*nrows_dst + row_dst] = sum[i/WARP_SIZE][j/nwarps];
  6490. }
  6491. }
  6492. }
  6493. #define MMQ_X_Q4_0_RDNA2 64
  6494. #define MMQ_Y_Q4_0_RDNA2 128
  6495. #define NWARPS_Q4_0_RDNA2 8
  6496. #define MMQ_X_Q4_0_RDNA1 64
  6497. #define MMQ_Y_Q4_0_RDNA1 64
  6498. #define NWARPS_Q4_0_RDNA1 8
  6499. #if defined(SYCL_USE_XMX)
  6500. #define MMQ_X_Q4_0_AMPERE 4
  6501. #define MMQ_Y_Q4_0_AMPERE 32
  6502. #define NWARPS_Q4_0_AMPERE 4
  6503. #else
  6504. #define MMQ_X_Q4_0_AMPERE 64
  6505. #define MMQ_Y_Q4_0_AMPERE 128
  6506. #define NWARPS_Q4_0_AMPERE 4
  6507. #endif
  6508. #define MMQ_X_Q4_0_PASCAL 64
  6509. #define MMQ_Y_Q4_0_PASCAL 64
  6510. #define NWARPS_Q4_0_PASCAL 8
  6511. template <bool need_check> static void
  6512. mul_mat_q4_0(
  6513. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6514. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6515. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_0, float *tile_x_d_q4_0,
  6516. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6517. int * tile_x_ql = nullptr;
  6518. sycl::half2 *tile_x_dm = nullptr;
  6519. int * tile_x_qh = nullptr;
  6520. int * tile_x_sc = nullptr;
  6521. //sycl_todo: change according to hardware
  6522. const int mmq_x = MMQ_X_Q4_0_AMPERE;
  6523. const int mmq_y = MMQ_Y_Q4_0_AMPERE;
  6524. const int nwarps = NWARPS_Q4_0_AMPERE;
  6525. allocate_tiles_q4_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6526. tile_x_qs_q4_0, tile_x_d_q4_0);
  6527. mul_mat_q<QK4_0, QR4_0, QI4_0, true, block_q4_0, mmq_x, mmq_y, nwarps,
  6528. load_tiles_q4_0<mmq_y, nwarps, need_check>, VDR_Q4_0_Q8_1_MMQ,
  6529. vec_dot_q4_0_q8_1_mul_mat>(
  6530. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6531. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6532. }
  6533. #define MMQ_X_Q4_1_RDNA2 64
  6534. #define MMQ_Y_Q4_1_RDNA2 128
  6535. #define NWARPS_Q4_1_RDNA2 8
  6536. #define MMQ_X_Q4_1_RDNA1 64
  6537. #define MMQ_Y_Q4_1_RDNA1 64
  6538. #define NWARPS_Q4_1_RDNA1 8
  6539. #if defined(SYCL_USE_XMX)
  6540. #define MMQ_X_Q4_1_AMPERE 4
  6541. #define MMQ_Y_Q4_1_AMPERE 32
  6542. #define NWARPS_Q4_1_AMPERE 4
  6543. #else
  6544. #define MMQ_X_Q4_1_AMPERE 64
  6545. #define MMQ_Y_Q4_1_AMPERE 128
  6546. #define NWARPS_Q4_1_AMPERE 4
  6547. #endif
  6548. #define MMQ_X_Q4_1_PASCAL 64
  6549. #define MMQ_Y_Q4_1_PASCAL 64
  6550. #define NWARPS_Q4_1_PASCAL 8
  6551. template <bool need_check> static void
  6552. mul_mat_q4_1(
  6553. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6554. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6555. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q4_1,
  6556. sycl::half2 *tile_x_dm_q4_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6557. int * tile_x_ql = nullptr;
  6558. sycl::half2 *tile_x_dm = nullptr;
  6559. int * tile_x_qh = nullptr;
  6560. int * tile_x_sc = nullptr;
  6561. //sycl_todo: change according to hardware
  6562. const int mmq_x = MMQ_X_Q4_1_AMPERE;
  6563. const int mmq_y = MMQ_Y_Q4_1_AMPERE;
  6564. const int nwarps = NWARPS_Q4_1_AMPERE;
  6565. allocate_tiles_q4_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6566. tile_x_qs_q4_1, tile_x_dm_q4_1);
  6567. mul_mat_q<QK4_1, QR4_1, QI4_1, true, block_q4_1, mmq_x, mmq_y, nwarps,
  6568. load_tiles_q4_1<mmq_y, nwarps, need_check>, VDR_Q4_1_Q8_1_MMQ,
  6569. vec_dot_q4_1_q8_1_mul_mat>(
  6570. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6571. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6572. }
  6573. #define MMQ_X_Q5_0_RDNA2 64
  6574. #define MMQ_Y_Q5_0_RDNA2 128
  6575. #define NWARPS_Q5_0_RDNA2 8
  6576. #define MMQ_X_Q5_0_RDNA1 64
  6577. #define MMQ_Y_Q5_0_RDNA1 64
  6578. #define NWARPS_Q5_0_RDNA1 8
  6579. #if defined(SYCL_USE_XMX)
  6580. #define MMQ_X_Q5_0_AMPERE 4
  6581. #define MMQ_Y_Q5_0_AMPERE 32
  6582. #define NWARPS_Q5_0_AMPERE 4
  6583. #else
  6584. #define MMQ_X_Q5_0_AMPERE 128
  6585. #define MMQ_Y_Q5_0_AMPERE 64
  6586. #define NWARPS_Q5_0_AMPERE 4
  6587. #endif
  6588. #define MMQ_X_Q5_0_PASCAL 64
  6589. #define MMQ_Y_Q5_0_PASCAL 64
  6590. #define NWARPS_Q5_0_PASCAL 8
  6591. template <bool need_check> static void
  6592. mul_mat_q5_0(
  6593. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6594. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6595. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_0, float *tile_x_d_q5_0,
  6596. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6597. int * tile_x_ql = nullptr;
  6598. sycl::half2 *tile_x_dm = nullptr;
  6599. int * tile_x_qh = nullptr;
  6600. int * tile_x_sc = nullptr;
  6601. //sycl_todo: change according to hardware
  6602. const int mmq_x = MMQ_X_Q5_0_AMPERE;
  6603. const int mmq_y = MMQ_Y_Q5_0_AMPERE;
  6604. const int nwarps = NWARPS_Q5_0_AMPERE;
  6605. allocate_tiles_q5_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6606. tile_x_ql_q5_0, tile_x_d_q5_0);
  6607. mul_mat_q<QK5_0, QR5_0, QI5_0, false, block_q5_0, mmq_x, mmq_y, nwarps,
  6608. load_tiles_q5_0<mmq_y, nwarps, need_check>, VDR_Q5_0_Q8_1_MMQ,
  6609. vec_dot_q5_0_q8_1_mul_mat>(
  6610. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6611. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6612. }
  6613. #define MMQ_X_Q5_1_RDNA2 64
  6614. #define MMQ_Y_Q5_1_RDNA2 128
  6615. #define NWARPS_Q5_1_RDNA2 8
  6616. #define MMQ_X_Q5_1_RDNA1 64
  6617. #define MMQ_Y_Q5_1_RDNA1 64
  6618. #define NWARPS_Q5_1_RDNA1 8
  6619. #if defined(SYCL_USE_XMX)
  6620. #define MMQ_X_Q5_1_AMPERE 4
  6621. #define MMQ_Y_Q5_1_AMPERE 32
  6622. #define NWARPS_Q5_1_AMPERE 4
  6623. #else
  6624. #define MMQ_X_Q5_1_AMPERE 128
  6625. #define MMQ_Y_Q5_1_AMPERE 64
  6626. #define NWARPS_Q5_1_AMPERE 4
  6627. #endif
  6628. #define MMQ_X_Q5_1_PASCAL 64
  6629. #define MMQ_Y_Q5_1_PASCAL 64
  6630. #define NWARPS_Q5_1_PASCAL 8
  6631. template <bool need_check> static void
  6632. mul_mat_q5_1(
  6633. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6634. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6635. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_1,
  6636. sycl::half2 *tile_x_dm_q5_1, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6637. int * tile_x_ql = nullptr;
  6638. sycl::half2 *tile_x_dm = nullptr;
  6639. int * tile_x_qh = nullptr;
  6640. int * tile_x_sc = nullptr;
  6641. //sycl_todo: change according to hardware
  6642. const int mmq_x = MMQ_X_Q5_1_AMPERE;
  6643. const int mmq_y = MMQ_Y_Q5_1_AMPERE;
  6644. const int nwarps = NWARPS_Q5_1_AMPERE;
  6645. allocate_tiles_q5_1<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6646. tile_x_ql_q5_1, tile_x_dm_q5_1);
  6647. mul_mat_q<QK5_1, QR5_1, QI5_1, true, block_q5_1, mmq_x, mmq_y, nwarps,
  6648. load_tiles_q5_1<mmq_y, nwarps, need_check>, VDR_Q5_1_Q8_1_MMQ,
  6649. vec_dot_q5_1_q8_1_mul_mat>(
  6650. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6651. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6652. }
  6653. #define MMQ_X_Q8_0_RDNA2 64
  6654. #define MMQ_Y_Q8_0_RDNA2 128
  6655. #define NWARPS_Q8_0_RDNA2 8
  6656. #define MMQ_X_Q8_0_RDNA1 64
  6657. #define MMQ_Y_Q8_0_RDNA1 64
  6658. #define NWARPS_Q8_0_RDNA1 8
  6659. #if defined(SYCL_USE_XMX)
  6660. #define MMQ_X_Q8_0_AMPERE 4
  6661. #define MMQ_Y_Q8_0_AMPERE 32
  6662. #define NWARPS_Q8_0_AMPERE 4
  6663. #else
  6664. #define MMQ_X_Q8_0_AMPERE 128
  6665. #define MMQ_Y_Q8_0_AMPERE 64
  6666. #define NWARPS_Q8_0_AMPERE 4
  6667. #endif
  6668. #define MMQ_X_Q8_0_PASCAL 64
  6669. #define MMQ_Y_Q8_0_PASCAL 64
  6670. #define NWARPS_Q8_0_PASCAL 8
  6671. template <bool need_check> static void
  6672. mul_mat_q8_0(
  6673. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6674. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6675. const sycl::nd_item<3> &item_ct1, int *tile_x_qs_q8_0, float *tile_x_d_q8_0,
  6676. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6677. int * tile_x_ql = nullptr;
  6678. sycl::half2 *tile_x_dm = nullptr;
  6679. int * tile_x_qh = nullptr;
  6680. int * tile_x_sc = nullptr;
  6681. //sycl_todo: change according to hardware
  6682. const int mmq_x = MMQ_X_Q8_0_AMPERE;
  6683. const int mmq_y = MMQ_Y_Q8_0_AMPERE;
  6684. const int nwarps = NWARPS_Q8_0_AMPERE;
  6685. allocate_tiles_q8_0<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6686. tile_x_qs_q8_0, tile_x_d_q8_0);
  6687. mul_mat_q<QK8_0, QR8_0, QI8_0, false, block_q8_0, mmq_x, mmq_y, nwarps,
  6688. load_tiles_q8_0<mmq_y, nwarps, need_check>, VDR_Q8_0_Q8_1_MMQ,
  6689. vec_dot_q8_0_q8_1_mul_mat>(
  6690. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6691. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6692. }
  6693. #define MMQ_X_Q2_K_RDNA2 64
  6694. #define MMQ_Y_Q2_K_RDNA2 128
  6695. #define NWARPS_Q2_K_RDNA2 8
  6696. #define MMQ_X_Q2_K_RDNA1 128
  6697. #define MMQ_Y_Q2_K_RDNA1 32
  6698. #define NWARPS_Q2_K_RDNA1 8
  6699. #if defined(SYCL_USE_XMX)
  6700. #define MMQ_X_Q2_K_AMPERE 4
  6701. #define MMQ_Y_Q2_K_AMPERE 32
  6702. #define NWARPS_Q2_K_AMPERE 4
  6703. #else
  6704. #define MMQ_X_Q2_K_AMPERE 64
  6705. #define MMQ_Y_Q2_K_AMPERE 128
  6706. #define NWARPS_Q2_K_AMPERE 4
  6707. #endif
  6708. #define MMQ_X_Q2_K_PASCAL 64
  6709. #define MMQ_Y_Q2_K_PASCAL 64
  6710. #define NWARPS_Q2_K_PASCAL 8
  6711. template <bool need_check> static void
  6712. mul_mat_q2_K(
  6713. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6714. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6715. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q2_K,
  6716. sycl::half2 *tile_x_dm_q2_K, int *tile_x_sc_q2_K, int *tile_y_qs,
  6717. sycl::half2 *tile_y_ds) {
  6718. int * tile_x_ql = nullptr;
  6719. sycl::half2 *tile_x_dm = nullptr;
  6720. int * tile_x_qh = nullptr;
  6721. int * tile_x_sc = nullptr;
  6722. //sycl_todo: change according to hardware
  6723. const int mmq_x = MMQ_X_Q2_K_AMPERE;
  6724. const int mmq_y = MMQ_Y_Q2_K_AMPERE;
  6725. const int nwarps = NWARPS_Q2_K_AMPERE;
  6726. allocate_tiles_q2_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6727. tile_x_ql_q2_K, tile_x_dm_q2_K, tile_x_sc_q2_K);
  6728. mul_mat_q<QK_K, QR2_K, QI2_K, false, block_q2_K, mmq_x, mmq_y, nwarps,
  6729. load_tiles_q2_K<mmq_y, nwarps, need_check>, VDR_Q2_K_Q8_1_MMQ,
  6730. vec_dot_q2_K_q8_1_mul_mat>(
  6731. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6732. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6733. }
  6734. #define MMQ_X_Q3_K_RDNA2 128
  6735. #define MMQ_Y_Q3_K_RDNA2 64
  6736. #define NWARPS_Q3_K_RDNA2 8
  6737. #define MMQ_X_Q3_K_RDNA1 32
  6738. #define MMQ_Y_Q3_K_RDNA1 128
  6739. #define NWARPS_Q3_K_RDNA1 8
  6740. #if defined(SYCL_USE_XMX)
  6741. #define MMQ_X_Q3_K_AMPERE 4
  6742. #define MMQ_Y_Q3_K_AMPERE 32
  6743. #define NWARPS_Q3_K_AMPERE 4
  6744. #else
  6745. #define MMQ_X_Q3_K_AMPERE 128
  6746. #define MMQ_Y_Q3_K_AMPERE 128
  6747. #define NWARPS_Q3_K_AMPERE 4
  6748. #endif
  6749. #define MMQ_X_Q3_K_PASCAL 64
  6750. #define MMQ_Y_Q3_K_PASCAL 64
  6751. #define NWARPS_Q3_K_PASCAL 8
  6752. template <bool need_check> static void
  6753. mul_mat_q3_K(
  6754. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6755. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6756. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q3_K,
  6757. sycl::half2 *tile_x_dm_q3_K, int *tile_x_qh_q3_K, int *tile_x_sc_q3_K,
  6758. int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6759. int * tile_x_ql = nullptr;
  6760. sycl::half2 *tile_x_dm = nullptr;
  6761. int * tile_x_qh = nullptr;
  6762. int * tile_x_sc = nullptr;
  6763. //sycl_todo: change according to hardware
  6764. const int mmq_x = MMQ_X_Q3_K_AMPERE;
  6765. const int mmq_y = MMQ_Y_Q3_K_AMPERE;
  6766. const int nwarps = NWARPS_Q3_K_AMPERE;
  6767. allocate_tiles_q3_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6768. tile_x_ql_q3_K, tile_x_dm_q3_K, tile_x_qh_q3_K,
  6769. tile_x_sc_q3_K);
  6770. mul_mat_q<QK_K, QR3_K, QI3_K, false, block_q3_K, mmq_x, mmq_y, nwarps,
  6771. load_tiles_q3_K<mmq_y, nwarps, need_check>, VDR_Q3_K_Q8_1_MMQ,
  6772. vec_dot_q3_K_q8_1_mul_mat>(
  6773. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6774. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6775. }
  6776. #define MMQ_X_Q4_K_RDNA2 64
  6777. #define MMQ_Y_Q4_K_RDNA2 128
  6778. #define NWARPS_Q4_K_RDNA2 8
  6779. #define MMQ_X_Q4_K_RDNA1 32
  6780. #define MMQ_Y_Q4_K_RDNA1 64
  6781. #define NWARPS_Q4_K_RDNA1 8
  6782. #if defined(SYCL_USE_XMX)
  6783. #define MMQ_X_Q4_K_AMPERE 4
  6784. #define MMQ_Y_Q4_K_AMPERE 32
  6785. #define NWARPS_Q4_K_AMPERE 4
  6786. #else
  6787. #define MMQ_X_Q4_K_AMPERE 64
  6788. #define MMQ_Y_Q4_K_AMPERE 128
  6789. #define NWARPS_Q4_K_AMPERE 4
  6790. #endif
  6791. #define MMQ_X_Q4_K_PASCAL 64
  6792. #define MMQ_Y_Q4_K_PASCAL 64
  6793. #define NWARPS_Q4_K_PASCAL 8
  6794. template <bool need_check> static void
  6795. mul_mat_q4_K(
  6796. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6797. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6798. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q4_K,
  6799. sycl::half2 *tile_x_dm_q4_K, int *tile_x_sc_q4_K, int *tile_y_qs,
  6800. sycl::half2 *tile_y_ds) {
  6801. int * tile_x_ql = nullptr;
  6802. sycl::half2 *tile_x_dm = nullptr;
  6803. int * tile_x_qh = nullptr;
  6804. int * tile_x_sc = nullptr;
  6805. //sycl_todo: change according to hardware
  6806. const int mmq_x = MMQ_X_Q4_K_AMPERE;
  6807. const int mmq_y = MMQ_Y_Q4_K_AMPERE;
  6808. const int nwarps = NWARPS_Q4_K_AMPERE;
  6809. allocate_tiles_q4_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6810. tile_x_ql_q4_K, tile_x_dm_q4_K, tile_x_sc_q4_K);
  6811. mul_mat_q<QK_K, QR4_K, QI4_K, true, block_q4_K, mmq_x, mmq_y, nwarps,
  6812. load_tiles_q4_K<mmq_y, nwarps, need_check>, VDR_Q4_K_Q8_1_MMQ,
  6813. vec_dot_q4_K_q8_1_mul_mat>(
  6814. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6815. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6816. }
  6817. #define MMQ_X_Q5_K_RDNA2 64
  6818. #define MMQ_Y_Q5_K_RDNA2 128
  6819. #define NWARPS_Q5_K_RDNA2 8
  6820. #define MMQ_X_Q5_K_RDNA1 32
  6821. #define MMQ_Y_Q5_K_RDNA1 64
  6822. #define NWARPS_Q5_K_RDNA1 8
  6823. #if defined(SYCL_USE_XMX)
  6824. #define MMQ_X_Q5_K_AMPERE 4
  6825. #define MMQ_Y_Q5_K_AMPERE 32
  6826. #define NWARPS_Q5_K_AMPERE 4
  6827. #else
  6828. #define MMQ_X_Q5_K_AMPERE 64
  6829. #define MMQ_Y_Q5_K_AMPERE 128
  6830. #define NWARPS_Q5_K_AMPERE 4
  6831. #endif
  6832. #define MMQ_X_Q5_K_PASCAL 64
  6833. #define MMQ_Y_Q5_K_PASCAL 64
  6834. #define NWARPS_Q5_K_PASCAL 8
  6835. template <bool need_check> static void
  6836. mul_mat_q5_K(
  6837. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6838. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6839. const sycl::nd_item<3> &item_ct1, int *tile_x_ql_q5_K,
  6840. sycl::half2 *tile_x_dm_q5_K, int *tile_x_sc_q5_K, int *tile_y_qs,
  6841. sycl::half2 *tile_y_ds) {
  6842. int * tile_x_ql = nullptr;
  6843. sycl::half2 *tile_x_dm = nullptr;
  6844. int * tile_x_qh = nullptr;
  6845. int * tile_x_sc = nullptr;
  6846. //sycl_todo: change according to hardware
  6847. const int mmq_x = MMQ_X_Q5_K_AMPERE;
  6848. const int mmq_y = MMQ_Y_Q5_K_AMPERE;
  6849. const int nwarps = NWARPS_Q5_K_AMPERE;
  6850. allocate_tiles_q5_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6851. tile_x_ql_q5_K, tile_x_dm_q5_K, tile_x_sc_q5_K);
  6852. mul_mat_q<QK_K, QR5_K, QI5_K, true, block_q5_K, mmq_x, mmq_y, nwarps,
  6853. load_tiles_q5_K<mmq_y, nwarps, need_check>, VDR_Q5_K_Q8_1_MMQ,
  6854. vec_dot_q5_K_q8_1_mul_mat>(
  6855. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6856. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6857. }
  6858. #define MMQ_X_Q6_K_RDNA2 64
  6859. #define MMQ_Y_Q6_K_RDNA2 128
  6860. #define NWARPS_Q6_K_RDNA2 8
  6861. #define MMQ_X_Q6_K_RDNA1 32
  6862. #define MMQ_Y_Q6_K_RDNA1 64
  6863. #define NWARPS_Q6_K_RDNA1 8
  6864. #if defined(SYCL_USE_XMX)
  6865. #define MMQ_X_Q6_K_AMPERE 4
  6866. #define MMQ_Y_Q6_K_AMPERE 32
  6867. #define NWARPS_Q6_K_AMPERE 4
  6868. #else
  6869. #define MMQ_X_Q6_K_AMPERE 64
  6870. #define MMQ_Y_Q6_K_AMPERE 64
  6871. #define NWARPS_Q6_K_AMPERE 4
  6872. #endif
  6873. #define MMQ_X_Q6_K_PASCAL 64
  6874. #define MMQ_Y_Q6_K_PASCAL 64
  6875. #define NWARPS_Q6_K_PASCAL 8
  6876. template <bool need_check> static void
  6877. mul_mat_q6_K(
  6878. const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
  6879. const int ncols_x, const int nrows_x, const int ncols_y, const int nrows_y, const int nrows_dst,
  6880. const sycl::nd_item<3> &item_ct1, int *tile_x_ql, sycl::half2 *tile_x_dm,
  6881. int *tile_x_sc, int *tile_y_qs, sycl::half2 *tile_y_ds) {
  6882. // int * tile_x_ql = nullptr;
  6883. // sycl::half2 *tile_x_dm = nullptr;
  6884. int * tile_x_qh = nullptr;
  6885. // int * tile_x_sc = nullptr;
  6886. //sycl_todo: change according to hardware
  6887. const int mmq_x = MMQ_X_Q6_K_AMPERE;
  6888. const int mmq_y = MMQ_Y_Q6_K_AMPERE;
  6889. const int nwarps = NWARPS_Q6_K_AMPERE;
  6890. allocate_tiles_q6_K<mmq_y>(&tile_x_ql, &tile_x_dm, &tile_x_qh, &tile_x_sc,
  6891. tile_x_ql, tile_x_dm, tile_x_sc);
  6892. mul_mat_q<QK_K, QR6_K, QI6_K, false, block_q6_K, mmq_x, mmq_y, nwarps,
  6893. load_tiles_q6_K<mmq_y, nwarps, need_check>, VDR_Q6_K_Q8_1_MMQ,
  6894. vec_dot_q6_K_q8_1_mul_mat>(
  6895. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y, nrows_dst, tile_x_ql,
  6896. tile_x_dm, tile_x_qh, tile_x_sc, item_ct1, tile_y_qs, tile_y_ds);
  6897. }
  6898. template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_sycl_t vec_dot_q_sycl>
  6899. static void mul_mat_vec_q(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  6900. const sycl::nd_item<3> &item_ct1,
  6901. const uint32_t *iq3xxs_grid_ptr, const uint64_t *ksigns64_ptr) {
  6902. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6903. item_ct1.get_local_id(1);
  6904. if (row >= nrows) {
  6905. return;
  6906. }
  6907. const int blocks_per_row = ncols / qk;
  6908. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6909. // partial sum for each thread
  6910. float tmp = 0.0f;
  6911. const block_q_t * x = (const block_q_t *) vx;
  6912. const block_q8_1 * y = (const block_q8_1 *) vy;
  6913. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  6914. i += blocks_per_warp) {
  6915. const int ibx = row*blocks_per_row + i; // x block index
  6916. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  6917. const int iqs =
  6918. vdr *
  6919. (item_ct1.get_local_id(2) %
  6920. (qi / vdr)); // x block quant index when casting the quants to int
  6921. tmp += vec_dot_q_sycl(&x[ibx], &y[iby], iqs);
  6922. }
  6923. // sum up partial sums and write back result
  6924. #pragma unroll
  6925. for (int mask = 16; mask > 0; mask >>= 1) {
  6926. tmp +=
  6927. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6928. }
  6929. if (item_ct1.get_local_id(2) == 0) {
  6930. dst[row] = tmp;
  6931. }
  6932. }
  6933. template <int qk, int qi, typename block_q_t, int vdr>
  6934. static void mul_mat_vec_q_iq2_xxs_q8_1(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  6935. const sycl::nd_item<3> &item_ct1,
  6936. const uint64_t *iq2xxs_grid_ptr, const uint8_t *ksigns_iq2xs_ptr,
  6937. const uint8_t *kmask_iq2xs_ptr ) {
  6938. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6939. item_ct1.get_local_id(1);
  6940. if (row >= nrows) {
  6941. return;
  6942. }
  6943. const int blocks_per_row = ncols / qk;
  6944. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6945. // partial sum for each thread
  6946. float tmp = 0.0f;
  6947. const block_q_t * x = (const block_q_t *) vx;
  6948. const block_q8_1 * y = (const block_q8_1 *) vy;
  6949. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  6950. i += blocks_per_warp) {
  6951. const int ibx = row*blocks_per_row + i; // x block index
  6952. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  6953. const int iqs =
  6954. vdr *
  6955. (item_ct1.get_local_id(2) %
  6956. (qi / vdr)); // x block quant index when casting the quants to int
  6957. tmp += vec_dot_iq2_xxs_q8_1(&x[ibx], &y[iby], iqs, iq2xxs_grid_ptr, ksigns_iq2xs_ptr, kmask_iq2xs_ptr);
  6958. }
  6959. // sum up partial sums and write back result
  6960. #pragma unroll
  6961. for (int mask = 16; mask > 0; mask >>= 1) {
  6962. tmp +=
  6963. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6964. }
  6965. if (item_ct1.get_local_id(2) == 0) {
  6966. dst[row] = tmp;
  6967. }
  6968. }
  6969. template <int qk, int qi, typename block_q_t, int vdr>
  6970. static void mul_mat_vec_q_iq2_xs_q8_1(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  6971. const sycl::nd_item<3> &item_ct1,
  6972. const uint64_t *iq2xs_grid_ptr, const uint64_t *ksigns64_ptr ) {
  6973. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  6974. item_ct1.get_local_id(1);
  6975. if (row >= nrows) {
  6976. return;
  6977. }
  6978. const int blocks_per_row = ncols / qk;
  6979. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  6980. // partial sum for each thread
  6981. float tmp = 0.0f;
  6982. const block_q_t * x = (const block_q_t *) vx;
  6983. const block_q8_1 * y = (const block_q8_1 *) vy;
  6984. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  6985. i += blocks_per_warp) {
  6986. const int ibx = row*blocks_per_row + i; // x block index
  6987. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  6988. const int iqs =
  6989. vdr *
  6990. (item_ct1.get_local_id(2) %
  6991. (qi / vdr)); // x block quant index when casting the quants to int
  6992. tmp += vec_dot_iq2_xs_q8_1(&x[ibx], &y[iby], iqs, iq2xs_grid_ptr, ksigns64_ptr);
  6993. }
  6994. // sum up partial sums and write back result
  6995. #pragma unroll
  6996. for (int mask = 16; mask > 0; mask >>= 1) {
  6997. tmp +=
  6998. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  6999. }
  7000. if (item_ct1.get_local_id(2) == 0) {
  7001. dst[row] = tmp;
  7002. }
  7003. }
  7004. template <int qk, int qi, typename block_q_t, int vdr>
  7005. static void mul_mat_vec_q_iq3_xxs_q8_1(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  7006. const sycl::nd_item<3> &item_ct1,
  7007. const uint32_t *iq3xxs_grid_ptr, const uint64_t *ksigns64_ptr ) {
  7008. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7009. item_ct1.get_local_id(1);
  7010. if (row >= nrows) {
  7011. return;
  7012. }
  7013. const int blocks_per_row = ncols / qk;
  7014. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7015. // partial sum for each thread
  7016. float tmp = 0.0f;
  7017. const block_q_t * x = (const block_q_t *) vx;
  7018. const block_q8_1 * y = (const block_q8_1 *) vy;
  7019. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7020. i += blocks_per_warp) {
  7021. const int ibx = row*blocks_per_row + i; // x block index
  7022. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7023. const int iqs =
  7024. vdr *
  7025. (item_ct1.get_local_id(2) %
  7026. (qi / vdr)); // x block quant index when casting the quants to int
  7027. tmp += vec_dot_iq3_xxs_q8_1(&x[ibx], &y[iby], iqs, iq3xxs_grid_ptr, ksigns64_ptr);
  7028. }
  7029. // sum up partial sums and write back result
  7030. #pragma unroll
  7031. for (int mask = 16; mask > 0; mask >>= 1) {
  7032. tmp +=
  7033. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7034. }
  7035. if (item_ct1.get_local_id(2) == 0) {
  7036. dst[row] = tmp;
  7037. }
  7038. }
  7039. template <int qk, int qi, typename block_q_t, int vdr>
  7040. static void mul_mat_vec_q_iq3_s_q8_1(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  7041. const sycl::nd_item<3> &item_ct1,
  7042. const uint32_t *iq3s_grid_ptr, const uint64_t *ksigns64_ptr ) {
  7043. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7044. item_ct1.get_local_id(1);
  7045. if (row >= nrows) {
  7046. return;
  7047. }
  7048. const int blocks_per_row = ncols / qk;
  7049. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7050. // partial sum for each thread
  7051. float tmp = 0.0f;
  7052. const block_q_t * x = (const block_q_t *) vx;
  7053. const block_q8_1 * y = (const block_q8_1 *) vy;
  7054. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7055. i += blocks_per_warp) {
  7056. const int ibx = row*blocks_per_row + i; // x block index
  7057. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7058. const int iqs =
  7059. vdr *
  7060. (item_ct1.get_local_id(2) %
  7061. (qi / vdr)); // x block quant index when casting the quants to int
  7062. tmp += vec_dot_iq3_s_q8_1(&x[ibx], &y[iby], iqs, iq3s_grid_ptr, ksigns64_ptr);
  7063. }
  7064. // sum up partial sums and write back result
  7065. #pragma unroll
  7066. for (int mask = 16; mask > 0; mask >>= 1) {
  7067. tmp +=
  7068. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7069. }
  7070. if (item_ct1.get_local_id(2) == 0) {
  7071. dst[row] = tmp;
  7072. }
  7073. }
  7074. template <int qk, int qi, typename block_q_t, int vdr>
  7075. static void mul_mat_vec_q_iq1_s_q8_1(const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst, const int ncols, const int nrows,
  7076. const sycl::nd_item<3> &item_ct1,
  7077. const uint32_t *iq1s_grid_ptr, const uint64_t *ksigns64_ptr ) {
  7078. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7079. item_ct1.get_local_id(1);
  7080. if (row >= nrows) {
  7081. return;
  7082. }
  7083. const int blocks_per_row = ncols / qk;
  7084. const int blocks_per_warp = vdr * WARP_SIZE / qi;
  7085. // partial sum for each thread
  7086. float tmp = 0.0f;
  7087. const block_q_t * x = (const block_q_t *) vx;
  7088. const block_q8_1 * y = (const block_q8_1 *) vy;
  7089. for (int i = item_ct1.get_local_id(2) / (qi / vdr); i < blocks_per_row;
  7090. i += blocks_per_warp) {
  7091. const int ibx = row*blocks_per_row + i; // x block index
  7092. const int iby = i * (qk/QK8_1); // y block index that aligns with ibx
  7093. const int iqs =
  7094. vdr *
  7095. (item_ct1.get_local_id(2) %
  7096. (qi / vdr)); // x block quant index when casting the quants to int
  7097. tmp += vec_dot_iq1_s_q8_1(&x[ibx], &y[iby], iqs, iq1s_grid_ptr, ksigns64_ptr);
  7098. }
  7099. // sum up partial sums and write back result
  7100. #pragma unroll
  7101. for (int mask = 16; mask > 0; mask >>= 1) {
  7102. tmp +=
  7103. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7104. }
  7105. if (item_ct1.get_local_id(2) == 0) {
  7106. dst[row] = tmp;
  7107. }
  7108. }
  7109. template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
  7110. static void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows,
  7111. const sycl::nd_item<3> &item_ct1) {
  7112. // qk = quantized weights per x block
  7113. // qr = number of quantized weights per data value in x block
  7114. const int row = item_ct1.get_group(2) * item_ct1.get_local_range(1) +
  7115. item_ct1.get_local_id(1);
  7116. if (row >= nrows) {
  7117. return;
  7118. }
  7119. const int tid = item_ct1.get_local_id(2);
  7120. const int iter_stride = 2*GGML_SYCL_DMMV_X;
  7121. const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
  7122. const int y_offset = qr == 1 ? 1 : qk/2;
  7123. // partial sum for each thread
  7124. #ifdef GGML_SYCL_F16
  7125. sycl::half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
  7126. #else
  7127. float tmp = 0.0f;
  7128. #endif // GGML_SYCL_F16
  7129. for (int i = 0; i < ncols; i += iter_stride) {
  7130. const int col = i + vals_per_iter*tid;
  7131. const int ib = (row*ncols + col)/qk; // x block index
  7132. const int iqs = (col%qk)/qr; // x quant index
  7133. const int iybs = col - col%qk; // y block start index
  7134. // processing >2 values per i iter is faster for fast GPUs
  7135. #pragma unroll
  7136. for (int j = 0; j < vals_per_iter; j += 2) {
  7137. // process 2 vals per j iter
  7138. // dequantize
  7139. // for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
  7140. dfloat2 v;
  7141. dequantize_kernel(vx, ib, iqs + j/qr, v);
  7142. // matrix multiplication
  7143. // for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
  7144. #ifdef GGML_SYCL_F16
  7145. dfloat2 t1{y[iybs + iqs + j / qr + 0],
  7146. y[iybs + iqs + j / qr + y_offset]};
  7147. tmp += v * t1;
  7148. #else
  7149. tmp += v.x() * y[iybs + iqs + j / qr + 0];
  7150. tmp += v.y() * y[iybs + iqs + j / qr + y_offset];
  7151. #endif // GGML_SYCL_F16
  7152. }
  7153. }
  7154. // sum up partial sums and write back result
  7155. #pragma unroll
  7156. for (int mask = 16; mask > 0; mask >>= 1) {
  7157. tmp +=
  7158. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7159. }
  7160. if (tid == 0) {
  7161. #ifdef GGML_SYCL_F16
  7162. dst[row] = tmp.x() + tmp.y();
  7163. #else
  7164. dst[row] = tmp;
  7165. #endif // GGML_SYCL_F16
  7166. }
  7167. }
  7168. static void mul_mat_p021_f16_f32(
  7169. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst,
  7170. const int ncols_x, const int nrows_x, const int nchannels_x, const int nchannels_y,
  7171. const sycl::nd_item<3> &item_ct1) {
  7172. const sycl::half *x = (const sycl::half *)vx;
  7173. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7174. item_ct1.get_local_id(1);
  7175. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  7176. item_ct1.get_local_id(0);
  7177. const int channel_x = channel / (nchannels_y / nchannels_x);
  7178. const int nrows_y = ncols_x;
  7179. const int nrows_dst = nrows_x;
  7180. const int row_dst = row_x;
  7181. float tmp = 0.0f;
  7182. for (int col_x0 = 0; col_x0 < ncols_x;
  7183. col_x0 += item_ct1.get_local_range(2)) {
  7184. const int col_x = col_x0 + item_ct1.get_local_id(2);
  7185. if (col_x >= ncols_x) {
  7186. break;
  7187. }
  7188. // x is transposed and permuted
  7189. const int ix = row_x*nchannels_x*ncols_x + channel_x*ncols_x + col_x;
  7190. const float xi =
  7191. sycl::vec<sycl::half, 1>(x[ix])
  7192. .convert<float, sycl::rounding_mode::automatic>()[0];
  7193. const int row_y = col_x;
  7194. // y is not transposed but permuted
  7195. const int iy = channel*nrows_y + row_y;
  7196. tmp += xi * y[iy];
  7197. }
  7198. // dst is not transposed and not permuted
  7199. const int idst = channel*nrows_dst + row_dst;
  7200. // sum up partial sums and write back result
  7201. #pragma unroll
  7202. for (int mask = 16; mask > 0; mask >>= 1) {
  7203. tmp +=
  7204. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7205. }
  7206. if (item_ct1.get_local_id(2) == 0) {
  7207. dst[idst] = tmp;
  7208. }
  7209. }
  7210. static void mul_mat_vec_nc_f16_f32( // nc == non-contiguous
  7211. const void * __restrict__ vx, const float * __restrict__ y, float * __restrict__ dst, const int ncols_x, const int nrows_x,
  7212. const int row_stride_x, const int channel_stride_x, const int channel_x_divisor,
  7213. const sycl::nd_item<3> &item_ct1) {
  7214. const sycl::half *x = (const sycl::half *)vx;
  7215. const int row_x = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7216. item_ct1.get_local_id(1);
  7217. const int channel = item_ct1.get_local_range(0) * item_ct1.get_group(0) +
  7218. item_ct1.get_local_id(0);
  7219. const int channel_x = channel / channel_x_divisor;
  7220. const int nrows_y = ncols_x;
  7221. const int nrows_dst = nrows_x;
  7222. const int row_dst = row_x;
  7223. const int idst = channel*nrows_dst + row_dst;
  7224. float tmp = 0.0f;
  7225. for (int col_x0 = 0; col_x0 < ncols_x;
  7226. col_x0 += item_ct1.get_local_range(2)) {
  7227. const int col_x = col_x0 + item_ct1.get_local_id(2);
  7228. if (col_x >= ncols_x) {
  7229. break;
  7230. }
  7231. const int row_y = col_x;
  7232. const int ix = channel_x*channel_stride_x + row_x*row_stride_x + col_x;
  7233. const int iy = channel*nrows_y + row_y;
  7234. const float xi =
  7235. sycl::vec<sycl::half, 1>(x[ix])
  7236. .convert<float, sycl::rounding_mode::automatic>()[0];
  7237. tmp += xi * y[iy];
  7238. }
  7239. // sum up partial sums and write back result
  7240. #pragma unroll
  7241. for (int mask = 16; mask > 0; mask >>= 1) {
  7242. tmp +=
  7243. dpct::permute_sub_group_by_xor(item_ct1.get_sub_group(), tmp, mask);
  7244. }
  7245. if (item_ct1.get_local_id(2) == 0) {
  7246. dst[idst] = tmp;
  7247. }
  7248. }
  7249. static void cpy_1_f32_f32(const char * cxi, char * cdsti) {
  7250. const float * xi = (const float *) cxi;
  7251. float * dsti = (float *) cdsti;
  7252. *dsti = *xi;
  7253. }
  7254. static void cpy_1_f32_f16(const char * cxi, char * cdsti) {
  7255. const float * xi = (const float *) cxi;
  7256. sycl::half *dsti = (sycl::half *)cdsti;
  7257. *dsti = sycl::vec<float, 1>(*xi)
  7258. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7259. }
  7260. static void cpy_1_f16_f16(const char * cxi, char * cdsti) {
  7261. const sycl::half *xi = (const sycl::half *)cxi;
  7262. sycl::half *dsti = (sycl::half *)cdsti;
  7263. *dsti = *xi;
  7264. }
  7265. static void cpy_1_f16_f32(const char * cxi, char * cdsti) {
  7266. const sycl::half *xi = (const sycl::half *)cxi;
  7267. float * dsti = (float *) cdsti;
  7268. *dsti = *xi;
  7269. }
  7270. static void cpy_1_i16_i16(const char * cxi, char * cdsti) {
  7271. const int16_t *xi = (const int16_t *)cxi;
  7272. int16_t *dsti = (int16_t *)cdsti;
  7273. *dsti = *xi;
  7274. }
  7275. static void cpy_1_i32_i32(const char * cxi, char * cdsti) {
  7276. const int32_t *xi = (const int32_t *)cxi;
  7277. int32_t *dsti = (int32_t *)cdsti;
  7278. *dsti = *xi;
  7279. }
  7280. template <cpy_kernel_t cpy_1>
  7281. static void cpy_f32_f16(const char * cx, char * cdst, const int ne,
  7282. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7283. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  7284. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  7285. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7286. item_ct1.get_local_id(2);
  7287. if (i >= ne) {
  7288. return;
  7289. }
  7290. // determine indices i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
  7291. // then combine those indices with the corresponding byte offsets to get the total offsets
  7292. const int i03 = i/(ne00 * ne01 * ne02);
  7293. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  7294. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  7295. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  7296. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  7297. const int i13 = i/(ne10 * ne11 * ne12);
  7298. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  7299. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  7300. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  7301. const int dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
  7302. cpy_1(cx + x_offset, cdst + dst_offset);
  7303. }
  7304. static void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
  7305. const float * xi = (const float *) cxi;
  7306. block_q8_0 * dsti = (block_q8_0 *) cdsti;
  7307. float amax = 0.0f; // absolute max
  7308. for (int j = 0; j < QK8_0; j++) {
  7309. const float v = xi[j];
  7310. amax = sycl::fmax(amax, sycl::fabs((float)v));
  7311. }
  7312. const float d = amax / ((1 << 7) - 1);
  7313. const float id = d ? 1.0f/d : 0.0f;
  7314. dsti->d = d;
  7315. for (int j = 0; j < QK8_0; ++j) {
  7316. const float x0 = xi[j]*id;
  7317. dsti->qs[j] = sycl::round((float)x0);
  7318. }
  7319. }
  7320. static void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
  7321. const float * xi = (const float *) cxi;
  7322. block_q4_0 * dsti = (block_q4_0 *) cdsti;
  7323. float amax = 0.0f;
  7324. float vmax = 0.0f;
  7325. for (int j = 0; j < QK4_0; ++j) {
  7326. const float v = xi[j];
  7327. if (amax < sycl::fabs((float)v)) {
  7328. amax = sycl::fabs((float)v);
  7329. vmax = v;
  7330. }
  7331. }
  7332. const float d = vmax / -8;
  7333. const float id = d ? 1.0f/d : 0.0f;
  7334. dsti->d = d;
  7335. for (int j = 0; j < QK4_0/2; ++j) {
  7336. const float x0 = xi[0 + j]*id;
  7337. const float x1 = xi[QK4_0/2 + j]*id;
  7338. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 8.5f));
  7339. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 8.5f));
  7340. dsti->qs[j] = xi0;
  7341. dsti->qs[j] |= xi1 << 4;
  7342. }
  7343. }
  7344. static void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
  7345. const float * xi = (const float *) cxi;
  7346. block_q4_1 * dsti = (block_q4_1 *) cdsti;
  7347. float vmin = FLT_MAX;
  7348. float vmax = -FLT_MAX;
  7349. for (int j = 0; j < QK4_1; ++j) {
  7350. const float v = xi[j];
  7351. if (v < vmin) vmin = v;
  7352. if (v > vmax) vmax = v;
  7353. }
  7354. const float d = (vmax - vmin) / ((1 << 4) - 1);
  7355. const float id = d ? 1.0f/d : 0.0f;
  7356. dsti->dm.x() = d;
  7357. dsti->dm.y() = vmin;
  7358. for (int j = 0; j < QK4_1/2; ++j) {
  7359. const float x0 = (xi[0 + j] - vmin)*id;
  7360. const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
  7361. const uint8_t xi0 = dpct::min(15, (int8_t)(x0 + 0.5f));
  7362. const uint8_t xi1 = dpct::min(15, (int8_t)(x1 + 0.5f));
  7363. dsti->qs[j] = xi0;
  7364. dsti->qs[j] |= xi1 << 4;
  7365. }
  7366. }
  7367. template <cpy_kernel_t cpy_blck, int qk>
  7368. static void cpy_f32_q(const char * cx, char * cdst, const int ne,
  7369. const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
  7370. const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
  7371. const int nb12, const int nb13, const sycl::nd_item<3> &item_ct1) {
  7372. const int i = (item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7373. item_ct1.get_local_id(2)) *
  7374. qk;
  7375. if (i >= ne) {
  7376. return;
  7377. }
  7378. const int i03 = i/(ne00 * ne01 * ne02);
  7379. const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  7380. const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  7381. const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  7382. const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  7383. const int i13 = i/(ne10 * ne11 * ne12);
  7384. const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  7385. const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  7386. const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  7387. const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  7388. cpy_blck(cx + x_offset, cdst + dst_offset);
  7389. }
  7390. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  7391. const float y = (i0 / 2 - low) / sycl::max(0.001f, high - low);
  7392. return 1.0f - sycl::min(1.0f, sycl::max(0.0f, y));
  7393. }
  7394. struct rope_corr_dims {
  7395. float v[4];
  7396. };
  7397. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  7398. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  7399. static void rope_yarn(
  7400. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  7401. float * cos_theta, float * sin_theta
  7402. ) {
  7403. // Get n-d rotational scaling corrected for extrapolation
  7404. float theta_interp = freq_scale * theta_extrap;
  7405. float theta = theta_interp;
  7406. if (ext_factor != 0.0f) {
  7407. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  7408. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  7409. // Get n-d magnitude scaling corrected for interpolation
  7410. mscale *= 1.0f + 0.1f * sycl::log(1.0f / freq_scale);
  7411. }
  7412. *cos_theta = sycl::cos(theta) * mscale;
  7413. *sin_theta = sycl::sin(theta) * mscale;
  7414. }
  7415. // rope == RoPE == rotary positional embedding
  7416. template<typename T, bool has_pos>
  7417. static void rope(
  7418. const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  7419. float ext_factor, float attn_factor, rope_corr_dims corr_dims
  7420. ,
  7421. const sycl::nd_item<3> &item_ct1) {
  7422. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7423. item_ct1.get_local_id(1));
  7424. if (col >= ncols) {
  7425. return;
  7426. }
  7427. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7428. item_ct1.get_local_id(2);
  7429. const int i = row*ncols + col;
  7430. const int i2 = row/p_delta_rows;
  7431. const int p = has_pos ? pos[i2] : 0;
  7432. const float theta_base = p * dpct::pow(freq_base, -float(col) / ncols);
  7433. float cos_theta, sin_theta;
  7434. rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
  7435. const float x0 = x[i + 0];
  7436. const float x1 = x[i + 1];
  7437. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7438. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  7439. }
  7440. template<typename T, bool has_pos>
  7441. static void rope_neox(
  7442. const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  7443. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
  7444. ,
  7445. const sycl::nd_item<3> &item_ct1) {
  7446. const int col = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7447. item_ct1.get_local_id(1));
  7448. if (col >= ncols) {
  7449. return;
  7450. }
  7451. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7452. item_ct1.get_local_id(2);
  7453. const int ib = col / n_dims;
  7454. const int ic = col % n_dims;
  7455. if (ib > 0) {
  7456. const int i = row*ncols + ib*n_dims + ic;
  7457. dst[i + 0] = x[i + 0];
  7458. dst[i + 1] = x[i + 1];
  7459. return;
  7460. }
  7461. const int i = row*ncols + ib*n_dims + ic/2;
  7462. const int i2 = row/p_delta_rows;
  7463. float cur_rot = inv_ndims * ic - ib;
  7464. const int p = has_pos ? pos[i2] : 0;
  7465. const float theta_base =
  7466. p * freq_scale * dpct::pow(theta_scale, col / 2.0f);
  7467. float cos_theta, sin_theta;
  7468. rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
  7469. const float x0 = x[i + 0];
  7470. const float x1 = x[i + n_dims/2];
  7471. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7472. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  7473. }
  7474. static void rope_glm_f32(
  7475. const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
  7476. int n_ctx
  7477. , const sycl::nd_item<3> &item_ct1) {
  7478. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7479. item_ct1.get_local_id(2);
  7480. const int half_n_dims = ncols/4;
  7481. if (col >= half_n_dims) {
  7482. return;
  7483. }
  7484. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7485. item_ct1.get_local_id(1);
  7486. const int i = row*ncols + col;
  7487. const int i2 = row/p_delta_rows;
  7488. const float col_theta_scale = dpct::pow(freq_base, -2.0f * col / ncols);
  7489. // FIXME: this is likely wrong
  7490. const int p = pos != nullptr ? pos[i2] : 0;
  7491. const float theta = sycl::min(p, n_ctx - 2) * freq_scale * col_theta_scale;
  7492. const float sin_theta = sycl::sin((float)theta);
  7493. const float cos_theta = sycl::cos((float)theta);
  7494. const float x0 = x[i + 0];
  7495. const float x1 = x[i + half_n_dims];
  7496. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  7497. dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
  7498. const float block_theta =
  7499. ((float)sycl::max(p - n_ctx - 2, 0)) * col_theta_scale;
  7500. const float sin_block_theta = sycl::sin((float)block_theta);
  7501. const float cos_block_theta = sycl::cos((float)block_theta);
  7502. const float x2 = x[i + half_n_dims * 2];
  7503. const float x3 = x[i + half_n_dims * 3];
  7504. dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
  7505. dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
  7506. }
  7507. static void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
  7508. const int n_heads_log2_floor, const float m0, const float m1,
  7509. const sycl::nd_item<3> &item_ct1) {
  7510. const int col = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7511. item_ct1.get_local_id(2);
  7512. if (col >= ncols) {
  7513. return;
  7514. }
  7515. const int row = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7516. item_ct1.get_local_id(1);
  7517. const int i = row*ncols + col;
  7518. const int k = row/k_rows;
  7519. float m_k;
  7520. if (k < n_heads_log2_floor) {
  7521. m_k = dpct::pow(m0, k + 1);
  7522. } else {
  7523. m_k = dpct::pow(m1, 2 * (k - n_heads_log2_floor) + 1);
  7524. }
  7525. dst[i] = col * m_k + x[i];
  7526. }
  7527. static void k_sum_rows_f32(const float * x, float * dst, const int ncols,
  7528. const sycl::nd_item<3> &item_ct1) {
  7529. const int row = item_ct1.get_group(1);
  7530. const int col = item_ct1.get_local_id(2);
  7531. float sum = 0.0f;
  7532. for (int i = col; i < ncols; i += item_ct1.get_local_range(2)) {
  7533. sum += x[row * ncols + i];
  7534. }
  7535. sum = warp_reduce_sum(sum, item_ct1);
  7536. if (col == 0) {
  7537. dst[row] = sum;
  7538. }
  7539. }
  7540. template<typename T>
  7541. static inline void swap(T & a, T & b) {
  7542. T tmp = a;
  7543. a = b;
  7544. b = tmp;
  7545. }
  7546. template<ggml_sort_order order>
  7547. static void k_argsort_f32_i32(const float * x, int * dst, const int ncols,
  7548. const sycl::nd_item<3> &item_ct1) {
  7549. // bitonic sort
  7550. int col = item_ct1.get_local_id(2);
  7551. int row = item_ct1.get_group(1);
  7552. if (col >= ncols) return;
  7553. const float * x_row = x + row * ncols;
  7554. int * dst_row = dst + row * ncols;
  7555. // initialize indices
  7556. if (col < ncols) {
  7557. dst_row[col] = col;
  7558. }
  7559. /*
  7560. DPCT1065:58: Consider replacing sycl::nd_item::barrier() with
  7561. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better
  7562. performance if there is no access to global memory.
  7563. */
  7564. item_ct1.barrier();
  7565. for (int k = 2; k <= ncols; k *= 2) {
  7566. for (int j = k / 2; j > 0; j /= 2) {
  7567. int ixj = col ^ j;
  7568. if (ixj > col) {
  7569. if ((col & k) == 0) {
  7570. 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]]) {
  7571. swap(dst_row[col], dst_row[ixj]);
  7572. }
  7573. } else {
  7574. 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]]) {
  7575. swap(dst_row[col], dst_row[ixj]);
  7576. }
  7577. }
  7578. }
  7579. /*
  7580. DPCT1118:11: SYCL group functions and algorithms must be encountered
  7581. in converged control flow. You may need to adjust the code.
  7582. */
  7583. /*
  7584. DPCT1065:59: Consider replacing sycl::nd_item::barrier() with
  7585. sycl::nd_item::barrier(sycl::access::fence_space::local_space) for
  7586. better performance if there is no access to global memory.
  7587. */
  7588. item_ct1.barrier();
  7589. }
  7590. }
  7591. }
  7592. static void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past,
  7593. const sycl::nd_item<3> &item_ct1) {
  7594. const int col = item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  7595. item_ct1.get_local_id(1);
  7596. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7597. item_ct1.get_local_id(2);
  7598. if (col >= ncols) {
  7599. return;
  7600. }
  7601. const int i = row*ncols + col;
  7602. //dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
  7603. //dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
  7604. dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
  7605. }
  7606. template <bool vals_smem, int ncols_template, int block_size_template>
  7607. static void soft_max_f32(const float * x, const float * mask, const float *pos, float * dst, const int ncols_par,
  7608. const int nrows_y, const float scale, const float max_bias, const float m0,
  7609. const float m1, uint32_t n_head_log2, const sycl::nd_item<3> &item_ct1, float *buf) {
  7610. const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
  7611. const int tid = item_ct1.get_local_id(2);
  7612. const int rowx = item_ct1.get_group(2);
  7613. const int rowy = rowx % nrows_y; // broadcast the mask (y) in the row dimension
  7614. const int block_size = block_size_template == 0 ? item_ct1.get_local_range(2) : block_size_template;
  7615. const int warp_id = item_ct1.get_local_id(2) / WARP_SIZE;
  7616. const int lane_id = item_ct1.get_local_id(2) % WARP_SIZE;
  7617. float slope = 0.0f;
  7618. // ALiBi
  7619. if (max_bias > 0.0f) {
  7620. const uint32_t h = rowx/nrows_y; // head index
  7621. const float base = h < n_head_log2 ? m0 : m1;
  7622. const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  7623. slope = sycl::pow(base, float(exp));
  7624. }
  7625. float * vals = vals_smem ? buf + WARP_SIZE : dst + rowx*ncols;
  7626. float max_val = -INFINITY;
  7627. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7628. const int col = col0 + tid;
  7629. if (ncols_template == 0 && col >= ncols) {
  7630. break;
  7631. }
  7632. const int ix = rowx*ncols + col;
  7633. const int iy = rowy*ncols + col;
  7634. const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
  7635. vals[col] = val;
  7636. max_val = sycl::max(max_val, val);
  7637. }
  7638. // find the max value in the block
  7639. max_val = warp_reduce_max(max_val, item_ct1);
  7640. if (block_size > WARP_SIZE) {
  7641. if (warp_id == 0) {
  7642. buf[lane_id] = -INFINITY;
  7643. }
  7644. item_ct1.barrier(sycl::access::fence_space::local_space);
  7645. if (lane_id == 0) {
  7646. buf[warp_id] = max_val;
  7647. }
  7648. item_ct1.barrier(sycl::access::fence_space::local_space);
  7649. max_val = buf[lane_id];
  7650. max_val = warp_reduce_max(max_val, item_ct1);
  7651. }
  7652. float tmp = 0.f;
  7653. #pragma unroll
  7654. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7655. const int col = col0 + tid;
  7656. if (ncols_template == 0 && col >= ncols) {
  7657. break;
  7658. }
  7659. const float val = sycl::native::exp(vals[col] - max_val);
  7660. tmp += val;
  7661. vals[col] = val;
  7662. }
  7663. // find the sum of exps in the block
  7664. tmp = warp_reduce_sum(tmp, item_ct1);
  7665. if (block_size > WARP_SIZE) {
  7666. if (warp_id == 0) {
  7667. buf[lane_id] = 0.f;
  7668. }
  7669. item_ct1.barrier(sycl::access::fence_space::local_space);
  7670. if (lane_id == 0) {
  7671. buf[warp_id] = tmp;
  7672. }
  7673. item_ct1.barrier(sycl::access::fence_space::local_space);
  7674. tmp = buf[lane_id];
  7675. tmp = warp_reduce_sum(tmp, item_ct1);
  7676. }
  7677. const float inv_sum = 1.f / tmp;
  7678. #pragma unroll
  7679. for (int col0 = 0; col0 < ncols; col0 += block_size) {
  7680. const int col = col0 + tid;
  7681. if (ncols_template == 0 && col >= ncols) {
  7682. return;
  7683. }
  7684. const int idst = rowx*ncols + col;
  7685. dst[idst] = vals[col] * inv_sum;
  7686. }
  7687. }
  7688. static void scale_f32(const float * x, float * dst, const float scale, const int k,
  7689. const sycl::nd_item<3> &item_ct1) {
  7690. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7691. item_ct1.get_local_id(2);
  7692. if (i >= k) {
  7693. return;
  7694. }
  7695. dst[i] = scale * x[i];
  7696. }
  7697. static void clamp_f32(const float * x, float * dst, const float min, const float max, const int k,
  7698. const sycl::nd_item<3> &item_ct1) {
  7699. const int i = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  7700. item_ct1.get_local_id(2);
  7701. if (i >= k) {
  7702. return;
  7703. }
  7704. dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
  7705. }
  7706. template <typename T>
  7707. static void im2col_kernel(const float *x, T *dst, int offset_delta,
  7708. int IW, int IH, int OW, int KW, int KH,
  7709. int pelements, int CHW, int s0, int s1, int p0,
  7710. int p1, int d0, int d1,
  7711. const sycl::nd_item<3> &item_ct1) {
  7712. const int i = item_ct1.get_local_id(2) +
  7713. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  7714. if (i >= pelements) {
  7715. return;
  7716. }
  7717. const int ksize = OW * (KH > 1 ? KW : 1);
  7718. const int kx = i / ksize;
  7719. const int kd = kx * ksize;
  7720. const int ky = (i - kd) / OW;
  7721. const int ix = i % OW;
  7722. const int64_t iiw = ix * s0 + kx * d0 - p0;
  7723. const int64_t iih = item_ct1.get_group(1) * s1 + ky * d1 - p1;
  7724. const int64_t offset_dst =
  7725. (item_ct1.get_group(1) * OW + ix) * CHW +
  7726. (item_ct1.get_group(0) * (KW * KH) + ky * KW + kx);
  7727. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  7728. dst[offset_dst] =
  7729. sycl::vec<float, 1>(0.0f)
  7730. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7731. } else {
  7732. const int64_t offset_src = item_ct1.get_group(0) * offset_delta;
  7733. dst[offset_dst] =
  7734. sycl::vec<float, 1>(x[offset_src + iih * IW + iiw])
  7735. .convert<sycl::half, sycl::rounding_mode::automatic>()[0];
  7736. }
  7737. }
  7738. template <typename Ti, typename To>
  7739. static void pool2d_nchw_kernel(
  7740. const int ih, const int iw, const int oh, const int ow,
  7741. const int kh, const int kw, const int sh, const int sw,
  7742. const int ph, const int pw, const int parallel_elements,
  7743. const Ti* src, To* dst, const enum ggml_op_pool op,
  7744. const sycl::nd_item<3> &item_ct1) {
  7745. int idx = item_ct1.get_local_id(2) +
  7746. item_ct1.get_group(2) * item_ct1.get_local_range(2);
  7747. if (idx >= parallel_elements) {
  7748. return;
  7749. }
  7750. const int I_HW = ih * iw;
  7751. const int O_HW = oh * ow;
  7752. const int nc = idx / O_HW;
  7753. const int cur_oh = idx % O_HW / ow;
  7754. const int cur_ow = idx % O_HW % ow;
  7755. const Ti* i_ptr = src + nc * I_HW;
  7756. To* o_ptr = dst + nc * O_HW;
  7757. const int start_h = cur_oh * sh - ph;
  7758. const int bh = sycl::max(0, start_h);
  7759. const int eh = sycl::min(ih, start_h + kh);
  7760. const int start_w = cur_ow * sw - pw;
  7761. const int bw = sycl::max(0, start_w);
  7762. const int ew = sycl::min(iw, start_w + kw);
  7763. To res = 0;
  7764. switch (op) {
  7765. case GGML_OP_POOL_AVG: res = 0; break;
  7766. case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
  7767. }
  7768. for (int i = bh; i < eh; i += 1) {
  7769. for (int j = bw; j < ew; j += 1) {
  7770. #if DPCT_COMPATIBILITY_TEMP >= 350
  7771. /*
  7772. DPCT1098:106: The '*' expression is used instead of the __ldg
  7773. call. These two expressions do not provide the exact same
  7774. functionality. Check the generated code for potential precision
  7775. and/or performance issues.
  7776. */
  7777. Ti cur = *(i_ptr + i * iw + j);
  7778. #else
  7779. Ti cur = i_ptr[i * iw + j];
  7780. #endif
  7781. switch (op) {
  7782. case GGML_OP_POOL_AVG: res += (cur / (kh * kw)); break;
  7783. case GGML_OP_POOL_MAX: res = sycl::max(res, (To)cur); break;
  7784. }
  7785. }
  7786. }
  7787. o_ptr[cur_oh * ow + cur_ow] = res;
  7788. }
  7789. template <int qk, int qr, dequantize_kernel_t dq>
  7790. static void get_rows_sycl(const ggml_tensor *src0, const ggml_tensor *src1,
  7791. ggml_tensor *dst, const void *src0_dd,
  7792. const int32_t *src1_dd, float *dst_dd,
  7793. dpct::queue_ptr stream) {
  7794. GGML_TENSOR_BINARY_OP_LOCALS
  7795. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  7796. const int block_num_x = (ne00 + 2*SYCL_GET_ROWS_BLOCK_SIZE - 1) / (2*SYCL_GET_ROWS_BLOCK_SIZE);
  7797. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  7798. // strides in elements
  7799. //const size_t s0 = nb0 / ggml_element_size(dst);
  7800. const size_t s1 = nb1 / ggml_element_size(dst);
  7801. const size_t s2 = nb2 / ggml_element_size(dst);
  7802. const size_t s3 = nb3 / ggml_element_size(dst);
  7803. const size_t s10 = nb10 / ggml_element_size(src1);
  7804. const size_t s11 = nb11 / ggml_element_size(src1);
  7805. const size_t s12 = nb12 / ggml_element_size(src1);
  7806. //const size_t s13 = nb13 / ggml_element_size(src1);
  7807. GGML_ASSERT(ne00 % 2 == 0);
  7808. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7809. [=](sycl::nd_item<3> item_ct1) {
  7810. k_get_rows<qk, qr, dq>(
  7811. src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  7812. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  7813. });
  7814. (void) dst;
  7815. }
  7816. template <typename src0_t>
  7817. static void get_rows_sycl_float(const ggml_tensor *src0,
  7818. const ggml_tensor *src1, ggml_tensor *dst,
  7819. const src0_t *src0_dd, const int32_t *src1_dd,
  7820. float *dst_dd, dpct::queue_ptr stream) {
  7821. GGML_TENSOR_BINARY_OP_LOCALS
  7822. const sycl::range<3> block_dims(1, 1, SYCL_GET_ROWS_BLOCK_SIZE);
  7823. const int block_num_x = (ne00 + SYCL_GET_ROWS_BLOCK_SIZE - 1) / SYCL_GET_ROWS_BLOCK_SIZE;
  7824. const sycl::range<3> block_nums(ne11 * ne12, ne10, block_num_x);
  7825. // strides in elements
  7826. //const size_t s0 = nb0 / ggml_element_size(dst);
  7827. const size_t s1 = nb1 / ggml_element_size(dst);
  7828. const size_t s2 = nb2 / ggml_element_size(dst);
  7829. const size_t s3 = nb3 / ggml_element_size(dst);
  7830. const size_t s10 = nb10 / ggml_element_size(src1);
  7831. const size_t s11 = nb11 / ggml_element_size(src1);
  7832. const size_t s12 = nb12 / ggml_element_size(src1);
  7833. //const size_t s13 = nb13 / ggml_element_size(src1);
  7834. {
  7835. dpct::has_capability_or_fail(stream->get_device(),
  7836. {sycl::aspect::fp16});
  7837. stream->parallel_for(
  7838. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7839. [=](sycl::nd_item<3> item_ct1) {
  7840. k_get_rows_float(src0_dd, src1_dd, dst_dd, ne00, ne12, s1, s2,
  7841. s3, nb01, nb02, nb03, s10, s11, s12, item_ct1);
  7842. });
  7843. }
  7844. (void) dst;
  7845. }
  7846. template<float (*bin_op)(const float, const float)>
  7847. struct bin_bcast_sycl {
  7848. template <typename src0_t, typename src1_t, typename dst_t>
  7849. void operator()(const struct ggml_tensor *src0,
  7850. const struct ggml_tensor *src1, struct ggml_tensor *dst,
  7851. const src0_t *src0_dd, const src1_t *src1_dd, dst_t *dst_dd,
  7852. dpct::queue_ptr stream) {
  7853. GGML_TENSOR_BINARY_OP_LOCALS
  7854. int nr0 = ne10/ne0;
  7855. int nr1 = ne11/ne1;
  7856. int nr2 = ne12/ne2;
  7857. int nr3 = ne13/ne3;
  7858. int nr[4] = { nr0, nr1, nr2, nr3 };
  7859. // collapse dimensions until first broadcast dimension
  7860. int64_t cne0[] = {ne0, ne1, ne2, ne3};
  7861. int64_t cne1[] = {ne10, ne11, ne12, ne13};
  7862. size_t cnb0[] = {nb0, nb1, nb2, nb3};
  7863. size_t cnb1[] = {nb10, nb11, nb12, nb13};
  7864. auto collapse = [](int64_t cne[]) {
  7865. cne[0] *= cne[1];
  7866. cne[1] = cne[2];
  7867. cne[2] = cne[3];
  7868. cne[3] = 1;
  7869. };
  7870. auto collapse_nb = [](size_t cnb[], int64_t cne[]) {
  7871. cnb[1] *= cne[1];
  7872. cnb[2] *= cne[2];
  7873. cnb[3] *= cne[3];
  7874. };
  7875. for (int i = 0; i < 4; i++) {
  7876. if (nr[i] != 1) {
  7877. break;
  7878. }
  7879. if (i > 0) {
  7880. collapse_nb(cnb0, cne0);
  7881. collapse_nb(cnb1, cne1);
  7882. collapse(cne0);
  7883. collapse(cne1);
  7884. }
  7885. }
  7886. {
  7887. int64_t ne0 = cne0[0];
  7888. int64_t ne1 = cne0[1];
  7889. int64_t ne2 = cne0[2];
  7890. int64_t ne3 = cne0[3];
  7891. int64_t ne10 = cne1[0];
  7892. int64_t ne11 = cne1[1];
  7893. int64_t ne12 = cne1[2];
  7894. int64_t ne13 = cne1[3];
  7895. size_t nb0 = cnb0[0];
  7896. size_t nb1 = cnb0[1];
  7897. size_t nb2 = cnb0[2];
  7898. size_t nb3 = cnb0[3];
  7899. size_t nb10 = cnb1[0];
  7900. size_t nb11 = cnb1[1];
  7901. size_t nb12 = cnb1[2];
  7902. size_t nb13 = cnb1[3];
  7903. size_t s0 = nb0 / sizeof(dst_t);
  7904. size_t s1 = nb1 / sizeof(dst_t);
  7905. size_t s2 = nb2 / sizeof(dst_t);
  7906. size_t s3 = nb3 / sizeof(dst_t);
  7907. size_t s10 = nb10 / sizeof(src1_t);
  7908. size_t s11 = nb11 / sizeof(src1_t);
  7909. size_t s12 = nb12 / sizeof(src1_t);
  7910. size_t s13 = nb13 / sizeof(src1_t);
  7911. GGML_ASSERT(s0 == 1);
  7912. GGML_ASSERT(s10 == 1);
  7913. const int block_size = 128;
  7914. int64_t hne0 = std::max(ne0/2LL, 1LL);
  7915. sycl::range<3> block_dims(1, 1, 1);
  7916. block_dims[2] = std::min<unsigned int>(hne0, block_size);
  7917. block_dims[1] = std::min<unsigned int>(
  7918. ne1, block_size / (unsigned int)block_dims[2]);
  7919. block_dims[0] = std::min(
  7920. std::min<unsigned int>(
  7921. ne2 * ne3, block_size / (unsigned int)block_dims[2] /
  7922. (unsigned int)block_dims[1]),
  7923. 64U);
  7924. sycl::range<3> block_nums(
  7925. (ne2 * ne3 + block_dims[0] - 1) / block_dims[0],
  7926. (ne1 + block_dims[1] - 1) / block_dims[1],
  7927. (hne0 + block_dims[2] - 1) / block_dims[2]);
  7928. if (block_nums[0] > 65535) {
  7929. // this is the maximum number of blocks in z direction, fallback to 1D grid kernel
  7930. int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
  7931. {
  7932. dpct::has_capability_or_fail(stream->get_device(),
  7933. {sycl::aspect::fp16});
  7934. stream->parallel_for(
  7935. sycl::nd_range<3>(sycl::range<3>(1, 1, block_num) *
  7936. sycl::range<3>(1, 1, block_size),
  7937. sycl::range<3>(1, 1, block_size)),
  7938. [=](sycl::nd_item<3> item_ct1) {
  7939. k_bin_bcast_unravel<bin_op>(
  7940. src0_dd, src1_dd, dst_dd, ne0, ne1, ne2, ne3,
  7941. ne10, ne11, ne12, ne13, s1, s2, s3, s11, s12,
  7942. s13, item_ct1);
  7943. });
  7944. }
  7945. } else {
  7946. /*
  7947. DPCT1049:16: The work-group size passed to the SYCL kernel may
  7948. exceed the limit. To get the device limit, query
  7949. info::device::max_work_group_size. Adjust the work-group size if
  7950. needed.
  7951. */
  7952. dpct::has_capability_or_fail(stream->get_device(),
  7953. {sycl::aspect::fp16});
  7954. stream->parallel_for(
  7955. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  7956. [=](sycl::nd_item<3> item_ct1) {
  7957. k_bin_bcast<bin_op>(src0_dd, src1_dd, dst_dd, ne0, ne1,
  7958. ne2, ne3, ne10, ne11, ne12, ne13,
  7959. s1, s2, s3, s11, s12, s13,
  7960. item_ct1);
  7961. });
  7962. }
  7963. }
  7964. }
  7965. };
  7966. static void acc_f32_sycl(const float *x, const float *y, float *dst,
  7967. const int n_elements, const int ne10, const int ne11,
  7968. const int ne12, const int nb1, const int nb2,
  7969. const int offset, dpct::queue_ptr stream) {
  7970. int num_blocks = (n_elements + SYCL_ACC_BLOCK_SIZE - 1) / SYCL_ACC_BLOCK_SIZE;
  7971. stream->parallel_for(
  7972. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7973. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE),
  7974. sycl::range<3>(1, 1, SYCL_ACC_BLOCK_SIZE)),
  7975. [=](sycl::nd_item<3> item_ct1) {
  7976. acc_f32(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset,
  7977. item_ct1);
  7978. });
  7979. }
  7980. static void gelu_f32_sycl(const float *x, float *dst, const int k,
  7981. dpct::queue_ptr stream) {
  7982. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  7983. stream->parallel_for(
  7984. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7985. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  7986. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  7987. [=](sycl::nd_item<3> item_ct1) {
  7988. gelu_f32(x, dst, k, item_ct1);
  7989. });
  7990. }
  7991. static void silu_f32_sycl(const float *x, float *dst, const int k,
  7992. dpct::queue_ptr stream) {
  7993. const int num_blocks = (k + SYCL_SILU_BLOCK_SIZE - 1) / SYCL_SILU_BLOCK_SIZE;
  7994. stream->parallel_for(
  7995. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  7996. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE),
  7997. sycl::range<3>(1, 1, SYCL_SILU_BLOCK_SIZE)),
  7998. [=](sycl::nd_item<3> item_ct1) {
  7999. silu_f32(x, dst, k, item_ct1);
  8000. });
  8001. }
  8002. static void gelu_quick_f32_sycl(const float *x, float *dst, const int k,
  8003. dpct::queue_ptr stream) {
  8004. const int num_blocks = (k + SYCL_GELU_BLOCK_SIZE - 1) / SYCL_GELU_BLOCK_SIZE;
  8005. stream->parallel_for(
  8006. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8007. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE),
  8008. sycl::range<3>(1, 1, SYCL_GELU_BLOCK_SIZE)),
  8009. [=](sycl::nd_item<3> item_ct1) {
  8010. gelu_quick_f32(x, dst, k, item_ct1);
  8011. });
  8012. }
  8013. static void tanh_f32_sycl(const float *x, float *dst, const int k,
  8014. dpct::queue_ptr stream) {
  8015. const int num_blocks = (k + SYCL_TANH_BLOCK_SIZE - 1) / SYCL_TANH_BLOCK_SIZE;
  8016. stream->parallel_for(
  8017. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8018. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE),
  8019. sycl::range<3>(1, 1, SYCL_TANH_BLOCK_SIZE)),
  8020. [=](sycl::nd_item<3> item_ct1) {
  8021. tanh_f32(x, dst, k, item_ct1);
  8022. });
  8023. }
  8024. static void relu_f32_sycl(const float *x, float *dst, const int k,
  8025. dpct::queue_ptr stream) {
  8026. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  8027. stream->parallel_for(
  8028. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8029. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  8030. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  8031. [=](sycl::nd_item<3> item_ct1) {
  8032. relu_f32(x, dst, k, item_ct1);
  8033. });
  8034. }
  8035. static void hardsigmoid_f32_sycl(const float *x, float *dst, const int k,
  8036. dpct::queue_ptr stream) {
  8037. const int num_blocks = (k + SYCL_HARDSIGMOID_BLOCK_SIZE - 1) / SYCL_HARDSIGMOID_BLOCK_SIZE;
  8038. stream->parallel_for(
  8039. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8040. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE),
  8041. sycl::range<3>(1, 1, SYCL_HARDSIGMOID_BLOCK_SIZE)),
  8042. [=](sycl::nd_item<3> item_ct1) {
  8043. hardsigmoid_f32(x, dst, k, item_ct1);
  8044. });
  8045. }
  8046. static void hardswish_f32_sycl(const float *x, float *dst, const int k,
  8047. dpct::queue_ptr stream) {
  8048. const int num_blocks = (k + SYCL_HARDSWISH_BLOCK_SIZE - 1) / SYCL_HARDSWISH_BLOCK_SIZE;
  8049. stream->parallel_for(
  8050. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8051. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE),
  8052. sycl::range<3>(1, 1, SYCL_HARDSWISH_BLOCK_SIZE)),
  8053. [=](sycl::nd_item<3> item_ct1) {
  8054. hardswish_f32(x, dst, k, item_ct1);
  8055. });
  8056. }
  8057. static void leaky_relu_f32_sycl(const float *x, float *dst, const int k,
  8058. const float negative_slope,
  8059. dpct::queue_ptr stream) {
  8060. const int num_blocks = (k + SYCL_RELU_BLOCK_SIZE - 1) / SYCL_RELU_BLOCK_SIZE;
  8061. stream->parallel_for(
  8062. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8063. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE),
  8064. sycl::range<3>(1, 1, SYCL_RELU_BLOCK_SIZE)),
  8065. [=](sycl::nd_item<3> item_ct1) {
  8066. leaky_relu_f32(x, dst, k, negative_slope, item_ct1);
  8067. });
  8068. }
  8069. static void sqr_f32_sycl(const float *x, float *dst, const int k,
  8070. dpct::queue_ptr stream) {
  8071. const int num_blocks = (k + SYCL_SQR_BLOCK_SIZE - 1) / SYCL_SQR_BLOCK_SIZE;
  8072. stream->parallel_for(
  8073. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  8074. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE),
  8075. sycl::range<3>(1, 1, SYCL_SQR_BLOCK_SIZE)),
  8076. [=](sycl::nd_item<3> item_ct1) {
  8077. sqr_f32(x, dst, k, item_ct1);
  8078. });
  8079. }
  8080. static void norm_f32_sycl(const float *x, float *dst, const int ncols,
  8081. const int nrows, const float eps,
  8082. dpct::queue_ptr stream) {
  8083. GGML_ASSERT(ncols % WARP_SIZE == 0);
  8084. if (ncols < 1024) {
  8085. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8086. stream->submit([&](sycl::handler &cgh) {
  8087. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  8088. sycl::range<1>(32), cgh);
  8089. cgh.parallel_for(
  8090. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8091. block_dims),
  8092. [=](sycl::nd_item<3> item_ct1)
  8093. [[intel::reqd_sub_group_size(32)]] {
  8094. norm_f32(x, dst, ncols, eps, item_ct1,
  8095. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8096. });
  8097. });
  8098. } else {
  8099. const int work_group_size = g_work_group_size;
  8100. const sycl::range<3> block_dims(1, 1, work_group_size);
  8101. /*
  8102. DPCT1049:17: The work-group size passed to the SYCL kernel may exceed
  8103. the limit. To get the device limit, query
  8104. info::device::max_work_group_size. Adjust the work-group size if needed.
  8105. */
  8106. stream->submit([&](sycl::handler &cgh) {
  8107. sycl::local_accessor<sycl::float2, 1> s_sum_acc_ct1(
  8108. sycl::range<1>(32), cgh);
  8109. cgh.parallel_for(
  8110. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8111. block_dims),
  8112. [=](sycl::nd_item<3> item_ct1)
  8113. [[intel::reqd_sub_group_size(32)]] {
  8114. norm_f32(x, dst, ncols, eps, item_ct1,
  8115. s_sum_acc_ct1.get_pointer(), work_group_size);
  8116. });
  8117. });
  8118. }
  8119. }
  8120. static void group_norm_f32_sycl(const float *x, float *dst,
  8121. const int num_groups, const int group_size,
  8122. const int ne_elements, dpct::queue_ptr stream) {
  8123. static const float eps = 1e-6f;
  8124. if (group_size < 1024) {
  8125. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8126. stream->submit([&](sycl::handler &cgh) {
  8127. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8128. cgh);
  8129. const float eps_ct4 = eps;
  8130. cgh.parallel_for(
  8131. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  8132. block_dims),
  8133. [=](sycl::nd_item<3> item_ct1)
  8134. [[intel::reqd_sub_group_size(32)]] {
  8135. group_norm_f32(
  8136. x, dst, group_size, ne_elements, eps_ct4, item_ct1,
  8137. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8138. });
  8139. });
  8140. } else {
  8141. const int work_group_size = g_work_group_size;
  8142. const sycl::range<3> block_dims(1, 1, work_group_size);
  8143. /*
  8144. DPCT1049:18: The work-group size passed to the SYCL kernel may exceed
  8145. the limit. To get the device limit, query
  8146. info::device::max_work_group_size. Adjust the work-group size if needed.
  8147. */
  8148. stream->submit([&](sycl::handler &cgh) {
  8149. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8150. cgh);
  8151. const float eps_ct4 = eps;
  8152. cgh.parallel_for(
  8153. sycl::nd_range<3>(sycl::range<3>(1, 1, num_groups) * block_dims,
  8154. block_dims),
  8155. [=](sycl::nd_item<3> item_ct1)
  8156. [[intel::reqd_sub_group_size(32)]] {
  8157. group_norm_f32(x, dst, group_size, ne_elements,
  8158. eps_ct4, item_ct1,
  8159. s_sum_acc_ct1.get_pointer(), work_group_size);
  8160. });
  8161. });
  8162. }
  8163. }
  8164. static void concat_f32_sycl(const float *x, const float *y, float *dst,
  8165. const int ne0, int ne1, int ne2, int ne02,
  8166. dpct::queue_ptr stream) {
  8167. int num_blocks = (ne0 + SYCL_CONCAT_BLOCK_SIZE - 1) / SYCL_CONCAT_BLOCK_SIZE;
  8168. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  8169. stream->parallel_for(
  8170. sycl::nd_range<3>(gridDim *
  8171. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE),
  8172. sycl::range<3>(1, 1, SYCL_CONCAT_BLOCK_SIZE)),
  8173. [=](sycl::nd_item<3> item_ct1) {
  8174. concat_f32(x, y, dst, ne0, ne02, item_ct1);
  8175. });
  8176. }
  8177. static void upscale_f32_sycl(const float *x, float *dst, const int ne00,
  8178. const int ne01, const int ne02,
  8179. const int scale_factor, dpct::queue_ptr stream) {
  8180. int ne0 = (ne00 * scale_factor);
  8181. int num_blocks = (ne0 + SYCL_UPSCALE_BLOCK_SIZE - 1) / SYCL_UPSCALE_BLOCK_SIZE;
  8182. sycl::range<3> gridDim(ne02, (ne01 * scale_factor), num_blocks);
  8183. stream->parallel_for(
  8184. sycl::nd_range<3>(gridDim *
  8185. sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE),
  8186. sycl::range<3>(1, 1, SYCL_UPSCALE_BLOCK_SIZE)),
  8187. [=](sycl::nd_item<3> item_ct1) {
  8188. upscale_f32(x, dst, ne00, ne00 * ne01, scale_factor, item_ct1);
  8189. });
  8190. }
  8191. static void pad_f32_sycl(const float *x, float *dst, const int ne00,
  8192. const int ne01, const int ne02, const int ne0,
  8193. const int ne1, const int ne2, dpct::queue_ptr stream) {
  8194. int num_blocks = (ne0 + SYCL_PAD_BLOCK_SIZE - 1) / SYCL_PAD_BLOCK_SIZE;
  8195. sycl::range<3> gridDim(ne2, ne1, num_blocks);
  8196. stream->parallel_for(
  8197. sycl::nd_range<3>(gridDim * sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE),
  8198. sycl::range<3>(1, 1, SYCL_PAD_BLOCK_SIZE)),
  8199. [=](sycl::nd_item<3> item_ct1) {
  8200. pad_f32(x, dst, ne0, ne00, ne01, ne02, item_ct1);
  8201. });
  8202. }
  8203. static void rms_norm_f32_sycl(const float *x, float *dst, const int ncols,
  8204. const int nrows, const float eps,
  8205. dpct::queue_ptr stream) {
  8206. GGML_ASSERT(ncols % WARP_SIZE == 0);
  8207. // printf("%s ncols=%d, nrows=%d, WARP_SIZE=%d\n", __func__, ncols, nrows, WARP_SIZE);
  8208. if (ncols < 1024) {
  8209. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  8210. stream->submit([&](sycl::handler &cgh) {
  8211. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8212. cgh);
  8213. cgh.parallel_for(
  8214. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8215. block_dims),
  8216. [=](sycl::nd_item<3> item_ct1)
  8217. [[intel::reqd_sub_group_size(32)]] {
  8218. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  8219. s_sum_acc_ct1.get_pointer(), WARP_SIZE);
  8220. });
  8221. });
  8222. } else {
  8223. const int work_group_size = g_work_group_size;
  8224. const sycl::range<3> block_dims(1, 1, work_group_size);
  8225. /*
  8226. DPCT1049:19: The work-group size passed to the SYCL kernel may exceed
  8227. the limit. To get the device limit, query
  8228. info::device::max_work_group_size. Adjust the work-group size if needed.
  8229. */
  8230. stream->submit([&](sycl::handler &cgh) {
  8231. sycl::local_accessor<float, 1> s_sum_acc_ct1(sycl::range<1>(32),
  8232. cgh);
  8233. cgh.parallel_for(
  8234. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims,
  8235. block_dims),
  8236. [=](sycl::nd_item<3> item_ct1)
  8237. [[intel::reqd_sub_group_size(32)]] {
  8238. rms_norm_f32(x, dst, ncols, eps, item_ct1,
  8239. s_sum_acc_ct1.get_pointer(), work_group_size);
  8240. });
  8241. });
  8242. }
  8243. }
  8244. static void quantize_row_q8_1_sycl(const float *x, void *vy, const int kx,
  8245. const int ky, const int kx_padded,
  8246. dpct::queue_ptr stream) {
  8247. const int block_num_x = (kx_padded + SYCL_QUANTIZE_BLOCK_SIZE - 1) / SYCL_QUANTIZE_BLOCK_SIZE;
  8248. const sycl::range<3> num_blocks(1, ky, block_num_x);
  8249. const sycl::range<3> block_size(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE);
  8250. {
  8251. dpct::has_capability_or_fail(stream->get_device(),
  8252. {sycl::aspect::fp16});
  8253. stream->parallel_for(
  8254. sycl::nd_range<3>(num_blocks * block_size, block_size),
  8255. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8256. quantize_q8_1(x, vy, kx, kx_padded, item_ct1);
  8257. });
  8258. }
  8259. }
  8260. template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
  8261. static void dequantize_block_sycl(const void *__restrict__ vx,
  8262. dst_t *__restrict__ y, const int k,
  8263. dpct::queue_ptr stream) {
  8264. const int num_blocks = (k + 2*SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / (2*SYCL_DEQUANTIZE_BLOCK_SIZE);
  8265. {
  8266. dpct::has_capability_or_fail(stream->get_device(),
  8267. {sycl::aspect::fp16});
  8268. stream->parallel_for(
  8269. sycl::nd_range<3>(
  8270. sycl::range<3>(1, 1, num_blocks) *
  8271. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  8272. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  8273. [=](sycl::nd_item<3> item_ct1) {
  8274. dequantize_block<qk, qr, dequantize_kernel>(vx, y, k, item_ct1);
  8275. });
  8276. }
  8277. }
  8278. template <typename dst_t>
  8279. static void dequantize_row_q2_K_sycl(const void *vx, dst_t *y, const int k,
  8280. dpct::queue_ptr stream) {
  8281. const int nb = k / QK_K;
  8282. #if QK_K == 256
  8283. {
  8284. dpct::has_capability_or_fail(stream->get_device(),
  8285. {sycl::aspect::fp16});
  8286. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8287. sycl::range<3>(1, 1, 64),
  8288. sycl::range<3>(1, 1, 64)),
  8289. [=](sycl::nd_item<3> item_ct1) {
  8290. dequantize_block_q2_K(vx, y, item_ct1);
  8291. });
  8292. }
  8293. #else
  8294. {
  8295. dpct::has_capability_or_fail(stream->get_device(),
  8296. {sycl::aspect::fp16});
  8297. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8298. sycl::range<3>(1, 1, 32),
  8299. sycl::range<3>(1, 1, 32)),
  8300. [=](sycl::nd_item<3> item_ct1) {
  8301. dequantize_block_q2_K(vx, y, item_ct1);
  8302. });
  8303. }
  8304. #endif
  8305. }
  8306. template <typename dst_t>
  8307. static void dequantize_row_q3_K_sycl(const void *vx, dst_t *y, const int k,
  8308. dpct::queue_ptr stream) {
  8309. const int nb = k / QK_K;
  8310. #if QK_K == 256
  8311. {
  8312. dpct::has_capability_or_fail(stream->get_device(),
  8313. {sycl::aspect::fp16});
  8314. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8315. sycl::range<3>(1, 1, 64),
  8316. sycl::range<3>(1, 1, 64)),
  8317. [=](sycl::nd_item<3> item_ct1) {
  8318. dequantize_block_q3_K(vx, y, item_ct1);
  8319. });
  8320. }
  8321. #else
  8322. {
  8323. dpct::has_capability_or_fail(stream->get_device(),
  8324. {sycl::aspect::fp16});
  8325. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8326. sycl::range<3>(1, 1, 32),
  8327. sycl::range<3>(1, 1, 32)),
  8328. [=](sycl::nd_item<3> item_ct1) {
  8329. dequantize_block_q3_K(vx, y, item_ct1);
  8330. });
  8331. }
  8332. #endif
  8333. }
  8334. template <typename dst_t>
  8335. static void dequantize_row_q4_0_sycl(const void *vx, dst_t *y, const int k,
  8336. dpct::queue_ptr stream) {
  8337. const int nb32 = k / 32;
  8338. const int nb = (k + 255) / 256;
  8339. {
  8340. dpct::has_capability_or_fail(stream->get_device(),
  8341. {sycl::aspect::fp16});
  8342. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8343. sycl::range<3>(1, 1, 32),
  8344. sycl::range<3>(1, 1, 32)),
  8345. [=](sycl::nd_item<3> item_ct1) {
  8346. dequantize_block_q4_0(vx, y, nb32, item_ct1);
  8347. });
  8348. }
  8349. }
  8350. template <typename dst_t>
  8351. static void dequantize_row_q4_1_sycl(const void *vx, dst_t *y, const int k,
  8352. dpct::queue_ptr stream) {
  8353. const int nb32 = k / 32;
  8354. const int nb = (k + 255) / 256;
  8355. {
  8356. dpct::has_capability_or_fail(stream->get_device(),
  8357. {sycl::aspect::fp16});
  8358. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8359. sycl::range<3>(1, 1, 32),
  8360. sycl::range<3>(1, 1, 32)),
  8361. [=](sycl::nd_item<3> item_ct1) {
  8362. dequantize_block_q4_1(vx, y, nb32, item_ct1);
  8363. });
  8364. }
  8365. }
  8366. template <typename dst_t>
  8367. static void dequantize_row_q4_K_sycl(const void *vx, dst_t *y, const int k,
  8368. dpct::queue_ptr stream) {
  8369. const int nb = k / QK_K;
  8370. {
  8371. dpct::has_capability_or_fail(stream->get_device(),
  8372. {sycl::aspect::fp16});
  8373. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8374. sycl::range<3>(1, 1, 32),
  8375. sycl::range<3>(1, 1, 32)),
  8376. [=](sycl::nd_item<3> item_ct1) {
  8377. dequantize_block_q4_K(vx, y, item_ct1);
  8378. });
  8379. }
  8380. }
  8381. template <typename dst_t>
  8382. static void dequantize_row_q5_K_sycl(const void *vx, dst_t *y, const int k,
  8383. dpct::queue_ptr stream) {
  8384. const int nb = k / QK_K;
  8385. #if QK_K == 256
  8386. {
  8387. dpct::has_capability_or_fail(stream->get_device(),
  8388. {sycl::aspect::fp16});
  8389. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8390. sycl::range<3>(1, 1, 64),
  8391. sycl::range<3>(1, 1, 64)),
  8392. [=](sycl::nd_item<3> item_ct1) {
  8393. dequantize_block_q5_K(vx, y, item_ct1);
  8394. });
  8395. }
  8396. #else
  8397. {
  8398. dpct::has_capability_or_fail(stream->get_device(),
  8399. {sycl::aspect::fp16});
  8400. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8401. sycl::range<3>(1, 1, 32),
  8402. sycl::range<3>(1, 1, 32)),
  8403. [=](sycl::nd_item<3> item_ct1) {
  8404. dequantize_block_q5_K(vx, y, item_ct1);
  8405. });
  8406. }
  8407. #endif
  8408. }
  8409. template <typename dst_t>
  8410. static void dequantize_row_q6_K_sycl(const void *vx, dst_t *y, const int k,
  8411. dpct::queue_ptr stream) {
  8412. const int nb = k / QK_K;
  8413. #if QK_K == 256
  8414. {
  8415. dpct::has_capability_or_fail(stream->get_device(),
  8416. {sycl::aspect::fp16});
  8417. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8418. sycl::range<3>(1, 1, 64),
  8419. sycl::range<3>(1, 1, 64)),
  8420. [=](sycl::nd_item<3> item_ct1) {
  8421. dequantize_block_q6_K(vx, y, item_ct1);
  8422. });
  8423. }
  8424. #else
  8425. {
  8426. dpct::has_capability_or_fail(stream->get_device(),
  8427. {sycl::aspect::fp16});
  8428. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8429. sycl::range<3>(1, 1, 32),
  8430. sycl::range<3>(1, 1, 32)),
  8431. [=](sycl::nd_item<3> item_ct1) {
  8432. dequantize_block_q6_K(vx, y, item_ct1);
  8433. });
  8434. }
  8435. #endif
  8436. }
  8437. template <typename dst_t>
  8438. static void dequantize_row_iq2_xxs_sycl(const void *vx, dst_t *y, const int k,
  8439. dpct::queue_ptr stream) {
  8440. const int nb = k / QK_K;
  8441. {
  8442. iq2xxs_grid.init(*stream);
  8443. ksigns_iq2xs.init(*stream);
  8444. kmask_iq2xs.init(*stream);
  8445. dpct::has_capability_or_fail(stream->get_device(),
  8446. {sycl::aspect::fp16});
  8447. stream->submit([&](sycl::handler &cgh) {
  8448. auto iq2xxs_grid_ptr_ct1 = iq2xxs_grid.get_ptr();
  8449. auto ksigns_iq2xs_ptr_ct1 = ksigns_iq2xs.get_ptr();
  8450. auto kmask_iq2xs_ptr_ct1 = kmask_iq2xs.get_ptr();
  8451. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8452. sycl::range<3>(1, 1, 32),
  8453. sycl::range<3>(1, 1, 32)),
  8454. [=](sycl::nd_item<3> item_ct1) {
  8455. dequantize_block_iq2_xxs(
  8456. vx, y, item_ct1, iq2xxs_grid_ptr_ct1,
  8457. ksigns_iq2xs_ptr_ct1, kmask_iq2xs_ptr_ct1);
  8458. });
  8459. });
  8460. }
  8461. }
  8462. template <typename dst_t>
  8463. static void dequantize_row_iq2_xs_sycl(const void *vx, dst_t *y, const int k,
  8464. dpct::queue_ptr stream) {
  8465. const int nb = k / QK_K;
  8466. {
  8467. iq2xs_grid.init(*stream);
  8468. ksigns_iq2xs.init(*stream);
  8469. kmask_iq2xs.init(*stream);
  8470. dpct::has_capability_or_fail(stream->get_device(),
  8471. {sycl::aspect::fp16});
  8472. stream->submit([&](sycl::handler &cgh) {
  8473. auto iq2xs_grid_ptr_ct1 = iq2xs_grid.get_ptr();
  8474. auto ksigns_iq2xs_ptr_ct1 = ksigns_iq2xs.get_ptr();
  8475. auto kmask_iq2xs_ptr_ct1 = kmask_iq2xs.get_ptr();
  8476. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8477. sycl::range<3>(1, 1, 32),
  8478. sycl::range<3>(1, 1, 32)),
  8479. [=](sycl::nd_item<3> item_ct1) {
  8480. dequantize_block_iq2_xs(
  8481. vx, y, item_ct1, iq2xs_grid_ptr_ct1,
  8482. ksigns_iq2xs_ptr_ct1, kmask_iq2xs_ptr_ct1);
  8483. });
  8484. });
  8485. }
  8486. }
  8487. template <typename dst_t>
  8488. static void dequantize_row_iq3_xxs_sycl(const void *vx, dst_t *y, const int k,
  8489. dpct::queue_ptr stream) {
  8490. const int nb = k / QK_K;
  8491. {
  8492. iq3xxs_grid.init(*stream);
  8493. ksigns_iq2xs.init(*stream);
  8494. kmask_iq2xs.init(*stream);
  8495. dpct::has_capability_or_fail(stream->get_device(),
  8496. {sycl::aspect::fp16});
  8497. stream->submit([&](sycl::handler &cgh) {
  8498. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  8499. auto ksigns_iq2xs_ptr_ct1 = ksigns_iq2xs.get_ptr();
  8500. auto kmask_iq2xs_ptr_ct1 = kmask_iq2xs.get_ptr();
  8501. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8502. sycl::range<3>(1, 1, 32),
  8503. sycl::range<3>(1, 1, 32)),
  8504. [=](sycl::nd_item<3> item_ct1) {
  8505. dequantize_block_iq3_xxs(
  8506. vx, y, item_ct1, iq3xxs_grid_ptr_ct1,
  8507. ksigns_iq2xs_ptr_ct1, kmask_iq2xs_ptr_ct1);
  8508. });
  8509. });
  8510. }
  8511. }
  8512. template <typename dst_t>
  8513. static void dequantize_row_iq3_s_sycl(const void *vx, dst_t *y, const int k,
  8514. dpct::queue_ptr stream) {
  8515. const int nb = k / QK_K;
  8516. {
  8517. iq3s_grid.init(*stream);
  8518. ksigns_iq2xs.init(*stream);
  8519. kmask_iq2xs.init(*stream);
  8520. dpct::has_capability_or_fail(stream->get_device(),
  8521. {sycl::aspect::fp16});
  8522. stream->submit([&](sycl::handler &cgh) {
  8523. auto iq3s_grid_ptr_ct1 = iq3s_grid.get_ptr();
  8524. auto ksigns_iq2xs_ptr_ct1 = ksigns_iq2xs.get_ptr();
  8525. auto kmask_iq2xs_ptr_ct1 = kmask_iq2xs.get_ptr();
  8526. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8527. sycl::range<3>(1, 1, 32),
  8528. sycl::range<3>(1, 1, 32)),
  8529. [=](sycl::nd_item<3> item_ct1) {
  8530. dequantize_block_iq3_s(
  8531. vx, y, item_ct1, iq3s_grid_ptr_ct1,
  8532. ksigns_iq2xs_ptr_ct1, kmask_iq2xs_ptr_ct1);
  8533. });
  8534. });
  8535. }
  8536. }
  8537. template <typename dst_t>
  8538. static void dequantize_row_iq1_s_sycl(const void *vx, dst_t *y, const int k,
  8539. dpct::queue_ptr stream) {
  8540. const int nb = k / QK_K;
  8541. {
  8542. iq1s_grid_gpu.init(*stream);
  8543. ksigns_iq2xs.init(*stream);
  8544. kmask_iq2xs.init(*stream);
  8545. dpct::has_capability_or_fail(stream->get_device(),
  8546. {sycl::aspect::fp16});
  8547. stream->submit([&](sycl::handler &cgh) {
  8548. auto iq1s_grid_ptr_ct1 = iq1s_grid_gpu.get_ptr();
  8549. auto ksigns_iq2xs_ptr_ct1 = ksigns_iq2xs.get_ptr();
  8550. auto kmask_iq2xs_ptr_ct1 = kmask_iq2xs.get_ptr();
  8551. cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, nb) *
  8552. sycl::range<3>(1, 1, 32),
  8553. sycl::range<3>(1, 1, 32)),
  8554. [=](sycl::nd_item<3> item_ct1) {
  8555. dequantize_block_iq1_s(
  8556. vx, y, item_ct1, iq1s_grid_ptr_ct1,
  8557. ksigns_iq2xs_ptr_ct1, kmask_iq2xs_ptr_ct1);
  8558. });
  8559. });
  8560. }
  8561. }
  8562. template <typename src_t, typename dst_t>
  8563. static void convert_unary_sycl(const void *__restrict__ vx,
  8564. dst_t *__restrict__ y, const int k,
  8565. dpct::queue_ptr stream) {
  8566. const int num_blocks = (k + SYCL_DEQUANTIZE_BLOCK_SIZE - 1) / SYCL_DEQUANTIZE_BLOCK_SIZE;
  8567. {
  8568. dpct::has_capability_or_fail(stream->get_device(),
  8569. {sycl::aspect::fp16});
  8570. stream->parallel_for(
  8571. sycl::nd_range<3>(
  8572. sycl::range<3>(1, 1, num_blocks) *
  8573. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE),
  8574. sycl::range<3>(1, 1, SYCL_DEQUANTIZE_BLOCK_SIZE)),
  8575. [=](sycl::nd_item<3> item_ct1) {
  8576. convert_unary<src_t>(vx, y, k, item_ct1);
  8577. });
  8578. }
  8579. }
  8580. static to_fp16_sycl_t ggml_get_to_fp16_sycl(ggml_type type) try {
  8581. int id;
  8582. switch (type) {
  8583. case GGML_TYPE_Q4_0:
  8584. return dequantize_block_sycl<QK4_0, QR4_0, dequantize_q4_0>;
  8585. case GGML_TYPE_Q4_1:
  8586. return dequantize_block_sycl<QK4_1, QR4_1, dequantize_q4_1>;
  8587. case GGML_TYPE_Q5_0:
  8588. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  8589. case GGML_TYPE_Q5_1:
  8590. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  8591. case GGML_TYPE_Q8_0:
  8592. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  8593. case GGML_TYPE_Q2_K:
  8594. return dequantize_row_q2_K_sycl;
  8595. case GGML_TYPE_Q3_K:
  8596. return dequantize_row_q3_K_sycl;
  8597. case GGML_TYPE_Q4_K:
  8598. return dequantize_row_q4_K_sycl;
  8599. case GGML_TYPE_Q5_K:
  8600. return dequantize_row_q5_K_sycl;
  8601. case GGML_TYPE_Q6_K:
  8602. return dequantize_row_q6_K_sycl;
  8603. case GGML_TYPE_IQ2_XXS:
  8604. return dequantize_row_iq2_xxs_sycl;
  8605. case GGML_TYPE_IQ2_XS:
  8606. return dequantize_row_iq2_xs_sycl;
  8607. case GGML_TYPE_IQ3_XXS:
  8608. return dequantize_row_iq3_xxs_sycl;
  8609. case GGML_TYPE_IQ3_S:
  8610. return dequantize_row_iq3_s_sycl;
  8611. case GGML_TYPE_IQ1_S:
  8612. return dequantize_row_iq1_s_sycl;
  8613. case GGML_TYPE_F32:
  8614. return convert_unary_sycl<float>;
  8615. default:
  8616. return nullptr;
  8617. }
  8618. }
  8619. catch (sycl::exception const &exc) {
  8620. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  8621. << ", line:" << __LINE__ << std::endl;
  8622. std::exit(1);
  8623. }
  8624. static to_fp32_sycl_t ggml_get_to_fp32_sycl(ggml_type type) {
  8625. switch (type) {
  8626. case GGML_TYPE_Q4_0:
  8627. return dequantize_row_q4_0_sycl;
  8628. case GGML_TYPE_Q4_1:
  8629. return dequantize_row_q4_1_sycl;
  8630. case GGML_TYPE_Q5_0:
  8631. return dequantize_block_sycl<QK5_0, QR5_0, dequantize_q5_0>;
  8632. case GGML_TYPE_Q5_1:
  8633. return dequantize_block_sycl<QK5_1, QR5_1, dequantize_q5_1>;
  8634. case GGML_TYPE_Q8_0:
  8635. return dequantize_block_sycl<QK8_0, QR8_0, dequantize_q8_0>;
  8636. case GGML_TYPE_Q2_K:
  8637. return dequantize_row_q2_K_sycl;
  8638. case GGML_TYPE_Q3_K:
  8639. return dequantize_row_q3_K_sycl;
  8640. case GGML_TYPE_Q4_K:
  8641. return dequantize_row_q4_K_sycl;
  8642. case GGML_TYPE_Q5_K:
  8643. return dequantize_row_q5_K_sycl;
  8644. case GGML_TYPE_Q6_K:
  8645. return dequantize_row_q6_K_sycl;
  8646. case GGML_TYPE_IQ2_XXS:
  8647. return dequantize_row_iq2_xxs_sycl;
  8648. case GGML_TYPE_IQ2_XS:
  8649. return dequantize_row_iq2_xs_sycl;
  8650. case GGML_TYPE_IQ3_XXS:
  8651. return dequantize_row_iq3_xxs_sycl;
  8652. case GGML_TYPE_IQ3_S:
  8653. return dequantize_row_iq3_s_sycl;
  8654. case GGML_TYPE_IQ1_S:
  8655. return dequantize_row_iq1_s_sycl;
  8656. case GGML_TYPE_F16:
  8657. return convert_unary_sycl<sycl::half>;
  8658. default:
  8659. return nullptr;
  8660. }
  8661. }
  8662. static void dequantize_mul_mat_vec_q4_0_sycl(const void *vx, const dfloat *y,
  8663. float *dst, const int ncols,
  8664. const int nrows,
  8665. dpct::queue_ptr stream) {
  8666. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8667. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8668. // the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
  8669. const sycl::range<3> block_nums(1, 1, block_num_y);
  8670. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8671. {
  8672. dpct::has_capability_or_fail(stream->get_device(),
  8673. {sycl::aspect::fp16});
  8674. stream->parallel_for(
  8675. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8676. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8677. dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>(
  8678. vx, y, dst, ncols, nrows, item_ct1);
  8679. });
  8680. }
  8681. }
  8682. static void dequantize_mul_mat_vec_q4_1_sycl(const void *vx, const dfloat *y,
  8683. float *dst, const int ncols,
  8684. const int nrows,
  8685. dpct::queue_ptr stream) {
  8686. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8687. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8688. const sycl::range<3> block_nums(1, 1, block_num_y);
  8689. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8690. {
  8691. dpct::has_capability_or_fail(stream->get_device(),
  8692. {sycl::aspect::fp16});
  8693. stream->parallel_for(
  8694. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8695. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8696. dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>(
  8697. vx, y, dst, ncols, nrows, item_ct1);
  8698. });
  8699. }
  8700. }
  8701. static void dequantize_mul_mat_vec_q5_0_sycl(const void *vx, const dfloat *y,
  8702. float *dst, const int ncols,
  8703. const int nrows,
  8704. dpct::queue_ptr stream) {
  8705. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8706. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8707. const sycl::range<3> block_nums(1, 1, block_num_y);
  8708. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8709. {
  8710. dpct::has_capability_or_fail(stream->get_device(),
  8711. {sycl::aspect::fp16});
  8712. stream->parallel_for(
  8713. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8714. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8715. dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>(
  8716. vx, y, dst, ncols, nrows, item_ct1);
  8717. });
  8718. }
  8719. }
  8720. static void dequantize_mul_mat_vec_q5_1_sycl(const void *vx, const dfloat *y,
  8721. float *dst, const int ncols,
  8722. const int nrows,
  8723. dpct::queue_ptr stream) {
  8724. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8725. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8726. const sycl::range<3> block_nums(1, 1, block_num_y);
  8727. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8728. {
  8729. dpct::has_capability_or_fail(stream->get_device(),
  8730. {sycl::aspect::fp16});
  8731. stream->parallel_for(
  8732. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8733. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8734. dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>(
  8735. vx, y, dst, ncols, nrows, item_ct1);
  8736. });
  8737. }
  8738. }
  8739. static void dequantize_mul_mat_vec_q8_0_sycl(const void *vx, const dfloat *y,
  8740. float *dst, const int ncols,
  8741. const int nrows,
  8742. dpct::queue_ptr stream) {
  8743. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8744. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8745. const sycl::range<3> block_nums(1, 1, block_num_y);
  8746. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8747. {
  8748. dpct::has_capability_or_fail(stream->get_device(),
  8749. {sycl::aspect::fp16});
  8750. stream->parallel_for(
  8751. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8752. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8753. dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>(
  8754. vx, y, dst, ncols, nrows, item_ct1);
  8755. });
  8756. }
  8757. }
  8758. static void dequantize_mul_mat_vec_q2_K_sycl(const void *vx, const float *y,
  8759. float *dst, const int ncols,
  8760. const int nrows,
  8761. dpct::queue_ptr stream) {
  8762. GGML_ASSERT(ncols % QK_K == 0);
  8763. const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
  8764. const int block_num_y = (nrows + ny - 1) / ny;
  8765. const sycl::range<3> block_nums(1, 1, block_num_y);
  8766. const sycl::range<3> block_dims(1, ny, 32);
  8767. stream->parallel_for(
  8768. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8769. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8770. dequantize_mul_mat_vec_q2_k(vx, y, dst, ncols, nrows, item_ct1);
  8771. });
  8772. }
  8773. static void dequantize_mul_mat_vec_q3_K_sycl(const void *vx, const float *y,
  8774. float *dst, const int ncols,
  8775. const int nrows,
  8776. dpct::queue_ptr stream) {
  8777. GGML_ASSERT(ncols % QK_K == 0);
  8778. const int ny = 2 / K_QUANTS_PER_ITERATION;
  8779. const int block_num_y = (nrows + ny - 1) / ny;
  8780. const sycl::range<3> block_nums(1, 1, block_num_y);
  8781. const sycl::range<3> block_dims(1, ny, 32);
  8782. stream->parallel_for(
  8783. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8784. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8785. dequantize_mul_mat_vec_q3_k(vx, y, dst, ncols, nrows, item_ct1);
  8786. });
  8787. }
  8788. static void dequantize_mul_mat_vec_q4_K_sycl(const void *vx, const float *y,
  8789. float *dst, const int ncols,
  8790. const int nrows,
  8791. dpct::queue_ptr stream) {
  8792. GGML_ASSERT(ncols % QK_K == 0);
  8793. const int ny = 2 / K_QUANTS_PER_ITERATION;
  8794. const int block_num_y = (nrows + ny - 1) / ny;
  8795. const sycl::range<3> block_nums(1, 1, block_num_y);
  8796. const sycl::range<3> block_dims(1, ny, 32);
  8797. stream->parallel_for(
  8798. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8799. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8800. dequantize_mul_mat_vec_q4_k(vx, y, dst, ncols, nrows, item_ct1);
  8801. });
  8802. }
  8803. static void dequantize_mul_mat_vec_q5_K_sycl(const void *vx, const float *y,
  8804. float *dst, const int ncols,
  8805. const int nrows,
  8806. dpct::queue_ptr stream) {
  8807. GGML_ASSERT(ncols % QK_K == 0);
  8808. const sycl::range<3> block_dims(1, 1, 32);
  8809. stream->parallel_for(
  8810. sycl::nd_range<3>(sycl::range<3>(1, 1, nrows) * block_dims, block_dims),
  8811. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8812. dequantize_mul_mat_vec_q5_k(vx, y, dst, ncols, item_ct1);
  8813. });
  8814. }
  8815. static void dequantize_mul_mat_vec_q6_K_sycl(const void *vx, const float *y,
  8816. float *dst, const int ncols,
  8817. const int nrows,
  8818. dpct::queue_ptr stream) {
  8819. GGML_ASSERT(ncols % QK_K == 0);
  8820. const int ny = 2 / K_QUANTS_PER_ITERATION;
  8821. const int block_num_y = (nrows + ny - 1) / ny;
  8822. const sycl::range<3> block_nums(1, 1, block_num_y);
  8823. const sycl::range<3> block_dims(1, ny, 32);
  8824. stream->parallel_for(
  8825. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8826. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8827. dequantize_mul_mat_vec_q6_k(vx, y, dst, ncols, nrows, item_ct1);
  8828. });
  8829. }
  8830. static void convert_mul_mat_vec_f16_sycl(const void *vx, const dfloat *y,
  8831. float *dst, const int ncols,
  8832. const int nrows,
  8833. dpct::queue_ptr stream) {
  8834. GGML_ASSERT(ncols % GGML_SYCL_DMMV_X == 0);
  8835. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8836. const sycl::range<3> block_nums(1, 1, block_num_y);
  8837. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8838. {
  8839. dpct::has_capability_or_fail(stream->get_device(),
  8840. {sycl::aspect::fp16});
  8841. stream->parallel_for(
  8842. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8843. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  8844. dequantize_mul_mat_vec<1, 1, convert_f16>(vx, y, dst, ncols,
  8845. nrows, item_ct1);
  8846. });
  8847. }
  8848. }
  8849. static void mul_mat_vec_q4_0_q8_1_sycl(const void *vx, const void *vy,
  8850. float *dst, const int ncols,
  8851. const int nrows,
  8852. dpct::queue_ptr stream) {
  8853. GGML_ASSERT(ncols % QK4_0 == 0);
  8854. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8855. const sycl::range<3> block_nums(1, 1, block_num_y);
  8856. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8857. {
  8858. iq3xxs_grid.init(*stream);
  8859. ksigns64.init(*stream);
  8860. stream->submit([&](sycl::handler &cgh) {
  8861. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  8862. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  8863. cgh.parallel_for(
  8864. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8865. [=](sycl::nd_item<3> item_ct1)
  8866. [[intel::reqd_sub_group_size(32)]] {
  8867. mul_mat_vec_q<QK4_0, QI4_0, block_q4_0,
  8868. VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>(
  8869. vx, vy, dst, ncols, nrows, item_ct1,
  8870. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  8871. });
  8872. });
  8873. }
  8874. }
  8875. static void mul_mat_vec_q4_1_q8_1_sycl(const void *vx, const void *vy,
  8876. float *dst, const int ncols,
  8877. const int nrows,
  8878. dpct::queue_ptr stream) {
  8879. GGML_ASSERT(ncols % QK4_1 == 0);
  8880. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8881. const sycl::range<3> block_nums(1, 1, block_num_y);
  8882. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8883. {
  8884. iq3xxs_grid.init(*stream);
  8885. ksigns64.init(*stream);
  8886. stream->submit([&](sycl::handler &cgh) {
  8887. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  8888. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  8889. cgh.parallel_for(
  8890. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8891. [=](sycl::nd_item<3> item_ct1)
  8892. [[intel::reqd_sub_group_size(32)]] {
  8893. mul_mat_vec_q<QK4_0, QI4_1, block_q4_1,
  8894. VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>(
  8895. vx, vy, dst, ncols, nrows, item_ct1,
  8896. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  8897. });
  8898. });
  8899. }
  8900. }
  8901. static void mul_mat_vec_q5_0_q8_1_sycl(const void *vx, const void *vy,
  8902. float *dst, const int ncols,
  8903. const int nrows,
  8904. dpct::queue_ptr stream) {
  8905. GGML_ASSERT(ncols % QK5_0 == 0);
  8906. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8907. const sycl::range<3> block_nums(1, 1, block_num_y);
  8908. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8909. {
  8910. iq3xxs_grid.init(*stream);
  8911. ksigns64.init(*stream);
  8912. stream->submit([&](sycl::handler &cgh) {
  8913. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  8914. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  8915. cgh.parallel_for(
  8916. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8917. [=](sycl::nd_item<3> item_ct1)
  8918. [[intel::reqd_sub_group_size(32)]] {
  8919. mul_mat_vec_q<QK5_0, QI5_0, block_q5_0,
  8920. VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>(
  8921. vx, vy, dst, ncols, nrows, item_ct1,
  8922. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  8923. });
  8924. });
  8925. }
  8926. }
  8927. static void mul_mat_vec_q5_1_q8_1_sycl(const void *vx, const void *vy,
  8928. float *dst, const int ncols,
  8929. const int nrows,
  8930. dpct::queue_ptr stream) {
  8931. GGML_ASSERT(ncols % QK5_1 == 0);
  8932. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8933. const sycl::range<3> block_nums(1, 1, block_num_y);
  8934. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8935. {
  8936. iq3xxs_grid.init(*stream);
  8937. ksigns64.init(*stream);
  8938. stream->submit([&](sycl::handler &cgh) {
  8939. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  8940. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  8941. cgh.parallel_for(
  8942. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8943. [=](sycl::nd_item<3> item_ct1)
  8944. [[intel::reqd_sub_group_size(32)]] {
  8945. mul_mat_vec_q<QK5_1, QI5_1, block_q5_1,
  8946. VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>(
  8947. vx, vy, dst, ncols, nrows, item_ct1,
  8948. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  8949. });
  8950. });
  8951. }
  8952. }
  8953. static void mul_mat_vec_q8_0_q8_1_sycl(const void *vx, const void *vy,
  8954. float *dst, const int ncols,
  8955. const int nrows,
  8956. dpct::queue_ptr stream) {
  8957. GGML_ASSERT(ncols % QK8_0 == 0);
  8958. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8959. const sycl::range<3> block_nums(1, 1, block_num_y);
  8960. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8961. {
  8962. iq3xxs_grid.init(*stream);
  8963. ksigns64.init(*stream);
  8964. stream->submit([&](sycl::handler &cgh) {
  8965. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  8966. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  8967. cgh.parallel_for(
  8968. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8969. [=](sycl::nd_item<3> item_ct1)
  8970. [[intel::reqd_sub_group_size(32)]] {
  8971. mul_mat_vec_q<QK8_0, QI8_0, block_q8_0,
  8972. VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>(
  8973. vx, vy, dst, ncols, nrows, item_ct1,
  8974. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  8975. });
  8976. });
  8977. }
  8978. }
  8979. static void mul_mat_vec_q2_K_q8_1_sycl(const void *vx, const void *vy,
  8980. float *dst, const int ncols,
  8981. const int nrows,
  8982. dpct::queue_ptr stream) {
  8983. GGML_ASSERT(ncols % QK_K == 0);
  8984. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  8985. const sycl::range<3> block_nums(1, 1, block_num_y);
  8986. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  8987. {
  8988. iq3xxs_grid.init(*stream);
  8989. ksigns64.init(*stream);
  8990. stream->submit([&](sycl::handler &cgh) {
  8991. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  8992. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  8993. cgh.parallel_for(
  8994. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  8995. [=](sycl::nd_item<3> item_ct1)
  8996. [[intel::reqd_sub_group_size(32)]] {
  8997. mul_mat_vec_q<QK_K, QI2_K, block_q2_K,
  8998. VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>(
  8999. vx, vy, dst, ncols, nrows, item_ct1,
  9000. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9001. });
  9002. });
  9003. }
  9004. }
  9005. static void mul_mat_vec_q3_K_q8_1_sycl(const void *vx, const void *vy,
  9006. float *dst, const int ncols,
  9007. const int nrows,
  9008. dpct::queue_ptr stream) {
  9009. GGML_ASSERT(ncols % QK_K == 0);
  9010. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9011. const sycl::range<3> block_nums(1, 1, block_num_y);
  9012. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9013. {
  9014. iq3xxs_grid.init(*stream);
  9015. ksigns64.init(*stream);
  9016. stream->submit([&](sycl::handler &cgh) {
  9017. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9018. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9019. cgh.parallel_for(
  9020. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9021. [=](sycl::nd_item<3> item_ct1)
  9022. [[intel::reqd_sub_group_size(32)]] {
  9023. mul_mat_vec_q<QK_K, QI3_K, block_q3_K,
  9024. VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>(
  9025. vx, vy, dst, ncols, nrows, item_ct1,
  9026. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9027. });
  9028. });
  9029. }
  9030. }
  9031. static void mul_mat_vec_q4_K_q8_1_sycl(const void *vx, const void *vy,
  9032. float *dst, const int ncols,
  9033. const int nrows,
  9034. dpct::queue_ptr stream) {
  9035. GGML_ASSERT(ncols % QK_K == 0);
  9036. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9037. const sycl::range<3> block_nums(1, 1, block_num_y);
  9038. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9039. {
  9040. iq3xxs_grid.init(*stream);
  9041. ksigns64.init(*stream);
  9042. stream->submit([&](sycl::handler &cgh) {
  9043. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9044. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9045. cgh.parallel_for(
  9046. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9047. [=](sycl::nd_item<3> item_ct1)
  9048. [[intel::reqd_sub_group_size(32)]] {
  9049. mul_mat_vec_q<QK_K, QI4_K, block_q4_K,
  9050. VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>(
  9051. vx, vy, dst, ncols, nrows, item_ct1,
  9052. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9053. });
  9054. });
  9055. }
  9056. }
  9057. static void mul_mat_vec_q5_K_q8_1_sycl(const void *vx, const void *vy,
  9058. float *dst, const int ncols,
  9059. const int nrows,
  9060. dpct::queue_ptr stream) {
  9061. GGML_ASSERT(ncols % QK_K == 0);
  9062. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9063. const sycl::range<3> block_nums(1, 1, block_num_y);
  9064. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9065. {
  9066. iq3xxs_grid.init(*stream);
  9067. ksigns64.init(*stream);
  9068. stream->submit([&](sycl::handler &cgh) {
  9069. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9070. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9071. cgh.parallel_for(
  9072. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9073. [=](sycl::nd_item<3> item_ct1)
  9074. [[intel::reqd_sub_group_size(32)]] {
  9075. mul_mat_vec_q<QK_K, QI5_K, block_q5_K,
  9076. VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>(
  9077. vx, vy, dst, ncols, nrows, item_ct1,
  9078. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9079. });
  9080. });
  9081. }
  9082. }
  9083. static void mul_mat_vec_q6_K_q8_1_sycl(const void *vx, const void *vy,
  9084. float *dst, const int ncols,
  9085. const int nrows,
  9086. dpct::queue_ptr stream) {
  9087. GGML_ASSERT(ncols % QK_K == 0);
  9088. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9089. const sycl::range<3> block_nums(1, 1, block_num_y);
  9090. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9091. {
  9092. iq3xxs_grid.init(*stream);
  9093. ksigns64.init(*stream);
  9094. stream->submit([&](sycl::handler &cgh) {
  9095. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9096. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9097. cgh.parallel_for(
  9098. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9099. [=](sycl::nd_item<3> item_ct1)
  9100. [[intel::reqd_sub_group_size(32)]] {
  9101. mul_mat_vec_q<QK_K, QI6_K, block_q6_K,
  9102. VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>(
  9103. vx, vy, dst, ncols, nrows, item_ct1,
  9104. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9105. });
  9106. });
  9107. }
  9108. }
  9109. static void mul_mat_vec_iq2_xxs_q8_1_sycl(const void *vx, const void *vy,
  9110. float *dst, const int ncols,
  9111. const int nrows,
  9112. dpct::queue_ptr stream) {
  9113. GGML_ASSERT(ncols % QK_K == 0);
  9114. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9115. const sycl::range<3> block_nums(1, 1, block_num_y);
  9116. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9117. {
  9118. iq2xxs_grid.init(*stream);
  9119. ksigns_iq2xs.init(*stream);
  9120. kmask_iq2xs.init(*stream);
  9121. stream->submit([&](sycl::handler &cgh) {
  9122. auto iq2xxs_grid_ptr_ct1 = iq2xxs_grid.get_ptr();
  9123. auto ksigns_iq2xs_ptr_ct1 = ksigns_iq2xs.get_ptr();
  9124. auto kmask_iq2xs_ptr_ct1 = kmask_iq2xs.get_ptr();
  9125. cgh.parallel_for(
  9126. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9127. [=](sycl::nd_item<3> item_ct1)
  9128. [[intel::reqd_sub_group_size(32)]] {
  9129. mul_mat_vec_q_iq2_xxs_q8_1<QK_K, QI2_XXS, block_iq2_xxs, 1>(
  9130. vx, vy, dst, ncols, nrows, item_ct1,
  9131. iq2xxs_grid_ptr_ct1, ksigns_iq2xs_ptr_ct1, kmask_iq2xs_ptr_ct1);
  9132. });
  9133. });
  9134. }
  9135. }
  9136. static void mul_mat_vec_iq2_xs_q8_1_sycl(const void *vx, const void *vy,
  9137. float *dst, const int ncols,
  9138. const int nrows,
  9139. dpct::queue_ptr stream) {
  9140. GGML_ASSERT(ncols % QK_K == 0);
  9141. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9142. const sycl::range<3> block_nums(1, 1, block_num_y);
  9143. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9144. {
  9145. iq2xs_grid.init(*stream);
  9146. ksigns64.init(*stream);
  9147. stream->submit([&](sycl::handler &cgh) {
  9148. auto iq2xs_grid_ptr_ct1 = iq2xs_grid.get_ptr();
  9149. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9150. cgh.parallel_for(
  9151. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9152. [=](sycl::nd_item<3> item_ct1)
  9153. [[intel::reqd_sub_group_size(32)]] {
  9154. mul_mat_vec_q_iq2_xs_q8_1<QK_K, QI2_XS, block_iq2_xs, 1>(
  9155. vx, vy, dst, ncols, nrows, item_ct1,
  9156. iq2xs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9157. });
  9158. });
  9159. }
  9160. }
  9161. static void mul_mat_vec_iq3_xxs_q8_1_sycl(const void *vx, const void *vy,
  9162. float *dst, const int ncols,
  9163. const int nrows,
  9164. dpct::queue_ptr stream) {
  9165. GGML_ASSERT(ncols % QK_K == 0);
  9166. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9167. const sycl::range<3> block_nums(1, 1, block_num_y);
  9168. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9169. {
  9170. iq3xxs_grid.init(*stream);
  9171. ksigns64.init(*stream);
  9172. stream->submit([&](sycl::handler &cgh) {
  9173. auto iq3xxs_grid_ptr_ct1 = iq3xxs_grid.get_ptr();
  9174. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9175. cgh.parallel_for(
  9176. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9177. [=](sycl::nd_item<3> item_ct1)
  9178. [[intel::reqd_sub_group_size(32)]] {
  9179. mul_mat_vec_q_iq3_xxs_q8_1<QK_K, QI3_XXS, block_iq3_xxs, 1>(
  9180. vx, vy, dst, ncols, nrows, item_ct1,
  9181. iq3xxs_grid_ptr_ct1, ksigns64_ptr_ct1);
  9182. });
  9183. });
  9184. }
  9185. }
  9186. static void mul_mat_vec_iq3_s_q8_1_sycl(const void *vx, const void *vy,
  9187. float *dst, const int ncols,
  9188. const int nrows,
  9189. dpct::queue_ptr stream) {
  9190. GGML_ASSERT(ncols % QK_K == 0);
  9191. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9192. const sycl::range<3> block_nums(1, 1, block_num_y);
  9193. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9194. {
  9195. iq3s_grid.init(*stream);
  9196. ksigns64.init(*stream);
  9197. stream->submit([&](sycl::handler &cgh) {
  9198. auto iq3s_grid_ptr_ct1 = iq3s_grid.get_ptr();
  9199. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9200. cgh.parallel_for(
  9201. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9202. [=](sycl::nd_item<3> item_ct1)
  9203. [[intel::reqd_sub_group_size(32)]] {
  9204. mul_mat_vec_q_iq3_s_q8_1<QK_K, QI3_XS, block_iq3_s, 1>(
  9205. vx, vy, dst, ncols, nrows, item_ct1,
  9206. iq3s_grid_ptr_ct1, ksigns64_ptr_ct1);
  9207. });
  9208. });
  9209. }
  9210. }
  9211. static void mul_mat_vec_iq1_s_q8_1_sycl(const void *vx, const void *vy,
  9212. float *dst, const int ncols,
  9213. const int nrows,
  9214. dpct::queue_ptr stream) {
  9215. GGML_ASSERT(ncols % QK_K == 0);
  9216. const int block_num_y = (nrows + GGML_SYCL_MMV_Y - 1) / GGML_SYCL_MMV_Y;
  9217. const sycl::range<3> block_nums(1, 1, block_num_y);
  9218. const sycl::range<3> block_dims(1, GGML_SYCL_MMV_Y, WARP_SIZE);
  9219. {
  9220. iq1s_grid_gpu.init(*stream);
  9221. ksigns64.init(*stream);
  9222. stream->submit([&](sycl::handler &cgh) {
  9223. auto iq1s_grid_ptr_ct1 = iq1s_grid_gpu.get_ptr();
  9224. auto ksigns64_ptr_ct1 = ksigns64.get_ptr();
  9225. cgh.parallel_for(
  9226. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9227. [=](sycl::nd_item<3> item_ct1)
  9228. [[intel::reqd_sub_group_size(32)]] {
  9229. mul_mat_vec_q_iq1_s_q8_1<QK_K, QI1_S, block_iq1_s, 1>(
  9230. vx, vy, dst, ncols, nrows, item_ct1,
  9231. iq1s_grid_ptr_ct1, ksigns64_ptr_ct1);
  9232. });
  9233. });
  9234. }
  9235. }
  9236. static void ggml_mul_mat_q4_0_q8_1_sycl(const void *vx, const void *vy,
  9237. float *dst, const int ncols_x,
  9238. const int nrows_x, const int ncols_y,
  9239. const int nrows_y, const int nrows_dst,
  9240. dpct::queue_ptr stream) try {
  9241. int id;
  9242. SYCL_CHECK(
  9243. CHECK_TRY_ERROR(id = get_current_device_id()));
  9244. const int compute_capability = g_device_caps[id].cc;
  9245. int mmq_x, mmq_y, nwarps;
  9246. if (compute_capability >= VER_GEN13) {
  9247. mmq_x = MMQ_X_Q4_0_RDNA2;
  9248. mmq_y = MMQ_Y_Q4_0_RDNA2;
  9249. nwarps = NWARPS_Q4_0_RDNA2;
  9250. } else if (compute_capability >= VER_GEN12) {
  9251. mmq_x = MMQ_X_Q4_0_RDNA1;
  9252. mmq_y = MMQ_Y_Q4_0_RDNA1;
  9253. nwarps = NWARPS_Q4_0_RDNA1;
  9254. } else if (compute_capability >= VER_GEN9) {
  9255. mmq_x = MMQ_X_Q4_0_AMPERE;
  9256. mmq_y = MMQ_Y_Q4_0_AMPERE;
  9257. nwarps = NWARPS_Q4_0_AMPERE;
  9258. } else if (compute_capability >= VER_4VEC) {
  9259. mmq_x = MMQ_X_Q4_0_PASCAL;
  9260. mmq_y = MMQ_Y_Q4_0_PASCAL;
  9261. nwarps = NWARPS_Q4_0_PASCAL;
  9262. } else {
  9263. GGML_ASSERT(false);
  9264. }
  9265. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9266. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9267. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9268. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9269. if (nrows_x % mmq_y == 0) {
  9270. const bool need_check = false;
  9271. /*
  9272. DPCT1049:20: The work-group size passed to the SYCL kernel may exceed
  9273. the limit. To get the device limit, query
  9274. info::device::max_work_group_size. Adjust the work-group size if needed.
  9275. */
  9276. {
  9277. dpct::has_capability_or_fail(stream->get_device(),
  9278. {sycl::aspect::fp16});
  9279. stream->submit([&](sycl::handler &cgh) {
  9280. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  9281. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9282. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  9283. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  9284. cgh);
  9285. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9286. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9287. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9288. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9289. cgh.parallel_for(
  9290. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9291. [=](sycl::nd_item<3> item_ct1) {
  9292. mul_mat_q4_0<need_check>(
  9293. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9294. nrows_dst, item_ct1,
  9295. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  9296. tile_x_d_q4_0_acc_ct1.get_pointer(),
  9297. tile_y_qs_acc_ct1.get_pointer(),
  9298. tile_y_ds_acc_ct1.get_pointer());
  9299. });
  9300. });
  9301. }
  9302. } else {
  9303. const bool need_check = true;
  9304. /*
  9305. DPCT1049:21: The work-group size passed to the SYCL kernel may exceed
  9306. the limit. To get the device limit, query
  9307. info::device::max_work_group_size. Adjust the work-group size if needed.
  9308. */
  9309. {
  9310. dpct::has_capability_or_fail(stream->get_device(),
  9311. {sycl::aspect::fp16});
  9312. stream->submit([&](sycl::handler &cgh) {
  9313. sycl::local_accessor<int, 1> tile_x_qs_q4_0_acc_ct1(
  9314. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9315. sycl::local_accessor<float, 1> tile_x_d_q4_0_acc_ct1(
  9316. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_0) + mmq_y / QI4_0),
  9317. cgh);
  9318. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9319. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9320. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9321. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9322. cgh.parallel_for(
  9323. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9324. [=](sycl::nd_item<3> item_ct1) {
  9325. mul_mat_q4_0<need_check>(
  9326. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9327. nrows_dst, item_ct1,
  9328. tile_x_qs_q4_0_acc_ct1.get_pointer(),
  9329. tile_x_d_q4_0_acc_ct1.get_pointer(),
  9330. tile_y_qs_acc_ct1.get_pointer(),
  9331. tile_y_ds_acc_ct1.get_pointer());
  9332. });
  9333. });
  9334. }
  9335. }
  9336. }
  9337. catch (sycl::exception const &exc) {
  9338. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9339. << ", line:" << __LINE__ << std::endl;
  9340. std::exit(1);
  9341. }
  9342. static void ggml_mul_mat_q4_1_q8_1_sycl(const void *vx, const void *vy,
  9343. float *dst, const int ncols_x,
  9344. const int nrows_x, const int ncols_y,
  9345. const int nrows_y, const int nrows_dst,
  9346. dpct::queue_ptr stream) try {
  9347. int id;
  9348. SYCL_CHECK(
  9349. CHECK_TRY_ERROR(id = get_current_device_id()));
  9350. const int compute_capability = g_device_caps[id].cc;
  9351. int mmq_x, mmq_y, nwarps;
  9352. if (compute_capability >= VER_GEN13) {
  9353. mmq_x = MMQ_X_Q4_1_RDNA2;
  9354. mmq_y = MMQ_Y_Q4_1_RDNA2;
  9355. nwarps = NWARPS_Q4_1_RDNA2;
  9356. } else if (compute_capability >= VER_GEN12) {
  9357. mmq_x = MMQ_X_Q4_1_RDNA1;
  9358. mmq_y = MMQ_Y_Q4_1_RDNA1;
  9359. nwarps = NWARPS_Q4_1_RDNA1;
  9360. } else if (compute_capability >= VER_GEN9) {
  9361. mmq_x = MMQ_X_Q4_1_AMPERE;
  9362. mmq_y = MMQ_Y_Q4_1_AMPERE;
  9363. nwarps = NWARPS_Q4_1_AMPERE;
  9364. } else if (compute_capability >= VER_4VEC) {
  9365. mmq_x = MMQ_X_Q4_1_PASCAL;
  9366. mmq_y = MMQ_Y_Q4_1_PASCAL;
  9367. nwarps = NWARPS_Q4_1_PASCAL;
  9368. } else {
  9369. GGML_ASSERT(false);
  9370. }
  9371. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9372. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9373. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9374. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9375. if (nrows_x % mmq_y == 0) {
  9376. const bool need_check = false;
  9377. /*
  9378. DPCT1049:22: The work-group size passed to the SYCL kernel may exceed
  9379. the limit. To get the device limit, query
  9380. info::device::max_work_group_size. Adjust the work-group size if needed.
  9381. */
  9382. {
  9383. dpct::has_capability_or_fail(stream->get_device(),
  9384. {sycl::aspect::fp16});
  9385. stream->submit([&](sycl::handler &cgh) {
  9386. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  9387. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  9388. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  9389. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  9390. cgh);
  9391. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9392. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9393. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9394. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9395. cgh.parallel_for(
  9396. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9397. [=](sycl::nd_item<3> item_ct1) {
  9398. mul_mat_q4_1<need_check>(
  9399. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9400. nrows_dst, item_ct1,
  9401. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  9402. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  9403. tile_y_qs_acc_ct1.get_pointer(),
  9404. tile_y_ds_acc_ct1.get_pointer());
  9405. });
  9406. });
  9407. }
  9408. } else {
  9409. const bool need_check = true;
  9410. /*
  9411. DPCT1049:23: The work-group size passed to the SYCL kernel may exceed
  9412. the limit. To get the device limit, query
  9413. info::device::max_work_group_size. Adjust the work-group size if needed.
  9414. */
  9415. {
  9416. dpct::has_capability_or_fail(stream->get_device(),
  9417. {sycl::aspect::fp16});
  9418. stream->submit([&](sycl::handler &cgh) {
  9419. sycl::local_accessor<int, 1> tile_x_qs_q4_1_acc_ct1(
  9420. sycl::range<1>(mmq_y * (WARP_SIZE) + +mmq_y), cgh);
  9421. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_1_acc_ct1(
  9422. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_1) + mmq_y / QI4_1),
  9423. cgh);
  9424. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9425. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9426. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9427. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9428. cgh.parallel_for(
  9429. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9430. [=](sycl::nd_item<3> item_ct1) {
  9431. mul_mat_q4_1<need_check>(
  9432. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9433. nrows_dst, item_ct1,
  9434. tile_x_qs_q4_1_acc_ct1.get_pointer(),
  9435. tile_x_dm_q4_1_acc_ct1.get_pointer(),
  9436. tile_y_qs_acc_ct1.get_pointer(),
  9437. tile_y_ds_acc_ct1.get_pointer());
  9438. });
  9439. });
  9440. }
  9441. }
  9442. }
  9443. catch (sycl::exception const &exc) {
  9444. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9445. << ", line:" << __LINE__ << std::endl;
  9446. std::exit(1);
  9447. }
  9448. static void ggml_mul_mat_q5_0_q8_1_sycl(const void *vx, const void *vy,
  9449. float *dst, const int ncols_x,
  9450. const int nrows_x, const int ncols_y,
  9451. const int nrows_y, const int nrows_dst,
  9452. dpct::queue_ptr stream) try {
  9453. int id;
  9454. SYCL_CHECK(
  9455. CHECK_TRY_ERROR(id = get_current_device_id()));
  9456. const int compute_capability = g_device_caps[id].cc;
  9457. int mmq_x, mmq_y, nwarps;
  9458. if (compute_capability >= VER_GEN13) {
  9459. mmq_x = MMQ_X_Q5_0_RDNA2;
  9460. mmq_y = MMQ_Y_Q5_0_RDNA2;
  9461. nwarps = NWARPS_Q5_0_RDNA2;
  9462. } else if (compute_capability >= VER_GEN12) {
  9463. mmq_x = MMQ_X_Q5_0_RDNA1;
  9464. mmq_y = MMQ_Y_Q5_0_RDNA1;
  9465. nwarps = NWARPS_Q5_0_RDNA1;
  9466. } else if (compute_capability >= VER_GEN9) {
  9467. mmq_x = MMQ_X_Q5_0_AMPERE;
  9468. mmq_y = MMQ_Y_Q5_0_AMPERE;
  9469. nwarps = NWARPS_Q5_0_AMPERE;
  9470. } else if (compute_capability >= VER_4VEC) {
  9471. mmq_x = MMQ_X_Q5_0_PASCAL;
  9472. mmq_y = MMQ_Y_Q5_0_PASCAL;
  9473. nwarps = NWARPS_Q5_0_PASCAL;
  9474. } else {
  9475. GGML_ASSERT(false);
  9476. }
  9477. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9478. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9479. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9480. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9481. if (nrows_x % mmq_y == 0) {
  9482. const bool need_check = false;
  9483. /*
  9484. DPCT1049:24: The work-group size passed to the SYCL kernel may exceed
  9485. the limit. To get the device limit, query
  9486. info::device::max_work_group_size. Adjust the work-group size if needed.
  9487. */
  9488. {
  9489. dpct::has_capability_or_fail(stream->get_device(),
  9490. {sycl::aspect::fp16});
  9491. stream->submit([&](sycl::handler &cgh) {
  9492. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  9493. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9494. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  9495. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  9496. cgh);
  9497. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9498. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9499. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9500. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9501. cgh.parallel_for(
  9502. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9503. [=](sycl::nd_item<3> item_ct1) {
  9504. mul_mat_q5_0<need_check>(
  9505. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9506. nrows_dst, item_ct1,
  9507. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  9508. tile_x_d_q5_0_acc_ct1.get_pointer(),
  9509. tile_y_qs_acc_ct1.get_pointer(),
  9510. tile_y_ds_acc_ct1.get_pointer());
  9511. });
  9512. });
  9513. }
  9514. } else {
  9515. const bool need_check = true;
  9516. /*
  9517. DPCT1049:25: The work-group size passed to the SYCL kernel may exceed
  9518. the limit. To get the device limit, query
  9519. info::device::max_work_group_size. Adjust the work-group size if needed.
  9520. */
  9521. {
  9522. dpct::has_capability_or_fail(stream->get_device(),
  9523. {sycl::aspect::fp16});
  9524. stream->submit([&](sycl::handler &cgh) {
  9525. sycl::local_accessor<int, 1> tile_x_ql_q5_0_acc_ct1(
  9526. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9527. sycl::local_accessor<float, 1> tile_x_d_q5_0_acc_ct1(
  9528. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_0) + mmq_y / QI5_0),
  9529. cgh);
  9530. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9531. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9532. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9533. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9534. cgh.parallel_for(
  9535. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9536. [=](sycl::nd_item<3> item_ct1) {
  9537. mul_mat_q5_0<need_check>(
  9538. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9539. nrows_dst, item_ct1,
  9540. tile_x_ql_q5_0_acc_ct1.get_pointer(),
  9541. tile_x_d_q5_0_acc_ct1.get_pointer(),
  9542. tile_y_qs_acc_ct1.get_pointer(),
  9543. tile_y_ds_acc_ct1.get_pointer());
  9544. });
  9545. });
  9546. }
  9547. }
  9548. }
  9549. catch (sycl::exception const &exc) {
  9550. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9551. << ", line:" << __LINE__ << std::endl;
  9552. std::exit(1);
  9553. }
  9554. static void ggml_mul_mat_q5_1_q8_1_sycl(const void *vx, const void *vy,
  9555. float *dst, const int ncols_x,
  9556. const int nrows_x, const int ncols_y,
  9557. const int nrows_y, const int nrows_dst,
  9558. dpct::queue_ptr stream) try {
  9559. int id;
  9560. SYCL_CHECK(
  9561. CHECK_TRY_ERROR(id = get_current_device_id()));
  9562. const int compute_capability = g_device_caps[id].cc;
  9563. int mmq_x, mmq_y, nwarps;
  9564. if (compute_capability >= VER_GEN13) {
  9565. mmq_x = MMQ_X_Q5_1_RDNA2;
  9566. mmq_y = MMQ_Y_Q5_1_RDNA2;
  9567. nwarps = NWARPS_Q5_1_RDNA2;
  9568. } else if (compute_capability >= VER_GEN12) {
  9569. mmq_x = MMQ_X_Q5_1_RDNA1;
  9570. mmq_y = MMQ_Y_Q5_1_RDNA1;
  9571. nwarps = NWARPS_Q5_1_RDNA1;
  9572. } else if (compute_capability >= VER_GEN9) {
  9573. mmq_x = MMQ_X_Q5_1_AMPERE;
  9574. mmq_y = MMQ_Y_Q5_1_AMPERE;
  9575. nwarps = NWARPS_Q5_1_AMPERE;
  9576. } else if (compute_capability >= VER_4VEC) {
  9577. mmq_x = MMQ_X_Q5_1_PASCAL;
  9578. mmq_y = MMQ_Y_Q5_1_PASCAL;
  9579. nwarps = NWARPS_Q5_1_PASCAL;
  9580. } else {
  9581. GGML_ASSERT(false);
  9582. }
  9583. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9584. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9585. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9586. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9587. if (nrows_x % mmq_y == 0) {
  9588. const bool need_check = false;
  9589. /*
  9590. DPCT1049:26: The work-group size passed to the SYCL kernel may exceed
  9591. the limit. To get the device limit, query
  9592. info::device::max_work_group_size. Adjust the work-group size if needed.
  9593. */
  9594. {
  9595. dpct::has_capability_or_fail(stream->get_device(),
  9596. {sycl::aspect::fp16});
  9597. stream->submit([&](sycl::handler &cgh) {
  9598. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  9599. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9600. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  9601. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  9602. cgh);
  9603. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9604. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9605. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9606. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9607. cgh.parallel_for(
  9608. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9609. [=](sycl::nd_item<3> item_ct1) {
  9610. mul_mat_q5_1<need_check>(
  9611. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9612. nrows_dst, item_ct1,
  9613. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  9614. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  9615. tile_y_qs_acc_ct1.get_pointer(),
  9616. tile_y_ds_acc_ct1.get_pointer());
  9617. });
  9618. });
  9619. }
  9620. } else {
  9621. const bool need_check = true;
  9622. /*
  9623. DPCT1049:27: The work-group size passed to the SYCL kernel may exceed
  9624. the limit. To get the device limit, query
  9625. info::device::max_work_group_size. Adjust the work-group size if needed.
  9626. */
  9627. {
  9628. dpct::has_capability_or_fail(stream->get_device(),
  9629. {sycl::aspect::fp16});
  9630. stream->submit([&](sycl::handler &cgh) {
  9631. sycl::local_accessor<int, 1> tile_x_ql_q5_1_acc_ct1(
  9632. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  9633. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_1_acc_ct1(
  9634. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_1) + mmq_y / QI5_1),
  9635. cgh);
  9636. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9637. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9638. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9639. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9640. cgh.parallel_for(
  9641. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9642. [=](sycl::nd_item<3> item_ct1) {
  9643. mul_mat_q5_1<need_check>(
  9644. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9645. nrows_dst, item_ct1,
  9646. tile_x_ql_q5_1_acc_ct1.get_pointer(),
  9647. tile_x_dm_q5_1_acc_ct1.get_pointer(),
  9648. tile_y_qs_acc_ct1.get_pointer(),
  9649. tile_y_ds_acc_ct1.get_pointer());
  9650. });
  9651. });
  9652. }
  9653. }
  9654. }
  9655. catch (sycl::exception const &exc) {
  9656. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9657. << ", line:" << __LINE__ << std::endl;
  9658. std::exit(1);
  9659. }
  9660. static void ggml_mul_mat_q8_0_q8_1_sycl(const void *vx, const void *vy,
  9661. float *dst, const int ncols_x,
  9662. const int nrows_x, const int ncols_y,
  9663. const int nrows_y, const int nrows_dst,
  9664. dpct::queue_ptr stream) try {
  9665. int id;
  9666. SYCL_CHECK(
  9667. CHECK_TRY_ERROR(id = get_current_device_id()));
  9668. const int compute_capability = g_device_caps[id].cc;
  9669. int mmq_x, mmq_y, nwarps;
  9670. if (compute_capability >= VER_GEN13) {
  9671. mmq_x = MMQ_X_Q8_0_RDNA2;
  9672. mmq_y = MMQ_Y_Q8_0_RDNA2;
  9673. nwarps = NWARPS_Q8_0_RDNA2;
  9674. } else if (compute_capability >= VER_GEN12) {
  9675. mmq_x = MMQ_X_Q8_0_RDNA1;
  9676. mmq_y = MMQ_Y_Q8_0_RDNA1;
  9677. nwarps = NWARPS_Q8_0_RDNA1;
  9678. } else if (compute_capability >= VER_GEN9) {
  9679. mmq_x = MMQ_X_Q8_0_AMPERE;
  9680. mmq_y = MMQ_Y_Q8_0_AMPERE;
  9681. nwarps = NWARPS_Q8_0_AMPERE;
  9682. } else if (compute_capability >= VER_4VEC) {
  9683. mmq_x = MMQ_X_Q8_0_PASCAL;
  9684. mmq_y = MMQ_Y_Q8_0_PASCAL;
  9685. nwarps = NWARPS_Q8_0_PASCAL;
  9686. } else {
  9687. GGML_ASSERT(false);
  9688. }
  9689. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9690. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9691. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9692. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9693. if (nrows_x % mmq_y == 0) {
  9694. const bool need_check = false;
  9695. /*
  9696. DPCT1049:28: The work-group size passed to the SYCL kernel may exceed
  9697. the limit. To get the device limit, query
  9698. info::device::max_work_group_size. Adjust the work-group size if needed.
  9699. */
  9700. {
  9701. dpct::has_capability_or_fail(stream->get_device(),
  9702. {sycl::aspect::fp16});
  9703. stream->submit([&](sycl::handler &cgh) {
  9704. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  9705. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9706. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  9707. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  9708. cgh);
  9709. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9710. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9711. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9712. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9713. cgh.parallel_for(
  9714. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9715. [=](sycl::nd_item<3> item_ct1) {
  9716. mul_mat_q8_0<need_check>(
  9717. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9718. nrows_dst, item_ct1,
  9719. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  9720. tile_x_d_q8_0_acc_ct1.get_pointer(),
  9721. tile_y_qs_acc_ct1.get_pointer(),
  9722. tile_y_ds_acc_ct1.get_pointer());
  9723. });
  9724. });
  9725. }
  9726. } else {
  9727. const bool need_check = true;
  9728. /*
  9729. DPCT1049:29: The work-group size passed to the SYCL kernel may exceed
  9730. the limit. To get the device limit, query
  9731. info::device::max_work_group_size. Adjust the work-group size if needed.
  9732. */
  9733. {
  9734. dpct::has_capability_or_fail(stream->get_device(),
  9735. {sycl::aspect::fp16});
  9736. stream->submit([&](sycl::handler &cgh) {
  9737. sycl::local_accessor<int, 1> tile_x_qs_q8_0_acc_ct1(
  9738. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9739. sycl::local_accessor<float, 1> tile_x_d_q8_0_acc_ct1(
  9740. sycl::range<1>(mmq_y * (WARP_SIZE / QI8_0) + mmq_y / QI8_0),
  9741. cgh);
  9742. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9743. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9744. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9745. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9746. cgh.parallel_for(
  9747. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9748. [=](sycl::nd_item<3> item_ct1) {
  9749. mul_mat_q8_0<need_check>(
  9750. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9751. nrows_dst, item_ct1,
  9752. tile_x_qs_q8_0_acc_ct1.get_pointer(),
  9753. tile_x_d_q8_0_acc_ct1.get_pointer(),
  9754. tile_y_qs_acc_ct1.get_pointer(),
  9755. tile_y_ds_acc_ct1.get_pointer());
  9756. });
  9757. });
  9758. }
  9759. }
  9760. }
  9761. catch (sycl::exception const &exc) {
  9762. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9763. << ", line:" << __LINE__ << std::endl;
  9764. std::exit(1);
  9765. }
  9766. static void ggml_mul_mat_q2_K_q8_1_sycl(const void *vx, const void *vy,
  9767. float *dst, const int ncols_x,
  9768. const int nrows_x, const int ncols_y,
  9769. const int nrows_y, const int nrows_dst,
  9770. dpct::queue_ptr stream) try {
  9771. int id;
  9772. SYCL_CHECK(
  9773. CHECK_TRY_ERROR(id = get_current_device_id()));
  9774. const int compute_capability = g_device_caps[id].cc;
  9775. int mmq_x, mmq_y, nwarps;
  9776. if (compute_capability >= VER_GEN13) {
  9777. mmq_x = MMQ_X_Q2_K_RDNA2;
  9778. mmq_y = MMQ_Y_Q2_K_RDNA2;
  9779. nwarps = NWARPS_Q2_K_RDNA2;
  9780. } else if (compute_capability >= VER_GEN12) {
  9781. mmq_x = MMQ_X_Q2_K_RDNA1;
  9782. mmq_y = MMQ_Y_Q2_K_RDNA1;
  9783. nwarps = NWARPS_Q2_K_RDNA1;
  9784. } else if (compute_capability >= VER_GEN9) {
  9785. mmq_x = MMQ_X_Q2_K_AMPERE;
  9786. mmq_y = MMQ_Y_Q2_K_AMPERE;
  9787. nwarps = NWARPS_Q2_K_AMPERE;
  9788. } else if (compute_capability >= VER_4VEC) {
  9789. mmq_x = MMQ_X_Q2_K_PASCAL;
  9790. mmq_y = MMQ_Y_Q2_K_PASCAL;
  9791. nwarps = NWARPS_Q2_K_PASCAL;
  9792. } else {
  9793. GGML_ASSERT(false);
  9794. }
  9795. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9796. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9797. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9798. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9799. if (nrows_x % mmq_y == 0) {
  9800. const bool need_check = false;
  9801. /*
  9802. DPCT1049:30: The work-group size passed to the SYCL kernel may exceed
  9803. the limit. To get the device limit, query
  9804. info::device::max_work_group_size. Adjust the work-group size if needed.
  9805. */
  9806. {
  9807. dpct::has_capability_or_fail(stream->get_device(),
  9808. {sycl::aspect::fp16});
  9809. stream->submit([&](sycl::handler &cgh) {
  9810. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  9811. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9812. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  9813. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  9814. cgh);
  9815. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  9816. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  9817. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9818. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9819. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9820. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9821. cgh.parallel_for(
  9822. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9823. [=](sycl::nd_item<3> item_ct1) {
  9824. mul_mat_q2_K<need_check>(
  9825. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9826. nrows_dst, item_ct1,
  9827. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  9828. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  9829. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  9830. tile_y_qs_acc_ct1.get_pointer(),
  9831. tile_y_ds_acc_ct1.get_pointer());
  9832. });
  9833. });
  9834. }
  9835. } else {
  9836. const bool need_check = true;
  9837. /*
  9838. DPCT1049:31: The work-group size passed to the SYCL kernel may exceed
  9839. the limit. To get the device limit, query
  9840. info::device::max_work_group_size. Adjust the work-group size if needed.
  9841. */
  9842. {
  9843. dpct::has_capability_or_fail(stream->get_device(),
  9844. {sycl::aspect::fp16});
  9845. stream->submit([&](sycl::handler &cgh) {
  9846. sycl::local_accessor<int, 1> tile_x_ql_q2_K_acc_ct1(
  9847. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9848. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q2_K_acc_ct1(
  9849. sycl::range<1>(mmq_y * (WARP_SIZE / QI2_K) + mmq_y / QI2_K),
  9850. cgh);
  9851. sycl::local_accessor<int, 1> tile_x_sc_q2_K_acc_ct1(
  9852. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  9853. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9854. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9855. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9856. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9857. cgh.parallel_for(
  9858. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9859. [=](sycl::nd_item<3> item_ct1) {
  9860. mul_mat_q2_K<need_check>(
  9861. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9862. nrows_dst, item_ct1,
  9863. tile_x_ql_q2_K_acc_ct1.get_pointer(),
  9864. tile_x_dm_q2_K_acc_ct1.get_pointer(),
  9865. tile_x_sc_q2_K_acc_ct1.get_pointer(),
  9866. tile_y_qs_acc_ct1.get_pointer(),
  9867. tile_y_ds_acc_ct1.get_pointer());
  9868. });
  9869. });
  9870. }
  9871. }
  9872. }
  9873. catch (sycl::exception const &exc) {
  9874. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9875. << ", line:" << __LINE__ << std::endl;
  9876. std::exit(1);
  9877. }
  9878. static void ggml_mul_mat_q3_K_q8_1_sycl(const void *vx, const void *vy,
  9879. float *dst, const int ncols_x,
  9880. const int nrows_x, const int ncols_y,
  9881. const int nrows_y, const int nrows_dst,
  9882. dpct::queue_ptr stream) try {
  9883. #if QK_K == 256
  9884. int id;
  9885. SYCL_CHECK(
  9886. CHECK_TRY_ERROR(id = get_current_device_id()));
  9887. const int compute_capability = g_device_caps[id].cc;
  9888. int mmq_x, mmq_y, nwarps;
  9889. if (compute_capability >= VER_GEN13) {
  9890. mmq_x = MMQ_X_Q3_K_RDNA2;
  9891. mmq_y = MMQ_Y_Q3_K_RDNA2;
  9892. nwarps = NWARPS_Q3_K_RDNA2;
  9893. } else if (compute_capability >= VER_GEN12) {
  9894. mmq_x = MMQ_X_Q3_K_RDNA1;
  9895. mmq_y = MMQ_Y_Q3_K_RDNA1;
  9896. nwarps = NWARPS_Q3_K_RDNA1;
  9897. } else if (compute_capability >= VER_GEN9) {
  9898. mmq_x = MMQ_X_Q3_K_AMPERE;
  9899. mmq_y = MMQ_Y_Q3_K_AMPERE;
  9900. nwarps = NWARPS_Q3_K_AMPERE;
  9901. } else if (compute_capability >= VER_4VEC) {
  9902. mmq_x = MMQ_X_Q3_K_PASCAL;
  9903. mmq_y = MMQ_Y_Q3_K_PASCAL;
  9904. nwarps = NWARPS_Q3_K_PASCAL;
  9905. } else {
  9906. GGML_ASSERT(false);
  9907. }
  9908. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  9909. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  9910. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  9911. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  9912. if (nrows_x % mmq_y == 0) {
  9913. const bool need_check = false;
  9914. /*
  9915. DPCT1049:32: The work-group size passed to the SYCL kernel may exceed
  9916. the limit. To get the device limit, query
  9917. info::device::max_work_group_size. Adjust the work-group size if needed.
  9918. */
  9919. {
  9920. dpct::has_capability_or_fail(stream->get_device(),
  9921. {sycl::aspect::fp16});
  9922. stream->submit([&](sycl::handler &cgh) {
  9923. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  9924. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9925. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  9926. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  9927. cgh);
  9928. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  9929. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  9930. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  9931. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  9932. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9933. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9934. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9935. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9936. cgh.parallel_for(
  9937. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9938. [=](sycl::nd_item<3> item_ct1) {
  9939. mul_mat_q3_K<need_check>(
  9940. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9941. nrows_dst, item_ct1,
  9942. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  9943. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  9944. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  9945. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  9946. tile_y_qs_acc_ct1.get_pointer(),
  9947. tile_y_ds_acc_ct1.get_pointer());
  9948. });
  9949. });
  9950. }
  9951. } else {
  9952. const bool need_check = true;
  9953. /*
  9954. DPCT1049:33: The work-group size passed to the SYCL kernel may exceed
  9955. the limit. To get the device limit, query
  9956. info::device::max_work_group_size. Adjust the work-group size if needed.
  9957. */
  9958. {
  9959. dpct::has_capability_or_fail(stream->get_device(),
  9960. {sycl::aspect::fp16});
  9961. stream->submit([&](sycl::handler &cgh) {
  9962. sycl::local_accessor<int, 1> tile_x_ql_q3_K_acc_ct1(
  9963. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  9964. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q3_K_acc_ct1(
  9965. sycl::range<1>(mmq_y * (WARP_SIZE / QI3_K) + mmq_y / QI3_K),
  9966. cgh);
  9967. sycl::local_accessor<int, 1> tile_x_qh_q3_K_acc_ct1(
  9968. sycl::range<1>(mmq_y * (WARP_SIZE / 2) + mmq_y / 2), cgh);
  9969. sycl::local_accessor<int, 1> tile_x_sc_q3_K_acc_ct1(
  9970. sycl::range<1>(mmq_y * (WARP_SIZE / 4) + mmq_y / 4), cgh);
  9971. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  9972. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  9973. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  9974. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  9975. cgh.parallel_for(
  9976. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  9977. [=](sycl::nd_item<3> item_ct1) {
  9978. mul_mat_q3_K<need_check>(
  9979. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  9980. nrows_dst, item_ct1,
  9981. tile_x_ql_q3_K_acc_ct1.get_pointer(),
  9982. tile_x_dm_q3_K_acc_ct1.get_pointer(),
  9983. tile_x_qh_q3_K_acc_ct1.get_pointer(),
  9984. tile_x_sc_q3_K_acc_ct1.get_pointer(),
  9985. tile_y_qs_acc_ct1.get_pointer(),
  9986. tile_y_ds_acc_ct1.get_pointer());
  9987. });
  9988. });
  9989. }
  9990. }
  9991. #endif
  9992. }
  9993. catch (sycl::exception const &exc) {
  9994. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  9995. << ", line:" << __LINE__ << std::endl;
  9996. std::exit(1);
  9997. }
  9998. static void ggml_mul_mat_q4_K_q8_1_sycl(const void *vx, const void *vy,
  9999. float *dst, const int ncols_x,
  10000. const int nrows_x, const int ncols_y,
  10001. const int nrows_y, const int nrows_dst,
  10002. dpct::queue_ptr stream) try {
  10003. int id;
  10004. SYCL_CHECK(
  10005. CHECK_TRY_ERROR(id = get_current_device_id()));
  10006. const int compute_capability = g_device_caps[id].cc;
  10007. int mmq_x, mmq_y, nwarps;
  10008. if (compute_capability >= VER_GEN13) {
  10009. mmq_x = MMQ_X_Q4_K_RDNA2;
  10010. mmq_y = MMQ_Y_Q4_K_RDNA2;
  10011. nwarps = NWARPS_Q4_K_RDNA2;
  10012. } else if (compute_capability >= VER_GEN12) {
  10013. mmq_x = MMQ_X_Q4_K_RDNA1;
  10014. mmq_y = MMQ_Y_Q4_K_RDNA1;
  10015. nwarps = NWARPS_Q4_K_RDNA1;
  10016. } else if (compute_capability >= VER_GEN9) {
  10017. mmq_x = MMQ_X_Q4_K_AMPERE;
  10018. mmq_y = MMQ_Y_Q4_K_AMPERE;
  10019. nwarps = NWARPS_Q4_K_AMPERE;
  10020. } else if (compute_capability >= VER_4VEC) {
  10021. mmq_x = MMQ_X_Q4_K_PASCAL;
  10022. mmq_y = MMQ_Y_Q4_K_PASCAL;
  10023. nwarps = NWARPS_Q4_K_PASCAL;
  10024. } else {
  10025. GGML_ASSERT(false);
  10026. }
  10027. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10028. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10029. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10030. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10031. if (nrows_x % mmq_y == 0) {
  10032. const bool need_check = false;
  10033. /*
  10034. DPCT1049:34: The work-group size passed to the SYCL kernel may exceed
  10035. the limit. To get the device limit, query
  10036. info::device::max_work_group_size. Adjust the work-group size if needed.
  10037. */
  10038. {
  10039. dpct::has_capability_or_fail(stream->get_device(),
  10040. {sycl::aspect::fp16});
  10041. stream->submit([&](sycl::handler &cgh) {
  10042. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  10043. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10044. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  10045. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  10046. cgh);
  10047. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  10048. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10049. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10050. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10051. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10052. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10053. cgh.parallel_for(
  10054. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10055. [=](sycl::nd_item<3> item_ct1) {
  10056. mul_mat_q4_K<need_check>(
  10057. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10058. nrows_dst, item_ct1,
  10059. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  10060. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  10061. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  10062. tile_y_qs_acc_ct1.get_pointer(),
  10063. tile_y_ds_acc_ct1.get_pointer());
  10064. });
  10065. });
  10066. }
  10067. } else {
  10068. const bool need_check = true;
  10069. /*
  10070. DPCT1049:35: The work-group size passed to the SYCL kernel may exceed
  10071. the limit. To get the device limit, query
  10072. info::device::max_work_group_size. Adjust the work-group size if needed.
  10073. */
  10074. {
  10075. dpct::has_capability_or_fail(stream->get_device(),
  10076. {sycl::aspect::fp16});
  10077. stream->submit([&](sycl::handler &cgh) {
  10078. sycl::local_accessor<int, 1> tile_x_ql_q4_K_acc_ct1(
  10079. sycl::range<1>(mmq_y * (WARP_SIZE) + mmq_y), cgh);
  10080. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q4_K_acc_ct1(
  10081. sycl::range<1>(mmq_y * (WARP_SIZE / QI4_K) + mmq_y / QI4_K),
  10082. cgh);
  10083. sycl::local_accessor<int, 1> tile_x_sc_q4_K_acc_ct1(
  10084. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10085. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10086. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10087. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10088. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10089. cgh.parallel_for(
  10090. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10091. [=](sycl::nd_item<3> item_ct1) {
  10092. mul_mat_q4_K<need_check>(
  10093. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10094. nrows_dst, item_ct1,
  10095. tile_x_ql_q4_K_acc_ct1.get_pointer(),
  10096. tile_x_dm_q4_K_acc_ct1.get_pointer(),
  10097. tile_x_sc_q4_K_acc_ct1.get_pointer(),
  10098. tile_y_qs_acc_ct1.get_pointer(),
  10099. tile_y_ds_acc_ct1.get_pointer());
  10100. });
  10101. });
  10102. }
  10103. }
  10104. }
  10105. catch (sycl::exception const &exc) {
  10106. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10107. << ", line:" << __LINE__ << std::endl;
  10108. std::exit(1);
  10109. }
  10110. static void ggml_mul_mat_q5_K_q8_1_sycl(const void *vx, const void *vy,
  10111. float *dst, const int ncols_x,
  10112. const int nrows_x, const int ncols_y,
  10113. const int nrows_y, const int nrows_dst,
  10114. dpct::queue_ptr stream) try {
  10115. int id;
  10116. SYCL_CHECK(
  10117. CHECK_TRY_ERROR(id = get_current_device_id()));
  10118. const int compute_capability = g_device_caps[id].cc;
  10119. int mmq_x, mmq_y, nwarps;
  10120. if (compute_capability >= VER_GEN13) {
  10121. mmq_x = MMQ_X_Q5_K_RDNA2;
  10122. mmq_y = MMQ_Y_Q5_K_RDNA2;
  10123. nwarps = NWARPS_Q5_K_RDNA2;
  10124. } else if (compute_capability >= VER_GEN12) {
  10125. mmq_x = MMQ_X_Q5_K_RDNA1;
  10126. mmq_y = MMQ_Y_Q5_K_RDNA1;
  10127. nwarps = NWARPS_Q5_K_RDNA1;
  10128. } else if (compute_capability >= VER_GEN9) {
  10129. mmq_x = MMQ_X_Q5_K_AMPERE;
  10130. mmq_y = MMQ_Y_Q5_K_AMPERE;
  10131. nwarps = NWARPS_Q5_K_AMPERE;
  10132. } else if (compute_capability >= VER_4VEC) {
  10133. mmq_x = MMQ_X_Q5_K_PASCAL;
  10134. mmq_y = MMQ_Y_Q5_K_PASCAL;
  10135. nwarps = NWARPS_Q5_K_PASCAL;
  10136. } else {
  10137. GGML_ASSERT(false);
  10138. }
  10139. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10140. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10141. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10142. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10143. if (nrows_x % mmq_y == 0) {
  10144. const bool need_check = false;
  10145. /*
  10146. DPCT1049:36: The work-group size passed to the SYCL kernel may exceed
  10147. the limit. To get the device limit, query
  10148. info::device::max_work_group_size. Adjust the work-group size if needed.
  10149. */
  10150. {
  10151. dpct::has_capability_or_fail(stream->get_device(),
  10152. {sycl::aspect::fp16});
  10153. stream->submit([&](sycl::handler &cgh) {
  10154. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  10155. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10156. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  10157. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  10158. cgh);
  10159. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  10160. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10161. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10162. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10163. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10164. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10165. cgh.parallel_for(
  10166. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10167. [=](sycl::nd_item<3> item_ct1) {
  10168. mul_mat_q5_K<need_check>(
  10169. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10170. nrows_dst, item_ct1,
  10171. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  10172. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  10173. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  10174. tile_y_qs_acc_ct1.get_pointer(),
  10175. tile_y_ds_acc_ct1.get_pointer());
  10176. });
  10177. });
  10178. }
  10179. } else {
  10180. const bool need_check = true;
  10181. /*
  10182. DPCT1049:37: The work-group size passed to the SYCL kernel may exceed
  10183. the limit. To get the device limit, query
  10184. info::device::max_work_group_size. Adjust the work-group size if needed.
  10185. */
  10186. {
  10187. dpct::has_capability_or_fail(stream->get_device(),
  10188. {sycl::aspect::fp16});
  10189. stream->submit([&](sycl::handler &cgh) {
  10190. sycl::local_accessor<int, 1> tile_x_ql_q5_K_acc_ct1(
  10191. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10192. sycl::local_accessor<sycl::half2, 1> tile_x_dm_q5_K_acc_ct1(
  10193. sycl::range<1>(mmq_y * (WARP_SIZE / QI5_K) + mmq_y / QI5_K),
  10194. cgh);
  10195. sycl::local_accessor<int, 1> tile_x_sc_q5_K_acc_ct1(
  10196. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10197. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10198. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10199. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10200. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10201. cgh.parallel_for(
  10202. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10203. [=](sycl::nd_item<3> item_ct1) {
  10204. mul_mat_q5_K<need_check>(
  10205. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10206. nrows_dst, item_ct1,
  10207. tile_x_ql_q5_K_acc_ct1.get_pointer(),
  10208. tile_x_dm_q5_K_acc_ct1.get_pointer(),
  10209. tile_x_sc_q5_K_acc_ct1.get_pointer(),
  10210. tile_y_qs_acc_ct1.get_pointer(),
  10211. tile_y_ds_acc_ct1.get_pointer());
  10212. });
  10213. });
  10214. }
  10215. }
  10216. }
  10217. catch (sycl::exception const &exc) {
  10218. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10219. << ", line:" << __LINE__ << std::endl;
  10220. std::exit(1);
  10221. }
  10222. static void ggml_mul_mat_q6_K_q8_1_sycl(const void *vx, const void *vy,
  10223. float *dst, const int ncols_x,
  10224. const int nrows_x, const int ncols_y,
  10225. const int nrows_y, const int nrows_dst,
  10226. dpct::queue_ptr stream) try {
  10227. int id;
  10228. SYCL_CHECK(
  10229. CHECK_TRY_ERROR(id = get_current_device_id()));
  10230. const int compute_capability = g_device_caps[id].cc;
  10231. int mmq_x, mmq_y, nwarps;
  10232. if (compute_capability >= VER_GEN13) {
  10233. mmq_x = MMQ_X_Q6_K_RDNA2;
  10234. mmq_y = MMQ_Y_Q6_K_RDNA2;
  10235. nwarps = NWARPS_Q6_K_RDNA2;
  10236. } else if (compute_capability >= VER_GEN12) {
  10237. mmq_x = MMQ_X_Q6_K_RDNA1;
  10238. mmq_y = MMQ_Y_Q6_K_RDNA1;
  10239. nwarps = NWARPS_Q6_K_RDNA1;
  10240. } else if (compute_capability >= VER_GEN9) {
  10241. mmq_x = MMQ_X_Q6_K_AMPERE;
  10242. mmq_y = MMQ_Y_Q6_K_AMPERE;
  10243. nwarps = NWARPS_Q6_K_AMPERE;
  10244. } else if (compute_capability >= VER_4VEC) {
  10245. mmq_x = MMQ_X_Q6_K_PASCAL;
  10246. mmq_y = MMQ_Y_Q6_K_PASCAL;
  10247. nwarps = NWARPS_Q6_K_PASCAL;
  10248. } else {
  10249. GGML_ASSERT(false);
  10250. }
  10251. const int block_num_x = (nrows_x + mmq_y - 1) / mmq_y;
  10252. const int block_num_y = (ncols_y + mmq_x - 1) / mmq_x;
  10253. const sycl::range<3> block_nums(1, block_num_y, block_num_x);
  10254. const sycl::range<3> block_dims(1, nwarps, WARP_SIZE);
  10255. if (nrows_x % mmq_y == 0) {
  10256. const bool need_check = false;
  10257. /*
  10258. DPCT1049:38: The work-group size passed to the SYCL kernel may exceed
  10259. the limit. To get the device limit, query
  10260. info::device::max_work_group_size. Adjust the work-group size if needed.
  10261. */
  10262. {
  10263. dpct::has_capability_or_fail(stream->get_device(),
  10264. {sycl::aspect::fp16});
  10265. stream->submit([&](sycl::handler &cgh) {
  10266. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  10267. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10268. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  10269. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  10270. cgh);
  10271. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  10272. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10273. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10274. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10275. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10276. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10277. cgh.parallel_for(
  10278. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10279. [=](sycl::nd_item<3> item_ct1) {
  10280. mul_mat_q6_K<need_check>(
  10281. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10282. nrows_dst, item_ct1,
  10283. tile_x_ql_acc_ct1.get_pointer(),
  10284. tile_x_dm_acc_ct1.get_pointer(),
  10285. tile_x_sc_acc_ct1.get_pointer(),
  10286. tile_y_qs_acc_ct1.get_pointer(),
  10287. tile_y_ds_acc_ct1.get_pointer());
  10288. });
  10289. });
  10290. }
  10291. } else {
  10292. const bool need_check = true;
  10293. /*
  10294. DPCT1049:39: The work-group size passed to the SYCL kernel may exceed
  10295. the limit. To get the device limit, query
  10296. info::device::max_work_group_size. Adjust the work-group size if needed.
  10297. */
  10298. {
  10299. dpct::has_capability_or_fail(stream->get_device(),
  10300. {sycl::aspect::fp16});
  10301. stream->submit([&](sycl::handler &cgh) {
  10302. sycl::local_accessor<int, 1> tile_x_ql_acc_ct1(
  10303. sycl::range<1>(mmq_y * (2 * WARP_SIZE) + mmq_y), cgh);
  10304. sycl::local_accessor<sycl::half2, 1> tile_x_dm_acc_ct1(
  10305. sycl::range<1>(mmq_y * (WARP_SIZE / QI6_K) + mmq_y / QI6_K),
  10306. cgh);
  10307. sycl::local_accessor<int, 1> tile_x_sc_acc_ct1(
  10308. sycl::range<1>(mmq_y * (WARP_SIZE / 8) + mmq_y / 8), cgh);
  10309. sycl::local_accessor<int, 1> tile_y_qs_acc_ct1(
  10310. sycl::range<1>(mmq_x * WARP_SIZE), cgh);
  10311. sycl::local_accessor<sycl::half2, 1> tile_y_ds_acc_ct1(
  10312. sycl::range<1>(mmq_x * WARP_SIZE / QI8_1), cgh);
  10313. cgh.parallel_for(
  10314. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10315. [=](sycl::nd_item<3> item_ct1) {
  10316. mul_mat_q6_K<need_check>(
  10317. vx, vy, dst, ncols_x, nrows_x, ncols_y, nrows_y,
  10318. nrows_dst, item_ct1,
  10319. tile_x_ql_acc_ct1.get_pointer(),
  10320. tile_x_dm_acc_ct1.get_pointer(),
  10321. tile_x_sc_acc_ct1.get_pointer(),
  10322. tile_y_qs_acc_ct1.get_pointer(),
  10323. tile_y_ds_acc_ct1.get_pointer());
  10324. });
  10325. });
  10326. }
  10327. }
  10328. }
  10329. catch (sycl::exception const &exc) {
  10330. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10331. << ", line:" << __LINE__ << std::endl;
  10332. std::exit(1);
  10333. }
  10334. static void ggml_mul_mat_p021_f16_f32_sycl(const void *vx, const float *y,
  10335. float *dst, const int ncols_x,
  10336. const int nrows_x,
  10337. const int nchannels_x,
  10338. const int nchannels_y,
  10339. dpct::queue_ptr stream) {
  10340. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  10341. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10342. {
  10343. dpct::has_capability_or_fail(stream->get_device(),
  10344. {sycl::aspect::fp16});
  10345. stream->parallel_for(
  10346. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10347. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10348. mul_mat_p021_f16_f32(vx, y, dst, ncols_x, nrows_x, nchannels_x,
  10349. nchannels_y, item_ct1);
  10350. });
  10351. }
  10352. }
  10353. static void ggml_mul_mat_vec_nc_f16_f32_sycl(
  10354. const void *vx, const float *y, float *dst, const int ncols_x,
  10355. const int nrows_x, const int row_stride_x, const int nchannels_x,
  10356. const int nchannels_y, const int channel_stride_x, dpct::queue_ptr stream) {
  10357. const sycl::range<3> block_nums(nchannels_y, nrows_x, 1);
  10358. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10359. {
  10360. dpct::has_capability_or_fail(stream->get_device(),
  10361. {sycl::aspect::fp16});
  10362. stream->parallel_for(
  10363. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10364. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10365. mul_mat_vec_nc_f16_f32(vx, y, dst, ncols_x, nrows_x,
  10366. row_stride_x, channel_stride_x,
  10367. nchannels_y / nchannels_x, item_ct1);
  10368. });
  10369. }
  10370. }
  10371. static void
  10372. ggml_cpy_f16_f32_sycl(const char *cx, char *cdst, const int ne, const int ne00,
  10373. const int ne01, const int ne02, const int nb00,
  10374. const int nb01, const int nb02, const int nb03,
  10375. const int ne10, const int ne11, const int ne12,
  10376. const int nb10, const int nb11, const int nb12,
  10377. const int nb13, dpct::queue_ptr stream) {
  10378. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10379. {
  10380. dpct::has_capability_or_fail(stream->get_device(),
  10381. {sycl::aspect::fp16});
  10382. stream->parallel_for(
  10383. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10384. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10385. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10386. [=](sycl::nd_item<3> item_ct1) {
  10387. cpy_f32_f16<cpy_1_f16_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00,
  10388. nb01, nb02, nb03, ne10, ne11, ne12,
  10389. nb10, nb11, nb12, nb13, item_ct1);
  10390. });
  10391. }
  10392. }
  10393. static void ggml_cpy_f32_f32_sycl(const char *cx, char *cdst, const int ne,
  10394. const int ne00, const int ne01,
  10395. const int ne02, const int nb00,
  10396. const int nb01, const int nb02,
  10397. const int nb03, const int ne10,
  10398. const int ne11, const int ne12,
  10399. const int nb10, const int nb11,
  10400. const int nb12, const int nb13,
  10401. dpct::queue_ptr stream) {
  10402. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10403. {
  10404. dpct::has_capability_or_fail(stream->get_device(),
  10405. {sycl::aspect::fp16});
  10406. stream->parallel_for(
  10407. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10408. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10409. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10410. [=](sycl::nd_item<3> item_ct1) {
  10411. cpy_f32_f16<cpy_1_f32_f32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10412. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10413. item_ct1);
  10414. });
  10415. }
  10416. }
  10417. static void ggml_cpy_f32_f16_sycl(const char *cx, char *cdst, const int ne,
  10418. const int ne00, const int ne01,
  10419. const int ne02, const int nb00,
  10420. const int nb01, const int nb02,
  10421. const int nb03, const int ne10,
  10422. const int ne11, const int ne12,
  10423. const int nb10, const int nb11,
  10424. const int nb12, const int nb13,
  10425. dpct::queue_ptr stream) {
  10426. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10427. {
  10428. dpct::has_capability_or_fail(stream->get_device(),
  10429. {sycl::aspect::fp16});
  10430. stream->parallel_for(
  10431. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10432. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10433. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10434. [=](sycl::nd_item<3> item_ct1) {
  10435. cpy_f32_f16<cpy_1_f32_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10436. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10437. item_ct1);
  10438. });
  10439. }
  10440. }
  10441. static void ggml_cpy_f32_q8_0_sycl(const char *cx, char *cdst, const int ne,
  10442. const int ne00, const int ne01,
  10443. const int ne02, const int nb00,
  10444. const int nb01, const int nb02,
  10445. const int nb03, const int ne10,
  10446. const int ne11, const int ne12,
  10447. const int nb10, const int nb11,
  10448. const int nb12, const int nb13,
  10449. dpct::queue_ptr stream) {
  10450. GGML_ASSERT(ne % QK8_0 == 0);
  10451. const int num_blocks = ne / QK8_0;
  10452. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10453. sycl::range<3>(1, 1, 1)),
  10454. [=](sycl::nd_item<3> item_ct1) {
  10455. cpy_f32_q<cpy_blck_f32_q8_0, QK8_0>(
  10456. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10457. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10458. item_ct1);
  10459. });
  10460. }
  10461. static void ggml_cpy_f32_q4_0_sycl(const char *cx, char *cdst, const int ne,
  10462. const int ne00, const int ne01,
  10463. const int ne02, const int nb00,
  10464. const int nb01, const int nb02,
  10465. const int nb03, const int ne10,
  10466. const int ne11, const int ne12,
  10467. const int nb10, const int nb11,
  10468. const int nb12, const int nb13,
  10469. dpct::queue_ptr stream) {
  10470. GGML_ASSERT(ne % QK4_0 == 0);
  10471. const int num_blocks = ne / QK4_0;
  10472. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10473. sycl::range<3>(1, 1, 1)),
  10474. [=](sycl::nd_item<3> item_ct1) {
  10475. cpy_f32_q<cpy_blck_f32_q4_0, QK4_0>(
  10476. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10477. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10478. item_ct1);
  10479. });
  10480. }
  10481. static void ggml_cpy_f32_q4_1_sycl(const char *cx, char *cdst, const int ne,
  10482. const int ne00, const int ne01,
  10483. const int ne02, const int nb00,
  10484. const int nb01, const int nb02,
  10485. const int nb03, const int ne10,
  10486. const int ne11, const int ne12,
  10487. const int nb10, const int nb11,
  10488. const int nb12, const int nb13,
  10489. dpct::queue_ptr stream) {
  10490. GGML_ASSERT(ne % QK4_1 == 0);
  10491. const int num_blocks = ne / QK4_1;
  10492. stream->parallel_for(sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks),
  10493. sycl::range<3>(1, 1, 1)),
  10494. [=](sycl::nd_item<3> item_ct1) {
  10495. cpy_f32_q<cpy_blck_f32_q4_1, QK4_1>(
  10496. cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10497. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10498. item_ct1);
  10499. });
  10500. }
  10501. static void ggml_cpy_f16_f16_sycl(const char *cx, char *cdst, const int ne,
  10502. const int ne00, const int ne01,
  10503. const int ne02, const int nb00,
  10504. const int nb01, const int nb02,
  10505. const int nb03, const int ne10,
  10506. const int ne11, const int ne12,
  10507. const int nb10, const int nb11,
  10508. const int nb12, const int nb13,
  10509. dpct::queue_ptr stream) {
  10510. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10511. {
  10512. dpct::has_capability_or_fail(stream->get_device(),
  10513. {sycl::aspect::fp16});
  10514. stream->parallel_for(
  10515. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10516. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10517. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10518. [=](sycl::nd_item<3> item_ct1) {
  10519. cpy_f32_f16<cpy_1_f16_f16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10520. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10521. item_ct1);
  10522. });
  10523. }
  10524. }
  10525. static void ggml_cpy_i16_i16_sycl(const char *cx, char *cdst, const int ne,
  10526. const int ne00, const int ne01,
  10527. const int ne02, const int nb00,
  10528. const int nb01, const int nb02,
  10529. const int nb03, const int ne10,
  10530. const int ne11, const int ne12,
  10531. const int nb10, const int nb11,
  10532. const int nb12, const int nb13,
  10533. dpct::queue_ptr stream) {
  10534. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10535. {
  10536. // dpct::has_capability_or_fail(stream->get_device(),
  10537. // {sycl::aspect::fp16});
  10538. stream->parallel_for(
  10539. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10540. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10541. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10542. [=](sycl::nd_item<3> item_ct1) {
  10543. cpy_f32_f16<cpy_1_i16_i16>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10544. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10545. item_ct1);
  10546. });
  10547. }
  10548. }
  10549. static void ggml_cpy_i32_i32_sycl(const char *cx, char *cdst, const int ne,
  10550. const int ne00, const int ne01,
  10551. const int ne02, const int nb00,
  10552. const int nb01, const int nb02,
  10553. const int nb03, const int ne10,
  10554. const int ne11, const int ne12,
  10555. const int nb10, const int nb11,
  10556. const int nb12, const int nb13,
  10557. dpct::queue_ptr stream) {
  10558. const int num_blocks = (ne + SYCL_CPY_BLOCK_SIZE - 1) / SYCL_CPY_BLOCK_SIZE;
  10559. {
  10560. // dpct::has_capability_or_fail(stream->get_device(),
  10561. // {sycl::aspect::fp16});
  10562. stream->parallel_for(
  10563. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10564. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE),
  10565. sycl::range<3>(1, 1, SYCL_CPY_BLOCK_SIZE)),
  10566. [=](sycl::nd_item<3> item_ct1) {
  10567. cpy_f32_f16<cpy_1_i32_i32>(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02,
  10568. nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13,
  10569. item_ct1);
  10570. });
  10571. }
  10572. }
  10573. static void scale_f32_sycl(const float *x, float *dst, const float scale,
  10574. const int k, dpct::queue_ptr stream) {
  10575. const int num_blocks = (k + SYCL_SCALE_BLOCK_SIZE - 1) / SYCL_SCALE_BLOCK_SIZE;
  10576. stream->parallel_for(
  10577. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10578. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE),
  10579. sycl::range<3>(1, 1, SYCL_SCALE_BLOCK_SIZE)),
  10580. [=](sycl::nd_item<3> item_ct1) {
  10581. scale_f32(x, dst, scale, k, item_ct1);
  10582. });
  10583. }
  10584. static void clamp_f32_sycl(const float *x, float *dst, const float min,
  10585. const float max, const int k,
  10586. dpct::queue_ptr stream) {
  10587. const int num_blocks = (k + SYCL_CLAMP_BLOCK_SIZE - 1) / SYCL_CLAMP_BLOCK_SIZE;
  10588. stream->parallel_for(
  10589. sycl::nd_range<3>(sycl::range<3>(1, 1, num_blocks) *
  10590. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE),
  10591. sycl::range<3>(1, 1, SYCL_CLAMP_BLOCK_SIZE)),
  10592. [=](sycl::nd_item<3> item_ct1) {
  10593. clamp_f32(x, dst, min, max, k, item_ct1);
  10594. });
  10595. }
  10596. template <typename T>
  10597. static void rope_sycl(const T *x, T *dst, int ncols, int nrows,
  10598. const int32_t *pos, float freq_scale, int p_delta_rows,
  10599. float freq_base, float ext_factor, float attn_factor,
  10600. rope_corr_dims corr_dims, dpct::queue_ptr stream) {
  10601. GGML_ASSERT(ncols % 2 == 0);
  10602. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  10603. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  10604. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  10605. if (pos == nullptr) {
  10606. /*
  10607. DPCT1049:40: The work-group size passed to the SYCL kernel may exceed
  10608. the limit. To get the device limit, query
  10609. info::device::max_work_group_size. Adjust the work-group size if needed.
  10610. */
  10611. dpct::has_capability_or_fail(stream->get_device(),
  10612. {sycl::aspect::fp16});
  10613. stream->parallel_for(
  10614. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10615. [=](sycl::nd_item<3> item_ct1) {
  10616. rope<T, false>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  10617. freq_base, ext_factor, attn_factor, corr_dims,
  10618. item_ct1);
  10619. });
  10620. } else {
  10621. /*
  10622. DPCT1049:41: The work-group size passed to the SYCL kernel may exceed
  10623. the limit. To get the device limit, query
  10624. info::device::max_work_group_size. Adjust the work-group size if needed.
  10625. */
  10626. dpct::has_capability_or_fail(stream->get_device(),
  10627. {sycl::aspect::fp16});
  10628. stream->parallel_for(
  10629. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10630. [=](sycl::nd_item<3> item_ct1) {
  10631. rope<T, true>(x, dst, ncols, pos, freq_scale, p_delta_rows,
  10632. freq_base, ext_factor, attn_factor, corr_dims,
  10633. item_ct1);
  10634. });
  10635. }
  10636. }
  10637. template <typename T>
  10638. static void rope_neox_sycl(const T *x, T *dst, int ncols, int n_dims, int nrows,
  10639. const int32_t *pos, float freq_scale,
  10640. int p_delta_rows, float freq_base, float ext_factor,
  10641. float attn_factor, rope_corr_dims corr_dims,
  10642. dpct::queue_ptr stream) {
  10643. GGML_ASSERT(ncols % 2 == 0);
  10644. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  10645. const int num_blocks_x = (ncols + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  10646. const sycl::range<3> block_nums(1, num_blocks_x, nrows);
  10647. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  10648. const float inv_ndims = -1.0f / n_dims;
  10649. if (pos == nullptr) {
  10650. /*
  10651. DPCT1049:42: The work-group size passed to the SYCL kernel may exceed
  10652. the limit. To get the device limit, query
  10653. info::device::max_work_group_size. Adjust the work-group size if needed.
  10654. */
  10655. dpct::has_capability_or_fail(stream->get_device(),
  10656. {sycl::aspect::fp16});
  10657. stream->parallel_for(
  10658. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10659. [=](sycl::nd_item<3> item_ct1) {
  10660. rope_neox<T, false>(x, dst, ncols, n_dims, pos, freq_scale,
  10661. p_delta_rows, ext_factor, attn_factor,
  10662. corr_dims, theta_scale, inv_ndims,
  10663. item_ct1);
  10664. });
  10665. } else {
  10666. /*
  10667. DPCT1049:43: The work-group size passed to the SYCL kernel may exceed
  10668. the limit. To get the device limit, query
  10669. info::device::max_work_group_size. Adjust the work-group size if needed.
  10670. */
  10671. dpct::has_capability_or_fail(stream->get_device(),
  10672. {sycl::aspect::fp16});
  10673. stream->parallel_for(
  10674. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10675. [=](sycl::nd_item<3> item_ct1) {
  10676. rope_neox<T, true>(x, dst, ncols, n_dims, pos, freq_scale,
  10677. p_delta_rows, ext_factor, attn_factor,
  10678. corr_dims, theta_scale, inv_ndims, item_ct1);
  10679. });
  10680. }
  10681. }
  10682. static void rope_glm_f32_sycl(const float *x, float *dst, int ncols, int nrows,
  10683. const int32_t *pos, float freq_scale,
  10684. int p_delta_rows, float freq_base, int n_ctx,
  10685. dpct::queue_ptr stream) {
  10686. GGML_ASSERT(ncols % 4 == 0);
  10687. const sycl::range<3> block_dims(1, 1, SYCL_ROPE_BLOCK_SIZE / 4);
  10688. const int num_blocks_x = (ncols + SYCL_ROPE_BLOCK_SIZE - 1) / SYCL_ROPE_BLOCK_SIZE;
  10689. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  10690. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10691. [=](sycl::nd_item<3> item_ct1) {
  10692. rope_glm_f32(x, dst, ncols, pos, freq_scale,
  10693. p_delta_rows, freq_base, n_ctx,
  10694. item_ct1);
  10695. });
  10696. }
  10697. static void alibi_f32_sycl(const float *x, float *dst, const int ncols,
  10698. const int nrows, const int k_rows,
  10699. const int n_heads_log2_floor, const float m0,
  10700. const float m1, dpct::queue_ptr stream) {
  10701. const sycl::range<3> block_dims(1, 1, SYCL_ALIBI_BLOCK_SIZE);
  10702. const int num_blocks_x = (ncols + SYCL_ALIBI_BLOCK_SIZE - 1) / (SYCL_ALIBI_BLOCK_SIZE);
  10703. const sycl::range<3> block_nums(1, nrows, num_blocks_x);
  10704. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10705. [=](sycl::nd_item<3> item_ct1) {
  10706. alibi_f32(x, dst, ncols, k_rows,
  10707. n_heads_log2_floor, m0, m1, item_ct1);
  10708. });
  10709. }
  10710. static void sum_rows_f32_sycl(const float *x, float *dst, const int ncols,
  10711. const int nrows, dpct::queue_ptr stream) {
  10712. const sycl::range<3> block_dims(1, 1, WARP_SIZE);
  10713. const sycl::range<3> block_nums(1, nrows, 1);
  10714. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10715. [=](sycl::nd_item<3> item_ct1)
  10716. [[intel::reqd_sub_group_size(32)]] {
  10717. k_sum_rows_f32(x, dst, ncols, item_ct1);
  10718. });
  10719. }
  10720. static void argsort_f32_i32_sycl(const float *x, int *dst, const int ncols,
  10721. const int nrows, ggml_sort_order order,
  10722. dpct::queue_ptr stream) {
  10723. // bitonic sort requires ncols to be power of 2
  10724. GGML_ASSERT((ncols & (ncols - 1)) == 0);
  10725. const sycl::range<3> block_dims(1, 1, ncols);
  10726. const sycl::range<3> block_nums(1, nrows, 1);
  10727. if (order == GGML_SORT_ORDER_ASC) {
  10728. /*
  10729. DPCT1049:44: The work-group size passed to the SYCL kernel may exceed
  10730. the limit. To get the device limit, query
  10731. info::device::max_work_group_size. Adjust the work-group size if needed.
  10732. */
  10733. stream->parallel_for(
  10734. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10735. [=](sycl::nd_item<3> item_ct1) {
  10736. k_argsort_f32_i32<GGML_SORT_ORDER_ASC>(x, dst, ncols, item_ct1);
  10737. });
  10738. } else if (order == GGML_SORT_ORDER_DESC) {
  10739. /*
  10740. DPCT1049:45: The work-group size passed to the SYCL kernel may exceed
  10741. the limit. To get the device limit, query
  10742. info::device::max_work_group_size. Adjust the work-group size if needed.
  10743. */
  10744. stream->parallel_for(
  10745. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10746. [=](sycl::nd_item<3> item_ct1) {
  10747. k_argsort_f32_i32<GGML_SORT_ORDER_DESC>(x, dst, ncols, item_ct1);
  10748. });
  10749. } else {
  10750. GGML_ASSERT(false);
  10751. }
  10752. }
  10753. static void diag_mask_inf_f32_sycl(const float *x, float *dst,
  10754. const int ncols_x, const int nrows_x,
  10755. const int rows_per_channel, const int n_past,
  10756. dpct::queue_ptr stream) {
  10757. const sycl::range<3> block_dims(1, SYCL_DIAG_MASK_INF_BLOCK_SIZE, 1);
  10758. const int block_num_x = (ncols_x + SYCL_DIAG_MASK_INF_BLOCK_SIZE - 1) / SYCL_DIAG_MASK_INF_BLOCK_SIZE;
  10759. const sycl::range<3> block_nums(1, block_num_x, nrows_x);
  10760. stream->parallel_for(sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10761. [=](sycl::nd_item<3> item_ct1) {
  10762. diag_mask_inf_f32(x, dst, ncols_x,
  10763. rows_per_channel, n_past,
  10764. item_ct1);
  10765. });
  10766. }
  10767. template <bool vals_smem, int ncols_template, int block_size_template>
  10768. static void soft_max_f32_submitter(const float * x, const float * mask, const float *pos, float * dst, const int ncols_par,
  10769. const int nrows_y, const float scale, const float max_bias, const float m0,
  10770. const float m1, uint32_t n_head_log2, sycl::range<3> block_nums, sycl::range<3> block_dims,
  10771. const size_t n_local_scratch, dpct::queue_ptr stream) {
  10772. stream->submit([&](sycl::handler &cgh) {
  10773. sycl::local_accessor<float, 1> local_buf_acc(n_local_scratch, cgh);
  10774. cgh.parallel_for(
  10775. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  10776. [=](sycl::nd_item<3> item_ct1) [[intel::reqd_sub_group_size(32)]] {
  10777. soft_max_f32<vals_smem, ncols_template, block_size_template>(x, mask, pos, dst, ncols_par,
  10778. nrows_y, scale, max_bias, m0,
  10779. m1, n_head_log2, item_ct1,
  10780. local_buf_acc.get_pointer());
  10781. });
  10782. });
  10783. }
  10784. static void soft_max_f32_sycl(const float * x, const float * mask, const float * pos,
  10785. float * dst, const int ncols_x, const int nrows_x,
  10786. const int nrows_y, const float scale, const float max_bias,
  10787. dpct::queue_ptr stream) {
  10788. int nth = WARP_SIZE;
  10789. while (nth < ncols_x && nth < SYCL_SOFT_MAX_BLOCK_SIZE) nth *= 2;
  10790. const sycl::range<3> block_dims(1, 1, nth);
  10791. const sycl::range<3> block_nums(1, 1, nrows_x);
  10792. const size_t n_local_scratch = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE);
  10793. static_assert(SYCL_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
  10794. const uint32_t n_head_kv = nrows_x/nrows_y;
  10795. const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
  10796. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  10797. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  10798. const size_t local_mem_size = stream->get_device().get_info<sycl::info::device::local_mem_size>();
  10799. if (n_local_scratch*sizeof(float) < local_mem_size) {
  10800. switch (ncols_x) {
  10801. case 32:
  10802. soft_max_f32_submitter<true, 32, 32>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  10803. max_bias, m0, m1, n_head_log2, block_nums,
  10804. block_dims, n_local_scratch, stream);
  10805. break;
  10806. case 64:
  10807. soft_max_f32_submitter<true, 64, 64>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  10808. max_bias, m0, m1, n_head_log2, block_nums,
  10809. block_dims, n_local_scratch, stream);
  10810. break;
  10811. case 128:
  10812. soft_max_f32_submitter<true, 128, 128>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  10813. max_bias, m0, m1, n_head_log2, block_nums,
  10814. block_dims, n_local_scratch, stream);
  10815. break;
  10816. case 256:
  10817. soft_max_f32_submitter<true, 256, 256>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  10818. max_bias, m0, m1, n_head_log2, block_nums,
  10819. block_dims, n_local_scratch, stream);
  10820. break;
  10821. case 512:
  10822. soft_max_f32_submitter<true, 512, 512>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  10823. max_bias, m0, m1, n_head_log2, block_nums,
  10824. block_dims, n_local_scratch, stream);
  10825. break;
  10826. case 1024:
  10827. soft_max_f32_submitter<true, 1024, 1024>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  10828. max_bias, m0, m1, n_head_log2, block_nums,
  10829. block_dims, n_local_scratch, stream);
  10830. break;
  10831. case 2048:
  10832. soft_max_f32_submitter<true, 2048, 1024>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  10833. max_bias, m0, m1, n_head_log2, block_nums,
  10834. block_dims, n_local_scratch, stream);
  10835. break;
  10836. case 4096:
  10837. soft_max_f32_submitter<true, 4096, 1024>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  10838. max_bias, m0, m1, n_head_log2, block_nums,
  10839. block_dims, n_local_scratch, stream);
  10840. break;
  10841. default:
  10842. soft_max_f32_submitter<true, 0, 0>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  10843. max_bias, m0, m1, n_head_log2, block_nums,
  10844. block_dims, n_local_scratch, stream);
  10845. break;
  10846. }
  10847. } else {
  10848. soft_max_f32_submitter<false, 0, 0>(x, mask, pos, dst, ncols_x, nrows_y, scale,
  10849. max_bias, m0, m1, n_head_log2, block_nums,
  10850. block_dims, WARP_SIZE, stream);
  10851. }
  10852. }
  10853. template <typename T>
  10854. static void im2col_sycl(const float *x, T *dst, int IW, int IH,
  10855. int OW, int OH, int KW, int KH, int IC,
  10856. int offset_delta, int s0, int s1, int p0,
  10857. int p1, int d0, int d1,
  10858. dpct::queue_ptr stream) {
  10859. const int parallel_elements = OW * KW * KH;
  10860. const int num_blocks = (parallel_elements + SYCL_IM2COL_BLOCK_SIZE - 1) / SYCL_IM2COL_BLOCK_SIZE;
  10861. sycl::range<3> block_nums(IC, OH, num_blocks);
  10862. {
  10863. dpct::has_capability_or_fail(stream->get_device(),
  10864. {sycl::aspect::fp16});
  10865. stream->parallel_for(
  10866. sycl::nd_range<3>(block_nums *
  10867. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  10868. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  10869. [=](sycl::nd_item<3> item_ct1) {
  10870. im2col_kernel(x, dst, offset_delta, IW, IH, OW, KW, KH,
  10871. parallel_elements, (IC * KH * KW), s0, s1, p0,
  10872. p1, d0, d1, item_ct1);
  10873. });
  10874. }
  10875. }
  10876. // buffer pool for sycl
  10877. #define MAX_SYCL_BUFFERS 256
  10878. struct scoped_spin_lock {
  10879. std::atomic_flag& lock;
  10880. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  10881. while (lock.test_and_set(std::memory_order_acquire)) {
  10882. ; // spin
  10883. }
  10884. }
  10885. ~scoped_spin_lock() {
  10886. lock.clear(std::memory_order_release);
  10887. }
  10888. scoped_spin_lock(const scoped_spin_lock&) = delete;
  10889. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  10890. };
  10891. static std::atomic_flag g_sycl_pool_lock = ATOMIC_FLAG_INIT;
  10892. // #define DEBUG_SYCL_MALLOC
  10893. struct sycl_buffer {
  10894. void * ptr = nullptr;
  10895. size_t size = 0;
  10896. };
  10897. static sycl_buffer g_sycl_buffer_pool[GGML_SYCL_MAX_DEVICES][MAX_SYCL_BUFFERS];
  10898. static size_t g_sycl_pool_size[GGML_SYCL_MAX_DEVICES] = {0};
  10899. static void *ggml_sycl_pool_malloc_leg(int device_index, size_t size, size_t *actual_size) try {
  10900. scoped_spin_lock lock(g_sycl_pool_lock);
  10901. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg device_index %d size=%lu\n", device_index, size);
  10902. #ifdef DEBUG_SYCL_MALLOC
  10903. int nnz = 0;
  10904. size_t max_size = 0;
  10905. #endif
  10906. size_t best_diff = 1ull << 36;
  10907. int ibest = -1;
  10908. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  10909. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  10910. if (b.ptr != nullptr) {
  10911. #ifdef DEBUG_SYCL_MALLOC
  10912. ++nnz;
  10913. if (b.size > max_size) max_size = b.size;
  10914. #endif
  10915. if (b.size >= size) {
  10916. size_t diff = b.size - size;
  10917. if (diff < best_diff) {
  10918. best_diff = diff;
  10919. ibest = i;
  10920. if (!best_diff) {
  10921. void * ptr = b.ptr;
  10922. *actual_size = b.size;
  10923. b.ptr = nullptr;
  10924. b.size = 0;
  10925. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 1 %p and rm in pool\n", ptr);
  10926. return ptr;
  10927. }
  10928. }
  10929. }
  10930. }
  10931. }
  10932. if (ibest >= 0) {
  10933. sycl_buffer& b = g_sycl_buffer_pool[device_index][ibest];
  10934. void * ptr = b.ptr;
  10935. *actual_size = b.size;
  10936. b.ptr = nullptr;
  10937. b.size = 0;
  10938. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg return 2 %p and rm in pool\n", ptr);
  10939. return ptr;
  10940. }
  10941. void * ptr;
  10942. size_t look_ahead_size = (size_t) (1.05 * size);
  10943. look_ahead_size = 256 * ((look_ahead_size + 255)/256);
  10944. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  10945. SYCL_CHECK(
  10946. CHECK_TRY_ERROR(ptr = (void *)sycl::malloc_device(
  10947. look_ahead_size, *stream)));
  10948. *actual_size = look_ahead_size;
  10949. g_sycl_pool_size[device_index] += look_ahead_size;
  10950. #ifdef DEBUG_SYCL_MALLOC
  10951. fprintf(stderr, "%s[%d]: %d buffers, max_size = %u MB, pool_size = %u MB, requested %u MB\n", __func__, id, nnz,
  10952. (uint32_t)(max_size/1024/1024), (uint32_t)(g_sycl_pool_size[id]/1024/1024), (uint32_t)(size/1024/1024));
  10953. #endif
  10954. // GGML_SYCL_DEBUG("ggml_sycl_pool_malloc_leg look_ahead_size=%lu, return %p\n", look_ahead_size, ptr);
  10955. return ptr;
  10956. }
  10957. catch (sycl::exception const &exc) {
  10958. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10959. << ", line:" << __LINE__ << std::endl;
  10960. std::exit(1);
  10961. }
  10962. static void ggml_sycl_pool_free_leg(int device_index, void *ptr, size_t size) try {
  10963. scoped_spin_lock lock(g_sycl_pool_lock);
  10964. const dpct::queue_ptr stream = g_syclStreams[device_index][0];
  10965. for (int i = 0; i < MAX_SYCL_BUFFERS; ++i) {
  10966. sycl_buffer& b = g_sycl_buffer_pool[device_index][i];
  10967. if (b.ptr == nullptr) {
  10968. b.ptr = ptr;
  10969. b.size = size;
  10970. return;
  10971. }
  10972. }
  10973. fprintf(stderr, "WARNING: sycl buffer pool full, increase MAX_SYCL_BUFFERS\n");
  10974. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, *stream)));
  10975. g_sycl_pool_size[device_index] -= size;
  10976. }
  10977. catch (sycl::exception const &exc) {
  10978. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10979. << ", line:" << __LINE__ << std::endl;
  10980. std::exit(1);
  10981. }
  10982. // pool with virtual memory
  10983. /*
  10984. DPCT1082:64: Migration of CUmemGenericAllocationHandle type is not supported.
  10985. */
  10986. // static std::vector<CUmemGenericAllocationHandle>
  10987. // g_sycl_pool_handles[GGML_SYCL_MAX_DEVICES];
  10988. static dpct::device_ptr g_sycl_pool_addr[GGML_SYCL_MAX_DEVICES] = {0};
  10989. static size_t g_sycl_pool_used[GGML_SYCL_MAX_DEVICES] = {0};
  10990. static void *ggml_sycl_pool_malloc_vmm(int device_index, size_t size, size_t *actual_size) try {
  10991. GGML_UNUSED(device_index);
  10992. GGML_UNUSED(size);
  10993. GGML_UNUSED(actual_size);
  10994. return NULL;
  10995. }
  10996. catch (sycl::exception const &exc) {
  10997. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  10998. << ", line:" << __LINE__ << std::endl;
  10999. std::exit(1);
  11000. }
  11001. static void ggml_sycl_pool_free_vmm(int device_index, void *ptr, size_t size) try {
  11002. scoped_spin_lock lock(g_sycl_pool_lock);
  11003. #ifdef DEBUG_SYCL_MALLOC
  11004. printf("sycl pool[%d]: freed %llu bytes at %llx\n", device_index, (unsigned long long) size, ptr);
  11005. #endif
  11006. g_sycl_pool_used[device_index] -= size;
  11007. // all deallocations must be in reverse order of the allocations
  11008. GGML_ASSERT(ptr == (void *) (g_sycl_pool_addr[device_index] + g_sycl_pool_used[device_index]));
  11009. }
  11010. catch (sycl::exception const &exc) {
  11011. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11012. << ", line:" << __LINE__ << std::endl;
  11013. std::exit(1);
  11014. }
  11015. static void *ggml_sycl_pool_malloc(int device_index, size_t size, size_t *actual_size) try {
  11016. if (g_device_caps[device_index].vmm) {
  11017. return ggml_sycl_pool_malloc_vmm(device_index, size, actual_size);
  11018. } else {
  11019. return ggml_sycl_pool_malloc_leg(device_index, size, actual_size);
  11020. }
  11021. }
  11022. catch (sycl::exception const &exc) {
  11023. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11024. << ", line:" << __LINE__ << std::endl;
  11025. std::exit(1);
  11026. }
  11027. static void ggml_sycl_pool_free(int device_index, void *ptr, size_t size) try {
  11028. if (g_device_caps[device_index].vmm) {
  11029. ggml_sycl_pool_free_vmm(device_index, ptr, size);
  11030. } else {
  11031. ggml_sycl_pool_free_leg(device_index, ptr, size);
  11032. }
  11033. }
  11034. catch (sycl::exception const &exc) {
  11035. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11036. << ", line:" << __LINE__ << std::endl;
  11037. std::exit(1);
  11038. }
  11039. template<typename T>
  11040. struct sycl_pool_alloc {
  11041. int device_index = -1;
  11042. int device_id = -1;
  11043. T * ptr = nullptr;
  11044. size_t actual_size = 0;
  11045. // size is in number of elements
  11046. T * alloc(size_t size) {
  11047. GGML_ASSERT(ptr == nullptr);
  11048. device_id = get_current_device_id();
  11049. device_index = g_sycl_gpu_mgr->get_index(device_id);
  11050. ptr = (T *) ggml_sycl_pool_malloc(device_index, size * sizeof(T), &this->actual_size);
  11051. // GGML_SYCL_DEBUG("sycl_pool_alloc %lu return %p actual size=%lu\n", size * sizeof(T), ptr, this->actual_size);
  11052. return ptr;
  11053. }
  11054. sycl_pool_alloc(size_t size) {
  11055. alloc(size);
  11056. }
  11057. ~sycl_pool_alloc() {
  11058. if (ptr != nullptr) {
  11059. ggml_sycl_pool_free(device_index, ptr, actual_size);
  11060. }
  11061. }
  11062. T * get() {
  11063. return ptr;
  11064. }
  11065. sycl_pool_alloc() = default;
  11066. sycl_pool_alloc(const sycl_pool_alloc &) = delete;
  11067. sycl_pool_alloc(sycl_pool_alloc &&) = delete;
  11068. sycl_pool_alloc& operator=(const sycl_pool_alloc &) = delete;
  11069. sycl_pool_alloc& operator=(sycl_pool_alloc &&) = delete;
  11070. };
  11071. static bool g_sycl_loaded = false;
  11072. bool ggml_sycl_loaded(void) {
  11073. return g_sycl_loaded;
  11074. }
  11075. void print_device_detail(int id, sycl::device &device, std::string device_type) {
  11076. dpct::device_info prop;
  11077. SYCL_CHECK(CHECK_TRY_ERROR(
  11078. dpct::get_device_info(prop, device)));
  11079. std::string version;
  11080. version += std::to_string(prop.get_major_version());
  11081. version += ".";
  11082. version += std::to_string(prop.get_minor_version());
  11083. device_type = std::regex_replace(device_type, std::regex("ext_oneapi_"), "");
  11084. fprintf(stderr, "|%2d|%18s|%45s|%10s|%11d|%8d|%7d|%15lu|\n", id, device_type.c_str(),
  11085. prop.get_name(), version.c_str(), prop.get_max_compute_units(),
  11086. prop.get_max_work_group_size(), prop.get_max_sub_group_size(),
  11087. prop.get_global_mem_size());
  11088. }
  11089. void ggml_backend_sycl_print_sycl_devices() {
  11090. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_print_sycl_devices\n");
  11091. int device_count = dpct::dev_mgr::instance().device_count();
  11092. std::map<std::string, size_t> DeviceNums;
  11093. fprintf(stderr, "found %d SYCL devices:\n", device_count);
  11094. fprintf(stderr, "| | | |Compute |Max compute|Max work|Max sub| |\n");
  11095. fprintf(stderr, "|ID| Device Type| Name|capability|units |group |group |Global mem size|\n");
  11096. fprintf(stderr, "|--|------------------|---------------------------------------------|----------|-----------|--------|-------|---------------|\n");
  11097. for (int id = 0; id < device_count; ++id) {
  11098. sycl::device device = dpct::dev_mgr::instance().get_device(id);
  11099. sycl::backend backend = device.get_backend();
  11100. std::string backend_type = get_device_backend_and_type(device);
  11101. int type_id=DeviceNums[backend_type]++;
  11102. std::stringstream device_type;
  11103. device_type << "[" << backend_type << ":" << std::to_string(type_id) << "]";
  11104. print_device_detail(id, device, device_type.str());
  11105. }
  11106. }
  11107. void print_gpu_device_list() {
  11108. GGML_ASSERT(g_sycl_gpu_mgr);
  11109. char* hint=NULL;
  11110. if (g_ggml_sycl_backend_gpu_mode == SYCL_SINGLE_GPU_MODE) {
  11111. hint = "use %d SYCL GPUs: [%s] with Max compute units:%d\n";
  11112. } else {
  11113. hint = "detect %d SYCL GPUs: [%s] with top Max compute units:%d\n";
  11114. }
  11115. fprintf(stderr, hint,
  11116. g_sycl_gpu_mgr->get_gpu_count(),
  11117. g_sycl_gpu_mgr->gpus_list.c_str(),
  11118. g_sycl_gpu_mgr->max_compute_units);
  11119. }
  11120. int get_sycl_env(const char *env_name, int default_val) {
  11121. char *user_device_string = getenv(env_name);
  11122. int user_number = default_val;
  11123. unsigned n;
  11124. if (user_device_string != NULL &&
  11125. sscanf(user_device_string, " %u", &n) == 1) {
  11126. user_number = (int)n;
  11127. } else {
  11128. user_number = default_val;
  11129. }
  11130. return user_number;
  11131. }
  11132. int get_work_group_size(int user_device_id) {
  11133. dpct::device_info prop;
  11134. dpct::get_device_info(prop,
  11135. dpct::dev_mgr::instance().get_device(user_device_id));
  11136. return prop.get_max_work_group_size();
  11137. }
  11138. static void ggml_init_sycl() try {
  11139. static bool initialized = false;
  11140. if (!initialized) {
  11141. fprintf(stderr, "[SYCL] call ggml_init_sycl\n");
  11142. g_ggml_sycl_debug = get_sycl_env("GGML_SYCL_DEBUG", 0);
  11143. fprintf(stderr, "%s: GGML_SYCL_DEBUG: %d\n", __func__, g_ggml_sycl_debug);
  11144. #if defined(GGML_SYCL_F16)
  11145. fprintf(stderr, "%s: GGML_SYCL_F16: yes\n", __func__);
  11146. #else
  11147. fprintf(stderr, "%s: GGML_SYCL_F16: no\n", __func__);
  11148. #endif
  11149. /* NOT REMOVE, keep it for next optimize for XMX.
  11150. #if defined(SYCL_USE_XMX)
  11151. fprintf(stderr, "%s: SYCL_USE_XMX: yes\n", __func__);
  11152. #else
  11153. fprintf(stderr, "%s: SYCL_USE_XMX: no\n", __func__);
  11154. #endif
  11155. */
  11156. if (CHECK_TRY_ERROR(g_all_sycl_device_count =
  11157. dpct::dev_mgr::instance().device_count()) != 0) {
  11158. initialized = true;
  11159. g_sycl_loaded = false;
  11160. return;
  11161. }
  11162. GGML_ASSERT(g_all_sycl_device_count <= GGML_SYCL_MAX_DEVICES);
  11163. ggml_backend_sycl_print_sycl_devices();
  11164. initialized = true;
  11165. g_sycl_loaded = true;
  11166. }
  11167. }
  11168. catch (sycl::exception const &exc) {
  11169. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11170. << ", line:" << __LINE__ << std::endl;
  11171. std::exit(1);
  11172. }
  11173. void ggml_init_by_gpus(int device_count) try {
  11174. g_device_count = device_count;
  11175. g_work_group_size = g_sycl_gpu_mgr->work_group_size;
  11176. int64_t total_vram = 0;
  11177. print_gpu_device_list();
  11178. for (int id = 0; id < GGML_SYCL_MAX_DEVICES; ++id) {
  11179. g_device_caps[id].vmm = 0;
  11180. g_device_caps[id].device_id = -1;
  11181. g_device_caps[id].cc = 0;
  11182. g_tensor_split[id] = 0;
  11183. g_default_tensor_split[id] = 0;
  11184. }
  11185. for (int i = 0; i < g_device_count; ++i) {
  11186. int device_id = g_sycl_gpu_mgr->gpus[i];
  11187. g_device_caps[i].vmm = 0;
  11188. dpct::device_info prop;
  11189. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  11190. prop, dpct::dev_mgr::instance().get_device(device_id))));
  11191. g_default_tensor_split[i] = total_vram;
  11192. total_vram += prop.get_global_mem_size();
  11193. g_device_caps[i].cc =
  11194. 100 * prop.get_major_version() + 10 * prop.get_minor_version();
  11195. }
  11196. for (int i = 0; i < g_device_count; ++i) {
  11197. g_default_tensor_split[i] /= total_vram;
  11198. }
  11199. for (int i = 0; i < g_device_count; ++i) {
  11200. SYCL_CHECK(ggml_sycl_set_device(i));
  11201. // create sycl streams
  11202. for (int is = 0; is < MAX_STREAMS; ++is) {
  11203. SYCL_CHECK(CHECK_TRY_ERROR(
  11204. g_syclStreams[i][is] =
  11205. dpct::get_current_device().create_queue(
  11206. g_sycl_gpu_mgr->get_co_ctx(), dpct::get_current_device())));
  11207. }
  11208. const dpct::queue_ptr stream = g_syclStreams[i][0];
  11209. // create sycl handle
  11210. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[i] = stream));
  11211. }
  11212. }
  11213. catch (sycl::exception const &exc) {
  11214. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11215. << ", line:" << __LINE__ << std::endl;
  11216. std::exit(1);
  11217. }
  11218. void *ggml_sycl_host_malloc(size_t size) try {
  11219. if (getenv("GGML_SYCL_NO_PINNED") != nullptr) {
  11220. return nullptr;
  11221. }
  11222. void * ptr = nullptr;
  11223. //allow to use dpct::get_in_order_queue() for host malloc
  11224. dpct::err0 err = CHECK_TRY_ERROR(
  11225. ptr = (void *)sycl::malloc_host(size, dpct::get_in_order_queue()));
  11226. if (err != 0) {
  11227. // clear the error
  11228. fprintf(
  11229. stderr,
  11230. "WARNING: failed to allocate %.2f MB of pinned memory: %s\n",
  11231. size / 1024.0 / 1024.0,
  11232. "syclGetErrorString is not supported");
  11233. return nullptr;
  11234. }
  11235. return ptr;
  11236. }
  11237. catch (sycl::exception const &exc) {
  11238. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11239. << ", line:" << __LINE__ << std::endl;
  11240. std::exit(1);
  11241. }
  11242. void ggml_sycl_host_free(void *ptr) try {
  11243. //allow to use dpct::get_in_order_queue() for host malloc
  11244. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(ptr, dpct::get_in_order_queue())));
  11245. }
  11246. catch (sycl::exception const &exc) {
  11247. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11248. << ", line:" << __LINE__ << std::endl;
  11249. std::exit(1);
  11250. }
  11251. static dpct::err0 ggml_sycl_cpy_tensor_2d(void *dst,
  11252. const struct ggml_tensor *src,
  11253. int64_t i3, int64_t i2,
  11254. int64_t i1_low, int64_t i1_high,
  11255. dpct::queue_ptr stream) try {
  11256. dpct::memcpy_direction kind;
  11257. char * src_ptr;
  11258. if (src->backend == GGML_BACKEND_TYPE_CPU) {
  11259. kind = dpct::host_to_device;
  11260. src_ptr = (char *) src->data;
  11261. // GGML_SYCL_DEBUG("ggml_sycl_cpy_tensor_2d GGML_BACKEND_TYPE_CPU src_ptr %p\n", src_ptr);
  11262. } else if (src->backend == GGML_BACKEND_TYPE_GPU || src->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  11263. GGML_ASSERT(src->backend != GGML_BACKEND_TYPE_GPU_SPLIT || (i1_low == 0 && i1_high == src->ne[1]));
  11264. kind = dpct::device_to_device;
  11265. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) src->extra;
  11266. int id;
  11267. SYCL_CHECK(CHECK_TRY_ERROR(
  11268. id = get_current_device_id()));
  11269. // GGML_SYCL_DEBUG("current device index %d\n", id);
  11270. src_ptr = (char *) extra->data_device[id];
  11271. } else {
  11272. // GGML_SYCL_DEBUG("GGML_ASSERT(false)\n");
  11273. GGML_ASSERT(false);
  11274. }
  11275. char * dst_ptr = (char *) dst;
  11276. GGML_TENSOR_LOCALS_1(int64_t, ne, src, ne);
  11277. GGML_TENSOR_LOCALS(int64_t, nb, src, nb);
  11278. const enum ggml_type type = src->type;
  11279. const int64_t ts = ggml_type_size(type);
  11280. const int64_t bs = ggml_blck_size(type);
  11281. int64_t i1_diff = i1_high - i1_low;
  11282. const char * x = src_ptr + i1_low*nb1 + i2*nb2 + i3*nb3;
  11283. if (nb0 == ts && nb1 == ts*ne0/bs) {
  11284. // GGML_SYCL_DEBUG("stream->memcpy: dst_ptr=%p, x=%p, size=%lu\n", dst_ptr, x, i1_diff * nb1);
  11285. // return CHECK_TRY_ERROR(stream->memcpy(dst_ptr, x, i1_diff * nb1));
  11286. return CHECK_TRY_ERROR(dpct::async_dpct_memcpy(dst_ptr, x, i1_diff * nb1,
  11287. kind, *stream));
  11288. } else if (nb0 == ts) {
  11289. return CHECK_TRY_ERROR(
  11290. dpct::async_dpct_memcpy(dst_ptr, ts * ne0 / bs, x, nb1,
  11291. ts * ne0 / bs, i1_diff, kind, *stream));
  11292. } else {
  11293. for (int64_t i1 = 0; i1 < i1_diff; i1++) {
  11294. const void * rx = (const void *) ((const char *) x + i1*nb1);
  11295. void * rd = (void *) (dst_ptr + i1*ts*ne0/bs);
  11296. // pretend the row is a matrix with cols=1
  11297. dpct::err0 r = CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  11298. rd, ts / bs, rx, nb0, ts / bs, ne0, kind, *stream));
  11299. /*
  11300. DPCT1001:85: The statement could not be removed.
  11301. */
  11302. /*
  11303. DPCT1000:86: Error handling if-stmt was detected but could not be
  11304. rewritten.
  11305. */
  11306. if (r != 0) return r;
  11307. }
  11308. return 0;
  11309. }
  11310. }
  11311. catch (sycl::exception const &exc) {
  11312. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11313. << ", line:" << __LINE__ << std::endl;
  11314. std::exit(1);
  11315. }
  11316. static void ggml_sycl_op_get_rows(const ggml_tensor *src0,
  11317. const ggml_tensor *src1, ggml_tensor *dst,
  11318. const float *src0_d, const float *src1_d,
  11319. float *dst_d, const dpct::queue_ptr &stream) {
  11320. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  11321. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11322. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  11323. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  11324. GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
  11325. const int32_t * src1_i32 = (const int32_t *) src1_d;
  11326. switch (src0->type) {
  11327. case GGML_TYPE_F16:
  11328. get_rows_sycl_float(src0, src1, dst, (const sycl::half *)src0_d,
  11329. src1_i32, dst_d, stream);
  11330. break;
  11331. case GGML_TYPE_F32:
  11332. get_rows_sycl_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11333. break;
  11334. case GGML_TYPE_Q4_0:
  11335. get_rows_sycl<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11336. break;
  11337. case GGML_TYPE_Q4_1:
  11338. get_rows_sycl<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11339. break;
  11340. case GGML_TYPE_Q5_0:
  11341. get_rows_sycl<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11342. break;
  11343. case GGML_TYPE_Q5_1:
  11344. get_rows_sycl<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11345. break;
  11346. case GGML_TYPE_Q8_0:
  11347. get_rows_sycl<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
  11348. break;
  11349. default:
  11350. // TODO: k-quants
  11351. fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
  11352. GGML_ASSERT(false);
  11353. break;
  11354. }
  11355. }
  11356. template <class op>
  11357. inline void ggml_sycl_op_bin_bcast(const ggml_tensor *src0,
  11358. const ggml_tensor *src1, ggml_tensor *dst,
  11359. const float *src0_dd, const float *src1_dd,
  11360. float *dst_dd,
  11361. const dpct::queue_ptr &main_stream) {
  11362. if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
  11363. op()(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11364. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
  11365. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd,
  11366. (sycl::half *)dst_dd, main_stream);
  11367. } else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
  11368. op()(src0, src1, dst, (const sycl::half *)src0_dd, src1_dd, dst_dd,
  11369. main_stream);
  11370. } else if (src0->type == GGML_TYPE_I32 && dst->type == GGML_TYPE_I32) {
  11371. op()(src0, src1, dst, (const int32_t *)src0_dd, (const int32_t *)src1_dd, (int32_t *)dst_dd,
  11372. main_stream);
  11373. } else if (src0->type == GGML_TYPE_I16 && dst->type == GGML_TYPE_I16) {
  11374. op()(src0, src1, dst, (const int16_t *)src0_dd, (const int16_t *)src1_dd, (int16_t *)dst_dd,
  11375. main_stream);
  11376. } else {
  11377. fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
  11378. ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
  11379. GGML_ASSERT(false);
  11380. }
  11381. }
  11382. static void ggml_sycl_op_repeat(const ggml_tensor *src0,
  11383. const ggml_tensor *src1, ggml_tensor *dst,
  11384. const float *src0_d, const float *src1_d,
  11385. float *dst_d,
  11386. const dpct::queue_ptr &main_stream) {
  11387. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_repeat>>(dst, src0, dst, nullptr, src0_d, dst_d, main_stream);
  11388. (void) src1;
  11389. (void) src1_d;
  11390. }
  11391. inline void ggml_sycl_op_add(const ggml_tensor *src0, const ggml_tensor *src1,
  11392. ggml_tensor *dst, const float *src0_dd,
  11393. const float *src1_dd, float *dst_dd,
  11394. const dpct::queue_ptr &main_stream) {
  11395. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_add>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11396. }
  11397. inline void ggml_sycl_op_acc(const ggml_tensor *src0, const ggml_tensor *src1,
  11398. ggml_tensor *dst, const float *src0_dd,
  11399. const float *src1_dd, float *dst_dd,
  11400. const dpct::queue_ptr &main_stream) {
  11401. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11402. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11403. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11404. GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
  11405. int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
  11406. int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
  11407. // int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
  11408. int offset = dst->op_params[3] / 4; // offset in bytes
  11409. 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);
  11410. (void) dst;
  11411. }
  11412. inline void ggml_sycl_op_mul(const ggml_tensor *src0, const ggml_tensor *src1,
  11413. ggml_tensor *dst, const float *src0_dd,
  11414. const float *src1_dd, float *dst_dd,
  11415. const dpct::queue_ptr &main_stream) {
  11416. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_mul>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11417. }
  11418. inline void ggml_sycl_op_div(const ggml_tensor *src0, const ggml_tensor *src1,
  11419. ggml_tensor *dst, const float *src0_dd,
  11420. const float *src1_dd, float *dst_dd,
  11421. const dpct::queue_ptr &main_stream) {
  11422. ggml_sycl_op_bin_bcast<bin_bcast_sycl<op_div>>(src0, src1, dst, src0_dd, src1_dd, dst_dd, main_stream);
  11423. }
  11424. inline void ggml_sycl_op_gelu(const ggml_tensor *src0, const ggml_tensor *src1,
  11425. ggml_tensor *dst, const float *src0_dd,
  11426. const float *src1_dd, float *dst_dd,
  11427. const dpct::queue_ptr &main_stream) {
  11428. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11429. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11430. gelu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11431. (void) src1;
  11432. (void) dst;
  11433. (void) src1_dd;
  11434. }
  11435. inline void ggml_sycl_op_silu(const ggml_tensor *src0, const ggml_tensor *src1,
  11436. ggml_tensor *dst, const float *src0_dd,
  11437. const float *src1_dd, float *dst_dd,
  11438. const dpct::queue_ptr &main_stream) {
  11439. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11440. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11441. silu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11442. (void) src1;
  11443. (void) dst;
  11444. (void) src1_dd;
  11445. }
  11446. inline void ggml_sycl_op_gelu_quick(const ggml_tensor *src0,
  11447. const ggml_tensor *src1, ggml_tensor *dst,
  11448. const float *src0_dd, const float *src1_dd,
  11449. float *dst_dd,
  11450. const dpct::queue_ptr &main_stream) {
  11451. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11452. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11453. gelu_quick_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11454. (void) src1;
  11455. (void) dst;
  11456. (void) src1_dd;
  11457. }
  11458. inline void ggml_sycl_op_tanh(const ggml_tensor *src0, const ggml_tensor *src1,
  11459. ggml_tensor *dst, const float *src0_dd,
  11460. const float *src1_dd, float *dst_dd,
  11461. const dpct::queue_ptr &main_stream) {
  11462. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11463. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11464. tanh_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11465. (void) src1;
  11466. (void) dst;
  11467. (void) src1_dd;
  11468. }
  11469. inline void ggml_sycl_op_relu(const ggml_tensor *src0, const ggml_tensor *src1,
  11470. ggml_tensor *dst, const float *src0_dd,
  11471. const float *src1_dd, float *dst_dd,
  11472. const dpct::queue_ptr &main_stream) {
  11473. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11474. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11475. relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11476. (void) src1;
  11477. (void) dst;
  11478. (void) src1_dd;
  11479. }
  11480. static void ggml_sycl_op_hardsigmoid(const ggml_tensor *src0,
  11481. const ggml_tensor *src1, ggml_tensor *dst,
  11482. const float *src0_dd, const float *src1_dd,
  11483. float *dst_dd,
  11484. const dpct::queue_ptr &main_stream) {
  11485. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11486. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11487. hardsigmoid_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11488. (void) src1;
  11489. (void) dst;
  11490. (void) src1_dd;
  11491. }
  11492. static void ggml_sycl_op_hardswish(const ggml_tensor *src0,
  11493. const ggml_tensor *src1, ggml_tensor *dst,
  11494. const float *src0_dd, const float *src1_dd,
  11495. float *dst_dd, const dpct::queue_ptr &main_stream) {
  11496. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11497. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11498. hardswish_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11499. (void) src1;
  11500. (void) dst;
  11501. (void) src1_dd;
  11502. }
  11503. inline void ggml_sycl_op_leaky_relu(const ggml_tensor *src0,
  11504. const ggml_tensor *src1, ggml_tensor *dst,
  11505. const float *src0_dd, const float *src1_dd,
  11506. float *dst_dd,
  11507. const dpct::queue_ptr &main_stream) {
  11508. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11509. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11510. float negative_slope;
  11511. memcpy(&negative_slope, dst->op_params, sizeof(float));
  11512. leaky_relu_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), negative_slope, main_stream);
  11513. (void) src1;
  11514. (void) dst;
  11515. (void) src1_dd;
  11516. }
  11517. inline void ggml_sycl_op_sqr(const ggml_tensor *src0, const ggml_tensor *src1,
  11518. ggml_tensor *dst, const float *src0_dd,
  11519. const float *src1_dd, float *dst_dd,
  11520. const dpct::queue_ptr &main_stream) {
  11521. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11522. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11523. sqr_f32_sycl(src0_dd, dst_dd, ggml_nelements(src0), main_stream);
  11524. (void) src1;
  11525. (void) dst;
  11526. (void) src1_dd;
  11527. }
  11528. inline void ggml_sycl_op_norm(const ggml_tensor *src0, const ggml_tensor *src1,
  11529. ggml_tensor *dst, const float *src0_dd,
  11530. const float *src1_dd, float *dst_dd,
  11531. const dpct::queue_ptr &main_stream) {
  11532. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11533. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11534. const int64_t ne00 = src0->ne[0];
  11535. const int64_t nrows = ggml_nrows(src0);
  11536. float eps;
  11537. memcpy(&eps, dst->op_params, sizeof(float));
  11538. norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  11539. (void) src1;
  11540. (void) dst;
  11541. (void) src1_dd;
  11542. }
  11543. inline void ggml_sycl_op_group_norm(const ggml_tensor *src0,
  11544. const ggml_tensor *src1, ggml_tensor *dst,
  11545. const float *src0_dd, const float *src1_dd,
  11546. float *dst_dd,
  11547. const dpct::queue_ptr &main_stream) {
  11548. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11549. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11550. int num_groups = dst->op_params[0];
  11551. int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
  11552. group_norm_f32_sycl(src0_dd, dst_dd, num_groups, group_size, src0->ne[0] * src0->ne[1] * src0->ne[2], main_stream);
  11553. (void) src1;
  11554. (void) dst;
  11555. (void) src1_dd;
  11556. }
  11557. inline void ggml_sycl_op_concat(const ggml_tensor *src0,
  11558. const ggml_tensor *src1, ggml_tensor *dst,
  11559. const float *src0_dd, const float *src1_dd,
  11560. float *dst_dd,
  11561. const dpct::queue_ptr &main_stream) {
  11562. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11563. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11564. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11565. for (int i3 = 0; i3 < dst->ne[3]; i3++) {
  11566. 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);
  11567. }
  11568. (void) src1;
  11569. (void) dst;
  11570. }
  11571. inline void ggml_sycl_op_upscale(const ggml_tensor *src0,
  11572. const ggml_tensor *src1, ggml_tensor *dst,
  11573. const float *src0_dd, const float *src1_dd,
  11574. float *dst_dd,
  11575. const dpct::queue_ptr &main_stream) {
  11576. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11577. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11578. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  11579. const int scale_factor = dst->op_params[0];
  11580. upscale_f32_sycl(src0_dd, dst_dd, src0->ne[0], src0->ne[1], src0->ne[2], scale_factor, main_stream);
  11581. (void) src1;
  11582. (void) dst;
  11583. (void) src1_dd;
  11584. }
  11585. inline void ggml_sycl_op_pad(const ggml_tensor *src0, const ggml_tensor *src1,
  11586. ggml_tensor *dst, const float *src0_dd,
  11587. const float *src1_dd, float *dst_dd,
  11588. const dpct::queue_ptr &main_stream) {
  11589. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11590. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  11591. GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
  11592. pad_f32_sycl(src0_dd, dst_dd,
  11593. src0->ne[0], src0->ne[1], src0->ne[2],
  11594. dst->ne[0], dst->ne[1], dst->ne[2], main_stream);
  11595. (void) src1;
  11596. (void) dst;
  11597. (void) src1_dd;
  11598. }
  11599. inline void ggml_sycl_op_rms_norm(const ggml_tensor *src0,
  11600. const ggml_tensor *src1, ggml_tensor *dst,
  11601. const float *src0_dd, const float *src1_dd,
  11602. float *dst_dd,
  11603. const dpct::queue_ptr &main_stream) {
  11604. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  11605. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  11606. const int64_t ne00 = src0->ne[0];
  11607. const int64_t nrows = ggml_nrows(src0);
  11608. float eps;
  11609. memcpy(&eps, dst->op_params, sizeof(float));
  11610. rms_norm_f32_sycl(src0_dd, dst_dd, ne00, nrows, eps, main_stream);
  11611. (void) src1;
  11612. (void) dst;
  11613. (void) src1_dd;
  11614. }
  11615. inline void ggml_sycl_op_mul_mat_q(
  11616. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11617. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11618. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11619. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11620. const dpct::queue_ptr &stream) try {
  11621. const int64_t ne00 = src0->ne[0];
  11622. const int64_t ne10 = src1->ne[0];
  11623. GGML_ASSERT(ne10 % QK8_1 == 0);
  11624. const int64_t ne0 = dst->ne[0];
  11625. const int64_t row_diff = row_high - row_low;
  11626. int device_id;
  11627. SYCL_CHECK(
  11628. CHECK_TRY_ERROR(device_id = get_current_device_id()));
  11629. // the main device has a larger memory buffer to hold the results from all GPUs
  11630. // nrows_dst == nrows of the matrix that the dequantize_mul_mat kernel writes into
  11631. const int64_t nrows_dst = dst->backend == GGML_BACKEND_TYPE_GPU && device_id == g_main_device ? ne0 : row_diff;
  11632. switch (src0->type) {
  11633. case GGML_TYPE_Q4_0:
  11634. 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);
  11635. break;
  11636. case GGML_TYPE_Q4_1:
  11637. 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);
  11638. break;
  11639. case GGML_TYPE_Q5_0:
  11640. 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);
  11641. break;
  11642. case GGML_TYPE_Q5_1:
  11643. 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);
  11644. break;
  11645. case GGML_TYPE_Q8_0:
  11646. 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);
  11647. break;
  11648. case GGML_TYPE_Q2_K:
  11649. 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);
  11650. break;
  11651. case GGML_TYPE_Q3_K:
  11652. 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);
  11653. break;
  11654. case GGML_TYPE_Q4_K:
  11655. 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);
  11656. break;
  11657. case GGML_TYPE_Q5_K:
  11658. 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);
  11659. break;
  11660. case GGML_TYPE_Q6_K:
  11661. 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);
  11662. break;
  11663. default:
  11664. GGML_ASSERT(false);
  11665. break;
  11666. }
  11667. (void) src1;
  11668. (void) dst;
  11669. (void) src1_ddf_i;
  11670. }
  11671. catch (sycl::exception const &exc) {
  11672. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11673. << ", line:" << __LINE__ << std::endl;
  11674. std::exit(1);
  11675. }
  11676. static int64_t get_row_rounding(ggml_type type, const std::array<float, GGML_SYCL_MAX_DEVICES> & tensor_split) {
  11677. int64_t min_compute_capability = INT_MAX;
  11678. int64_t max_compute_capability = INT_MIN;
  11679. for (int i = 0; i < g_device_count; ++i) {
  11680. if (tensor_split[i] < (i + 1 < g_device_count ? tensor_split[i + 1] : 1.0f)) {
  11681. if (min_compute_capability > g_device_caps[i].cc) {
  11682. min_compute_capability = g_device_caps[i].cc;
  11683. }
  11684. if (max_compute_capability < g_device_caps[i].cc) {
  11685. max_compute_capability = g_device_caps[i].cc;
  11686. }
  11687. }
  11688. }
  11689. switch(type) {
  11690. case GGML_TYPE_Q4_0:
  11691. case GGML_TYPE_Q4_1:
  11692. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  11693. case GGML_TYPE_Q5_0:
  11694. case GGML_TYPE_Q5_1:
  11695. case GGML_TYPE_Q8_0:
  11696. return 64;
  11697. case GGML_TYPE_F16:
  11698. case GGML_TYPE_F32:
  11699. return 1;
  11700. case GGML_TYPE_Q2_K:
  11701. case GGML_TYPE_Q3_K:
  11702. case GGML_TYPE_Q4_K:
  11703. case GGML_TYPE_Q5_K:
  11704. case GGML_TYPE_IQ2_XXS:
  11705. case GGML_TYPE_IQ2_XS:
  11706. case GGML_TYPE_IQ1_S:
  11707. case GGML_TYPE_IQ3_XXS:
  11708. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  11709. case GGML_TYPE_IQ3_S:
  11710. return max_compute_capability >= VER_GEN9 ? 128 : 64;
  11711. case GGML_TYPE_Q6_K:
  11712. return 64;
  11713. default:
  11714. GGML_ASSERT(false);
  11715. }
  11716. }
  11717. inline void ggml_sycl_op_mul_mat_vec_q(
  11718. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11719. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11720. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11721. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11722. const dpct::queue_ptr &stream) {
  11723. GGML_ASSERT(ggml_nrows(src1) == 1);
  11724. const int64_t ne00 = src0->ne[0];
  11725. const int64_t row_diff = row_high - row_low;
  11726. switch (src0->type) {
  11727. case GGML_TYPE_Q4_0:
  11728. mul_mat_vec_q4_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11729. break;
  11730. case GGML_TYPE_Q4_1:
  11731. mul_mat_vec_q4_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11732. break;
  11733. case GGML_TYPE_Q5_0:
  11734. mul_mat_vec_q5_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11735. break;
  11736. case GGML_TYPE_Q5_1:
  11737. mul_mat_vec_q5_1_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11738. break;
  11739. case GGML_TYPE_Q8_0:
  11740. mul_mat_vec_q8_0_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11741. break;
  11742. case GGML_TYPE_Q2_K:
  11743. mul_mat_vec_q2_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11744. break;
  11745. case GGML_TYPE_Q3_K:
  11746. mul_mat_vec_q3_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11747. break;
  11748. case GGML_TYPE_Q4_K:
  11749. mul_mat_vec_q4_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11750. break;
  11751. case GGML_TYPE_Q5_K:
  11752. mul_mat_vec_q5_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11753. break;
  11754. case GGML_TYPE_Q6_K:
  11755. mul_mat_vec_q6_K_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11756. break;
  11757. case GGML_TYPE_IQ2_XXS:
  11758. mul_mat_vec_iq2_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11759. break;
  11760. case GGML_TYPE_IQ2_XS:
  11761. mul_mat_vec_iq2_xs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11762. break;
  11763. case GGML_TYPE_IQ3_XXS:
  11764. mul_mat_vec_iq3_xxs_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11765. break;
  11766. case GGML_TYPE_IQ3_S:
  11767. mul_mat_vec_iq3_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11768. break;
  11769. case GGML_TYPE_IQ1_S:
  11770. mul_mat_vec_iq1_s_q8_1_sycl(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, stream);
  11771. break;
  11772. default:
  11773. GGML_ASSERT(false);
  11774. break;
  11775. }
  11776. (void) src1;
  11777. (void) dst;
  11778. (void) src1_ddf_i;
  11779. (void) src1_ncols;
  11780. (void) src1_padded_row_size;
  11781. }
  11782. inline void ggml_sycl_op_dequantize_mul_mat_vec(
  11783. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11784. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11785. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11786. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11787. const dpct::queue_ptr &stream) {
  11788. const int64_t ne00 = src0->ne[0];
  11789. const int64_t row_diff = row_high - row_low;
  11790. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  11791. // on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
  11792. #ifdef GGML_SYCL_F16
  11793. sycl_pool_alloc<sycl::half> src1_dfloat_a;
  11794. sycl::half *src1_dfloat = nullptr; // dfloat == half
  11795. bool src1_convert_f16 =
  11796. src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
  11797. src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
  11798. src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
  11799. if (src1_convert_f16) {
  11800. src1_dfloat = src1_dfloat_a.alloc(ne00);
  11801. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  11802. GGML_ASSERT(to_fp16_sycl != nullptr);
  11803. to_fp16_sycl(src1_ddf_i, src1_dfloat, ne00, stream);
  11804. }
  11805. #else
  11806. const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
  11807. #endif // GGML_SYCL_F16
  11808. switch (src0->type) {
  11809. case GGML_TYPE_Q4_0:
  11810. dequantize_mul_mat_vec_q4_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11811. break;
  11812. case GGML_TYPE_Q4_1:
  11813. dequantize_mul_mat_vec_q4_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11814. break;
  11815. case GGML_TYPE_Q5_0:
  11816. dequantize_mul_mat_vec_q5_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11817. break;
  11818. case GGML_TYPE_Q5_1:
  11819. dequantize_mul_mat_vec_q5_1_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11820. break;
  11821. case GGML_TYPE_Q8_0:
  11822. dequantize_mul_mat_vec_q8_0_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11823. break;
  11824. case GGML_TYPE_Q2_K:
  11825. dequantize_mul_mat_vec_q2_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11826. break;
  11827. case GGML_TYPE_Q3_K:
  11828. dequantize_mul_mat_vec_q3_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11829. break;
  11830. case GGML_TYPE_Q4_K:
  11831. dequantize_mul_mat_vec_q4_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11832. break;
  11833. case GGML_TYPE_Q5_K:
  11834. dequantize_mul_mat_vec_q5_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11835. break;
  11836. case GGML_TYPE_Q6_K:
  11837. dequantize_mul_mat_vec_q6_K_sycl(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
  11838. break;
  11839. case GGML_TYPE_F16:
  11840. convert_mul_mat_vec_f16_sycl(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
  11841. break;
  11842. default:
  11843. GGML_ASSERT(false);
  11844. break;
  11845. }
  11846. (void) src1;
  11847. (void) dst;
  11848. (void) src1_ddq_i;
  11849. (void) src1_ncols;
  11850. (void) src1_padded_row_size;
  11851. }
  11852. inline void ggml_sycl_op_mul_mat_sycl(
  11853. const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  11854. const char *src0_dd_i, const float *src1_ddf_i, const char *src1_ddq_i,
  11855. float *dst_dd_i, const int64_t row_low, const int64_t row_high,
  11856. const int64_t src1_ncols, const int64_t src1_padded_row_size,
  11857. const dpct::queue_ptr &stream) try {
  11858. GGML_ASSERT(src0_dd_i != nullptr);
  11859. GGML_ASSERT(src1_ddf_i != nullptr);
  11860. GGML_ASSERT(dst_dd_i != nullptr);
  11861. const int64_t ne00 = src0->ne[0];
  11862. const int64_t ne10 = src1->ne[0];
  11863. const int64_t ne0 = dst->ne[0];
  11864. const int64_t row_diff = row_high - row_low;
  11865. int id;
  11866. SYCL_CHECK(
  11867. CHECK_TRY_ERROR(id = get_current_device_id()));
  11868. // the main device has a larger memory buffer to hold the results from all GPUs
  11869. // ldc == nrows of the matrix that cuBLAS writes into
  11870. int ldc = dst->backend == GGML_BACKEND_TYPE_GPU && id == g_main_device ? ne0 : row_diff;
  11871. #ifdef GGML_SYCL_F16
  11872. bool use_fp16 = true; // TODO(Yu) SYCL capability check
  11873. #else
  11874. bool use_fp16 = false;
  11875. #endif
  11876. if ((src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  11877. use_fp16 && ggml_is_contiguous(src0) && row_diff == src0->ne[1] &&
  11878. dst->op_params[0] == GGML_PREC_DEFAULT) {
  11879. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp16 path\n");
  11880. sycl_pool_alloc<sycl::half> src0_as_f16;
  11881. if (src0->type != GGML_TYPE_F16) {
  11882. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src0->type);
  11883. GGML_ASSERT(to_fp16_sycl != nullptr);
  11884. size_t ne = row_diff*ne00;
  11885. src0_as_f16.alloc(ne);
  11886. to_fp16_sycl(src0_dd_i, src0_as_f16.get(), ne, stream);
  11887. }
  11888. const sycl::half *src0_ptr = src0->type == GGML_TYPE_F16
  11889. ? (const sycl::half *)src0_dd_i
  11890. : src0_as_f16.get();
  11891. sycl_pool_alloc<sycl::half> src1_as_f16;
  11892. if (src1->type != GGML_TYPE_F16) {
  11893. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  11894. GGML_ASSERT(to_fp16_sycl != nullptr);
  11895. size_t ne = src1_ncols*ne10;
  11896. src1_as_f16.alloc(ne);
  11897. to_fp16_sycl(src1_ddf_i, src1_as_f16.get(), ne, stream);
  11898. }
  11899. const sycl::half *src1_ptr = src1->type == GGML_TYPE_F16
  11900. ? (const sycl::half *)src1->data + src1_padded_row_size
  11901. : src1_as_f16.get();
  11902. sycl_pool_alloc<sycl::half> dst_f16(row_diff * src1_ncols);
  11903. const sycl::half alpha_f16 = 1.0f;
  11904. const sycl::half beta_f16 = 0.0f;
  11905. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  11906. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm(
  11907. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  11908. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  11909. &alpha_f16, src0_ptr, dpct::library_data_t::real_half, ne00,
  11910. src1_ptr, dpct::library_data_t::real_half, ne10, &beta_f16,
  11911. dst_f16.get(), dpct::library_data_t::real_half, ldc,
  11912. dpct::library_data_t::real_half)));
  11913. g_sycl_handles[id]->wait();
  11914. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  11915. to_fp32_sycl(dst_f16.get(), dst_dd_i, row_diff*src1_ncols, stream);
  11916. }
  11917. else {
  11918. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat_sycl - fp32 path\n");
  11919. sycl_pool_alloc<float> src0_ddq_as_f32;
  11920. sycl_pool_alloc<float> src1_ddq_as_f32;
  11921. if (src0->type != GGML_TYPE_F32) {
  11922. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src0->type);
  11923. GGML_ASSERT(to_fp32_sycl != nullptr);
  11924. src0_ddq_as_f32.alloc(row_diff*ne00);
  11925. to_fp32_sycl(src0_dd_i, src0_ddq_as_f32.get(), row_diff*ne00, stream);
  11926. }
  11927. if (src1->type != GGML_TYPE_F32) {
  11928. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(src1->type);
  11929. GGML_ASSERT(to_fp32_sycl != nullptr);
  11930. src1_ddq_as_f32.alloc(src1_ncols*ne10);
  11931. to_fp32_sycl(src1_ddf_i, src1_ddq_as_f32.get(), src1_ncols*ne10, stream);
  11932. }
  11933. const float * src0_ddf_i = src0->type == GGML_TYPE_F32 ? (const float *) src0_dd_i : src0_ddq_as_f32.get();
  11934. const float * src1_ddf1_i = src1->type == GGML_TYPE_F32 ? (const float *) src1_ddf_i : src1_ddq_as_f32.get();
  11935. const float alpha = 1.0f;
  11936. const float beta = 0.0f;
  11937. SYCL_CHECK(CHECK_TRY_ERROR(g_sycl_handles[id] = stream));
  11938. SYCL_CHECK(CHECK_TRY_ERROR(oneapi::mkl::blas::column_major::gemm(
  11939. *g_sycl_handles[id], oneapi::mkl::transpose::trans,
  11940. oneapi::mkl::transpose::nontrans, row_diff, src1_ncols, ne10,
  11941. dpct::get_value(&alpha, *g_sycl_handles[id]), src0_ddf_i, ne00,
  11942. src1_ddf1_i, ne10, dpct::get_value(&beta, *g_sycl_handles[id]),
  11943. dst_dd_i, ldc)));
  11944. g_sycl_handles[id]->wait();
  11945. }
  11946. (void) dst;
  11947. (void) src1_ddq_i;
  11948. (void) src1_padded_row_size;
  11949. }
  11950. catch (sycl::exception const &exc) {
  11951. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  11952. << ", line:" << __LINE__ << std::endl;
  11953. std::exit(1);
  11954. }
  11955. inline void ggml_sycl_op_rope(const ggml_tensor *src0, const ggml_tensor *src1,
  11956. ggml_tensor *dst, const float *src0_dd,
  11957. const float *src1_dd, float *dst_dd,
  11958. const dpct::queue_ptr &main_stream) {
  11959. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  11960. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  11961. GGML_ASSERT(src0->type == dst->type);
  11962. const int64_t ne00 = src0->ne[0];
  11963. const int64_t ne01 = src0->ne[1];
  11964. const int64_t ne2 = dst->ne[2];
  11965. const int64_t nrows = ggml_nrows(src0);
  11966. //const int n_past = ((int32_t *) dst->op_params)[0];
  11967. const int n_dims = ((int32_t *) dst->op_params)[1];
  11968. const int mode = ((int32_t *) dst->op_params)[2];
  11969. const int n_ctx = ((int32_t *) dst->op_params)[3];
  11970. const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
  11971. // RoPE alteration for extended context
  11972. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  11973. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  11974. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  11975. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  11976. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  11977. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  11978. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  11979. const int32_t * pos = nullptr;
  11980. if ((mode & 1) == 0) {
  11981. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  11982. GGML_ASSERT(src1->ne[0] == ne2);
  11983. pos = (const int32_t *) src1_dd;
  11984. }
  11985. const bool is_neox = mode & 2;
  11986. const bool is_glm = mode & 4;
  11987. rope_corr_dims corr_dims;
  11988. ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
  11989. // compute
  11990. if (is_glm) {
  11991. GGML_ASSERT(false);
  11992. rope_glm_f32_sycl(src0_dd, dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, main_stream);
  11993. } else if (is_neox) {
  11994. if (src0->type == GGML_TYPE_F32) {
  11995. rope_neox_sycl(
  11996. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  11997. attn_factor, corr_dims, main_stream
  11998. );
  11999. } else if (src0->type == GGML_TYPE_F16) {
  12000. rope_neox_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd,
  12001. ne00, n_dims, nrows, pos, freq_scale, ne01,
  12002. freq_base, ext_factor, attn_factor, corr_dims,
  12003. main_stream);
  12004. } else {
  12005. GGML_ASSERT(false);
  12006. }
  12007. } else {
  12008. if (src0->type == GGML_TYPE_F32) {
  12009. rope_sycl(
  12010. (const float *)src0_dd, (float *)dst_dd, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12011. attn_factor, corr_dims, main_stream
  12012. );
  12013. } else if (src0->type == GGML_TYPE_F16) {
  12014. rope_sycl((const sycl::half *)src0_dd, (sycl::half *)dst_dd, ne00,
  12015. nrows, pos, freq_scale, ne01, freq_base, ext_factor,
  12016. attn_factor, corr_dims, main_stream);
  12017. } else {
  12018. GGML_ASSERT(false);
  12019. }
  12020. }
  12021. (void) src1;
  12022. (void) dst;
  12023. (void) src1_dd;
  12024. }
  12025. inline void ggml_sycl_op_alibi(const ggml_tensor *src0, const ggml_tensor *src1,
  12026. ggml_tensor *dst, const float *src0_dd,
  12027. const float *src1_dd, float *dst_dd,
  12028. const dpct::queue_ptr &main_stream) {
  12029. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12030. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12031. GGML_TENSOR_LOCALS_3(int64_t, ne0, src0, ne);
  12032. const int64_t nrows = ggml_nrows(src0);
  12033. //const int n_past = ((int32_t *) dst->op_params)[0];
  12034. const int n_head = ((int32_t *) dst->op_params)[1];
  12035. float max_bias;
  12036. memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
  12037. //GGML_ASSERT(ne01 + n_past == ne00);
  12038. GGML_ASSERT(n_head == ne02);
  12039. const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
  12040. const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
  12041. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
  12042. alibi_f32_sycl(src0_dd, dst_dd, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, main_stream);
  12043. (void) src1;
  12044. (void) src1_dd;
  12045. }
  12046. static void ggml_sycl_op_pool2d(const ggml_tensor *src0,
  12047. const ggml_tensor *src1, ggml_tensor *dst,
  12048. const float *src0_dd, const float *src1_dd,
  12049. float *dst_dd, const dpct::queue_ptr &main_stream) {
  12050. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12051. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12052. const int32_t * opts = (const int32_t *)dst->op_params;
  12053. enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
  12054. const int k0 = opts[1];
  12055. const int k1 = opts[2];
  12056. const int s0 = opts[3];
  12057. const int s1 = opts[4];
  12058. const int p0 = opts[5];
  12059. const int p1 = opts[6];
  12060. const int64_t IH = src0->ne[1];
  12061. const int64_t IW = src0->ne[0];
  12062. const int64_t N = dst->ne[3];
  12063. const int64_t OC = dst->ne[2];
  12064. const int64_t OH = dst->ne[1];
  12065. const int64_t OW = dst->ne[0];
  12066. const int parallel_elements = N * OC * OH * OW;
  12067. const int num_blocks = (parallel_elements + SYCL_POOL2D_BLOCK_SIZE - 1) / SYCL_POOL2D_BLOCK_SIZE;
  12068. sycl::range<3> block_nums(1, 1, num_blocks);
  12069. main_stream->parallel_for(
  12070. sycl::nd_range<3>(block_nums *
  12071. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE),
  12072. sycl::range<3>(1, 1, SYCL_IM2COL_BLOCK_SIZE)),
  12073. [=](sycl::nd_item<3> item_ct1) {
  12074. pool2d_nchw_kernel(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0,
  12075. parallel_elements, src0_dd, dst_dd, op,
  12076. item_ct1);
  12077. });
  12078. (void) src1;
  12079. (void) src1_dd;
  12080. }
  12081. inline void ggml_sycl_op_im2col(const ggml_tensor *src0,
  12082. const ggml_tensor *src1, ggml_tensor *dst,
  12083. const float *src0_dd, const float *src1_dd,
  12084. float *dst_dd,
  12085. const dpct::queue_ptr &main_stream) {
  12086. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12087. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12088. GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
  12089. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  12090. const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
  12091. const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
  12092. const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
  12093. const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
  12094. const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
  12095. const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
  12096. const int64_t IC = src1->ne[is_2D ? 2 : 1];
  12097. const int64_t IH = is_2D ? src1->ne[1] : 1;
  12098. const int64_t IW = src1->ne[0];
  12099. const int64_t KH = is_2D ? src0->ne[1] : 1;
  12100. const int64_t KW = src0->ne[0];
  12101. const int64_t OH = is_2D ? dst->ne[2] : 1;
  12102. const int64_t OW = dst->ne[1];
  12103. const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
  12104. if (dst->type == GGML_TYPE_F16) {
  12105. 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);
  12106. } else {
  12107. im2col_sycl(src1_dd, (float *)dst_dd, IW, IH, OW, OH, KW, KH, IC, delta_offset, s0, s1, p0, p1, d0, d1, main_stream);
  12108. }
  12109. (void) src0;
  12110. (void) src0_dd;
  12111. }
  12112. inline void ggml_sycl_op_sum_rows(const ggml_tensor *src0,
  12113. const ggml_tensor *src1, ggml_tensor *dst,
  12114. const float *src0_dd, const float *src1_dd,
  12115. float *dst_dd,
  12116. const dpct::queue_ptr &main_stream) {
  12117. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12118. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12119. const int64_t ncols = src0->ne[0];
  12120. const int64_t nrows = ggml_nrows(src0);
  12121. sum_rows_f32_sycl(src0_dd, dst_dd, ncols, nrows, main_stream);
  12122. (void) src1;
  12123. (void) dst;
  12124. (void) src1_dd;
  12125. }
  12126. inline void ggml_sycl_op_argsort(const ggml_tensor *src0,
  12127. const ggml_tensor *src1, ggml_tensor *dst,
  12128. const float *src0_dd, const float *src1_dd,
  12129. float *dst_dd,
  12130. const dpct::queue_ptr &main_stream) {
  12131. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12132. GGML_ASSERT( dst->type == GGML_TYPE_I32);
  12133. const int64_t ncols = src0->ne[0];
  12134. const int64_t nrows = ggml_nrows(src0);
  12135. enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
  12136. argsort_f32_i32_sycl(src0_dd, (int *)dst_dd, ncols, nrows, order, main_stream);
  12137. (void) src1;
  12138. (void) dst;
  12139. (void) src1_dd;
  12140. }
  12141. inline void ggml_sycl_op_diag_mask_inf(const ggml_tensor *src0,
  12142. const ggml_tensor *src1,
  12143. ggml_tensor *dst, const float *src0_dd,
  12144. const float *src1_dd, float *dst_dd,
  12145. const dpct::queue_ptr &main_stream) {
  12146. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12147. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12148. const int64_t ne00 = src0->ne[0];
  12149. const int64_t ne01 = src0->ne[1];
  12150. const int nrows0 = ggml_nrows(src0);
  12151. const int n_past = ((int32_t *) dst->op_params)[0];
  12152. diag_mask_inf_f32_sycl(src0_dd, dst_dd, ne00, nrows0, ne01, n_past, main_stream);
  12153. (void) src1;
  12154. (void) dst;
  12155. (void) src1_dd;
  12156. }
  12157. inline void ggml_sycl_op_soft_max(const ggml_tensor *src0,
  12158. const ggml_tensor *src1, ggml_tensor *dst,
  12159. const float *src0_dd, const float *src1_dd,
  12160. float *dst_dd,
  12161. const dpct::queue_ptr &main_stream) {
  12162. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12163. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12164. GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
  12165. const int64_t ne00 = src0->ne[0];
  12166. const int64_t nrows_x = ggml_nrows(src0);
  12167. const int64_t nrows_y = src0->ne[1];
  12168. float scale = 1.0f;
  12169. float max_bias = 0.0f;
  12170. memcpy(&scale, dst->op_params + 0, sizeof(float));
  12171. memcpy(&max_bias, dst->op_params + 1, sizeof(float));
  12172. // positions tensor
  12173. float * src2_dd = nullptr;
  12174. sycl_pool_alloc<float> src2_f;
  12175. ggml_tensor * src2 = dst->src[2];
  12176. const bool use_src2 = src2 != nullptr;
  12177. if (use_src2) {
  12178. const bool src2_on_device = src2->backend == GGML_BACKEND_TYPE_GPU;
  12179. if (src2_on_device) {
  12180. ggml_tensor_extra_gpu * src2_extra = (ggml_tensor_extra_gpu *) src2->extra;
  12181. src2_dd = (float *) src2_extra->data_device[g_main_device];
  12182. } else {
  12183. src2_dd = src2_f.alloc(ggml_nelements(src2));
  12184. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src2_dd, src2, 0, 0, 0, 1, main_stream));
  12185. }
  12186. }
  12187. soft_max_f32_sycl(src0_dd, src1 ? src1_dd : nullptr, src2_dd, dst_dd, ne00,
  12188. nrows_x, nrows_y, scale, max_bias, main_stream);
  12189. }
  12190. inline void ggml_sycl_op_scale(const ggml_tensor *src0, const ggml_tensor *src1,
  12191. ggml_tensor *dst, const float *src0_dd,
  12192. const float *src1_dd, float *dst_dd,
  12193. const dpct::queue_ptr &main_stream) {
  12194. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12195. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12196. float scale;
  12197. memcpy(&scale, dst->op_params, sizeof(float));
  12198. scale_f32_sycl(src0_dd, dst_dd, scale, ggml_nelements(src0), main_stream);
  12199. /*
  12200. DPCT1010:87: SYCL uses exceptions to report errors and does not use the
  12201. error codes. The call was replaced with 0. You need to rewrite this code.
  12202. */
  12203. SYCL_CHECK(0);
  12204. (void) src1;
  12205. (void) dst;
  12206. (void) src1_dd;
  12207. }
  12208. inline void ggml_sycl_op_clamp(const ggml_tensor *src0, const ggml_tensor *src1,
  12209. ggml_tensor *dst, const float *src0_dd,
  12210. const float *src1_dd, float *dst_dd,
  12211. const dpct::queue_ptr &main_stream) {
  12212. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  12213. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  12214. float min;
  12215. float max;
  12216. memcpy(&min, dst->op_params, sizeof(float));
  12217. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  12218. clamp_f32_sycl(src0_dd, dst_dd, min, max, ggml_nelements(src0), main_stream);
  12219. /*
  12220. DPCT1010:88: SYCL uses exceptions to report errors and does not use the
  12221. error codes. The call was replaced with 0. You need to rewrite this code.
  12222. */
  12223. SYCL_CHECK(0);
  12224. (void) src1;
  12225. (void) dst;
  12226. (void) src1_dd;
  12227. }
  12228. static void ggml_sycl_op_flatten(const ggml_tensor *src0,
  12229. const ggml_tensor *src1, ggml_tensor *dst,
  12230. const ggml_sycl_op_flatten_t op) try {
  12231. const int64_t nrows0 = ggml_nrows(src0);
  12232. const bool use_src1 = src1 != nullptr;
  12233. const int64_t nrows1 = use_src1 ? ggml_nrows(src1) : 1;
  12234. GGML_ASSERT(!use_src1 || src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12235. GGML_ASSERT( dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12236. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12237. ggml_tensor_extra_gpu * src1_extra = use_src1 ? (ggml_tensor_extra_gpu *) src1->extra : nullptr;
  12238. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12239. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12240. const bool src1_on_device = use_src1 && src1->backend == GGML_BACKEND_TYPE_GPU;
  12241. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU;
  12242. // dd = data device
  12243. float * src0_ddf = nullptr;
  12244. float * src1_ddf = nullptr;
  12245. float * dst_ddf = nullptr;
  12246. sycl_pool_alloc<float> src0_f;
  12247. sycl_pool_alloc<float> src1_f;
  12248. sycl_pool_alloc<float> dst_f;
  12249. ggml_sycl_set_device(g_main_device);
  12250. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12251. // GGML_SYCL_DEBUG("g_main_device=%d, main_stream=%p src0_on_device=%d, src1_on_device=%d, dst_on_device=%d\n",
  12252. // g_main_device, main_stream, src0_on_device, src1_on_device, dst_on_device);
  12253. if (src0_on_device) {
  12254. src0_ddf = (float *) src0_extra->data_device[g_main_device];
  12255. } else {
  12256. src0_ddf = src0_f.alloc(ggml_nelements(src0));
  12257. // GGML_SYCL_DEBUG("before ggml_sycl_cpy_tensor_2d src0_ddf=%p, src0=%p\n", src0_ddf, src0);
  12258. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_ddf, src0, 0, 0, 0, nrows0, main_stream));
  12259. }
  12260. if (use_src1) {
  12261. if (src1_on_device) {
  12262. src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12263. } else {
  12264. src1_ddf = src1_f.alloc(ggml_nelements(src1));
  12265. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src1_ddf, src1, 0, 0, 0, nrows1, main_stream));
  12266. }
  12267. }
  12268. if (dst_on_device) {
  12269. dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12270. } else {
  12271. dst_ddf = dst_f.alloc(ggml_nelements(dst));
  12272. }
  12273. // GGML_SYCL_DEBUG("op src0=%p, src1=%p, dst=%p, src0_ddf=%p, src1_ddf=%p, dst_ddf=%p, main_stream=%p\n",
  12274. // src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  12275. // do the computation
  12276. op(src0, src1, dst, src0_ddf, src1_ddf, dst_ddf, main_stream);
  12277. /*
  12278. DPCT1010:89: SYCL uses exceptions to report errors and does not use the
  12279. error codes. The call was replaced with 0. You need to rewrite this code.
  12280. */
  12281. SYCL_CHECK(0);
  12282. // copy dst to host if necessary
  12283. if (!dst_on_device) {
  12284. SYCL_CHECK(CHECK_TRY_ERROR(
  12285. main_stream->memcpy(dst->data, dst_ddf, ggml_nbytes(dst)).wait()));
  12286. }
  12287. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12288. SYCL_CHECK(CHECK_TRY_ERROR(
  12289. dpct::get_current_device().queues_wait_and_throw()));
  12290. }
  12291. // print_ggml_tensor("tensor", dst);
  12292. }
  12293. catch (sycl::exception const &exc) {
  12294. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12295. << ", line:" << __LINE__ << std::endl;
  12296. std::exit(1);
  12297. }
  12298. static void ggml_sycl_set_peer_access(const int n_tokens) {
  12299. static bool peer_access_enabled = false;
  12300. const bool enable_peer_access = n_tokens <= GGML_SYCL_PEER_MAX_BATCH_SIZE;
  12301. if (peer_access_enabled == enable_peer_access) {
  12302. return;
  12303. }
  12304. #ifdef NDEBUG
  12305. for (int i = 0; i < g_device_count; ++i) {
  12306. SYCL_CHECK(ggml_sycl_set_device(i));
  12307. // SYCL_CHECK(syclDeviceSynchronize());
  12308. }
  12309. for (int i = 0; i < g_device_count; ++i) {
  12310. SYCL_CHECK(ggml_sycl_set_device(i));
  12311. for (int id_other = 0; id_other < g_device_count; ++id_other) {
  12312. if (i == id_other) {
  12313. continue;
  12314. }
  12315. if (i != g_main_device && id_other != g_main_device) {
  12316. continue;
  12317. }
  12318. // int can_access_peer;
  12319. // SYCL_CHECK(syclDeviceCanAccessPeer(&can_access_peer, id, id_other));
  12320. // if (can_access_peer) {
  12321. // if (enable_peer_access) {
  12322. // SYCL_CHECK(syclDeviceEnablePeerAccess(id_other, 0));
  12323. // } else {
  12324. // SYCL_CHECK(syclDeviceDisablePeerAccess(id_other));
  12325. // }
  12326. // }
  12327. }
  12328. }
  12329. #endif // NDEBUG
  12330. peer_access_enabled = enable_peer_access;
  12331. }
  12332. struct ggml_backend_sycl_split_buffer_type_context {
  12333. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  12334. };
  12335. static void ggml_sycl_op_mul_mat(const ggml_tensor *src0,
  12336. const ggml_tensor *src1, ggml_tensor *dst,
  12337. ggml_sycl_op_mul_mat_t op,
  12338. const bool convert_src1_to_q8_1) try {
  12339. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne);
  12340. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  12341. const int64_t nrows1 = ggml_nrows(src1);
  12342. GGML_ASSERT(ne03 == ne13);
  12343. const int64_t ne0 = dst->ne[0];
  12344. const int64_t ne1 = dst->ne[1];
  12345. const int nb2 = dst->nb[2];
  12346. const int nb3 = dst->nb[3];
  12347. GGML_ASSERT(dst->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12348. GGML_ASSERT(src1->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12349. GGML_ASSERT(src1->type == GGML_TYPE_F32 || (src1->ne[2] == 1 && src1->ne[3] == 1));
  12350. GGML_ASSERT(ne12 >= ne02 && ne12 % ne02 == 0);
  12351. const int64_t i02_divisor = ne12 / ne02;
  12352. const size_t src0_ts = ggml_type_size(src0->type);
  12353. const size_t src0_bs = ggml_blck_size(src0->type);
  12354. const size_t q8_1_ts = sizeof(block_q8_1);
  12355. const size_t q8_1_bs = QK8_1;
  12356. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12357. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12358. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12359. const bool src0_on_device = src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12360. const bool src0_is_contiguous = ggml_is_contiguous(src0);
  12361. const bool src1_is_contiguous = ggml_is_contiguous(src1);
  12362. int64_t src1_padded_col_size = GGML_PAD(ne10, MATRIX_ROW_PADDING);
  12363. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12364. GGML_ASSERT(!(split && ne02 > 1));
  12365. GGML_ASSERT(!(split && ne03 > 1));
  12366. GGML_ASSERT(!(split && ne02 < ne12));
  12367. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split;
  12368. if (split) {
  12369. // TODO: check that src0->buffer->buft is a split buffer type, replace GGML_BACKEND_TYPE_GPU_SPLIT check
  12370. // GGML_ASSERT(src0->buffer != nullptr && src0->buffer->buft == ...);
  12371. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *) src0->buffer->buft->context;
  12372. tensor_split = buft_ctx->tensor_split;
  12373. }
  12374. struct dev_data {
  12375. sycl_pool_alloc<char> src0_dd_alloc;
  12376. sycl_pool_alloc<float> src1_ddf_alloc;
  12377. sycl_pool_alloc<char> src1_ddq_alloc;
  12378. sycl_pool_alloc<float> dst_dd_alloc;
  12379. char *src0_dd = nullptr;
  12380. float *src1_ddf = nullptr; // float
  12381. char *src1_ddq = nullptr; // q8_1
  12382. float *dst_dd = nullptr;
  12383. int64_t row_low;
  12384. int64_t row_high;
  12385. };
  12386. dev_data dev[GGML_SYCL_MAX_DEVICES];
  12387. int used_devices = 0;
  12388. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12389. for (int i = 0; i < g_device_count; ++i) {
  12390. // by default, use all rows
  12391. dev[i].row_low = 0;
  12392. dev[i].row_high = ne01;
  12393. // for multi GPU, get the row boundaries from tensor split
  12394. // and round to mul_mat_q tile sizes
  12395. if (split) {
  12396. const int64_t rounding = get_row_rounding(src0->type, tensor_split);
  12397. if (i != 0) {
  12398. dev[i].row_low = ne01*tensor_split[i];
  12399. if (dev[i].row_low < ne01) {
  12400. dev[i].row_low -= dev[i].row_low % rounding;
  12401. }
  12402. }
  12403. if (i != g_device_count - 1) {
  12404. dev[i].row_high = ne01*tensor_split[i + 1];
  12405. if (dev[i].row_high < ne01) {
  12406. dev[i].row_high -= dev[i].row_high % rounding;
  12407. }
  12408. }
  12409. }
  12410. }
  12411. for (int i = 0; i < g_device_count; ++i) {
  12412. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  12413. continue;
  12414. }
  12415. used_devices++;
  12416. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12417. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12418. ggml_sycl_set_device(i);
  12419. dpct::queue_ptr stream = g_syclStreams[i][0];
  12420. if (src0_on_device && src0_is_contiguous) {
  12421. dev[i].src0_dd = (char *) src0_extra->data_device[i];
  12422. } else {
  12423. dev[i].src0_dd = dev[i].src0_dd_alloc.alloc(ggml_nbytes(src0));
  12424. }
  12425. if (src1_on_device && src1_is_contiguous) {
  12426. dev[i].src1_ddf = (float *) src1_extra->data_device[i];
  12427. } else {
  12428. dev[i].src1_ddf = dev[i].src1_ddf_alloc.alloc(ggml_nelements(src1));
  12429. }
  12430. if (convert_src1_to_q8_1) {
  12431. dev[i].src1_ddq = dev[i].src1_ddq_alloc.alloc(nrows1*src1_padded_col_size*q8_1_ts/q8_1_bs);
  12432. if (src1_on_device && src1_is_contiguous) {
  12433. quantize_row_q8_1_sycl(dev[i].src1_ddf, dev[i].src1_ddq, ne10, nrows1, src1_padded_col_size, stream);
  12434. /*
  12435. DPCT1010:90: SYCL uses exceptions to report errors and does not
  12436. use the error codes. The call was replaced with 0. You need to
  12437. rewrite this code.
  12438. */
  12439. SYCL_CHECK(0);
  12440. }
  12441. }
  12442. if (dst_on_device) {
  12443. dev[i].dst_dd = (float *) dst_extra->data_device[i];
  12444. } else {
  12445. const size_t size_dst_ddf = split ? (dev[i].row_high - dev[i].row_low)*ne1 : ggml_nelements(dst);
  12446. dev[i].dst_dd = dev[i].dst_dd_alloc.alloc(size_dst_ddf);
  12447. }
  12448. }
  12449. // if multiple devices are used they need to wait for the main device
  12450. // here an event is recorded that signals that the main device has finished calculating the input data
  12451. if (split && used_devices > 1) {
  12452. ggml_sycl_set_device(g_main_device);
  12453. /*
  12454. DPCT1024:91: The original code returned the error code that was further
  12455. consumed by the program logic. This original code was replaced with 0.
  12456. You may need to rewrite the program logic consuming the error code.
  12457. */
  12458. SYCL_CHECK(CHECK_TRY_ERROR(
  12459. *src0_extra->events[g_main_device][0] =
  12460. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier()));
  12461. }
  12462. const int64_t src1_col_stride = split && used_devices > 1 ? MUL_MAT_SRC1_COL_STRIDE : ne11;
  12463. for (int64_t src1_col_0 = 0; src1_col_0 < ne11; src1_col_0 += src1_col_stride) {
  12464. const int64_t is = split ? (src1_col_0/src1_col_stride) % MAX_STREAMS : 0;
  12465. const int64_t src1_ncols = src1_col_0 + src1_col_stride > ne11 ? ne11 - src1_col_0 : src1_col_stride;
  12466. for (int i = 0; i < g_device_count; ++i) {
  12467. if ((!split && i != g_main_device) || dev[i].row_low == dev[i].row_high) {
  12468. continue;
  12469. }
  12470. const bool src1_on_device = src1->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12471. const bool dst_on_device = dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device;
  12472. const int64_t row_diff = dev[i].row_high - dev[i].row_low;
  12473. ggml_sycl_set_device(i);
  12474. dpct::queue_ptr stream = g_syclStreams[i][is];
  12475. // wait for main GPU data if necessary
  12476. if (split && (i != g_main_device || is != 0)) {
  12477. /*
  12478. DPCT1009:163: SYCL uses exceptions to report errors and does not
  12479. use the error codes. The original code was commented out and a
  12480. warning string was inserted. You need to rewrite this code.
  12481. */
  12482. SYCL_CHECK(CHECK_TRY_ERROR(stream->ext_oneapi_submit_barrier(
  12483. {*src0_extra->events[g_main_device][0]})));
  12484. }
  12485. for (int64_t i0 = 0; i0 < ne13*ne12; ++i0) {
  12486. const int64_t i03 = i0 / ne12;
  12487. const int64_t i02 = i0 % ne12;
  12488. const size_t src1_ddq_i_offset = (i0*ne11 + src1_col_0) * src1_padded_col_size*q8_1_ts/q8_1_bs;
  12489. // for split tensors the data begins at i0 == i0_offset_low
  12490. char * src0_dd_i = dev[i].src0_dd + (i0/i02_divisor) * (ne01*ne00*src0_ts)/src0_bs;
  12491. float * src1_ddf_i = dev[i].src1_ddf + (i0*ne11 + src1_col_0) * ne10;
  12492. char * src1_ddq_i = dev[i].src1_ddq + src1_ddq_i_offset;
  12493. float * dst_dd_i = dev[i].dst_dd + (i0*ne1 + src1_col_0) * (dst_on_device ? ne0 : row_diff);
  12494. // the main device memory buffer can be on VRAM scratch, with space for all partial results
  12495. // in that case an offset on dst_ddf_i is needed
  12496. if (dst->backend == GGML_BACKEND_TYPE_GPU && i == g_main_device) {
  12497. dst_dd_i += dev[i].row_low; // offset is 0 if no tensor split
  12498. }
  12499. // copy src0, src1 to device if necessary
  12500. if (src1->backend == GGML_BACKEND_TYPE_GPU && src1_is_contiguous) {
  12501. if (i != g_main_device) {
  12502. if (convert_src1_to_q8_1) {
  12503. char * src1_ddq_i_source = dev[g_main_device].src1_ddq + src1_ddq_i_offset;
  12504. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  12505. src1_ddq_i, src1_ddq_i_source,
  12506. src1_ncols * src1_padded_col_size * q8_1_ts /
  12507. q8_1_bs).wait()));
  12508. } else {
  12509. float * src1_ddf_i_source = (float *) src1_extra->data_device[g_main_device];
  12510. src1_ddf_i_source += (i0*ne11 + src1_col_0) * ne10;
  12511. SYCL_CHECK(CHECK_TRY_ERROR(dev2dev_memcpy(*stream, *main_stream,
  12512. src1_ddf_i, src1_ddf_i_source,
  12513. src1_ncols * ne10 * sizeof(float))));
  12514. }
  12515. }
  12516. } else if (src1->backend == GGML_BACKEND_TYPE_CPU || (src1_on_device && !src1_is_contiguous)) {
  12517. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(
  12518. src1_ddf_i, src1, i03, i02, src1_col_0, src1_col_0+src1_ncols, stream));
  12519. } else {
  12520. GGML_ASSERT(false);
  12521. }
  12522. if (convert_src1_to_q8_1 && (src1->backend == GGML_BACKEND_TYPE_CPU || !src1_is_contiguous)) {
  12523. quantize_row_q8_1_sycl(src1_ddf_i, src1_ddq_i, ne10, src1_ncols, src1_padded_col_size, stream);
  12524. /*
  12525. DPCT1010:92: SYCL uses exceptions to report errors and does
  12526. not use the error codes. The call was replaced with 0. You
  12527. need to rewrite this code.
  12528. */
  12529. SYCL_CHECK(0);
  12530. }
  12531. if (src1_col_0 == 0 && (!src0_on_device || !src0_is_contiguous) && i02 % i02_divisor == 0) {
  12532. SYCL_CHECK(ggml_sycl_cpy_tensor_2d(src0_dd_i, src0, i03, i02/i02_divisor, dev[i].row_low, dev[i].row_high, stream));
  12533. }
  12534. if (src1->type == GGML_TYPE_F16) {
  12535. src1_padded_col_size = (i0 * ne11 + src1_col_0) * ne10;
  12536. }
  12537. // do the computation
  12538. op(src0, src1, dst, src0_dd_i, src1_ddf_i, src1_ddq_i, dst_dd_i,
  12539. dev[i].row_low, dev[i].row_high, src1_ncols, src1_padded_col_size, stream);
  12540. /*
  12541. DPCT1010:93: SYCL uses exceptions to report errors and does not
  12542. use the error codes. The call was replaced with 0. You need to
  12543. rewrite this code.
  12544. */
  12545. SYCL_CHECK(0);
  12546. // copy dst to host or other device if necessary
  12547. if (!dst_on_device) {
  12548. void * dst_off_device;
  12549. dpct::memcpy_direction kind;
  12550. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12551. dst_off_device = dst->data;
  12552. kind = dpct::device_to_host;
  12553. } else if (dst->backend == GGML_BACKEND_TYPE_GPU) {
  12554. dst_off_device = dst_extra->data_device[g_main_device];
  12555. kind = dpct::device_to_device;
  12556. } else {
  12557. GGML_ASSERT(false);
  12558. }
  12559. if (split) {
  12560. // src0 = weight matrix is saved as a transposed matrix for better memory layout.
  12561. // dst is NOT transposed.
  12562. // The outputs of matrix matrix multiplications can therefore NOT simply be concatenated for >1 GPU.
  12563. // Instead they need to be copied to the correct slice in ne0 = dst row index.
  12564. // If dst is a vector with ne0 == 1 then you don't have to do this but it still produces correct results.
  12565. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  12566. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  12567. dhf_dst_i += src1_col_0*ne0 + dev[i].row_low;
  12568. //todo, dirty solution. Need be updated when device2device memcpy() is supported.
  12569. if (kind == dpct::device_to_device) {
  12570. size_t dst_size = ggml_nbytes_pad(dst);
  12571. float *host_buf = (float *)malloc(dst_size);
  12572. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12573. host_buf, ne0 * sizeof(float), dst_dd_i,
  12574. row_diff * sizeof(float), row_diff * sizeof(float),
  12575. src1_ncols, dpct::device_to_host, *stream)));
  12576. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[i]).queues_wait_and_throw();
  12577. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12578. dhf_dst_i, ne0 * sizeof(float), host_buf,
  12579. row_diff * sizeof(float), row_diff * sizeof(float),
  12580. src1_ncols, dpct::host_to_device, *main_stream)));
  12581. dpct::dev_mgr::instance().get_device(g_sycl_gpu_mgr->gpus[g_main_device]).queues_wait_and_throw();
  12582. free(host_buf);
  12583. } else {
  12584. SYCL_CHECK(CHECK_TRY_ERROR(dpct::async_dpct_memcpy(
  12585. dhf_dst_i, ne0 * sizeof(float), dst_dd_i,
  12586. row_diff * sizeof(float), row_diff * sizeof(float),
  12587. src1_ncols, kind, *stream)));
  12588. }
  12589. } else {
  12590. float * dhf_dst_i = (float *) ((char *) dst_off_device + i02*nb2 + i03*nb3);
  12591. GGML_ASSERT(dst->nb[1] == ne0*sizeof(float));
  12592. dhf_dst_i += src1_col_0*ne0;
  12593. SYCL_CHECK(CHECK_TRY_ERROR(
  12594. stream->memcpy(dhf_dst_i, dst_dd_i,
  12595. src1_ncols * ne0 * sizeof(float)).wait()));
  12596. }
  12597. }
  12598. // add event for the main device to wait on until other device is done
  12599. if (split && (i != g_main_device || is != 0)) {
  12600. /*
  12601. DPCT1024:94: The original code returned the error code that
  12602. was further consumed by the program logic. This original
  12603. code was replaced with 0. You may need to rewrite the
  12604. program logic consuming the error code.
  12605. */
  12606. SYCL_CHECK(CHECK_TRY_ERROR(
  12607. *src0_extra->events[i][is] =
  12608. stream->ext_oneapi_submit_barrier()));
  12609. }
  12610. }
  12611. }
  12612. }
  12613. // main device waits for all other devices to be finished
  12614. if (split && g_device_count > 1) {
  12615. int64_t is_max = (ne11 + MUL_MAT_SRC1_COL_STRIDE - 1) / MUL_MAT_SRC1_COL_STRIDE;
  12616. is_max = is_max <= MAX_STREAMS ? is_max : MAX_STREAMS;
  12617. ggml_sycl_set_device(g_main_device);
  12618. for (int i = 0; i < g_device_count; ++i) {
  12619. if (dev[i].row_low == dev[i].row_high) {
  12620. continue;
  12621. }
  12622. for (int64_t is = 0; is < is_max; ++is) {
  12623. SYCL_CHECK(CHECK_TRY_ERROR(
  12624. g_syclStreams[g_main_device][0]->ext_oneapi_submit_barrier(
  12625. {*src0_extra->events[i][is]})));
  12626. }
  12627. }
  12628. }
  12629. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  12630. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12631. SYCL_CHECK(CHECK_TRY_ERROR(
  12632. dpct::get_current_device().queues_wait_and_throw()));
  12633. }
  12634. }
  12635. catch (sycl::exception const &exc) {
  12636. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12637. << ", line:" << __LINE__ << std::endl;
  12638. std::exit(1);
  12639. }
  12640. static void ggml_sycl_repeat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12641. GGML_SYCL_DEBUG("call %s\n", __func__);
  12642. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_repeat);
  12643. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12644. }
  12645. static void ggml_sycl_get_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12646. GGML_SYCL_DEBUG("call %s\n", __func__);
  12647. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_get_rows);
  12648. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12649. }
  12650. static void ggml_sycl_add(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12651. GGML_SYCL_DEBUG("call %s\n", __func__);
  12652. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_add);
  12653. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12654. }
  12655. static void ggml_sycl_acc(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12656. GGML_SYCL_DEBUG("call %s\n", __func__);
  12657. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_acc);
  12658. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12659. }
  12660. static void ggml_sycl_mul(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12661. GGML_SYCL_DEBUG("call %s\n", __func__);
  12662. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_mul);
  12663. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12664. }
  12665. static void ggml_sycl_div(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12666. GGML_SYCL_DEBUG("call %s\n", __func__);
  12667. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_div);
  12668. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12669. }
  12670. static void ggml_sycl_gelu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12671. GGML_SYCL_DEBUG("call %s\n", __func__);
  12672. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu);
  12673. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12674. }
  12675. static void ggml_sycl_silu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12676. GGML_SYCL_DEBUG("call %s\n", __func__);
  12677. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_silu);
  12678. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12679. }
  12680. static void ggml_sycl_gelu_quick(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12681. GGML_SYCL_DEBUG("call %s\n", __func__);
  12682. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_gelu_quick);
  12683. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12684. }
  12685. static void ggml_sycl_tanh(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12686. GGML_SYCL_DEBUG("call %s\n", __func__);
  12687. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_tanh);
  12688. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12689. }
  12690. static void ggml_sycl_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12691. GGML_SYCL_DEBUG("call %s\n", __func__);
  12692. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_relu);
  12693. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12694. }
  12695. static void ggml_sycl_hardsigmoid(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12696. GGML_SYCL_DEBUG("call %s\n", __func__);
  12697. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardsigmoid);
  12698. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12699. }
  12700. static void ggml_sycl_hardswish(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12701. GGML_SYCL_DEBUG("call %s\n", __func__);
  12702. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_hardswish);
  12703. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12704. }
  12705. static void ggml_sycl_leaky_relu(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12706. GGML_SYCL_DEBUG("call %s\n", __func__);
  12707. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_leaky_relu);
  12708. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12709. }
  12710. static void ggml_sycl_sqr(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12711. GGML_SYCL_DEBUG("call %s\n", __func__);
  12712. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sqr);
  12713. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12714. }
  12715. static void ggml_sycl_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12716. GGML_SYCL_DEBUG("call %s\n", __func__);
  12717. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_norm);
  12718. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12719. }
  12720. static void ggml_sycl_group_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12721. GGML_SYCL_DEBUG("call %s\n", __func__);
  12722. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_group_norm);
  12723. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12724. }
  12725. static void ggml_sycl_concat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12726. GGML_SYCL_DEBUG("call %s\n", __func__);
  12727. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_concat);
  12728. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12729. }
  12730. static void ggml_sycl_upscale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12731. GGML_SYCL_DEBUG("call %s\n", __func__);
  12732. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_upscale);
  12733. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12734. }
  12735. static void ggml_sycl_pad(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12736. GGML_SYCL_DEBUG("call %s\n", __func__);
  12737. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pad);
  12738. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12739. }
  12740. static void ggml_sycl_rms_norm(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12741. GGML_SYCL_DEBUG("call %s\n", __func__);
  12742. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rms_norm);
  12743. GGML_SYCL_DEBUG("call %s done\n", __func__);
  12744. }
  12745. bool ggml_sycl_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst) {
  12746. if (!g_sycl_loaded) return false;
  12747. const int64_t ne10 = src1->ne[0];
  12748. const int64_t ne0 = dst->ne[0];
  12749. const int64_t ne1 = dst->ne[1];
  12750. // TODO: find the optimal values for these
  12751. return (src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16 || ggml_is_quantized(src0->type)) &&
  12752. src1->type == GGML_TYPE_F32 &&
  12753. dst->type == GGML_TYPE_F32 &&
  12754. (ne0 >= 32 && ne1 >= 32 && ne10 >= 32);
  12755. }
  12756. static void ggml_sycl_mul_mat_vec_p021(const ggml_tensor *src0,
  12757. const ggml_tensor *src1,
  12758. ggml_tensor *dst) try {
  12759. GGML_ASSERT(ggml_is_permuted(src0) && ggml_is_permuted(src1));
  12760. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12761. GGML_ASSERT(src0->nb[0] <= src0->nb[1] && src0->nb[2] <= src0->nb[3]); // 0213 permutation
  12762. GGML_ASSERT(src1->nb[0] <= src1->nb[1] && src1->nb[2] <= src1->nb[3]); // 0213 permutation
  12763. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12764. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12765. const int64_t ne00 = src0->ne[0];
  12766. const int64_t ne01 = src0->ne[1];
  12767. const int64_t ne02 = src0->ne[2];
  12768. const int64_t ne12 = src1->ne[2];
  12769. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12770. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12771. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12772. void * src0_ddq = src0_extra->data_device[g_main_device];
  12773. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12774. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12775. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12776. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12777. ggml_mul_mat_p021_f16_f32_sycl(src0_ddq, src1_ddf, dst_ddf, ne00, ne01, ne02, ne12, main_stream);
  12778. }
  12779. catch (sycl::exception const &exc) {
  12780. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12781. << ", line:" << __LINE__ << std::endl;
  12782. std::exit(1);
  12783. }
  12784. static void ggml_sycl_mul_mat_vec_nc(const ggml_tensor *src0,
  12785. const ggml_tensor *src1,
  12786. ggml_tensor *dst) try {
  12787. GGML_ASSERT(!ggml_is_transposed(src0));
  12788. GGML_ASSERT(!ggml_is_transposed(src1));
  12789. GGML_ASSERT(!ggml_is_permuted(src0));
  12790. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12791. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12792. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  12793. const int64_t ne00 = src0->ne[0];
  12794. const int64_t ne01 = src0->ne[1];
  12795. const int64_t ne02 = src0->ne[2];
  12796. const int64_t nb01 = src0->nb[1];
  12797. const int64_t nb02 = src0->nb[2];
  12798. const int64_t ne12 = src1->ne[2];
  12799. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12800. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12801. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12802. void * src0_ddq = src0_extra->data_device[g_main_device];
  12803. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12804. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12805. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12806. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12807. const int64_t row_stride_x = nb01 / sizeof(sycl::half);
  12808. const int64_t channel_stride_x = nb02 / sizeof(sycl::half);
  12809. 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);
  12810. }
  12811. catch (sycl::exception const &exc) {
  12812. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12813. << ", line:" << __LINE__ << std::endl;
  12814. std::exit(1);
  12815. }
  12816. static void k_compute_batched_ptrs(const sycl::half *src0_as_f16,
  12817. const sycl::half *src1_as_f16, char *dst,
  12818. const void **ptrs_src, void **ptrs_dst,
  12819. int64_t ne12, int64_t ne13, int64_t ne23,
  12820. size_t nb02, size_t nb03, size_t nb12,
  12821. size_t nb13, size_t nbd2, size_t nbd3,
  12822. int64_t r2, int64_t r3,
  12823. const sycl::nd_item<3> &item_ct1) {
  12824. int64_t i13 = item_ct1.get_group(2) * item_ct1.get_local_range(2) +
  12825. item_ct1.get_local_id(2);
  12826. int64_t i12 = item_ct1.get_group(1) * item_ct1.get_local_range(1) +
  12827. item_ct1.get_local_id(1);
  12828. if (i13 >= ne13 || i12 >= ne12) {
  12829. return;
  12830. }
  12831. int64_t i03 = i13 / r3;
  12832. int64_t i02 = i12 / r2;
  12833. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_as_f16 + i02*nb02 + i03*nb03;
  12834. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_as_f16 + i12*nb12 + i13*nb13;
  12835. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst + i12*nbd2 + i13*nbd3;
  12836. }
  12837. static void ggml_sycl_mul_mat_batched_sycl(const ggml_tensor *src0,
  12838. const ggml_tensor *src1,
  12839. ggml_tensor *dst) try {
  12840. GGML_ASSERT(!ggml_is_transposed(src0));
  12841. GGML_ASSERT(!ggml_is_transposed(src1));
  12842. GGML_ASSERT(src0->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  12843. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  12844. GGML_TENSOR_BINARY_OP_LOCALS
  12845. const int64_t ne_dst = ggml_nelements(dst);
  12846. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  12847. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  12848. bool no_mixed_dtypes = main_stream->get_backend() == sycl::backend::ext_oneapi_cuda ||
  12849. main_stream->get_backend() == sycl::backend::ext_oneapi_hip;
  12850. SYCL_CHECK(
  12851. CHECK_TRY_ERROR(g_sycl_handles[g_main_device] = main_stream));
  12852. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  12853. void * src0_ddq = src0_extra->data_device[g_main_device];
  12854. sycl::half *src0_as_f16 = (sycl::half *)src0_ddq;
  12855. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  12856. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  12857. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  12858. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  12859. // convert src1 to fp16
  12860. sycl_pool_alloc<sycl::half> src1_f16_alloc;
  12861. if (src1->type != GGML_TYPE_F16) {
  12862. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  12863. const int64_t ne_src1 = ggml_nelements(src1);
  12864. src1_f16_alloc.alloc(ne_src1);
  12865. GGML_ASSERT(to_fp16_sycl != nullptr);
  12866. to_fp16_sycl(src1_ddf, src1_f16_alloc.get(), ne_src1, main_stream);
  12867. }
  12868. sycl::half *src1_f16 = src1->type == GGML_TYPE_F16 ? (sycl::half *)src1_ddf
  12869. : src1_f16_alloc.get();
  12870. sycl_pool_alloc<sycl::half> dst_f16;
  12871. char * dst_t;
  12872. dpct::library_data_t cu_compute_type = dpct::library_data_t::real_float;
  12873. dpct::library_data_t cu_data_type = dpct::library_data_t::real_float;
  12874. if (no_mixed_dtypes) {
  12875. cu_compute_type = dpct::library_data_t::real_half;
  12876. cu_data_type = dpct::library_data_t::real_half;
  12877. }
  12878. // dst strides
  12879. size_t nbd2 = dst->nb[2];
  12880. size_t nbd3 = dst->nb[3];
  12881. const float alpha_f32 = 1.0f;
  12882. const float beta_f32 = 0.0f;
  12883. const sycl::half alpha_f16 = 1.0f;
  12884. const sycl::half beta_f16 = 0.0f;
  12885. const void * alpha = &alpha_f32;
  12886. const void * beta = &beta_f32;
  12887. if (no_mixed_dtypes) {
  12888. alpha = &alpha_f16;
  12889. beta = &beta_f16;
  12890. }
  12891. // TODO: Renable (dst->op_params[0] =! GGML_PREC_DEFAULT) pathway
  12892. // when oneMKL open source supports half, half, float, float: datatypes
  12893. dst_t = (char *) dst_ddf;
  12894. if (no_mixed_dtypes) {
  12895. dst_t = (char *) dst_f16.alloc(ne_dst);
  12896. nbd2 /= sizeof(float) / sizeof(sycl::half);
  12897. nbd3 /= sizeof(float) / sizeof(sycl::half);
  12898. }
  12899. GGML_ASSERT(ne12 % ne02 == 0);
  12900. GGML_ASSERT(ne13 % ne03 == 0);
  12901. // broadcast factors
  12902. const int64_t r2 = ne12/ne02;
  12903. const int64_t r3 = ne13/ne03;
  12904. #if 0
  12905. // use syclGemmEx
  12906. {
  12907. for (int i13 = 0; i13 < ne13; ++i13) {
  12908. for (int i12 = 0; i12 < ne12; ++i12) {
  12909. int i03 = i13 / r3;
  12910. int i02 = i12 / r2;
  12911. SYCL_CHECK(
  12912. syclGemmEx(g_sycl_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  12913. ne01, ne11, ne10,
  12914. alpha, (const char *) src0_as_f16 + i02*src0->nb[2] + i03*src0->nb[3] , SYCL_R_16F, nb01/sizeof(half),
  12915. (const char *) src1_as_f16 + i12*src1->nb[2]/2 + i13*src1->nb[3]/2, SYCL_R_16F, nb11/sizeof(float),
  12916. beta, ( char *) dst_t + i12*nbd2 + i13*nbd3, cu_data_type, ne01,
  12917. cu_compute_type,
  12918. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  12919. }
  12920. }
  12921. }
  12922. #else
  12923. if (r2 == 1 && r3 == 1 && src0->nb[2]*src0->ne[2] == src0->nb[3] && src1->nb[2]*src1->ne[2] == src1->nb[3]) {
  12924. // there is no broadcast and src0, src1 are contiguous across dims 2, 3
  12925. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  12926. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  12927. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  12928. (const char *)src0_as_f16, dpct::library_data_t::real_half,
  12929. nb01 / nb00, nb02 / nb00,
  12930. (const char *)src1_f16, dpct::library_data_t::real_half,
  12931. nb11 / nb10, nb12 / nb10, beta,
  12932. (char *)dst_t, cu_data_type, ne01, nb2 / nb0,
  12933. ne12 * ne13, cu_compute_type)));
  12934. g_sycl_handles[g_main_device]->wait();
  12935. } else {
  12936. const int ne23 = ne12*ne13;
  12937. sycl_pool_alloc<const void *> ptrs_src(2*ne23);
  12938. sycl_pool_alloc< void *> ptrs_dst(1*ne23);
  12939. sycl::range<3> block_dims(1, ne12, ne13);
  12940. /*
  12941. DPCT1049:47: The work-group size passed to the SYCL kernel may exceed
  12942. the limit. To get the device limit, query
  12943. info::device::max_work_group_size. Adjust the work-group size if needed.
  12944. */
  12945. {
  12946. dpct::has_capability_or_fail(main_stream->get_device(),
  12947. {sycl::aspect::fp16});
  12948. main_stream->submit([&](sycl::handler &cgh) {
  12949. const void **ptrs_src_get = ptrs_src.get();
  12950. void **ptrs_dst_get = ptrs_dst.get();
  12951. size_t nb12_scaled = src1->type == GGML_TYPE_F16 ? nb12 : nb12 / 2;
  12952. size_t nb13_scaled = src1->type == GGML_TYPE_F16 ? nb13 : nb13 / 2;
  12953. cgh.parallel_for(sycl::nd_range<3>(block_dims, block_dims),
  12954. [=](sycl::nd_item<3> item_ct1) {
  12955. k_compute_batched_ptrs(
  12956. src0_as_f16, src1_f16,
  12957. dst_t, ptrs_src_get,
  12958. ptrs_dst_get, ne12, ne13, ne23,
  12959. nb02, nb03, nb12_scaled, nb13_scaled,
  12960. nbd2, nbd3, r2, r3, item_ct1);
  12961. });
  12962. }).wait();
  12963. }
  12964. SYCL_CHECK(CHECK_TRY_ERROR(dpct::gemm_batch(
  12965. *g_sycl_handles[g_main_device], oneapi::mkl::transpose::trans,
  12966. oneapi::mkl::transpose::nontrans, ne01, ne11, ne10, alpha,
  12967. (const void **)(ptrs_src.get() + 0 * ne23),
  12968. dpct::library_data_t::real_half, nb01 / nb00,
  12969. (const void **)(ptrs_src.get() + 1 * ne23),
  12970. dpct::library_data_t::real_half, nb11 / nb10, beta,
  12971. (void **)(ptrs_dst.get() + 0 * ne23), cu_data_type, ne01, ne23,
  12972. cu_compute_type)));
  12973. g_sycl_handles[g_main_device]->wait();
  12974. }
  12975. #endif
  12976. if (no_mixed_dtypes) {
  12977. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  12978. to_fp32_sycl(dst_f16.get(), dst_ddf, ne_dst, main_stream);
  12979. }
  12980. }
  12981. catch (sycl::exception const &exc) {
  12982. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  12983. << ", line:" << __LINE__ << std::endl;
  12984. std::exit(1);
  12985. }
  12986. static void ggml_sycl_mul_mat(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  12987. const bool all_on_device =
  12988. (src0->backend == GGML_BACKEND_TYPE_GPU || src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT) &&
  12989. (src1->backend == GGML_BACKEND_TYPE_GPU) &&
  12990. ( dst->backend == GGML_BACKEND_TYPE_GPU);
  12991. const bool split = src0->backend == GGML_BACKEND_TYPE_GPU_SPLIT;
  12992. int64_t min_compute_capability = INT_MAX;
  12993. for (int i = 0; i < g_device_count; ++i) {
  12994. 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)) {
  12995. min_compute_capability = g_device_caps[i].cc;
  12996. }
  12997. }
  12998. #ifdef SYCL_USE_XMX
  12999. const bool use_xmx = true;
  13000. #else
  13001. const bool use_xmx = false;
  13002. #endif
  13003. // debug helpers
  13004. //printf("src0: %8d %8d %8d %8d\n", src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3]);
  13005. //printf(" %8d %8d %8d %8d\n", src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3]);
  13006. //printf("src1: %8d %8d %8d %8d\n", src1->ne[0], src1->ne[1], src1->ne[2], src1->ne[3]);
  13007. //printf(" %8d %8d %8d %8d\n", src1->nb[0], src1->nb[1], src1->nb[2], src1->nb[3]);
  13008. //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);
  13009. //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);
  13010. if (!split && all_on_device && !use_xmx && src0->type == GGML_TYPE_F16 && ggml_is_permuted(src0) && ggml_is_permuted(src1) && src1->ne[1] == 1) {
  13011. // KQ single-batch
  13012. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_p021\n");
  13013. ggml_sycl_mul_mat_vec_p021(src0, src1, dst);
  13014. } 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) {
  13015. // KQV single-batch
  13016. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_vec_nc\n");
  13017. ggml_sycl_mul_mat_vec_nc(src0, src1, dst);
  13018. } else if (!split && all_on_device && use_xmx && src0->type == GGML_TYPE_F16 && !ggml_is_transposed(src0) && !ggml_is_transposed(src1)) {
  13019. // KQ + KQV multi-batch
  13020. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat_batched_sycl\n");
  13021. ggml_sycl_mul_mat_batched_sycl(src0, src1, dst);
  13022. } else if (src0->type == GGML_TYPE_F32) {
  13023. // GGML_SYCL_DEBUG("ggml_sycl_op_mul_mat\n");
  13024. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  13025. } else if (ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F16) {
  13026. // GGML_SYCL_DEBUG("ggml_is_quantized or GGML_TYPE_F16\n");
  13027. if (src1->ne[1] == 1 && src0->ne[0] % GGML_SYCL_DMMV_X == 0) {
  13028. #ifdef GGML_SYCL_FORCE_DMMV
  13029. const bool use_mul_mat_vec_q = false;
  13030. #else
  13031. const bool use_mul_mat_vec_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type) && ggml_nrows(src1) == 1;
  13032. #endif // GGML_SYCL_FORCE_DMMV
  13033. if (use_mul_mat_vec_q) {
  13034. // NOTE: this kernel does not support ggml_nrows(src1) > 1
  13035. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_vec_q path\n");
  13036. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_vec_q, true);
  13037. } else {
  13038. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_dequantize_mul_mat_vec path\n");
  13039. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_dequantize_mul_mat_vec, false);
  13040. }
  13041. } else {
  13042. bool use_mul_mat_q = min_compute_capability >= VER_4VEC && ggml_is_quantized(src0->type);
  13043. if (use_xmx && min_compute_capability >= VER_GEN9 && src1->ne[1] > XMX_MAX_BATCH_SIZE) {
  13044. use_mul_mat_q = false;
  13045. }
  13046. if (use_mul_mat_q) {
  13047. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_q path\n");
  13048. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_q, true);
  13049. } else {
  13050. // GGML_SYCL_DEBUG("ggml_sycl_mul_mat ggml_sycl_op_mul_mat_sycl path\n");
  13051. ggml_sycl_op_mul_mat(src0, src1, dst, ggml_sycl_op_mul_mat_sycl, false);
  13052. }
  13053. }
  13054. } else {
  13055. GGML_ASSERT(false);
  13056. }
  13057. }
  13058. #if 0
  13059. template<typename ... Srcs>
  13060. static __global__ void k_compute_batched_ptrs_id(
  13061. const void ** ptrs_src, void ** ptrs_dst,
  13062. int ne12, int ne13,
  13063. int ne23,
  13064. int nb02, int nb03,
  13065. int nb12, int nb13,
  13066. int nb2, int nb3,
  13067. int r2, int r3,
  13068. ggml_type src0_type, half * src0_as_f16, int64_t src0_ne,
  13069. const half * src1_f16, half * dst_f16,
  13070. const int32_t * ids, const int id,
  13071. Srcs... src0s) {
  13072. int i = ids[id];
  13073. half * src0_f16;
  13074. const void * srcs_ar[] = { (const half *) src0s... };
  13075. if (src0_type == GGML_TYPE_F16) {
  13076. src0_f16 = (half *) srcs_ar[i];
  13077. } else {
  13078. src0_f16 = src0_as_f16;
  13079. if (item_ct1.get_local_id(2) == 0 && threadIdx.y == 0) {
  13080. const to_fp16_sycl_t to_fp16 = ggml_get_to_fp16_sycl(src0_type);
  13081. to_fp16(srcs_ar[i], src0_f16, src0_ne, syclStreamFireAndForget);
  13082. }
  13083. }
  13084. int i13 = blockIdx.x * blockDim.x + item_ct1.get_local_id(2);
  13085. int i12 = blockIdx.y * blockDim.y + threadIdx.y;
  13086. if (i13 >= ne13 || i12 >= ne12) {
  13087. return;
  13088. }
  13089. int i03 = i13 / r3;
  13090. int i02 = i12 / r2;
  13091. ptrs_src[0*ne23 + i12 + i13*ne12] = (const char *) src0_f16 + i02*nb02 + i03*nb03;
  13092. ptrs_src[1*ne23 + i12 + i13*ne12] = (const char *) src1_f16 + i12*nb12/2 + i13*nb13/2;
  13093. ptrs_dst[0*ne23 + i12 + i13*ne12] = ( char *) dst_f16 + i12* nb2/2 + i13* nb3/2;
  13094. }
  13095. static void ggml_sycl_mul_mat_id_sycl(ggml_tensor * dst) {
  13096. const struct ggml_tensor * ids = dst->src[0];
  13097. const struct ggml_tensor * src1 = dst->src[1];
  13098. const struct ggml_tensor * src00 = dst->src[2];
  13099. const int id = dst->op_params[0];
  13100. GGML_ASSERT(!ggml_is_transposed(src00));
  13101. GGML_ASSERT(!ggml_is_transposed(src1));
  13102. GGML_ASSERT(src00->backend != GGML_BACKEND_TYPE_GPU_SPLIT);
  13103. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  13104. GGML_TENSOR_LOCALS(int64_t, ne0, src00, ne);
  13105. //const int64_t nb01 = src00->nb[1];
  13106. GGML_TENSOR_LOCALS(int64_t, nb0, src00, nb);
  13107. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
  13108. GGML_TENSOR_LOCALS(int64_t, nb1, src1, nb);
  13109. //const int64_t nb11 = src1->nb[1];
  13110. const int64_t ne1 = ggml_nelements(src1);
  13111. const int64_t ne = ggml_nelements(dst);
  13112. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13113. syclStream_t main_stream = g_syclStreams[g_main_device][0];
  13114. SYCL_CHECK(syclSetStream(g_sycl_handles[g_main_device], main_stream));
  13115. //ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13116. //void * src0_ddq = src0_extra->data_device[g_main_device];
  13117. //half * src0_as_f16 = (half *) src0_ddq;
  13118. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13119. float * src1_ddf = (float *) src1_extra->data_device[g_main_device];
  13120. ggml_tensor_extra_gpu * dst_extra = (ggml_tensor_extra_gpu *) dst->extra;
  13121. float * dst_ddf = (float *) dst_extra->data_device[g_main_device];
  13122. // convert src1 to fp16
  13123. const to_fp16_sycl_t to_fp16_sycl = ggml_get_to_fp16_sycl(src1->type);
  13124. GGML_ASSERT(to_fp16_sycl != nullptr);
  13125. size_t src1_as = 0;
  13126. half * src1_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne1 * sizeof(half), &src1_as);
  13127. to_fp16_sycl(src1_ddf, src1_as_f16, ne1, main_stream);
  13128. size_t dst_as = 0;
  13129. half * dst_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, ne * sizeof(half), &dst_as);
  13130. GGML_ASSERT(ne12 % ne02 == 0);
  13131. GGML_ASSERT(ne13 % ne03 == 0);
  13132. // broadcast factors
  13133. const int64_t r2 = ne12/ne02;
  13134. const int64_t r3 = ne13/ne03;
  13135. const half alpha_f16 = 1.0f;
  13136. const half beta_f16 = 0.0f;
  13137. // use syclGemmBatchedEx
  13138. const int ne23 = ne12*ne13;
  13139. const void ** ptrs_src = nullptr;
  13140. void ** ptrs_dst = nullptr;
  13141. size_t ptrs_src_s = 0;
  13142. size_t ptrs_dst_s = 0;
  13143. ptrs_src = (const void **) ggml_sycl_pool_malloc(g_main_device, 2*ne23*sizeof(void *), &ptrs_src_s);
  13144. ptrs_dst = ( void **) ggml_sycl_pool_malloc(g_main_device, 1*ne23*sizeof(void *), &ptrs_dst_s);
  13145. int64_t src0_ne = ggml_nelements(src00);
  13146. half * src0_as_f16 = nullptr;
  13147. size_t src0_as = 0;
  13148. if (src00->type != GGML_TYPE_F16) {
  13149. src0_as_f16 = (half *) ggml_sycl_pool_malloc(g_main_device, src0_ne * sizeof(half), &src0_as);
  13150. }
  13151. static_assert(GGML_MAX_SRC == 6, "GGML_MAX_SRC == 6");
  13152. dim3 block_dims(ne13, ne12);
  13153. k_compute_batched_ptrs_id<<<1, block_dims, 0, main_stream>>>(
  13154. ptrs_src, ptrs_dst,
  13155. ne12, ne13,
  13156. ne23,
  13157. ne00*ne01*sizeof(half), ne00*ne01*ne02*sizeof(half),
  13158. nb12, nb13,
  13159. dst->nb[2], dst->nb[3],
  13160. r2, r3,
  13161. src00->type, src0_as_f16, src0_ne,
  13162. src1_as_f16, dst_f16,
  13163. (const int *)((ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device], id,
  13164. dst->src[2] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[2]->extra)->data_device[g_main_device] : nullptr,
  13165. dst->src[3] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[3]->extra)->data_device[g_main_device] : nullptr,
  13166. dst->src[4] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[4]->extra)->data_device[g_main_device] : nullptr,
  13167. dst->src[5] ? (const half *)((ggml_tensor_extra_gpu *)dst->src[5]->extra)->data_device[g_main_device] : nullptr
  13168. );
  13169. SYCL_CHECK(syclGetLastError());
  13170. SYCL_CHECK(
  13171. syclGemmBatchedEx(g_sycl_handles[g_main_device], CUBLAS_OP_T, CUBLAS_OP_N,
  13172. ne01, ne11, ne10,
  13173. &alpha_f16, (const void **) (ptrs_src + 0*ne23), SYCL_R_16F, ne00,
  13174. (const void **) (ptrs_src + 1*ne23), SYCL_R_16F, ne10,
  13175. &beta_f16, ( void **) (ptrs_dst + 0*ne23), SYCL_R_16F, ne01,
  13176. ne23,
  13177. CUBLAS_COMPUTE_16F,
  13178. CUBLAS_GEMM_DEFAULT_TENSOR_OP));
  13179. if (src0_as != 0) {
  13180. ggml_sycl_pool_free(g_main_device, src0_as_f16, src0_as);
  13181. }
  13182. if (ptrs_src_s != 0) {
  13183. ggml_sycl_pool_free(g_main_device, ptrs_src, ptrs_src_s);
  13184. }
  13185. if (ptrs_dst_s != 0) {
  13186. ggml_sycl_pool_free(g_main_device, ptrs_dst, ptrs_dst_s);
  13187. }
  13188. const to_fp32_sycl_t to_fp32_sycl = ggml_get_to_fp32_sycl(GGML_TYPE_F16);
  13189. to_fp32_sycl(dst_f16, dst_ddf, ne, main_stream);
  13190. ggml_sycl_pool_free(g_main_device, src1_as_f16, src1_as);
  13191. ggml_sycl_pool_free(g_main_device, dst_f16, dst_as);
  13192. }
  13193. #endif
  13194. static void ggml_sycl_mul_mat_id(const ggml_tensor *src0,
  13195. const ggml_tensor *src1,
  13196. ggml_tensor *dst) try {
  13197. #if 0
  13198. ggml_sycl_mul_mat_id_sycl(dst);
  13199. // TODO: mmq/mmv support
  13200. #endif
  13201. const int64_t nb11 = src1->nb[1];
  13202. const int64_t nb1 = dst->nb[1];
  13203. const struct ggml_tensor * ids = src0;
  13204. const int32_t id = ((int32_t *) dst->op_params)[0];
  13205. const int32_t n_as = ((int32_t *) dst->op_params)[1];
  13206. std::vector<char> ids_host(ggml_nbytes(ids));
  13207. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13208. if (ids->backend == GGML_BACKEND_TYPE_GPU) {
  13209. const char * ids_dev = (const char *)((const ggml_tensor_extra_gpu *)ids->extra)->data_device[g_main_device];
  13210. SYCL_CHECK(CHECK_TRY_ERROR(
  13211. stream->memcpy(ids_host.data(), ids_dev, ggml_nbytes(ids)).wait()));
  13212. // SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  13213. } else {
  13214. memcpy(ids_host.data(), ids->data, ggml_nbytes(ids));
  13215. }
  13216. const ggml_tensor_extra_gpu * src1_extra = (const ggml_tensor_extra_gpu *) src1->extra;
  13217. const ggml_tensor_extra_gpu * dst_extra = (const ggml_tensor_extra_gpu *) dst->extra;
  13218. ggml_tensor_extra_gpu src1_row_extra;
  13219. ggml_tensor_extra_gpu dst_row_extra;
  13220. ggml_tensor src1_row = *src1;
  13221. ggml_tensor dst_row = *dst;
  13222. src1_row.backend = GGML_BACKEND_TYPE_GPU;
  13223. dst_row.backend = GGML_BACKEND_TYPE_GPU;
  13224. src1_row.extra = &src1_row_extra;
  13225. dst_row.extra = &dst_row_extra;
  13226. char * src1_original = src1->backend == GGML_BACKEND_TYPE_CPU ?
  13227. (char *) src1->data : (char *) src1_extra->data_device[g_main_device];
  13228. char * dst_original = dst->backend == GGML_BACKEND_TYPE_CPU ?
  13229. (char *) dst->data : (char *) dst_extra->data_device[g_main_device];
  13230. if (src1->ne[1] == 1) {
  13231. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  13232. GGML_ASSERT(dst->backend == GGML_BACKEND_TYPE_GPU);
  13233. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13234. //int32_t row_id;
  13235. //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]));
  13236. //SYCL_CHECK(syclStreamSynchronize(g_syclStreams[g_main_device][0]));
  13237. const int32_t row_id = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13238. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13239. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  13240. src1_row_extra.data_device[g_main_device] = src1_original + i01*src1->nb[1];
  13241. src1_row.data = (char *) src1->data + i01*src1->nb[1]; // TODO why is this set?
  13242. dst_row_extra.data_device[g_main_device] = dst_original + i01*dst->nb[1];
  13243. dst_row.data = (char *) dst->data + i01*dst->nb[1]; // TODO why is this set?
  13244. ggml_sycl_mul_mat(src0_row, &src1_row, &dst_row);
  13245. }
  13246. } else {
  13247. sycl_pool_alloc<char> src1_contiguous(sizeof(float)*ggml_nelements(src1));
  13248. sycl_pool_alloc<char> dst_contiguous(sizeof(float)*ggml_nelements(dst));
  13249. src1_row_extra.data_device[g_main_device] = src1_contiguous.get();
  13250. dst_row_extra.data_device[g_main_device] = dst_contiguous.get();
  13251. for (int32_t row_id = 0; row_id < n_as; ++row_id) {
  13252. const struct ggml_tensor * src0_row = dst->src[row_id + 2];
  13253. int64_t num_src1_rows = 0;
  13254. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13255. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13256. if (row_id_i != row_id) {
  13257. continue;
  13258. }
  13259. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13260. SYCL_CHECK(CHECK_TRY_ERROR(
  13261. stream->memcpy(src1_contiguous.get() + num_src1_rows * nb11,
  13262. src1_original + i01 * nb11, nb11).wait()));
  13263. num_src1_rows++;
  13264. }
  13265. if (num_src1_rows == 0) {
  13266. continue;
  13267. }
  13268. src1_row.ne[1] = num_src1_rows;
  13269. dst_row.ne[1] = num_src1_rows;
  13270. src1_row.nb[1] = nb11;
  13271. src1_row.nb[2] = num_src1_rows*nb11;
  13272. src1_row.nb[3] = num_src1_rows*nb11;
  13273. dst_row.nb[1] = nb1;
  13274. dst_row.nb[2] = num_src1_rows*nb1;
  13275. dst_row.nb[3] = num_src1_rows*nb1;
  13276. ggml_sycl_mul_mat(src0_row, &src1_row, &dst_row);
  13277. num_src1_rows = 0;
  13278. for (int64_t i01 = 0; i01 < ids->ne[1]; i01++) {
  13279. const int32_t row_id_i = *(const int32_t *) (ids_host.data() + i01*ids->nb[1] + id*ids->nb[0]);
  13280. if (row_id_i != row_id) {
  13281. continue;
  13282. }
  13283. GGML_ASSERT(row_id >= 0 && row_id < n_as);
  13284. SYCL_CHECK(CHECK_TRY_ERROR(stream->memcpy(
  13285. dst_original + i01 * nb1,
  13286. dst_contiguous.get() + num_src1_rows * nb1, nb1).wait()));
  13287. num_src1_rows++;
  13288. }
  13289. }
  13290. }
  13291. if (dst->backend == GGML_BACKEND_TYPE_CPU) {
  13292. SYCL_CHECK(CHECK_TRY_ERROR(stream->wait()));
  13293. }
  13294. }
  13295. catch (sycl::exception const &exc) {
  13296. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13297. << ", line:" << __LINE__ << std::endl;
  13298. std::exit(1);
  13299. }
  13300. static void ggml_sycl_scale(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13301. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_scale);
  13302. }
  13303. static void ggml_sycl_clamp(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13304. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_clamp);
  13305. }
  13306. static void ggml_sycl_cpy(const ggml_tensor *src0, const ggml_tensor *src1,
  13307. ggml_tensor *dst) try {
  13308. const int64_t ne = ggml_nelements(src0);
  13309. GGML_ASSERT(ne == ggml_nelements(src1));
  13310. GGML_ASSERT(src0->backend == GGML_BACKEND_TYPE_GPU);
  13311. GGML_ASSERT(src1->backend == GGML_BACKEND_TYPE_GPU);
  13312. GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
  13313. GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
  13314. GGML_TENSOR_BINARY_OP_LOCALS;
  13315. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13316. dpct::queue_ptr main_stream = g_syclStreams[g_main_device][0];
  13317. const ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu *) src0->extra;
  13318. const ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu *) src1->extra;
  13319. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  13320. char * src1_ddc = (char *) src1_extra->data_device[g_main_device];
  13321. if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
  13322. 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);
  13323. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
  13324. 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);
  13325. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
  13326. 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);
  13327. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
  13328. 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);
  13329. } else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
  13330. 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);
  13331. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
  13332. 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);
  13333. } else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
  13334. 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);
  13335. } else if (src0->type == GGML_TYPE_I16 && src1->type == GGML_TYPE_I16) {
  13336. 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);
  13337. } else if (src0->type == GGML_TYPE_I32 && src1->type == GGML_TYPE_I32) {
  13338. 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);
  13339. } else {
  13340. fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
  13341. ggml_type_name(src0->type), ggml_type_name(src1->type));
  13342. GGML_ASSERT(false);
  13343. }
  13344. (void) dst;
  13345. }
  13346. catch (sycl::exception const &exc) {
  13347. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13348. << ", line:" << __LINE__ << std::endl;
  13349. std::exit(1);
  13350. }
  13351. static void ggml_sycl_dup(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13352. // TODO: why do we pass dst as src1 here?
  13353. ggml_sycl_cpy(src0, dst, nullptr);
  13354. (void) src1;
  13355. }
  13356. static void ggml_sycl_diag_mask_inf(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13357. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_diag_mask_inf);
  13358. }
  13359. static void ggml_sycl_soft_max(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13360. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_soft_max);
  13361. }
  13362. static void ggml_sycl_rope(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13363. GGML_ASSERT(ggml_is_contiguous(src0)); // TODO: this restriction is temporary until non-cont support is implemented
  13364. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_rope);
  13365. }
  13366. static void ggml_sycl_alibi(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13367. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_alibi);
  13368. }
  13369. static void ggml_sycl_pool2d(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13370. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_pool2d);
  13371. }
  13372. static void ggml_sycl_im2col(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13373. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_im2col);
  13374. }
  13375. static void ggml_sycl_sum_rows(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13376. GGML_ASSERT(ggml_is_contiguous(src0));
  13377. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_sum_rows);
  13378. }
  13379. static void ggml_sycl_argsort(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13380. GGML_ASSERT(ggml_is_contiguous(src0));
  13381. ggml_sycl_op_flatten(src0, src1, dst, ggml_sycl_op_argsort);
  13382. }
  13383. static void ggml_sycl_nop(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
  13384. (void) src0;
  13385. (void) src1;
  13386. (void) dst;
  13387. }
  13388. static size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
  13389. static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
  13390. return nrows_split*ggml_row_size(tensor->type, tensor->ne[0]);
  13391. }
  13392. void ggml_sycl_free_data(struct ggml_tensor *tensor) try {
  13393. if (!tensor || !tensor->extra || (tensor->backend != GGML_BACKEND_TYPE_GPU && tensor->backend != GGML_BACKEND_TYPE_GPU_SPLIT) ) {
  13394. return;
  13395. }
  13396. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  13397. for (int i = 0; i < g_device_count; ++i) {
  13398. const dpct::queue_ptr stream = g_syclStreams[i][0];
  13399. if (extra->data_device[i] != nullptr) {
  13400. SYCL_CHECK(ggml_sycl_set_device(i));
  13401. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(extra->data_device[i], *stream)));
  13402. }
  13403. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  13404. if (extra->events[i][is] != nullptr) {
  13405. SYCL_CHECK(ggml_sycl_set_device(i));
  13406. SYCL_CHECK(CHECK_TRY_ERROR(
  13407. dpct::destroy_event(extra->events[i][is])));
  13408. }
  13409. }
  13410. }
  13411. delete extra;
  13412. }
  13413. catch (sycl::exception const &exc) {
  13414. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13415. << ", line:" << __LINE__ << std::endl;
  13416. std::exit(1);
  13417. }
  13418. static ggml_tensor_extra_gpu * g_temp_tensor_extras = nullptr;
  13419. static size_t g_temp_tensor_extra_index = 0;
  13420. static ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  13421. if (g_temp_tensor_extras == nullptr) {
  13422. g_temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  13423. }
  13424. size_t alloc_index = g_temp_tensor_extra_index;
  13425. g_temp_tensor_extra_index = (g_temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  13426. ggml_tensor_extra_gpu * extra = &g_temp_tensor_extras[alloc_index];
  13427. memset(extra, 0, sizeof(*extra));
  13428. return extra;
  13429. }
  13430. static void ggml_sycl_assign_buffers_impl(struct ggml_tensor *tensor,
  13431. bool scratch, bool force_inplace,
  13432. bool no_alloc) try {
  13433. if (scratch && g_scratch_size == 0) {
  13434. return;
  13435. }
  13436. tensor->backend = GGML_BACKEND_TYPE_GPU;
  13437. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_CPU) {
  13438. const ggml_op src0_op = tensor->src[0]->op;
  13439. if (src0_op == GGML_OP_RESHAPE || src0_op == GGML_OP_TRANSPOSE || src0_op == GGML_OP_VIEW || src0_op == GGML_OP_PERMUTE) {
  13440. ggml_sycl_assign_buffers_impl(tensor->src[0], scratch, force_inplace, no_alloc);
  13441. }
  13442. }
  13443. if (tensor->op == GGML_OP_CPY && tensor->src[1]->backend == GGML_BACKEND_TYPE_CPU) {
  13444. ggml_sycl_assign_buffers_impl(tensor->src[1], scratch, force_inplace, no_alloc);
  13445. }
  13446. if (scratch && no_alloc) {
  13447. return;
  13448. }
  13449. ggml_tensor_extra_gpu * extra;
  13450. const bool inplace = (tensor->src[0] != nullptr && tensor->src[0]->data == tensor->data) ||
  13451. tensor->op == GGML_OP_VIEW ||
  13452. force_inplace;
  13453. const size_t size = ggml_nbytes(tensor);
  13454. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13455. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13456. if (inplace && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT)) {
  13457. ggml_tensor_extra_gpu * src0_extra = (ggml_tensor_extra_gpu * ) tensor->src[0]->extra;
  13458. char * src0_ddc = (char *) src0_extra->data_device[g_main_device];
  13459. size_t offset = 0;
  13460. if (tensor->op == GGML_OP_VIEW) {
  13461. memcpy(&offset, tensor->op_params, sizeof(size_t));
  13462. }
  13463. extra = ggml_sycl_alloc_temp_tensor_extra();
  13464. extra->data_device[g_main_device] = src0_ddc + offset;
  13465. } else if (tensor->op == GGML_OP_CPY) {
  13466. ggml_tensor_extra_gpu * src1_extra = (ggml_tensor_extra_gpu * ) tensor->src[1]->extra;
  13467. void * src1_ddv = src1_extra->data_device[g_main_device];
  13468. extra = ggml_sycl_alloc_temp_tensor_extra();
  13469. extra->data_device[g_main_device] = src1_ddv;
  13470. } else if (scratch) {
  13471. GGML_ASSERT(size <= g_scratch_size);
  13472. if (g_scratch_offset + size > g_scratch_size) {
  13473. g_scratch_offset = 0;
  13474. }
  13475. char * data = (char *) g_scratch_buffer;
  13476. if (data == nullptr) {
  13477. SYCL_CHECK(CHECK_TRY_ERROR(
  13478. data = (char *)sycl::malloc_device(
  13479. g_scratch_size, *stream)));
  13480. g_scratch_buffer = data;
  13481. }
  13482. extra = ggml_sycl_alloc_temp_tensor_extra();
  13483. extra->data_device[g_main_device] = data + g_scratch_offset;
  13484. g_scratch_offset += size;
  13485. GGML_ASSERT(g_scratch_offset <= g_scratch_size);
  13486. } else { // allocate new buffers outside of scratch
  13487. void * data;
  13488. SYCL_CHECK(CHECK_TRY_ERROR(data = (void *)sycl::malloc_device(
  13489. size, *stream)));
  13490. SYCL_CHECK(CHECK_TRY_ERROR(
  13491. (*stream).memset(data, 0, size).wait()));
  13492. extra = new ggml_tensor_extra_gpu;
  13493. memset(extra, 0, sizeof(*extra));
  13494. extra->data_device[g_main_device] = data;
  13495. }
  13496. tensor->extra = extra;
  13497. }
  13498. catch (sycl::exception const &exc) {
  13499. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13500. << ", line:" << __LINE__ << std::endl;
  13501. std::exit(1);
  13502. }
  13503. void ggml_sycl_copy_to_device(struct ggml_tensor *tensor) try {
  13504. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  13505. GGML_ASSERT(ggml_is_contiguous(tensor));
  13506. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *) tensor->extra;
  13507. SYCL_CHECK(ggml_sycl_set_device(g_main_device));
  13508. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13509. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  13510. .memcpy(extra->data_device[g_main_device],
  13511. tensor->data, ggml_nbytes(tensor))
  13512. .wait()));
  13513. }
  13514. catch (sycl::exception const &exc) {
  13515. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13516. << ", line:" << __LINE__ << std::endl;
  13517. std::exit(1);
  13518. }
  13519. void ggml_sycl_assign_buffers(struct ggml_tensor * tensor) {
  13520. ggml_sycl_assign_buffers_impl(tensor, true, false, false);
  13521. }
  13522. void ggml_sycl_assign_buffers_no_alloc(struct ggml_tensor * tensor) {
  13523. ggml_sycl_assign_buffers_impl(tensor, true, false, true);
  13524. }
  13525. void ggml_sycl_assign_buffers_no_scratch(struct ggml_tensor * tensor) {
  13526. ggml_sycl_assign_buffers_impl(tensor, false, false, false);
  13527. }
  13528. void ggml_sycl_assign_buffers_force_inplace(struct ggml_tensor * tensor) {
  13529. ggml_sycl_assign_buffers_impl(tensor, false, true, false);
  13530. }
  13531. void ggml_sycl_set_main_device(const int main_device) try {
  13532. if (g_main_device == main_device) return;
  13533. check_allow_gpu_index(main_device);
  13534. g_main_device = main_device;
  13535. g_main_device_id = g_sycl_gpu_mgr->gpus[main_device];
  13536. if (g_ggml_sycl_debug) {
  13537. dpct::device_info prop;
  13538. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  13539. prop, dpct::dev_mgr::instance().get_device(g_main_device_id))));
  13540. fprintf(stderr, "Using device %d (%s) as main device\n",
  13541. g_main_device_id, prop.get_name());
  13542. }
  13543. }
  13544. catch (sycl::exception const &exc) {
  13545. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13546. << ", line:" << __LINE__ << std::endl;
  13547. std::exit(1);
  13548. }
  13549. void ggml_sycl_set_scratch_size(const size_t scratch_size) {
  13550. // this is a hack to not completely break llama.cpp when using multiple models or contexts simultaneously
  13551. // it still won't always work as expected, but it's better than nothing
  13552. if (scratch_size > g_scratch_size) {
  13553. ggml_sycl_free_scratch();
  13554. }
  13555. g_scratch_size = std::max(g_scratch_size, scratch_size);
  13556. }
  13557. void ggml_sycl_free_scratch() try {
  13558. if (g_scratch_buffer == nullptr) {
  13559. return;
  13560. }
  13561. ggml_sycl_set_device(g_main_device);
  13562. const dpct::queue_ptr stream = g_syclStreams[g_main_device][0];
  13563. SYCL_CHECK(CHECK_TRY_ERROR(
  13564. sycl::free(g_scratch_buffer, *stream)));
  13565. g_scratch_buffer = nullptr;
  13566. }
  13567. catch (sycl::exception const &exc) {
  13568. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13569. << ", line:" << __LINE__ << std::endl;
  13570. std::exit(1);
  13571. }
  13572. bool ggml_sycl_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  13573. if (!g_sycl_loaded) return false;
  13574. ggml_sycl_func_t func;
  13575. const bool any_on_device = tensor->backend == GGML_BACKEND_TYPE_GPU
  13576. || (tensor->src[0] != nullptr && (tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU || tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT))
  13577. || (tensor->src[1] != nullptr && tensor->src[1]->backend == GGML_BACKEND_TYPE_GPU);
  13578. if (!any_on_device && tensor->op != GGML_OP_MUL_MAT && tensor->op != GGML_OP_MUL_MAT_ID) {
  13579. return false;
  13580. }
  13581. if (tensor->op == GGML_OP_MUL_MAT) {
  13582. if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
  13583. #ifndef NDEBUG
  13584. 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]);
  13585. #endif
  13586. return false;
  13587. }
  13588. }
  13589. switch (tensor->op) {
  13590. case GGML_OP_REPEAT:
  13591. func = ggml_sycl_repeat;
  13592. break;
  13593. case GGML_OP_GET_ROWS:
  13594. func = ggml_sycl_get_rows;
  13595. break;
  13596. case GGML_OP_DUP:
  13597. func = ggml_sycl_dup;
  13598. break;
  13599. case GGML_OP_ADD:
  13600. func = ggml_sycl_add;
  13601. break;
  13602. case GGML_OP_ACC:
  13603. func = ggml_sycl_acc;
  13604. break;
  13605. case GGML_OP_MUL:
  13606. func = ggml_sycl_mul;
  13607. break;
  13608. case GGML_OP_DIV:
  13609. func = ggml_sycl_div;
  13610. break;
  13611. case GGML_OP_UNARY:
  13612. switch (ggml_get_unary_op(tensor)) {
  13613. case GGML_UNARY_OP_GELU:
  13614. func = ggml_sycl_gelu;
  13615. break;
  13616. case GGML_UNARY_OP_SILU:
  13617. func = ggml_sycl_silu;
  13618. break;
  13619. case GGML_UNARY_OP_GELU_QUICK:
  13620. func = ggml_sycl_gelu_quick;
  13621. break;
  13622. case GGML_UNARY_OP_TANH:
  13623. func = ggml_sycl_tanh;
  13624. break;
  13625. case GGML_UNARY_OP_RELU:
  13626. func = ggml_sycl_relu;
  13627. break;
  13628. case GGML_UNARY_OP_HARDSIGMOID:
  13629. func = ggml_sycl_hardsigmoid;
  13630. break;
  13631. case GGML_UNARY_OP_HARDSWISH:
  13632. func = ggml_sycl_hardswish;
  13633. break;
  13634. default:
  13635. return false;
  13636. }
  13637. break;
  13638. case GGML_OP_NORM:
  13639. func = ggml_sycl_norm;
  13640. break;
  13641. case GGML_OP_GROUP_NORM:
  13642. func = ggml_sycl_group_norm;
  13643. break;
  13644. case GGML_OP_CONCAT:
  13645. func = ggml_sycl_concat;
  13646. break;
  13647. case GGML_OP_UPSCALE:
  13648. func = ggml_sycl_upscale;
  13649. break;
  13650. case GGML_OP_PAD:
  13651. func = ggml_sycl_pad;
  13652. break;
  13653. case GGML_OP_LEAKY_RELU:
  13654. func = ggml_sycl_leaky_relu;
  13655. break;
  13656. case GGML_OP_RMS_NORM:
  13657. func = ggml_sycl_rms_norm;
  13658. break;
  13659. case GGML_OP_MUL_MAT:
  13660. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[0], tensor->src[1], tensor)) {
  13661. return false;
  13662. }
  13663. func = ggml_sycl_mul_mat;
  13664. break;
  13665. case GGML_OP_MUL_MAT_ID:
  13666. if (!any_on_device && !ggml_sycl_can_mul_mat(tensor->src[2], tensor->src[1], tensor)) {
  13667. return false;
  13668. }
  13669. func = ggml_sycl_mul_mat_id;
  13670. break;
  13671. case GGML_OP_SCALE:
  13672. func = ggml_sycl_scale;
  13673. break;
  13674. case GGML_OP_SQR:
  13675. func = ggml_sycl_sqr;
  13676. break;
  13677. case GGML_OP_CLAMP:
  13678. func = ggml_sycl_clamp;
  13679. break;
  13680. case GGML_OP_CPY:
  13681. func = ggml_sycl_cpy;
  13682. break;
  13683. case GGML_OP_CONT:
  13684. func = ggml_sycl_dup;
  13685. break;
  13686. case GGML_OP_NONE:
  13687. case GGML_OP_RESHAPE:
  13688. case GGML_OP_VIEW:
  13689. case GGML_OP_PERMUTE:
  13690. case GGML_OP_TRANSPOSE:
  13691. func = ggml_sycl_nop;
  13692. break;
  13693. case GGML_OP_DIAG_MASK_INF:
  13694. func = ggml_sycl_diag_mask_inf;
  13695. break;
  13696. case GGML_OP_SOFT_MAX:
  13697. func = ggml_sycl_soft_max;
  13698. break;
  13699. case GGML_OP_ROPE:
  13700. func = ggml_sycl_rope;
  13701. break;
  13702. case GGML_OP_ALIBI:
  13703. func = ggml_sycl_alibi;
  13704. break;
  13705. case GGML_OP_IM2COL:
  13706. func = ggml_sycl_im2col;
  13707. break;
  13708. case GGML_OP_POOL_2D:
  13709. func = ggml_sycl_pool2d;
  13710. break;
  13711. case GGML_OP_SUM_ROWS:
  13712. func = ggml_sycl_sum_rows;
  13713. break;
  13714. case GGML_OP_ARGSORT:
  13715. func = ggml_sycl_argsort;
  13716. break;
  13717. default:
  13718. return false;
  13719. }
  13720. if (tensor->src[0] != nullptr && tensor->src[0]->backend == GGML_BACKEND_TYPE_GPU_SPLIT) {
  13721. ggml_sycl_set_peer_access(tensor->src[1]->ne[1]);
  13722. }
  13723. if (params->ith != 0) {
  13724. return true;
  13725. }
  13726. if (params->type == GGML_TASK_TYPE_INIT || params->type == GGML_TASK_TYPE_FINALIZE) {
  13727. return true;
  13728. }
  13729. func(tensor->src[0], tensor->src[1], tensor);
  13730. return true;
  13731. }
  13732. GGML_API GGML_CALL void ggml_sycl_get_gpu_list(int *id_list, int max_len) try {
  13733. GGML_SYCL_DEBUG("[SYCL] call ggml_sycl_get_gpu_list\n");
  13734. for(int i=0;i<max_len;i++) id_list[i] = -1;
  13735. if (!g_sycl_gpu_mgr) {
  13736. g_sycl_gpu_mgr = new sycl_gpu_mgr();
  13737. }
  13738. for (int i=0;i< g_sycl_gpu_mgr->gpus.size();i++){
  13739. if (i>=max_len) break;
  13740. id_list[i] = g_sycl_gpu_mgr->gpus[i];
  13741. }
  13742. return;
  13743. }
  13744. catch (sycl::exception const &exc) {
  13745. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13746. << ", line:" << __LINE__ << std::endl;
  13747. std::exit(1);
  13748. }
  13749. int ggml_sycl_get_device_count() try {
  13750. int device_count;
  13751. if (CHECK_TRY_ERROR(device_count =
  13752. dpct::dev_mgr::instance().device_count()) != 0) {
  13753. return 0;
  13754. }
  13755. return device_count;
  13756. }
  13757. catch (sycl::exception const &exc) {
  13758. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13759. << ", line:" << __LINE__ << std::endl;
  13760. std::exit(1);
  13761. }
  13762. GGML_API GGML_CALL void ggml_sycl_get_device_description(int device, char *description,
  13763. size_t description_size) try {
  13764. GGML_SYCL_DEBUG("[SYCL] call ggml_sycl_get_device_description\n");
  13765. dpct::device_info prop;
  13766. int device_id = g_sycl_gpu_mgr->gpus[device];
  13767. SYCL_CHECK(CHECK_TRY_ERROR(dpct::get_device_info(
  13768. prop, dpct::dev_mgr::instance().get_device(device_id))));
  13769. snprintf(description, description_size, "%s", prop.get_name());
  13770. }
  13771. catch (sycl::exception const &exc) {
  13772. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13773. << ", line:" << __LINE__ << std::endl;
  13774. std::exit(1);
  13775. }
  13776. GGML_CALL void ggml_backend_sycl_get_device_memory(int device, size_t *free,
  13777. size_t *total) try {
  13778. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_memory\n");
  13779. ggml_sycl_set_device(device);
  13780. /*
  13781. DPCT1009:218: SYCL uses exceptions to report errors and does not use the
  13782. error codes. The original code was commented out and a warning string was
  13783. inserted. You need to rewrite this code.
  13784. */
  13785. /*
  13786. DPCT1106:217: 'cudaMemGetInfo' was migrated with the Intel extensions for
  13787. device information which may not be supported by all compilers or runtimes.
  13788. You may need to adjust the code.
  13789. */
  13790. int device_id = g_sycl_gpu_mgr->gpus[device];
  13791. SYCL_CHECK(CHECK_TRY_ERROR(
  13792. dpct::dev_mgr::instance().get_device(device_id).get_memory_info(*free, *total)));
  13793. }
  13794. catch (sycl::exception const &exc) {
  13795. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13796. << ", line:" << __LINE__ << std::endl;
  13797. std::exit(1);
  13798. }
  13799. ////////////////////////////////////////////////////////////////////////////////
  13800. // backend interface
  13801. #define UNUSED GGML_UNUSED
  13802. // sycl buffer
  13803. struct ggml_backend_sycl_buffer_context {
  13804. int device;
  13805. void * dev_ptr = nullptr;
  13806. ggml_tensor_extra_gpu * temp_tensor_extras = nullptr;
  13807. size_t temp_tensor_extra_index = 0;
  13808. std::string name;
  13809. ggml_backend_sycl_buffer_context(int device, void * dev_ptr) :
  13810. device(device), dev_ptr(dev_ptr) {
  13811. check_allow_gpu_index(device);
  13812. int id = g_sycl_gpu_mgr->gpus[device];
  13813. name = (GGML_SYCL_NAME + std::to_string(id));
  13814. }
  13815. ~ ggml_backend_sycl_buffer_context() {
  13816. delete[] temp_tensor_extras;
  13817. }
  13818. ggml_tensor_extra_gpu * ggml_sycl_alloc_temp_tensor_extra() {
  13819. if (temp_tensor_extras == nullptr) {
  13820. temp_tensor_extras = new ggml_tensor_extra_gpu[GGML_SYCL_MAX_NODES];
  13821. }
  13822. size_t alloc_index = temp_tensor_extra_index;
  13823. temp_tensor_extra_index = (temp_tensor_extra_index + 1) % GGML_SYCL_MAX_NODES;
  13824. ggml_tensor_extra_gpu * extra = &temp_tensor_extras[alloc_index];
  13825. memset(extra, 0, sizeof(*extra));
  13826. return extra;
  13827. }
  13828. };
  13829. GGML_CALL static const char * ggml_backend_sycl_buffer_get_name(ggml_backend_buffer_t buffer) {
  13830. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  13831. return ctx->name.c_str();
  13832. }
  13833. GGML_CALL static bool ggml_backend_buffer_is_sycl(ggml_backend_buffer_t buffer) {
  13834. return buffer->iface.get_name == ggml_backend_sycl_buffer_get_name;
  13835. }
  13836. static void
  13837. ggml_backend_sycl_buffer_free_buffer(ggml_backend_buffer_t buffer) try {
  13838. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  13839. ggml_sycl_set_device(ctx->device);
  13840. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  13841. SYCL_CHECK(
  13842. CHECK_TRY_ERROR(sycl::free(ctx->dev_ptr, *stream)));
  13843. delete ctx;
  13844. }
  13845. catch (sycl::exception const &exc) {
  13846. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13847. << ", line:" << __LINE__ << std::endl;
  13848. std::exit(1);
  13849. }
  13850. static void * ggml_backend_sycl_buffer_get_base(ggml_backend_buffer_t buffer) {
  13851. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  13852. return ctx->dev_ptr;
  13853. }
  13854. GGML_CALL static void
  13855. ggml_backend_sycl_buffer_init_tensor(ggml_backend_buffer_t buffer,
  13856. ggml_tensor *tensor) try {
  13857. ggml_backend_sycl_buffer_context * ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  13858. if (tensor->view_src != NULL && tensor->view_offs == 0) {
  13859. assert(tensor->view_src->buffer->buft == buffer->buft);
  13860. tensor->backend = tensor->view_src->backend;
  13861. tensor->extra = tensor->view_src->extra;
  13862. return;
  13863. }
  13864. ggml_tensor_extra_gpu * extra = ctx->ggml_sycl_alloc_temp_tensor_extra();
  13865. extra->data_device[ctx->device] = tensor->data;
  13866. tensor->backend = GGML_BACKEND_TYPE_GPU;
  13867. tensor->extra = extra;
  13868. if (ggml_is_quantized(tensor->type)) {
  13869. // initialize padding to 0 to avoid possible NaN values
  13870. size_t original_size = ggml_nbytes(tensor);
  13871. size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor);
  13872. if (padded_size > original_size && tensor->view_src == nullptr) {
  13873. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[ctx->device][0]->memset(
  13874. (char *)tensor->data + original_size, 0,
  13875. padded_size - original_size).wait()));
  13876. }
  13877. }
  13878. }
  13879. catch (sycl::exception const &exc) {
  13880. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13881. << ", line:" << __LINE__ << std::endl;
  13882. std::exit(1);
  13883. }
  13884. static void ggml_backend_sycl_buffer_set_tensor(ggml_backend_buffer_t buffer,
  13885. ggml_tensor *tensor,
  13886. const void *data, size_t offset,
  13887. size_t size) try {
  13888. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  13889. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  13890. ggml_sycl_set_device(ctx->device);
  13891. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  13892. SYCL_CHECK(
  13893. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  13894. SYCL_CHECK(
  13895. CHECK_TRY_ERROR((*stream)
  13896. .memcpy((char *)tensor->data + offset, data, size)
  13897. .wait()));
  13898. }
  13899. catch (sycl::exception const &exc) {
  13900. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13901. << ", line:" << __LINE__ << std::endl;
  13902. std::exit(1);
  13903. }
  13904. static void ggml_backend_sycl_buffer_get_tensor(ggml_backend_buffer_t buffer,
  13905. const ggml_tensor *tensor,
  13906. void *data, size_t offset,
  13907. size_t size) try {
  13908. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  13909. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  13910. ggml_sycl_set_device(ctx->device);
  13911. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  13912. SYCL_CHECK(
  13913. CHECK_TRY_ERROR(dpct::dev_mgr::instance().get_device(ctx->device).queues_wait_and_throw()));
  13914. SYCL_CHECK(CHECK_TRY_ERROR(
  13915. (*stream)
  13916. .memcpy(data, (const char *)tensor->data + offset, size)
  13917. .wait()));
  13918. }
  13919. catch (sycl::exception const &exc) {
  13920. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13921. << ", line:" << __LINE__ << std::endl;
  13922. std::exit(1);
  13923. }
  13924. GGML_CALL static bool
  13925. ggml_backend_sycl_buffer_cpy_tensor(ggml_backend_buffer_t buffer,
  13926. const ggml_tensor *src,
  13927. ggml_tensor *dst) try {
  13928. if (ggml_backend_buffer_is_sycl(src->buffer)) {
  13929. ggml_backend_sycl_buffer_context * src_ctx = (ggml_backend_sycl_buffer_context *)src->buffer->context;
  13930. ggml_backend_sycl_buffer_context * dst_ctx = (ggml_backend_sycl_buffer_context *)buffer->context;
  13931. ggml_sycl_set_device(src_ctx->device);
  13932. /*
  13933. DPCT1009:198: SYCL uses exceptions to report errors and does not use the
  13934. error codes. The original code was commented out and a warning string
  13935. was inserted. You need to rewrite this code.
  13936. */
  13937. SYCL_CHECK(CHECK_TRY_ERROR(
  13938. dpct::dev_mgr::instance().get_device(src_ctx->device).queues_wait_and_throw()));
  13939. ggml_sycl_set_device(dst_ctx->device);
  13940. /*
  13941. DPCT1009:199: SYCL uses exceptions to report errors and does not use the
  13942. error codes. The original code was commented out and a warning string
  13943. was inserted. You need to rewrite this code.
  13944. */
  13945. SYCL_CHECK(CHECK_TRY_ERROR(
  13946. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  13947. /*
  13948. DPCT1009:200: SYCL uses exceptions to report errors and does not use the
  13949. error codes. The original code was commented out and a warning string
  13950. was inserted. You need to rewrite this code.
  13951. */
  13952. dpct::queue_ptr stream_dst = g_syclStreams[dst_ctx->device][0];
  13953. dpct::queue_ptr stream_src = g_syclStreams[src_ctx->device][0];
  13954. size_t size = ggml_nbytes(src);
  13955. //todo. it's dirty solutino to walkaroud known issue:device2device cross GPUs.
  13956. dev2dev_memcpy(*stream_dst, *stream_src, dst->data, src->data, size);
  13957. //todo, it's known issue:error in device2device cross GPUs. reused when the issue is fixed. DON"T remove
  13958. #if 0
  13959. SYCL_CHECK(CHECK_TRY_ERROR((*stream).memcpy(
  13960. (char *)dst->data, (const char *)src->data, size).wait()));
  13961. /*
  13962. DPCT1009:201: SYCL uses exceptions to report errors and does not use the
  13963. error codes. The original code was commented out and a warning string
  13964. was inserted. You need to rewrite this code.
  13965. */
  13966. SYCL_CHECK(CHECK_TRY_ERROR(
  13967. dpct::dev_mgr::instance().get_device(dst_ctx->device).queues_wait_and_throw()));
  13968. #endif
  13969. return true;
  13970. }
  13971. return false;
  13972. }
  13973. catch (sycl::exception const &exc) {
  13974. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13975. << ", line:" << __LINE__ << std::endl;
  13976. std::exit(1);
  13977. }
  13978. static void ggml_backend_sycl_buffer_clear(ggml_backend_buffer_t buffer,
  13979. uint8_t value) try {
  13980. ggml_backend_sycl_buffer_context * ctx = ( ggml_backend_sycl_buffer_context *)buffer->context;
  13981. ggml_sycl_set_device(ctx->device);
  13982. const dpct::queue_ptr stream = g_syclStreams[ctx->device][0];
  13983. SYCL_CHECK(
  13984. CHECK_TRY_ERROR(dpct::get_current_device().queues_wait_and_throw()));
  13985. SYCL_CHECK(CHECK_TRY_ERROR((*stream)
  13986. .memset(ctx->dev_ptr, value, buffer->size)
  13987. .wait()));
  13988. }
  13989. catch (sycl::exception const &exc) {
  13990. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  13991. << ", line:" << __LINE__ << std::endl;
  13992. std::exit(1);
  13993. }
  13994. static struct ggml_backend_buffer_i ggml_backend_sycl_buffer_interface = {
  13995. /* .get_name = */ ggml_backend_sycl_buffer_get_name,
  13996. /* .free_buffer = */ ggml_backend_sycl_buffer_free_buffer,
  13997. /* .get_base = */ ggml_backend_sycl_buffer_get_base,
  13998. /* .init_tensor = */ ggml_backend_sycl_buffer_init_tensor,
  13999. /* .set_tensor = */ ggml_backend_sycl_buffer_set_tensor,
  14000. /* .get_tensor = */ ggml_backend_sycl_buffer_get_tensor,
  14001. /* .cpy_tensor = */ ggml_backend_sycl_buffer_cpy_tensor,
  14002. /* .clear = */ ggml_backend_sycl_buffer_clear,
  14003. /* .reset = */ NULL,
  14004. };
  14005. // sycl buffer type
  14006. struct ggml_backend_sycl_buffer_type_context {
  14007. int device;
  14008. std::string name;
  14009. };
  14010. struct ggml_backend_sycl_context {
  14011. int device;
  14012. std::string name;
  14013. };
  14014. GGML_CALL static const char * ggml_backend_sycl_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14015. ggml_backend_sycl_buffer_type_context * ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14016. return ctx->name.c_str();
  14017. }
  14018. GGML_CALL static ggml_backend_buffer_t
  14019. ggml_backend_sycl_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft,
  14020. size_t size) try {
  14021. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14022. ggml_sycl_set_device(buft_ctx->device);
  14023. const dpct::queue_ptr stream = g_syclStreams[buft_ctx->device][0];
  14024. size = std::max(size, (size_t)1); // syclMalloc returns null for size 0
  14025. void * dev_ptr;
  14026. SYCL_CHECK(CHECK_TRY_ERROR(dev_ptr = (void *)sycl::malloc_device(
  14027. size, *stream)));
  14028. ggml_backend_sycl_buffer_context * ctx = new ggml_backend_sycl_buffer_context(buft_ctx->device, dev_ptr);
  14029. return ggml_backend_buffer_init(buft, ggml_backend_sycl_buffer_interface, ctx, size);
  14030. }
  14031. catch (sycl::exception const &exc) {
  14032. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14033. << ", line:" << __LINE__ << std::endl;
  14034. std::exit(1);
  14035. }
  14036. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  14037. return 128;
  14038. UNUSED(buft);
  14039. }
  14040. static size_t ggml_backend_sycl_buffer_type_get_max_size(ggml_backend_buffer_type_t buft) {
  14041. return dpct::get_current_device().get_max_mem_alloc_size();
  14042. UNUSED(buft);
  14043. }
  14044. GGML_CALL static size_t ggml_backend_sycl_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  14045. size_t size = ggml_nbytes(tensor);
  14046. int64_t ne0 = tensor->ne[0];
  14047. if (ggml_is_quantized(tensor->type)) {
  14048. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14049. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14050. }
  14051. }
  14052. return size;
  14053. UNUSED(buft);
  14054. }
  14055. GGML_CALL static bool ggml_backend_sycl_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  14056. if (!ggml_backend_is_sycl(backend)) {
  14057. return false;
  14058. }
  14059. ggml_backend_sycl_buffer_type_context * buft_ctx = (ggml_backend_sycl_buffer_type_context *)buft->context;
  14060. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14061. return buft_ctx->device == sycl_ctx->device;
  14062. }
  14063. static ggml_backend_buffer_type_i ggml_backend_sycl_buffer_type_interface = {
  14064. /* .get_name = */ ggml_backend_sycl_buffer_type_name,
  14065. /* .alloc_buffer = */ ggml_backend_sycl_buffer_type_alloc_buffer,
  14066. /* .get_alignment = */ ggml_backend_sycl_buffer_type_get_alignment,
  14067. /* .get_max_size = */ ggml_backend_sycl_buffer_type_get_max_size,
  14068. /* .get_alloc_size = */ ggml_backend_sycl_buffer_type_get_alloc_size,
  14069. /* .supports_backend = */ ggml_backend_sycl_buffer_type_supports_backend,
  14070. /* .is_host = */ nullptr,
  14071. };
  14072. ggml_backend_buffer_type_t ggml_backend_sycl_buffer_type(int device_index) {
  14073. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_buffer_type\n");
  14074. if (device_index>=g_device_count or device_index<0) {
  14075. printf("ggml_backend_sycl_buffer_type error: device_index:%d is out of range [0, %d], miss to call ggml_backend_sycl_set_single_device()\n",
  14076. device_index, g_device_count-1);
  14077. GGML_ASSERT(device_index<g_device_count);
  14078. }
  14079. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_types[GGML_SYCL_MAX_DEVICES];
  14080. if (!g_ggml_backend_sycl_buffer_type_initialized) {
  14081. for (int i = 0; i < g_device_count; i++) {
  14082. ggml_backend_sycl_buffer_types[i] = {
  14083. /* .iface = */ ggml_backend_sycl_buffer_type_interface,
  14084. /* .context = */ new ggml_backend_sycl_buffer_type_context{i, GGML_SYCL_NAME + std::to_string(g_sycl_gpu_mgr->gpus[i])},
  14085. };
  14086. }
  14087. g_ggml_backend_sycl_buffer_type_initialized = true;
  14088. }
  14089. return &ggml_backend_sycl_buffer_types[device_index];
  14090. }
  14091. // sycl split buffer type
  14092. 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) {
  14093. const int64_t nrows = ggml_nrows(tensor);
  14094. const int64_t rounding = get_row_rounding(tensor->type, tensor_split);
  14095. *row_low = id == 0 ? 0 : nrows*tensor_split[id];
  14096. *row_low -= *row_low % rounding;
  14097. if (id == g_device_count - 1) {
  14098. *row_high = nrows;
  14099. } else {
  14100. *row_high = nrows*tensor_split[id + 1];
  14101. *row_high -= *row_high % rounding;
  14102. }
  14103. }
  14104. struct ggml_backend_sycl_split_buffer_context {
  14105. ~ggml_backend_sycl_split_buffer_context() try {
  14106. for (ggml_tensor_extra_gpu * extra : tensor_extras) {
  14107. for (int i = 0; i < g_device_count; ++i) {
  14108. // int id = g_sycl_gpu_mgr->gpus[i];
  14109. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  14110. if (extra->events[i][is] != nullptr) {
  14111. /*
  14112. DPCT1009:206: SYCL uses exceptions to report errors and
  14113. does not use the error codes. The original code was
  14114. commented out and a warning string was inserted. You
  14115. need to rewrite this code.
  14116. */
  14117. SYCL_CHECK(CHECK_TRY_ERROR(
  14118. dpct::destroy_event(extra->events[i][is])));
  14119. }
  14120. }
  14121. if (extra->data_device[i] != nullptr) {
  14122. /*
  14123. DPCT1009:207: SYCL uses exceptions to report errors and does
  14124. not use the error codes. The original code was commented out
  14125. and a warning string was inserted. You need to rewrite this
  14126. code.
  14127. */
  14128. ggml_sycl_set_device(i);
  14129. SYCL_CHECK(CHECK_TRY_ERROR(sycl::free(
  14130. extra->data_device[i], *g_syclStreams[i][0])));
  14131. }
  14132. }
  14133. delete extra;
  14134. }
  14135. }
  14136. catch (sycl::exception const &exc) {
  14137. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14138. << ", line:" << __LINE__ << std::endl;
  14139. std::exit(1);
  14140. }
  14141. std::vector<ggml_tensor_extra_gpu *> tensor_extras;
  14142. };
  14143. GGML_CALL static const char * ggml_backend_sycl_split_buffer_get_name(ggml_backend_buffer_t buffer) {
  14144. return GGML_SYCL_NAME "_Split";
  14145. UNUSED(buffer);
  14146. }
  14147. // unused at the moment
  14148. //static bool ggml_backend_buffer_is_sycl_split(ggml_backend_buffer_t buffer) {
  14149. // return buffer->iface.get_name == ggml_backend_sycl_split_buffer_get_name;
  14150. //}
  14151. GGML_CALL static void ggml_backend_sycl_split_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  14152. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  14153. delete ctx;
  14154. }
  14155. GGML_CALL static void * ggml_backend_sycl_split_buffer_get_base(ggml_backend_buffer_t buffer) {
  14156. // the pointers are stored in the tensor extras, this is just a dummy address and never dereferenced
  14157. return (void *)0x1000;
  14158. UNUSED(buffer);
  14159. }
  14160. GGML_CALL static void
  14161. ggml_backend_sycl_split_buffer_init_tensor(ggml_backend_buffer_t buffer,
  14162. ggml_tensor *tensor) try {
  14163. GGML_ASSERT(tensor->view_src == nullptr); // views of split tensors are not supported
  14164. ggml_backend_sycl_split_buffer_context * ctx = (ggml_backend_sycl_split_buffer_context *)buffer->context;
  14165. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14166. const int64_t ne0 = tensor->ne[0];
  14167. ggml_tensor_extra_gpu * extra = new ggml_tensor_extra_gpu{};
  14168. ctx->tensor_extras.push_back(extra);
  14169. for (int i = 0; i < g_device_count; ++i) {
  14170. // int id = g_sycl_gpu_mgr->gpus[i];
  14171. int64_t row_low, row_high;
  14172. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14173. int64_t nrows_split = row_high - row_low;
  14174. if (nrows_split == 0) {
  14175. continue;
  14176. }
  14177. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14178. const size_t original_size = size;
  14179. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14180. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14181. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14182. }
  14183. // FIXME: do not crash if cudaMalloc fails
  14184. // currently, init_tensor cannot fail, it needs to be fixed in ggml-backend first
  14185. ggml_sycl_set_device(i);
  14186. char * buf;
  14187. /*
  14188. DPCT1009:208: SYCL uses exceptions to report errors and does not use the
  14189. error codes. The original code was commented out and a warning string
  14190. was inserted. You need to rewrite this code.
  14191. */
  14192. SYCL_CHECK(CHECK_TRY_ERROR(buf = (char *)sycl::malloc_device(
  14193. size, *g_syclStreams[i][0])));
  14194. // set padding to 0 to avoid possible NaN values
  14195. if (size > original_size) {
  14196. /*
  14197. DPCT1009:209: SYCL uses exceptions to report errors and does not use
  14198. the error codes. The original code was commented out and a warning
  14199. string was inserted. You need to rewrite this code.
  14200. */
  14201. SYCL_CHECK(CHECK_TRY_ERROR(
  14202. (*g_syclStreams[i][0])
  14203. .memset(buf + original_size, 0, size - original_size)
  14204. .wait()));
  14205. }
  14206. extra->data_device[i] = buf;
  14207. for (int64_t is = 0; is < MAX_STREAMS; ++is) {
  14208. /*
  14209. DPCT1009:210: SYCL uses exceptions to report errors and does not use
  14210. the error codes. The original code was commented out and a warning
  14211. string was inserted. You need to rewrite this code.
  14212. */
  14213. SYCL_CHECK(
  14214. CHECK_TRY_ERROR(extra->events[i][is] = new sycl::event()));
  14215. }
  14216. }
  14217. tensor->backend = GGML_BACKEND_TYPE_GPU_SPLIT;
  14218. tensor->extra = extra;
  14219. }
  14220. catch (sycl::exception const &exc) {
  14221. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14222. << ", line:" << __LINE__ << std::endl;
  14223. std::exit(1);
  14224. }
  14225. GGML_CALL static void
  14226. ggml_backend_sycl_split_buffer_set_tensor(ggml_backend_buffer_t buffer,
  14227. ggml_tensor *tensor, const void *data,
  14228. size_t offset, size_t size) try {
  14229. // split tensors must always be set in their entirety at once
  14230. GGML_ASSERT(offset == 0);
  14231. GGML_ASSERT(size == ggml_nbytes(tensor));
  14232. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14233. const int64_t ne0 = tensor->ne[0];
  14234. const size_t nb1 = tensor->nb[1];
  14235. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  14236. for (int i = 0; i < g_device_count; ++i) {
  14237. // int id = g_sycl_gpu_mgr->gpus[i];
  14238. int64_t row_low, row_high;
  14239. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14240. int64_t nrows_split = row_high - row_low;
  14241. if (nrows_split == 0) {
  14242. continue;
  14243. }
  14244. const size_t offset_split = row_low*nb1;
  14245. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14246. const size_t original_size = size;
  14247. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14248. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14249. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14250. }
  14251. const char * buf_host = (const char *)data + offset_split;
  14252. /*
  14253. DPCT1009:211: SYCL uses exceptions to report errors and does not use the
  14254. error codes. The original code was commented out and a warning string
  14255. was inserted. You need to rewrite this code.
  14256. */
  14257. ggml_sycl_set_device(i);
  14258. SYCL_CHECK(CHECK_TRY_ERROR(
  14259. (*g_syclStreams[i][0])
  14260. .memcpy(extra->data_device[i], buf_host, original_size)
  14261. .wait()));
  14262. }
  14263. }
  14264. catch (sycl::exception const &exc) {
  14265. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14266. << ", line:" << __LINE__ << std::endl;
  14267. std::exit(1);
  14268. }
  14269. GGML_CALL static void
  14270. ggml_backend_sycl_split_buffer_get_tensor(ggml_backend_buffer_t buffer,
  14271. const ggml_tensor *tensor, void *data,
  14272. size_t offset, size_t size) try {
  14273. // split tensors must always be set in their entirety at once
  14274. GGML_ASSERT(offset == 0);
  14275. GGML_ASSERT(size == ggml_nbytes(tensor));
  14276. ggml_backend_sycl_split_buffer_type_context * buft_ctx = (ggml_backend_sycl_split_buffer_type_context *)buffer->buft->context;
  14277. const int64_t ne0 = tensor->ne[0];
  14278. const size_t nb1 = tensor->nb[1];
  14279. ggml_tensor_extra_gpu * extra = (ggml_tensor_extra_gpu *)tensor->extra;
  14280. for (int i = 0; i < g_device_count; ++i) {
  14281. // int id = g_sycl_gpu_mgr->gpus[i];
  14282. int64_t row_low, row_high;
  14283. get_row_split(&row_low, &row_high, tensor, buft_ctx->tensor_split, i);
  14284. int64_t nrows_split = row_high - row_low;
  14285. if (nrows_split == 0) {
  14286. continue;
  14287. }
  14288. const size_t offset_split = row_low*nb1;
  14289. size_t size = ggml_nbytes_split(tensor, nrows_split);
  14290. const size_t original_size = size;
  14291. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14292. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14293. size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14294. }
  14295. char * buf_host = (char *)data + offset_split;
  14296. /*
  14297. DPCT1009:212: SYCL uses exceptions to report errors and does not use the
  14298. error codes. The original code was commented out and a warning string
  14299. was inserted. You need to rewrite this code.
  14300. */
  14301. ggml_sycl_set_device(i);
  14302. SYCL_CHECK(CHECK_TRY_ERROR(
  14303. (*g_syclStreams[i][0])
  14304. .memcpy(buf_host, extra->data_device[i], original_size)
  14305. .wait()));
  14306. }
  14307. }
  14308. catch (sycl::exception const &exc) {
  14309. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14310. << ", line:" << __LINE__ << std::endl;
  14311. std::exit(1);
  14312. }
  14313. GGML_CALL static void ggml_backend_sycl_split_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  14314. UNUSED(buffer);
  14315. UNUSED(value);
  14316. }
  14317. static struct ggml_backend_buffer_i ggml_backend_sycl_split_buffer_interface = {
  14318. /* .get_name = */ ggml_backend_sycl_split_buffer_get_name,
  14319. /* .free_buffer = */ ggml_backend_sycl_split_buffer_free_buffer,
  14320. /* .get_base = */ ggml_backend_sycl_split_buffer_get_base,
  14321. /* .init_tensor = */ ggml_backend_sycl_split_buffer_init_tensor,
  14322. /* .set_tensor = */ ggml_backend_sycl_split_buffer_set_tensor,
  14323. /* .get_tensor = */ ggml_backend_sycl_split_buffer_get_tensor,
  14324. /* .cpy_tensor = */ NULL,
  14325. /* .clear = */ ggml_backend_sycl_split_buffer_clear,
  14326. /* .reset = */ NULL,
  14327. };
  14328. GGML_CALL static const char * ggml_backend_sycl_split_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14329. return GGML_SYCL_NAME "_Split";
  14330. UNUSED(buft);
  14331. }
  14332. GGML_CALL static ggml_backend_buffer_t ggml_backend_sycl_split_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  14333. // since we don't know the exact split after rounding, we cannot allocate the device buffers at this point
  14334. // instead, we allocate them for each tensor separately in init_tensor
  14335. // however, the size still represents the maximum cumulative size of all the device buffers after the tensors are allocated,
  14336. // as returned by get_alloc_size. this limit is enforced during tensor allocation by ggml-alloc, so it must be correct.
  14337. ggml_backend_sycl_split_buffer_context * ctx = new ggml_backend_sycl_split_buffer_context();
  14338. return ggml_backend_buffer_init(buft, ggml_backend_sycl_split_buffer_interface, ctx, size);
  14339. }
  14340. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  14341. return 128;
  14342. UNUSED(buft);
  14343. }
  14344. GGML_CALL static size_t ggml_backend_sycl_split_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) {
  14345. ggml_backend_sycl_split_buffer_type_context * ctx = (ggml_backend_sycl_split_buffer_type_context *)buft->context;
  14346. size_t total_size = 0;
  14347. const int64_t ne0 = tensor->ne[0];
  14348. for (int i = 0; i < g_device_count; ++i) {
  14349. // int id = g_sycl_gpu_mgr->gpus[i];
  14350. int64_t row_low, row_high;
  14351. get_row_split(&row_low, &row_high, tensor, ctx->tensor_split, i);
  14352. int64_t nrows_split = row_high - row_low;
  14353. if (nrows_split == 0) {
  14354. continue;
  14355. }
  14356. total_size += ggml_nbytes_split(tensor, nrows_split);
  14357. // pad last row to a multiple of 512 elements to avoid out-of-bounds memory accesses
  14358. if (ne0 % MATRIX_ROW_PADDING != 0) {
  14359. total_size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING);
  14360. }
  14361. }
  14362. return total_size;
  14363. }
  14364. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_supports_backend(ggml_backend_buffer_type_t buft, ggml_backend_t backend) {
  14365. return ggml_backend_is_sycl(backend);
  14366. UNUSED(buft);
  14367. }
  14368. GGML_CALL static bool ggml_backend_sycl_split_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  14369. return false;
  14370. UNUSED(buft);
  14371. }
  14372. static ggml_backend_buffer_type_i ggml_backend_sycl_split_buffer_type_interface = {
  14373. /* .get_name = */ ggml_backend_sycl_split_buffer_type_name,
  14374. /* .alloc_buffer = */ ggml_backend_sycl_split_buffer_type_alloc_buffer,
  14375. /* .get_alignment = */ ggml_backend_sycl_split_buffer_type_get_alignment,
  14376. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  14377. /* .get_alloc_size = */ ggml_backend_sycl_split_buffer_type_get_alloc_size,
  14378. /* .supports_backend = */ ggml_backend_sycl_split_buffer_type_supports_backend,
  14379. /* .is_host = */ ggml_backend_sycl_split_buffer_type_is_host,
  14380. };
  14381. GGML_CALL ggml_backend_buffer_type_t ggml_backend_sycl_split_buffer_type(const float * tensor_split) {
  14382. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_split_buffer_type\n");
  14383. ggml_init_sycl();
  14384. // FIXME: this is not thread safe
  14385. static std::map<std::array<float, GGML_SYCL_MAX_DEVICES>, struct ggml_backend_buffer_type> buft_map;
  14386. std::array<float, GGML_SYCL_MAX_DEVICES> tensor_split_arr = {};
  14387. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + GGML_SYCL_MAX_DEVICES, [](float x) { return x == 0.0f; });
  14388. if (all_zero) {
  14389. tensor_split_arr = g_default_tensor_split;
  14390. } else {
  14391. float split_sum = 0.0f;
  14392. for (int i = 0; i < g_device_count; ++i) {
  14393. // int id = g_sycl_gpu_mgr->gpus[i];
  14394. tensor_split_arr[i] = split_sum;
  14395. split_sum += tensor_split[i];
  14396. }
  14397. for (int i = 0; i < g_device_count; ++i) {
  14398. // int id = g_sycl_gpu_mgr->gpus[i];
  14399. tensor_split_arr[i] /= split_sum;
  14400. }
  14401. }
  14402. auto it = buft_map.find(tensor_split_arr);
  14403. if (it != buft_map.end()) {
  14404. return &it->second;
  14405. }
  14406. struct ggml_backend_buffer_type buft {
  14407. /* .iface = */ ggml_backend_sycl_split_buffer_type_interface,
  14408. /* .context = */ new ggml_backend_sycl_split_buffer_type_context{tensor_split_arr},
  14409. };
  14410. auto result = buft_map.emplace(tensor_split_arr, buft);
  14411. return &result.first->second;
  14412. }
  14413. // host buffer type
  14414. GGML_CALL static const char * ggml_backend_sycl_host_buffer_type_name(ggml_backend_buffer_type_t buft) {
  14415. return GGML_SYCL_NAME "_Host";
  14416. UNUSED(buft);
  14417. }
  14418. GGML_CALL static const char * ggml_backend_sycl_host_buffer_name(ggml_backend_buffer_t buffer) {
  14419. return GGML_SYCL_NAME "_Host";
  14420. UNUSED(buffer);
  14421. }
  14422. static void ggml_backend_sycl_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  14423. ggml_sycl_host_free(buffer->context);
  14424. }
  14425. static ggml_backend_buffer_t ggml_backend_sycl_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  14426. void * ptr = ggml_sycl_host_malloc(size);
  14427. if (ptr == nullptr) {
  14428. // fallback to cpu buffer
  14429. return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size);
  14430. }
  14431. // FIXME: this is a hack to avoid having to implement a new buffer type
  14432. ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(ptr, size);
  14433. buffer->buft = buft;
  14434. buffer->iface.get_name = ggml_backend_sycl_host_buffer_name;
  14435. buffer->iface.free_buffer = ggml_backend_sycl_host_buffer_free_buffer;
  14436. return buffer;
  14437. }
  14438. ggml_backend_buffer_type_t ggml_backend_sycl_host_buffer_type() {
  14439. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_host_buffer_type\n");
  14440. static struct ggml_backend_buffer_type ggml_backend_sycl_buffer_type_host = {
  14441. /* .iface = */ {
  14442. /* .get_name = */ ggml_backend_sycl_host_buffer_type_name,
  14443. /* .alloc_buffer = */ ggml_backend_sycl_host_buffer_type_alloc_buffer,
  14444. /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment,
  14445. /* .get_max_size = */ NULL, // TODO: return device.maxBufferLength
  14446. /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size,
  14447. /* .supports_backend = */ ggml_backend_cpu_buffer_type()->iface.supports_backend,
  14448. /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host,
  14449. },
  14450. /* .context = */ nullptr,
  14451. };
  14452. return &ggml_backend_sycl_buffer_type_host;
  14453. }
  14454. // backend
  14455. GGML_CALL static const char * ggml_backend_sycl_name(ggml_backend_t backend) {
  14456. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14457. return sycl_ctx->name.c_str();
  14458. }
  14459. GGML_CALL static void ggml_backend_sycl_free(ggml_backend_t backend) {
  14460. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14461. delete sycl_ctx;
  14462. delete backend;
  14463. }
  14464. GGML_CALL static ggml_backend_buffer_type_t ggml_backend_sycl_get_default_buffer_type(ggml_backend_t backend) {
  14465. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14466. return ggml_backend_sycl_buffer_type(sycl_ctx->device);
  14467. }
  14468. GGML_CALL static void ggml_backend_sycl_set_tensor_async(ggml_backend_t backend,
  14469. ggml_tensor *tensor,
  14470. const void *data, size_t offset,
  14471. size_t size) try {
  14472. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14473. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  14474. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14475. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14476. (char *)tensor->data + offset, data, size).wait()));
  14477. }
  14478. catch (sycl::exception const &exc) {
  14479. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14480. << ", line:" << __LINE__ << std::endl;
  14481. std::exit(1);
  14482. }
  14483. GGML_CALL static void ggml_backend_sycl_get_tensor_async(ggml_backend_t backend,
  14484. const ggml_tensor *tensor,
  14485. void *data, size_t offset,
  14486. size_t size) try {
  14487. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14488. GGML_ASSERT(tensor->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && "unsupported buffer type");
  14489. GGML_ASSERT(tensor->backend == GGML_BACKEND_TYPE_GPU);
  14490. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14491. data, (const char *)tensor->data + offset, size).wait()));
  14492. }
  14493. catch (sycl::exception const &exc) {
  14494. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14495. << ", line:" << __LINE__ << std::endl;
  14496. std::exit(1);
  14497. }
  14498. GGML_CALL static bool ggml_backend_sycl_cpy_tensor_async(ggml_backend_t backend,
  14499. const ggml_tensor *src,
  14500. ggml_tensor *dst) try {
  14501. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14502. if (dst->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device) && ggml_backend_buffer_is_sycl(src->buffer)) {
  14503. /*
  14504. DPCT1009:215: SYCL uses exceptions to report errors and does not use the
  14505. error codes. The original code was commented out and a warning string
  14506. was inserted. You need to rewrite this code.
  14507. */
  14508. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->memcpy(
  14509. dst->data, src->data, ggml_nbytes(dst)).wait()));
  14510. return true;
  14511. }
  14512. return false;
  14513. }
  14514. catch (sycl::exception const &exc) {
  14515. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14516. << ", line:" << __LINE__ << std::endl;
  14517. std::exit(1);
  14518. }
  14519. static void ggml_backend_sycl_synchronize(ggml_backend_t backend) try {
  14520. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14521. SYCL_CHECK(CHECK_TRY_ERROR(g_syclStreams[sycl_ctx->device][0]->wait()));
  14522. UNUSED(backend);
  14523. }
  14524. catch (sycl::exception const &exc) {
  14525. std::cerr << exc.what() << "Exception caught at file:" << __FILE__
  14526. << ", line:" << __LINE__ << std::endl;
  14527. std::exit(1);
  14528. }
  14529. GGML_CALL static ggml_status ggml_backend_sycl_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) {
  14530. ggml_backend_sycl_context * sycl_ctx = (ggml_backend_sycl_context *)backend->context;
  14531. ggml_sycl_set_main_device(sycl_ctx->device);
  14532. ggml_compute_params params = {};
  14533. params.type = GGML_TASK_TYPE_COMPUTE;
  14534. params.ith = 0;
  14535. for (int i = 0; i < cgraph->n_nodes; i++) {
  14536. ggml_tensor * node = cgraph->nodes[i];
  14537. if (ggml_is_empty(node) || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) {
  14538. continue;
  14539. }
  14540. #ifndef NDEBUG
  14541. assert(node->backend == GGML_BACKEND_TYPE_GPU || node->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  14542. assert(node->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  14543. assert(node->extra != nullptr);
  14544. for (int j = 0; j < GGML_MAX_SRC; j++) {
  14545. if (node->src[j] != nullptr) {
  14546. assert(node->src[j]->backend == GGML_BACKEND_TYPE_GPU || node->src[j]->backend == GGML_BACKEND_TYPE_GPU_SPLIT);
  14547. assert(node->src[j]->buffer->buft == ggml_backend_sycl_buffer_type(sycl_ctx->device));
  14548. assert(node->src[j]->extra != nullptr);
  14549. }
  14550. }
  14551. #endif
  14552. bool ok = ggml_sycl_compute_forward(&params, node);
  14553. if (!ok) {
  14554. fprintf(stderr, "%s: error: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op));
  14555. }
  14556. GGML_ASSERT(ok);
  14557. }
  14558. return GGML_STATUS_SUCCESS;
  14559. }
  14560. GGML_CALL static bool ggml_backend_sycl_supports_op(ggml_backend_t backend, const ggml_tensor * op) {
  14561. switch (op->op) {
  14562. case GGML_OP_UNARY:
  14563. switch (ggml_get_unary_op(op)) {
  14564. case GGML_UNARY_OP_GELU:
  14565. case GGML_UNARY_OP_SILU:
  14566. case GGML_UNARY_OP_RELU:
  14567. case GGML_UNARY_OP_HARDSIGMOID:
  14568. case GGML_UNARY_OP_HARDSWISH:
  14569. case GGML_UNARY_OP_GELU_QUICK:
  14570. case GGML_UNARY_OP_TANH:
  14571. return true;
  14572. default:
  14573. return false;
  14574. }
  14575. break;
  14576. case GGML_OP_MUL_MAT:
  14577. case GGML_OP_MUL_MAT_ID:
  14578. {
  14579. struct ggml_tensor * a;
  14580. struct ggml_tensor * b;
  14581. if (op->op == GGML_OP_MUL_MAT) {
  14582. a = op->src[0];
  14583. b = op->src[1];
  14584. } else {
  14585. a = op->src[2];
  14586. b = op->src[1];
  14587. }
  14588. if (a->ne[3] != b->ne[3]) {
  14589. return false;
  14590. }
  14591. ggml_type a_type = a->type;
  14592. if (a_type == GGML_TYPE_IQ4_NL || a_type == GGML_TYPE_IQ2_S ||
  14593. a_type == GGML_TYPE_IQ4_XS) {
  14594. return false;
  14595. }
  14596. return true;
  14597. } break;
  14598. case GGML_OP_GET_ROWS:
  14599. {
  14600. switch (op->src[0]->type) {
  14601. case GGML_TYPE_F16:
  14602. case GGML_TYPE_F32:
  14603. case GGML_TYPE_Q4_0:
  14604. case GGML_TYPE_Q4_1:
  14605. case GGML_TYPE_Q5_0:
  14606. case GGML_TYPE_Q5_1:
  14607. case GGML_TYPE_Q8_0:
  14608. return true;
  14609. default:
  14610. return false;
  14611. }
  14612. } break;
  14613. case GGML_OP_CPY:
  14614. {
  14615. ggml_type src0_type = op->src[0]->type;
  14616. ggml_type src1_type = op->src[1]->type;
  14617. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F32) {
  14618. return true;
  14619. }
  14620. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_F16) {
  14621. return true;
  14622. }
  14623. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q8_0) {
  14624. return true;
  14625. }
  14626. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_0) {
  14627. return true;
  14628. }
  14629. if (src0_type == GGML_TYPE_F32 && src1_type == GGML_TYPE_Q4_1) {
  14630. return true;
  14631. }
  14632. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F16) {
  14633. return true;
  14634. }
  14635. if (src0_type == GGML_TYPE_F16 && src1_type == GGML_TYPE_F32) {
  14636. return true;
  14637. }
  14638. return false;
  14639. } break;
  14640. case GGML_OP_CONCAT:
  14641. {
  14642. ggml_type src0_type = op->src[0]->type;
  14643. return src0_type != GGML_TYPE_I32 && src0_type != GGML_TYPE_I16;
  14644. } break;
  14645. case GGML_OP_DUP:
  14646. case GGML_OP_NONE:
  14647. case GGML_OP_RESHAPE:
  14648. case GGML_OP_REPEAT:
  14649. case GGML_OP_VIEW:
  14650. case GGML_OP_PERMUTE:
  14651. case GGML_OP_TRANSPOSE:
  14652. case GGML_OP_NORM:
  14653. case GGML_OP_ADD:
  14654. case GGML_OP_MUL:
  14655. case GGML_OP_DIV:
  14656. case GGML_OP_RMS_NORM:
  14657. case GGML_OP_SCALE:
  14658. case GGML_OP_SQR:
  14659. case GGML_OP_CLAMP:
  14660. case GGML_OP_CONT:
  14661. case GGML_OP_DIAG_MASK_INF:
  14662. case GGML_OP_SOFT_MAX:
  14663. case GGML_OP_ROPE:
  14664. case GGML_OP_ALIBI:
  14665. case GGML_OP_IM2COL:
  14666. case GGML_OP_POOL_2D:
  14667. case GGML_OP_SUM_ROWS:
  14668. case GGML_OP_ARGSORT:
  14669. case GGML_OP_ACC:
  14670. case GGML_OP_GROUP_NORM:
  14671. case GGML_OP_UPSCALE:
  14672. case GGML_OP_PAD:
  14673. case GGML_OP_LEAKY_RELU:
  14674. return true;
  14675. default:
  14676. return false;
  14677. }
  14678. UNUSED(backend);
  14679. }
  14680. GGML_CALL static bool ggml_backend_sycl_offload_op(ggml_backend_t backend, const ggml_tensor * op) {
  14681. const int min_batch_size = 32;
  14682. return op->ne[1] >= min_batch_size && op->op != GGML_OP_GET_ROWS;
  14683. GGML_UNUSED(backend);
  14684. }
  14685. static ggml_backend_i ggml_backend_sycl_interface = {
  14686. /* .get_name = */ ggml_backend_sycl_name,
  14687. /* .free = */ ggml_backend_sycl_free,
  14688. /* .get_default_buffer_type = */ ggml_backend_sycl_get_default_buffer_type,
  14689. /* .set_tensor_async = */ ggml_backend_sycl_set_tensor_async,
  14690. /* .get_tensor_async = */ ggml_backend_sycl_get_tensor_async,
  14691. /* .cpy_tensor_async = */ NULL, //ggml_backend_sycl_cpy_tensor_async, // TODO: update for the new interface
  14692. /* .synchronize = */ ggml_backend_sycl_synchronize,
  14693. /* .graph_plan_create = */ NULL,
  14694. /* .graph_plan_free = */ NULL,
  14695. /* .graph_plan_compute = */ NULL,
  14696. /* .graph_compute = */ ggml_backend_sycl_graph_compute,
  14697. /* .supports_op = */ ggml_backend_sycl_supports_op,
  14698. /* .offload_op = */ ggml_backend_sycl_offload_op,
  14699. /* .event_new = */ NULL,
  14700. /* .event_free = */ NULL,
  14701. /* .event_record = */ NULL,
  14702. /* .event_wait = */ NULL,
  14703. /* .event_synchronize = */ NULL,
  14704. };
  14705. static ggml_guid_t ggml_backend_sycl_guid() {
  14706. static ggml_guid guid = { 0x58, 0x05, 0x13, 0x8f, 0xcd, 0x3a, 0x61, 0x9d, 0xe7, 0xcd, 0x98, 0xa9, 0x03, 0xfd, 0x7c, 0x53 };
  14707. return &guid;
  14708. }
  14709. GGML_CALL ggml_backend_t ggml_backend_sycl_init(int device) {
  14710. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_init\n");
  14711. ggml_init_sycl();
  14712. check_allow_gpu_index(device);
  14713. // not strictly necessary, but it may reduce the overhead of the first graph_compute
  14714. ggml_sycl_set_main_device(device);
  14715. int id = g_sycl_gpu_mgr->gpus[device];
  14716. ggml_backend_sycl_context * ctx = new ggml_backend_sycl_context {
  14717. /* .device = */ device,
  14718. /* .name = */ GGML_SYCL_NAME + std::to_string(id),
  14719. };
  14720. ggml_backend_t sycl_backend = new ggml_backend {
  14721. /* .guid = */ ggml_backend_sycl_guid(),
  14722. /* .interface = */ ggml_backend_sycl_interface,
  14723. /* .context = */ ctx
  14724. };
  14725. return sycl_backend;
  14726. }
  14727. bool ggml_backend_is_sycl(ggml_backend_t backend) {
  14728. return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_sycl_guid());
  14729. }
  14730. GGML_CALL int ggml_backend_sycl_get_device_count() {
  14731. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_count\n");
  14732. if (!g_sycl_gpu_mgr) g_sycl_gpu_mgr = new sycl_gpu_mgr();
  14733. return g_sycl_gpu_mgr->get_gpu_count();
  14734. }
  14735. GGML_CALL static ggml_backend_t ggml_backend_reg_sycl_init(const char * params, void * user_data) {
  14736. ggml_backend_t sycl_backend = ggml_backend_sycl_init((int) (intptr_t) user_data);
  14737. return sycl_backend;
  14738. UNUSED(params);
  14739. }
  14740. GGML_API GGML_CALL int ggml_backend_sycl_get_device_index(int device_id) {
  14741. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_index\n");
  14742. return g_sycl_gpu_mgr->get_index(device_id);
  14743. }
  14744. GGML_API GGML_CALL int ggml_backend_sycl_get_device_id(int device_index) {
  14745. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_get_device_id\n");
  14746. return g_sycl_gpu_mgr->gpus[device_index];
  14747. }
  14748. GGML_API GGML_CALL void ggml_backend_sycl_set_single_device_mode(int main_gpu_id) {
  14749. ggml_init_sycl();
  14750. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_set_single_device_mode\n");
  14751. fprintf(stderr, "ggml_backend_sycl_set_single_device: use single device: [%d]\n", main_gpu_id);
  14752. GGML_ASSERT(main_gpu_id<g_all_sycl_device_count);
  14753. if (g_sycl_gpu_mgr) {
  14754. delete g_sycl_gpu_mgr;
  14755. }
  14756. g_sycl_gpu_mgr = new sycl_gpu_mgr(main_gpu_id);
  14757. g_ggml_sycl_backend_gpu_mode = SYCL_SINGLE_GPU_MODE;
  14758. ggml_init_by_gpus(g_sycl_gpu_mgr->get_gpu_count());
  14759. g_ggml_backend_sycl_buffer_type_initialized = false;
  14760. }
  14761. GGML_API GGML_CALL void ggml_backend_sycl_set_mul_device_mode() {
  14762. ggml_init_sycl();
  14763. GGML_SYCL_DEBUG("[SYCL] call ggml_backend_sycl_set_mul_device_mode\n");
  14764. if (g_ggml_sycl_backend_gpu_mode == SYCL_MUL_GPU_MODE) {
  14765. return;
  14766. }
  14767. fprintf(stderr, "ggml_backend_sycl_set_mul_device_mode: true\n");
  14768. if (g_sycl_gpu_mgr) {
  14769. delete g_sycl_gpu_mgr;
  14770. }
  14771. g_sycl_gpu_mgr = new sycl_gpu_mgr();
  14772. g_ggml_sycl_backend_gpu_mode = SYCL_MUL_GPU_MODE;
  14773. ggml_init_by_gpus(g_sycl_gpu_mgr->get_gpu_count());
  14774. g_ggml_backend_sycl_buffer_type_initialized = false;
  14775. }
  14776. extern "C" int ggml_backend_sycl_reg_devices();
  14777. int ggml_backend_sycl_reg_devices() {
  14778. ggml_backend_sycl_set_mul_device_mode();
  14779. assert(g_device_count>0);
  14780. for (int i = 0; i < g_device_count; i++) {
  14781. int id = g_sycl_gpu_mgr->gpus[i];
  14782. char name[128];
  14783. snprintf(name, sizeof(name), "%s%d", GGML_SYCL_NAME, id);
  14784. ggml_backend_register(name, ggml_backend_reg_sycl_init, ggml_backend_sycl_buffer_type(i), (void *) (intptr_t) i);
  14785. }
  14786. return g_device_count;
  14787. }