ggml-cpu.c 477 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337
  1. #define _CRT_SECURE_NO_DEPRECATE // Disables "unsafe" warnings on Windows
  2. #define _USE_MATH_DEFINES // For M_PI on MSVC
  3. #include "ggml-backend-impl.h"
  4. #include "ggml-backend.h"
  5. #include "ggml-cpu-traits.h"
  6. #include "ggml-cpu-impl.h"
  7. #include "ggml-cpu.h"
  8. #include "ggml-impl.h"
  9. #include "ggml-quants.h"
  10. #include "ggml-cpu-quants.h"
  11. #include "ggml-threading.h"
  12. #include "amx/amx.h"
  13. #include "ggml.h"
  14. #if defined(_MSC_VER) || defined(__MINGW32__)
  15. #include <malloc.h> // using malloc.h with MSC/MINGW
  16. #elif !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
  17. #include <alloca.h>
  18. #endif
  19. #include <assert.h>
  20. #include <errno.h>
  21. #include <time.h>
  22. #include <math.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdint.h>
  26. #include <inttypes.h>
  27. #include <stdio.h>
  28. #include <float.h>
  29. #include <limits.h>
  30. #include <stdarg.h>
  31. #include <signal.h>
  32. #if defined(__gnu_linux__)
  33. #include <syscall.h>
  34. #endif
  35. #ifdef GGML_USE_OPENMP
  36. #include <omp.h>
  37. #endif
  38. #if defined(__ARM_FEATURE_SVE) || defined(__ARM_FEATURE_MATMUL_INT8)
  39. #undef GGML_USE_LLAMAFILE
  40. #endif
  41. #ifdef GGML_USE_LLAMAFILE
  42. #include "llamafile/sgemm.h"
  43. #endif
  44. #if defined(_MSC_VER)
  45. // disable "possible loss of data" to avoid hundreds of casts
  46. // we should just be careful :)
  47. #pragma warning(disable: 4244 4267)
  48. // disable POSIX deprecation warnings
  49. // these functions are never going away, anyway
  50. #pragma warning(disable: 4996)
  51. // unreachable code because of multiple instances of code after GGML_ABORT
  52. #pragma warning(disable: 4702)
  53. #endif
  54. // Note: once we move threading into a separate C++ file
  55. // will use std::hardware_destructive_interference_size instead of hardcoding it here
  56. // and we'll use C++ attribute syntax.
  57. #define GGML_CACHE_LINE 64
  58. #if defined(__clang__) || defined(__GNUC__)
  59. #define GGML_CACHE_ALIGN __attribute__((aligned(GGML_CACHE_LINE)))
  60. #endif
  61. #if defined(__has_feature)
  62. #if __has_feature(thread_sanitizer)
  63. #define GGML_TSAN_ENABLED 1
  64. #endif
  65. #else // __has_feature
  66. #if defined(__SANITIZE_THREAD__)
  67. #define GGML_TSAN_ENABLED 1
  68. #endif
  69. #endif // __has_feature
  70. #define UNUSED GGML_UNUSED
  71. #define SWAP(x, y, T) do { T SWAP = x; (x) = y; (y) = SWAP; } while (0)
  72. #if defined(GGML_USE_ACCELERATE)
  73. #include <Accelerate/Accelerate.h>
  74. #endif
  75. // floating point type used to accumulate sums
  76. typedef double ggml_float;
  77. #define GGML_GELU_FP16
  78. #define GGML_GELU_QUICK_FP16
  79. #define GGML_SOFT_MAX_UNROLL 4
  80. #define GGML_VEC_DOT_UNROLL 2
  81. #define GGML_VEC_MAD_UNROLL 32
  82. //
  83. // global data
  84. //
  85. // precomputed gelu table for f16 (128 KB)
  86. static ggml_fp16_t ggml_table_gelu_f16[1 << 16];
  87. // precomputed quick gelu table for f16 (128 KB)
  88. static ggml_fp16_t ggml_table_gelu_quick_f16[1 << 16];
  89. #if defined(__ARM_ARCH)
  90. struct ggml_arm_arch_features_type {
  91. int has_neon;
  92. int has_dotprod;
  93. int has_i8mm;
  94. int has_sve;
  95. int sve_cnt;
  96. } ggml_arm_arch_features = {-1, -1, -1, -1, 0};
  97. #endif
  98. #if defined(_WIN32)
  99. #define WIN32_LEAN_AND_MEAN
  100. #ifndef NOMINMAX
  101. #define NOMINMAX
  102. #endif
  103. #include <windows.h>
  104. #if defined(_MSC_VER) && !defined(__clang__)
  105. #define GGML_CACHE_ALIGN __declspec(align(GGML_CACHE_LINE))
  106. typedef volatile LONG atomic_int;
  107. typedef atomic_int atomic_bool;
  108. typedef atomic_int atomic_flag;
  109. #define ATOMIC_FLAG_INIT 0
  110. typedef enum {
  111. memory_order_relaxed,
  112. memory_order_consume,
  113. memory_order_acquire,
  114. memory_order_release,
  115. memory_order_acq_rel,
  116. memory_order_seq_cst
  117. } memory_order;
  118. static void atomic_store(atomic_int * ptr, LONG val) {
  119. InterlockedExchange(ptr, val);
  120. }
  121. static void atomic_store_explicit(atomic_int * ptr, LONG val, memory_order mo) {
  122. // TODO: add support for explicit memory order
  123. InterlockedExchange(ptr, val);
  124. }
  125. static LONG atomic_load(atomic_int * ptr) {
  126. return InterlockedCompareExchange(ptr, 0, 0);
  127. }
  128. static LONG atomic_load_explicit(atomic_int * ptr, memory_order mo) {
  129. // TODO: add support for explicit memory order
  130. return InterlockedCompareExchange(ptr, 0, 0);
  131. }
  132. static LONG atomic_fetch_add(atomic_int * ptr, LONG inc) {
  133. return InterlockedExchangeAdd(ptr, inc);
  134. }
  135. static LONG atomic_fetch_add_explicit(atomic_int * ptr, LONG inc, memory_order mo) {
  136. // TODO: add support for explicit memory order
  137. return InterlockedExchangeAdd(ptr, inc);
  138. }
  139. static atomic_bool atomic_flag_test_and_set(atomic_flag * ptr) {
  140. return InterlockedExchange(ptr, 1);
  141. }
  142. static void atomic_flag_clear(atomic_flag * ptr) {
  143. InterlockedExchange(ptr, 0);
  144. }
  145. static void atomic_thread_fence(memory_order mo) {
  146. MemoryBarrier();
  147. }
  148. #else // clang
  149. #include <stdatomic.h>
  150. #endif
  151. typedef HANDLE pthread_t;
  152. typedef DWORD thread_ret_t;
  153. static int pthread_create(pthread_t * out, void * unused, thread_ret_t(*func)(void *), void * arg) {
  154. (void) unused;
  155. HANDLE handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL);
  156. if (handle == NULL)
  157. {
  158. return EAGAIN;
  159. }
  160. *out = handle;
  161. return 0;
  162. }
  163. static int pthread_join(pthread_t thread, void * unused) {
  164. (void) unused;
  165. int ret = (int) WaitForSingleObject(thread, INFINITE);
  166. CloseHandle(thread);
  167. return ret;
  168. }
  169. static int sched_yield (void) {
  170. Sleep (0);
  171. return 0;
  172. }
  173. #else
  174. #include <pthread.h>
  175. #include <stdatomic.h>
  176. #include <sched.h>
  177. #if defined(__FreeBSD__)
  178. #include <pthread_np.h>
  179. #endif
  180. typedef void * thread_ret_t;
  181. #include <sys/types.h>
  182. #include <sys/stat.h>
  183. #include <unistd.h>
  184. #endif
  185. typedef pthread_t ggml_thread_t;
  186. #if defined(__APPLE__)
  187. #include <unistd.h>
  188. #include <mach/mach.h>
  189. #include <TargetConditionals.h>
  190. #endif
  191. //
  192. // cache line
  193. //
  194. #if defined(__cpp_lib_hardware_interference_size)
  195. #define CACHE_LINE_SIZE hardware_destructive_interference_size
  196. #else
  197. #if defined(__POWER9_VECTOR__)
  198. #define CACHE_LINE_SIZE 128
  199. #else
  200. #define CACHE_LINE_SIZE 64
  201. #endif
  202. #endif
  203. static const size_t CACHE_LINE_SIZE_F32 = CACHE_LINE_SIZE/sizeof(float);
  204. static void ggml_vec_dot_f32(int n, float * restrict s, size_t bs, const float * restrict x, size_t bx, const float * restrict y, size_t by, int nrc);
  205. static void ggml_vec_dot_f16(int n, float * restrict s, size_t bs, ggml_fp16_t * restrict x, size_t bx, ggml_fp16_t * restrict y, size_t by, int nrc);
  206. static void ggml_vec_dot_bf16(int n, float * restrict s, size_t bs, ggml_bf16_t * restrict x, size_t bx, ggml_bf16_t * restrict y, size_t by, int nrc);
  207. static const struct ggml_type_traits_cpu type_traits_cpu[GGML_TYPE_COUNT] = {
  208. [GGML_TYPE_F32] = {
  209. .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f32,
  210. .vec_dot_type = GGML_TYPE_F32,
  211. .nrows = 1,
  212. },
  213. [GGML_TYPE_F16] = {
  214. .from_float = (ggml_from_float_t) ggml_fp32_to_fp16_row,
  215. .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f16,
  216. .vec_dot_type = GGML_TYPE_F16,
  217. .nrows = 1,
  218. },
  219. [GGML_TYPE_Q4_0] = {
  220. .from_float = quantize_row_q4_0,
  221. .vec_dot = ggml_vec_dot_q4_0_q8_0,
  222. .vec_dot_type = GGML_TYPE_Q8_0,
  223. #if defined (__ARM_FEATURE_MATMUL_INT8)
  224. .nrows = 2,
  225. #else
  226. .nrows = 1,
  227. #endif
  228. },
  229. [GGML_TYPE_Q4_1] = {
  230. .from_float = quantize_row_q4_1,
  231. .vec_dot = ggml_vec_dot_q4_1_q8_1,
  232. .vec_dot_type = GGML_TYPE_Q8_1,
  233. #if defined (__ARM_FEATURE_MATMUL_INT8)
  234. .nrows = 2,
  235. #else
  236. .nrows = 1,
  237. #endif
  238. },
  239. [GGML_TYPE_Q5_0] = {
  240. .from_float = quantize_row_q5_0,
  241. .vec_dot = ggml_vec_dot_q5_0_q8_0,
  242. .vec_dot_type = GGML_TYPE_Q8_0,
  243. .nrows = 1,
  244. },
  245. [GGML_TYPE_Q5_1] = {
  246. .from_float = quantize_row_q5_1,
  247. .vec_dot = ggml_vec_dot_q5_1_q8_1,
  248. .vec_dot_type = GGML_TYPE_Q8_1,
  249. .nrows = 1,
  250. },
  251. [GGML_TYPE_Q8_0] = {
  252. .from_float = quantize_row_q8_0,
  253. .vec_dot = ggml_vec_dot_q8_0_q8_0,
  254. .vec_dot_type = GGML_TYPE_Q8_0,
  255. #if defined (__ARM_FEATURE_MATMUL_INT8)
  256. .nrows = 2,
  257. #else
  258. .nrows = 1,
  259. #endif
  260. },
  261. [GGML_TYPE_Q8_1] = {
  262. .from_float = quantize_row_q8_1,
  263. .vec_dot_type = GGML_TYPE_Q8_1,
  264. .nrows = 1,
  265. },
  266. [GGML_TYPE_Q2_K] = {
  267. .from_float = quantize_row_q2_K,
  268. .vec_dot = ggml_vec_dot_q2_K_q8_K,
  269. .vec_dot_type = GGML_TYPE_Q8_K,
  270. .nrows = 1,
  271. },
  272. [GGML_TYPE_Q3_K] = {
  273. .from_float = quantize_row_q3_K,
  274. .vec_dot = ggml_vec_dot_q3_K_q8_K,
  275. .vec_dot_type = GGML_TYPE_Q8_K,
  276. .nrows = 1,
  277. },
  278. [GGML_TYPE_Q4_K] = {
  279. .from_float = quantize_row_q4_K,
  280. .vec_dot = ggml_vec_dot_q4_K_q8_K,
  281. .vec_dot_type = GGML_TYPE_Q8_K,
  282. .nrows = 1,
  283. },
  284. [GGML_TYPE_Q5_K] = {
  285. .from_float = quantize_row_q5_K,
  286. .vec_dot = ggml_vec_dot_q5_K_q8_K,
  287. .vec_dot_type = GGML_TYPE_Q8_K,
  288. .nrows = 1,
  289. },
  290. [GGML_TYPE_Q6_K] = {
  291. .from_float = quantize_row_q6_K,
  292. .vec_dot = ggml_vec_dot_q6_K_q8_K,
  293. .vec_dot_type = GGML_TYPE_Q8_K,
  294. .nrows = 1,
  295. },
  296. [GGML_TYPE_IQ2_XXS] = {
  297. .from_float = NULL,
  298. .vec_dot = ggml_vec_dot_iq2_xxs_q8_K,
  299. .vec_dot_type = GGML_TYPE_Q8_K,
  300. .nrows = 1,
  301. },
  302. [GGML_TYPE_IQ2_XS] = {
  303. .from_float = NULL,
  304. .vec_dot = ggml_vec_dot_iq2_xs_q8_K,
  305. .vec_dot_type = GGML_TYPE_Q8_K,
  306. .nrows = 1,
  307. },
  308. [GGML_TYPE_IQ3_XXS] = {
  309. // NOTE: from_float for iq3 and iq2_s was removed because these quants require initialization in ggml_quantize_init
  310. //.from_float = quantize_row_iq3_xxs,
  311. .vec_dot = ggml_vec_dot_iq3_xxs_q8_K,
  312. .vec_dot_type = GGML_TYPE_Q8_K,
  313. .nrows = 1,
  314. },
  315. [GGML_TYPE_IQ3_S] = {
  316. //.from_float = quantize_row_iq3_s,
  317. .vec_dot = ggml_vec_dot_iq3_s_q8_K,
  318. .vec_dot_type = GGML_TYPE_Q8_K,
  319. .nrows = 1,
  320. },
  321. [GGML_TYPE_IQ2_S] = {
  322. //.from_float = quantize_row_iq2_s,
  323. .vec_dot = ggml_vec_dot_iq2_s_q8_K,
  324. .vec_dot_type = GGML_TYPE_Q8_K,
  325. .nrows = 1,
  326. },
  327. [GGML_TYPE_IQ1_S] = {
  328. .from_float = NULL,
  329. .vec_dot = ggml_vec_dot_iq1_s_q8_K,
  330. .vec_dot_type = GGML_TYPE_Q8_K,
  331. .nrows = 1,
  332. },
  333. [GGML_TYPE_IQ1_M] = {
  334. .from_float = NULL,
  335. .vec_dot = ggml_vec_dot_iq1_m_q8_K,
  336. .vec_dot_type = GGML_TYPE_Q8_K,
  337. .nrows = 1,
  338. },
  339. [GGML_TYPE_IQ4_NL] = {
  340. .from_float = quantize_row_iq4_nl,
  341. .vec_dot = ggml_vec_dot_iq4_nl_q8_0,
  342. .vec_dot_type = GGML_TYPE_Q8_0,
  343. .nrows = 1,
  344. },
  345. [GGML_TYPE_IQ4_XS] = {
  346. .from_float = quantize_row_iq4_xs,
  347. .vec_dot = ggml_vec_dot_iq4_xs_q8_K,
  348. .vec_dot_type = GGML_TYPE_Q8_K,
  349. .nrows = 1,
  350. },
  351. [GGML_TYPE_Q8_K] = {
  352. .from_float = quantize_row_q8_K,
  353. },
  354. [GGML_TYPE_BF16] = {
  355. .from_float = (ggml_from_float_t) ggml_fp32_to_bf16_row,
  356. .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_bf16,
  357. .vec_dot_type = GGML_TYPE_BF16,
  358. .nrows = 1,
  359. },
  360. [GGML_TYPE_TQ1_0] = {
  361. .from_float = quantize_row_tq1_0,
  362. .vec_dot = ggml_vec_dot_tq1_0_q8_K,
  363. .vec_dot_type = GGML_TYPE_Q8_K,
  364. .nrows = 1,
  365. },
  366. [GGML_TYPE_TQ2_0] = {
  367. .from_float = quantize_row_tq2_0,
  368. .vec_dot = ggml_vec_dot_tq2_0_q8_K,
  369. .vec_dot_type = GGML_TYPE_Q8_K,
  370. .nrows = 1,
  371. },
  372. };
  373. const struct ggml_type_traits_cpu * ggml_get_type_traits_cpu(enum ggml_type type) {
  374. return &type_traits_cpu[type];
  375. }
  376. //
  377. // simd mappings
  378. //
  379. // we define a common set of C macros which map to specific intrinsics based on the current architecture
  380. // we then implement the fundamental computation operations below using only these macros
  381. // adding support for new architectures requires to define the corresponding SIMD macros
  382. //
  383. // GGML_F32_STEP / GGML_F16_STEP
  384. // number of elements to process in a single step
  385. //
  386. // GGML_F32_EPR / GGML_F16_EPR
  387. // number of elements to fit in a single register
  388. //
  389. #if defined(__ARM_NEON) && defined(__ARM_FEATURE_FMA)
  390. #define GGML_SIMD
  391. // F32 NEON
  392. #define GGML_F32_STEP 16
  393. #define GGML_F32_EPR 4
  394. #define GGML_F32x4 float32x4_t
  395. #define GGML_F32x4_ZERO vdupq_n_f32(0.0f)
  396. #define GGML_F32x4_SET1(x) vdupq_n_f32(x)
  397. #define GGML_F32x4_LOAD vld1q_f32
  398. #define GGML_F32x4_STORE vst1q_f32
  399. #define GGML_F32x4_FMA(a, b, c) vfmaq_f32(a, b, c)
  400. #define GGML_F32x4_ADD vaddq_f32
  401. #define GGML_F32x4_MUL vmulq_f32
  402. #define GGML_F32x4_REDUCE_ONE(x) vaddvq_f32(x)
  403. #define GGML_F32x4_REDUCE(res, x) \
  404. { \
  405. int offset = GGML_F32_ARR >> 1; \
  406. for (int i = 0; i < offset; ++i) { \
  407. (x)[i] = vaddq_f32((x)[i], (x)[offset+i]); \
  408. } \
  409. offset >>= 1; \
  410. for (int i = 0; i < offset; ++i) { \
  411. (x)[i] = vaddq_f32((x)[i], (x)[offset+i]); \
  412. } \
  413. offset >>= 1; \
  414. for (int i = 0; i < offset; ++i) { \
  415. (x)[i] = vaddq_f32((x)[i], (x)[offset+i]); \
  416. } \
  417. (res) = (ggml_float) GGML_F32x4_REDUCE_ONE((x)[0]); \
  418. }
  419. #define GGML_F32_VEC GGML_F32x4
  420. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  421. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  422. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  423. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  424. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  425. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  426. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  427. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  428. // F16 NEON
  429. #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
  430. #define GGML_F16_STEP 32
  431. #define GGML_F16_EPR 8
  432. #define GGML_F16x8 float16x8_t
  433. #define GGML_F16x8_ZERO vdupq_n_f16(0.0f)
  434. #define GGML_F16x8_SET1(x) vdupq_n_f16(x)
  435. #define GGML_F16x8_LOAD(x) vld1q_f16((const ggml_fp16_internal_t *)(x))
  436. #define GGML_F16x8_STORE vst1q_f16
  437. #define GGML_F16x8_FMA(a, b, c) vfmaq_f16(a, b, c)
  438. #define GGML_F16x8_ADD vaddq_f16
  439. #define GGML_F16x8_MUL vmulq_f16
  440. #define GGML_F16x8_REDUCE(res, x) \
  441. do { \
  442. int offset = GGML_F16_ARR >> 1; \
  443. for (int i = 0; i < offset; ++i) { \
  444. (x)[i] = vaddq_f16((x)[i], (x)[offset+i]); \
  445. } \
  446. offset >>= 1; \
  447. for (int i = 0; i < offset; ++i) { \
  448. (x)[i] = vaddq_f16((x)[i], (x)[offset+i]); \
  449. } \
  450. offset >>= 1; \
  451. for (int i = 0; i < offset; ++i) { \
  452. (x)[i] = vaddq_f16((x)[i], (x)[offset+i]); \
  453. } \
  454. const float32x4_t t0 = vcvt_f32_f16(vget_low_f16 ((x)[0])); \
  455. const float32x4_t t1 = vcvt_f32_f16(vget_high_f16((x)[0])); \
  456. (res) = (ggml_float) vaddvq_f32(vaddq_f32(t0, t1)); \
  457. } while (0)
  458. #define GGML_F16_VEC GGML_F16x8
  459. #define GGML_F16_VEC_ZERO GGML_F16x8_ZERO
  460. #define GGML_F16_VEC_SET1 GGML_F16x8_SET1
  461. #define GGML_F16_VEC_LOAD(p, i) GGML_F16x8_LOAD(p)
  462. #define GGML_F16_VEC_STORE(p, r, i) GGML_F16x8_STORE((ggml_fp16_internal_t *)(p), (r)[i])
  463. #define GGML_F16_VEC_FMA GGML_F16x8_FMA
  464. #define GGML_F16_VEC_ADD GGML_F16x8_ADD
  465. #define GGML_F16_VEC_MUL GGML_F16x8_MUL
  466. #define GGML_F16_VEC_REDUCE GGML_F16x8_REDUCE
  467. #else
  468. // if FP16 vector arithmetic is not supported, we use FP32 instead
  469. // and take advantage of the vcvt_ functions to convert to/from FP16
  470. #define GGML_F16_STEP 16
  471. #define GGML_F16_EPR 4
  472. #define GGML_F32Cx4 float32x4_t
  473. #define GGML_F32Cx4_ZERO vdupq_n_f32(0.0f)
  474. #define GGML_F32Cx4_SET1(x) vdupq_n_f32(x)
  475. #define GGML_F32Cx4_LOAD(x) vcvt_f32_f16(vld1_f16((const ggml_fp16_internal_t *)(x)))
  476. #define GGML_F32Cx4_STORE(x, y) vst1_f16(x, vcvt_f16_f32(y))
  477. #define GGML_F32Cx4_FMA(a, b, c) vfmaq_f32(a, b, c)
  478. #define GGML_F32Cx4_ADD vaddq_f32
  479. #define GGML_F32Cx4_MUL vmulq_f32
  480. #define GGML_F32Cx4_REDUCE GGML_F32x4_REDUCE
  481. #define GGML_F16_VEC GGML_F32Cx4
  482. #define GGML_F16_VEC_ZERO GGML_F32Cx4_ZERO
  483. #define GGML_F16_VEC_SET1 GGML_F32Cx4_SET1
  484. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx4_LOAD(p)
  485. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx4_STORE((ggml_fp16_internal_t *)(p), r[i])
  486. #define GGML_F16_VEC_FMA GGML_F32Cx4_FMA
  487. #define GGML_F16_VEC_ADD GGML_F32Cx4_ADD
  488. #define GGML_F16_VEC_MUL GGML_F32Cx4_MUL
  489. #define GGML_F16_VEC_REDUCE GGML_F32Cx4_REDUCE
  490. #endif
  491. #elif defined(__AVX512F__)
  492. #define GGML_SIMD
  493. // F32 AVX512
  494. #define GGML_F32_STEP 64
  495. #define GGML_F32_EPR 16
  496. #define GGML_F32x16 __m512
  497. #define GGML_F32x16_ZERO _mm512_setzero_ps()
  498. #define GGML_F32x16_SET1(x) _mm512_set1_ps(x)
  499. #define GGML_F32x16_LOAD _mm512_loadu_ps
  500. #define GGML_F32x16_STORE _mm512_storeu_ps
  501. // _mm512_fmadd_ps is defined in AVX512F so no guard is required
  502. #define GGML_F32x16_FMA(a, b, c) _mm512_fmadd_ps(b, c, a)
  503. #define GGML_F32x16_ADD _mm512_add_ps
  504. #define GGML_F32x16_MUL _mm512_mul_ps
  505. #define GGML_F32x16_REDUCE(res, x) \
  506. do { \
  507. int offset = GGML_F32_ARR >> 1; \
  508. for (int i = 0; i < offset; ++i) { \
  509. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  510. } \
  511. offset >>= 1; \
  512. for (int i = 0; i < offset; ++i) { \
  513. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  514. } \
  515. offset >>= 1; \
  516. for (int i = 0; i < offset; ++i) { \
  517. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  518. } \
  519. res = (ggml_float) _mm512_reduce_add_ps(x[0]); \
  520. } while (0)
  521. // TODO: is this optimal ?
  522. #define GGML_F32_VEC GGML_F32x16
  523. #define GGML_F32_VEC_ZERO GGML_F32x16_ZERO
  524. #define GGML_F32_VEC_SET1 GGML_F32x16_SET1
  525. #define GGML_F32_VEC_LOAD GGML_F32x16_LOAD
  526. #define GGML_F32_VEC_STORE GGML_F32x16_STORE
  527. #define GGML_F32_VEC_FMA GGML_F32x16_FMA
  528. #define GGML_F32_VEC_ADD GGML_F32x16_ADD
  529. #define GGML_F32_VEC_MUL GGML_F32x16_MUL
  530. #define GGML_F32_VEC_REDUCE GGML_F32x16_REDUCE
  531. // F16 AVX512
  532. // F16 AVX
  533. #define GGML_F16_STEP 64
  534. #define GGML_F16_EPR 16
  535. // AVX512 has FP16 extension (AVX512_FP16) but I don't have it on my machine so I use FP32 instead
  536. #define GGML_F32Cx16 __m512
  537. #define GGML_F32Cx16_ZERO _mm512_setzero_ps()
  538. #define GGML_F32Cx16_SET1(x) _mm512_set1_ps(x)
  539. // unlike _mm256_cvt intrinsics that require F16C, _mm512_cvt is defined in AVX512F
  540. // so F16C guard isn't required
  541. #define GGML_F32Cx16_LOAD(x) _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(x)))
  542. #define GGML_F32Cx16_STORE(x, y) _mm256_storeu_si256((__m256i *)(x), _mm512_cvtps_ph(y, 0))
  543. #define GGML_F32Cx16_FMA(a, b, c) _mm512_fmadd_ps(b, c, a)
  544. #define GGML_F32Cx16_ADD _mm512_add_ps
  545. #define GGML_F32Cx16_MUL _mm512_mul_ps
  546. #define GGML_F32Cx16_REDUCE(res, x) \
  547. do { \
  548. int offset = GGML_F32_ARR >> 1; \
  549. for (int i = 0; i < offset; ++i) { \
  550. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  551. } \
  552. offset >>= 1; \
  553. for (int i = 0; i < offset; ++i) { \
  554. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  555. } \
  556. offset >>= 1; \
  557. for (int i = 0; i < offset; ++i) { \
  558. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  559. } \
  560. res = (ggml_float) _mm512_reduce_add_ps(x[0]); \
  561. } while (0)
  562. #define GGML_F16_VEC GGML_F32Cx16
  563. #define GGML_F16_VEC_ZERO GGML_F32Cx16_ZERO
  564. #define GGML_F16_VEC_SET1 GGML_F32Cx16_SET1
  565. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx16_LOAD(p)
  566. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx16_STORE(p, r[i])
  567. #define GGML_F16_VEC_FMA GGML_F32Cx16_FMA
  568. #define GGML_F16_VEC_ADD GGML_F32Cx16_ADD
  569. #define GGML_F16_VEC_MUL GGML_F32Cx16_MUL
  570. #define GGML_F16_VEC_REDUCE GGML_F32Cx16_REDUCE
  571. #elif defined(__AVX__)
  572. #define GGML_SIMD
  573. // F32 AVX
  574. #define GGML_F32_STEP 32
  575. #define GGML_F32_EPR 8
  576. #define GGML_F32x8 __m256
  577. #define GGML_F32x8_ZERO _mm256_setzero_ps()
  578. #define GGML_F32x8_SET1(x) _mm256_set1_ps(x)
  579. #define GGML_F32x8_LOAD _mm256_loadu_ps
  580. #define GGML_F32x8_STORE _mm256_storeu_ps
  581. #if defined(__FMA__)
  582. #define GGML_F32x8_FMA(a, b, c) _mm256_fmadd_ps(b, c, a)
  583. #else
  584. #define GGML_F32x8_FMA(a, b, c) _mm256_add_ps(_mm256_mul_ps(b, c), a)
  585. #endif
  586. #define GGML_F32x8_ADD _mm256_add_ps
  587. #define GGML_F32x8_MUL _mm256_mul_ps
  588. #define GGML_F32x8_REDUCE(res, x) \
  589. do { \
  590. int offset = GGML_F32_ARR >> 1; \
  591. for (int i = 0; i < offset; ++i) { \
  592. x[i] = _mm256_add_ps(x[i], x[offset+i]); \
  593. } \
  594. offset >>= 1; \
  595. for (int i = 0; i < offset; ++i) { \
  596. x[i] = _mm256_add_ps(x[i], x[offset+i]); \
  597. } \
  598. offset >>= 1; \
  599. for (int i = 0; i < offset; ++i) { \
  600. x[i] = _mm256_add_ps(x[i], x[offset+i]); \
  601. } \
  602. const __m128 t0 = _mm_add_ps(_mm256_castps256_ps128(x[0]), \
  603. _mm256_extractf128_ps(x[0], 1)); \
  604. const __m128 t1 = _mm_hadd_ps(t0, t0); \
  605. res = (ggml_float) _mm_cvtss_f32(_mm_hadd_ps(t1, t1)); \
  606. } while (0)
  607. // TODO: is this optimal ?
  608. #define GGML_F32_VEC GGML_F32x8
  609. #define GGML_F32_VEC_ZERO GGML_F32x8_ZERO
  610. #define GGML_F32_VEC_SET1 GGML_F32x8_SET1
  611. #define GGML_F32_VEC_LOAD GGML_F32x8_LOAD
  612. #define GGML_F32_VEC_STORE GGML_F32x8_STORE
  613. #define GGML_F32_VEC_FMA GGML_F32x8_FMA
  614. #define GGML_F32_VEC_ADD GGML_F32x8_ADD
  615. #define GGML_F32_VEC_MUL GGML_F32x8_MUL
  616. #define GGML_F32_VEC_REDUCE GGML_F32x8_REDUCE
  617. // F16 AVX
  618. #define GGML_F16_STEP 32
  619. #define GGML_F16_EPR 8
  620. // F16 arithmetic is not supported by AVX, so we use F32 instead
  621. #define GGML_F32Cx8 __m256
  622. #define GGML_F32Cx8_ZERO _mm256_setzero_ps()
  623. #define GGML_F32Cx8_SET1(x) _mm256_set1_ps(x)
  624. #if defined(__F16C__)
  625. // the _mm256_cvt intrinsics require F16C
  626. #define GGML_F32Cx8_LOAD(x) _mm256_cvtph_ps(_mm_loadu_si128((const __m128i *)(x)))
  627. #define GGML_F32Cx8_STORE(x, y) _mm_storeu_si128((__m128i *)(x), _mm256_cvtps_ph(y, 0))
  628. #else
  629. static inline __m256 __avx_f32cx8_load(const ggml_fp16_t * x) {
  630. float tmp[8];
  631. for (int i = 0; i < 8; i++) {
  632. tmp[i] = GGML_FP16_TO_FP32(x[i]);
  633. }
  634. return _mm256_loadu_ps(tmp);
  635. }
  636. static inline void __avx_f32cx8_store(ggml_fp16_t *x, __m256 y) {
  637. float arr[8];
  638. _mm256_storeu_ps(arr, y);
  639. for (int i = 0; i < 8; i++)
  640. x[i] = GGML_FP32_TO_FP16(arr[i]);
  641. }
  642. #define GGML_F32Cx8_LOAD(x) __avx_f32cx8_load(x)
  643. #define GGML_F32Cx8_STORE(x, y) __avx_f32cx8_store(x, y)
  644. #endif
  645. #define GGML_F32Cx8_FMA GGML_F32x8_FMA
  646. #define GGML_F32Cx8_ADD _mm256_add_ps
  647. #define GGML_F32Cx8_MUL _mm256_mul_ps
  648. #define GGML_F32Cx8_REDUCE GGML_F32x8_REDUCE
  649. #define GGML_F16_VEC GGML_F32Cx8
  650. #define GGML_F16_VEC_ZERO GGML_F32Cx8_ZERO
  651. #define GGML_F16_VEC_SET1 GGML_F32Cx8_SET1
  652. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx8_LOAD(p)
  653. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx8_STORE(p, r[i])
  654. #define GGML_F16_VEC_FMA GGML_F32Cx8_FMA
  655. #define GGML_F16_VEC_ADD GGML_F32Cx8_ADD
  656. #define GGML_F16_VEC_MUL GGML_F32Cx8_MUL
  657. #define GGML_F16_VEC_REDUCE GGML_F32Cx8_REDUCE
  658. #elif defined(__POWER9_VECTOR__)
  659. #define GGML_SIMD
  660. // F32 POWER9
  661. #define GGML_F32_STEP 32
  662. #define GGML_F32_EPR 4
  663. #define GGML_F32x4 vector float
  664. #define GGML_F32x4_ZERO 0.0f
  665. #define GGML_F32x4_SET1 vec_splats
  666. #define GGML_F32x4_LOAD(p) vec_xl(0, p)
  667. #define GGML_F32x4_STORE(p, r) vec_xst(r, 0, p)
  668. #define GGML_F32x4_FMA(a, b, c) vec_madd(b, c, a)
  669. #define GGML_F32x4_ADD vec_add
  670. #define GGML_F32x4_MUL vec_mul
  671. #define GGML_F32x4_REDUCE(res, x) \
  672. { \
  673. int offset = GGML_F32_ARR >> 1; \
  674. for (int i = 0; i < offset; ++i) { \
  675. x[i] = vec_add(x[i], x[offset+i]); \
  676. } \
  677. offset >>= 1; \
  678. for (int i = 0; i < offset; ++i) { \
  679. x[i] = vec_add(x[i], x[offset+i]); \
  680. } \
  681. offset >>= 1; \
  682. for (int i = 0; i < offset; ++i) { \
  683. x[i] = vec_add(x[i], x[offset+i]); \
  684. } \
  685. res = vec_extract(x[0], 0) + \
  686. vec_extract(x[0], 1) + \
  687. vec_extract(x[0], 2) + \
  688. vec_extract(x[0], 3); \
  689. }
  690. #define GGML_F32_VEC GGML_F32x4
  691. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  692. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  693. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  694. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  695. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  696. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  697. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  698. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  699. // F16 POWER9
  700. #define GGML_F16_STEP GGML_F32_STEP
  701. #define GGML_F16_EPR GGML_F32_EPR
  702. #define GGML_F16_VEC GGML_F32x4
  703. #define GGML_F16_VEC_ZERO GGML_F32x4_ZERO
  704. #define GGML_F16_VEC_SET1 GGML_F32x4_SET1
  705. #define GGML_F16_VEC_FMA GGML_F32x4_FMA
  706. #define GGML_F16_VEC_ADD GGML_F32x4_ADD
  707. #define GGML_F16_VEC_MUL GGML_F32x4_MUL
  708. #define GGML_F16_VEC_REDUCE GGML_F32x4_REDUCE
  709. // Use vec_xl, not vec_ld, in case the load address is not aligned.
  710. #define GGML_F16_VEC_LOAD(p, i) (i & 0x1) ? \
  711. vec_extract_fp32_from_shorth(vec_xl(0, p - GGML_F16_EPR)) : \
  712. vec_extract_fp32_from_shortl(vec_xl(0, p))
  713. #define GGML_ENDIAN_BYTE(i) ((unsigned char *)&(uint16_t){1})[i]
  714. #define GGML_F16_VEC_STORE(p, r, i) \
  715. if (i & 0x1) \
  716. vec_xst(vec_pack_to_short_fp32(r[i - GGML_ENDIAN_BYTE(1)], \
  717. r[i - GGML_ENDIAN_BYTE(0)]), \
  718. 0, p - GGML_F16_EPR)
  719. #elif defined(__wasm_simd128__)
  720. #define GGML_SIMD
  721. // F32 WASM
  722. #define GGML_F32_STEP 16
  723. #define GGML_F32_EPR 4
  724. #define GGML_F32x4 v128_t
  725. #define GGML_F32x4_ZERO wasm_f32x4_splat(0.0f)
  726. #define GGML_F32x4_SET1(x) wasm_f32x4_splat(x)
  727. #define GGML_F32x4_LOAD wasm_v128_load
  728. #define GGML_F32x4_STORE wasm_v128_store
  729. #define GGML_F32x4_FMA(a, b, c) wasm_f32x4_add(wasm_f32x4_mul(b, c), a)
  730. #define GGML_F32x4_ADD wasm_f32x4_add
  731. #define GGML_F32x4_MUL wasm_f32x4_mul
  732. #define GGML_F32x4_REDUCE(res, x) \
  733. { \
  734. int offset = GGML_F32_ARR >> 1; \
  735. for (int i = 0; i < offset; ++i) { \
  736. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  737. } \
  738. offset >>= 1; \
  739. for (int i = 0; i < offset; ++i) { \
  740. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  741. } \
  742. offset >>= 1; \
  743. for (int i = 0; i < offset; ++i) { \
  744. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  745. } \
  746. res = wasm_f32x4_extract_lane(x[0], 0) + \
  747. wasm_f32x4_extract_lane(x[0], 1) + \
  748. wasm_f32x4_extract_lane(x[0], 2) + \
  749. wasm_f32x4_extract_lane(x[0], 3); \
  750. }
  751. #define GGML_F32_VEC GGML_F32x4
  752. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  753. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  754. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  755. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  756. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  757. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  758. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  759. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  760. // F16 WASM
  761. #define GGML_F16_STEP 16
  762. #define GGML_F16_EPR 4
  763. inline static v128_t __wasm_f16x4_load(const ggml_fp16_t * p) {
  764. float tmp[4];
  765. tmp[0] = GGML_FP16_TO_FP32(p[0]);
  766. tmp[1] = GGML_FP16_TO_FP32(p[1]);
  767. tmp[2] = GGML_FP16_TO_FP32(p[2]);
  768. tmp[3] = GGML_FP16_TO_FP32(p[3]);
  769. return wasm_v128_load(tmp);
  770. }
  771. inline static void __wasm_f16x4_store(ggml_fp16_t * p, v128_t x) {
  772. float tmp[4];
  773. wasm_v128_store(tmp, x);
  774. p[0] = GGML_FP32_TO_FP16(tmp[0]);
  775. p[1] = GGML_FP32_TO_FP16(tmp[1]);
  776. p[2] = GGML_FP32_TO_FP16(tmp[2]);
  777. p[3] = GGML_FP32_TO_FP16(tmp[3]);
  778. }
  779. #define GGML_F16x4 v128_t
  780. #define GGML_F16x4_ZERO wasm_f32x4_splat(0.0f)
  781. #define GGML_F16x4_SET1(x) wasm_f32x4_splat(x)
  782. #define GGML_F16x4_LOAD(x) __wasm_f16x4_load(x)
  783. #define GGML_F16x4_STORE(x, y) __wasm_f16x4_store(x, y)
  784. #define GGML_F16x4_FMA GGML_F32x4_FMA
  785. #define GGML_F16x4_ADD wasm_f32x4_add
  786. #define GGML_F16x4_MUL wasm_f32x4_mul
  787. #define GGML_F16x4_REDUCE(res, x) \
  788. { \
  789. int offset = GGML_F16_ARR >> 1; \
  790. for (int i = 0; i < offset; ++i) { \
  791. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  792. } \
  793. offset >>= 1; \
  794. for (int i = 0; i < offset; ++i) { \
  795. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  796. } \
  797. offset >>= 1; \
  798. for (int i = 0; i < offset; ++i) { \
  799. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  800. } \
  801. res = wasm_f32x4_extract_lane(x[0], 0) + \
  802. wasm_f32x4_extract_lane(x[0], 1) + \
  803. wasm_f32x4_extract_lane(x[0], 2) + \
  804. wasm_f32x4_extract_lane(x[0], 3); \
  805. }
  806. #define GGML_F16_VEC GGML_F16x4
  807. #define GGML_F16_VEC_ZERO GGML_F16x4_ZERO
  808. #define GGML_F16_VEC_SET1 GGML_F16x4_SET1
  809. #define GGML_F16_VEC_LOAD(p, i) GGML_F16x4_LOAD(p)
  810. #define GGML_F16_VEC_STORE(p, r, i) GGML_F16x4_STORE(p, r[i])
  811. #define GGML_F16_VEC_FMA GGML_F16x4_FMA
  812. #define GGML_F16_VEC_ADD GGML_F16x4_ADD
  813. #define GGML_F16_VEC_MUL GGML_F16x4_MUL
  814. #define GGML_F16_VEC_REDUCE GGML_F16x4_REDUCE
  815. #elif defined(__SSE3__)
  816. #define GGML_SIMD
  817. // F32 SSE
  818. #define GGML_F32_STEP 32
  819. #define GGML_F32_EPR 4
  820. #define GGML_F32x4 __m128
  821. #define GGML_F32x4_ZERO _mm_setzero_ps()
  822. #define GGML_F32x4_SET1(x) _mm_set1_ps(x)
  823. #define GGML_F32x4_LOAD _mm_loadu_ps
  824. #define GGML_F32x4_STORE _mm_storeu_ps
  825. #if defined(__FMA__)
  826. // TODO: Does this work?
  827. #define GGML_F32x4_FMA(a, b, c) _mm_fmadd_ps(b, c, a)
  828. #else
  829. #define GGML_F32x4_FMA(a, b, c) _mm_add_ps(_mm_mul_ps(b, c), a)
  830. #endif
  831. #define GGML_F32x4_ADD _mm_add_ps
  832. #define GGML_F32x4_MUL _mm_mul_ps
  833. #define GGML_F32x4_REDUCE(res, x) \
  834. { \
  835. int offset = GGML_F32_ARR >> 1; \
  836. for (int i = 0; i < offset; ++i) { \
  837. x[i] = _mm_add_ps(x[i], x[offset+i]); \
  838. } \
  839. offset >>= 1; \
  840. for (int i = 0; i < offset; ++i) { \
  841. x[i] = _mm_add_ps(x[i], x[offset+i]); \
  842. } \
  843. offset >>= 1; \
  844. for (int i = 0; i < offset; ++i) { \
  845. x[i] = _mm_add_ps(x[i], x[offset+i]); \
  846. } \
  847. const __m128 t0 = _mm_hadd_ps(x[0], x[0]); \
  848. res = (ggml_float) _mm_cvtss_f32(_mm_hadd_ps(t0, t0)); \
  849. }
  850. // TODO: is this optimal ?
  851. #define GGML_F32_VEC GGML_F32x4
  852. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  853. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  854. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  855. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  856. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  857. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  858. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  859. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  860. // F16 SSE
  861. #define GGML_F16_STEP 32
  862. #define GGML_F16_EPR 4
  863. static inline __m128 __sse_f16x4_load(const ggml_fp16_t * x) {
  864. float tmp[4];
  865. tmp[0] = GGML_FP16_TO_FP32(x[0]);
  866. tmp[1] = GGML_FP16_TO_FP32(x[1]);
  867. tmp[2] = GGML_FP16_TO_FP32(x[2]);
  868. tmp[3] = GGML_FP16_TO_FP32(x[3]);
  869. return _mm_loadu_ps(tmp);
  870. }
  871. static inline void __sse_f16x4_store(ggml_fp16_t * x, __m128 y) {
  872. float arr[4];
  873. _mm_storeu_ps(arr, y);
  874. x[0] = GGML_FP32_TO_FP16(arr[0]);
  875. x[1] = GGML_FP32_TO_FP16(arr[1]);
  876. x[2] = GGML_FP32_TO_FP16(arr[2]);
  877. x[3] = GGML_FP32_TO_FP16(arr[3]);
  878. }
  879. #define GGML_F32Cx4 __m128
  880. #define GGML_F32Cx4_ZERO _mm_setzero_ps()
  881. #define GGML_F32Cx4_SET1(x) _mm_set1_ps(x)
  882. #define GGML_F32Cx4_LOAD(x) __sse_f16x4_load(x)
  883. #define GGML_F32Cx4_STORE(x, y) __sse_f16x4_store(x, y)
  884. #define GGML_F32Cx4_FMA GGML_F32x4_FMA
  885. #define GGML_F32Cx4_ADD _mm_add_ps
  886. #define GGML_F32Cx4_MUL _mm_mul_ps
  887. #define GGML_F32Cx4_REDUCE GGML_F32x4_REDUCE
  888. #define GGML_F16_VEC GGML_F32Cx4
  889. #define GGML_F16_VEC_ZERO GGML_F32Cx4_ZERO
  890. #define GGML_F16_VEC_SET1 GGML_F32Cx4_SET1
  891. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx4_LOAD(p)
  892. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx4_STORE(p, r[i])
  893. #define GGML_F16_VEC_FMA GGML_F32Cx4_FMA
  894. #define GGML_F16_VEC_ADD GGML_F32Cx4_ADD
  895. #define GGML_F16_VEC_MUL GGML_F32Cx4_MUL
  896. #define GGML_F16_VEC_REDUCE GGML_F32Cx4_REDUCE
  897. #elif defined(__loongarch_asx)
  898. #define GGML_SIMD
  899. // F32 LASX
  900. #define GGML_F32_STEP 32
  901. #define GGML_F32_EPR 8
  902. #define GGML_F32x8 __m256
  903. #define GGML_F32x8_ZERO (__m256)__lasx_xvldi(0)
  904. #define GGML_F32x8_SET1(x) (__m256)__lasx_xvreplfr2vr_s((x))
  905. #define GGML_F32x8_LOAD(x) (__m256)__lasx_xvld((x), 0)
  906. #define GGML_F32x8_STORE(x,y) __lasx_xvst((y), (x), 0)
  907. #define GGML_F32x8_FMA(a, b, c) __lasx_xvfmadd_s(b, c, a)
  908. #define GGML_F32x8_ADD __lasx_xvfadd_s
  909. #define GGML_F32x8_MUL __lasx_xvfmul_s
  910. #define GGML_F32x8_REDUCE(res, x) \
  911. do { \
  912. int offset = GGML_F32_ARR >> 1; \
  913. for (int i = 0; i < offset; ++i) { \
  914. x[i] = __lasx_xvfadd_s(x[i], x[offset+i]); \
  915. } \
  916. offset >>= 1; \
  917. for (int i = 0; i < offset; ++i) { \
  918. x[i] = __lasx_xvfadd_s(x[i], x[offset+i]); \
  919. } \
  920. offset >>= 1; \
  921. for (int i = 0; i < offset; ++i) { \
  922. x[i] = __lasx_xvfadd_s(x[i], x[offset+i]); \
  923. } \
  924. float *tmp_p = (float *)&x[0]; \
  925. res = tmp_p[0] + tmp_p[1] + tmp_p[2] + tmp_p[3] + tmp_p[4] + tmp_p[5] + tmp_p[6] + tmp_p[7]; \
  926. } while (0)
  927. // TODO: is this optimal ?
  928. #define GGML_F32_VEC GGML_F32x8
  929. #define GGML_F32_VEC_ZERO GGML_F32x8_ZERO
  930. #define GGML_F32_VEC_SET1 GGML_F32x8_SET1
  931. #define GGML_F32_VEC_LOAD GGML_F32x8_LOAD
  932. #define GGML_F32_VEC_STORE GGML_F32x8_STORE
  933. #define GGML_F32_VEC_FMA GGML_F32x8_FMA
  934. #define GGML_F32_VEC_ADD GGML_F32x8_ADD
  935. #define GGML_F32_VEC_MUL GGML_F32x8_MUL
  936. #define GGML_F32_VEC_REDUCE GGML_F32x8_REDUCE
  937. // F16 LASX
  938. #define GGML_F16_STEP 32
  939. #define GGML_F16_EPR 8
  940. // F16 arithmetic is not supported by AVX, so we use F32 instead
  941. #define GGML_F32Cx8 __m256
  942. #define GGML_F32Cx8_ZERO (__m256)__lasx_xvldi(0)
  943. #define GGML_F32Cx8_SET1(x) (__m256)__lasx_xvreplgr2vr_w((x))
  944. static inline __m256 __lasx_f32cx8_load(const ggml_fp16_t * x) {
  945. float tmp[8];
  946. for (int i = 0; i < 8; i++) {
  947. tmp[i] = GGML_FP16_TO_FP32(x[i]);
  948. }
  949. return (__m256)__lasx_xvld(tmp, 0);
  950. }
  951. static inline void __lasx_f32cx8_store(ggml_fp16_t * x, __m256 y) {
  952. float arr[8];
  953. __lasx_xvst(y, arr, 0);
  954. for (int i = 0; i < 8; i++) {
  955. x[i] = GGML_FP32_TO_FP16(arr[i]);
  956. }
  957. }
  958. #define GGML_F32Cx8_LOAD(x) __lasx_f32cx8_load(x)
  959. #define GGML_F32Cx8_STORE(x, y) __lasx_f32cx8_store(x, y)
  960. #define GGML_F32Cx8_FMA GGML_F32x8_FMA
  961. #define GGML_F32Cx8_ADD __lasx_xvfadd_s
  962. #define GGML_F32Cx8_MUL __lasx_xvfmul_s
  963. #define GGML_F32Cx8_REDUCE GGML_F32x8_REDUCE
  964. #define GGML_F16_VEC GGML_F32Cx8
  965. #define GGML_F16_VEC_ZERO GGML_F32Cx8_ZERO
  966. #define GGML_F16_VEC_SET1 GGML_F32Cx8_SET1
  967. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx8_LOAD(p)
  968. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx8_STORE(p, r[i])
  969. #define GGML_F16_VEC_FMA GGML_F32Cx8_FMA
  970. #define GGML_F16_VEC_ADD GGML_F32Cx8_ADD
  971. #define GGML_F16_VEC_MUL GGML_F32Cx8_MUL
  972. #define GGML_F16_VEC_REDUCE GGML_F32Cx8_REDUCE
  973. #elif defined(__loongarch_sx)
  974. #define GGML_SIMD
  975. // F32 LSX
  976. #define GGML_F32_STEP 32
  977. #define GGML_F32_EPR 4
  978. #define GGML_F32x4 __m128
  979. #define GGML_F32x4_ZERO __lsx_vldi(0)
  980. #define GGML_F32x4_SET1(x) __lsx_vinsgr2vr_w(__lsx_vldi(0),(x), 0)
  981. #define GGML_F32x4_LOAD(x) __lsx_vld((x), 0)
  982. #define GGML_F32x4_STORE((x),(y)) __lsx_vst((y), (x), 0)
  983. #define GGML_F32x4_FMA(a, b, c) __lsx_vfmadd_s(b, c, a)
  984. #define GGML_F32x4_ADD __lsx_vfadd_s
  985. #define GGML_F32x4_MUL __lsx_vfmul_s
  986. #define GGML_F32x4_REDUCE(res, x) \
  987. { \
  988. int offset = GGML_F32_ARR >> 1; \
  989. for (int i = 0; i < offset; ++i) { \
  990. x[i] = __lsx_vfadd_s(x[i], x[offset + i]); \
  991. } \
  992. offset >>= 1; \
  993. for (int i = 0; i < offset; ++i) { \
  994. x[i] = __lsx_vfadd_s(x[i], x[offset + i]); \
  995. } \
  996. offset >>= 1; \
  997. for (int i = 0; i < offset; ++i) { \
  998. x[i] = __lsx_vfadd_s(x[i], x[offset + i]); \
  999. } \
  1000. __m128i tmp = __lsx_vsrli_d((__m128i) x[0], 32); \
  1001. tmp = (__m128i) __lsx_vfadd_s((__m128) tmp, x[0]); \
  1002. tmp = __lsx_vpickev_w(__lsx_vldi(0), tmp); \
  1003. const __m128 t0 = __lsx_vshuf4i_w(tmp, 0x88); \
  1004. tmp = __lsx_vsrli_d((__m128i) t0, 32); \
  1005. tmp = (__m128i) __lsx_vfadd_s((__m128) tmp, t0); \
  1006. tmp = __lsx_vpickev_w(__lsx_vldi(0), tmp); \
  1007. res = (ggml_float) __lsx_vpickve2gr_w(__lsx_vshuf4i_w(tmp, 0x88), 0); \
  1008. }
  1009. #define GGML_F32_VEC GGML_F32x4
  1010. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  1011. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  1012. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  1013. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  1014. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  1015. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  1016. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  1017. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  1018. // F16 LSX
  1019. #define GGML_F16_STEP 32
  1020. #define GGML_F16_EPR 4
  1021. static inline __m128 __lsx_f16x4_load(const ggml_fp16_t * x) {
  1022. float tmp[4];
  1023. tmp[0] = GGML_FP16_TO_FP32(x[0]);
  1024. tmp[1] = GGML_FP16_TO_FP32(x[1]);
  1025. tmp[2] = GGML_FP16_TO_FP32(x[2]);
  1026. tmp[3] = GGML_FP16_TO_FP32(x[3]);
  1027. return __lsx_vld(tmp, 0);
  1028. }
  1029. static inline void __lsx_f16x4_store(ggml_fp16_t * x, __m128 y) {
  1030. float arr[4];
  1031. __lsx_vst(y, arr, 0);
  1032. x[0] = GGML_FP32_TO_FP16(arr[0]);
  1033. x[1] = GGML_FP32_TO_FP16(arr[1]);
  1034. x[2] = GGML_FP32_TO_FP16(arr[2]);
  1035. x[3] = GGML_FP32_TO_FP16(arr[3]);
  1036. }
  1037. #define GGML_F32Cx4 __m128
  1038. #define GGML_F32Cx4_ZERO __lsx_vldi(0)
  1039. #define GGML_F32Cx4_SET1(x) __lsx_vinsgr2vr_w(__lsx_vldi(0),(x), 0)
  1040. #define GGML_F32Cx4_LOAD(x) __lsx_f16x4_load(x)
  1041. #define GGML_F32Cx4_STORE(x, y) __lsx_f16x4_store(x, y)
  1042. #define GGML_F32Cx4_FMA GGML_F32x4_FMA
  1043. #define GGML_F32Cx4_ADD __lsx_vfadd_s
  1044. #define GGML_F32Cx4_MUL __lsx_vfmul_s
  1045. #define GGML_F32Cx4_REDUCE GGML_F32x4_REDUCE
  1046. #define GGML_F16_VEC GGML_F32Cx4
  1047. #define GGML_F16_VEC_ZERO GGML_F32Cx4_ZERO
  1048. #define GGML_F16_VEC_SET1 GGML_F32Cx4_SET1
  1049. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx4_LOAD(p)
  1050. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx4_STORE(p, r[i])
  1051. #define GGML_F16_VEC_FMA GGML_F32Cx4_FMA
  1052. #define GGML_F16_VEC_ADD GGML_F32Cx4_ADD
  1053. #define GGML_F16_VEC_MUL GGML_F32Cx4_MUL
  1054. #define GGML_F16_VEC_REDUCE GGML_F32Cx4_REDUCE
  1055. #endif
  1056. // GGML_F32_ARR / GGML_F16_ARR
  1057. // number of registers to use per step
  1058. #ifdef GGML_SIMD
  1059. #define GGML_F32_ARR (GGML_F32_STEP/GGML_F32_EPR)
  1060. #define GGML_F16_ARR (GGML_F16_STEP/GGML_F16_EPR)
  1061. #endif
  1062. //
  1063. // Threading defs
  1064. //
  1065. typedef pthread_t ggml_thread_t;
  1066. #if defined(_WIN32)
  1067. typedef CONDITION_VARIABLE ggml_cond_t;
  1068. typedef SRWLOCK ggml_mutex_t;
  1069. #define ggml_mutex_init(m) InitializeSRWLock(m)
  1070. #define ggml_mutex_destroy(m)
  1071. #define ggml_mutex_lock(m) AcquireSRWLockExclusive(m)
  1072. #define ggml_mutex_unlock(m) ReleaseSRWLockExclusive(m)
  1073. #define ggml_mutex_lock_shared(m) AcquireSRWLockShared(m)
  1074. #define ggml_mutex_unlock_shared(m) ReleaseSRWLockShared(m)
  1075. #define ggml_cond_init(c) InitializeConditionVariable(c)
  1076. #define ggml_cond_destroy(c)
  1077. #define ggml_cond_wait(c, m) SleepConditionVariableSRW(c, m, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED)
  1078. #define ggml_cond_broadcast(c) WakeAllConditionVariable(c)
  1079. #define ggml_thread_create pthread_create
  1080. #define ggml_thread_join pthread_join
  1081. #else
  1082. typedef pthread_cond_t ggml_cond_t;
  1083. typedef pthread_mutex_t ggml_mutex_t;
  1084. #define ggml_mutex_init(m) pthread_mutex_init(m, NULL)
  1085. #define ggml_mutex_destroy(m) pthread_mutex_destroy(m)
  1086. #define ggml_mutex_lock(m) pthread_mutex_lock(m)
  1087. #define ggml_mutex_unlock(m) pthread_mutex_unlock(m)
  1088. #define ggml_mutex_lock_shared(m) pthread_mutex_lock(m)
  1089. #define ggml_mutex_unlock_shared(m) pthread_mutex_unlock(m)
  1090. #define ggml_lock_init(x) UNUSED(x)
  1091. #define ggml_lock_destroy(x) UNUSED(x)
  1092. #if defined(__x86_64__) || (defined(_MSC_VER) && defined(_M_AMD64))
  1093. #define ggml_lock_lock(x) _mm_pause()
  1094. #else
  1095. #define ggml_lock_lock(x) UNUSED(x)
  1096. #endif
  1097. #define ggml_lock_unlock(x) UNUSED(x)
  1098. #define GGML_LOCK_INITIALIZER 0
  1099. #define ggml_cond_init(c) pthread_cond_init(c, NULL)
  1100. #define ggml_cond_destroy(c) pthread_cond_destroy(c)
  1101. #define ggml_cond_wait(c, m) pthread_cond_wait(c, m)
  1102. #define ggml_cond_broadcast(c) pthread_cond_broadcast(c)
  1103. #define ggml_thread_create pthread_create
  1104. #define ggml_thread_join pthread_join
  1105. #endif
  1106. // Threadpool def
  1107. struct ggml_threadpool {
  1108. ggml_mutex_t mutex; // mutex for cond.var
  1109. ggml_cond_t cond; // cond.var for waiting for new work
  1110. struct ggml_cgraph * cgraph;
  1111. struct ggml_cplan * cplan;
  1112. // synchronization primitives
  1113. atomic_int n_graph; // incremented when there is work to be done (i.e each graph)
  1114. atomic_int GGML_CACHE_ALIGN n_barrier;
  1115. atomic_int GGML_CACHE_ALIGN n_barrier_passed;
  1116. atomic_int current_chunk; // currently processing chunk during Mat_Mul, shared between all the threads.
  1117. // these are atomic as an annotation for thread-sanitizer
  1118. atomic_bool stop; // Used for stopping the threadpool altogether
  1119. atomic_bool pause; // Used for pausing the threadpool or individual threads
  1120. atomic_bool abort; // Used for aborting processing of a graph
  1121. struct ggml_compute_state * workers; // per thread state
  1122. int n_threads_max; // number of threads in the pool
  1123. atomic_int n_threads_cur; // number of threads used in the current graph
  1124. int32_t prio; // Scheduling priority
  1125. uint32_t poll; // Polling level (0 - no polling)
  1126. enum ggml_status ec;
  1127. };
  1128. // Per-thread state
  1129. struct ggml_compute_state {
  1130. #ifndef GGML_USE_OPENMP
  1131. ggml_thread_t thrd;
  1132. bool cpumask[GGML_MAX_N_THREADS];
  1133. int last_graph;
  1134. bool pending;
  1135. #endif
  1136. struct ggml_threadpool * threadpool;
  1137. int ith;
  1138. };
  1139. //
  1140. // fundamental operations
  1141. //
  1142. inline static void ggml_vec_set_i8(const int n, int8_t * x, const int8_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
  1143. inline static void ggml_vec_set_i16(const int n, int16_t * x, const int16_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
  1144. inline static void ggml_vec_set_i32(const int n, int32_t * x, const int32_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
  1145. inline static void ggml_vec_cpy_i32(const int n, int32_t * y, const int32_t * x) { for (int i = 0; i < n; ++i) y[i] = x[i]; }
  1146. inline static void ggml_vec_set_f16(const int n, ggml_fp16_t * x, const int32_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
  1147. inline static void ggml_vec_set_bf16(const int n, ggml_bf16_t * x, const ggml_bf16_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
  1148. inline static void ggml_vec_add_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i] + y[i]; }
  1149. inline static void ggml_vec_add1_f32(const int n, float * z, const float * x, const float v) { for (int i = 0; i < n; ++i) z[i] = x[i] + v; }
  1150. inline static void ggml_vec_acc_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] += x[i]; }
  1151. inline static void ggml_vec_acc1_f32(const int n, float * y, const float v) { for (int i = 0; i < n; ++i) y[i] += v; }
  1152. inline static void ggml_vec_sub_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i] - y[i]; }
  1153. inline static void ggml_vec_set_f32 (const int n, float * x, const float v) { for (int i = 0; i < n; ++i) x[i] = v; }
  1154. inline static void ggml_vec_cpy_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i]; }
  1155. inline static void ggml_vec_neg_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = -x[i]; }
  1156. inline static void ggml_vec_mul_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i]*y[i]; }
  1157. inline static void ggml_vec_div_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i]/y[i]; }
  1158. static void ggml_vec_dot_f32(int n, float * restrict s, size_t bs, const float * restrict x, size_t bx, const float * restrict y, size_t by, int nrc) {
  1159. assert(nrc == 1);
  1160. UNUSED(nrc);
  1161. UNUSED(bx);
  1162. UNUSED(by);
  1163. UNUSED(bs);
  1164. #if defined(GGML_SIMD)
  1165. float sumf = 0.0f;
  1166. const int np = (n & ~(GGML_F32_STEP - 1));
  1167. GGML_F32_VEC sum[GGML_F32_ARR] = { GGML_F32_VEC_ZERO };
  1168. GGML_F32_VEC ax[GGML_F32_ARR];
  1169. GGML_F32_VEC ay[GGML_F32_ARR];
  1170. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1171. for (int j = 0; j < GGML_F32_ARR; j++) {
  1172. ax[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
  1173. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1174. sum[j] = GGML_F32_VEC_FMA(sum[j], ax[j], ay[j]);
  1175. }
  1176. }
  1177. // reduce sum0..sum3 to sum0
  1178. GGML_F32_VEC_REDUCE(sumf, sum);
  1179. // leftovers
  1180. for (int i = np; i < n; ++i) {
  1181. sumf += x[i]*y[i];
  1182. }
  1183. #else
  1184. // scalar
  1185. ggml_float sumf = 0.0;
  1186. for (int i = 0; i < n; ++i) {
  1187. sumf += (ggml_float)(x[i]*y[i]);
  1188. }
  1189. #endif
  1190. *s = sumf;
  1191. }
  1192. static void ggml_vec_dot_bf16(int n, float * restrict s, size_t bs, ggml_bf16_t * restrict x, size_t bx, ggml_bf16_t * restrict y, size_t by, int nrc) {
  1193. assert(nrc == 1);
  1194. UNUSED(nrc);
  1195. UNUSED(bx);
  1196. UNUSED(by);
  1197. UNUSED(bs);
  1198. int i = 0;
  1199. ggml_float sumf = 0;
  1200. #if defined(__AVX512BF16__)
  1201. __m512 c1 = _mm512_setzero_ps();
  1202. __m512 c2 = _mm512_setzero_ps();
  1203. for (; i + 64 <= n; i += 64) {
  1204. c1 = _mm512_dpbf16_ps(c1, m512bh(_mm512_loadu_si512((x + i))),
  1205. m512bh(_mm512_loadu_si512((y + i))));
  1206. c2 = _mm512_dpbf16_ps(c2, m512bh(_mm512_loadu_si512((x + i + 32))),
  1207. m512bh(_mm512_loadu_si512((y + i + 32))));
  1208. }
  1209. sumf += (ggml_float)_mm512_reduce_add_ps(c1);
  1210. sumf += (ggml_float)_mm512_reduce_add_ps(c2);
  1211. #elif defined(__AVX512F__)
  1212. #define LOAD(p) _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(_mm256_loadu_si256((const __m256i *)(p))), 16))
  1213. __m512 c1 = _mm512_setzero_ps();
  1214. __m512 c2 = _mm512_setzero_ps();
  1215. for (; i + 32 <= n; i += 32) {
  1216. c1 = _mm512_add_ps(_mm512_mul_ps(LOAD(x + i), LOAD(y + i)), c1);
  1217. c2 = _mm512_add_ps(_mm512_mul_ps(LOAD(x + i + 16), LOAD(y + i + 16)), c2);
  1218. }
  1219. sumf += (ggml_float)_mm512_reduce_add_ps(c1);
  1220. sumf += (ggml_float)_mm512_reduce_add_ps(c2);
  1221. #undef LOAD
  1222. #elif defined(__AVX2__) || defined(__AVX__)
  1223. #if defined(__AVX2__)
  1224. #define LOAD(p) _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i *)(p))), 16))
  1225. #else
  1226. #define LOAD(p) _mm256_castsi256_ps(_mm256_insertf128_si256(_mm256_castsi128_si256(_mm_slli_epi32(_mm_cvtepu16_epi32(_mm_loadu_si128((const __m128i *)(p))), 16)), (_mm_slli_epi32(_mm_cvtepu16_epi32(_mm_bsrli_si128(_mm_loadu_si128((const __m128i *)(p)), 8)), 16)), 1))
  1227. #endif
  1228. __m256 c1 = _mm256_setzero_ps();
  1229. __m256 c2 = _mm256_setzero_ps();
  1230. __m256 c3 = _mm256_setzero_ps();
  1231. __m256 c4 = _mm256_setzero_ps();
  1232. for (; i + 32 <= n; i += 32) {
  1233. c1 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i), LOAD(y + i)), c1);
  1234. c2 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 8), LOAD(y + i + 8)), c2);
  1235. c3 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 16), LOAD(y + i + 16)), c3);
  1236. c4 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 24), LOAD(y + i + 24)), c4);
  1237. }
  1238. __m128 g;
  1239. c1 = _mm256_add_ps(_mm256_add_ps(c1, c3),
  1240. _mm256_add_ps(c2, c4));
  1241. g = _mm_add_ps(_mm256_extractf128_ps(c1, 1),
  1242. _mm256_castps256_ps128(c1));
  1243. g = _mm_add_ps(g, _mm_movehl_ps(g, g));
  1244. g = _mm_add_ss(g, _mm_movehdup_ps(g));
  1245. sumf += (ggml_float)_mm_cvtss_f32(g);
  1246. #undef LOAD
  1247. #endif
  1248. for (; i < n; ++i) {
  1249. sumf += (ggml_float)(GGML_BF16_TO_FP32(x[i]) *
  1250. GGML_BF16_TO_FP32(y[i]));
  1251. }
  1252. *s = sumf;
  1253. }
  1254. static void ggml_vec_dot_f16(int n, float * restrict s, size_t bs, ggml_fp16_t * restrict x, size_t bx, ggml_fp16_t * restrict y, size_t by, int nrc) {
  1255. assert(nrc == 1);
  1256. UNUSED(nrc);
  1257. UNUSED(bx);
  1258. UNUSED(by);
  1259. UNUSED(bs);
  1260. ggml_float sumf = 0.0;
  1261. #if defined(GGML_SIMD)
  1262. const int np = (n & ~(GGML_F16_STEP - 1));
  1263. GGML_F16_VEC sum[GGML_F16_ARR] = { GGML_F16_VEC_ZERO };
  1264. GGML_F16_VEC ax[GGML_F16_ARR];
  1265. GGML_F16_VEC ay[GGML_F16_ARR];
  1266. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1267. for (int j = 0; j < GGML_F16_ARR; j++) {
  1268. ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j);
  1269. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1270. sum[j] = GGML_F16_VEC_FMA(sum[j], ax[j], ay[j]);
  1271. }
  1272. }
  1273. // reduce sum0..sum3 to sum0
  1274. GGML_F16_VEC_REDUCE(sumf, sum);
  1275. // leftovers
  1276. for (int i = np; i < n; ++i) {
  1277. sumf += (ggml_float)(GGML_FP16_TO_FP32(x[i])*GGML_FP16_TO_FP32(y[i]));
  1278. }
  1279. #else
  1280. for (int i = 0; i < n; ++i) {
  1281. sumf += (ggml_float)(GGML_FP16_TO_FP32(x[i])*GGML_FP16_TO_FP32(y[i]));
  1282. }
  1283. #endif
  1284. *s = sumf;
  1285. }
  1286. // compute GGML_VEC_DOT_UNROLL dot products at once
  1287. // xs - x row stride in bytes
  1288. inline static void ggml_vec_dot_f16_unroll(const int n, const int xs, float * restrict s, void * restrict xv, ggml_fp16_t * restrict y) {
  1289. ggml_float sumf[GGML_VEC_DOT_UNROLL] = { 0.0 };
  1290. ggml_fp16_t * restrict x[GGML_VEC_DOT_UNROLL];
  1291. for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
  1292. x[i] = (ggml_fp16_t *) ((char *) xv + i*xs);
  1293. }
  1294. #if defined(GGML_SIMD)
  1295. const int np = (n & ~(GGML_F16_STEP - 1));
  1296. GGML_F16_VEC sum[GGML_VEC_DOT_UNROLL][GGML_F16_ARR] = { { GGML_F16_VEC_ZERO } };
  1297. GGML_F16_VEC ax[GGML_F16_ARR];
  1298. GGML_F16_VEC ay[GGML_F16_ARR];
  1299. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1300. for (int j = 0; j < GGML_F16_ARR; j++) {
  1301. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1302. for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
  1303. ax[j] = GGML_F16_VEC_LOAD(x[k] + i + j*GGML_F16_EPR, j);
  1304. sum[k][j] = GGML_F16_VEC_FMA(sum[k][j], ax[j], ay[j]);
  1305. }
  1306. }
  1307. }
  1308. // reduce sum0..sum3 to sum0
  1309. for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
  1310. GGML_F16_VEC_REDUCE(sumf[k], sum[k]);
  1311. }
  1312. // leftovers
  1313. for (int i = np; i < n; ++i) {
  1314. for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
  1315. sumf[j] += (ggml_float)(GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]));
  1316. }
  1317. }
  1318. #else
  1319. for (int i = 0; i < n; ++i) {
  1320. for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
  1321. sumf[j] += (ggml_float)(GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]));
  1322. }
  1323. }
  1324. #endif
  1325. for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
  1326. s[i] = sumf[i];
  1327. }
  1328. }
  1329. inline static void ggml_vec_mad_f32(const int n, float * restrict y, const float * restrict x, const float v) {
  1330. #if defined(GGML_SIMD)
  1331. const int np = (n & ~(GGML_F32_STEP - 1));
  1332. GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
  1333. GGML_F32_VEC ax[GGML_F32_ARR];
  1334. GGML_F32_VEC ay[GGML_F32_ARR];
  1335. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1336. for (int j = 0; j < GGML_F32_ARR; j++) {
  1337. ax[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
  1338. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1339. ay[j] = GGML_F32_VEC_FMA(ay[j], ax[j], vx);
  1340. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  1341. }
  1342. }
  1343. // leftovers
  1344. for (int i = np; i < n; ++i) {
  1345. y[i] += x[i]*v;
  1346. }
  1347. #else
  1348. // scalar
  1349. for (int i = 0; i < n; ++i) {
  1350. y[i] += x[i]*v;
  1351. }
  1352. #endif
  1353. }
  1354. inline static void ggml_vec_mad_f16(const int n, ggml_fp16_t * restrict y, const ggml_fp16_t * restrict x, const float v) {
  1355. #if defined(GGML_SIMD)
  1356. const int np = (n & ~(GGML_F16_STEP - 1));
  1357. GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
  1358. GGML_F16_VEC ax[GGML_F16_ARR];
  1359. GGML_F16_VEC ay[GGML_F16_ARR];
  1360. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1361. for (int j = 0; j < GGML_F16_ARR; j++) {
  1362. ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j);
  1363. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1364. ay[j] = GGML_F16_VEC_FMA(ay[j], ax[j], vx);
  1365. GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
  1366. }
  1367. }
  1368. // leftovers
  1369. for (int i = np; i < n; ++i) {
  1370. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i]) + GGML_FP16_TO_FP32(x[i])*v);
  1371. }
  1372. #else
  1373. // scalar
  1374. for (int i = 0; i < n; ++i) {
  1375. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i]) + GGML_FP16_TO_FP32(x[i])*v);
  1376. }
  1377. #endif
  1378. }
  1379. // xs and vs are byte strides of x and v
  1380. inline static void ggml_vec_mad_f32_unroll(const int n, const int xs, const int vs, float * restrict y, const float * restrict xv, const float * restrict vv) {
  1381. const float * restrict x[GGML_VEC_MAD_UNROLL];
  1382. const float * restrict v[GGML_VEC_MAD_UNROLL];
  1383. for (int i = 0; i < GGML_VEC_MAD_UNROLL; ++i) {
  1384. x[i] = (const float *) ((const char *) xv + i*xs);
  1385. v[i] = (const float *) ((const char *) vv + i*vs);
  1386. }
  1387. #if defined(GGML_SIMD)
  1388. const int np = (n & ~(GGML_F32_STEP - 1));
  1389. GGML_F32_VEC vx[GGML_VEC_MAD_UNROLL];
  1390. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1391. vx[k] = GGML_F32_VEC_SET1(v[k][0]);
  1392. }
  1393. GGML_F32_VEC ax[GGML_VEC_MAD_UNROLL][GGML_F32_ARR];
  1394. GGML_F32_VEC ay[GGML_F32_ARR];
  1395. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1396. for (int j = 0; j < GGML_F32_ARR; j++) {
  1397. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1398. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1399. ax[k][j] = GGML_F32_VEC_LOAD(x[k] + i + j*GGML_F32_EPR);
  1400. ay[j] = GGML_F32_VEC_FMA(ay[j], ax[k][j], vx[k]);
  1401. }
  1402. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  1403. }
  1404. }
  1405. // leftovers
  1406. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1407. for (int i = np; i < n; ++i) {
  1408. y[i] += x[k][i]*v[k][0];
  1409. }
  1410. }
  1411. #else
  1412. // scalar
  1413. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1414. for (int i = 0; i < n; ++i) {
  1415. y[i] += x[k][i]*v[k][0];
  1416. }
  1417. }
  1418. #endif
  1419. }
  1420. //inline static void ggml_vec_scale_f32(const int n, float * y, const float v) { for (int i = 0; i < n; ++i) y[i] *= v; }
  1421. inline static void ggml_vec_scale_f32(const int n, float * y, const float v) {
  1422. #if defined(GGML_USE_ACCELERATE)
  1423. vDSP_vsmul(y, 1, &v, y, 1, n);
  1424. #elif defined(GGML_SIMD)
  1425. const int np = (n & ~(GGML_F32_STEP - 1));
  1426. GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
  1427. GGML_F32_VEC ay[GGML_F32_ARR];
  1428. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1429. for (int j = 0; j < GGML_F32_ARR; j++) {
  1430. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1431. ay[j] = GGML_F32_VEC_MUL(ay[j], vx);
  1432. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  1433. }
  1434. }
  1435. // leftovers
  1436. for (int i = np; i < n; ++i) {
  1437. y[i] *= v;
  1438. }
  1439. #else
  1440. // scalar
  1441. for (int i = 0; i < n; ++i) {
  1442. y[i] *= v;
  1443. }
  1444. #endif
  1445. }
  1446. inline static void ggml_vec_scale_f16(const int n, ggml_fp16_t * y, const float v) {
  1447. #if defined(GGML_SIMD)
  1448. const int np = (n & ~(GGML_F16_STEP - 1));
  1449. GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
  1450. GGML_F16_VEC ay[GGML_F16_ARR];
  1451. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1452. for (int j = 0; j < GGML_F16_ARR; j++) {
  1453. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1454. ay[j] = GGML_F16_VEC_MUL(ay[j], vx);
  1455. GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
  1456. }
  1457. }
  1458. // leftovers
  1459. for (int i = np; i < n; ++i) {
  1460. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i])*v);
  1461. }
  1462. #else
  1463. // scalar
  1464. for (int i = 0; i < n; ++i) {
  1465. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i])*v);
  1466. }
  1467. #endif
  1468. }
  1469. inline static void ggml_vec_norm_f32 (const int n, float * s, const float * x) { ggml_vec_dot_f32(n, s, 0, x, 0, x, 0, 1); *s = sqrtf(*s); }
  1470. inline static void ggml_vec_sqr_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i]*x[i]; }
  1471. inline static void ggml_vec_sqrt_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = sqrtf(x[i]); }
  1472. inline static void ggml_vec_log_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = logf(x[i]); }
  1473. inline static void ggml_vec_sin_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = sinf(x[i]); }
  1474. inline static void ggml_vec_cos_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = cosf(x[i]); }
  1475. inline static void ggml_vec_abs_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = fabsf(x[i]); }
  1476. inline static void ggml_vec_sgn_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? 1.f : ((x[i] < 0.f) ? -1.f : 0.f); }
  1477. inline static void ggml_vec_step_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? 1.f : 0.f; }
  1478. inline static void ggml_vec_tanh_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = tanhf(x[i]); }
  1479. inline static void ggml_vec_elu_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? x[i] : expm1f(x[i]); }
  1480. inline static void ggml_vec_relu_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? x[i] : 0.f; }
  1481. inline static void ggml_vec_leaky_relu_f32 (const int n, float * y, const float * x, const float ns) { for (int i = 0; i < n; ++i) y[i] = ((x[i] > 0.f) ? x[i] : 0.f) + ns * ((x[i] < 0.0f) ? x[i] : 0.f); }
  1482. inline static void ggml_vec_sigmoid_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = 1.f / (1.f + expf(-x[i])); }
  1483. // TODO: optimize performance
  1484. inline static void ggml_vec_hardswish_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f)); }
  1485. inline static void ggml_vec_hardsigmoid_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f)); }
  1486. inline static void ggml_vec_exp_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = expf(x[i]); }
  1487. static const float GELU_COEF_A = 0.044715f;
  1488. static const float GELU_QUICK_COEF = -1.702f;
  1489. static const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  1490. inline static float ggml_gelu_f32(float x) {
  1491. return 0.5f*x*(1.0f + tanhf(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  1492. }
  1493. inline static void ggml_vec_gelu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  1494. const uint16_t * i16 = (const uint16_t *) x;
  1495. for (int i = 0; i < n; ++i) {
  1496. y[i] = ggml_table_gelu_f16[i16[i]];
  1497. }
  1498. }
  1499. #ifdef GGML_GELU_FP16
  1500. inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
  1501. uint16_t t;
  1502. for (int i = 0; i < n; ++i) {
  1503. if (x[i] <= -10.0f) {
  1504. y[i] = 0.0f;
  1505. } else if (x[i] >= 10.0f) {
  1506. y[i] = x[i];
  1507. } else {
  1508. ggml_fp16_t fp16 = GGML_FP32_TO_FP16(x[i]);
  1509. memcpy(&t, &fp16, sizeof(uint16_t));
  1510. y[i] = GGML_FP16_TO_FP32(ggml_table_gelu_f16[t]);
  1511. }
  1512. }
  1513. }
  1514. #else
  1515. inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
  1516. for (int i = 0; i < n; ++i) {
  1517. y[i] = ggml_gelu_f32(x[i]);
  1518. }
  1519. }
  1520. #endif
  1521. inline static float ggml_gelu_quick_f32(float x) {
  1522. return x*(1.0f/(1.0f+expf(GELU_QUICK_COEF*x)));
  1523. }
  1524. //inline static void ggml_vec_gelu_quick_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  1525. // const uint16_t * i16 = (const uint16_t *) x;
  1526. // for (int i = 0; i < n; ++i) {
  1527. // y[i] = ggml_table_gelu_quick_f16[i16[i]];
  1528. // }
  1529. //}
  1530. #ifdef GGML_GELU_QUICK_FP16
  1531. inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) {
  1532. uint16_t t;
  1533. for (int i = 0; i < n; ++i) {
  1534. ggml_fp16_t fp16 = GGML_FP32_TO_FP16(x[i]);
  1535. memcpy(&t, &fp16, sizeof(uint16_t));
  1536. y[i] = GGML_FP16_TO_FP32(ggml_table_gelu_quick_f16[t]);
  1537. }
  1538. }
  1539. #else
  1540. inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) {
  1541. for (int i = 0; i < n; ++i) {
  1542. y[i] = ggml_gelu_quick_f32(x[i]);
  1543. }
  1544. }
  1545. #endif
  1546. // Sigmoid Linear Unit (SiLU) function
  1547. inline static float ggml_silu_f32(float x) {
  1548. return x/(1.0f + expf(-x));
  1549. }
  1550. #if __FINITE_MATH_ONLY__
  1551. #error "some routines in ggml.c require non-finite math arithmetics -- pass -fno-finite-math-only to the compiler to fix"
  1552. #error "ref: https://github.com/ggerganov/llama.cpp/pull/7154#issuecomment-2143844461"
  1553. #endif
  1554. #if defined(__ARM_NEON) && defined(__aarch64__)
  1555. // adapted from arm limited optimized routine
  1556. // the maximum error is 1.45358 plus 0.5 ulps
  1557. // numbers above 88.38 will flush to infinity
  1558. // numbers beneath -103.97 will flush to zero
  1559. inline static float32x4_t ggml_v_expf(float32x4_t x) {
  1560. const float32x4_t r = vdupq_n_f32(0x1.8p23f);
  1561. const float32x4_t z = vfmaq_f32(r, x, vdupq_n_f32(0x1.715476p+0f));
  1562. const float32x4_t n = vsubq_f32(z, r);
  1563. const float32x4_t b = vfmsq_f32(vfmsq_f32(x, n, vdupq_n_f32(0x1.62e4p-1f)), n,
  1564. vdupq_n_f32(0x1.7f7d1cp-20f));
  1565. const uint32x4_t e = vshlq_n_u32(vreinterpretq_u32_f32(z), 23);
  1566. const float32x4_t k = vreinterpretq_f32_u32(vaddq_u32(e, vreinterpretq_u32_f32(vdupq_n_f32(1))));
  1567. const uint32x4_t c = vcagtq_f32(n, vdupq_n_f32(126));
  1568. const float32x4_t u = vmulq_f32(b, b);
  1569. const float32x4_t j = vfmaq_f32(
  1570. vmulq_f32(vdupq_n_f32(0x1.ffffecp-1f), b),
  1571. vfmaq_f32(vfmaq_f32(vdupq_n_f32(0x1.fffdb6p-2f), vdupq_n_f32(0x1.555e66p-3f), b),
  1572. vfmaq_f32(vdupq_n_f32(0x1.573e2ep-5f), vdupq_n_f32(0x1.0e4020p-7f), b), u), u);
  1573. if (!vpaddd_u64(vreinterpretq_u64_u32(c)))
  1574. return vfmaq_f32(k, j, k);
  1575. const uint32x4_t d = vandq_u32(vclezq_f32(n), vdupq_n_u32(0x82000000));
  1576. const float32x4_t s1 = vreinterpretq_f32_u32(vaddq_u32(d, vdupq_n_u32(0x7f000000)));
  1577. const float32x4_t s2 = vreinterpretq_f32_u32(vsubq_u32(e, d));
  1578. return vbslq_f32(vcagtq_f32(n, vdupq_n_f32(192)), vmulq_f32(s1, s1),
  1579. vbslq_f32(c, vmulq_f32(vfmaq_f32(s2, s2, j), s1), vfmaq_f32(k, k, j)));
  1580. }
  1581. // computes silu x/(1+exp(-x)) in single precision vector
  1582. inline static float32x4_t ggml_v_silu(float32x4_t x) {
  1583. const float32x4_t one = vdupq_n_f32(1.0f);
  1584. const float32x4_t zero = vdupq_n_f32(0.0f);
  1585. const float32x4_t neg_x = vsubq_f32(zero, x);
  1586. const float32x4_t exp_neg_x = ggml_v_expf(neg_x);
  1587. const float32x4_t one_plus_exp_neg_x = vaddq_f32(one, exp_neg_x);
  1588. return vdivq_f32(x, one_plus_exp_neg_x);
  1589. }
  1590. #elif defined(__AVX512F__) && defined(__AVX512DQ__)
  1591. // adapted from arm limited optimized routine
  1592. // the maximum error is 1.45358 plus 0.5 ulps
  1593. // numbers above 88.38 will flush to infinity
  1594. // numbers beneath -103.97 will flush to zero
  1595. inline static __m512 ggml_v_expf(__m512 x) {
  1596. const __m512 r = _mm512_set1_ps(0x1.8p23f);
  1597. const __m512 z = _mm512_fmadd_ps(x, _mm512_set1_ps(0x1.715476p+0f), r);
  1598. const __m512 n = _mm512_sub_ps(z, r);
  1599. const __m512 b =
  1600. _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.7f7d1cp-20f),
  1601. _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.62e4p-1f), x));
  1602. const __mmask16 d =
  1603. _mm512_cmp_ps_mask(_mm512_abs_ps(n), _mm512_set1_ps(192), _CMP_GT_OQ);
  1604. const __m512 u = _mm512_mul_ps(b, b);
  1605. const __m512 j = _mm512_fmadd_ps(
  1606. _mm512_fmadd_ps(_mm512_fmadd_ps(_mm512_set1_ps(0x1.0e4020p-7f), b,
  1607. _mm512_set1_ps(0x1.573e2ep-5f)),
  1608. u,
  1609. _mm512_fmadd_ps(_mm512_set1_ps(0x1.555e66p-3f), b,
  1610. _mm512_set1_ps(0x1.fffdb6p-2f))),
  1611. u,
  1612. _mm512_fmadd_ps(_mm512_set1_ps(0x1.ffffecp-1f), b, _mm512_set1_ps(1.0F)));
  1613. const __m512 res = _mm512_scalef_ps(j, n);
  1614. if (_mm512_kortestz(d, d))
  1615. return res;
  1616. const __m512 zero = _mm512_setzero_ps();
  1617. const __m512 alt = _mm512_mask_blend_ps(
  1618. _mm512_cmp_ps_mask(n, zero, _CMP_LE_OQ), _mm512_set1_ps(INFINITY), zero);
  1619. return _mm512_mask_blend_ps(d, res, alt);
  1620. }
  1621. // computes silu x/(1+exp(-x)) in single precision vector
  1622. inline static __m512 ggml_v_silu(__m512 x) {
  1623. const __m512 one = _mm512_set1_ps(1);
  1624. const __m512 zero = _mm512_setzero_ps();
  1625. const __m512 neg_x = _mm512_sub_ps(zero, x);
  1626. const __m512 exp_neg_x = ggml_v_expf(neg_x);
  1627. const __m512 one_plus_exp_neg_x = _mm512_add_ps(one, exp_neg_x);
  1628. return _mm512_div_ps(x, one_plus_exp_neg_x);
  1629. }
  1630. #elif defined(__AVX2__) && defined(__FMA__)
  1631. // adapted from arm limited optimized routine
  1632. // the maximum error is 1.45358 plus 0.5 ulps
  1633. // numbers above 88.38 will flush to infinity
  1634. // numbers beneath -103.97 will flush to zero
  1635. inline static __m256 ggml_v_expf(__m256 x) {
  1636. const __m256 r = _mm256_set1_ps(0x1.8p23f);
  1637. const __m256 z = _mm256_fmadd_ps(x, _mm256_set1_ps(0x1.715476p+0f), r);
  1638. const __m256 n = _mm256_sub_ps(z, r);
  1639. const __m256 b = _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.7f7d1cp-20f),
  1640. _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.62e4p-1f), x));
  1641. const __m256i e = _mm256_slli_epi32(_mm256_castps_si256(z), 23);
  1642. const __m256 k = _mm256_castsi256_ps(
  1643. _mm256_add_epi32(e, _mm256_castps_si256(_mm256_set1_ps(1))));
  1644. const __m256i c = _mm256_castps_si256(
  1645. _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n),
  1646. _mm256_set1_ps(126), _CMP_GT_OQ));
  1647. const __m256 u = _mm256_mul_ps(b, b);
  1648. const __m256 j = _mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_set1_ps(0x1.0e4020p-7f), b,
  1649. _mm256_set1_ps(0x1.573e2ep-5f)), u,
  1650. _mm256_fmadd_ps(_mm256_set1_ps(0x1.555e66p-3f), b,
  1651. _mm256_set1_ps(0x1.fffdb6p-2f))),
  1652. u, _mm256_mul_ps(_mm256_set1_ps(0x1.ffffecp-1f), b));
  1653. if (!_mm256_movemask_ps(_mm256_castsi256_ps(c)))
  1654. return _mm256_fmadd_ps(j, k, k);
  1655. const __m256i g = _mm256_and_si256(
  1656. _mm256_castps_si256(_mm256_cmp_ps(n, _mm256_setzero_ps(), _CMP_LE_OQ)),
  1657. _mm256_set1_epi32(0x82000000u));
  1658. const __m256 s1 =
  1659. _mm256_castsi256_ps(_mm256_add_epi32(g, _mm256_set1_epi32(0x7f000000u)));
  1660. const __m256 s2 = _mm256_castsi256_ps(_mm256_sub_epi32(e, g));
  1661. const __m256i d = _mm256_castps_si256(
  1662. _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n),
  1663. _mm256_set1_ps(192), _CMP_GT_OQ));
  1664. return _mm256_or_ps(
  1665. _mm256_and_ps(_mm256_castsi256_ps(d), _mm256_mul_ps(s1, s1)),
  1666. _mm256_andnot_ps(
  1667. _mm256_castsi256_ps(d),
  1668. _mm256_or_ps(
  1669. _mm256_and_ps(_mm256_castsi256_ps(c),
  1670. _mm256_mul_ps(_mm256_fmadd_ps(s2, j, s2), s1)),
  1671. _mm256_andnot_ps(_mm256_castsi256_ps(c), _mm256_fmadd_ps(k, j, k)))));
  1672. }
  1673. // computes silu x/(1+exp(-x)) in single precision vector
  1674. inline static __m256 ggml_v_silu(__m256 x) {
  1675. const __m256 one = _mm256_set1_ps(1);
  1676. const __m256 zero = _mm256_setzero_ps();
  1677. const __m256 neg_x = _mm256_sub_ps(zero, x);
  1678. const __m256 exp_neg_x = ggml_v_expf(neg_x);
  1679. const __m256 one_plus_exp_neg_x = _mm256_add_ps(one, exp_neg_x);
  1680. return _mm256_div_ps(x, one_plus_exp_neg_x);
  1681. }
  1682. #elif defined(__SSE2__) // __AVX2__ / __ARM_NEON
  1683. #if defined(__FMA__)
  1684. #define MADD128(x, y, z) _mm_fmadd_ps(x, y, z)
  1685. #define NMADD128(x, y, z) _mm_fnmadd_ps(x, y, z)
  1686. #else
  1687. #define MADD128(x, y, z) _mm_add_ps(_mm_mul_ps(x, y), z)
  1688. #define NMADD128(x, y, z) _mm_sub_ps(z, _mm_mul_ps(x, y))
  1689. #endif
  1690. // adapted from arm limited optimized routine
  1691. // the maximum error is 1.45358 plus 0.5 ulps
  1692. // numbers above 88.38 will flush to infinity
  1693. // numbers beneath -103.97 will flush to zero
  1694. inline static __m128 ggml_v_expf(__m128 x) {
  1695. const __m128 r = _mm_set1_ps(0x1.8p23f);
  1696. const __m128 z = MADD128(x, _mm_set1_ps(0x1.715476p+0f), r);
  1697. const __m128 n = _mm_sub_ps(z, r);
  1698. const __m128 b =
  1699. NMADD128(n, _mm_set1_ps(0x1.7f7d1cp-20f), NMADD128(n, _mm_set1_ps(0x1.62e4p-1f), x));
  1700. const __m128i e = _mm_slli_epi32(_mm_castps_si128(z), 23);
  1701. const __m128 k = _mm_castsi128_ps(_mm_add_epi32(e, _mm_castps_si128(_mm_set1_ps(1))));
  1702. const __m128i c =
  1703. _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(126)));
  1704. const __m128 u = _mm_mul_ps(b, b);
  1705. const __m128 j =
  1706. MADD128(MADD128(MADD128(_mm_set1_ps(0x1.0e4020p-7f), b, _mm_set1_ps(0x1.573e2ep-5f)), u,
  1707. MADD128(_mm_set1_ps(0x1.555e66p-3f), b, _mm_set1_ps(0x1.fffdb6p-2f))),
  1708. u, _mm_mul_ps(_mm_set1_ps(0x1.ffffecp-1f), b));
  1709. if (!_mm_movemask_epi8(c))
  1710. return MADD128(j, k, k);
  1711. const __m128i g = _mm_and_si128(_mm_castps_si128(_mm_cmple_ps(n, _mm_setzero_ps())),
  1712. _mm_set1_epi32(0x82000000u));
  1713. const __m128 s1 = _mm_castsi128_ps(_mm_add_epi32(g, _mm_set1_epi32(0x7f000000u)));
  1714. const __m128 s2 = _mm_castsi128_ps(_mm_sub_epi32(e, g));
  1715. const __m128i d =
  1716. _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(192)));
  1717. return _mm_or_ps(
  1718. _mm_and_ps(_mm_castsi128_ps(d), _mm_mul_ps(s1, s1)),
  1719. _mm_andnot_ps(_mm_castsi128_ps(d),
  1720. _mm_or_ps(_mm_and_ps(_mm_castsi128_ps(c), _mm_mul_ps(MADD128(s2, j, s2), s1)),
  1721. _mm_andnot_ps(_mm_castsi128_ps(c), MADD128(k, j, k)))));
  1722. }
  1723. // computes silu x/(1+exp(-x)) in single precision vector
  1724. inline static __m128 ggml_v_silu(__m128 x) {
  1725. const __m128 one = _mm_set1_ps(1);
  1726. const __m128 zero = _mm_setzero_ps();
  1727. const __m128 neg_x = _mm_sub_ps(zero, x);
  1728. const __m128 exp_neg_x = ggml_v_expf(neg_x);
  1729. const __m128 one_plus_exp_neg_x = _mm_add_ps(one, exp_neg_x);
  1730. return _mm_div_ps(x, one_plus_exp_neg_x);
  1731. }
  1732. #endif // __ARM_NEON / __AVX2__ / __SSE2__
  1733. static void ggml_vec_silu_f32(const int n, float * y, const float * x) {
  1734. int i = 0;
  1735. #if defined(__AVX512F__) && defined(__AVX512DQ__)
  1736. for (; i + 15 < n; i += 16) {
  1737. _mm512_storeu_ps(y + i, ggml_v_silu(_mm512_loadu_ps(x + i)));
  1738. }
  1739. #elif defined(__AVX2__) && defined(__FMA__)
  1740. for (; i + 7 < n; i += 8) {
  1741. _mm256_storeu_ps(y + i, ggml_v_silu(_mm256_loadu_ps(x + i)));
  1742. }
  1743. #elif defined(__SSE2__)
  1744. for (; i + 3 < n; i += 4) {
  1745. _mm_storeu_ps(y + i, ggml_v_silu(_mm_loadu_ps(x + i)));
  1746. }
  1747. #elif defined(__ARM_NEON) && defined(__aarch64__)
  1748. for (; i + 3 < n; i += 4) {
  1749. vst1q_f32(y + i, ggml_v_silu(vld1q_f32(x + i)));
  1750. }
  1751. #endif
  1752. for (; i < n; ++i) {
  1753. y[i] = ggml_silu_f32(x[i]);
  1754. }
  1755. }
  1756. static ggml_float ggml_vec_soft_max_f32(const int n, float * y, const float * x, float max) {
  1757. int i = 0;
  1758. ggml_float sum = 0;
  1759. #if defined(__AVX512F__) && defined(__AVX512DQ__)
  1760. for (; i + 15 < n; i += 16) {
  1761. __m512 val = ggml_v_expf(_mm512_sub_ps(_mm512_loadu_ps(x + i),
  1762. _mm512_set1_ps(max)));
  1763. _mm512_storeu_ps(y + i, val);
  1764. sum += (ggml_float)_mm512_reduce_add_ps(val);
  1765. }
  1766. #elif defined(__AVX2__) && defined(__FMA__)
  1767. for (; i + 7 < n; i += 8) {
  1768. __m256 val = ggml_v_expf(_mm256_sub_ps(_mm256_loadu_ps(x + i),
  1769. _mm256_set1_ps(max)));
  1770. _mm256_storeu_ps(y + i, val);
  1771. __m128 val2 = _mm_add_ps(_mm256_extractf128_ps(val, 1),
  1772. _mm256_castps256_ps128(val));
  1773. val2 = _mm_add_ps(val2, _mm_movehl_ps(val2, val2));
  1774. val2 = _mm_add_ss(val2, _mm_movehdup_ps(val2));
  1775. sum += (ggml_float)_mm_cvtss_f32(val2);
  1776. }
  1777. #elif defined(__SSE2__)
  1778. for (; i + 3 < n; i += 4) {
  1779. __m128 val = ggml_v_expf(_mm_sub_ps(_mm_loadu_ps(x + i),
  1780. _mm_set1_ps(max)));
  1781. _mm_storeu_ps(y + i, val);
  1782. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__)
  1783. val = _mm_add_ps(val, _mm_movehl_ps(val, val));
  1784. val = _mm_add_ss(val, _mm_movehdup_ps(val));
  1785. #else
  1786. __m128 tmp = _mm_shuffle_ps(val, val, _MM_SHUFFLE(2, 3, 0, 1));
  1787. val = _mm_add_ps(val, tmp);
  1788. tmp = _mm_movehl_ps(tmp, val);
  1789. val = _mm_add_ss(val, tmp);
  1790. #endif
  1791. sum += (ggml_float)_mm_cvtss_f32(val);
  1792. }
  1793. #elif defined(__ARM_NEON) && defined(__aarch64__)
  1794. for (; i + 3 < n; i += 4) {
  1795. float32x4_t val = ggml_v_expf(vsubq_f32(vld1q_f32(x + i),
  1796. vdupq_n_f32(max)));
  1797. vst1q_f32(y + i, val);
  1798. sum += (ggml_float)vaddvq_f32(val);
  1799. }
  1800. #endif
  1801. for (; i < n; ++i) {
  1802. float val = expf(x[i] - max);
  1803. sum += (ggml_float)val;
  1804. y[i] = val;
  1805. }
  1806. return sum;
  1807. }
  1808. static ggml_float ggml_vec_log_soft_max_f32(const int n, float * y, const float * x, float max) {
  1809. // log(soft_max) = log(soft_max_i / soft_max_sum) = log(soft_max_i) - log(soft_max_sum) = (logit_i - max) - log(soft_max_i)
  1810. int i = 0;
  1811. ggml_float sum = 0;
  1812. for (; i < n; ++i) {
  1813. float val = x[i] - max;
  1814. y[i] = val;
  1815. sum += (ggml_float)expf(val);
  1816. }
  1817. return sum = (ggml_float)logf(sum);
  1818. }
  1819. inline static float ggml_silu_backward_f32(float x, float dy) {
  1820. const float s = 1.0f/(1.0f + expf(-x));
  1821. return dy*s*(1.0f + x*(1.0f - s));
  1822. }
  1823. inline static void ggml_vec_silu_backward_f32(const int n, float * dx, const float * x, const float * dy) {
  1824. for (int i = 0; i < n; ++i) {
  1825. dx[i] = ggml_silu_backward_f32(x[i], dy[i]);
  1826. }
  1827. }
  1828. inline static void ggml_vec_sum_f32(const int n, float * s, const float * x) {
  1829. #ifndef GGML_USE_ACCELERATE
  1830. ggml_float sum = 0.0;
  1831. for (int i = 0; i < n; ++i) {
  1832. sum += (ggml_float)x[i];
  1833. }
  1834. *s = sum;
  1835. #else
  1836. vDSP_sve(x, 1, s, n);
  1837. #endif
  1838. }
  1839. inline static void ggml_vec_sum_f32_ggf(const int n, ggml_float * s, const float * x) {
  1840. ggml_float sum = 0.0;
  1841. for (int i = 0; i < n; ++i) {
  1842. sum += (ggml_float)x[i];
  1843. }
  1844. *s = sum;
  1845. }
  1846. inline static void ggml_vec_sum_f16_ggf(const int n, float * s, const ggml_fp16_t * x) {
  1847. float sum = 0.0f;
  1848. for (int i = 0; i < n; ++i) {
  1849. sum += GGML_FP16_TO_FP32(x[i]);
  1850. }
  1851. *s = sum;
  1852. }
  1853. inline static void ggml_vec_sum_bf16_ggf(const int n, float * s, const ggml_bf16_t * x) {
  1854. float sum = 0.0f;
  1855. for (int i = 0; i < n; ++i) {
  1856. sum += GGML_BF16_TO_FP32(x[i]);
  1857. }
  1858. *s = sum;
  1859. }
  1860. inline static void ggml_vec_max_f32(const int n, float * s, const float * x) {
  1861. #ifndef GGML_USE_ACCELERATE
  1862. float max = -INFINITY;
  1863. for (int i = 0; i < n; ++i) {
  1864. max = MAX(max, x[i]);
  1865. }
  1866. *s = max;
  1867. #else
  1868. vDSP_maxv(x, 1, s, n);
  1869. #endif
  1870. }
  1871. inline static void ggml_vec_norm_inv_f32(const int n, float * s, const float * x) {
  1872. ggml_vec_norm_f32(n, s, x);
  1873. *s = 1.f/(*s);
  1874. }
  1875. inline static void ggml_vec_argmax_f32(const int n, int * s, const float * x) {
  1876. float max = -INFINITY;
  1877. int idx = 0;
  1878. for (int i = 0; i < n; ++i) {
  1879. max = MAX(max, x[i]);
  1880. if (max == x[i]) { idx = i; }
  1881. }
  1882. *s = idx;
  1883. }
  1884. // Helpers for polling loops
  1885. #if defined(__aarch64__) && ( defined(__clang__) || defined(__GNUC__) )
  1886. static inline void ggml_thread_cpu_relax(void) {
  1887. __asm__ volatile("yield" ::: "memory");
  1888. }
  1889. #elif defined(__x86_64__)
  1890. static inline void ggml_thread_cpu_relax(void) {
  1891. _mm_pause();
  1892. }
  1893. #else
  1894. static inline void ggml_thread_cpu_relax(void) {;}
  1895. #endif
  1896. //
  1897. // NUMA support
  1898. //
  1899. #define GGML_NUMA_MAX_NODES 8
  1900. #define GGML_NUMA_MAX_CPUS 512
  1901. struct ggml_numa_node {
  1902. uint32_t cpus[GGML_NUMA_MAX_CPUS]; // hardware threads on this node
  1903. uint32_t n_cpus;
  1904. };
  1905. struct ggml_numa_nodes {
  1906. enum ggml_numa_strategy numa_strategy;
  1907. struct ggml_numa_node nodes[GGML_NUMA_MAX_NODES];
  1908. uint32_t n_nodes;
  1909. uint32_t total_cpus; // hardware threads on system
  1910. uint32_t current_node; // node on which main process is execting
  1911. #if defined(__gnu_linux__)
  1912. cpu_set_t cpuset; // cpuset from numactl
  1913. #else
  1914. uint32_t cpuset; // no NUMA support outside of Linux at this time. Use a portable datatype
  1915. #endif
  1916. };
  1917. //
  1918. // ggml state
  1919. //
  1920. struct ggml_state {
  1921. struct ggml_numa_nodes numa;
  1922. };
  1923. static struct ggml_state g_state = {0};
  1924. void ggml_barrier(struct ggml_threadpool * tp) {
  1925. int n_threads = atomic_load_explicit(&tp->n_threads_cur, memory_order_relaxed);
  1926. if (n_threads == 1) {
  1927. return;
  1928. }
  1929. #ifdef GGML_USE_OPENMP
  1930. #pragma omp barrier
  1931. #else
  1932. int n_passed = atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed);
  1933. // enter barrier (full seq-cst fence)
  1934. int n_barrier = atomic_fetch_add_explicit(&tp->n_barrier, 1, memory_order_seq_cst);
  1935. if (n_barrier == (n_threads - 1)) {
  1936. // last thread
  1937. atomic_store_explicit(&tp->n_barrier, 0, memory_order_relaxed);
  1938. // exit barrier (fill seq-cst fence)
  1939. atomic_fetch_add_explicit(&tp->n_barrier_passed, 1, memory_order_seq_cst);
  1940. return;
  1941. }
  1942. // wait for other threads
  1943. while (atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed) == n_passed) {
  1944. ggml_thread_cpu_relax();
  1945. }
  1946. // exit barrier (full seq-cst fence)
  1947. // TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
  1948. #ifdef GGML_TSAN_ENABLED
  1949. atomic_fetch_add_explicit(&tp->n_barrier_passed, 0, memory_order_seq_cst);
  1950. #else
  1951. atomic_thread_fence(memory_order_seq_cst);
  1952. #endif
  1953. #endif
  1954. }
  1955. #if defined(__gnu_linux__)
  1956. static cpu_set_t ggml_get_numa_affinity(void) {
  1957. cpu_set_t cpuset;
  1958. pthread_t thread;
  1959. thread = pthread_self();
  1960. CPU_ZERO(&cpuset);
  1961. pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
  1962. return cpuset;
  1963. }
  1964. #else
  1965. static uint32_t ggml_get_numa_affinity(void) {
  1966. return 0; // no NUMA support
  1967. }
  1968. #endif
  1969. void ggml_numa_init(enum ggml_numa_strategy numa_flag) {
  1970. if (g_state.numa.n_nodes > 0) {
  1971. fprintf(stderr, "ggml_numa_init: NUMA already initialized\n");
  1972. return;
  1973. }
  1974. #if defined(__gnu_linux__)
  1975. struct stat st;
  1976. char path[256];
  1977. int rv;
  1978. // set numa scheme
  1979. g_state.numa.numa_strategy = numa_flag;
  1980. GGML_PRINT_DEBUG("numa strategy %u\n",g_state.numa.numa_strategy);
  1981. g_state.numa.cpuset = ggml_get_numa_affinity();
  1982. // enumerate nodes
  1983. while (g_state.numa.n_nodes < GGML_NUMA_MAX_NODES) {
  1984. rv = snprintf(path, sizeof(path), "/sys/devices/system/node/node%u", g_state.numa.n_nodes);
  1985. GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
  1986. if (stat(path, &st) != 0) { break; }
  1987. ++g_state.numa.n_nodes;
  1988. }
  1989. // enumerate CPUs
  1990. while (g_state.numa.total_cpus < GGML_NUMA_MAX_CPUS) {
  1991. rv = snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u", g_state.numa.total_cpus);
  1992. GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
  1993. if (stat(path, &st) != 0) { break; }
  1994. ++g_state.numa.total_cpus;
  1995. }
  1996. GGML_PRINT_DEBUG("found %u numa nodes, %u CPUs\n", g_state.numa.n_nodes, g_state.numa.total_cpus);
  1997. // figure out which node we're on
  1998. uint current_cpu;
  1999. int getcpu_ret = 0;
  2000. #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 33) || defined(__COSMOPOLITAN__)
  2001. getcpu_ret = getcpu(&current_cpu, &g_state.numa.current_node);
  2002. #else
  2003. // old glibc doesn't have a wrapper for this call. Fall back on direct syscall
  2004. # if !defined(SYS_getcpu) && defined(SYS_get_cpu)
  2005. # define SYS_getcpu SYS_get_cpu // some older glibc versions use this name
  2006. # endif
  2007. getcpu_ret = syscall(SYS_getcpu, &current_cpu, &g_state.numa.current_node);
  2008. #endif
  2009. if (g_state.numa.n_nodes < 1 || g_state.numa.total_cpus < 1 || getcpu_ret != 0) {
  2010. g_state.numa.n_nodes = 0;
  2011. return;
  2012. }
  2013. GGML_PRINT_DEBUG("found our process on numa node %u, CPU %u\n", g_state.numa.current_node, current_cpu);
  2014. for (uint32_t n = 0; n < g_state.numa.n_nodes; ++n) {
  2015. struct ggml_numa_node * node = &g_state.numa.nodes[n];
  2016. GGML_PRINT_DEBUG("CPUs on node %u:", n);
  2017. node->n_cpus = 0;
  2018. for (uint32_t c = 0; c < g_state.numa.total_cpus; ++c) {
  2019. rv = snprintf(path, sizeof(path), "/sys/devices/system/node/node%u/cpu%u", n, c);
  2020. GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
  2021. if (stat(path, &st) == 0) {
  2022. node->cpus[node->n_cpus++] = c;
  2023. GGML_PRINT_DEBUG(" %u", c);
  2024. }
  2025. }
  2026. GGML_PRINT_DEBUG("\n");
  2027. }
  2028. if (ggml_is_numa()) {
  2029. FILE *fptr = fopen("/proc/sys/kernel/numa_balancing", "r");
  2030. if (fptr != NULL) {
  2031. char buf[42];
  2032. if (fgets(buf, sizeof(buf), fptr) && strncmp(buf, "0\n", sizeof(buf)) != 0) {
  2033. GGML_LOG_WARN("/proc/sys/kernel/numa_balancing is enabled, this has been observed to impair performance\n");
  2034. }
  2035. fclose(fptr);
  2036. }
  2037. }
  2038. #else
  2039. UNUSED(numa_flag);
  2040. // TODO
  2041. #endif
  2042. }
  2043. bool ggml_is_numa(void) {
  2044. return g_state.numa.n_nodes > 1;
  2045. }
  2046. #if defined(__ARM_ARCH)
  2047. #if defined(__linux__) && defined(__aarch64__)
  2048. #include <sys/auxv.h>
  2049. #elif defined(__APPLE__)
  2050. #include <sys/sysctl.h>
  2051. #endif
  2052. #if !defined(HWCAP2_I8MM)
  2053. #define HWCAP2_I8MM (1 << 13)
  2054. #endif
  2055. static void ggml_init_arm_arch_features(void) {
  2056. #if defined(__linux__) && defined(__aarch64__)
  2057. uint32_t hwcap = getauxval(AT_HWCAP);
  2058. uint32_t hwcap2 = getauxval(AT_HWCAP2);
  2059. ggml_arm_arch_features.has_neon = !!(hwcap & HWCAP_ASIMD);
  2060. ggml_arm_arch_features.has_dotprod = !!(hwcap & HWCAP_ASIMDDP);
  2061. ggml_arm_arch_features.has_i8mm = !!(hwcap2 & HWCAP2_I8MM);
  2062. ggml_arm_arch_features.has_sve = !!(hwcap & HWCAP_SVE);
  2063. #if defined(__ARM_FEATURE_SVE)
  2064. ggml_arm_arch_features.sve_cnt = PR_SVE_VL_LEN_MASK & prctl(PR_SVE_GET_VL);
  2065. #endif
  2066. #elif defined(__APPLE__)
  2067. int oldp = 0;
  2068. size_t size = sizeof(oldp);
  2069. if (sysctlbyname("hw.optional.AdvSIMD", &oldp, &size, NULL, 0) != 0) {
  2070. oldp = 0;
  2071. }
  2072. ggml_arm_arch_features.has_neon = oldp;
  2073. if (sysctlbyname("hw.optional.arm.FEAT_DotProd", &oldp, &size, NULL, 0) != 0) {
  2074. oldp = 0;
  2075. }
  2076. ggml_arm_arch_features.has_dotprod = oldp;
  2077. if (sysctlbyname("hw.optional.arm.FEAT_I8MM", &oldp, &size, NULL, 0) != 0) {
  2078. oldp = 0;
  2079. }
  2080. ggml_arm_arch_features.has_i8mm = oldp;
  2081. ggml_arm_arch_features.has_sve = 0;
  2082. ggml_arm_arch_features.sve_cnt = 0;
  2083. #else
  2084. // Run-time CPU feature detection not implemented for this platform, fallback to compile time
  2085. #if defined(__ARM_NEON)
  2086. ggml_arm_arch_features.has_neon = 1;
  2087. #else
  2088. ggml_arm_arch_features.has_neon = 0;
  2089. #endif
  2090. #if defined(__ARM_FEATURE_MATMUL_INT8)
  2091. ggml_arm_arch_features.has_i8mm = 1;
  2092. #else
  2093. ggml_arm_arch_features.has_i8mm = 0;
  2094. #endif
  2095. #if defined(__ARM_FEATURE_SVE)
  2096. ggml_arm_arch_features.has_sve = 1;
  2097. ggml_arm_arch_features.sve_cnt = 16;
  2098. #else
  2099. ggml_arm_arch_features.has_sve = 0;
  2100. ggml_arm_arch_features.sve_cnt = 0;
  2101. #endif
  2102. #endif
  2103. }
  2104. #endif
  2105. struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value) {
  2106. GGML_ASSERT(!ggml_get_no_alloc(ctx));
  2107. struct ggml_tensor * result = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 1);
  2108. ggml_set_i32(result, value);
  2109. return result;
  2110. }
  2111. struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value) {
  2112. GGML_ASSERT(!ggml_get_no_alloc(ctx));
  2113. struct ggml_tensor * result = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
  2114. ggml_set_f32(result, value);
  2115. return result;
  2116. }
  2117. struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value) {
  2118. const int n = ggml_nrows(tensor);
  2119. const int nc = tensor->ne[0];
  2120. const size_t n1 = tensor->nb[1];
  2121. char * const data = tensor->data;
  2122. switch (tensor->type) {
  2123. case GGML_TYPE_I8:
  2124. {
  2125. assert(tensor->nb[0] == sizeof(int8_t));
  2126. for (int i = 0; i < n; i++) {
  2127. ggml_vec_set_i8(nc, (int8_t *)(data + i*n1), value);
  2128. }
  2129. } break;
  2130. case GGML_TYPE_I16:
  2131. {
  2132. assert(tensor->nb[0] == sizeof(int16_t));
  2133. for (int i = 0; i < n; i++) {
  2134. ggml_vec_set_i16(nc, (int16_t *)(data + i*n1), value);
  2135. }
  2136. } break;
  2137. case GGML_TYPE_I32:
  2138. {
  2139. assert(tensor->nb[0] == sizeof(int32_t));
  2140. for (int i = 0; i < n; i++) {
  2141. ggml_vec_set_i32(nc, (int32_t *)(data + i*n1), value);
  2142. }
  2143. } break;
  2144. case GGML_TYPE_F16:
  2145. {
  2146. assert(tensor->nb[0] == sizeof(ggml_fp16_t));
  2147. for (int i = 0; i < n; i++) {
  2148. ggml_vec_set_f16(nc, (ggml_fp16_t *)(data + i*n1), GGML_FP32_TO_FP16(value));
  2149. }
  2150. } break;
  2151. case GGML_TYPE_BF16:
  2152. {
  2153. assert(tensor->nb[0] == sizeof(ggml_fp16_t));
  2154. for (int i = 0; i < n; i++) {
  2155. ggml_vec_set_bf16(nc, (ggml_bf16_t *)(data + i*n1), GGML_FP32_TO_BF16(value));
  2156. }
  2157. } break;
  2158. case GGML_TYPE_F32:
  2159. {
  2160. assert(tensor->nb[0] == sizeof(float));
  2161. for (int i = 0; i < n; i++) {
  2162. ggml_vec_set_f32(nc, (float *)(data + i*n1), value);
  2163. }
  2164. } break;
  2165. default:
  2166. {
  2167. GGML_ABORT("fatal error");
  2168. }
  2169. }
  2170. return tensor;
  2171. }
  2172. struct ggml_tensor * ggml_set_f32(struct ggml_tensor * tensor, float value) {
  2173. const int n = ggml_nrows(tensor);
  2174. const int nc = tensor->ne[0];
  2175. const size_t n1 = tensor->nb[1];
  2176. char * const data = tensor->data;
  2177. switch (tensor->type) {
  2178. case GGML_TYPE_I8:
  2179. {
  2180. assert(tensor->nb[0] == sizeof(int8_t));
  2181. for (int i = 0; i < n; i++) {
  2182. ggml_vec_set_i8(nc, (int8_t *)(data + i*n1), value);
  2183. }
  2184. } break;
  2185. case GGML_TYPE_I16:
  2186. {
  2187. assert(tensor->nb[0] == sizeof(int16_t));
  2188. for (int i = 0; i < n; i++) {
  2189. ggml_vec_set_i16(nc, (int16_t *)(data + i*n1), value);
  2190. }
  2191. } break;
  2192. case GGML_TYPE_I32:
  2193. {
  2194. assert(tensor->nb[0] == sizeof(int32_t));
  2195. for (int i = 0; i < n; i++) {
  2196. ggml_vec_set_i32(nc, (int32_t *)(data + i*n1), value);
  2197. }
  2198. } break;
  2199. case GGML_TYPE_F16:
  2200. {
  2201. assert(tensor->nb[0] == sizeof(ggml_fp16_t));
  2202. for (int i = 0; i < n; i++) {
  2203. ggml_vec_set_f16(nc, (ggml_fp16_t *)(data + i*n1), GGML_FP32_TO_FP16(value));
  2204. }
  2205. } break;
  2206. case GGML_TYPE_BF16:
  2207. {
  2208. assert(tensor->nb[0] == sizeof(ggml_bf16_t));
  2209. for (int i = 0; i < n; i++) {
  2210. ggml_vec_set_bf16(nc, (ggml_bf16_t *)(data + i*n1), GGML_FP32_TO_BF16(value));
  2211. }
  2212. } break;
  2213. case GGML_TYPE_F32:
  2214. {
  2215. assert(tensor->nb[0] == sizeof(float));
  2216. for (int i = 0; i < n; i++) {
  2217. ggml_vec_set_f32(nc, (float *)(data + i*n1), value);
  2218. }
  2219. } break;
  2220. default:
  2221. {
  2222. GGML_ABORT("fatal error");
  2223. }
  2224. }
  2225. return tensor;
  2226. }
  2227. int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i) {
  2228. if (!ggml_is_contiguous(tensor)) {
  2229. int64_t id[4] = { 0, 0, 0, 0 };
  2230. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2231. return ggml_get_i32_nd(tensor, id[0], id[1], id[2], id[3]);
  2232. }
  2233. switch (tensor->type) {
  2234. case GGML_TYPE_I8:
  2235. {
  2236. GGML_ASSERT(tensor->nb[0] == sizeof(int8_t));
  2237. return ((int8_t *)(tensor->data))[i];
  2238. }
  2239. case GGML_TYPE_I16:
  2240. {
  2241. GGML_ASSERT(tensor->nb[0] == sizeof(int16_t));
  2242. return ((int16_t *)(tensor->data))[i];
  2243. }
  2244. case GGML_TYPE_I32:
  2245. {
  2246. GGML_ASSERT(tensor->nb[0] == sizeof(int32_t));
  2247. return ((int32_t *)(tensor->data))[i];
  2248. }
  2249. case GGML_TYPE_F16:
  2250. {
  2251. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t));
  2252. return GGML_FP16_TO_FP32(((ggml_fp16_t *)(tensor->data))[i]);
  2253. }
  2254. case GGML_TYPE_BF16:
  2255. {
  2256. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_bf16_t));
  2257. return GGML_BF16_TO_FP32(((ggml_bf16_t *)(tensor->data))[i]);
  2258. }
  2259. case GGML_TYPE_F32:
  2260. {
  2261. GGML_ASSERT(tensor->nb[0] == sizeof(float));
  2262. return ((float *)(tensor->data))[i];
  2263. }
  2264. default:
  2265. {
  2266. GGML_ABORT("fatal error");
  2267. }
  2268. }
  2269. }
  2270. void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value) {
  2271. if (!ggml_is_contiguous(tensor)) {
  2272. int64_t id[4] = { 0, 0, 0, 0 };
  2273. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2274. ggml_set_i32_nd(tensor, id[0], id[1], id[2], id[3], value);
  2275. return;
  2276. }
  2277. switch (tensor->type) {
  2278. case GGML_TYPE_I8:
  2279. {
  2280. GGML_ASSERT(tensor->nb[0] == sizeof(int8_t));
  2281. ((int8_t *)(tensor->data))[i] = value;
  2282. } break;
  2283. case GGML_TYPE_I16:
  2284. {
  2285. GGML_ASSERT(tensor->nb[0] == sizeof(int16_t));
  2286. ((int16_t *)(tensor->data))[i] = value;
  2287. } break;
  2288. case GGML_TYPE_I32:
  2289. {
  2290. GGML_ASSERT(tensor->nb[0] == sizeof(int32_t));
  2291. ((int32_t *)(tensor->data))[i] = value;
  2292. } break;
  2293. case GGML_TYPE_F16:
  2294. {
  2295. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t));
  2296. ((ggml_fp16_t *)(tensor->data))[i] = GGML_FP32_TO_FP16(value);
  2297. } break;
  2298. case GGML_TYPE_BF16:
  2299. {
  2300. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_bf16_t));
  2301. ((ggml_bf16_t *)(tensor->data))[i] = GGML_FP32_TO_BF16(value);
  2302. } break;
  2303. case GGML_TYPE_F32:
  2304. {
  2305. GGML_ASSERT(tensor->nb[0] == sizeof(float));
  2306. ((float *)(tensor->data))[i] = value;
  2307. } break;
  2308. default:
  2309. {
  2310. GGML_ABORT("fatal error");
  2311. }
  2312. }
  2313. }
  2314. int32_t ggml_get_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  2315. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2316. switch (tensor->type) {
  2317. case GGML_TYPE_I8:
  2318. return ((int8_t *) data)[0];
  2319. case GGML_TYPE_I16:
  2320. return ((int16_t *) data)[0];
  2321. case GGML_TYPE_I32:
  2322. return ((int32_t *) data)[0];
  2323. case GGML_TYPE_F16:
  2324. return GGML_FP16_TO_FP32(((ggml_fp16_t *) data)[0]);
  2325. case GGML_TYPE_BF16:
  2326. return GGML_BF16_TO_FP32(((ggml_bf16_t *) data)[0]);
  2327. case GGML_TYPE_F32:
  2328. return ((float *) data)[0];
  2329. default:
  2330. GGML_ABORT("fatal error");
  2331. }
  2332. }
  2333. void ggml_set_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, int32_t value) {
  2334. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2335. switch (tensor->type) {
  2336. case GGML_TYPE_I8:
  2337. {
  2338. ((int8_t *)(data))[0] = value;
  2339. } break;
  2340. case GGML_TYPE_I16:
  2341. {
  2342. ((int16_t *)(data))[0] = value;
  2343. } break;
  2344. case GGML_TYPE_I32:
  2345. {
  2346. ((int32_t *)(data))[0] = value;
  2347. } break;
  2348. case GGML_TYPE_F16:
  2349. {
  2350. ((ggml_fp16_t *)(data))[0] = GGML_FP32_TO_FP16(value);
  2351. } break;
  2352. case GGML_TYPE_BF16:
  2353. {
  2354. ((ggml_bf16_t *)(data))[0] = GGML_FP32_TO_BF16(value);
  2355. } break;
  2356. case GGML_TYPE_F32:
  2357. {
  2358. ((float *)(data))[0] = value;
  2359. } break;
  2360. default:
  2361. {
  2362. GGML_ABORT("fatal error");
  2363. }
  2364. }
  2365. }
  2366. float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i) {
  2367. if (!ggml_is_contiguous(tensor)) {
  2368. int64_t id[4] = { 0, 0, 0, 0 };
  2369. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2370. return ggml_get_f32_nd(tensor, id[0], id[1], id[2], id[3]);
  2371. }
  2372. switch (tensor->type) {
  2373. case GGML_TYPE_I8:
  2374. {
  2375. return ((int8_t *)(tensor->data))[i];
  2376. }
  2377. case GGML_TYPE_I16:
  2378. {
  2379. return ((int16_t *)(tensor->data))[i];
  2380. }
  2381. case GGML_TYPE_I32:
  2382. {
  2383. return ((int32_t *)(tensor->data))[i];
  2384. }
  2385. case GGML_TYPE_F16:
  2386. {
  2387. return GGML_FP16_TO_FP32(((ggml_fp16_t *)(tensor->data))[i]);
  2388. }
  2389. case GGML_TYPE_BF16:
  2390. {
  2391. return GGML_BF16_TO_FP32(((ggml_bf16_t *)(tensor->data))[i]);
  2392. }
  2393. case GGML_TYPE_F32:
  2394. {
  2395. return ((float *)(tensor->data))[i];
  2396. }
  2397. default:
  2398. {
  2399. GGML_ABORT("fatal error");
  2400. }
  2401. }
  2402. }
  2403. void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value) {
  2404. if (!ggml_is_contiguous(tensor)) {
  2405. int64_t id[4] = { 0, 0, 0, 0 };
  2406. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2407. ggml_set_f32_nd(tensor, id[0], id[1], id[2], id[3], value);
  2408. return;
  2409. }
  2410. switch (tensor->type) {
  2411. case GGML_TYPE_I8:
  2412. {
  2413. ((int8_t *)(tensor->data))[i] = value;
  2414. } break;
  2415. case GGML_TYPE_I16:
  2416. {
  2417. ((int16_t *)(tensor->data))[i] = value;
  2418. } break;
  2419. case GGML_TYPE_I32:
  2420. {
  2421. ((int32_t *)(tensor->data))[i] = value;
  2422. } break;
  2423. case GGML_TYPE_F16:
  2424. {
  2425. ((ggml_fp16_t *)(tensor->data))[i] = GGML_FP32_TO_FP16(value);
  2426. } break;
  2427. case GGML_TYPE_BF16:
  2428. {
  2429. ((ggml_bf16_t *)(tensor->data))[i] = GGML_FP32_TO_BF16(value);
  2430. } break;
  2431. case GGML_TYPE_F32:
  2432. {
  2433. ((float *)(tensor->data))[i] = value;
  2434. } break;
  2435. default:
  2436. {
  2437. GGML_ABORT("fatal error");
  2438. }
  2439. }
  2440. }
  2441. float ggml_get_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  2442. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2443. switch (tensor->type) {
  2444. case GGML_TYPE_I8:
  2445. return ((int8_t *) data)[0];
  2446. case GGML_TYPE_I16:
  2447. return ((int16_t *) data)[0];
  2448. case GGML_TYPE_I32:
  2449. return ((int32_t *) data)[0];
  2450. case GGML_TYPE_F16:
  2451. return GGML_FP16_TO_FP32(((ggml_fp16_t *) data)[0]);
  2452. case GGML_TYPE_BF16:
  2453. return GGML_BF16_TO_FP32(((ggml_bf16_t *) data)[0]);
  2454. case GGML_TYPE_F32:
  2455. return ((float *) data)[0];
  2456. default:
  2457. GGML_ABORT("fatal error");
  2458. }
  2459. }
  2460. void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, float value) {
  2461. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2462. switch (tensor->type) {
  2463. case GGML_TYPE_I8:
  2464. {
  2465. ((int8_t *)(data))[0] = value;
  2466. } break;
  2467. case GGML_TYPE_I16:
  2468. {
  2469. ((int16_t *)(data))[0] = value;
  2470. } break;
  2471. case GGML_TYPE_I32:
  2472. {
  2473. ((int32_t *)(data))[0] = value;
  2474. } break;
  2475. case GGML_TYPE_F16:
  2476. {
  2477. ((ggml_fp16_t *)(data))[0] = GGML_FP32_TO_FP16(value);
  2478. } break;
  2479. case GGML_TYPE_BF16:
  2480. {
  2481. ((ggml_bf16_t *)(data))[0] = GGML_FP32_TO_BF16(value);
  2482. } break;
  2483. case GGML_TYPE_F32:
  2484. {
  2485. ((float *)(data))[0] = value;
  2486. } break;
  2487. default:
  2488. {
  2489. GGML_ABORT("fatal error");
  2490. }
  2491. }
  2492. }
  2493. ////////////////////////////////////////////////////////////////////////////////
  2494. // ggml_compute_forward_dup
  2495. static void ggml_compute_forward_dup_same_cont(
  2496. const struct ggml_compute_params * params,
  2497. struct ggml_tensor * dst) {
  2498. const struct ggml_tensor * src0 = dst->src[0];
  2499. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  2500. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  2501. GGML_ASSERT(src0->type == dst->type);
  2502. const size_t nb0 = ggml_type_size(src0->type);
  2503. const int ith = params->ith; // thread index
  2504. const int nth = params->nth; // number of threads
  2505. // parallelize by elements
  2506. const int ne = ggml_nelements(dst);
  2507. const int dr = (ne + nth - 1) / nth;
  2508. const int ie0 = dr * ith;
  2509. const int ie1 = MIN(ie0 + dr, ne);
  2510. if (ie0 < ie1) {
  2511. memcpy(
  2512. ((char *) dst->data + ie0*nb0),
  2513. ((char *) src0->data + ie0*nb0),
  2514. (ie1 - ie0) * nb0);
  2515. }
  2516. }
  2517. static void ggml_compute_forward_dup_f16(
  2518. const struct ggml_compute_params * params,
  2519. struct ggml_tensor * dst) {
  2520. const struct ggml_tensor * src0 = dst->src[0];
  2521. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  2522. GGML_TENSOR_UNARY_OP_LOCALS
  2523. const int ith = params->ith; // thread index
  2524. const int nth = params->nth; // number of threads
  2525. // parallelize by rows
  2526. const int nr = ne01;
  2527. // number of rows per thread
  2528. const int dr = (nr + nth - 1) / nth;
  2529. // row range for this thread
  2530. const int ir0 = dr * ith;
  2531. const int ir1 = MIN(ir0 + dr, nr);
  2532. if (src0->type == dst->type &&
  2533. ne00 == ne0 &&
  2534. nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
  2535. // copy by rows
  2536. const size_t rs = ne00*nb00;
  2537. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2538. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2539. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2540. memcpy(
  2541. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  2542. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  2543. rs);
  2544. }
  2545. }
  2546. }
  2547. return;
  2548. }
  2549. // TODO: add more special-case implementations for tensor shapes/strides that can benefit from memcpy
  2550. if (ggml_is_contiguous(dst)) {
  2551. if (nb00 == sizeof(ggml_fp16_t)) {
  2552. if (dst->type == GGML_TYPE_F16) {
  2553. size_t id = 0;
  2554. const size_t rs = ne00 * nb00;
  2555. char * dst_ptr = (char *) dst->data;
  2556. for (int i03 = 0; i03 < ne03; i03++) {
  2557. for (int i02 = 0; i02 < ne02; i02++) {
  2558. id += rs * ir0;
  2559. for (int i01 = ir0; i01 < ir1; i01++) {
  2560. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  2561. memcpy(dst_ptr + id, src0_ptr, rs);
  2562. id += rs;
  2563. }
  2564. id += rs * (ne01 - ir1);
  2565. }
  2566. }
  2567. } else if (dst->type == GGML_TYPE_F32) {
  2568. size_t id = 0;
  2569. float * dst_ptr = (float *) dst->data;
  2570. for (int i03 = 0; i03 < ne03; i03++) {
  2571. for (int i02 = 0; i02 < ne02; i02++) {
  2572. id += ne00 * ir0;
  2573. for (int i01 = ir0; i01 < ir1; i01++) {
  2574. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2575. for (int i00 = 0; i00 < ne00; i00++) {
  2576. dst_ptr[id] = GGML_FP16_TO_FP32(src0_ptr[i00]);
  2577. id++;
  2578. }
  2579. }
  2580. id += ne00 * (ne01 - ir1);
  2581. }
  2582. }
  2583. } else if (ggml_get_type_traits_cpu(dst->type)->from_float) {
  2584. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float;
  2585. float * src0_f32 = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
  2586. size_t id = 0;
  2587. size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
  2588. char * dst_ptr = (char *) dst->data;
  2589. for (int i03 = 0; i03 < ne03; i03++) {
  2590. for (int i02 = 0; i02 < ne02; i02++) {
  2591. id += rs * ir0;
  2592. for (int i01 = ir0; i01 < ir1; i01++) {
  2593. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2594. for (int i00 = 0; i00 < ne00; i00++) {
  2595. src0_f32[i00] = GGML_FP16_TO_FP32(src0_ptr[i00]);
  2596. }
  2597. quantize_row_q(src0_f32, dst_ptr + id, ne00);
  2598. id += rs;
  2599. }
  2600. id += rs * (ne01 - ir1);
  2601. }
  2602. }
  2603. } else {
  2604. GGML_ABORT("fatal error"); // TODO: implement
  2605. }
  2606. } else {
  2607. //printf("%s: this is not optimal - fix me\n", __func__);
  2608. if (dst->type == GGML_TYPE_F32) {
  2609. size_t id = 0;
  2610. float * dst_ptr = (float *) dst->data;
  2611. for (int i03 = 0; i03 < ne03; i03++) {
  2612. for (int i02 = 0; i02 < ne02; i02++) {
  2613. id += ne00 * ir0;
  2614. for (int i01 = ir0; i01 < ir1; i01++) {
  2615. for (int i00 = 0; i00 < ne00; i00++) {
  2616. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2617. dst_ptr[id] = GGML_FP16_TO_FP32(*src0_ptr);
  2618. id++;
  2619. }
  2620. }
  2621. id += ne00 * (ne01 - ir1);
  2622. }
  2623. }
  2624. } else if (dst->type == GGML_TYPE_F16) {
  2625. size_t id = 0;
  2626. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  2627. for (int i03 = 0; i03 < ne03; i03++) {
  2628. for (int i02 = 0; i02 < ne02; i02++) {
  2629. id += ne00 * ir0;
  2630. for (int i01 = ir0; i01 < ir1; i01++) {
  2631. for (int i00 = 0; i00 < ne00; i00++) {
  2632. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2633. dst_ptr[id] = *src0_ptr;
  2634. id++;
  2635. }
  2636. }
  2637. id += ne00 * (ne01 - ir1);
  2638. }
  2639. }
  2640. } else {
  2641. GGML_ABORT("fatal error"); // TODO: implement
  2642. }
  2643. }
  2644. return;
  2645. }
  2646. // dst counters
  2647. int64_t i10 = 0;
  2648. int64_t i11 = 0;
  2649. int64_t i12 = 0;
  2650. int64_t i13 = 0;
  2651. if (dst->type == GGML_TYPE_F16) {
  2652. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2653. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2654. i10 += ne00 * ir0;
  2655. while (i10 >= ne0) {
  2656. i10 -= ne0;
  2657. if (++i11 == ne1) {
  2658. i11 = 0;
  2659. if (++i12 == ne2) {
  2660. i12 = 0;
  2661. if (++i13 == ne3) {
  2662. i13 = 0;
  2663. }
  2664. }
  2665. }
  2666. }
  2667. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2668. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2669. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2670. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2671. memcpy(dst_ptr, src0_ptr, sizeof(ggml_fp16_t));
  2672. if (++i10 == ne00) {
  2673. i10 = 0;
  2674. if (++i11 == ne01) {
  2675. i11 = 0;
  2676. if (++i12 == ne02) {
  2677. i12 = 0;
  2678. if (++i13 == ne03) {
  2679. i13 = 0;
  2680. }
  2681. }
  2682. }
  2683. }
  2684. }
  2685. }
  2686. i10 += ne00 * (ne01 - ir1);
  2687. while (i10 >= ne0) {
  2688. i10 -= ne0;
  2689. if (++i11 == ne1) {
  2690. i11 = 0;
  2691. if (++i12 == ne2) {
  2692. i12 = 0;
  2693. if (++i13 == ne3) {
  2694. i13 = 0;
  2695. }
  2696. }
  2697. }
  2698. }
  2699. }
  2700. }
  2701. } else if (dst->type == GGML_TYPE_F32) {
  2702. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2703. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2704. i10 += ne00 * ir0;
  2705. while (i10 >= ne0) {
  2706. i10 -= ne0;
  2707. if (++i11 == ne1) {
  2708. i11 = 0;
  2709. if (++i12 == ne2) {
  2710. i12 = 0;
  2711. if (++i13 == ne3) {
  2712. i13 = 0;
  2713. }
  2714. }
  2715. }
  2716. }
  2717. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2718. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2719. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2720. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2721. *(float *) dst_ptr = GGML_FP16_TO_FP32(*(const ggml_fp16_t *) src0_ptr);
  2722. if (++i10 == ne0) {
  2723. i10 = 0;
  2724. if (++i11 == ne1) {
  2725. i11 = 0;
  2726. if (++i12 == ne2) {
  2727. i12 = 0;
  2728. if (++i13 == ne3) {
  2729. i13 = 0;
  2730. }
  2731. }
  2732. }
  2733. }
  2734. }
  2735. }
  2736. i10 += ne00 * (ne01 - ir1);
  2737. while (i10 >= ne0) {
  2738. i10 -= ne0;
  2739. if (++i11 == ne1) {
  2740. i11 = 0;
  2741. if (++i12 == ne2) {
  2742. i12 = 0;
  2743. if (++i13 == ne3) {
  2744. i13 = 0;
  2745. }
  2746. }
  2747. }
  2748. }
  2749. }
  2750. }
  2751. } else {
  2752. GGML_ABORT("fatal error"); // TODO: implement
  2753. }
  2754. }
  2755. static void ggml_compute_forward_dup_bf16(
  2756. const struct ggml_compute_params * params,
  2757. struct ggml_tensor * dst) {
  2758. const struct ggml_tensor * src0 = dst->src[0];
  2759. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  2760. GGML_TENSOR_UNARY_OP_LOCALS
  2761. const int ith = params->ith; // thread index
  2762. const int nth = params->nth; // number of threads
  2763. // parallelize by rows
  2764. const int nr = ne01;
  2765. // number of rows per thread
  2766. const int dr = (nr + nth - 1) / nth;
  2767. // row range for this thread
  2768. const int ir0 = dr * ith;
  2769. const int ir1 = MIN(ir0 + dr, nr);
  2770. if (src0->type == dst->type &&
  2771. ne00 == ne0 &&
  2772. nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
  2773. // copy by rows
  2774. const size_t rs = ne00*nb00;
  2775. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2776. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2777. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2778. memcpy(
  2779. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  2780. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  2781. rs);
  2782. }
  2783. }
  2784. }
  2785. return;
  2786. }
  2787. // TODO: add more special-case implementations for tensor shapes/strides that can benefit from memcpy
  2788. if (ggml_is_contiguous(dst)) {
  2789. if (nb00 == sizeof(ggml_bf16_t)) {
  2790. if (dst->type == GGML_TYPE_BF16) {
  2791. size_t id = 0;
  2792. const size_t rs = ne00 * nb00;
  2793. char * dst_ptr = (char *) dst->data;
  2794. for (int i03 = 0; i03 < ne03; i03++) {
  2795. for (int i02 = 0; i02 < ne02; i02++) {
  2796. id += rs * ir0;
  2797. for (int i01 = ir0; i01 < ir1; i01++) {
  2798. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  2799. memcpy(dst_ptr + id, src0_ptr, rs);
  2800. id += rs;
  2801. }
  2802. id += rs * (ne01 - ir1);
  2803. }
  2804. }
  2805. } else if (dst->type == GGML_TYPE_F16) {
  2806. size_t id = 0;
  2807. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  2808. for (int i03 = 0; i03 < ne03; i03++) {
  2809. for (int i02 = 0; i02 < ne02; i02++) {
  2810. id += ne00 * ir0;
  2811. for (int i01 = ir0; i01 < ir1; i01++) {
  2812. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2813. for (int i00 = 0; i00 < ne00; i00++) {
  2814. dst_ptr[id] = GGML_FP32_TO_FP16(GGML_BF16_TO_FP32(src0_ptr[i00]));
  2815. id++;
  2816. }
  2817. }
  2818. id += ne00 * (ne01 - ir1);
  2819. }
  2820. }
  2821. } else if (dst->type == GGML_TYPE_F32) {
  2822. size_t id = 0;
  2823. float * dst_ptr = (float *) dst->data;
  2824. for (int i03 = 0; i03 < ne03; i03++) {
  2825. for (int i02 = 0; i02 < ne02; i02++) {
  2826. id += ne00 * ir0;
  2827. for (int i01 = ir0; i01 < ir1; i01++) {
  2828. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2829. for (int i00 = 0; i00 < ne00; i00++) {
  2830. dst_ptr[id] = GGML_BF16_TO_FP32(src0_ptr[i00]);
  2831. id++;
  2832. }
  2833. }
  2834. id += ne00 * (ne01 - ir1);
  2835. }
  2836. }
  2837. } else if (ggml_get_type_traits_cpu(dst->type)->from_float) {
  2838. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float;
  2839. float * src0_f32 = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
  2840. size_t id = 0;
  2841. size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
  2842. char * dst_ptr = (char *) dst->data;
  2843. for (int i03 = 0; i03 < ne03; i03++) {
  2844. for (int i02 = 0; i02 < ne02; i02++) {
  2845. id += rs * ir0;
  2846. for (int i01 = ir0; i01 < ir1; i01++) {
  2847. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2848. for (int i00 = 0; i00 < ne00; i00++) {
  2849. src0_f32[i00] = GGML_BF16_TO_FP32(src0_ptr[i00]);
  2850. }
  2851. quantize_row_q(src0_f32, dst_ptr + id, ne00);
  2852. id += rs;
  2853. }
  2854. id += rs * (ne01 - ir1);
  2855. }
  2856. }
  2857. } else {
  2858. GGML_ABORT("fatal error"); // TODO: implement
  2859. }
  2860. } else {
  2861. //printf("%s: this is not optimal - fix me\n", __func__);
  2862. if (dst->type == GGML_TYPE_F32) {
  2863. size_t id = 0;
  2864. float * dst_ptr = (float *) dst->data;
  2865. for (int i03 = 0; i03 < ne03; i03++) {
  2866. for (int i02 = 0; i02 < ne02; i02++) {
  2867. id += ne00 * ir0;
  2868. for (int i01 = ir0; i01 < ir1; i01++) {
  2869. for (int i00 = 0; i00 < ne00; i00++) {
  2870. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2871. dst_ptr[id] = GGML_BF16_TO_FP32(*src0_ptr);
  2872. id++;
  2873. }
  2874. }
  2875. id += ne00 * (ne01 - ir1);
  2876. }
  2877. }
  2878. } else if (dst->type == GGML_TYPE_BF16) {
  2879. size_t id = 0;
  2880. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) dst->data;
  2881. for (int i03 = 0; i03 < ne03; i03++) {
  2882. for (int i02 = 0; i02 < ne02; i02++) {
  2883. id += ne00 * ir0;
  2884. for (int i01 = ir0; i01 < ir1; i01++) {
  2885. for (int i00 = 0; i00 < ne00; i00++) {
  2886. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2887. dst_ptr[id] = *src0_ptr;
  2888. id++;
  2889. }
  2890. }
  2891. id += ne00 * (ne01 - ir1);
  2892. }
  2893. }
  2894. } else if (dst->type == GGML_TYPE_F16) {
  2895. size_t id = 0;
  2896. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  2897. for (int i03 = 0; i03 < ne03; i03++) {
  2898. for (int i02 = 0; i02 < ne02; i02++) {
  2899. id += ne00 * ir0;
  2900. for (int i01 = ir0; i01 < ir1; i01++) {
  2901. for (int i00 = 0; i00 < ne00; i00++) {
  2902. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2903. dst_ptr[id] = GGML_FP32_TO_FP16(GGML_BF16_TO_FP32(*src0_ptr));
  2904. id++;
  2905. }
  2906. }
  2907. id += ne00 * (ne01 - ir1);
  2908. }
  2909. }
  2910. } else {
  2911. GGML_ABORT("fatal error"); // TODO: implement
  2912. }
  2913. }
  2914. return;
  2915. }
  2916. // dst counters
  2917. int64_t i10 = 0;
  2918. int64_t i11 = 0;
  2919. int64_t i12 = 0;
  2920. int64_t i13 = 0;
  2921. if (dst->type == GGML_TYPE_BF16) {
  2922. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2923. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2924. i10 += ne00 * ir0;
  2925. while (i10 >= ne0) {
  2926. i10 -= ne0;
  2927. if (++i11 == ne1) {
  2928. i11 = 0;
  2929. if (++i12 == ne2) {
  2930. i12 = 0;
  2931. if (++i13 == ne3) {
  2932. i13 = 0;
  2933. }
  2934. }
  2935. }
  2936. }
  2937. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2938. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2939. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2940. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2941. memcpy(dst_ptr, src0_ptr, sizeof(ggml_bf16_t));
  2942. if (++i10 == ne00) {
  2943. i10 = 0;
  2944. if (++i11 == ne01) {
  2945. i11 = 0;
  2946. if (++i12 == ne02) {
  2947. i12 = 0;
  2948. if (++i13 == ne03) {
  2949. i13 = 0;
  2950. }
  2951. }
  2952. }
  2953. }
  2954. }
  2955. }
  2956. i10 += ne00 * (ne01 - ir1);
  2957. while (i10 >= ne0) {
  2958. i10 -= ne0;
  2959. if (++i11 == ne1) {
  2960. i11 = 0;
  2961. if (++i12 == ne2) {
  2962. i12 = 0;
  2963. if (++i13 == ne3) {
  2964. i13 = 0;
  2965. }
  2966. }
  2967. }
  2968. }
  2969. }
  2970. }
  2971. } else if (dst->type == GGML_TYPE_F16) {
  2972. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2973. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2974. i10 += ne00 * ir0;
  2975. while (i10 >= ne0) {
  2976. i10 -= ne0;
  2977. if (++i11 == ne1) {
  2978. i11 = 0;
  2979. if (++i12 == ne2) {
  2980. i12 = 0;
  2981. if (++i13 == ne3) {
  2982. i13 = 0;
  2983. }
  2984. }
  2985. }
  2986. }
  2987. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2988. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2989. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2990. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2991. *(ggml_fp16_t *) dst_ptr = GGML_FP32_TO_FP16(GGML_BF16_TO_FP32(*(const ggml_bf16_t *) src0_ptr));
  2992. if (++i10 == ne0) {
  2993. i10 = 0;
  2994. if (++i11 == ne1) {
  2995. i11 = 0;
  2996. if (++i12 == ne2) {
  2997. i12 = 0;
  2998. if (++i13 == ne3) {
  2999. i13 = 0;
  3000. }
  3001. }
  3002. }
  3003. }
  3004. }
  3005. }
  3006. i10 += ne00 * (ne01 - ir1);
  3007. while (i10 >= ne0) {
  3008. i10 -= ne0;
  3009. if (++i11 == ne1) {
  3010. i11 = 0;
  3011. if (++i12 == ne2) {
  3012. i12 = 0;
  3013. if (++i13 == ne3) {
  3014. i13 = 0;
  3015. }
  3016. }
  3017. }
  3018. }
  3019. }
  3020. }
  3021. } else if (dst->type == GGML_TYPE_F32) {
  3022. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3023. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3024. i10 += ne00 * ir0;
  3025. while (i10 >= ne0) {
  3026. i10 -= ne0;
  3027. if (++i11 == ne1) {
  3028. i11 = 0;
  3029. if (++i12 == ne2) {
  3030. i12 = 0;
  3031. if (++i13 == ne3) {
  3032. i13 = 0;
  3033. }
  3034. }
  3035. }
  3036. }
  3037. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3038. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3039. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3040. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3041. *(float *) dst_ptr = GGML_BF16_TO_FP32(*(const ggml_bf16_t *) src0_ptr);
  3042. if (++i10 == ne0) {
  3043. i10 = 0;
  3044. if (++i11 == ne1) {
  3045. i11 = 0;
  3046. if (++i12 == ne2) {
  3047. i12 = 0;
  3048. if (++i13 == ne3) {
  3049. i13 = 0;
  3050. }
  3051. }
  3052. }
  3053. }
  3054. }
  3055. }
  3056. i10 += ne00 * (ne01 - ir1);
  3057. while (i10 >= ne0) {
  3058. i10 -= ne0;
  3059. if (++i11 == ne1) {
  3060. i11 = 0;
  3061. if (++i12 == ne2) {
  3062. i12 = 0;
  3063. if (++i13 == ne3) {
  3064. i13 = 0;
  3065. }
  3066. }
  3067. }
  3068. }
  3069. }
  3070. }
  3071. } else {
  3072. GGML_ABORT("fatal error"); // TODO: implement
  3073. }
  3074. }
  3075. static void ggml_compute_forward_dup_f32(
  3076. const struct ggml_compute_params * params,
  3077. struct ggml_tensor * dst) {
  3078. const struct ggml_tensor * src0 = dst->src[0];
  3079. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  3080. GGML_TENSOR_UNARY_OP_LOCALS
  3081. const int ith = params->ith; // thread index
  3082. const int nth = params->nth; // number of threads
  3083. // parallelize by rows
  3084. const int nr = ne01;
  3085. // number of rows per thread
  3086. const int dr = (nr + nth - 1) / nth;
  3087. // row range for this thread
  3088. const int ir0 = dr * ith;
  3089. const int ir1 = MIN(ir0 + dr, nr);
  3090. if (src0->type == dst->type &&
  3091. ne00 == ne0 &&
  3092. nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
  3093. // copy by rows
  3094. const size_t rs = ne00*nb00;
  3095. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3096. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3097. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3098. memcpy(
  3099. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  3100. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  3101. rs);
  3102. }
  3103. }
  3104. }
  3105. return;
  3106. }
  3107. if (ggml_is_contiguous(dst)) {
  3108. // TODO: simplify
  3109. if (nb00 == sizeof(float)) {
  3110. if (dst->type == GGML_TYPE_F32) {
  3111. size_t id = 0;
  3112. const size_t rs = ne00 * nb00;
  3113. char * dst_ptr = (char *) dst->data;
  3114. for (int i03 = 0; i03 < ne03; i03++) {
  3115. for (int i02 = 0; i02 < ne02; i02++) {
  3116. id += rs * ir0;
  3117. for (int i01 = ir0; i01 < ir1; i01++) {
  3118. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  3119. memcpy(dst_ptr + id, src0_ptr, rs);
  3120. id += rs;
  3121. }
  3122. id += rs * (ne01 - ir1);
  3123. }
  3124. }
  3125. } else if (ggml_get_type_traits_cpu(dst->type)->from_float) {
  3126. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float;
  3127. size_t id = 0;
  3128. size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
  3129. char * dst_ptr = (char *) dst->data;
  3130. for (int i03 = 0; i03 < ne03; i03++) {
  3131. for (int i02 = 0; i02 < ne02; i02++) {
  3132. id += rs * ir0;
  3133. for (int i01 = ir0; i01 < ir1; i01++) {
  3134. const float * src0_ptr = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  3135. quantize_row_q(src0_ptr, dst_ptr + id, ne00);
  3136. id += rs;
  3137. }
  3138. id += rs * (ne01 - ir1);
  3139. }
  3140. }
  3141. } else {
  3142. GGML_ABORT("fatal error"); // TODO: implement
  3143. }
  3144. } else {
  3145. //printf("%s: this is not optimal - fix me\n", __func__);
  3146. if (dst->type == GGML_TYPE_F32) {
  3147. size_t id = 0;
  3148. float * dst_ptr = (float *) dst->data;
  3149. for (int i03 = 0; i03 < ne03; i03++) {
  3150. for (int i02 = 0; i02 < ne02; i02++) {
  3151. id += ne00 * ir0;
  3152. for (int i01 = ir0; i01 < ir1; i01++) {
  3153. for (int i00 = 0; i00 < ne00; i00++) {
  3154. const float * src0_ptr = (float *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3155. dst_ptr[id] = *src0_ptr;
  3156. id++;
  3157. }
  3158. }
  3159. id += ne00 * (ne01 - ir1);
  3160. }
  3161. }
  3162. } else if (dst->type == GGML_TYPE_F16) {
  3163. size_t id = 0;
  3164. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  3165. for (int i03 = 0; i03 < ne03; i03++) {
  3166. for (int i02 = 0; i02 < ne02; i02++) {
  3167. id += ne00 * ir0;
  3168. for (int i01 = ir0; i01 < ir1; i01++) {
  3169. for (int i00 = 0; i00 < ne00; i00++) {
  3170. const float * src0_ptr = (float *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3171. dst_ptr[id] = GGML_FP32_TO_FP16(*src0_ptr);
  3172. id++;
  3173. }
  3174. }
  3175. id += ne00 * (ne01 - ir1);
  3176. }
  3177. }
  3178. } else if (dst->type == GGML_TYPE_BF16) {
  3179. size_t id = 0;
  3180. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) dst->data;
  3181. for (int i03 = 0; i03 < ne03; i03++) {
  3182. for (int i02 = 0; i02 < ne02; i02++) {
  3183. id += ne00 * ir0;
  3184. for (int i01 = ir0; i01 < ir1; i01++) {
  3185. for (int i00 = 0; i00 < ne00; i00++) {
  3186. const float * src0_ptr = (float *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3187. dst_ptr[id] = GGML_FP32_TO_BF16(*src0_ptr);
  3188. id++;
  3189. }
  3190. }
  3191. id += ne00 * (ne01 - ir1);
  3192. }
  3193. }
  3194. } else {
  3195. GGML_ABORT("fatal error"); // TODO: implement
  3196. }
  3197. }
  3198. return;
  3199. }
  3200. // dst counters
  3201. int64_t i10 = 0;
  3202. int64_t i11 = 0;
  3203. int64_t i12 = 0;
  3204. int64_t i13 = 0;
  3205. if (dst->type == GGML_TYPE_F32) {
  3206. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3207. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3208. i10 += ne00 * ir0;
  3209. while (i10 >= ne0) {
  3210. i10 -= ne0;
  3211. if (++i11 == ne1) {
  3212. i11 = 0;
  3213. if (++i12 == ne2) {
  3214. i12 = 0;
  3215. if (++i13 == ne3) {
  3216. i13 = 0;
  3217. }
  3218. }
  3219. }
  3220. }
  3221. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3222. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3223. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3224. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3225. memcpy(dst_ptr, src0_ptr, sizeof(float));
  3226. if (++i10 == ne0) {
  3227. i10 = 0;
  3228. if (++i11 == ne1) {
  3229. i11 = 0;
  3230. if (++i12 == ne2) {
  3231. i12 = 0;
  3232. if (++i13 == ne3) {
  3233. i13 = 0;
  3234. }
  3235. }
  3236. }
  3237. }
  3238. }
  3239. }
  3240. i10 += ne00 * (ne01 - ir1);
  3241. while (i10 >= ne0) {
  3242. i10 -= ne0;
  3243. if (++i11 == ne1) {
  3244. i11 = 0;
  3245. if (++i12 == ne2) {
  3246. i12 = 0;
  3247. if (++i13 == ne3) {
  3248. i13 = 0;
  3249. }
  3250. }
  3251. }
  3252. }
  3253. }
  3254. }
  3255. } else if (dst->type == GGML_TYPE_F16) {
  3256. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3257. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3258. i10 += ne00 * ir0;
  3259. while (i10 >= ne0) {
  3260. i10 -= ne0;
  3261. if (++i11 == ne1) {
  3262. i11 = 0;
  3263. if (++i12 == ne2) {
  3264. i12 = 0;
  3265. if (++i13 == ne3) {
  3266. i13 = 0;
  3267. }
  3268. }
  3269. }
  3270. }
  3271. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3272. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3273. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3274. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3275. *(ggml_fp16_t *) dst_ptr = GGML_FP32_TO_FP16(*(const float *) src0_ptr);
  3276. if (++i10 == ne0) {
  3277. i10 = 0;
  3278. if (++i11 == ne1) {
  3279. i11 = 0;
  3280. if (++i12 == ne2) {
  3281. i12 = 0;
  3282. if (++i13 == ne3) {
  3283. i13 = 0;
  3284. }
  3285. }
  3286. }
  3287. }
  3288. }
  3289. }
  3290. i10 += ne00 * (ne01 - ir1);
  3291. while (i10 >= ne0) {
  3292. i10 -= ne0;
  3293. if (++i11 == ne1) {
  3294. i11 = 0;
  3295. if (++i12 == ne2) {
  3296. i12 = 0;
  3297. if (++i13 == ne3) {
  3298. i13 = 0;
  3299. }
  3300. }
  3301. }
  3302. }
  3303. }
  3304. }
  3305. } else if (dst->type == GGML_TYPE_BF16) {
  3306. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3307. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3308. i10 += ne00 * ir0;
  3309. while (i10 >= ne0) {
  3310. i10 -= ne0;
  3311. if (++i11 == ne1) {
  3312. i11 = 0;
  3313. if (++i12 == ne2) {
  3314. i12 = 0;
  3315. if (++i13 == ne3) {
  3316. i13 = 0;
  3317. }
  3318. }
  3319. }
  3320. }
  3321. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3322. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3323. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3324. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3325. *(ggml_bf16_t *) dst_ptr = GGML_FP32_TO_BF16(*(const float *) src0_ptr);
  3326. if (++i10 == ne0) {
  3327. i10 = 0;
  3328. if (++i11 == ne1) {
  3329. i11 = 0;
  3330. if (++i12 == ne2) {
  3331. i12 = 0;
  3332. if (++i13 == ne3) {
  3333. i13 = 0;
  3334. }
  3335. }
  3336. }
  3337. }
  3338. }
  3339. }
  3340. i10 += ne00 * (ne01 - ir1);
  3341. while (i10 >= ne0) {
  3342. i10 -= ne0;
  3343. if (++i11 == ne1) {
  3344. i11 = 0;
  3345. if (++i12 == ne2) {
  3346. i12 = 0;
  3347. if (++i13 == ne3) {
  3348. i13 = 0;
  3349. }
  3350. }
  3351. }
  3352. }
  3353. }
  3354. }
  3355. } else {
  3356. GGML_ABORT("fatal error"); // TODO: implement
  3357. }
  3358. }
  3359. // A simplified version of ggml_compute_forward_dup that doesn't do float upcasting, and just plain old memcpy.
  3360. static void ggml_compute_forward_dup_bytes(
  3361. const struct ggml_compute_params * params,
  3362. struct ggml_tensor * dst) {
  3363. const struct ggml_tensor * src0 = dst->src[0];
  3364. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  3365. GGML_ASSERT(src0->type == dst->type);
  3366. GGML_TENSOR_UNARY_OP_LOCALS;
  3367. if (ggml_is_contiguous(src0) && ggml_is_contiguous(dst)) {
  3368. ggml_compute_forward_dup_same_cont(params, dst);
  3369. return;
  3370. }
  3371. const size_t type_size = ggml_type_size(src0->type);
  3372. const int ith = params->ith; // thread index
  3373. const int nth = params->nth; // number of threads
  3374. // parallelize by rows
  3375. const int nr = ne01;
  3376. // number of rows per thread
  3377. const int dr = (nr + nth - 1) / nth;
  3378. // row range for this thread
  3379. const int ir0 = dr * ith;
  3380. const int ir1 = MIN(ir0 + dr, nr);
  3381. if (src0->type == dst->type &&
  3382. ne00 == ne0 &&
  3383. nb00 == type_size && nb0 == type_size) {
  3384. // copy by rows
  3385. const size_t rs = ne00 * type_size;
  3386. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3387. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3388. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3389. memcpy(
  3390. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  3391. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  3392. rs);
  3393. }
  3394. }
  3395. }
  3396. return;
  3397. }
  3398. if (ggml_is_contiguous(dst)) {
  3399. size_t id = 0;
  3400. char * dst_ptr = (char *) dst->data;
  3401. const size_t rs = ne00 * type_size;
  3402. if (nb00 == type_size) {
  3403. // src0 is contigous on first dimension, copy by rows
  3404. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3405. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3406. id += rs * ir0;
  3407. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3408. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  3409. memcpy(dst_ptr + id, src0_ptr, rs);
  3410. id += rs;
  3411. }
  3412. id += rs * (ne01 - ir1);
  3413. }
  3414. }
  3415. } else {
  3416. //printf("%s: this is not optimal - fix me\n", __func__);
  3417. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3418. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3419. id += rs * ir0;
  3420. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3421. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3422. const char * src0_ptr = (char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03;
  3423. memcpy(dst_ptr + id, src0_ptr, type_size);
  3424. id += type_size;
  3425. }
  3426. }
  3427. id += rs * (ne01 - ir1);
  3428. }
  3429. }
  3430. }
  3431. return;
  3432. }
  3433. // dst counters
  3434. int64_t i10 = 0;
  3435. int64_t i11 = 0;
  3436. int64_t i12 = 0;
  3437. int64_t i13 = 0;
  3438. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3439. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3440. i10 += ne00 * ir0;
  3441. while (i10 >= ne0) {
  3442. i10 -= ne0;
  3443. if (++i11 == ne1) {
  3444. i11 = 0;
  3445. if (++i12 == ne2) {
  3446. i12 = 0;
  3447. if (++i13 == ne3) {
  3448. i13 = 0;
  3449. }
  3450. }
  3451. }
  3452. }
  3453. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3454. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3455. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3456. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3457. memcpy(dst_ptr, src0_ptr, type_size);
  3458. if (++i10 == ne0) {
  3459. i10 = 0;
  3460. if (++i11 == ne1) {
  3461. i11 = 0;
  3462. if (++i12 == ne2) {
  3463. i12 = 0;
  3464. if (++i13 == ne3) {
  3465. i13 = 0;
  3466. }
  3467. }
  3468. }
  3469. }
  3470. }
  3471. }
  3472. i10 += ne00 * (ne01 - ir1);
  3473. while (i10 >= ne0) {
  3474. i10 -= ne0;
  3475. if (++i11 == ne1) {
  3476. i11 = 0;
  3477. if (++i12 == ne2) {
  3478. i12 = 0;
  3479. if (++i13 == ne3) {
  3480. i13 = 0;
  3481. }
  3482. }
  3483. }
  3484. }
  3485. }
  3486. }
  3487. }
  3488. static void ggml_compute_forward_dup(
  3489. const struct ggml_compute_params * params,
  3490. struct ggml_tensor * dst) {
  3491. const struct ggml_tensor * src0 = dst->src[0];
  3492. if (src0->type == dst->type) {
  3493. ggml_compute_forward_dup_bytes(params, dst);
  3494. return;
  3495. }
  3496. switch (src0->type) {
  3497. case GGML_TYPE_F16:
  3498. {
  3499. ggml_compute_forward_dup_f16(params, dst);
  3500. } break;
  3501. case GGML_TYPE_BF16:
  3502. {
  3503. ggml_compute_forward_dup_bf16(params, dst);
  3504. } break;
  3505. case GGML_TYPE_F32:
  3506. {
  3507. ggml_compute_forward_dup_f32(params, dst);
  3508. } break;
  3509. default:
  3510. {
  3511. GGML_ABORT("fatal error");
  3512. }
  3513. }
  3514. }
  3515. // ggml_compute_forward_add
  3516. static void ggml_compute_forward_add_f32(
  3517. const struct ggml_compute_params * params,
  3518. struct ggml_tensor * dst) {
  3519. const struct ggml_tensor * src0 = dst->src[0];
  3520. const struct ggml_tensor * src1 = dst->src[1];
  3521. GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  3522. const int ith = params->ith;
  3523. const int nth = params->nth;
  3524. const int nr = ggml_nrows(src0);
  3525. GGML_TENSOR_BINARY_OP_LOCALS
  3526. GGML_ASSERT( nb0 == sizeof(float));
  3527. GGML_ASSERT(nb00 == sizeof(float));
  3528. // rows per thread
  3529. const int dr = (nr + nth - 1)/nth;
  3530. // row range for this thread
  3531. const int ir0 = dr*ith;
  3532. const int ir1 = MIN(ir0 + dr, nr);
  3533. if (nb10 == sizeof(float)) {
  3534. for (int ir = ir0; ir < ir1; ++ir) {
  3535. // src1 is broadcastable across src0 and dst in i1, i2, i3
  3536. const int64_t i03 = ir/(ne02*ne01);
  3537. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  3538. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  3539. const int64_t i13 = i03 % ne13;
  3540. const int64_t i12 = i02 % ne12;
  3541. const int64_t i11 = i01 % ne11;
  3542. const int64_t nr0 = ne00 / ne10;
  3543. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  3544. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  3545. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  3546. for (int64_t r = 0; r < nr0; ++r) {
  3547. #ifdef GGML_USE_ACCELERATE
  3548. vDSP_vadd(src0_ptr + r*ne10, 1, src1_ptr, 1, dst_ptr + r*ne10, 1, ne10);
  3549. #else
  3550. ggml_vec_add_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  3551. #endif
  3552. }
  3553. }
  3554. } else {
  3555. // src1 is not contiguous
  3556. for (int ir = ir0; ir < ir1; ++ir) {
  3557. // src1 is broadcastable across src0 and dst in i1, i2, i3
  3558. const int64_t i03 = ir/(ne02*ne01);
  3559. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  3560. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  3561. const int64_t i13 = i03 % ne13;
  3562. const int64_t i12 = i02 % ne12;
  3563. const int64_t i11 = i01 % ne11;
  3564. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  3565. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  3566. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  3567. const int64_t i10 = i0 % ne10;
  3568. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  3569. dst_ptr[i0] = src0_ptr[i0] + *src1_ptr;
  3570. }
  3571. }
  3572. }
  3573. }
  3574. static void ggml_compute_forward_add_f16_f32(
  3575. const struct ggml_compute_params * params,
  3576. struct ggml_tensor * dst) {
  3577. const struct ggml_tensor * src0 = dst->src[0];
  3578. const struct ggml_tensor * src1 = dst->src[1];
  3579. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3580. const int ith = params->ith;
  3581. const int nth = params->nth;
  3582. const int nr = ggml_nrows(src0);
  3583. GGML_TENSOR_BINARY_OP_LOCALS
  3584. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3585. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3586. if (dst->type == GGML_TYPE_F32) {
  3587. GGML_ASSERT( nb0 == sizeof(float));
  3588. }
  3589. else {
  3590. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3591. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3592. }
  3593. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3594. // rows per thread
  3595. const int dr = (nr + nth - 1)/nth;
  3596. // row range for this thread
  3597. const int ir0 = dr*ith;
  3598. const int ir1 = MIN(ir0 + dr, nr);
  3599. if (nb10 == sizeof(float)) {
  3600. if (dst->type == GGML_TYPE_F16) {
  3601. for (int ir = ir0; ir < ir1; ++ir) {
  3602. // src0, src1 and dst are same shape => same indices
  3603. const int i3 = ir/(ne2*ne1);
  3604. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3605. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3606. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3607. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3608. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3609. for (int i = 0; i < ne0; i++) {
  3610. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + src1_ptr[i]);
  3611. }
  3612. }
  3613. } else {
  3614. for (int ir = ir0; ir < ir1; ++ir) {
  3615. // src0, src1 and dst are same shape => same indices
  3616. const int i3 = ir/(ne2*ne1);
  3617. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3618. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3619. float * dst_ptr = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3620. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3621. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3622. for (int i = 0; i < ne0; i++) {
  3623. dst_ptr[i] = GGML_FP16_TO_FP32(src0_ptr[i]) + src1_ptr[i];
  3624. }
  3625. }
  3626. }
  3627. }
  3628. else {
  3629. // src1 is not contiguous
  3630. GGML_ABORT("fatal error");
  3631. }
  3632. }
  3633. static void ggml_compute_forward_add_bf16_f32(
  3634. const struct ggml_compute_params * params,
  3635. struct ggml_tensor * dst) {
  3636. const struct ggml_tensor * src0 = dst->src[0];
  3637. const struct ggml_tensor * src1 = dst->src[1];
  3638. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3639. const int ith = params->ith;
  3640. const int nth = params->nth;
  3641. const int nr = ggml_nrows(src0);
  3642. GGML_TENSOR_BINARY_OP_LOCALS
  3643. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  3644. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3645. if (dst->type == GGML_TYPE_F32) {
  3646. GGML_ASSERT( nb0 == sizeof(float));
  3647. }
  3648. else {
  3649. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  3650. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  3651. }
  3652. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  3653. // rows per thread
  3654. const int dr = (nr + nth - 1)/nth;
  3655. // row range for this thread
  3656. const int ir0 = dr*ith;
  3657. const int ir1 = MIN(ir0 + dr, nr);
  3658. if (nb10 == sizeof(float)) {
  3659. if (dst->type == GGML_TYPE_BF16) {
  3660. for (int ir = ir0; ir < ir1; ++ir) {
  3661. // src0, src1 and dst are same shape => same indices
  3662. const int i3 = ir/(ne2*ne1);
  3663. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3664. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3665. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3666. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3667. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3668. for (int i = 0; i < ne0; i++) {
  3669. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + src1_ptr[i]);
  3670. }
  3671. }
  3672. } else {
  3673. for (int ir = ir0; ir < ir1; ++ir) {
  3674. // src0, src1 and dst are same shape => same indices
  3675. const int i3 = ir/(ne2*ne1);
  3676. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3677. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3678. float * dst_ptr = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3679. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3680. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3681. for (int i = 0; i < ne0; i++) {
  3682. dst_ptr[i] = GGML_BF16_TO_FP32(src0_ptr[i]) + src1_ptr[i];
  3683. }
  3684. }
  3685. }
  3686. }
  3687. else {
  3688. // src1 is not contiguous
  3689. GGML_ABORT("fatal error");
  3690. }
  3691. }
  3692. static void ggml_compute_forward_add_f16_f16(
  3693. const struct ggml_compute_params * params,
  3694. struct ggml_tensor * dst) {
  3695. const struct ggml_tensor * src0 = dst->src[0];
  3696. const struct ggml_tensor * src1 = dst->src[1];
  3697. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3698. const int ith = params->ith;
  3699. const int nth = params->nth;
  3700. const int nr = ggml_nrows(src0);
  3701. GGML_TENSOR_BINARY_OP_LOCALS
  3702. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3703. GGML_ASSERT(src1->type == GGML_TYPE_F16);
  3704. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3705. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3706. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3707. // rows per thread
  3708. const int dr = (nr + nth - 1)/nth;
  3709. // row range for this thread
  3710. const int ir0 = dr*ith;
  3711. const int ir1 = MIN(ir0 + dr, nr);
  3712. if (nb10 == sizeof(ggml_fp16_t)) {
  3713. for (int ir = ir0; ir < ir1; ++ir) {
  3714. // src0, src1 and dst are same shape => same indices
  3715. const int i3 = ir/(ne2*ne1);
  3716. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3717. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3718. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3719. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3720. ggml_fp16_t * src1_ptr = (ggml_fp16_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3721. for (int i = 0; i < ne0; i++) {
  3722. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + GGML_FP16_TO_FP32(src1_ptr[i]));
  3723. }
  3724. }
  3725. }
  3726. else {
  3727. // src1 is not contiguous
  3728. GGML_ABORT("fatal error");
  3729. }
  3730. }
  3731. static void ggml_compute_forward_add_bf16_bf16(
  3732. const struct ggml_compute_params * params,
  3733. struct ggml_tensor * dst) {
  3734. const struct ggml_tensor * src0 = dst->src[0];
  3735. const struct ggml_tensor * src1 = dst->src[1];
  3736. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3737. const int ith = params->ith;
  3738. const int nth = params->nth;
  3739. const int nr = ggml_nrows(src0);
  3740. GGML_TENSOR_BINARY_OP_LOCALS
  3741. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  3742. GGML_ASSERT(src1->type == GGML_TYPE_BF16);
  3743. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  3744. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  3745. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  3746. // rows per thread
  3747. const int dr = (nr + nth - 1)/nth;
  3748. // row range for this thread
  3749. const int ir0 = dr*ith;
  3750. const int ir1 = MIN(ir0 + dr, nr);
  3751. if (nb10 == sizeof(ggml_bf16_t)) {
  3752. for (int ir = ir0; ir < ir1; ++ir) {
  3753. // src0, src1 and dst are same shape => same indices
  3754. const int i3 = ir/(ne2*ne1);
  3755. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3756. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3757. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3758. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3759. ggml_bf16_t * src1_ptr = (ggml_bf16_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3760. for (int i = 0; i < ne0; i++) {
  3761. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + GGML_BF16_TO_FP32(src1_ptr[i]));
  3762. }
  3763. }
  3764. }
  3765. else {
  3766. // src1 is not contiguous
  3767. GGML_ABORT("fatal error");
  3768. }
  3769. }
  3770. static void ggml_compute_forward_add_q_f32(
  3771. const struct ggml_compute_params * params,
  3772. struct ggml_tensor * dst) {
  3773. const struct ggml_tensor * src0 = dst->src[0];
  3774. const struct ggml_tensor * src1 = dst->src[1];
  3775. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3776. const int nr = ggml_nrows(src0);
  3777. GGML_TENSOR_BINARY_OP_LOCALS
  3778. const int ith = params->ith;
  3779. const int nth = params->nth;
  3780. const enum ggml_type type = src0->type;
  3781. const enum ggml_type dtype = dst->type;
  3782. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  3783. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dtype)->from_float;
  3784. // we don't support permuted src0 or src1
  3785. GGML_ASSERT(nb00 == ggml_type_size(type));
  3786. GGML_ASSERT(nb10 == sizeof(float));
  3787. // dst cannot be transposed or permuted
  3788. GGML_ASSERT(nb0 <= nb1);
  3789. GGML_ASSERT(nb1 <= nb2);
  3790. GGML_ASSERT(nb2 <= nb3);
  3791. GGML_ASSERT(ggml_is_quantized(src0->type));
  3792. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3793. // rows per thread
  3794. const int dr = (nr + nth - 1)/nth;
  3795. // row range for this thread
  3796. const int ir0 = dr*ith;
  3797. const int ir1 = MIN(ir0 + dr, nr);
  3798. float * wdata = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
  3799. for (int ir = ir0; ir < ir1; ++ir) {
  3800. // src0 indices
  3801. const int i03 = ir/(ne02*ne01);
  3802. const int i02 = (ir - i03*ne02*ne01)/ne01;
  3803. const int i01 = (ir - i03*ne02*ne01 - i02*ne01);
  3804. // src1 and dst are same shape as src0 => same indices
  3805. const int i13 = i03;
  3806. const int i12 = i02;
  3807. const int i11 = i01;
  3808. const int i3 = i03;
  3809. const int i2 = i02;
  3810. const int i1 = i01;
  3811. void * src0_row = (void *) ((char *) src0->data + (i01*nb01 + i02*nb02 + i03*nb03));
  3812. float * src1_row = (float *)((char *) src1->data + (i11*nb11 + i12*nb12 + i13*nb13));
  3813. void * dst_row = (void *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  3814. assert(ne00 % 32 == 0);
  3815. // unquantize row from src0 to temp buffer
  3816. dequantize_row_q(src0_row, wdata, ne00);
  3817. // add src1
  3818. ggml_vec_acc_f32(ne00, wdata, src1_row);
  3819. // quantize row to dst
  3820. if (quantize_row_q != NULL) {
  3821. quantize_row_q(wdata, dst_row, ne00);
  3822. } else {
  3823. memcpy(dst_row, wdata, ne0*nb0);
  3824. }
  3825. }
  3826. }
  3827. static void ggml_compute_forward_add(
  3828. const struct ggml_compute_params * params,
  3829. struct ggml_tensor * dst) {
  3830. const struct ggml_tensor * src0 = dst->src[0];
  3831. const struct ggml_tensor * src1 = dst->src[1];
  3832. switch (src0->type) {
  3833. case GGML_TYPE_F32:
  3834. {
  3835. if (src1->type == GGML_TYPE_F32) {
  3836. ggml_compute_forward_add_f32(params, dst);
  3837. }
  3838. else {
  3839. GGML_ABORT("fatal error");
  3840. }
  3841. } break;
  3842. case GGML_TYPE_F16:
  3843. {
  3844. if (src1->type == GGML_TYPE_F16) {
  3845. ggml_compute_forward_add_f16_f16(params, dst);
  3846. }
  3847. else if (src1->type == GGML_TYPE_F32) {
  3848. ggml_compute_forward_add_f16_f32(params, dst);
  3849. }
  3850. else {
  3851. GGML_ABORT("fatal error");
  3852. }
  3853. } break;
  3854. case GGML_TYPE_BF16:
  3855. {
  3856. if (src1->type == GGML_TYPE_BF16) {
  3857. ggml_compute_forward_add_bf16_bf16(params, dst);
  3858. }
  3859. else if (src1->type == GGML_TYPE_F32) {
  3860. ggml_compute_forward_add_bf16_f32(params, dst);
  3861. }
  3862. else {
  3863. GGML_ABORT("fatal error");
  3864. }
  3865. } break;
  3866. case GGML_TYPE_Q4_0:
  3867. case GGML_TYPE_Q4_1:
  3868. case GGML_TYPE_Q5_0:
  3869. case GGML_TYPE_Q5_1:
  3870. case GGML_TYPE_Q8_0:
  3871. case GGML_TYPE_Q2_K:
  3872. case GGML_TYPE_Q3_K:
  3873. case GGML_TYPE_Q4_K:
  3874. case GGML_TYPE_Q5_K:
  3875. case GGML_TYPE_Q6_K:
  3876. case GGML_TYPE_TQ1_0:
  3877. case GGML_TYPE_TQ2_0:
  3878. case GGML_TYPE_IQ2_XXS:
  3879. case GGML_TYPE_IQ2_XS:
  3880. case GGML_TYPE_IQ3_XXS:
  3881. case GGML_TYPE_IQ1_S:
  3882. case GGML_TYPE_IQ1_M:
  3883. case GGML_TYPE_IQ4_NL:
  3884. case GGML_TYPE_IQ4_XS:
  3885. case GGML_TYPE_IQ3_S:
  3886. case GGML_TYPE_IQ2_S:
  3887. {
  3888. ggml_compute_forward_add_q_f32(params, dst);
  3889. } break;
  3890. default:
  3891. {
  3892. GGML_ABORT("fatal error");
  3893. }
  3894. }
  3895. }
  3896. // ggml_compute_forward_add1
  3897. static void ggml_compute_forward_add1_f32(
  3898. const struct ggml_compute_params * params,
  3899. struct ggml_tensor * dst) {
  3900. const struct ggml_tensor * src0 = dst->src[0];
  3901. const struct ggml_tensor * src1 = dst->src[1];
  3902. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  3903. GGML_ASSERT(ggml_is_scalar(src1));
  3904. const int ith = params->ith;
  3905. const int nth = params->nth;
  3906. const int nr = ggml_nrows(src0);
  3907. GGML_TENSOR_UNARY_OP_LOCALS
  3908. GGML_ASSERT( nb0 == sizeof(float));
  3909. GGML_ASSERT(nb00 == sizeof(float));
  3910. // rows per thread
  3911. const int dr = (nr + nth - 1)/nth;
  3912. // row range for this thread
  3913. const int ir0 = dr*ith;
  3914. const int ir1 = MIN(ir0 + dr, nr);
  3915. for (int ir = ir0; ir < ir1; ++ir) {
  3916. // src0 and dst are same shape => same indices
  3917. const int i3 = ir/(ne2*ne1);
  3918. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3919. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3920. #ifdef GGML_USE_ACCELERATE
  3921. UNUSED(ggml_vec_add1_f32);
  3922. vDSP_vadd(
  3923. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), 1,
  3924. (float *) ((char *) src1->data), 0,
  3925. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), 1,
  3926. ne0);
  3927. #else
  3928. ggml_vec_add1_f32(ne0,
  3929. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ),
  3930. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01),
  3931. *(float *) src1->data);
  3932. #endif
  3933. }
  3934. }
  3935. static void ggml_compute_forward_add1_f16_f32(
  3936. const struct ggml_compute_params * params,
  3937. struct ggml_tensor * dst) {
  3938. const struct ggml_tensor * src0 = dst->src[0];
  3939. const struct ggml_tensor * src1 = dst->src[1];
  3940. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  3941. GGML_ASSERT(ggml_is_scalar(src1));
  3942. // scalar to add
  3943. const float v = *(float *) src1->data;
  3944. const int ith = params->ith;
  3945. const int nth = params->nth;
  3946. const int nr = ggml_nrows(src0);
  3947. GGML_TENSOR_UNARY_OP_LOCALS
  3948. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3949. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3950. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3951. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3952. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3953. // rows per thread
  3954. const int dr = (nr + nth - 1)/nth;
  3955. // row range for this thread
  3956. const int ir0 = dr*ith;
  3957. const int ir1 = MIN(ir0 + dr, nr);
  3958. for (int ir = ir0; ir < ir1; ++ir) {
  3959. // src0 and dst are same shape => same indices
  3960. const int i3 = ir/(ne2*ne1);
  3961. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3962. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3963. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  3964. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3965. for (int i = 0; i < ne0; i++) {
  3966. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + v);
  3967. }
  3968. }
  3969. }
  3970. static void ggml_compute_forward_add1_f16_f16(
  3971. const struct ggml_compute_params * params,
  3972. struct ggml_tensor * dst) {
  3973. const struct ggml_tensor * src0 = dst->src[0];
  3974. const struct ggml_tensor * src1 = dst->src[1];
  3975. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  3976. GGML_ASSERT(ggml_is_scalar(src1));
  3977. // scalar to add
  3978. const float v = GGML_FP16_TO_FP32(*(ggml_fp16_t *) src1->data);
  3979. const int ith = params->ith;
  3980. const int nth = params->nth;
  3981. const int nr = ggml_nrows(src0);
  3982. GGML_TENSOR_UNARY_OP_LOCALS
  3983. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3984. GGML_ASSERT(src1->type == GGML_TYPE_F16);
  3985. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3986. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3987. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3988. // rows per thread
  3989. const int dr = (nr + nth - 1)/nth;
  3990. // row range for this thread
  3991. const int ir0 = dr*ith;
  3992. const int ir1 = MIN(ir0 + dr, nr);
  3993. for (int ir = ir0; ir < ir1; ++ir) {
  3994. // src0 and dst are same shape => same indices
  3995. const int i3 = ir/(ne2*ne1);
  3996. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3997. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3998. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  3999. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4000. for (int i = 0; i < ne0; i++) {
  4001. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + v);
  4002. }
  4003. }
  4004. }
  4005. static void ggml_compute_forward_add1_q_f32(
  4006. const struct ggml_compute_params * params,
  4007. struct ggml_tensor * dst) {
  4008. const struct ggml_tensor * src0 = dst->src[0];
  4009. const struct ggml_tensor * src1 = dst->src[1];
  4010. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4011. GGML_ASSERT(ggml_is_scalar(src1));
  4012. // scalar to add
  4013. const float v = *(float *) src1->data;
  4014. const int ith = params->ith;
  4015. const int nth = params->nth;
  4016. const int nr = ggml_nrows(src0);
  4017. GGML_TENSOR_UNARY_OP_LOCALS
  4018. const enum ggml_type type = src0->type;
  4019. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  4020. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(type)->from_float;
  4021. // we don't support permuted src0
  4022. GGML_ASSERT(nb00 == ggml_type_size(type));
  4023. // dst cannot be transposed or permuted
  4024. GGML_ASSERT(nb0 <= nb1);
  4025. GGML_ASSERT(nb1 <= nb2);
  4026. GGML_ASSERT(nb2 <= nb3);
  4027. GGML_ASSERT(ggml_is_quantized(src0->type));
  4028. GGML_ASSERT(dst->type == src0->type);
  4029. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4030. // rows per thread
  4031. const int dr = (nr + nth - 1)/nth;
  4032. // row range for this thread
  4033. const int ir0 = dr*ith;
  4034. const int ir1 = MIN(ir0 + dr, nr);
  4035. float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith;
  4036. for (int ir = ir0; ir < ir1; ++ir) {
  4037. // src0 and dst are same shape => same indices
  4038. const int i3 = ir/(ne2*ne1);
  4039. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4040. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4041. void * src0_row = (void *) ((char *) src0->data + (i1*nb01 + i2*nb02 + i3*nb03));
  4042. void * dst_row = (void *) ((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb0 ));
  4043. assert(ne0 % 32 == 0);
  4044. // unquantize row from src0 to temp buffer
  4045. dequantize_row_q(src0_row, wdata, ne0);
  4046. // add src1
  4047. ggml_vec_acc1_f32(ne0, wdata, v);
  4048. // quantize row to dst
  4049. quantize_row_q(wdata, dst_row, ne0);
  4050. }
  4051. }
  4052. static void ggml_compute_forward_add1_bf16_f32(
  4053. const struct ggml_compute_params * params,
  4054. struct ggml_tensor * dst) {
  4055. const struct ggml_tensor * src0 = dst->src[0];
  4056. const struct ggml_tensor * src1 = dst->src[1];
  4057. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4058. GGML_ASSERT(ggml_is_scalar(src1));
  4059. // scalar to add
  4060. const float v = *(float *) src1->data;
  4061. const int ith = params->ith;
  4062. const int nth = params->nth;
  4063. const int nr = ggml_nrows(src0);
  4064. GGML_TENSOR_UNARY_OP_LOCALS
  4065. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  4066. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4067. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  4068. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  4069. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  4070. // rows per thread
  4071. const int dr = (nr + nth - 1)/nth;
  4072. // row range for this thread
  4073. const int ir0 = dr*ith;
  4074. const int ir1 = MIN(ir0 + dr, nr);
  4075. for (int ir = ir0; ir < ir1; ++ir) {
  4076. // src0 and dst are same shape => same indices
  4077. const int i3 = ir/(ne2*ne1);
  4078. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4079. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4080. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  4081. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4082. for (int i = 0; i < ne0; i++) {
  4083. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v);
  4084. }
  4085. }
  4086. }
  4087. static void ggml_compute_forward_add1_bf16_bf16(
  4088. const struct ggml_compute_params * params,
  4089. struct ggml_tensor * dst) {
  4090. const struct ggml_tensor * src0 = dst->src[0];
  4091. const struct ggml_tensor * src1 = dst->src[1];
  4092. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4093. GGML_ASSERT(ggml_is_scalar(src1));
  4094. // scalar to add
  4095. const float v = GGML_BF16_TO_FP32(*(ggml_bf16_t *) src1->data);
  4096. const int ith = params->ith;
  4097. const int nth = params->nth;
  4098. const int nr = ggml_nrows(src0);
  4099. GGML_TENSOR_UNARY_OP_LOCALS
  4100. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  4101. GGML_ASSERT(src1->type == GGML_TYPE_BF16);
  4102. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  4103. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  4104. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  4105. // rows per thread
  4106. const int dr = (nr + nth - 1)/nth;
  4107. // row range for this thread
  4108. const int ir0 = dr*ith;
  4109. const int ir1 = MIN(ir0 + dr, nr);
  4110. for (int ir = ir0; ir < ir1; ++ir) {
  4111. // src0 and dst are same shape => same indices
  4112. const int i3 = ir/(ne2*ne1);
  4113. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4114. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4115. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  4116. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4117. for (int i = 0; i < ne0; i++) {
  4118. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v);
  4119. }
  4120. }
  4121. }
  4122. static void ggml_compute_forward_add1(
  4123. const struct ggml_compute_params * params,
  4124. struct ggml_tensor * dst) {
  4125. const struct ggml_tensor * src0 = dst->src[0];
  4126. const struct ggml_tensor * src1 = dst->src[1];
  4127. switch (src0->type) {
  4128. case GGML_TYPE_F32:
  4129. {
  4130. ggml_compute_forward_add1_f32(params, dst);
  4131. } break;
  4132. case GGML_TYPE_F16:
  4133. {
  4134. if (src1->type == GGML_TYPE_F16) {
  4135. ggml_compute_forward_add1_f16_f16(params, dst);
  4136. }
  4137. else if (src1->type == GGML_TYPE_F32) {
  4138. ggml_compute_forward_add1_f16_f32(params, dst);
  4139. }
  4140. else {
  4141. GGML_ABORT("fatal error");
  4142. }
  4143. } break;
  4144. case GGML_TYPE_BF16:
  4145. {
  4146. if (src1->type == GGML_TYPE_BF16) {
  4147. ggml_compute_forward_add1_bf16_bf16(params, dst);
  4148. }
  4149. else if (src1->type == GGML_TYPE_F32) {
  4150. ggml_compute_forward_add1_bf16_f32(params, dst);
  4151. }
  4152. else {
  4153. GGML_ABORT("fatal error");
  4154. }
  4155. } break;
  4156. case GGML_TYPE_Q4_0:
  4157. case GGML_TYPE_Q4_1:
  4158. case GGML_TYPE_Q5_0:
  4159. case GGML_TYPE_Q5_1:
  4160. case GGML_TYPE_Q8_0:
  4161. case GGML_TYPE_Q8_1:
  4162. case GGML_TYPE_Q2_K:
  4163. case GGML_TYPE_Q3_K:
  4164. case GGML_TYPE_Q4_K:
  4165. case GGML_TYPE_Q5_K:
  4166. case GGML_TYPE_Q6_K:
  4167. case GGML_TYPE_TQ1_0:
  4168. case GGML_TYPE_TQ2_0:
  4169. case GGML_TYPE_IQ2_XXS:
  4170. case GGML_TYPE_IQ2_XS:
  4171. case GGML_TYPE_IQ3_XXS:
  4172. case GGML_TYPE_IQ1_S:
  4173. case GGML_TYPE_IQ1_M:
  4174. case GGML_TYPE_IQ4_NL:
  4175. case GGML_TYPE_IQ4_XS:
  4176. case GGML_TYPE_IQ3_S:
  4177. case GGML_TYPE_IQ2_S:
  4178. {
  4179. ggml_compute_forward_add1_q_f32(params, dst);
  4180. } break;
  4181. default:
  4182. {
  4183. GGML_ABORT("fatal error");
  4184. }
  4185. }
  4186. }
  4187. // ggml_compute_forward_acc
  4188. static void ggml_compute_forward_acc_f32(
  4189. const struct ggml_compute_params * params,
  4190. struct ggml_tensor * dst) {
  4191. const struct ggml_tensor * src0 = dst->src[0];
  4192. const struct ggml_tensor * src1 = dst->src[1];
  4193. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4194. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  4195. // view src0 and dst with these strides and data offset inbytes during acc
  4196. // nb0 is implicitly element_size because src0 and dst are contiguous
  4197. size_t nb1 = ((int32_t *) dst->op_params)[0];
  4198. size_t nb2 = ((int32_t *) dst->op_params)[1];
  4199. size_t nb3 = ((int32_t *) dst->op_params)[2];
  4200. size_t offset = ((int32_t *) dst->op_params)[3];
  4201. bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  4202. if (!inplace) {
  4203. if (params->ith == 0) {
  4204. // memcpy needs to be synchronized across threads to avoid race conditions.
  4205. // => do it in INIT phase
  4206. memcpy(
  4207. ((char *) dst->data),
  4208. ((char *) src0->data),
  4209. ggml_nbytes(dst));
  4210. }
  4211. ggml_barrier(params->threadpool);
  4212. }
  4213. const int ith = params->ith;
  4214. const int nth = params->nth;
  4215. const int nr = ggml_nrows(src1);
  4216. const int nc = src1->ne[0];
  4217. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne)
  4218. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  4219. // src0 and dst as viewed during acc
  4220. const size_t nb0 = ggml_element_size(src0);
  4221. const size_t nb00 = nb0;
  4222. const size_t nb01 = nb1;
  4223. const size_t nb02 = nb2;
  4224. const size_t nb03 = nb3;
  4225. GGML_ASSERT(offset + (ne10 == 0 ? 0 : ne10-1)*nb0 + (ne11 == 0 ? 0 : ne11-1)*nb1 + (ne12 == 0 ? 0 : ne12-1)*nb2 + (ne13 == 0 ? 0 : ne13-1)*nb3 < ggml_nbytes(dst));
  4226. GGML_ASSERT(offset + (ne10 == 0 ? 0 : ne10-1)*nb00 + (ne11 == 0 ? 0 : ne11-1)*nb01 + (ne12 == 0 ? 0 : ne12-1)*nb02 + (ne13 == 0 ? 0 : ne13-1)*nb03 < ggml_nbytes(src0));
  4227. GGML_ASSERT(nb10 == sizeof(float));
  4228. // rows per thread
  4229. const int dr = (nr + nth - 1)/nth;
  4230. // row range for this thread
  4231. const int ir0 = dr*ith;
  4232. const int ir1 = MIN(ir0 + dr, nr);
  4233. for (int ir = ir0; ir < ir1; ++ir) {
  4234. // src0 and dst are viewed with shape of src1 and offset
  4235. // => same indices
  4236. const int i3 = ir/(ne12*ne11);
  4237. const int i2 = (ir - i3*ne12*ne11)/ne11;
  4238. const int i1 = (ir - i3*ne12*ne11 - i2*ne11);
  4239. #ifdef GGML_USE_ACCELERATE
  4240. vDSP_vadd(
  4241. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset), 1,
  4242. (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11), 1,
  4243. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), 1, nc);
  4244. #else
  4245. ggml_vec_add_f32(nc,
  4246. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset),
  4247. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset),
  4248. (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
  4249. #endif
  4250. }
  4251. }
  4252. static void ggml_compute_forward_acc(
  4253. const struct ggml_compute_params * params,
  4254. struct ggml_tensor * dst) {
  4255. const struct ggml_tensor * src0 = dst->src[0];
  4256. switch (src0->type) {
  4257. case GGML_TYPE_F32:
  4258. {
  4259. ggml_compute_forward_acc_f32(params, dst);
  4260. } break;
  4261. case GGML_TYPE_F16:
  4262. case GGML_TYPE_BF16:
  4263. case GGML_TYPE_Q4_0:
  4264. case GGML_TYPE_Q4_1:
  4265. case GGML_TYPE_Q5_0:
  4266. case GGML_TYPE_Q5_1:
  4267. case GGML_TYPE_Q8_0:
  4268. case GGML_TYPE_Q8_1:
  4269. case GGML_TYPE_Q2_K:
  4270. case GGML_TYPE_Q3_K:
  4271. case GGML_TYPE_Q4_K:
  4272. case GGML_TYPE_Q5_K:
  4273. case GGML_TYPE_Q6_K:
  4274. case GGML_TYPE_TQ1_0:
  4275. case GGML_TYPE_TQ2_0:
  4276. case GGML_TYPE_IQ2_XXS:
  4277. case GGML_TYPE_IQ2_XS:
  4278. case GGML_TYPE_IQ3_XXS:
  4279. case GGML_TYPE_IQ1_S:
  4280. case GGML_TYPE_IQ1_M:
  4281. case GGML_TYPE_IQ4_NL:
  4282. case GGML_TYPE_IQ4_XS:
  4283. case GGML_TYPE_IQ3_S:
  4284. case GGML_TYPE_IQ2_S:
  4285. default:
  4286. {
  4287. GGML_ABORT("fatal error");
  4288. }
  4289. }
  4290. }
  4291. // ggml_compute_forward_sub
  4292. static void ggml_compute_forward_sub_f32(
  4293. const struct ggml_compute_params * params,
  4294. struct ggml_tensor * dst) {
  4295. const struct ggml_tensor * src0 = dst->src[0];
  4296. const struct ggml_tensor * src1 = dst->src[1];
  4297. assert(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  4298. const int ith = params->ith;
  4299. const int nth = params->nth;
  4300. const int nr = ggml_nrows(src0);
  4301. GGML_TENSOR_BINARY_OP_LOCALS
  4302. GGML_ASSERT( nb0 == sizeof(float));
  4303. GGML_ASSERT(nb00 == sizeof(float));
  4304. // rows per thread
  4305. const int dr = (nr + nth - 1)/nth;
  4306. // row range for this thread
  4307. const int ir0 = dr*ith;
  4308. const int ir1 = MIN(ir0 + dr, nr);
  4309. if (nb10 == sizeof(float)) {
  4310. for (int ir = ir0; ir < ir1; ++ir) {
  4311. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4312. const int64_t i03 = ir/(ne02*ne01);
  4313. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4314. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4315. const int64_t i13 = i03 % ne13;
  4316. const int64_t i12 = i02 % ne12;
  4317. const int64_t i11 = i01 % ne11;
  4318. const int64_t nr0 = ne00 / ne10;
  4319. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4320. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4321. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  4322. for (int64_t r = 0; r < nr0; ++r) {
  4323. #ifdef GGML_USE_ACCELERATE
  4324. vDSP_vsub(src1_ptr, 1, src0_ptr + r*ne10, 1, dst_ptr + r*ne10, 1, ne10);
  4325. #else
  4326. ggml_vec_sub_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  4327. #endif
  4328. }
  4329. }
  4330. } else {
  4331. // src1 is not contiguous
  4332. for (int ir = ir0; ir < ir1; ++ir) {
  4333. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4334. const int64_t i03 = ir/(ne02*ne01);
  4335. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4336. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4337. const int64_t i13 = i03 % ne13;
  4338. const int64_t i12 = i02 % ne12;
  4339. const int64_t i11 = i01 % ne11;
  4340. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4341. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4342. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  4343. const int64_t i10 = i0 % ne10;
  4344. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  4345. dst_ptr[i0] = src0_ptr[i0] - *src1_ptr;
  4346. }
  4347. }
  4348. }
  4349. }
  4350. static void ggml_compute_forward_sub(
  4351. const struct ggml_compute_params * params,
  4352. struct ggml_tensor * dst) {
  4353. const struct ggml_tensor * src0 = dst->src[0];
  4354. switch (src0->type) {
  4355. case GGML_TYPE_F32:
  4356. {
  4357. ggml_compute_forward_sub_f32(params, dst);
  4358. } break;
  4359. default:
  4360. {
  4361. GGML_ABORT("fatal error");
  4362. }
  4363. }
  4364. }
  4365. // ggml_compute_forward_mul
  4366. static void ggml_compute_forward_mul_f32(
  4367. const struct ggml_compute_params * params,
  4368. struct ggml_tensor * dst) {
  4369. const struct ggml_tensor * src0 = dst->src[0];
  4370. const struct ggml_tensor * src1 = dst->src[1];
  4371. GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  4372. const int ith = params->ith;
  4373. const int nth = params->nth;
  4374. const int64_t nr = ggml_nrows(src0);
  4375. GGML_TENSOR_BINARY_OP_LOCALS
  4376. GGML_ASSERT( nb0 == sizeof(float));
  4377. GGML_ASSERT(nb00 == sizeof(float));
  4378. if (nb10 == sizeof(float)) {
  4379. for (int64_t ir = ith; ir < nr; ir += nth) {
  4380. // src0 and dst are same shape => same indices
  4381. const int64_t i03 = ir/(ne02*ne01);
  4382. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4383. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4384. const int64_t i13 = i03 % ne13;
  4385. const int64_t i12 = i02 % ne12;
  4386. const int64_t i11 = i01 % ne11;
  4387. const int64_t nr0 = ne00 / ne10;
  4388. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4389. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4390. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  4391. for (int64_t r = 0 ; r < nr0; ++r) {
  4392. #ifdef GGML_USE_ACCELERATE
  4393. UNUSED(ggml_vec_mul_f32);
  4394. vDSP_vmul(src0_ptr + r*ne10, 1, src1_ptr, 1, dst_ptr + r*ne10, 1, ne10);
  4395. #else
  4396. ggml_vec_mul_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  4397. #endif
  4398. }
  4399. }
  4400. } else {
  4401. // src1 is not contiguous
  4402. for (int64_t ir = ith; ir < nr; ir += nth) {
  4403. // src0 and dst are same shape => same indices
  4404. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4405. const int64_t i03 = ir/(ne02*ne01);
  4406. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4407. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4408. const int64_t i13 = i03 % ne13;
  4409. const int64_t i12 = i02 % ne12;
  4410. const int64_t i11 = i01 % ne11;
  4411. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4412. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4413. for (int64_t i0 = 0; i0 < ne00; ++i0) {
  4414. const int64_t i10 = i0 % ne10;
  4415. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  4416. dst_ptr[i0] = src0_ptr[i0] * (*src1_ptr);
  4417. }
  4418. }
  4419. }
  4420. }
  4421. static void ggml_compute_forward_mul(
  4422. const struct ggml_compute_params * params,
  4423. struct ggml_tensor * dst) {
  4424. const struct ggml_tensor * src0 = dst->src[0];
  4425. const struct ggml_tensor * src1 = dst->src[1];
  4426. GGML_ASSERT(src1->type == GGML_TYPE_F32 && "only f32 src1 supported for now");
  4427. switch (src0->type) {
  4428. case GGML_TYPE_F32:
  4429. {
  4430. ggml_compute_forward_mul_f32(params, dst);
  4431. } break;
  4432. default:
  4433. {
  4434. GGML_ABORT("fatal error");
  4435. }
  4436. }
  4437. }
  4438. // ggml_compute_forward_div
  4439. static void ggml_compute_forward_div_f32(
  4440. const struct ggml_compute_params * params,
  4441. struct ggml_tensor * dst) {
  4442. const struct ggml_tensor * src0 = dst->src[0];
  4443. const struct ggml_tensor * src1 = dst->src[1];
  4444. GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  4445. const int ith = params->ith;
  4446. const int nth = params->nth;
  4447. const int64_t nr = ggml_nrows(src0);
  4448. GGML_TENSOR_BINARY_OP_LOCALS
  4449. GGML_ASSERT( nb0 == sizeof(float));
  4450. GGML_ASSERT(nb00 == sizeof(float));
  4451. if (nb10 == sizeof(float)) {
  4452. for (int64_t ir = ith; ir < nr; ir += nth) {
  4453. // src0 and dst are same shape => same indices
  4454. const int64_t i03 = ir/(ne02*ne01);
  4455. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4456. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4457. const int64_t i13 = i03 % ne13;
  4458. const int64_t i12 = i02 % ne12;
  4459. const int64_t i11 = i01 % ne11;
  4460. const int64_t nr0 = ne00 / ne10;
  4461. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4462. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4463. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  4464. for (int64_t r = 0; r < nr0; ++r) {
  4465. #ifdef GGML_USE_ACCELERATE
  4466. UNUSED(ggml_vec_div_f32);
  4467. vDSP_vdiv(src1_ptr, 1, src0_ptr + r*ne10, 1, dst_ptr + r*ne10, 1, ne10);
  4468. #else
  4469. ggml_vec_div_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  4470. #endif
  4471. }
  4472. }
  4473. } else {
  4474. // src1 is not contiguous
  4475. for (int64_t ir = ith; ir < nr; ir += nth) {
  4476. // src0 and dst are same shape => same indices
  4477. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4478. const int64_t i03 = ir/(ne02*ne01);
  4479. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4480. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4481. const int64_t i13 = i03 % ne13;
  4482. const int64_t i12 = i02 % ne12;
  4483. const int64_t i11 = i01 % ne11;
  4484. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4485. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4486. for (int64_t i0 = 0; i0 < ne00; ++i0) {
  4487. const int64_t i10 = i0 % ne10;
  4488. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  4489. dst_ptr[i0] = src0_ptr[i0] / (*src1_ptr);
  4490. }
  4491. }
  4492. }
  4493. }
  4494. static void ggml_compute_forward_div(
  4495. const struct ggml_compute_params * params,
  4496. struct ggml_tensor * dst) {
  4497. const struct ggml_tensor * src0 = dst->src[0];
  4498. switch (src0->type) {
  4499. case GGML_TYPE_F32:
  4500. {
  4501. ggml_compute_forward_div_f32(params, dst);
  4502. } break;
  4503. default:
  4504. {
  4505. GGML_ABORT("fatal error");
  4506. }
  4507. }
  4508. }
  4509. // ggml_compute_forward_sqr
  4510. static void ggml_compute_forward_sqr_f32(
  4511. const struct ggml_compute_params * params,
  4512. struct ggml_tensor * dst) {
  4513. const struct ggml_tensor * src0 = dst->src[0];
  4514. if (params->ith != 0) {
  4515. return;
  4516. }
  4517. assert(ggml_are_same_shape(src0, dst));
  4518. const int n = ggml_nrows(src0);
  4519. const int nc = src0->ne[0];
  4520. assert( dst->nb[0] == sizeof(float));
  4521. assert(src0->nb[0] == sizeof(float));
  4522. for (int i = 0; i < n; i++) {
  4523. ggml_vec_sqr_f32(nc,
  4524. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4525. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4526. }
  4527. }
  4528. static void ggml_compute_forward_sqr(
  4529. const struct ggml_compute_params * params,
  4530. struct ggml_tensor * dst) {
  4531. const struct ggml_tensor * src0 = dst->src[0];
  4532. switch (src0->type) {
  4533. case GGML_TYPE_F32:
  4534. {
  4535. ggml_compute_forward_sqr_f32(params, dst);
  4536. } break;
  4537. default:
  4538. {
  4539. GGML_ABORT("fatal error");
  4540. }
  4541. }
  4542. }
  4543. // ggml_compute_forward_sqrt
  4544. static void ggml_compute_forward_sqrt_f32(
  4545. const struct ggml_compute_params * params,
  4546. struct ggml_tensor * dst) {
  4547. const struct ggml_tensor * src0 = dst->src[0];
  4548. if (params->ith != 0) {
  4549. return;
  4550. }
  4551. assert(ggml_are_same_shape(src0, dst));
  4552. const int n = ggml_nrows(src0);
  4553. const int nc = src0->ne[0];
  4554. assert( dst->nb[0] == sizeof(float));
  4555. assert(src0->nb[0] == sizeof(float));
  4556. for (int i = 0; i < n; i++) {
  4557. ggml_vec_sqrt_f32(nc,
  4558. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4559. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4560. }
  4561. }
  4562. static void ggml_compute_forward_sqrt(
  4563. const struct ggml_compute_params * params,
  4564. struct ggml_tensor * dst) {
  4565. const struct ggml_tensor * src0 = dst->src[0];
  4566. switch (src0->type) {
  4567. case GGML_TYPE_F32:
  4568. {
  4569. ggml_compute_forward_sqrt_f32(params, dst);
  4570. } break;
  4571. default:
  4572. {
  4573. GGML_ABORT("fatal error");
  4574. }
  4575. }
  4576. }
  4577. // ggml_compute_forward_log
  4578. static void ggml_compute_forward_log_f32(
  4579. const struct ggml_compute_params * params,
  4580. struct ggml_tensor * dst) {
  4581. const struct ggml_tensor * src0 = dst->src[0];
  4582. if (params->ith != 0) {
  4583. return;
  4584. }
  4585. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4586. const int n = ggml_nrows(src0);
  4587. const int nc = src0->ne[0];
  4588. GGML_ASSERT( dst->nb[0] == sizeof(float));
  4589. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4590. for (int i = 0; i < n; i++) {
  4591. ggml_vec_log_f32(nc,
  4592. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4593. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4594. }
  4595. }
  4596. static void ggml_compute_forward_log(
  4597. const struct ggml_compute_params * params,
  4598. struct ggml_tensor * dst) {
  4599. const struct ggml_tensor * src0 = dst->src[0];
  4600. switch (src0->type) {
  4601. case GGML_TYPE_F32:
  4602. {
  4603. ggml_compute_forward_log_f32(params, dst);
  4604. } break;
  4605. default:
  4606. {
  4607. GGML_ABORT("fatal error");
  4608. }
  4609. }
  4610. }
  4611. // ggml_compute_forward_sin
  4612. static void ggml_compute_forward_sin_f32(
  4613. const struct ggml_compute_params * params,
  4614. struct ggml_tensor * dst) {
  4615. const struct ggml_tensor * src0 = dst->src[0];
  4616. if (params->ith != 0) {
  4617. return;
  4618. }
  4619. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4620. const int n = ggml_nrows(src0);
  4621. const int nc = src0->ne[0];
  4622. GGML_ASSERT( dst->nb[0] == sizeof(float));
  4623. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4624. for (int i = 0; i < n; i++) {
  4625. ggml_vec_sin_f32(nc,
  4626. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4627. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4628. }
  4629. }
  4630. static void ggml_compute_forward_sin(
  4631. const struct ggml_compute_params * params,
  4632. struct ggml_tensor * dst) {
  4633. const struct ggml_tensor * src0 = dst->src[0];
  4634. switch (src0->type) {
  4635. case GGML_TYPE_F32:
  4636. {
  4637. ggml_compute_forward_sin_f32(params, dst);
  4638. } break;
  4639. default:
  4640. {
  4641. GGML_ABORT("fatal error");
  4642. }
  4643. }
  4644. }
  4645. // ggml_compute_forward_cos
  4646. static void ggml_compute_forward_cos_f32(
  4647. const struct ggml_compute_params * params,
  4648. struct ggml_tensor * dst) {
  4649. const struct ggml_tensor * src0 = dst->src[0];
  4650. if (params->ith != 0) {
  4651. return;
  4652. }
  4653. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4654. const int n = ggml_nrows(src0);
  4655. const int nc = src0->ne[0];
  4656. GGML_ASSERT( dst->nb[0] == sizeof(float));
  4657. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4658. for (int i = 0; i < n; i++) {
  4659. ggml_vec_cos_f32(nc,
  4660. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4661. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4662. }
  4663. }
  4664. static void ggml_compute_forward_cos(
  4665. const struct ggml_compute_params * params,
  4666. struct ggml_tensor * dst) {
  4667. const struct ggml_tensor * src0 = dst->src[0];
  4668. switch (src0->type) {
  4669. case GGML_TYPE_F32:
  4670. {
  4671. ggml_compute_forward_cos_f32(params, dst);
  4672. } break;
  4673. default:
  4674. {
  4675. GGML_ABORT("fatal error");
  4676. }
  4677. }
  4678. }
  4679. // ggml_compute_forward_sum
  4680. static void ggml_compute_forward_sum_f32(
  4681. const struct ggml_compute_params * params,
  4682. struct ggml_tensor * dst) {
  4683. const struct ggml_tensor * src0 = dst->src[0];
  4684. if (params->ith != 0) {
  4685. return;
  4686. }
  4687. assert(ggml_is_scalar(dst));
  4688. assert(src0->nb[0] == sizeof(float));
  4689. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  4690. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb)
  4691. ggml_float sum = 0;
  4692. ggml_float row_sum = 0;
  4693. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4694. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4695. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4696. ggml_vec_sum_f32_ggf(ne00,
  4697. &row_sum,
  4698. (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03));
  4699. sum += row_sum;
  4700. }
  4701. }
  4702. }
  4703. ((float *) dst->data)[0] = sum;
  4704. }
  4705. static void ggml_compute_forward_sum_f16(
  4706. const struct ggml_compute_params * params,
  4707. struct ggml_tensor * dst) {
  4708. const struct ggml_tensor * src0 = dst->src[0];
  4709. if (params->ith != 0) {
  4710. return;
  4711. }
  4712. assert(ggml_is_scalar(dst));
  4713. assert(src0->nb[0] == sizeof(ggml_fp16_t));
  4714. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  4715. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb)
  4716. float sum = 0;
  4717. float row_sum = 0;
  4718. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4719. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4720. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4721. ggml_vec_sum_f16_ggf(ne00,
  4722. &row_sum,
  4723. (ggml_fp16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03));
  4724. sum += row_sum;
  4725. }
  4726. }
  4727. }
  4728. ((ggml_fp16_t *) dst->data)[0] = GGML_FP32_TO_FP16(sum);
  4729. }
  4730. static void ggml_compute_forward_sum_bf16(
  4731. const struct ggml_compute_params * params,
  4732. struct ggml_tensor * dst) {
  4733. const struct ggml_tensor * src0 = dst->src[0];
  4734. if (params->ith != 0) {
  4735. return;
  4736. }
  4737. assert(ggml_is_scalar(dst));
  4738. assert(src0->nb[0] == sizeof(ggml_bf16_t));
  4739. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  4740. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb)
  4741. float sum = 0;
  4742. float row_sum = 0;
  4743. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4744. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4745. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4746. ggml_vec_sum_bf16_ggf(ne00,
  4747. &row_sum,
  4748. (ggml_bf16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03));
  4749. sum += row_sum;
  4750. }
  4751. }
  4752. }
  4753. ((ggml_bf16_t *) dst->data)[0] = GGML_FP32_TO_BF16(sum);
  4754. }
  4755. static void ggml_compute_forward_sum(
  4756. const struct ggml_compute_params * params,
  4757. struct ggml_tensor * dst) {
  4758. const struct ggml_tensor * src0 = dst->src[0];
  4759. switch (src0->type) {
  4760. case GGML_TYPE_F32:
  4761. {
  4762. ggml_compute_forward_sum_f32(params, dst);
  4763. } break;
  4764. case GGML_TYPE_F16:
  4765. {
  4766. ggml_compute_forward_sum_f16(params, dst);
  4767. } break;
  4768. case GGML_TYPE_BF16:
  4769. {
  4770. ggml_compute_forward_sum_bf16(params, dst);
  4771. } break;
  4772. default:
  4773. {
  4774. GGML_ABORT("fatal error");
  4775. }
  4776. }
  4777. }
  4778. // ggml_compute_forward_sum_rows
  4779. static void ggml_compute_forward_sum_rows_f32(
  4780. const struct ggml_compute_params * params,
  4781. struct ggml_tensor * dst) {
  4782. const struct ggml_tensor * src0 = dst->src[0];
  4783. if (params->ith != 0) {
  4784. return;
  4785. }
  4786. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4787. GGML_ASSERT(dst->nb[0] == sizeof(float));
  4788. GGML_TENSOR_UNARY_OP_LOCALS
  4789. GGML_ASSERT(ne0 == 1);
  4790. GGML_ASSERT(ne1 == ne01);
  4791. GGML_ASSERT(ne2 == ne02);
  4792. GGML_ASSERT(ne3 == ne03);
  4793. for (int64_t i3 = 0; i3 < ne03; i3++) {
  4794. for (int64_t i2 = 0; i2 < ne02; i2++) {
  4795. for (int64_t i1 = 0; i1 < ne01; i1++) {
  4796. float * src_row = (float *) ((char *) src0->data + i1*nb01 + i2*nb02 + i3*nb03);
  4797. float * dst_row = (float *) ((char *) dst->data + i1*nb1 + i2*nb2 + i3*nb3);
  4798. float row_sum = 0;
  4799. ggml_vec_sum_f32(ne00, &row_sum, src_row);
  4800. dst_row[0] = row_sum;
  4801. }
  4802. }
  4803. }
  4804. }
  4805. static void ggml_compute_forward_sum_rows(
  4806. const struct ggml_compute_params * params,
  4807. struct ggml_tensor * dst) {
  4808. const struct ggml_tensor * src0 = dst->src[0];
  4809. switch (src0->type) {
  4810. case GGML_TYPE_F32:
  4811. {
  4812. ggml_compute_forward_sum_rows_f32(params, dst);
  4813. } break;
  4814. default:
  4815. {
  4816. GGML_ABORT("fatal error");
  4817. }
  4818. }
  4819. }
  4820. // ggml_compute_forward_mean
  4821. static void ggml_compute_forward_mean_f32(
  4822. const struct ggml_compute_params * params,
  4823. struct ggml_tensor * dst) {
  4824. const struct ggml_tensor * src0 = dst->src[0];
  4825. if (params->ith != 0) {
  4826. return;
  4827. }
  4828. assert(src0->nb[0] == sizeof(float));
  4829. GGML_TENSOR_UNARY_OP_LOCALS
  4830. assert(ne0 == 1);
  4831. assert(ne1 == ne01);
  4832. assert(ne2 == ne02);
  4833. assert(ne3 == ne03);
  4834. UNUSED(ne0);
  4835. UNUSED(ne1);
  4836. UNUSED(ne2);
  4837. UNUSED(ne3);
  4838. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4839. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4840. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4841. ggml_vec_sum_f32(ne00,
  4842. (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  4843. (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03));
  4844. *(float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3) /= (float) ne00;
  4845. }
  4846. }
  4847. }
  4848. }
  4849. static void ggml_compute_forward_mean(
  4850. const struct ggml_compute_params * params,
  4851. struct ggml_tensor * dst) {
  4852. const struct ggml_tensor * src0 = dst->src[0];
  4853. switch (src0->type) {
  4854. case GGML_TYPE_F32:
  4855. {
  4856. ggml_compute_forward_mean_f32(params, dst);
  4857. } break;
  4858. default:
  4859. {
  4860. GGML_ABORT("fatal error");
  4861. }
  4862. }
  4863. }
  4864. // ggml_compute_forward_argmax
  4865. static void ggml_compute_forward_argmax_f32(
  4866. const struct ggml_compute_params * params,
  4867. struct ggml_tensor * dst) {
  4868. const struct ggml_tensor * src0 = dst->src[0];
  4869. if (params->ith != 0) {
  4870. return;
  4871. }
  4872. assert(src0->nb[0] == sizeof(float));
  4873. assert(dst->nb[0] == sizeof(float));
  4874. const int64_t ne00 = src0->ne[0];
  4875. const int64_t ne01 = src0->ne[1];
  4876. const size_t nb01 = src0->nb[1];
  4877. const size_t nb0 = dst->nb[0];
  4878. for (int64_t i1 = 0; i1 < ne01; i1++) {
  4879. float * src = (float *) ((char *) src0->data + i1*nb01);
  4880. int32_t * dst_ = (int32_t *) ((char *) dst->data + i1*nb0);
  4881. int v = 0;
  4882. ggml_vec_argmax_f32(ne00, &v, src);
  4883. dst_[0] = v;
  4884. }
  4885. }
  4886. static void ggml_compute_forward_argmax(
  4887. const struct ggml_compute_params * params,
  4888. struct ggml_tensor * dst) {
  4889. const struct ggml_tensor * src0 = dst->src[0];
  4890. switch (src0->type) {
  4891. case GGML_TYPE_F32:
  4892. {
  4893. ggml_compute_forward_argmax_f32(params, dst);
  4894. } break;
  4895. default:
  4896. {
  4897. GGML_ABORT("fatal error");
  4898. }
  4899. }
  4900. }
  4901. // ggml_compute_forward_count_equal
  4902. static void ggml_compute_forward_count_equal_i32(
  4903. const struct ggml_compute_params * params,
  4904. struct ggml_tensor * dst) {
  4905. const struct ggml_tensor * src0 = dst->src[0];
  4906. const struct ggml_tensor * src1 = dst->src[1];
  4907. GGML_TENSOR_BINARY_OP_LOCALS;
  4908. GGML_ASSERT(src0->type == GGML_TYPE_I32);
  4909. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  4910. GGML_ASSERT(ggml_are_same_shape(src0, src1));
  4911. GGML_ASSERT(ggml_is_scalar(dst));
  4912. GGML_ASSERT(dst->type == GGML_TYPE_I64);
  4913. const int64_t nr = ggml_nrows(src0);
  4914. const int ith = params->ith;
  4915. const int nth = params->nth;
  4916. int64_t * sums = (int64_t *) params->wdata;
  4917. int64_t sum_thread = 0;
  4918. // rows per thread
  4919. const int64_t dr = (nr + nth - 1)/nth;
  4920. // row range for this thread
  4921. const int64_t ir0 = dr*ith;
  4922. const int64_t ir1 = MIN(ir0 + dr, nr);
  4923. for (int64_t ir = ir0; ir < ir1; ++ir) {
  4924. const int64_t i03 = ir / (ne02*ne01);
  4925. const int64_t i02 = (ir - i03*ne03) / ne01;
  4926. const int64_t i01 = ir - i03*ne03 - i02*ne02;
  4927. const char * data0 = (const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01;
  4928. const char * data1 = (const char *) src1->data + i03*nb13 + i02*nb12 + i01*nb11;
  4929. for (int64_t i00 = 0; i00 < ne00; ++i00) {
  4930. const int32_t val0 = *((const int32_t *) (data0 + i00*nb00));
  4931. const int32_t val1 = *((const int32_t *) (data1 + i00*nb10));
  4932. sum_thread += val0 == val1;
  4933. }
  4934. }
  4935. if (ith != 0) {
  4936. sums[ith] = sum_thread;
  4937. }
  4938. ggml_barrier(params->threadpool);
  4939. if (ith != 0) {
  4940. return;
  4941. }
  4942. for (int ith_other = 1; ith_other < nth; ++ith_other) {
  4943. sum_thread += sums[ith_other];
  4944. }
  4945. *((int64_t *) dst->data) = sum_thread;
  4946. }
  4947. static void ggml_compute_forward_count_equal(
  4948. const struct ggml_compute_params * params,
  4949. struct ggml_tensor * dst) {
  4950. const struct ggml_tensor * src0 = dst->src[0];
  4951. switch (src0->type) {
  4952. case GGML_TYPE_I32:
  4953. {
  4954. ggml_compute_forward_count_equal_i32(params, dst);
  4955. } break;
  4956. default:
  4957. {
  4958. GGML_ABORT("fatal error");
  4959. }
  4960. }
  4961. }
  4962. // ggml_compute_forward_repeat
  4963. static void ggml_compute_forward_repeat_f32(
  4964. const struct ggml_compute_params * params,
  4965. struct ggml_tensor * dst) {
  4966. const struct ggml_tensor * src0 = dst->src[0];
  4967. if (params->ith != 0) {
  4968. return;
  4969. }
  4970. GGML_ASSERT(ggml_can_repeat(src0, dst));
  4971. GGML_TENSOR_UNARY_OP_LOCALS
  4972. // guaranteed to be an integer due to the check in ggml_can_repeat
  4973. const int nr0 = (int)(ne0/ne00);
  4974. const int nr1 = (int)(ne1/ne01);
  4975. const int nr2 = (int)(ne2/ne02);
  4976. const int nr3 = (int)(ne3/ne03);
  4977. // TODO: support for transposed / permuted tensors
  4978. GGML_ASSERT(nb0 == sizeof(float));
  4979. GGML_ASSERT(nb00 == sizeof(float));
  4980. // TODO: maybe this is not optimal?
  4981. for (int i3 = 0; i3 < nr3; i3++) {
  4982. for (int k3 = 0; k3 < ne03; k3++) {
  4983. for (int i2 = 0; i2 < nr2; i2++) {
  4984. for (int k2 = 0; k2 < ne02; k2++) {
  4985. for (int i1 = 0; i1 < nr1; i1++) {
  4986. for (int k1 = 0; k1 < ne01; k1++) {
  4987. for (int i0 = 0; i0 < nr0; i0++) {
  4988. ggml_vec_cpy_f32(ne00,
  4989. (float *) ((char *) dst->data + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0),
  4990. (float *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01));
  4991. }
  4992. }
  4993. }
  4994. }
  4995. }
  4996. }
  4997. }
  4998. }
  4999. static void ggml_compute_forward_repeat_f16(
  5000. const struct ggml_compute_params * params,
  5001. struct ggml_tensor * dst) {
  5002. const struct ggml_tensor * src0 = dst->src[0];
  5003. if (params->ith != 0) {
  5004. return;
  5005. }
  5006. GGML_ASSERT(ggml_can_repeat(src0, dst));
  5007. GGML_TENSOR_UNARY_OP_LOCALS
  5008. // guaranteed to be an integer due to the check in ggml_can_repeat
  5009. const int nr0 = (int)(ne0/ne00);
  5010. const int nr1 = (int)(ne1/ne01);
  5011. const int nr2 = (int)(ne2/ne02);
  5012. const int nr3 = (int)(ne3/ne03);
  5013. // TODO: support for transposed / permuted tensors
  5014. GGML_ASSERT(nb0 == sizeof(ggml_fp16_t));
  5015. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  5016. // TODO: maybe this is not optimal?
  5017. for (int i3 = 0; i3 < nr3; i3++) {
  5018. for (int k3 = 0; k3 < ne03; k3++) {
  5019. for (int i2 = 0; i2 < nr2; i2++) {
  5020. for (int k2 = 0; k2 < ne02; k2++) {
  5021. for (int i1 = 0; i1 < nr1; i1++) {
  5022. for (int k1 = 0; k1 < ne01; k1++) {
  5023. for (int i0 = 0; i0 < nr0; i0++) {
  5024. ggml_fp16_t * y = (ggml_fp16_t *) ((char *) dst->data + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0);
  5025. ggml_fp16_t * x = (ggml_fp16_t *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01);
  5026. // ggml_vec_cpy_f16(ne00, y, x)
  5027. for (int i = 0; i < ne00; ++i) {
  5028. y[i] = x[i];
  5029. }
  5030. }
  5031. }
  5032. }
  5033. }
  5034. }
  5035. }
  5036. }
  5037. }
  5038. static void ggml_compute_forward_repeat(
  5039. const struct ggml_compute_params * params,
  5040. struct ggml_tensor * dst) {
  5041. const struct ggml_tensor * src0 = dst->src[0];
  5042. switch (src0->type) {
  5043. case GGML_TYPE_F16:
  5044. case GGML_TYPE_BF16:
  5045. case GGML_TYPE_I16:
  5046. {
  5047. ggml_compute_forward_repeat_f16(params, dst);
  5048. } break;
  5049. case GGML_TYPE_F32:
  5050. case GGML_TYPE_I32:
  5051. {
  5052. ggml_compute_forward_repeat_f32(params, dst);
  5053. } break;
  5054. default:
  5055. {
  5056. GGML_ABORT("fatal error");
  5057. }
  5058. }
  5059. }
  5060. // ggml_compute_forward_repeat_back
  5061. static void ggml_compute_forward_repeat_back_f32(
  5062. const struct ggml_compute_params * params,
  5063. struct ggml_tensor * dst) {
  5064. const struct ggml_tensor * src0 = dst->src[0];
  5065. if (params->ith != 0) {
  5066. return;
  5067. }
  5068. GGML_ASSERT(ggml_can_repeat(dst, src0));
  5069. GGML_TENSOR_UNARY_OP_LOCALS
  5070. // guaranteed to be an integer due to the check in ggml_can_repeat
  5071. const int nr0 = (int)(ne00/ne0);
  5072. const int nr1 = (int)(ne01/ne1);
  5073. const int nr2 = (int)(ne02/ne2);
  5074. const int nr3 = (int)(ne03/ne3);
  5075. // TODO: support for transposed / permuted tensors
  5076. GGML_ASSERT(nb0 == sizeof(float));
  5077. GGML_ASSERT(nb00 == sizeof(float));
  5078. if (ggml_is_contiguous(dst)) {
  5079. ggml_vec_set_f32(ne0*ne1*ne2*ne3, dst->data, 0);
  5080. } else {
  5081. for (int k3 = 0; k3 < ne3; k3++) {
  5082. for (int k2 = 0; k2 < ne2; k2++) {
  5083. for (int k1 = 0; k1 < ne1; k1++) {
  5084. ggml_vec_set_f32(ne0,
  5085. (float *) ((char *) dst->data + k1*nb1 + k2*nb2 + k3*nb3),
  5086. 0);
  5087. }
  5088. }
  5089. }
  5090. }
  5091. // TODO: maybe this is not optimal?
  5092. for (int i3 = 0; i3 < nr3; i3++) {
  5093. for (int k3 = 0; k3 < ne3; k3++) {
  5094. for (int i2 = 0; i2 < nr2; i2++) {
  5095. for (int k2 = 0; k2 < ne2; k2++) {
  5096. for (int i1 = 0; i1 < nr1; i1++) {
  5097. for (int k1 = 0; k1 < ne1; k1++) {
  5098. for (int i0 = 0; i0 < nr0; i0++) {
  5099. ggml_vec_acc_f32(ne0,
  5100. (float *) ((char *) dst->data + ( k3)*nb3 + ( k2)*nb2 + ( k1)*nb1),
  5101. (float *) ((char *) src0->data + (i3*ne3 + k3)*nb03 + (i2*ne2 + k2)*nb02 + (i1*ne1 + k1)*nb01 + (i0*ne0)*nb00));
  5102. }
  5103. }
  5104. }
  5105. }
  5106. }
  5107. }
  5108. }
  5109. }
  5110. static void ggml_compute_forward_repeat_back(
  5111. const struct ggml_compute_params * params,
  5112. struct ggml_tensor * dst) {
  5113. const struct ggml_tensor * src0 = dst->src[0];
  5114. switch (src0->type) {
  5115. case GGML_TYPE_F32:
  5116. {
  5117. ggml_compute_forward_repeat_back_f32(params, dst);
  5118. } break;
  5119. default:
  5120. {
  5121. GGML_ABORT("fatal error");
  5122. }
  5123. }
  5124. }
  5125. // ggml_compute_forward_concat
  5126. static void ggml_compute_forward_concat_f32(
  5127. const struct ggml_compute_params * params,
  5128. struct ggml_tensor * dst) {
  5129. const struct ggml_tensor * src0 = dst->src[0];
  5130. const struct ggml_tensor * src1 = dst->src[1];
  5131. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5132. const int ith = params->ith;
  5133. const int nth = params->nth;
  5134. GGML_TENSOR_BINARY_OP_LOCALS
  5135. const int32_t dim = ggml_get_op_params_i32(dst, 0);
  5136. GGML_ASSERT(dim >= 0 && dim < 4);
  5137. int64_t o[4] = {0, 0, 0, 0};
  5138. o[dim] = src0->ne[dim];
  5139. const float * x;
  5140. // TODO: smarter multi-theading
  5141. for (int i3 = 0; i3 < ne3; i3++) {
  5142. for (int i2 = ith; i2 < ne2; i2 += nth) {
  5143. for (int i1 = 0; i1 < ne1; i1++) {
  5144. for (int i0 = 0; i0 < ne0; i0++) {
  5145. if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
  5146. x = (const float *) ((const char *)src0->data + (i0 )*nb00 + (i1 )*nb01 + (i2 )*nb02 + (i3 )*nb03);
  5147. } else {
  5148. x = (const float *) ((const char *)src1->data + (i0 - o[0])*nb10 + (i1 - o[1])*nb11 + (i2 - o[2])*nb12 + (i3 - o[3])*nb13);
  5149. }
  5150. float * y = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3);
  5151. *y = *x;
  5152. }
  5153. }
  5154. }
  5155. }
  5156. }
  5157. static void ggml_compute_forward_concat(
  5158. const struct ggml_compute_params * params,
  5159. struct ggml_tensor * dst) {
  5160. const struct ggml_tensor * src0 = dst->src[0];
  5161. switch (src0->type) {
  5162. case GGML_TYPE_F32:
  5163. case GGML_TYPE_I32:
  5164. {
  5165. ggml_compute_forward_concat_f32(params, dst);
  5166. } break;
  5167. default:
  5168. {
  5169. GGML_ABORT("fatal error");
  5170. }
  5171. }
  5172. }
  5173. // ggml_compute_forward_abs
  5174. static void ggml_compute_forward_abs_f32(
  5175. const struct ggml_compute_params * params,
  5176. struct ggml_tensor * dst) {
  5177. const struct ggml_tensor * src0 = dst->src[0];
  5178. if (params->ith != 0) {
  5179. return;
  5180. }
  5181. assert(ggml_is_contiguous_1(src0));
  5182. assert(ggml_is_contiguous_1(dst));
  5183. assert(ggml_are_same_shape(src0, dst));
  5184. const int n = ggml_nrows(src0);
  5185. const int nc = src0->ne[0];
  5186. for (int i = 0; i < n; i++) {
  5187. ggml_vec_abs_f32(nc,
  5188. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5189. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5190. }
  5191. }
  5192. static void ggml_compute_forward_abs(
  5193. const struct ggml_compute_params * params,
  5194. struct ggml_tensor * dst) {
  5195. const struct ggml_tensor * src0 = dst->src[0];
  5196. switch (src0->type) {
  5197. case GGML_TYPE_F32:
  5198. {
  5199. ggml_compute_forward_abs_f32(params, dst);
  5200. } break;
  5201. default:
  5202. {
  5203. GGML_ABORT("fatal error");
  5204. }
  5205. }
  5206. }
  5207. // ggml_compute_forward_sgn
  5208. static void ggml_compute_forward_sgn_f32(
  5209. const struct ggml_compute_params * params,
  5210. struct ggml_tensor * dst) {
  5211. const struct ggml_tensor * src0 = dst->src[0];
  5212. if (params->ith != 0) {
  5213. return;
  5214. }
  5215. assert(ggml_is_contiguous_1(src0));
  5216. assert(ggml_is_contiguous_1(dst));
  5217. assert(ggml_are_same_shape(src0, dst));
  5218. const int n = ggml_nrows(src0);
  5219. const int nc = src0->ne[0];
  5220. for (int i = 0; i < n; i++) {
  5221. ggml_vec_sgn_f32(nc,
  5222. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5223. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5224. }
  5225. }
  5226. static void ggml_compute_forward_sgn(
  5227. const struct ggml_compute_params * params,
  5228. struct ggml_tensor * dst) {
  5229. const struct ggml_tensor * src0 = dst->src[0];
  5230. switch (src0->type) {
  5231. case GGML_TYPE_F32:
  5232. {
  5233. ggml_compute_forward_sgn_f32(params, dst);
  5234. } break;
  5235. default:
  5236. {
  5237. GGML_ABORT("fatal error");
  5238. }
  5239. }
  5240. }
  5241. // ggml_compute_forward_neg
  5242. static void ggml_compute_forward_neg_f32(
  5243. const struct ggml_compute_params * params,
  5244. struct ggml_tensor * dst) {
  5245. const struct ggml_tensor * src0 = dst->src[0];
  5246. if (params->ith != 0) {
  5247. return;
  5248. }
  5249. assert(ggml_is_contiguous_1(src0));
  5250. assert(ggml_is_contiguous_1(dst));
  5251. assert(ggml_are_same_shape(src0, dst));
  5252. const int n = ggml_nrows(src0);
  5253. const int nc = src0->ne[0];
  5254. for (int i = 0; i < n; i++) {
  5255. ggml_vec_neg_f32(nc,
  5256. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5257. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5258. }
  5259. }
  5260. static void ggml_compute_forward_neg(
  5261. const struct ggml_compute_params * params,
  5262. struct ggml_tensor * dst) {
  5263. const struct ggml_tensor * src0 = dst->src[0];
  5264. switch (src0->type) {
  5265. case GGML_TYPE_F32:
  5266. {
  5267. ggml_compute_forward_neg_f32(params, dst);
  5268. } break;
  5269. default:
  5270. {
  5271. GGML_ABORT("fatal error");
  5272. }
  5273. }
  5274. }
  5275. // ggml_compute_forward_step
  5276. static void ggml_compute_forward_step_f32(
  5277. const struct ggml_compute_params * params,
  5278. struct ggml_tensor * dst) {
  5279. const struct ggml_tensor * src0 = dst->src[0];
  5280. if (params->ith != 0) {
  5281. return;
  5282. }
  5283. assert(ggml_is_contiguous_1(src0));
  5284. assert(ggml_is_contiguous_1(dst));
  5285. assert(ggml_are_same_shape(src0, dst));
  5286. const int n = ggml_nrows(src0);
  5287. const int nc = src0->ne[0];
  5288. for (int i = 0; i < n; i++) {
  5289. ggml_vec_step_f32(nc,
  5290. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5291. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5292. }
  5293. }
  5294. static void ggml_compute_forward_step(
  5295. const struct ggml_compute_params * params,
  5296. struct ggml_tensor * dst) {
  5297. const struct ggml_tensor * src0 = dst->src[0];
  5298. switch (src0->type) {
  5299. case GGML_TYPE_F32:
  5300. {
  5301. ggml_compute_forward_step_f32(params, dst);
  5302. } break;
  5303. default:
  5304. {
  5305. GGML_ABORT("fatal error");
  5306. }
  5307. }
  5308. }
  5309. // ggml_compute_forward_tanh
  5310. static void ggml_compute_forward_tanh_f32(
  5311. const struct ggml_compute_params * params,
  5312. struct ggml_tensor * dst) {
  5313. const struct ggml_tensor * src0 = dst->src[0];
  5314. if (params->ith != 0) {
  5315. return;
  5316. }
  5317. assert(ggml_is_contiguous_1(src0));
  5318. assert(ggml_is_contiguous_1(dst));
  5319. assert(ggml_are_same_shape(src0, dst));
  5320. const int n = ggml_nrows(src0);
  5321. const int nc = src0->ne[0];
  5322. for (int i = 0; i < n; i++) {
  5323. ggml_vec_tanh_f32(nc,
  5324. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5325. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5326. }
  5327. }
  5328. static void ggml_compute_forward_tanh(
  5329. const struct ggml_compute_params * params,
  5330. struct ggml_tensor * dst) {
  5331. const struct ggml_tensor * src0 = dst->src[0];
  5332. switch (src0->type) {
  5333. case GGML_TYPE_F32:
  5334. {
  5335. ggml_compute_forward_tanh_f32(params, dst);
  5336. } break;
  5337. default:
  5338. {
  5339. GGML_ABORT("fatal error");
  5340. }
  5341. }
  5342. }
  5343. // ggml_compute_forward_elu
  5344. static void ggml_compute_forward_elu_f32(
  5345. const struct ggml_compute_params * params,
  5346. struct ggml_tensor * dst) {
  5347. const struct ggml_tensor * src0 = dst->src[0];
  5348. if (params->ith != 0) {
  5349. return;
  5350. }
  5351. assert(ggml_is_contiguous_1(src0));
  5352. assert(ggml_is_contiguous_1(dst));
  5353. assert(ggml_are_same_shape(src0, dst));
  5354. const int n = ggml_nrows(src0);
  5355. const int nc = src0->ne[0];
  5356. for (int i = 0; i < n; i++) {
  5357. ggml_vec_elu_f32(nc,
  5358. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5359. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5360. }
  5361. }
  5362. static void ggml_compute_forward_elu(
  5363. const struct ggml_compute_params * params,
  5364. struct ggml_tensor * dst) {
  5365. const struct ggml_tensor * src0 = dst->src[0];
  5366. switch (src0->type) {
  5367. case GGML_TYPE_F32:
  5368. {
  5369. ggml_compute_forward_elu_f32(params, dst);
  5370. } break;
  5371. default:
  5372. {
  5373. GGML_ABORT("fatal error");
  5374. }
  5375. }
  5376. }
  5377. // ggml_compute_forward_relu
  5378. static void ggml_compute_forward_relu_f32(
  5379. const struct ggml_compute_params * params,
  5380. struct ggml_tensor * dst) {
  5381. const struct ggml_tensor * src0 = dst->src[0];
  5382. if (params->ith != 0) {
  5383. return;
  5384. }
  5385. assert(ggml_is_contiguous_1(src0));
  5386. assert(ggml_is_contiguous_1(dst));
  5387. assert(ggml_are_same_shape(src0, dst));
  5388. const int n = ggml_nrows(src0);
  5389. const int nc = src0->ne[0];
  5390. for (int i = 0; i < n; i++) {
  5391. ggml_vec_relu_f32(nc,
  5392. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5393. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5394. }
  5395. }
  5396. static void ggml_compute_forward_relu(
  5397. const struct ggml_compute_params * params,
  5398. struct ggml_tensor * dst) {
  5399. const struct ggml_tensor * src0 = dst->src[0];
  5400. switch (src0->type) {
  5401. case GGML_TYPE_F32:
  5402. {
  5403. ggml_compute_forward_relu_f32(params, dst);
  5404. } break;
  5405. default:
  5406. {
  5407. GGML_ABORT("fatal error");
  5408. }
  5409. }
  5410. }
  5411. // ggml_compute_forward_sigmoid
  5412. static void ggml_compute_forward_sigmoid_f32(
  5413. const struct ggml_compute_params * params,
  5414. struct ggml_tensor * dst) {
  5415. const struct ggml_tensor * src0 = dst->src[0];
  5416. if (params->ith != 0) {
  5417. return;
  5418. }
  5419. assert(ggml_is_contiguous_1(src0));
  5420. assert(ggml_is_contiguous_1(dst));
  5421. assert(ggml_are_same_shape(src0, dst));
  5422. const int n = ggml_nrows(src0);
  5423. const int nc = src0->ne[0];
  5424. for (int i = 0; i < n; i++) {
  5425. ggml_vec_sigmoid_f32(nc,
  5426. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5427. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5428. }
  5429. }
  5430. static void ggml_compute_forward_sigmoid(
  5431. const struct ggml_compute_params * params,
  5432. struct ggml_tensor * dst) {
  5433. const struct ggml_tensor * src0 = dst->src[0];
  5434. switch (src0->type) {
  5435. case GGML_TYPE_F32:
  5436. {
  5437. ggml_compute_forward_sigmoid_f32(params, dst);
  5438. } break;
  5439. default:
  5440. {
  5441. GGML_ABORT("fatal error");
  5442. }
  5443. }
  5444. }
  5445. // ggml_compute_forward_gelu
  5446. static void ggml_compute_forward_gelu_f32(
  5447. const struct ggml_compute_params * params,
  5448. struct ggml_tensor * dst) {
  5449. const struct ggml_tensor * src0 = dst->src[0];
  5450. assert(ggml_is_contiguous_1(src0));
  5451. assert(ggml_is_contiguous_1(dst));
  5452. assert(ggml_are_same_shape(src0, dst));
  5453. const int ith = params->ith;
  5454. const int nth = params->nth;
  5455. const int nc = src0->ne[0];
  5456. const int nr = ggml_nrows(src0);
  5457. // rows per thread
  5458. const int dr = (nr + nth - 1)/nth;
  5459. // row range for this thread
  5460. const int ir0 = dr*ith;
  5461. const int ir1 = MIN(ir0 + dr, nr);
  5462. for (int i1 = ir0; i1 < ir1; i1++) {
  5463. ggml_vec_gelu_f32(nc,
  5464. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5465. (float *) ((char *) src0->data + i1*(src0->nb[1])));
  5466. #ifndef NDEBUG
  5467. for (int k = 0; k < nc; k++) {
  5468. const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k];
  5469. UNUSED(x);
  5470. assert(!isnan(x));
  5471. assert(!isinf(x));
  5472. }
  5473. #endif
  5474. }
  5475. }
  5476. static void ggml_compute_forward_gelu(
  5477. const struct ggml_compute_params * params,
  5478. struct ggml_tensor * dst) {
  5479. const struct ggml_tensor * src0 = dst->src[0];
  5480. switch (src0->type) {
  5481. case GGML_TYPE_F32:
  5482. {
  5483. ggml_compute_forward_gelu_f32(params, dst);
  5484. } break;
  5485. default:
  5486. {
  5487. GGML_ABORT("fatal error");
  5488. }
  5489. }
  5490. }
  5491. // ggml_compute_forward_gelu_quick
  5492. static void ggml_compute_forward_gelu_quick_f32(
  5493. const struct ggml_compute_params * params,
  5494. struct ggml_tensor * dst) {
  5495. const struct ggml_tensor * src0 = dst->src[0];
  5496. assert(ggml_is_contiguous_1(src0));
  5497. assert(ggml_is_contiguous_1(dst));
  5498. assert(ggml_are_same_shape(src0, dst));
  5499. const int ith = params->ith;
  5500. const int nth = params->nth;
  5501. const int nc = src0->ne[0];
  5502. const int nr = ggml_nrows(src0);
  5503. // rows per thread
  5504. const int dr = (nr + nth - 1)/nth;
  5505. // row range for this thread
  5506. const int ir0 = dr*ith;
  5507. const int ir1 = MIN(ir0 + dr, nr);
  5508. for (int i1 = ir0; i1 < ir1; i1++) {
  5509. ggml_vec_gelu_quick_f32(nc,
  5510. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5511. (float *) ((char *) src0->data + i1*(src0->nb[1])));
  5512. #ifndef NDEBUG
  5513. for (int k = 0; k < nc; k++) {
  5514. const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k];
  5515. UNUSED(x);
  5516. assert(!isnan(x));
  5517. assert(!isinf(x));
  5518. }
  5519. #endif
  5520. }
  5521. }
  5522. static void ggml_compute_forward_gelu_quick(
  5523. const struct ggml_compute_params * params,
  5524. struct ggml_tensor * dst) {
  5525. const struct ggml_tensor * src0 = dst->src[0];
  5526. switch (src0->type) {
  5527. case GGML_TYPE_F32:
  5528. {
  5529. ggml_compute_forward_gelu_quick_f32(params, dst);
  5530. } break;
  5531. default:
  5532. {
  5533. GGML_ABORT("fatal error");
  5534. }
  5535. }
  5536. }
  5537. // ggml_compute_forward_silu
  5538. static void ggml_compute_forward_silu_f32(
  5539. const struct ggml_compute_params * params,
  5540. struct ggml_tensor * dst) {
  5541. const struct ggml_tensor * src0 = dst->src[0];
  5542. assert(ggml_is_contiguous_1(src0));
  5543. assert(ggml_is_contiguous_1(dst));
  5544. assert(ggml_are_same_shape(src0, dst));
  5545. const int ith = params->ith;
  5546. const int nth = params->nth;
  5547. const int nc = src0->ne[0];
  5548. const int nr = ggml_nrows(src0);
  5549. // rows per thread
  5550. const int dr = (nr + nth - 1)/nth;
  5551. // row range for this thread
  5552. const int ir0 = dr*ith;
  5553. const int ir1 = MIN(ir0 + dr, nr);
  5554. for (int i1 = ir0; i1 < ir1; i1++) {
  5555. ggml_vec_silu_f32(nc,
  5556. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5557. (float *) ((char *) src0->data + i1*(src0->nb[1])));
  5558. #ifndef NDEBUG
  5559. for (int k = 0; k < nc; k++) {
  5560. const float x = ((float *) ((char *) dst->data + i1*(dst->nb[1])))[k];
  5561. UNUSED(x);
  5562. assert(!isnan(x));
  5563. assert(!isinf(x));
  5564. }
  5565. #endif
  5566. }
  5567. }
  5568. static void ggml_compute_forward_silu(
  5569. const struct ggml_compute_params * params,
  5570. struct ggml_tensor * dst) {
  5571. const struct ggml_tensor * src0 = dst->src[0];
  5572. switch (src0->type) {
  5573. case GGML_TYPE_F32:
  5574. {
  5575. ggml_compute_forward_silu_f32(params, dst);
  5576. } break;
  5577. default:
  5578. {
  5579. GGML_ABORT("fatal error");
  5580. }
  5581. }
  5582. }
  5583. // ggml_compute_forward_leaky_relu
  5584. static void ggml_compute_forward_leaky_relu_f32(
  5585. const struct ggml_compute_params * params,
  5586. struct ggml_tensor * dst) {
  5587. const struct ggml_tensor * src0 = dst->src[0];
  5588. if (params->ith != 0) {
  5589. return;
  5590. }
  5591. assert(ggml_is_contiguous_1(src0));
  5592. assert(ggml_is_contiguous_1(dst));
  5593. assert(ggml_are_same_shape(src0, dst));
  5594. const int n = ggml_nrows(src0);
  5595. const int nc = src0->ne[0];
  5596. float negative_slope;
  5597. memcpy(&negative_slope, dst->op_params, sizeof(float));
  5598. assert(dst->nb[0] == sizeof(float));
  5599. assert(src0->nb[0] == sizeof(float));
  5600. for (int i = 0; i < n; i++) {
  5601. ggml_vec_leaky_relu_f32(nc,
  5602. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5603. (float *) ((char *) src0->data + i*(src0->nb[1])), negative_slope);
  5604. }
  5605. }
  5606. static void ggml_compute_forward_leaky_relu(
  5607. const struct ggml_compute_params * params,
  5608. struct ggml_tensor * dst) {
  5609. const struct ggml_tensor * src0 = dst->src[0];
  5610. switch (src0->type) {
  5611. case GGML_TYPE_F32:
  5612. {
  5613. ggml_compute_forward_leaky_relu_f32(params, dst);
  5614. } break;
  5615. default:
  5616. {
  5617. GGML_ABORT("fatal error");
  5618. }
  5619. }
  5620. }
  5621. // ggml_compute_forward_silu_back
  5622. static void ggml_compute_forward_silu_back_f32(
  5623. const struct ggml_compute_params * params,
  5624. struct ggml_tensor * dst) {
  5625. const struct ggml_tensor * grad = dst->src[0];
  5626. const struct ggml_tensor * src1 = dst->src[1];
  5627. assert(ggml_is_contiguous_1(grad));
  5628. assert(ggml_is_contiguous_1(src1));
  5629. assert(ggml_is_contiguous_1(dst));
  5630. assert(ggml_are_same_shape(src1, dst));
  5631. assert(ggml_are_same_shape(src1, grad));
  5632. const int ith = params->ith;
  5633. const int nth = params->nth;
  5634. const int nc = src1->ne[0];
  5635. const int nr = ggml_nrows(src1);
  5636. // rows per thread
  5637. const int dr = (nr + nth - 1)/nth;
  5638. // row range for this thread
  5639. const int ir0 = dr*ith;
  5640. const int ir1 = MIN(ir0 + dr, nr);
  5641. for (int i1 = ir0; i1 < ir1; i1++) {
  5642. ggml_vec_silu_backward_f32(nc,
  5643. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5644. (float *) ((char *) src1->data + i1*(src1->nb[1])),
  5645. (float *) ((char *) grad->data + i1*(grad->nb[1])));
  5646. #ifndef NDEBUG
  5647. for (int k = 0; k < nc; k++) {
  5648. const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k];
  5649. UNUSED(x);
  5650. assert(!isnan(x));
  5651. assert(!isinf(x));
  5652. }
  5653. #endif
  5654. }
  5655. }
  5656. static void ggml_compute_forward_silu_back(
  5657. const struct ggml_compute_params * params,
  5658. struct ggml_tensor * dst) {
  5659. const struct ggml_tensor * src0 = dst->src[0];
  5660. switch (src0->type) {
  5661. case GGML_TYPE_F32:
  5662. {
  5663. ggml_compute_forward_silu_back_f32(params, dst);
  5664. } break;
  5665. default:
  5666. {
  5667. GGML_ABORT("fatal error");
  5668. }
  5669. }
  5670. }
  5671. static void ggml_compute_forward_hardswish_f32(
  5672. const struct ggml_compute_params * params,
  5673. struct ggml_tensor * dst) {
  5674. const struct ggml_tensor * src0 = dst->src[0];
  5675. if (params->ith != 0) {
  5676. return;
  5677. }
  5678. assert(ggml_is_contiguous_1(src0));
  5679. assert(ggml_is_contiguous_1(dst));
  5680. assert(ggml_are_same_shape(src0, dst));
  5681. const int n = ggml_nrows(src0);
  5682. const int nc = src0->ne[0];
  5683. for (int i = 0; i < n; i++) {
  5684. ggml_vec_hardswish_f32(nc,
  5685. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5686. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5687. }
  5688. }
  5689. static void ggml_compute_forward_hardswish(
  5690. const struct ggml_compute_params * params,
  5691. struct ggml_tensor * dst) {
  5692. const struct ggml_tensor * src0 = dst->src[0];
  5693. switch (src0->type) {
  5694. case GGML_TYPE_F32:
  5695. {
  5696. ggml_compute_forward_hardswish_f32(params, dst);
  5697. } break;
  5698. default:
  5699. {
  5700. GGML_ABORT("fatal error");
  5701. }
  5702. }
  5703. }
  5704. static void ggml_compute_forward_hardsigmoid_f32(
  5705. const struct ggml_compute_params * params,
  5706. struct ggml_tensor * dst) {
  5707. const struct ggml_tensor * src0 = dst->src[0];
  5708. if (params->ith != 0) {
  5709. return;
  5710. }
  5711. assert(ggml_is_contiguous_1(src0));
  5712. assert(ggml_is_contiguous_1(dst));
  5713. assert(ggml_are_same_shape(src0, dst));
  5714. const int n = ggml_nrows(src0);
  5715. const int nc = src0->ne[0];
  5716. for (int i = 0; i < n; i++) {
  5717. ggml_vec_hardsigmoid_f32(nc,
  5718. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5719. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5720. }
  5721. }
  5722. static void ggml_compute_forward_hardsigmoid(
  5723. const struct ggml_compute_params * params,
  5724. struct ggml_tensor * dst) {
  5725. const struct ggml_tensor * src0 = dst->src[0];
  5726. switch (src0->type) {
  5727. case GGML_TYPE_F32:
  5728. {
  5729. ggml_compute_forward_hardsigmoid_f32(params, dst);
  5730. } break;
  5731. default:
  5732. {
  5733. GGML_ABORT("fatal error");
  5734. }
  5735. }
  5736. }
  5737. static void ggml_compute_forward_exp_f32(
  5738. const struct ggml_compute_params * params,
  5739. struct ggml_tensor * dst) {
  5740. const struct ggml_tensor * src0 = dst->src[0];
  5741. if (params->ith != 0) {
  5742. return;
  5743. }
  5744. assert(ggml_is_contiguous_1(src0));
  5745. assert(ggml_is_contiguous_1(dst));
  5746. assert(ggml_are_same_shape(src0, dst));
  5747. const int n = ggml_nrows(src0);
  5748. const int nc = src0->ne[0];
  5749. for (int i = 0; i < n; i++) {
  5750. ggml_vec_exp_f32(nc,
  5751. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5752. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5753. }
  5754. }
  5755. static void ggml_compute_forward_exp(
  5756. const struct ggml_compute_params * params,
  5757. struct ggml_tensor * dst) {
  5758. const struct ggml_tensor * src0 = dst->src[0];
  5759. switch (src0->type) {
  5760. case GGML_TYPE_F32:
  5761. {
  5762. ggml_compute_forward_exp_f32(params, dst);
  5763. } break;
  5764. default:
  5765. {
  5766. GGML_ABORT("fatal error");
  5767. }
  5768. }
  5769. }
  5770. // ggml_compute_forward_norm
  5771. static void ggml_compute_forward_norm_f32(
  5772. const struct ggml_compute_params * params,
  5773. struct ggml_tensor * dst) {
  5774. const struct ggml_tensor * src0 = dst->src[0];
  5775. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  5776. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5777. const int ith = params->ith;
  5778. const int nth = params->nth;
  5779. GGML_TENSOR_UNARY_OP_LOCALS
  5780. float eps;
  5781. memcpy(&eps, dst->op_params, sizeof(float));
  5782. GGML_ASSERT(eps >= 0.0f);
  5783. // TODO: optimize
  5784. for (int64_t i03 = 0; i03 < ne03; i03++) {
  5785. for (int64_t i02 = 0; i02 < ne02; i02++) {
  5786. for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
  5787. const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  5788. ggml_float sum = 0.0;
  5789. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5790. sum += (ggml_float)x[i00];
  5791. }
  5792. float mean = sum/ne00;
  5793. float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
  5794. ggml_float sum2 = 0.0;
  5795. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5796. float v = x[i00] - mean;
  5797. y[i00] = v;
  5798. sum2 += (ggml_float)(v*v);
  5799. }
  5800. float variance = sum2/ne00;
  5801. const float scale = 1.0f/sqrtf(variance + eps);
  5802. ggml_vec_scale_f32(ne00, y, scale);
  5803. }
  5804. }
  5805. }
  5806. }
  5807. static void ggml_compute_forward_norm(
  5808. const struct ggml_compute_params * params,
  5809. struct ggml_tensor * dst) {
  5810. const struct ggml_tensor * src0 = dst->src[0];
  5811. switch (src0->type) {
  5812. case GGML_TYPE_F32:
  5813. {
  5814. ggml_compute_forward_norm_f32(params, dst);
  5815. } break;
  5816. default:
  5817. {
  5818. GGML_ABORT("fatal error");
  5819. }
  5820. }
  5821. }
  5822. // ggml_compute_forward_group_rms_norm
  5823. static void ggml_compute_forward_rms_norm_f32(
  5824. const struct ggml_compute_params * params,
  5825. struct ggml_tensor * dst) {
  5826. const struct ggml_tensor * src0 = dst->src[0];
  5827. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  5828. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5829. const int ith = params->ith;
  5830. const int nth = params->nth;
  5831. GGML_TENSOR_UNARY_OP_LOCALS
  5832. float eps;
  5833. memcpy(&eps, dst->op_params, sizeof(float));
  5834. GGML_ASSERT(eps >= 0.0f);
  5835. // TODO: optimize
  5836. for (int64_t i03 = 0; i03 < ne03; i03++) {
  5837. for (int64_t i02 = 0; i02 < ne02; i02++) {
  5838. for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
  5839. const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  5840. ggml_float sum = 0.0;
  5841. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5842. sum += (ggml_float)(x[i00] * x[i00]);
  5843. }
  5844. const float mean = sum/ne00;
  5845. float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
  5846. memcpy(y, x, ne00 * sizeof(float));
  5847. // for (int i00 = 0; i00 < ne00; i00++) {
  5848. // y[i00] = x[i00];
  5849. // }
  5850. const float scale = 1.0f/sqrtf(mean + eps);
  5851. ggml_vec_scale_f32(ne00, y, scale);
  5852. }
  5853. }
  5854. }
  5855. }
  5856. static void ggml_compute_forward_rms_norm(
  5857. const struct ggml_compute_params * params,
  5858. struct ggml_tensor * dst) {
  5859. const struct ggml_tensor * src0 = dst->src[0];
  5860. switch (src0->type) {
  5861. case GGML_TYPE_F32:
  5862. {
  5863. ggml_compute_forward_rms_norm_f32(params, dst);
  5864. } break;
  5865. default:
  5866. {
  5867. GGML_ABORT("fatal error");
  5868. }
  5869. }
  5870. }
  5871. static void ggml_compute_forward_rms_norm_back_f32(
  5872. const struct ggml_compute_params * params,
  5873. struct ggml_tensor * dst) {
  5874. const struct ggml_tensor * src0 = dst->src[0]; // gradients from forward pass output
  5875. const struct ggml_tensor * src1 = dst->src[1]; // src1 from forward pass
  5876. GGML_ASSERT(ggml_are_same_shape(src0, dst) && ggml_are_same_shape(src0, src1));
  5877. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5878. GGML_ASSERT(src1->nb[0] == sizeof(float));
  5879. const int ith = params->ith;
  5880. const int nth = params->nth;
  5881. GGML_TENSOR_BINARY_OP_LOCALS
  5882. float eps;
  5883. memcpy(&eps, dst->op_params, sizeof(float));
  5884. // TODO: optimize
  5885. for (int64_t i03 = 0; i03 < ne03; i03++) {
  5886. for (int64_t i02 = 0; i02 < ne02; i02++) {
  5887. for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
  5888. // src1 is same shape as src0 => same indices
  5889. const int64_t i11 = i01;
  5890. const int64_t i12 = i02;
  5891. const int64_t i13 = i03;
  5892. const float * dz = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  5893. const float * x = (float *) ((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13);
  5894. ggml_float sum_xx = 0.0;
  5895. ggml_float sum_xdz = 0.0;
  5896. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5897. sum_xx += (ggml_float)(x[i00] * x[i00]);
  5898. sum_xdz += (ggml_float)(x[i00] * dz[i00]);
  5899. }
  5900. //const float mean = (float)(sum_xx)/ne00;
  5901. const float mean_eps = (float)(sum_xx)/ne00 + eps;
  5902. const float sum_eps = (float)(sum_xx) + eps*ne00;
  5903. //const float mean_xdz = (float)(sum_xdz)/ne00;
  5904. // we could cache rms from forward pass to improve performance.
  5905. // to do this implement ggml_rms and compose ggml_rms_norm using ggml_rms.
  5906. //const float rms = sqrtf(mean_eps);
  5907. const float rrms = 1.0f / sqrtf(mean_eps);
  5908. //const float scale = -rrms/(ne00 * mean_eps); // -1/(n*rms**3)
  5909. {
  5910. // z = rms_norm(x)
  5911. //
  5912. // rms_norm(src1) =
  5913. // scale(
  5914. // src1,
  5915. // div(
  5916. // 1,
  5917. // sqrt(
  5918. // add(
  5919. // scale(
  5920. // sum(
  5921. // sqr(
  5922. // src1)),
  5923. // (1.0/N)),
  5924. // eps))));
  5925. // postorder:
  5926. // ## op args grad
  5927. // 00 param src1 grad[#00]
  5928. // 01 const 1
  5929. // 02 sqr (#00) grad[#02]
  5930. // 03 sum (#02) grad[#03]
  5931. // 04 const 1/N
  5932. // 05 scale (#03, #04) grad[#05]
  5933. // 06 const eps
  5934. // 07 add (#05, #06) grad[#07]
  5935. // 08 sqrt (#07) grad[#08]
  5936. // 09 div (#01,#08) grad[#09]
  5937. // 10 scale (#00,#09) grad[#10]
  5938. //
  5939. // backward pass, given grad[#10]
  5940. // #10: scale
  5941. // grad[#00] += scale(grad[#10],#09)
  5942. // grad[#09] += sum(mul(grad[#10],#00))
  5943. // #09: div
  5944. // grad[#08] += neg(mul(grad[#09], div(#09,#08)))
  5945. // #08: sqrt
  5946. // grad[#07] += mul(grad[#08], div(0.5, #08))
  5947. // #07: add
  5948. // grad[#05] += grad[#07]
  5949. // #05: scale
  5950. // grad[#03] += scale(grad[#05],#04)
  5951. // #03: sum
  5952. // grad[#02] += repeat(grad[#03], #02)
  5953. // #02:
  5954. // grad[#00] += scale(mul(#00, grad[#02]), 2.0)
  5955. //
  5956. // substitute and simplify:
  5957. // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0)
  5958. // grad[#02] = repeat(grad[#03], #02)
  5959. // grad[#02] = repeat(scale(grad[#05],#04), #02)
  5960. // grad[#02] = repeat(scale(grad[#07],#04), #02)
  5961. // grad[#02] = repeat(scale(mul(grad[#08], div(0.5, #08)),#04), #02)
  5962. // grad[#02] = repeat(scale(mul(neg(mul(grad[#09], div(#09,#08))), div(0.5, #08)),#04), #02)
  5963. // grad[#02] = repeat(scale(mul(neg(mul(sum(mul(grad[#10],#00)), div(#09,#08))), div(0.5, #08)),#04), #02)
  5964. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(#09,#08) * div(0.5, #08) * (1/N)), #02)
  5965. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(div(#01,#08),#08) * div(0.5, #08) * (1/N)), #02)
  5966. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#08*#08) * div(0.5, #08) * (1/N)), #02)
  5967. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N)), #02)
  5968. // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0)
  5969. // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, repeat(-(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N)), #02)), 2.0)
  5970. // grad[#00] = scale(grad(#10), #09) + scale(scale(#00, -(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N))), 2.0)
  5971. // grad[#00] = scale(grad(#10), #09) + scale(#00, -(sum(mul(grad[#10],#00)) * div(1,#07) * div(1,#08) * (1/N)))
  5972. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N))
  5973. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N))
  5974. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,mean_eps*rms) * (-1/N))
  5975. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*mean_eps))
  5976. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*(sum_xx/N+eps)))
  5977. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*sum_xx+rms*N*eps))
  5978. // grad[#00] = scale(dz, rrms) + scale(x, sum(mul(dz,x)) * div(-1,rms*N*mean_eps))
  5979. // grad[#00] = scale(dz, rrms) + scale(x, sum_xdz * div(-1,rms*N*mean_eps))
  5980. // a = b*c + d*e
  5981. // a = b*c*f/f + d*e*f/f
  5982. // a = (b*c*f + d*e*f)*(1/f)
  5983. // a = (b*c*(1/c) + d*e*(1/c))*(1/(1/c))
  5984. // a = (b + d*e/c)*c
  5985. // b = dz, c = rrms, d = x, e = sum_xdz * div(-1,rms*N*mean_eps)
  5986. // a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)/rrms)*rrms
  5987. // a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)*rms)*rrms
  5988. // a = (dz + x*sum_xdz * div(-rms,rms*N*mean_eps))*rrms
  5989. // a = (dz + x*sum_xdz * div(-1,N*mean_eps))*rrms
  5990. // a = (dz + x*div(-sum_xdz,N*mean_eps))*rrms
  5991. // a = (dz + x*div(-mean_xdz,mean_eps))*rrms
  5992. // grad[#00] = scale(dz + scale(x, div(-mean_xdz,mean_eps)),rrms)
  5993. // grad[#00] = scale(dz + scale(x, -mean_xdz/mean_eps),rrms)
  5994. // dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms)
  5995. }
  5996. // dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms)
  5997. // post-order:
  5998. // dx := x
  5999. // dx := scale(dx,-mean_xdz/mean_eps)
  6000. // dx := add(dx, dz)
  6001. // dx := scale(dx, rrms)
  6002. float * dx = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
  6003. // dx[i00] = (x*(-sum_xdz/sum_eps) + dz) / sqrtf(mean_eps)
  6004. ggml_vec_cpy_f32 (ne00, dx, x);
  6005. // ggml_vec_scale_f32(ne00, dx, -mean_xdz/mean_eps);
  6006. ggml_vec_scale_f32(ne00, dx, (float)(-sum_xdz)/sum_eps);
  6007. ggml_vec_acc_f32 (ne00, dx, dz);
  6008. ggml_vec_scale_f32(ne00, dx, rrms);
  6009. }
  6010. }
  6011. }
  6012. }
  6013. static void ggml_compute_forward_rms_norm_back(
  6014. const struct ggml_compute_params * params,
  6015. struct ggml_tensor * dst) {
  6016. const struct ggml_tensor * src0 = dst->src[0];
  6017. switch (src0->type) {
  6018. case GGML_TYPE_F32:
  6019. {
  6020. ggml_compute_forward_rms_norm_back_f32(params, dst);
  6021. } break;
  6022. default:
  6023. {
  6024. GGML_ABORT("fatal error");
  6025. }
  6026. }
  6027. }
  6028. // ggml_compute_forward_group_norm
  6029. static void ggml_compute_forward_group_norm_f32(
  6030. const struct ggml_compute_params * params,
  6031. struct ggml_tensor * dst) {
  6032. const struct ggml_tensor * src0 = dst->src[0];
  6033. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6034. GGML_ASSERT(src0->nb[0] == sizeof(float));
  6035. const int ith = params->ith;
  6036. const int nth = params->nth;
  6037. GGML_TENSOR_UNARY_OP_LOCALS
  6038. // TODO: optimize
  6039. float eps;
  6040. memcpy(&eps, dst->op_params + 1, sizeof(float));
  6041. int n_channels = src0->ne[2];
  6042. int n_groups = dst->op_params[0];
  6043. int n_channels_per_group = (n_channels + n_groups - 1) / n_groups;
  6044. for (int i = ith; i < n_groups; i += nth) {
  6045. int start = i * n_channels_per_group;
  6046. int end = start + n_channels_per_group;
  6047. if (end > n_channels) {
  6048. end = n_channels;
  6049. }
  6050. int step = end - start;
  6051. for (int64_t i03 = 0; i03 < ne03; i03++) {
  6052. ggml_float sum = 0.0;
  6053. for (int64_t i02 = start; i02 < end; i02++) {
  6054. for (int64_t i01 = 0; i01 < ne01; i01++) {
  6055. const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03);
  6056. ggml_float sumr = 0.0;
  6057. for (int64_t i00 = 0; i00 < ne00; i00++) {
  6058. sumr += (ggml_float)x[i00];
  6059. }
  6060. sum += sumr;
  6061. }
  6062. }
  6063. const float mean = sum / (ne00 * ne01 * step);
  6064. ggml_float sum2 = 0.0;
  6065. for (int64_t i02 = start; i02 < end; i02++) {
  6066. for (int64_t i01 = 0; i01 < ne01; i01++) {
  6067. const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03);
  6068. float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3);
  6069. ggml_float sumr = 0.0;
  6070. for (int64_t i00 = 0; i00 < ne00; i00++) {
  6071. float v = x[i00] - mean;
  6072. y[i00] = v;
  6073. sumr += (ggml_float)(v * v);
  6074. }
  6075. sum2 += sumr;
  6076. }
  6077. }
  6078. const float variance = sum2 / (ne00 * ne01 * step);
  6079. const float scale = 1.0f / sqrtf(variance + eps);
  6080. for (int64_t i02 = start; i02 < end; i02++) {
  6081. for (int64_t i01 = 0; i01 < ne01; i01++) {
  6082. float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3);
  6083. ggml_vec_scale_f32(ne00, y, scale);
  6084. }
  6085. }
  6086. }
  6087. }
  6088. }
  6089. static void ggml_compute_forward_group_norm(
  6090. const struct ggml_compute_params * params,
  6091. struct ggml_tensor * dst) {
  6092. const struct ggml_tensor * src0 = dst->src[0];
  6093. switch (src0->type) {
  6094. case GGML_TYPE_F32:
  6095. {
  6096. ggml_compute_forward_group_norm_f32(params, dst);
  6097. } break;
  6098. default:
  6099. {
  6100. GGML_ABORT("fatal error");
  6101. }
  6102. }
  6103. }
  6104. // ggml_compute_forward_mul_mat
  6105. static void ggml_compute_forward_mul_mat_one_chunk(
  6106. const struct ggml_compute_params * params,
  6107. struct ggml_tensor * dst,
  6108. const enum ggml_type type,
  6109. const int64_t num_rows_per_vec_dot,
  6110. const int64_t ir0_start,
  6111. const int64_t ir0_end,
  6112. const int64_t ir1_start,
  6113. const int64_t ir1_end) {
  6114. const struct ggml_tensor * src0 = dst->src[0];
  6115. const struct ggml_tensor * src1 = dst->src[1];
  6116. GGML_TENSOR_BINARY_OP_LOCALS
  6117. const bool src1_cont = ggml_is_contiguous(src1);
  6118. ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot;
  6119. enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type;
  6120. // broadcast factors
  6121. const int64_t r2 = ne12 / ne02;
  6122. const int64_t r3 = ne13 / ne03;
  6123. //printf("ir0_start = %6lld, ir0_end = %6lld, ir1_start = %6lld, ir1_end = %6lld\n", ir0_start, ir0_end, ir1_start, ir1_end);
  6124. // threads with no work simply yield (not sure if it helps)
  6125. if (ir0_start >= ir0_end || ir1_start >= ir1_end) {
  6126. return;
  6127. }
  6128. const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
  6129. const size_t row_size = ggml_row_size(vec_dot_type, ne10);
  6130. assert(ne12 % ne02 == 0);
  6131. assert(ne13 % ne03 == 0);
  6132. // block-tiling attempt
  6133. const int64_t blck_0 = 16;
  6134. const int64_t blck_1 = 16;
  6135. const size_t src1_col_stride = src1_cont || src1->type != vec_dot_type ? row_size : nb11;
  6136. // attempt to reduce false-sharing (does not seem to make a difference)
  6137. // 16 * 2, accounting for mmla kernels
  6138. float tmp[32];
  6139. for (int64_t iir1 = ir1_start; iir1 < ir1_end; iir1 += blck_1) {
  6140. for (int64_t iir0 = ir0_start; iir0 < ir0_end; iir0 += blck_0) {
  6141. for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir1_end; ir1 += num_rows_per_vec_dot) {
  6142. const int64_t i13 = (ir1 / (ne12 * ne1));
  6143. const int64_t i12 = (ir1 - i13 * ne12 * ne1) / ne1;
  6144. const int64_t i11 = (ir1 - i13 * ne12 * ne1 - i12 * ne1);
  6145. // broadcast src0 into src1
  6146. const int64_t i03 = i13 / r3;
  6147. const int64_t i02 = i12 / r2;
  6148. const int64_t i1 = i11;
  6149. const int64_t i2 = i12;
  6150. const int64_t i3 = i13;
  6151. const char * src0_row = (const char*)src0->data + (0 + i02 * nb02 + i03 * nb03);
  6152. // desc: when src1 is not a contiguous memory block we have to calculate the offset using the strides
  6153. // if it is, then we have either copied the data to params->wdata and made it contiguous or we are using
  6154. // the original src1 data pointer, so we should index using the indices directly
  6155. // TODO: this is a bit of a hack, we should probably have a better way to handle this
  6156. const char * src1_col = (const char*)wdata +
  6157. (src1_cont || src1->type != vec_dot_type
  6158. ? (i11 + i12 * ne11 + i13 * ne12 * ne11) * row_size
  6159. : (i11 * nb11 + i12 * nb12 + i13 * nb13));
  6160. float * dst_col = (float*)((char*)dst->data + (i1 * nb1 + i2 * nb2 + i3 * nb3));
  6161. //for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ++ir0) {
  6162. // vec_dot(ne00, &dst_col[ir0], src0_row + ir0*nb01, src1_col);
  6163. //}
  6164. for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ir0 += num_rows_per_vec_dot) {
  6165. vec_dot(ne00, &tmp[ir0 - iir0], (num_rows_per_vec_dot > 1 ? 16 : 0), src0_row + ir0 * nb01, (num_rows_per_vec_dot > 1 ? nb01 : 0), src1_col, (num_rows_per_vec_dot > 1 ? src1_col_stride : 0), num_rows_per_vec_dot);
  6166. }
  6167. for (int cn = 0; cn < num_rows_per_vec_dot; ++cn) {
  6168. memcpy(&dst_col[iir0 + cn * nb1 / nb0], tmp + (cn * 16), (MIN(iir0 + blck_0, ir0_end) - iir0) * sizeof(float));
  6169. }
  6170. }
  6171. }
  6172. }
  6173. }
  6174. static void ggml_compute_forward_mul_mat(
  6175. const struct ggml_compute_params * params,
  6176. struct ggml_tensor * dst) {
  6177. const struct ggml_tensor * src0 = dst->src[0];
  6178. const struct ggml_tensor * src1 = dst->src[1];
  6179. GGML_TENSOR_BINARY_OP_LOCALS
  6180. const int ith = params->ith;
  6181. const int nth = params->nth;
  6182. enum ggml_type const vec_dot_type = type_traits_cpu[src0->type].vec_dot_type;
  6183. ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float;
  6184. int64_t const vec_dot_num_rows = type_traits_cpu[src0->type].nrows;
  6185. GGML_ASSERT(ne0 == ne01);
  6186. GGML_ASSERT(ne1 == ne11);
  6187. GGML_ASSERT(ne2 == ne12);
  6188. GGML_ASSERT(ne3 == ne13);
  6189. // we don't support permuted src0 or src1
  6190. GGML_ASSERT(nb00 == ggml_type_size(src0->type));
  6191. GGML_ASSERT(nb10 == ggml_type_size(src1->type));
  6192. // dst cannot be transposed or permuted
  6193. GGML_ASSERT(nb0 == sizeof(float));
  6194. GGML_ASSERT(nb0 <= nb1);
  6195. GGML_ASSERT(nb1 <= nb2);
  6196. GGML_ASSERT(nb2 <= nb3);
  6197. // nb01 >= nb00 - src0 is not transposed
  6198. // compute by src0 rows
  6199. // TODO: extract to "extra_op"
  6200. #if GGML_USE_LLAMAFILE
  6201. // broadcast factors
  6202. const int64_t r2 = ne12 / ne02;
  6203. const int64_t r3 = ne13 / ne03;
  6204. const bool src1_cont = ggml_is_contiguous(src1);
  6205. if (src1_cont) {
  6206. for (int64_t i13 = 0; i13 < ne13; i13++)
  6207. for (int64_t i12 = 0; i12 < ne12; i12++)
  6208. if (!llamafile_sgemm(params,
  6209. ne01, ne11, ne00/ggml_blck_size(src0->type),
  6210. (const char *)src0->data + i12/r2*nb02 + i13/r3*nb03,
  6211. nb01/ggml_type_size(src0->type),
  6212. (const char *)src1->data + i12*nb12 + i13*nb13,
  6213. nb11/ggml_type_size(src1->type),
  6214. (char *)dst->data + i12*nb2 + i13*nb3,
  6215. nb1/ggml_type_size(dst->type),
  6216. src0->type,
  6217. src1->type,
  6218. dst->type))
  6219. goto UseGgmlGemm1;
  6220. return;
  6221. }
  6222. UseGgmlGemm1:;
  6223. #endif
  6224. if (src1->type != vec_dot_type) {
  6225. char * wdata = params->wdata;
  6226. const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
  6227. const size_t nbw2 = nbw1*ne11;
  6228. const size_t nbw3 = nbw2*ne12;
  6229. assert(params->wsize >= ne13*nbw3);
  6230. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6231. for (int64_t i13 = 0; i13 < ne13; ++i13) {
  6232. for (int64_t i12 = 0; i12 < ne12; ++i12) {
  6233. for (int64_t i11 = ith; i11 < ne11; i11 += nth) {
  6234. from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11),
  6235. (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1),
  6236. ne10);
  6237. }
  6238. }
  6239. }
  6240. }
  6241. if (ith == 0) {
  6242. // Every thread starts at ith, so the first unprocessed chunk is nth. This save a bit of coordination right at the start.
  6243. atomic_store_explicit(&params->threadpool->current_chunk, nth, memory_order_relaxed);
  6244. }
  6245. ggml_barrier(params->threadpool);
  6246. #if GGML_USE_LLAMAFILE
  6247. if (src1->type != vec_dot_type) {
  6248. const void* wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
  6249. const size_t row_size = ggml_row_size(vec_dot_type, ne10);
  6250. for (int64_t i13 = 0; i13 < ne13; i13++)
  6251. for (int64_t i12 = 0; i12 < ne12; i12++)
  6252. if (!llamafile_sgemm(params,
  6253. ne01, ne11, ne00/ggml_blck_size(src0->type),
  6254. (const char *)src0->data + i12/r2*nb02 + i13/r3*nb03,
  6255. nb01/ggml_type_size(src0->type),
  6256. (const char *)wdata + (i12*ne11 + i13*ne12*ne11)*row_size,
  6257. row_size/ggml_type_size(vec_dot_type),
  6258. (char *)dst->data + i12*nb2 + i13*nb3,
  6259. nb1/ggml_type_size(dst->type),
  6260. src0->type,
  6261. vec_dot_type,
  6262. dst->type))
  6263. goto UseGgmlGemm2;
  6264. return;
  6265. }
  6266. UseGgmlGemm2:;
  6267. #endif
  6268. // This is the size of the first dimension of the result, so we can iterate that way. (see the ASSERT above, these are the same numbers)
  6269. const int64_t nr0 = ne0;
  6270. // This is the size of the rest of the dimensions of the result
  6271. const int64_t nr1 = ne1 * ne2 * ne3;
  6272. // Now select a reasonable chunk size.
  6273. int chunk_size = 16;
  6274. // We need to step up the size if it's small
  6275. if (nr0 == 1 || nr1 == 1) {
  6276. chunk_size = 64;
  6277. }
  6278. // distribute the work across the inner or outer loop based on which one is larger
  6279. // The number of chunks in the 0/1 dim.
  6280. // CEIL(nr0/chunk_size)
  6281. int64_t nchunk0 = (nr0 + chunk_size - 1) / chunk_size;
  6282. int64_t nchunk1 = (nr1 + chunk_size - 1) / chunk_size;
  6283. // If the chunking is poor for the number of threads on this setup, scrap the whole plan. Re-chunk it by thread.
  6284. // Also, chunking by thread was measured to have perform better on NUMA systems. See https://github.com/ggerganov/llama.cpp/pull/6915
  6285. // In theory, chunking should be just as useful on NUMA and non NUMA systems, but testing disagreed with that.
  6286. if (nchunk0 * nchunk1 < nth * 4 || ggml_is_numa()) {
  6287. // distribute the thread work across the inner or outer loop based on which one is larger
  6288. nchunk0 = nr0 > nr1 ? nth : 1; // parallelize by src0 rows
  6289. nchunk1 = nr0 > nr1 ? 1 : nth; // parallelize by src1 rows
  6290. }
  6291. // The number of elements in each chunk
  6292. const int64_t dr0 = (nr0 + nchunk0 - 1) / nchunk0;
  6293. const int64_t dr1 = (nr1 + nchunk1 - 1) / nchunk1;
  6294. // The first chunk comes from our thread_id, the rest will get auto-assigned.
  6295. int current_chunk = ith;
  6296. while (current_chunk < nchunk0 * nchunk1) {
  6297. const int64_t ith0 = current_chunk % nchunk0;
  6298. const int64_t ith1 = current_chunk / nchunk0;
  6299. const int64_t ir0_start = dr0 * ith0;
  6300. const int64_t ir0_end = MIN(ir0_start + dr0, nr0);
  6301. const int64_t ir1_start = dr1 * ith1;
  6302. const int64_t ir1_end = MIN(ir1_start + dr1, nr1);
  6303. // dot kernels can handle 1 row and col at a time, but mmla kernels can process 2 rows and cols
  6304. int64_t num_rows_per_vec_dot = vec_dot_num_rows;
  6305. // these checks are needed to avoid crossing dim1 boundaries
  6306. // can be optimized, but the logic would become more complicated, so keeping it like this for simplicity
  6307. if ((nr0 % 2 != 0) || (ne11 % 2 != 0) || ((ir0_end - ir0_start) % 2 != 0) || ((ir1_end - ir1_start) % 2 != 0)) {
  6308. num_rows_per_vec_dot = 1;
  6309. }
  6310. ggml_compute_forward_mul_mat_one_chunk(params, dst, src0->type, num_rows_per_vec_dot, ir0_start, ir0_end, ir1_start, ir1_end);
  6311. if (nth >= nchunk0 * nchunk1) {
  6312. break;
  6313. }
  6314. current_chunk = atomic_fetch_add_explicit(&params->threadpool->current_chunk, 1, memory_order_relaxed);
  6315. }
  6316. }
  6317. // ggml_compute_forward_mul_mat_id
  6318. static void ggml_compute_forward_mul_mat_id(
  6319. const struct ggml_compute_params * params,
  6320. struct ggml_tensor * dst) {
  6321. const struct ggml_tensor * src0 = dst->src[0];
  6322. const struct ggml_tensor * src1 = dst->src[1];
  6323. const struct ggml_tensor * ids = dst->src[2];
  6324. GGML_TENSOR_BINARY_OP_LOCALS
  6325. const int ith = params->ith;
  6326. const int nth = params->nth;
  6327. const enum ggml_type type = src0->type;
  6328. const bool src1_cont = ggml_is_contiguous(src1);
  6329. ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot;
  6330. enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type;
  6331. ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float;
  6332. // we don't support permuted src0 or src1
  6333. GGML_ASSERT(nb00 == ggml_type_size(type));
  6334. GGML_ASSERT(nb10 == ggml_type_size(src1->type));
  6335. // dst cannot be transposed or permuted
  6336. GGML_ASSERT(nb0 == sizeof(float));
  6337. GGML_ASSERT(nb0 <= nb1);
  6338. GGML_ASSERT(nb1 <= nb2);
  6339. GGML_ASSERT(nb2 <= nb3);
  6340. // row groups
  6341. const int n_ids = ids->ne[0]; // n_expert_used
  6342. const int n_as = ne02; // n_expert
  6343. char * wdata_src1_end = (src1->type == vec_dot_type) ?
  6344. (char *) params->wdata :
  6345. (char *) params->wdata + GGML_PAD(ggml_row_size(vec_dot_type, ggml_nelements(src1)), sizeof(int64_t));
  6346. struct mmid_row_mapping {
  6347. int32_t i1;
  6348. int32_t i2;
  6349. };
  6350. int64_t * matrix_row_counts = (int64_t *) (wdata_src1_end); // [n_as]
  6351. struct mmid_row_mapping * matrix_rows = (struct mmid_row_mapping *)(matrix_row_counts + n_as); // [n_as][ne11]
  6352. if (src1->type != vec_dot_type) {
  6353. char * wdata = params->wdata;
  6354. const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
  6355. const size_t nbw2 = nbw1*ne11;
  6356. const size_t nbw3 = nbw2*ne12;
  6357. assert(params->wsize >= ne13*nbw3);
  6358. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6359. for (int64_t i13 = 0; i13 < ne13; ++i13) {
  6360. for (int64_t i12 = 0; i12 < ne12; ++i12) {
  6361. for (int64_t i11 = ith; i11 < ne11; i11 += nth) {
  6362. from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11),
  6363. (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1),
  6364. ne10);
  6365. }
  6366. }
  6367. }
  6368. }
  6369. #define MMID_MATRIX_ROW(row_id, i1) matrix_rows[(row_id)*ne12 + (i1)]
  6370. if (ith == 0) {
  6371. // initialize matrix_row_counts
  6372. memset(matrix_row_counts, 0, n_as*sizeof(int64_t));
  6373. // group rows by src0 matrix
  6374. for (int64_t iid1 = 0; iid1 < ids->ne[1]; ++iid1) {
  6375. for (int id = 0; id < n_ids; ++id) {
  6376. const int32_t i02 = *(const int32_t *) ((const char *) ids->data + iid1*ids->nb[1] + id*ids->nb[0]);
  6377. assert(i02 >= 0 && i02 < n_as);
  6378. MMID_MATRIX_ROW(i02, matrix_row_counts[i02]) = (struct mmid_row_mapping) {id, iid1};
  6379. matrix_row_counts[i02] += 1;
  6380. }
  6381. }
  6382. }
  6383. ggml_barrier(params->threadpool);
  6384. // compute each matrix multiplication in sequence
  6385. for (int cur_a = 0; cur_a < n_as; ++cur_a) {
  6386. const int64_t cne1 = matrix_row_counts[cur_a];
  6387. if (cne1 == 0) {
  6388. continue;
  6389. }
  6390. const char * src0_cur = (const char *) src0->data + cur_a*nb02;
  6391. const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
  6392. const size_t row_size = ggml_row_size(vec_dot_type, ne10);
  6393. const int64_t nr0 = ne01; // src0 rows
  6394. const int64_t nr1 = cne1; // src1 rows
  6395. // distribute the thread work across the inner or outer loop based on which one is larger
  6396. const int64_t nth0 = nr0 > nr1 ? nth : 1; // parallelize by src0 rows
  6397. const int64_t nth1 = nr0 > nr1 ? 1 : nth; // parallelize by src1 rows
  6398. const int64_t ith0 = ith % nth0;
  6399. const int64_t ith1 = ith / nth0;
  6400. const int64_t dr0 = (nr0 + nth0 - 1)/nth0;
  6401. const int64_t dr1 = (nr1 + nth1 - 1)/nth1;
  6402. const int64_t ir010 = dr0*ith0;
  6403. const int64_t ir011 = MIN(ir010 + dr0, nr0);
  6404. const int64_t ir110 = dr1*ith1;
  6405. const int64_t ir111 = MIN(ir110 + dr1, nr1);
  6406. // threads with no work simply yield (not sure if it helps)
  6407. //if (ir010 >= ir011 || ir110 >= ir111) {
  6408. // sched_yield();
  6409. // continue;
  6410. //}
  6411. // block-tiling attempt
  6412. const int64_t blck_0 = 16;
  6413. const int64_t blck_1 = 16;
  6414. // attempt to reduce false-sharing (does not seem to make a difference)
  6415. float tmp[16];
  6416. for (int64_t iir1 = ir110; iir1 < ir111; iir1 += blck_1) {
  6417. for (int64_t iir0 = ir010; iir0 < ir011; iir0 += blck_0) {
  6418. for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir111; ++ir1) {
  6419. const int64_t _i12 = ir1; // logical row index for this expert
  6420. struct mmid_row_mapping row_mapping = MMID_MATRIX_ROW(cur_a, _i12);
  6421. const int id = row_mapping.i1; // selected expert index
  6422. const int64_t i11 = id % ne11;
  6423. const int64_t i12 = row_mapping.i2; // row index in src1
  6424. const int64_t i1 = id; // selected expert index
  6425. const int64_t i2 = i12; // row
  6426. // desc: when src1 is not a contiguous memory block we have to calculate the offset using the strides
  6427. // if it is, then we have either copied the data to params->wdata and made it contiguous or we are using
  6428. // the original src1 data pointer, so we should index using the indices directly
  6429. // TODO: this is a bit of a hack, we should probably have a better way to handle this
  6430. const char * src1_col = (const char *) wdata +
  6431. (src1_cont || src1->type != vec_dot_type
  6432. ? (i11 + i12*ne11)*row_size
  6433. : (i11*nb11 + i12*nb12));
  6434. float * dst_col = (float *) ((char *) dst->data + (i1*nb1 + i2*nb2));
  6435. //for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir011; ++ir0) {
  6436. // vec_dot(ne00, &dst_col[ir0], src0_row + ir0*nb01, src1_col);
  6437. //}
  6438. for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir011; ++ir0) {
  6439. vec_dot(ne00, &tmp[ir0 - iir0], 0, src0_cur + ir0*nb01, 0, src1_col, 0, 1);
  6440. }
  6441. memcpy(&dst_col[iir0], tmp, (MIN(iir0 + blck_0, ir011) - iir0)*sizeof(float));
  6442. }
  6443. }
  6444. }
  6445. }
  6446. #undef MMID_MATRIX_ROW
  6447. }
  6448. // ggml_compute_forward_out_prod
  6449. static void ggml_compute_forward_out_prod_f32(
  6450. const struct ggml_compute_params * params,
  6451. struct ggml_tensor * dst) {
  6452. const struct ggml_tensor * src0 = dst->src[0];
  6453. const struct ggml_tensor * src1 = dst->src[1];
  6454. GGML_TENSOR_BINARY_OP_LOCALS
  6455. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6456. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6457. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6458. const int ith = params->ith;
  6459. const int nth = params->nth;
  6460. GGML_ASSERT(ne0 == ne00);
  6461. GGML_ASSERT(ne1 == ne10);
  6462. GGML_ASSERT(ne2 == ne12);
  6463. GGML_ASSERT(ne3 == ne13);
  6464. GGML_ASSERT(ne2 % ne02 == 0);
  6465. GGML_ASSERT(ne3 % ne03 == 0);
  6466. // we don't support permuted src0 or src1
  6467. GGML_ASSERT(nb00 == sizeof(float));
  6468. // dst cannot be transposed or permuted
  6469. GGML_ASSERT(nb0 == sizeof(float));
  6470. // GGML_ASSERT(nb0 <= nb1);
  6471. // GGML_ASSERT(nb1 <= nb2);
  6472. // GGML_ASSERT(nb2 <= nb3);
  6473. // nb01 >= nb00 - src0 is not transposed
  6474. // compute by src0 rows
  6475. if (ith == 0) {
  6476. ggml_vec_set_f32(ne0*ne1*ne2*ne3, dst->data, 0);
  6477. }
  6478. ggml_barrier(params->threadpool);
  6479. // dst[:,:,:,:] = 0
  6480. // for i2,i3:
  6481. // for i1:
  6482. // for i01:
  6483. // for i0:
  6484. // dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3]
  6485. // parallelize by last three dimensions
  6486. // total rows in dst
  6487. const int64_t nr = ne1*ne2*ne3;
  6488. // rows per thread
  6489. const int64_t dr = (nr + nth - 1)/nth;
  6490. // row range for this thread
  6491. const int64_t ir0 = dr*ith;
  6492. const int64_t ir1 = MIN(ir0 + dr, nr);
  6493. // block-tiling attempt
  6494. const int64_t blck_0 = MAX(GGML_VEC_MAD_UNROLL, 32);
  6495. const int64_t blck_1 = 16;
  6496. // dps == dst per src0, used for group query attention
  6497. const int64_t dps2 = ne2 / ne02;
  6498. const int64_t dps3 = ne3 / ne03;
  6499. for (int64_t bir = ir0; bir < ir1; bir += blck_1) {
  6500. const int64_t bir1 = MIN(bir + blck_1, ir1);
  6501. for (int64_t bi01 = 0; bi01 < ne01; bi01 += blck_0) {
  6502. const int64_t bne01 = MIN(bi01 + blck_0, ne01);
  6503. for (int64_t ir = bir; ir < bir1; ++ir) {
  6504. // dst indices
  6505. const int64_t i3 = ir/(ne2*ne1);
  6506. const int64_t i2 = (ir - i3*ne2*ne1)/ne1;
  6507. const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1);
  6508. const int64_t i02 = i2 / dps2;
  6509. const int64_t i03 = i3 / dps3;
  6510. //const int64_t i10 = i1;
  6511. const int64_t i12 = i2;
  6512. const int64_t i13 = i3;
  6513. #if GGML_VEC_MAD_UNROLL > 2
  6514. const int64_t bne01_unroll = bne01 - (bne01 % GGML_VEC_MAD_UNROLL);
  6515. for (int64_t i01 = bi01; i01 < bne01_unroll; i01 += GGML_VEC_MAD_UNROLL) {
  6516. const int64_t i11 = i01;
  6517. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6518. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6519. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6520. ggml_vec_mad_f32_unroll(ne0, nb01, nb11, d, s0, s1);
  6521. }
  6522. for (int64_t i01 = bne01_unroll; i01 < bne01; ++i01) {
  6523. const int64_t i11 = i01;
  6524. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6525. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6526. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6527. ggml_vec_mad_f32(ne0, d, s0, *s1);
  6528. }
  6529. #else
  6530. for (int64_t i01 = bi01; i01 < bne01; ++i01) {
  6531. const int64_t i11 = i01;
  6532. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6533. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6534. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6535. ggml_vec_mad_f32(ne0, d, s0, *s1);
  6536. }
  6537. #endif
  6538. }
  6539. }
  6540. }
  6541. }
  6542. static void ggml_compute_forward_out_prod_q_f32(
  6543. const struct ggml_compute_params * params,
  6544. struct ggml_tensor * dst) {
  6545. const struct ggml_tensor * src0 = dst->src[0];
  6546. const struct ggml_tensor * src1 = dst->src[1];
  6547. GGML_TENSOR_BINARY_OP_LOCALS;
  6548. const int ith = params->ith;
  6549. const int nth = params->nth;
  6550. const enum ggml_type type = src0->type;
  6551. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  6552. GGML_ASSERT(ne02 == ne12);
  6553. GGML_ASSERT(ne03 == ne13);
  6554. GGML_ASSERT(ne2 == ne12);
  6555. GGML_ASSERT(ne3 == ne13);
  6556. // we don't support permuted src0 dim0
  6557. GGML_ASSERT(nb00 == ggml_type_size(type));
  6558. // dst dim0 cannot be transposed or permuted
  6559. GGML_ASSERT(nb0 == sizeof(float));
  6560. // GGML_ASSERT(nb0 <= nb1);
  6561. // GGML_ASSERT(nb1 <= nb2);
  6562. // GGML_ASSERT(nb2 <= nb3);
  6563. GGML_ASSERT(ne0 == ne00);
  6564. GGML_ASSERT(ne1 == ne10);
  6565. GGML_ASSERT(ne2 == ne02);
  6566. GGML_ASSERT(ne3 == ne03);
  6567. // nb01 >= nb00 - src0 is not transposed
  6568. // compute by src0 rows
  6569. if (ith == 0) {
  6570. ggml_vec_set_f32(ne0*ne1*ne2*ne3, dst->data, 0);
  6571. }
  6572. ggml_barrier(params->threadpool);
  6573. // parallelize by last three dimensions
  6574. // total rows in dst
  6575. const int64_t nr = ne1*ne2*ne3;
  6576. // rows per thread
  6577. const int64_t dr = (nr + nth - 1)/nth;
  6578. // row range for this thread
  6579. const int64_t ir0 = dr*ith;
  6580. const int64_t ir1 = MIN(ir0 + dr, nr);
  6581. // dst[:,:,:,:] = 0
  6582. // for i2,i3:
  6583. // for i1:
  6584. // for i01:
  6585. // for i0:
  6586. // dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3]
  6587. float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith;
  6588. for (int64_t ir = ir0; ir < ir1; ++ir) {
  6589. // dst indices
  6590. const int64_t i3 = ir/(ne2*ne1);
  6591. const int64_t i2 = (ir - i3*ne2*ne1)/ne1;
  6592. const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1);
  6593. const int64_t i02 = i2;
  6594. const int64_t i03 = i3;
  6595. //const int64_t i10 = i1;
  6596. const int64_t i12 = i2;
  6597. const int64_t i13 = i3;
  6598. for (int64_t i01 = 0; i01 < ne01; ++i01) {
  6599. const int64_t i11 = i01;
  6600. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6601. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6602. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6603. dequantize_row_q(s0, wdata, ne0);
  6604. ggml_vec_mad_f32(ne0, d, wdata, *s1);
  6605. }
  6606. }
  6607. }
  6608. static void ggml_compute_forward_out_prod(
  6609. const struct ggml_compute_params * params,
  6610. struct ggml_tensor * dst) {
  6611. const struct ggml_tensor * src0 = dst->src[0];
  6612. switch (src0->type) {
  6613. case GGML_TYPE_Q4_0:
  6614. case GGML_TYPE_Q4_1:
  6615. case GGML_TYPE_Q5_0:
  6616. case GGML_TYPE_Q5_1:
  6617. case GGML_TYPE_Q8_0:
  6618. case GGML_TYPE_Q2_K:
  6619. case GGML_TYPE_Q3_K:
  6620. case GGML_TYPE_Q4_K:
  6621. case GGML_TYPE_Q5_K:
  6622. case GGML_TYPE_Q6_K:
  6623. case GGML_TYPE_TQ1_0:
  6624. case GGML_TYPE_TQ2_0:
  6625. case GGML_TYPE_IQ2_XXS:
  6626. case GGML_TYPE_IQ2_XS:
  6627. case GGML_TYPE_IQ3_XXS:
  6628. case GGML_TYPE_IQ1_S:
  6629. case GGML_TYPE_IQ1_M:
  6630. case GGML_TYPE_IQ4_NL:
  6631. case GGML_TYPE_IQ4_XS:
  6632. case GGML_TYPE_IQ3_S:
  6633. case GGML_TYPE_IQ2_S:
  6634. {
  6635. ggml_compute_forward_out_prod_q_f32(params, dst);
  6636. } break;
  6637. case GGML_TYPE_F16:
  6638. {
  6639. GGML_ABORT("fatal error"); // todo
  6640. // ggml_compute_forward_out_prod_f16_f32(params, dst);
  6641. }
  6642. case GGML_TYPE_F32:
  6643. {
  6644. ggml_compute_forward_out_prod_f32(params, dst);
  6645. } break;
  6646. default:
  6647. {
  6648. GGML_ABORT("fatal error");
  6649. }
  6650. }
  6651. }
  6652. // ggml_compute_forward_scale
  6653. static void ggml_compute_forward_scale_f32(
  6654. const struct ggml_compute_params * params,
  6655. struct ggml_tensor * dst) {
  6656. const struct ggml_tensor * src0 = dst->src[0];
  6657. GGML_ASSERT(ggml_is_contiguous(src0));
  6658. GGML_ASSERT(ggml_is_contiguous(dst));
  6659. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6660. // scale factor
  6661. float v;
  6662. memcpy(&v, dst->op_params, sizeof(float));
  6663. const int ith = params->ith;
  6664. const int nth = params->nth;
  6665. const int nc = src0->ne[0];
  6666. const int nr = ggml_nrows(src0);
  6667. // rows per thread
  6668. const int dr = (nr + nth - 1)/nth;
  6669. // row range for this thread
  6670. const int ir0 = dr*ith;
  6671. const int ir1 = MIN(ir0 + dr, nr);
  6672. const size_t nb01 = src0->nb[1];
  6673. const size_t nb1 = dst->nb[1];
  6674. for (int i1 = ir0; i1 < ir1; i1++) {
  6675. if (dst->data != src0->data) {
  6676. // src0 is same shape as dst => same indices
  6677. memcpy((char *)dst->data + i1*nb1, (char *)src0->data + i1*nb01, nc * sizeof(float));
  6678. }
  6679. ggml_vec_scale_f32(nc, (float *) ((char *) dst->data + i1*nb1), v);
  6680. }
  6681. }
  6682. static void ggml_compute_forward_scale(
  6683. const struct ggml_compute_params * params,
  6684. struct ggml_tensor * dst) {
  6685. const struct ggml_tensor * src0 = dst->src[0];
  6686. switch (src0->type) {
  6687. case GGML_TYPE_F32:
  6688. {
  6689. ggml_compute_forward_scale_f32(params, dst);
  6690. } break;
  6691. default:
  6692. {
  6693. GGML_ABORT("fatal error");
  6694. }
  6695. }
  6696. }
  6697. // ggml_compute_forward_set
  6698. static void ggml_compute_forward_set_f32(
  6699. const struct ggml_compute_params * params,
  6700. struct ggml_tensor * dst) {
  6701. const struct ggml_tensor * src0 = dst->src[0];
  6702. const struct ggml_tensor * src1 = dst->src[1];
  6703. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6704. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  6705. // view src0 and dst with these strides and data offset inbytes during set
  6706. // nb0 is implicitly element_size because src0 and dst are contiguous
  6707. size_t nb1 = ((int32_t *) dst->op_params)[0];
  6708. size_t nb2 = ((int32_t *) dst->op_params)[1];
  6709. size_t nb3 = ((int32_t *) dst->op_params)[2];
  6710. size_t offset = ((int32_t *) dst->op_params)[3];
  6711. bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  6712. if (!inplace) {
  6713. if (params->ith == 0) {
  6714. // memcpy needs to be synchronized across threads to avoid race conditions.
  6715. // => do it in INIT phase
  6716. memcpy(
  6717. ((char *) dst->data),
  6718. ((char *) src0->data),
  6719. ggml_nbytes(dst));
  6720. }
  6721. ggml_barrier(params->threadpool);
  6722. }
  6723. const int ith = params->ith;
  6724. const int nth = params->nth;
  6725. const int nr = ggml_nrows(src1);
  6726. const int nc = src1->ne[0];
  6727. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne)
  6728. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  6729. // src0 and dst as viewed during set
  6730. const size_t nb0 = ggml_element_size(src0);
  6731. const int im0 = (ne10 == 0 ? 0 : ne10-1);
  6732. const int im1 = (ne11 == 0 ? 0 : ne11-1);
  6733. const int im2 = (ne12 == 0 ? 0 : ne12-1);
  6734. const int im3 = (ne13 == 0 ? 0 : ne13-1);
  6735. GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst));
  6736. GGML_ASSERT(nb10 == sizeof(float));
  6737. // rows per thread
  6738. const int dr = (nr + nth - 1)/nth;
  6739. // row range for this thread
  6740. const int ir0 = dr*ith;
  6741. const int ir1 = MIN(ir0 + dr, nr);
  6742. for (int ir = ir0; ir < ir1; ++ir) {
  6743. // src0 and dst are viewed with shape of src1 and offset
  6744. // => same indices
  6745. const int i3 = ir/(ne12*ne11);
  6746. const int i2 = (ir - i3*ne12*ne11)/ne11;
  6747. const int i1 = (ir - i3*ne12*ne11 - i2*ne11);
  6748. ggml_vec_cpy_f32(nc,
  6749. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset),
  6750. (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
  6751. }
  6752. }
  6753. static void ggml_compute_forward_set_i32(
  6754. const struct ggml_compute_params * params,
  6755. struct ggml_tensor * dst) {
  6756. const struct ggml_tensor * src0 = dst->src[0];
  6757. const struct ggml_tensor * src1 = dst->src[1];
  6758. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6759. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  6760. // view src0 and dst with these strides and data offset inbytes during set
  6761. // nb0 is implicitly element_size because src0 and dst are contiguous
  6762. size_t nb1 = ((int32_t *) dst->op_params)[0];
  6763. size_t nb2 = ((int32_t *) dst->op_params)[1];
  6764. size_t nb3 = ((int32_t *) dst->op_params)[2];
  6765. size_t offset = ((int32_t *) dst->op_params)[3];
  6766. bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  6767. if (!inplace) {
  6768. if (params->ith == 0) {
  6769. // memcpy needs to be synchronized across threads to avoid race conditions.
  6770. // => do it in INIT phase
  6771. memcpy(
  6772. ((char *) dst->data),
  6773. ((char *) src0->data),
  6774. ggml_nbytes(dst));
  6775. }
  6776. ggml_barrier(params->threadpool);
  6777. }
  6778. const int ith = params->ith;
  6779. const int nth = params->nth;
  6780. const int nr = ggml_nrows(src1);
  6781. const int nc = src1->ne[0];
  6782. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne)
  6783. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  6784. // src0 and dst as viewed during set
  6785. const size_t nb0 = ggml_element_size(src0);
  6786. const int im0 = (ne10 == 0 ? 0 : ne10-1);
  6787. const int im1 = (ne11 == 0 ? 0 : ne11-1);
  6788. const int im2 = (ne12 == 0 ? 0 : ne12-1);
  6789. const int im3 = (ne13 == 0 ? 0 : ne13-1);
  6790. GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst));
  6791. GGML_ASSERT(nb10 == sizeof(int32_t));
  6792. // rows per thread
  6793. const int dr = (nr + nth - 1)/nth;
  6794. // row range for this thread
  6795. const int ir0 = dr*ith;
  6796. const int ir1 = MIN(ir0 + dr, nr);
  6797. for (int ir = ir0; ir < ir1; ++ir) {
  6798. // src0 and dst are viewed with shape of src1 and offset
  6799. // => same indices
  6800. const int i3 = ir/(ne12*ne11);
  6801. const int i2 = (ir - i3*ne12*ne11)/ne11;
  6802. const int i1 = (ir - i3*ne12*ne11 - i2*ne11);
  6803. ggml_vec_cpy_i32(nc,
  6804. (int32_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset),
  6805. (int32_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
  6806. }
  6807. }
  6808. static void ggml_compute_forward_set(
  6809. const struct ggml_compute_params * params,
  6810. struct ggml_tensor * dst) {
  6811. const struct ggml_tensor * src0 = dst->src[0];
  6812. switch (src0->type) {
  6813. case GGML_TYPE_F32:
  6814. {
  6815. ggml_compute_forward_set_f32(params, dst);
  6816. } break;
  6817. case GGML_TYPE_I32:
  6818. {
  6819. ggml_compute_forward_set_i32(params, dst);
  6820. } break;
  6821. case GGML_TYPE_F16:
  6822. case GGML_TYPE_BF16:
  6823. case GGML_TYPE_Q4_0:
  6824. case GGML_TYPE_Q4_1:
  6825. case GGML_TYPE_Q5_0:
  6826. case GGML_TYPE_Q5_1:
  6827. case GGML_TYPE_Q8_0:
  6828. case GGML_TYPE_Q8_1:
  6829. case GGML_TYPE_Q2_K:
  6830. case GGML_TYPE_Q3_K:
  6831. case GGML_TYPE_Q4_K:
  6832. case GGML_TYPE_Q5_K:
  6833. case GGML_TYPE_Q6_K:
  6834. case GGML_TYPE_TQ1_0:
  6835. case GGML_TYPE_TQ2_0:
  6836. case GGML_TYPE_IQ2_XXS:
  6837. case GGML_TYPE_IQ2_XS:
  6838. case GGML_TYPE_IQ3_XXS:
  6839. case GGML_TYPE_IQ1_S:
  6840. case GGML_TYPE_IQ1_M:
  6841. case GGML_TYPE_IQ4_NL:
  6842. case GGML_TYPE_IQ4_XS:
  6843. case GGML_TYPE_IQ3_S:
  6844. case GGML_TYPE_IQ2_S:
  6845. default:
  6846. {
  6847. GGML_ABORT("fatal error");
  6848. }
  6849. }
  6850. }
  6851. // ggml_compute_forward_cpy
  6852. static void ggml_compute_forward_cpy(
  6853. const struct ggml_compute_params * params,
  6854. struct ggml_tensor * dst) {
  6855. ggml_compute_forward_dup(params, dst);
  6856. }
  6857. // ggml_compute_forward_cont
  6858. static void ggml_compute_forward_cont(
  6859. const struct ggml_compute_params * params,
  6860. struct ggml_tensor * dst) {
  6861. ggml_compute_forward_dup(params, dst);
  6862. }
  6863. // ggml_compute_forward_reshape
  6864. static void ggml_compute_forward_reshape(
  6865. const struct ggml_compute_params * params,
  6866. struct ggml_tensor * dst) {
  6867. // NOP
  6868. UNUSED(params);
  6869. UNUSED(dst);
  6870. }
  6871. // ggml_compute_forward_view
  6872. static void ggml_compute_forward_view(
  6873. const struct ggml_compute_params * params,
  6874. const struct ggml_tensor * dst) {
  6875. // NOP
  6876. UNUSED(params);
  6877. UNUSED(dst);
  6878. }
  6879. // ggml_compute_forward_permute
  6880. static void ggml_compute_forward_permute(
  6881. const struct ggml_compute_params * params,
  6882. const struct ggml_tensor * dst) {
  6883. // NOP
  6884. UNUSED(params);
  6885. UNUSED(dst);
  6886. }
  6887. // ggml_compute_forward_transpose
  6888. static void ggml_compute_forward_transpose(
  6889. const struct ggml_compute_params * params,
  6890. const struct ggml_tensor * dst) {
  6891. // NOP
  6892. UNUSED(params);
  6893. UNUSED(dst);
  6894. }
  6895. // ggml_compute_forward_get_rows
  6896. static void ggml_compute_forward_get_rows_q(
  6897. const struct ggml_compute_params * params,
  6898. struct ggml_tensor * dst) {
  6899. const struct ggml_tensor * src0 = dst->src[0];
  6900. const struct ggml_tensor * src1 = dst->src[1];
  6901. GGML_TENSOR_BINARY_OP_LOCALS
  6902. const int64_t nc = ne00;
  6903. const int64_t nr = ggml_nelements(src1);
  6904. const enum ggml_type type = src0->type;
  6905. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  6906. assert(ne0 == nc);
  6907. assert(ne02 == ne11);
  6908. assert(nb00 == ggml_type_size(type));
  6909. assert(ggml_nrows(dst) == nr);
  6910. const int ith = params->ith;
  6911. const int nth = params->nth;
  6912. // rows per thread
  6913. const int dr = (nr + nth - 1)/nth;
  6914. // row range for this thread
  6915. const int ir0 = dr*ith;
  6916. const int ir1 = MIN(ir0 + dr, nr);
  6917. for (int64_t i = ir0; i < ir1; ++i) {
  6918. const int64_t i12 = i/(ne11*ne10);
  6919. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  6920. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  6921. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  6922. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  6923. dequantize_row_q(
  6924. (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03),
  6925. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc);
  6926. }
  6927. }
  6928. static void ggml_compute_forward_get_rows_f16(
  6929. const struct ggml_compute_params * params,
  6930. struct ggml_tensor * dst) {
  6931. const struct ggml_tensor * src0 = dst->src[0];
  6932. const struct ggml_tensor * src1 = dst->src[1];
  6933. GGML_TENSOR_BINARY_OP_LOCALS
  6934. const int64_t nc = ne00;
  6935. const int64_t nr = ggml_nelements(src1);
  6936. assert(ne0 == nc);
  6937. assert(ne02 == ne11);
  6938. assert(nb00 == sizeof(ggml_fp16_t));
  6939. assert(ggml_nrows(dst) == nr);
  6940. const int ith = params->ith;
  6941. const int nth = params->nth;
  6942. // rows per thread
  6943. const int dr = (nr + nth - 1)/nth;
  6944. // row range for this thread
  6945. const int ir0 = dr*ith;
  6946. const int ir1 = MIN(ir0 + dr, nr);
  6947. for (int64_t i = ir0; i < ir1; ++i) {
  6948. const int64_t i12 = i/(ne11*ne10);
  6949. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  6950. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  6951. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  6952. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  6953. ggml_fp16_to_fp32_row(
  6954. (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03),
  6955. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc);
  6956. }
  6957. }
  6958. static void ggml_compute_forward_get_rows_bf16(
  6959. const struct ggml_compute_params * params,
  6960. struct ggml_tensor * dst) {
  6961. const struct ggml_tensor * src0 = dst->src[0];
  6962. const struct ggml_tensor * src1 = dst->src[1];
  6963. GGML_TENSOR_BINARY_OP_LOCALS
  6964. const int64_t nc = ne00;
  6965. const int64_t nr = ggml_nelements(src1);
  6966. assert(ne0 == nc);
  6967. assert(ne02 == ne11);
  6968. assert(nb00 == sizeof(ggml_bf16_t));
  6969. assert(ggml_nrows(dst) == nr);
  6970. const int ith = params->ith;
  6971. const int nth = params->nth;
  6972. // rows per thread
  6973. const int dr = (nr + nth - 1)/nth;
  6974. // row range for this thread
  6975. const int ir0 = dr*ith;
  6976. const int ir1 = MIN(ir0 + dr, nr);
  6977. for (int64_t i = ir0; i < ir1; ++i) {
  6978. const int64_t i12 = i/(ne11*ne10);
  6979. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  6980. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  6981. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  6982. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  6983. ggml_bf16_to_fp32_row(
  6984. (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03),
  6985. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc);
  6986. }
  6987. }
  6988. static void ggml_compute_forward_get_rows_f32(
  6989. const struct ggml_compute_params * params,
  6990. struct ggml_tensor * dst) {
  6991. const struct ggml_tensor * src0 = dst->src[0];
  6992. const struct ggml_tensor * src1 = dst->src[1];
  6993. GGML_TENSOR_BINARY_OP_LOCALS
  6994. const int64_t nc = ne00;
  6995. const int64_t nr = ggml_nelements(src1);
  6996. assert(ne0 == nc);
  6997. assert(ne02 == ne11);
  6998. assert(nb00 == sizeof(float));
  6999. assert(ggml_nrows(dst) == nr);
  7000. const int ith = params->ith;
  7001. const int nth = params->nth;
  7002. // rows per thread
  7003. const int dr = (nr + nth - 1)/nth;
  7004. // row range for this thread
  7005. const int ir0 = dr*ith;
  7006. const int ir1 = MIN(ir0 + dr, nr);
  7007. for (int64_t i = ir0; i < ir1; ++i) {
  7008. const int64_t i12 = i/(ne11*ne10);
  7009. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  7010. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  7011. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  7012. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  7013. ggml_vec_cpy_f32(nc,
  7014. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3),
  7015. (float *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03));
  7016. }
  7017. }
  7018. static void ggml_compute_forward_get_rows(
  7019. const struct ggml_compute_params * params,
  7020. struct ggml_tensor * dst) {
  7021. const struct ggml_tensor * src0 = dst->src[0];
  7022. switch (src0->type) {
  7023. case GGML_TYPE_Q4_0:
  7024. case GGML_TYPE_Q4_1:
  7025. case GGML_TYPE_Q5_0:
  7026. case GGML_TYPE_Q5_1:
  7027. case GGML_TYPE_Q8_0:
  7028. case GGML_TYPE_Q8_1:
  7029. case GGML_TYPE_Q2_K:
  7030. case GGML_TYPE_Q3_K:
  7031. case GGML_TYPE_Q4_K:
  7032. case GGML_TYPE_Q5_K:
  7033. case GGML_TYPE_Q6_K:
  7034. case GGML_TYPE_TQ1_0:
  7035. case GGML_TYPE_TQ2_0:
  7036. case GGML_TYPE_IQ2_XXS:
  7037. case GGML_TYPE_IQ2_XS:
  7038. case GGML_TYPE_IQ3_XXS:
  7039. case GGML_TYPE_IQ1_S:
  7040. case GGML_TYPE_IQ1_M:
  7041. case GGML_TYPE_IQ4_NL:
  7042. case GGML_TYPE_IQ4_XS:
  7043. case GGML_TYPE_IQ3_S:
  7044. case GGML_TYPE_IQ2_S:
  7045. {
  7046. ggml_compute_forward_get_rows_q(params, dst);
  7047. } break;
  7048. case GGML_TYPE_F16:
  7049. {
  7050. ggml_compute_forward_get_rows_f16(params, dst);
  7051. } break;
  7052. case GGML_TYPE_BF16:
  7053. {
  7054. ggml_compute_forward_get_rows_bf16(params, dst);
  7055. } break;
  7056. case GGML_TYPE_F32:
  7057. case GGML_TYPE_I32:
  7058. {
  7059. ggml_compute_forward_get_rows_f32(params, dst);
  7060. } break;
  7061. default:
  7062. {
  7063. GGML_ABORT("fatal error");
  7064. }
  7065. }
  7066. //static bool first = true;
  7067. //printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]);
  7068. //if (first) {
  7069. // first = false;
  7070. //} else {
  7071. // for (int k = 0; k < dst->ne[1]; ++k) {
  7072. // for (int j = 0; j < dst->ne[0]/16; ++j) {
  7073. // for (int i = 0; i < 16; ++i) {
  7074. // printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]);
  7075. // }
  7076. // printf("\n");
  7077. // }
  7078. // printf("\n");
  7079. // }
  7080. // printf("\n");
  7081. // exit(0);
  7082. //}
  7083. }
  7084. // ggml_compute_forward_get_rows_back
  7085. static void ggml_compute_forward_get_rows_back_f32_f16(
  7086. const struct ggml_compute_params * params,
  7087. struct ggml_tensor * dst) {
  7088. const struct ggml_tensor * src0 = dst->src[0];
  7089. const struct ggml_tensor * src1 = dst->src[1];
  7090. if (params->ith != 0) {
  7091. return;
  7092. }
  7093. GGML_ASSERT(ggml_is_contiguous(dst));
  7094. // ggml_compute_forward_dup_same_cont(params, opt0, dst);
  7095. memset(dst->data, 0, ggml_nbytes(dst));
  7096. const int nc = src0->ne[0];
  7097. const int nr = ggml_nelements(src1);
  7098. GGML_ASSERT( dst->ne[0] == nc);
  7099. GGML_ASSERT(src0->nb[0] == sizeof(ggml_fp16_t));
  7100. for (int i = 0; i < nr; ++i) {
  7101. const int r = ((int32_t *) src1->data)[i];
  7102. for (int j = 0; j < nc; ++j) {
  7103. ggml_fp16_t v = ((ggml_fp16_t *) ((char *) src0->data + i*src0->nb[1]))[j];
  7104. ((float *) ((char *) dst->data + r*dst->nb[1]))[j] += GGML_FP16_TO_FP32(v);
  7105. }
  7106. }
  7107. }
  7108. static void ggml_compute_forward_get_rows_back_f32(
  7109. const struct ggml_compute_params * params,
  7110. struct ggml_tensor * dst) {
  7111. const struct ggml_tensor * src0 = dst->src[0];
  7112. const struct ggml_tensor * src1 = dst->src[1];
  7113. if (params->ith != 0) {
  7114. return;
  7115. }
  7116. GGML_ASSERT(ggml_is_contiguous(dst));
  7117. // ggml_compute_forward_dup_same_cont(params, opt0, dst);
  7118. memset(dst->data, 0, ggml_nbytes(dst));
  7119. const int nc = src0->ne[0];
  7120. const int nr = ggml_nelements(src1);
  7121. GGML_ASSERT( dst->ne[0] == nc);
  7122. GGML_ASSERT(src0->nb[0] == sizeof(float));
  7123. for (int i = 0; i < nr; ++i) {
  7124. const int r = ((int32_t *) src1->data)[i];
  7125. ggml_vec_add_f32(nc,
  7126. (float *) ((char *) dst->data + r*dst->nb[1]),
  7127. (float *) ((char *) dst->data + r*dst->nb[1]),
  7128. (float *) ((char *) src0->data + i*src0->nb[1]));
  7129. }
  7130. }
  7131. static void ggml_compute_forward_get_rows_back(
  7132. const struct ggml_compute_params * params,
  7133. struct ggml_tensor * dst) {
  7134. const struct ggml_tensor * src0 = dst->src[0];
  7135. switch (src0->type) {
  7136. case GGML_TYPE_F16:
  7137. {
  7138. ggml_compute_forward_get_rows_back_f32_f16(params, dst);
  7139. } break;
  7140. case GGML_TYPE_F32:
  7141. {
  7142. ggml_compute_forward_get_rows_back_f32(params, dst);
  7143. } break;
  7144. default:
  7145. {
  7146. GGML_ABORT("fatal error");
  7147. }
  7148. }
  7149. //static bool first = true;
  7150. //printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]);
  7151. //if (first) {
  7152. // first = false;
  7153. //} else {
  7154. // for (int k = 0; k < dst->ne[1]; ++k) {
  7155. // for (int j = 0; j < dst->ne[0]/16; ++j) {
  7156. // for (int i = 0; i < 16; ++i) {
  7157. // printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]);
  7158. // }
  7159. // printf("\n");
  7160. // }
  7161. // printf("\n");
  7162. // }
  7163. // printf("\n");
  7164. // exit(0);
  7165. //}
  7166. }
  7167. // ggml_compute_forward_diag
  7168. static void ggml_compute_forward_diag_f32(
  7169. const struct ggml_compute_params * params,
  7170. struct ggml_tensor * dst) {
  7171. const struct ggml_tensor * src0 = dst->src[0];
  7172. if (params->ith != 0) {
  7173. return;
  7174. }
  7175. // TODO: handle transposed/permuted matrices
  7176. GGML_TENSOR_UNARY_OP_LOCALS
  7177. GGML_ASSERT(ne00 == ne0);
  7178. GGML_ASSERT(ne00 == ne1);
  7179. GGML_ASSERT(ne01 == 1);
  7180. GGML_ASSERT(ne02 == ne2);
  7181. GGML_ASSERT(ne03 == ne3);
  7182. GGML_ASSERT(nb00 == sizeof(float));
  7183. GGML_ASSERT(nb0 == sizeof(float));
  7184. for (int i3 = 0; i3 < ne3; i3++) {
  7185. for (int i2 = 0; i2 < ne2; i2++) {
  7186. for (int i1 = 0; i1 < ne1; i1++) {
  7187. float * d = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  7188. float * s = (float *)((char *) src0->data + i3*nb03 + i2*nb02);
  7189. for (int i0 = 0; i0 < i1; i0++) {
  7190. d[i0] = 0;
  7191. }
  7192. d[i1] = s[i1];
  7193. for (int i0 = i1+1; i0 < ne0; i0++) {
  7194. d[i0] = 0;
  7195. }
  7196. }
  7197. }
  7198. }
  7199. }
  7200. static void ggml_compute_forward_diag(
  7201. const struct ggml_compute_params * params,
  7202. struct ggml_tensor * dst) {
  7203. const struct ggml_tensor * src0 = dst->src[0];
  7204. switch (src0->type) {
  7205. case GGML_TYPE_F32:
  7206. {
  7207. ggml_compute_forward_diag_f32(params, dst);
  7208. } break;
  7209. default:
  7210. {
  7211. GGML_ABORT("fatal error");
  7212. }
  7213. }
  7214. }
  7215. // ggml_compute_forward_diag_mask_inf
  7216. static void ggml_compute_forward_diag_mask_f32(
  7217. const struct ggml_compute_params * params,
  7218. struct ggml_tensor * dst,
  7219. const float value) {
  7220. const struct ggml_tensor * src0 = dst->src[0];
  7221. const int ith = params->ith;
  7222. const int nth = params->nth;
  7223. const int n_past = ((int32_t *) dst->op_params)[0];
  7224. const bool inplace = src0->data == dst->data;
  7225. GGML_ASSERT(n_past >= 0);
  7226. if (!inplace) {
  7227. if (ith == 0) {
  7228. // memcpy needs to be synchronized across threads to avoid race conditions.
  7229. // => do it in INIT phase
  7230. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  7231. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  7232. memcpy(
  7233. ((char *) dst->data),
  7234. ((char *) src0->data),
  7235. ggml_nbytes(dst));
  7236. }
  7237. ggml_barrier(params->threadpool);
  7238. }
  7239. // TODO: handle transposed/permuted matrices
  7240. const int n = ggml_nrows(src0);
  7241. const int nc = src0->ne[0];
  7242. const int nr = src0->ne[1];
  7243. const int nz = n/nr;
  7244. GGML_ASSERT( dst->nb[0] == sizeof(float));
  7245. GGML_ASSERT(src0->nb[0] == sizeof(float));
  7246. for (int k = 0; k < nz; k++) {
  7247. for (int j = ith; j < nr; j += nth) {
  7248. for (int i = n_past; i < nc; i++) {
  7249. if (i > n_past + j) {
  7250. *(float *)((char *) dst->data + k*dst->nb[2] + j*dst->nb[1] + i*dst->nb[0]) = value;
  7251. }
  7252. }
  7253. }
  7254. }
  7255. }
  7256. static void ggml_compute_forward_diag_mask_inf(
  7257. const struct ggml_compute_params * params,
  7258. struct ggml_tensor * dst) {
  7259. const struct ggml_tensor * src0 = dst->src[0];
  7260. switch (src0->type) {
  7261. case GGML_TYPE_F32:
  7262. {
  7263. ggml_compute_forward_diag_mask_f32(params, dst, -INFINITY);
  7264. } break;
  7265. default:
  7266. {
  7267. GGML_ABORT("fatal error");
  7268. }
  7269. }
  7270. }
  7271. static void ggml_compute_forward_diag_mask_zero(
  7272. const struct ggml_compute_params * params,
  7273. struct ggml_tensor * dst) {
  7274. const struct ggml_tensor * src0 = dst->src[0];
  7275. switch (src0->type) {
  7276. case GGML_TYPE_F32:
  7277. {
  7278. ggml_compute_forward_diag_mask_f32(params, dst, 0);
  7279. } break;
  7280. default:
  7281. {
  7282. GGML_ABORT("fatal error");
  7283. }
  7284. }
  7285. }
  7286. // ggml_compute_forward_soft_max
  7287. static void ggml_compute_forward_soft_max_f32(
  7288. const struct ggml_compute_params * params,
  7289. struct ggml_tensor * dst) {
  7290. const struct ggml_tensor * src0 = dst->src[0];
  7291. const struct ggml_tensor * src1 = dst->src[1];
  7292. assert(ggml_is_contiguous(dst));
  7293. assert(ggml_are_same_shape(src0, dst));
  7294. float scale = 1.0f;
  7295. float max_bias = 0.0f;
  7296. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  7297. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  7298. // TODO: handle transposed/permuted matrices
  7299. const int ith = params->ith;
  7300. const int nth = params->nth;
  7301. GGML_TENSOR_UNARY_OP_LOCALS
  7302. //const int64_t ne11 = src1 ? src1->ne[1] : 1;
  7303. // TODO: is this supposed to be ceil instead of floor?
  7304. // https://huggingface.co/mosaicml/mpt-7b/blob/main/attention.py#L370
  7305. const uint32_t n_head = ne02;
  7306. const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head));
  7307. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  7308. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  7309. const int nc = src0->ne[0];
  7310. const int nr = ggml_nrows(src0);
  7311. // rows per thread
  7312. const int dr = (nr + nth - 1)/nth;
  7313. // row range for this thread
  7314. const int ir0 = dr*ith;
  7315. const int ir1 = MIN(ir0 + dr, nr);
  7316. float * wp = (float *) params->wdata + (nc + CACHE_LINE_SIZE_F32) * ith;
  7317. const bool use_f16 = (src1 && src1->type == GGML_TYPE_F16);
  7318. for (int i1 = ir0; i1 < ir1; i1++) {
  7319. // ALiBi
  7320. const uint32_t h = (i1/ne01)%ne02; // head
  7321. const float slope = (max_bias > 0.0f) ? h < n_head_log2 ? powf(m0, h + 1) : powf(m1, 2*(h - n_head_log2) + 1) : 1.0f;
  7322. float * sp = (float *)((char *) src0->data + i1*src0->nb[1]);
  7323. float * dp = (float *)((char *) dst->data + i1*dst->nb[1]);
  7324. // broadcast the mask across rows
  7325. ggml_fp16_t * mp_f16 = src1 ? (ggml_fp16_t *)((char *) src1->data) + (i1%ne01)*ne00 : NULL;
  7326. float * mp_f32 = src1 ? (float *)((char *) src1->data) + (i1%ne01)*ne00 : NULL;
  7327. ggml_vec_cpy_f32 (nc, wp, sp);
  7328. ggml_vec_scale_f32(nc, wp, scale);
  7329. if (mp_f32) {
  7330. if (use_f16) {
  7331. for (int i = 0; i < nc; ++i) {
  7332. wp[i] += slope*GGML_FP16_TO_FP32(mp_f16[i]);
  7333. }
  7334. } else {
  7335. for (int i = 0; i < nc; ++i) {
  7336. wp[i] += slope*mp_f32[i];
  7337. }
  7338. }
  7339. }
  7340. #ifndef NDEBUG
  7341. for (int i = 0; i < nc; ++i) {
  7342. //printf("p[%d] = %f\n", i, p[i]);
  7343. assert(!isnan(wp[i]));
  7344. }
  7345. #endif
  7346. float max = -INFINITY;
  7347. ggml_vec_max_f32(nc, &max, wp);
  7348. ggml_float sum = ggml_vec_soft_max_f32(nc, dp, wp, max);
  7349. assert(sum > 0.0);
  7350. sum = 1.0/sum;
  7351. ggml_vec_scale_f32(nc, dp, sum);
  7352. #ifndef NDEBUG
  7353. for (int i = 0; i < nc; ++i) {
  7354. assert(!isnan(dp[i]));
  7355. assert(!isinf(dp[i]));
  7356. }
  7357. #endif
  7358. }
  7359. }
  7360. static void ggml_compute_forward_soft_max(
  7361. const struct ggml_compute_params * params,
  7362. struct ggml_tensor * dst) {
  7363. const struct ggml_tensor * src0 = dst->src[0];
  7364. switch (src0->type) {
  7365. case GGML_TYPE_F32:
  7366. {
  7367. ggml_compute_forward_soft_max_f32(params, dst);
  7368. } break;
  7369. default:
  7370. {
  7371. GGML_ABORT("fatal error");
  7372. }
  7373. }
  7374. }
  7375. // ggml_compute_forward_soft_max_ext_back
  7376. static void ggml_compute_forward_soft_max_ext_back_f32(
  7377. const struct ggml_compute_params * params,
  7378. struct ggml_tensor * dst) {
  7379. const struct ggml_tensor * src0 = dst->src[0];
  7380. const struct ggml_tensor * src1 = dst->src[1];
  7381. GGML_ASSERT(ggml_is_contiguous(src0));
  7382. GGML_ASSERT(ggml_is_contiguous(src1));
  7383. GGML_ASSERT(ggml_is_contiguous(dst));
  7384. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  7385. GGML_ASSERT(ggml_are_same_shape(src1, dst));
  7386. float scale = 1.0f;
  7387. float max_bias = 0.0f;
  7388. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  7389. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  7390. GGML_ASSERT(max_bias == 0.0f);
  7391. // TODO: handle transposed/permuted matrices
  7392. const int ith = params->ith;
  7393. const int nth = params->nth;
  7394. const int nc = src0->ne[0];
  7395. const int nr = ggml_nrows(src0);
  7396. // rows per thread
  7397. const int dr = (nr + nth - 1)/nth;
  7398. // row range for this thread
  7399. const int ir0 = dr*ith;
  7400. const int ir1 = MIN(ir0 + dr, nr);
  7401. for (int i1 = ir0; i1 < ir1; i1++) {
  7402. float *dy = (float *)((char *) src0->data + i1*src0->nb[1]);
  7403. float *y = (float *)((char *) src1->data + i1*src1->nb[1]);
  7404. float *dx = (float *)((char *) dst->data + i1*dst->nb[1]);
  7405. #ifndef NDEBUG
  7406. for (int i = 0; i < nc; ++i) {
  7407. //printf("p[%d] = %f\n", i, p[i]);
  7408. assert(!isnan(dy[i]));
  7409. assert(!isnan(y[i]));
  7410. }
  7411. #endif
  7412. // Jii = yi - yi*yi
  7413. // Jij = -yi*yj
  7414. // J = diag(y)-y.T*y
  7415. // dx = J * dy
  7416. // dxk = sum_i(Jki * dyi)
  7417. // dxk = sum_i(-yk*yi * dyi) - (-yk*yk)*dyk + (yk - yk*yk)*dyk
  7418. // dxk = sum_i(-yk*yi * dyi) + yk*yk*dyk + yk*dyk - yk*yk*dyk
  7419. // dxk = sum_i(-yk*yi * dyi) + yk*dyk
  7420. // dxk = -yk * sum_i(yi * dyi) + yk*dyk
  7421. // dxk = -yk * dot(y, dy) + yk*dyk
  7422. // dxk = yk * (- dot(y, dy) + dyk)
  7423. // dxk = yk * (dyk - dot(y, dy))
  7424. //
  7425. // post-order:
  7426. // dot_y_dy := dot(y, dy)
  7427. // dx := dy
  7428. // dx := dx - dot_y_dy
  7429. // dx := dx * y
  7430. // linear runtime, no additional memory
  7431. float dot_y_dy = 0;
  7432. ggml_vec_dot_f32 (nc, &dot_y_dy, 0, y, 0, dy, 0, 1);
  7433. ggml_vec_cpy_f32 (nc, dx, dy);
  7434. ggml_vec_acc1_f32 (nc, dx, -dot_y_dy);
  7435. ggml_vec_mul_f32 (nc, dx, dx, y);
  7436. ggml_vec_scale_f32(nc, dx, scale);
  7437. #ifndef NDEBUG
  7438. for (int i = 0; i < nc; ++i) {
  7439. assert(!isnan(dx[i]));
  7440. assert(!isinf(dx[i]));
  7441. }
  7442. #endif
  7443. }
  7444. }
  7445. static void ggml_compute_forward_soft_max_ext_back(
  7446. const struct ggml_compute_params * params,
  7447. struct ggml_tensor * dst) {
  7448. const struct ggml_tensor * src0 = dst->src[0];
  7449. switch (src0->type) {
  7450. case GGML_TYPE_F32:
  7451. {
  7452. ggml_compute_forward_soft_max_ext_back_f32(params, dst);
  7453. } break;
  7454. default:
  7455. {
  7456. GGML_ABORT("fatal error");
  7457. }
  7458. }
  7459. }
  7460. // ggml_compute_forward_clamp
  7461. static void ggml_compute_forward_clamp_f32(
  7462. const struct ggml_compute_params * params,
  7463. struct ggml_tensor * dst) {
  7464. const struct ggml_tensor * src0 = dst->src[0];
  7465. if (params->ith != 0) {
  7466. return;
  7467. }
  7468. float min;
  7469. float max;
  7470. memcpy(&min, (float *) dst->op_params + 0, sizeof(float));
  7471. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  7472. const int ith = params->ith;
  7473. const int nth = params->nth;
  7474. const int n = ggml_nrows(src0);
  7475. const int nc = src0->ne[0];
  7476. const size_t nb00 = src0->nb[0];
  7477. const size_t nb01 = src0->nb[1];
  7478. const size_t nb0 = dst->nb[0];
  7479. const size_t nb1 = dst->nb[1];
  7480. GGML_ASSERT( nb0 == sizeof(float));
  7481. GGML_ASSERT(nb00 == sizeof(float));
  7482. for (int j = ith; j < n; j += nth) {
  7483. float * dst_ptr = (float *) ((char *) dst->data + j*nb1);
  7484. float * src0_ptr = (float *) ((char *) src0->data + j*nb01);
  7485. for (int i = 0; i < nc; i++) {
  7486. dst_ptr[i] = MAX(MIN(src0_ptr[i], max), min);
  7487. }
  7488. }
  7489. }
  7490. static void ggml_compute_forward_clamp(
  7491. const struct ggml_compute_params * params,
  7492. struct ggml_tensor * dst) {
  7493. const struct ggml_tensor * src0 = dst->src[0];
  7494. switch (src0->type) {
  7495. case GGML_TYPE_F32:
  7496. {
  7497. ggml_compute_forward_clamp_f32(params, dst);
  7498. } break;
  7499. case GGML_TYPE_F16:
  7500. case GGML_TYPE_BF16:
  7501. case GGML_TYPE_Q4_0:
  7502. case GGML_TYPE_Q4_1:
  7503. case GGML_TYPE_Q5_0:
  7504. case GGML_TYPE_Q5_1:
  7505. case GGML_TYPE_Q8_0:
  7506. case GGML_TYPE_Q8_1:
  7507. case GGML_TYPE_Q2_K:
  7508. case GGML_TYPE_Q3_K:
  7509. case GGML_TYPE_Q4_K:
  7510. case GGML_TYPE_Q5_K:
  7511. case GGML_TYPE_Q6_K:
  7512. case GGML_TYPE_TQ1_0:
  7513. case GGML_TYPE_TQ2_0:
  7514. case GGML_TYPE_IQ2_XXS:
  7515. case GGML_TYPE_IQ2_XS:
  7516. case GGML_TYPE_IQ3_XXS:
  7517. case GGML_TYPE_IQ1_S:
  7518. case GGML_TYPE_IQ1_M:
  7519. case GGML_TYPE_IQ4_NL:
  7520. case GGML_TYPE_IQ4_XS:
  7521. case GGML_TYPE_IQ3_S:
  7522. case GGML_TYPE_IQ2_S:
  7523. case GGML_TYPE_Q8_K:
  7524. case GGML_TYPE_I8:
  7525. case GGML_TYPE_I16:
  7526. case GGML_TYPE_I32:
  7527. case GGML_TYPE_I64:
  7528. case GGML_TYPE_F64:
  7529. case GGML_TYPE_COUNT:
  7530. {
  7531. GGML_ABORT("fatal error");
  7532. }
  7533. }
  7534. }
  7535. // ggml_compute_forward_rope
  7536. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  7537. const float y = (i0 / 2 - low) / MAX(0.001f, high - low);
  7538. return 1 - MIN(1, MAX(0, y));
  7539. }
  7540. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  7541. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  7542. static void rope_yarn(
  7543. float theta_extrap, float freq_scale, float corr_dims[2], int64_t i0, float ext_factor, float mscale,
  7544. float * cos_theta, float * sin_theta) {
  7545. // Get n-d rotational scaling corrected for extrapolation
  7546. float theta_interp = freq_scale * theta_extrap;
  7547. float theta = theta_interp;
  7548. if (ext_factor != 0.0f) {
  7549. float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor;
  7550. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  7551. // Get n-d magnitude scaling corrected for interpolation
  7552. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  7553. }
  7554. *cos_theta = cosf(theta) * mscale;
  7555. *sin_theta = sinf(theta) * mscale;
  7556. }
  7557. static void ggml_rope_cache_init(
  7558. float theta_base, float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale,
  7559. float * cache, float sin_sign, float theta_scale) {
  7560. // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py
  7561. float theta = theta_base;
  7562. for (int64_t i0 = 0; i0 < ne0; i0 += 2) {
  7563. const float ff = freq_factors ? freq_factors[i0/2] : 1.0f;
  7564. rope_yarn(
  7565. theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1]
  7566. );
  7567. cache[i0 + 1] *= sin_sign;
  7568. theta *= theta_scale;
  7569. }
  7570. }
  7571. static void ggml_mrope_cache_init(
  7572. float theta_base_t, float theta_base_h, float theta_base_w, float theta_base_e, int sections[4], bool indep_sects,
  7573. float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale,
  7574. float * cache, float sin_sign, float theta_scale) {
  7575. // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py
  7576. float theta_t = theta_base_t;
  7577. float theta_h = theta_base_h;
  7578. float theta_w = theta_base_w;
  7579. float theta_e = theta_base_e; // extra position id for vision encoder
  7580. int sect_dims = sections[0] + sections[1] + sections[2] + sections[3];
  7581. int sec_w = sections[1] + sections[0];
  7582. int sec_e = sections[2] + sec_w;
  7583. GGML_ASSERT(sect_dims <= ne0);
  7584. for (int64_t i0 = 0; i0 < ne0; i0 += 2) {
  7585. const float ff = freq_factors ? freq_factors[i0/2] : 1.0f;
  7586. int sector = (i0 / 2) % sect_dims;
  7587. if (indep_sects) {
  7588. // compute theta independently for each dim sections
  7589. // (i.e. reset corresponding theta when `i0` go from one section to another)
  7590. if (sector == 0) {
  7591. theta_t = theta_base_t;
  7592. }
  7593. else if (sector == sections[0]) {
  7594. theta_h = theta_base_h;;
  7595. }
  7596. else if (sector == sec_w) {
  7597. theta_w = theta_base_w;
  7598. }
  7599. else if (sector == sec_e) {
  7600. theta_e = theta_base_e;
  7601. }
  7602. }
  7603. float theta = theta_t;
  7604. if (sector >= sections[0] && sector < sec_w) {
  7605. theta = theta_h;
  7606. }
  7607. else if (sector >= sec_w && sector < sec_w + sections[2]) {
  7608. theta = theta_w;
  7609. }
  7610. else if (sector >= sec_w + sections[2]) {
  7611. theta = theta_e;
  7612. }
  7613. rope_yarn(
  7614. theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1]
  7615. );
  7616. cache[i0 + 1] *= sin_sign;
  7617. theta_t *= theta_scale;
  7618. theta_w *= theta_scale;
  7619. theta_h *= theta_scale;
  7620. theta_e *= theta_scale;
  7621. }
  7622. }
  7623. static void ggml_compute_forward_rope_f32(
  7624. const struct ggml_compute_params * params,
  7625. struct ggml_tensor * dst,
  7626. const bool forward) {
  7627. const struct ggml_tensor * src0 = dst->src[0];
  7628. const struct ggml_tensor * src1 = dst->src[1];
  7629. const struct ggml_tensor * src2 = dst->src[2];
  7630. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7631. int sections[4];
  7632. //const int n_past = ((int32_t *) dst->op_params)[0];
  7633. const int n_dims = ((int32_t *) dst->op_params)[1];
  7634. const int mode = ((int32_t *) dst->op_params)[2];
  7635. //const int n_ctx = ((int32_t *) dst->op_params)[3];
  7636. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  7637. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7638. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7639. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7640. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7641. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7642. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7643. memcpy(&sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  7644. GGML_TENSOR_UNARY_OP_LOCALS
  7645. //printf("ne0: %d, ne1: %d, ne2: %d, ne3: %d\n", ne0, ne1, ne2, ne3);
  7646. //printf("n_past = %d, ne2 = %d\n", n_past, ne2);
  7647. GGML_ASSERT(nb00 == sizeof(float));
  7648. const int ith = params->ith;
  7649. const int nth = params->nth;
  7650. const int nr = ggml_nrows(dst);
  7651. GGML_ASSERT(n_dims <= ne0);
  7652. GGML_ASSERT(n_dims % 2 == 0);
  7653. // rows per thread
  7654. const int dr = (nr + nth - 1)/nth;
  7655. // row range for this thread
  7656. const int ir0 = dr*ith;
  7657. const int ir1 = MIN(ir0 + dr, nr);
  7658. // row index used to determine which thread to use
  7659. int ir = 0;
  7660. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  7661. float corr_dims[2];
  7662. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  7663. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  7664. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE; // ggml_rope_multi, multimodal rotary position embedding
  7665. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  7666. if (is_mrope) {
  7667. GGML_ASSERT(sections[0] > 0 || sections[1] > 0 || sections[2] > 0);
  7668. }
  7669. if (is_vision) {
  7670. GGML_ASSERT(n_dims == ne0/2);
  7671. }
  7672. const float * freq_factors = NULL;
  7673. if (src2 != NULL) {
  7674. GGML_ASSERT(src2->type == GGML_TYPE_F32);
  7675. GGML_ASSERT(src2->ne[0] >= n_dims / 2);
  7676. freq_factors = (const float *) src2->data;
  7677. }
  7678. // backward process uses inverse rotation by cos and sin.
  7679. // cos and sin build a rotation matrix, where the inverse is the transpose.
  7680. // this essentially just switches the sign of sin.
  7681. const float sin_sign = forward ? 1.0f : -1.0f;
  7682. const int32_t * pos = (const int32_t *) src1->data;
  7683. for (int64_t i3 = 0; i3 < ne3; i3++) { // batch
  7684. for (int64_t i2 = 0; i2 < ne2; i2++) { // seq-len
  7685. float * cache = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32)*ith;
  7686. if (!is_mrope) {
  7687. const int64_t p = pos[i2];
  7688. ggml_rope_cache_init(p, freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7689. }
  7690. else {
  7691. const int64_t p_t = pos[i2];
  7692. const int64_t p_h = pos[i2 + ne2];
  7693. const int64_t p_w = pos[i2 + ne2 * 2];
  7694. const int64_t p_e = pos[i2 + ne2 * 3];
  7695. ggml_mrope_cache_init(
  7696. p_t, p_h, p_w, p_e, sections, is_vision,
  7697. freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7698. }
  7699. for (int64_t i1 = 0; i1 < ne1; i1++) { // attn-heads
  7700. if (ir++ < ir0) continue;
  7701. if (ir > ir1) break;
  7702. if (is_neox || is_mrope) {
  7703. if (is_vision){
  7704. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7705. const int64_t ic = i0/2;
  7706. const float cos_theta = cache[i0 + 0];
  7707. const float sin_theta = cache[i0 + 1];
  7708. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7709. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7710. const float x0 = src[0];
  7711. const float x1 = src[n_dims];
  7712. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7713. dst_data[n_dims] = x0*sin_theta + x1*cos_theta;
  7714. }
  7715. } else {
  7716. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7717. const int64_t ic = i0/2;
  7718. const float cos_theta = cache[i0 + 0];
  7719. const float sin_theta = cache[i0 + 1];
  7720. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7721. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7722. const float x0 = src[0];
  7723. const float x1 = src[n_dims/2];
  7724. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7725. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  7726. }
  7727. }
  7728. } else {
  7729. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7730. const float cos_theta = cache[i0 + 0];
  7731. const float sin_theta = cache[i0 + 1];
  7732. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7733. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7734. const float x0 = src[0];
  7735. const float x1 = src[1];
  7736. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7737. dst_data[1] = x0*sin_theta + x1*cos_theta;
  7738. }
  7739. }
  7740. if (is_vision) {
  7741. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7742. const int64_t ic = i0/2;
  7743. const float cos_theta = cache[i0 + 0];
  7744. const float sin_theta = cache[i0 + 1];
  7745. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7746. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7747. const float x0 = src[0];
  7748. const float x1 = src[n_dims];
  7749. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7750. dst_data[n_dims] = x0*sin_theta + x1*cos_theta;
  7751. }
  7752. } else {
  7753. // fill the remain channels with data from src tensor
  7754. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7755. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7756. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7757. dst_data[0] = src[0];
  7758. dst_data[1] = src[1];
  7759. }
  7760. }
  7761. }
  7762. }
  7763. }
  7764. }
  7765. // TODO: deduplicate f16/f32 code
  7766. static void ggml_compute_forward_rope_f16(
  7767. const struct ggml_compute_params * params,
  7768. struct ggml_tensor * dst,
  7769. const bool forward) {
  7770. const struct ggml_tensor * src0 = dst->src[0];
  7771. const struct ggml_tensor * src1 = dst->src[1];
  7772. const struct ggml_tensor * src2 = dst->src[2];
  7773. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7774. int sections[4];
  7775. //const int n_past = ((int32_t *) dst->op_params)[0];
  7776. const int n_dims = ((int32_t *) dst->op_params)[1];
  7777. const int mode = ((int32_t *) dst->op_params)[2];
  7778. //const int n_ctx = ((int32_t *) dst->op_params)[3];
  7779. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  7780. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7781. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7782. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7783. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7784. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7785. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7786. memcpy(&sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  7787. GGML_TENSOR_UNARY_OP_LOCALS
  7788. //printf("ne0: %d, ne1: %d, ne2: %d, ne3: %d\n", ne0, ne1, ne2, ne3);
  7789. //printf("n_past = %d, ne2 = %d\n", n_past, ne2);
  7790. GGML_ASSERT(nb0 == sizeof(ggml_fp16_t));
  7791. const int ith = params->ith;
  7792. const int nth = params->nth;
  7793. const int nr = ggml_nrows(dst);
  7794. GGML_ASSERT(n_dims <= ne0);
  7795. GGML_ASSERT(n_dims % 2 == 0);
  7796. // rows per thread
  7797. const int dr = (nr + nth - 1)/nth;
  7798. // row range for this thread
  7799. const int ir0 = dr*ith;
  7800. const int ir1 = MIN(ir0 + dr, nr);
  7801. // row index used to determine which thread to use
  7802. int ir = 0;
  7803. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  7804. float corr_dims[2];
  7805. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  7806. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  7807. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  7808. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  7809. if (is_mrope) {
  7810. GGML_ASSERT(sections[0] > 0 || sections[1] > 0 || sections[2] > 0);
  7811. }
  7812. if (is_vision) {
  7813. GGML_ASSERT(n_dims == ne0/2);
  7814. }
  7815. const float * freq_factors = NULL;
  7816. if (src2 != NULL) {
  7817. GGML_ASSERT(src2->type == GGML_TYPE_F32);
  7818. GGML_ASSERT(src2->ne[0] >= n_dims / 2);
  7819. freq_factors = (const float *) src2->data;
  7820. }
  7821. // backward process uses inverse rotation by cos and sin.
  7822. // cos and sin build a rotation matrix, where the inverse is the transpose.
  7823. // this essentially just switches the sign of sin.
  7824. const float sin_sign = forward ? 1.0f : -1.0f;
  7825. const int32_t * pos = (const int32_t *) src1->data;
  7826. for (int64_t i3 = 0; i3 < ne3; i3++) {
  7827. for (int64_t i2 = 0; i2 < ne2; i2++) {
  7828. float * cache = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32)*ith;
  7829. if (!is_mrope) {
  7830. const int64_t p = pos[i2];
  7831. ggml_rope_cache_init(p, freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7832. }
  7833. else {
  7834. const int64_t p_t = pos[i2];
  7835. const int64_t p_h = pos[i2 + ne2];
  7836. const int64_t p_w = pos[i2 + ne2 * 2];
  7837. const int64_t p_e = pos[i2 + ne2 * 3];
  7838. ggml_mrope_cache_init(
  7839. p_t, p_h, p_w, p_e, sections, is_vision,
  7840. freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7841. }
  7842. for (int64_t i1 = 0; i1 < ne1; i1++) {
  7843. if (ir++ < ir0) continue;
  7844. if (ir > ir1) break;
  7845. if (is_neox || is_mrope) {
  7846. if (is_vision) {
  7847. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7848. const int64_t ic = i0/2;
  7849. const float cos_theta = cache[i0 + 0];
  7850. const float sin_theta = cache[i0 + 1];
  7851. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7852. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7853. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7854. const float x1 = GGML_FP16_TO_FP32(src[n_dims]);
  7855. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7856. dst_data[n_dims] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7857. }
  7858. } else {
  7859. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7860. const int64_t ic = i0/2;
  7861. const float cos_theta = cache[i0 + 0];
  7862. const float sin_theta = cache[i0 + 1];
  7863. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7864. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7865. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7866. const float x1 = GGML_FP16_TO_FP32(src[n_dims/2]);
  7867. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7868. dst_data[n_dims/2] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7869. }
  7870. }
  7871. } else {
  7872. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7873. const float cos_theta = cache[i0 + 0];
  7874. const float sin_theta = cache[i0 + 1];
  7875. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7876. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7877. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7878. const float x1 = GGML_FP16_TO_FP32(src[1]);
  7879. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7880. dst_data[1] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7881. }
  7882. }
  7883. if (is_vision) {
  7884. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7885. const int64_t ic = i0/2;
  7886. const float cos_theta = cache[i0 + 0];
  7887. const float sin_theta = cache[i0 + 1];
  7888. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7889. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7890. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7891. const float x1 = GGML_FP16_TO_FP32(src[n_dims]);
  7892. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7893. dst_data[n_dims] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7894. }
  7895. } else {
  7896. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7897. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7898. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7899. dst_data[0] = src[0];
  7900. dst_data[1] = src[1];
  7901. }
  7902. }
  7903. }
  7904. }
  7905. }
  7906. }
  7907. static void ggml_compute_forward_rope(
  7908. const struct ggml_compute_params * params,
  7909. struct ggml_tensor * dst) {
  7910. const struct ggml_tensor * src0 = dst->src[0];
  7911. switch (src0->type) {
  7912. case GGML_TYPE_F16:
  7913. {
  7914. ggml_compute_forward_rope_f16(params, dst, true);
  7915. } break;
  7916. case GGML_TYPE_F32:
  7917. {
  7918. ggml_compute_forward_rope_f32(params, dst, true);
  7919. } break;
  7920. default:
  7921. {
  7922. GGML_ABORT("fatal error");
  7923. }
  7924. }
  7925. }
  7926. // ggml_compute_forward_rope_back
  7927. static void ggml_compute_forward_rope_back(
  7928. const struct ggml_compute_params * params,
  7929. struct ggml_tensor * dst) {
  7930. const struct ggml_tensor * src0 = dst->src[0];
  7931. switch (src0->type) {
  7932. case GGML_TYPE_F16:
  7933. {
  7934. ggml_compute_forward_rope_f16(params, dst, false);
  7935. } break;
  7936. case GGML_TYPE_F32:
  7937. {
  7938. ggml_compute_forward_rope_f32(params, dst, false);
  7939. } break;
  7940. default:
  7941. {
  7942. GGML_ABORT("fatal error");
  7943. }
  7944. }
  7945. }
  7946. // ggml_compute_forward_conv_transpose_1d
  7947. static void ggml_compute_forward_conv_transpose_1d_f16_f32(
  7948. const struct ggml_compute_params * params,
  7949. struct ggml_tensor * dst) {
  7950. const struct ggml_tensor * src0 = dst->src[0];
  7951. const struct ggml_tensor * src1 = dst->src[1];
  7952. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7953. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7954. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7955. GGML_TENSOR_BINARY_OP_LOCALS
  7956. const int ith = params->ith;
  7957. const int nth = params->nth;
  7958. const int nk = ne00*ne01*ne02;
  7959. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  7960. GGML_ASSERT(nb10 == sizeof(float));
  7961. if (ith == 0) {
  7962. memset(params->wdata, 0, params->wsize);
  7963. // permute kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout)
  7964. {
  7965. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  7966. for (int64_t i02 = 0; i02 < ne02; i02++) {
  7967. for (int64_t i01 = 0; i01 < ne01; i01++) {
  7968. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i02*nb02 + i01*nb01);
  7969. ggml_fp16_t * dst_data = wdata + i01*ne00*ne02;
  7970. for (int64_t i00 = 0; i00 < ne00; i00++) {
  7971. dst_data[i00*ne02 + i02] = src[i00];
  7972. }
  7973. }
  7974. }
  7975. }
  7976. // permute source data (src1) from (L x Cin) to (Cin x L)
  7977. {
  7978. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + nk;
  7979. ggml_fp16_t * dst_data = wdata;
  7980. for (int64_t i11 = 0; i11 < ne11; i11++) {
  7981. const float * const src = (float *)((char *) src1->data + i11*nb11);
  7982. for (int64_t i10 = 0; i10 < ne10; i10++) {
  7983. dst_data[i10*ne11 + i11] = GGML_FP32_TO_FP16(src[i10]);
  7984. }
  7985. }
  7986. }
  7987. // need to zero dst since we are accumulating into it
  7988. memset(dst->data, 0, ggml_nbytes(dst));
  7989. }
  7990. ggml_barrier(params->threadpool);
  7991. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  7992. // total rows in dst
  7993. const int nr = ne1;
  7994. // rows per thread
  7995. const int dr = (nr + nth - 1)/nth;
  7996. // row range for this thread
  7997. const int ir0 = dr*ith;
  7998. const int ir1 = MIN(ir0 + dr, nr);
  7999. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  8000. ggml_fp16_t * const wdata_src = wdata + nk;
  8001. for (int i1 = ir0; i1 < ir1; i1++) {
  8002. float * dst_data = (float *)((char *) dst->data + i1*nb1);
  8003. ggml_fp16_t * wdata_kernel = wdata + i1*ne02*ne00;
  8004. for (int i10 = 0; i10 < ne10; i10++) {
  8005. const int i1n = i10*ne11;
  8006. for (int i00 = 0; i00 < ne00; i00++) {
  8007. float v = 0;
  8008. ggml_vec_dot_f16(ne02, &v, 0,
  8009. (ggml_fp16_t *) wdata_src + i1n, 0,
  8010. (ggml_fp16_t *) wdata_kernel + i00*ne02, 0, 1);
  8011. dst_data[i10*s0 + i00] += v;
  8012. }
  8013. }
  8014. }
  8015. }
  8016. static void ggml_compute_forward_conv_transpose_1d_f32(
  8017. const struct ggml_compute_params * params,
  8018. struct ggml_tensor * dst) {
  8019. const struct ggml_tensor * src0 = dst->src[0];
  8020. const struct ggml_tensor * src1 = dst->src[1];
  8021. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8022. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8023. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8024. GGML_TENSOR_BINARY_OP_LOCALS
  8025. const int ith = params->ith;
  8026. const int nth = params->nth;
  8027. const int nk = ne00*ne01*ne02;
  8028. GGML_ASSERT(nb00 == sizeof(float));
  8029. GGML_ASSERT(nb10 == sizeof(float));
  8030. if (ith == 0) {
  8031. memset(params->wdata, 0, params->wsize);
  8032. // prepare kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout)
  8033. {
  8034. float * const wdata = (float *) params->wdata + 0;
  8035. for (int64_t i02 = 0; i02 < ne02; i02++) {
  8036. for (int64_t i01 = 0; i01 < ne01; i01++) {
  8037. const float * const src = (float *)((char *) src0->data + i02*nb02 + i01*nb01);
  8038. float * dst_data = wdata + i01*ne00*ne02;
  8039. for (int64_t i00 = 0; i00 < ne00; i00++) {
  8040. dst_data[i00*ne02 + i02] = src[i00];
  8041. }
  8042. }
  8043. }
  8044. }
  8045. // prepare source data (src1)
  8046. {
  8047. float * const wdata = (float *) params->wdata + nk;
  8048. float * dst_data = wdata;
  8049. for (int64_t i11 = 0; i11 < ne11; i11++) {
  8050. const float * const src = (float *)((char *) src1->data + i11*nb11);
  8051. for (int64_t i10 = 0; i10 < ne10; i10++) {
  8052. dst_data[i10*ne11 + i11] = src[i10];
  8053. }
  8054. }
  8055. }
  8056. // need to zero dst since we are accumulating into it
  8057. memset(dst->data, 0, ggml_nbytes(dst));
  8058. }
  8059. ggml_barrier(params->threadpool);
  8060. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  8061. // total rows in dst
  8062. const int nr = ne1;
  8063. // rows per thread
  8064. const int dr = (nr + nth - 1)/nth;
  8065. // row range for this thread
  8066. const int ir0 = dr*ith;
  8067. const int ir1 = MIN(ir0 + dr, nr);
  8068. float * const wdata = (float *) params->wdata + 0;
  8069. float * const wdata_src = wdata + nk;
  8070. for (int i1 = ir0; i1 < ir1; i1++) {
  8071. float * dst_data = (float *)((char *) dst->data + i1*nb1);
  8072. float * wdata_kernel = wdata + i1*ne02*ne00;
  8073. for (int i10 = 0; i10 < ne10; i10++) {
  8074. const int i1n = i10*ne11;
  8075. for (int i00 = 0; i00 < ne00; i00++) {
  8076. float v = 0;
  8077. ggml_vec_dot_f32(ne02, &v, 0,
  8078. wdata_src + i1n, 0,
  8079. wdata_kernel + i00*ne02, 0, 1);
  8080. dst_data[i10*s0 + i00] += v;
  8081. }
  8082. }
  8083. }
  8084. }
  8085. static void ggml_compute_forward_conv_transpose_1d(
  8086. const struct ggml_compute_params * params,
  8087. struct ggml_tensor * dst) {
  8088. const struct ggml_tensor * src0 = dst->src[0];
  8089. switch (src0->type) {
  8090. case GGML_TYPE_F16:
  8091. {
  8092. ggml_compute_forward_conv_transpose_1d_f16_f32(params, dst);
  8093. } break;
  8094. case GGML_TYPE_F32:
  8095. {
  8096. ggml_compute_forward_conv_transpose_1d_f32(params, dst);
  8097. } break;
  8098. default:
  8099. {
  8100. GGML_ABORT("fatal error");
  8101. }
  8102. }
  8103. }
  8104. // ggml_compute_forward_im2col_f32
  8105. // src0: kernel [OC, IC, KH, KW]
  8106. // src1: image [N, IC, IH, IW]
  8107. // dst: result [N, OH, OW, IC*KH*KW]
  8108. static void ggml_compute_forward_im2col_f32(
  8109. const struct ggml_compute_params * params,
  8110. struct ggml_tensor * dst) {
  8111. const struct ggml_tensor * src0 = dst->src[0];
  8112. const struct ggml_tensor * src1 = dst->src[1];
  8113. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8114. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8115. GGML_TENSOR_BINARY_OP_LOCALS;
  8116. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8117. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8118. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  8119. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  8120. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  8121. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  8122. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  8123. const int ith = params->ith;
  8124. const int nth = params->nth;
  8125. const int64_t N = is_2D ? ne13 : ne12;
  8126. const int64_t IC = is_2D ? ne12 : ne11;
  8127. const int64_t IH = is_2D ? ne11 : 1;
  8128. const int64_t IW = ne10;
  8129. const int64_t KH = is_2D ? ne01 : 1;
  8130. const int64_t KW = ne00;
  8131. const int64_t OH = is_2D ? ne2 : 1;
  8132. const int64_t OW = ne1;
  8133. int ofs0 = is_2D ? nb13 : nb12;
  8134. int ofs1 = is_2D ? nb12 : nb11;
  8135. GGML_ASSERT(nb10 == sizeof(float));
  8136. // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
  8137. {
  8138. float * const wdata = (float *) dst->data;
  8139. for (int64_t in = 0; in < N; in++) {
  8140. for (int64_t ioh = 0; ioh < OH; ioh++) { // 1
  8141. for (int64_t iow = 0; iow < OW; iow++) {
  8142. for (int64_t iic = ith; iic < IC; iic += nth) {
  8143. // micro kernel
  8144. float * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW]
  8145. const float * const src_data = (float *)((char *) src1->data + in*ofs0 + iic*ofs1); // [IH, IW]
  8146. for (int64_t ikh = 0; ikh < KH; ikh++) { // 1
  8147. for (int64_t ikw = 0; ikw < KW; ikw++) {
  8148. const int64_t iiw = iow*s0 + ikw*d0 - p0;
  8149. const int64_t iih = ioh*s1 + ikh*d1 - p1;
  8150. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  8151. dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0;
  8152. } else {
  8153. dst_data[iic*(KH*KW) + ikh*KW + ikw] = (src_data[iih*IW + iiw]);
  8154. }
  8155. }
  8156. }
  8157. }
  8158. }
  8159. }
  8160. }
  8161. }
  8162. }
  8163. // ggml_compute_forward_im2col_f16
  8164. // src0: kernel [OC, IC, KH, KW]
  8165. // src1: image [N, IC, IH, IW]
  8166. // dst: result [N, OH, OW, IC*KH*KW]
  8167. static void ggml_compute_forward_im2col_f16(
  8168. const struct ggml_compute_params * params,
  8169. struct ggml_tensor * dst) {
  8170. const struct ggml_tensor * src0 = dst->src[0];
  8171. const struct ggml_tensor * src1 = dst->src[1];
  8172. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8173. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8174. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  8175. GGML_TENSOR_BINARY_OP_LOCALS;
  8176. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8177. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8178. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  8179. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  8180. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  8181. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  8182. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  8183. const int ith = params->ith;
  8184. const int nth = params->nth;
  8185. const int64_t N = is_2D ? ne13 : ne12;
  8186. const int64_t IC = is_2D ? ne12 : ne11;
  8187. const int64_t IH = is_2D ? ne11 : 1;
  8188. const int64_t IW = ne10;
  8189. const int64_t KH = is_2D ? ne01 : 1;
  8190. const int64_t KW = ne00;
  8191. const int64_t OH = is_2D ? ne2 : 1;
  8192. const int64_t OW = ne1;
  8193. int ofs0 = is_2D ? nb13 : nb12;
  8194. int ofs1 = is_2D ? nb12 : nb11;
  8195. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  8196. GGML_ASSERT(nb10 == sizeof(float));
  8197. // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
  8198. {
  8199. ggml_fp16_t * const wdata = (ggml_fp16_t *) dst->data;
  8200. for (int64_t in = 0; in < N; in++) {
  8201. for (int64_t ioh = 0; ioh < OH; ioh++) { // 1
  8202. for (int64_t iow = 0; iow < OW; iow++) {
  8203. for (int64_t iic = ith; iic < IC; iic += nth) {
  8204. // micro kernel
  8205. ggml_fp16_t * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW]
  8206. const float * const src_data = (float *)((char *) src1->data + in*ofs0 + iic*ofs1); // [IH, IW]
  8207. for (int64_t ikh = 0; ikh < KH; ikh++) { // 1
  8208. for (int64_t ikw = 0; ikw < KW; ikw++) {
  8209. const int64_t iiw = iow*s0 + ikw*d0 - p0;
  8210. const int64_t iih = ioh*s1 + ikh*d1 - p1;
  8211. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  8212. dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0;
  8213. } else {
  8214. dst_data[iic*(KH*KW) + ikh*KW + ikw] = GGML_FP32_TO_FP16(src_data[iih*IW + iiw]);
  8215. }
  8216. }
  8217. }
  8218. }
  8219. }
  8220. }
  8221. }
  8222. }
  8223. }
  8224. static void ggml_compute_forward_im2col(
  8225. const struct ggml_compute_params * params,
  8226. struct ggml_tensor * dst) {
  8227. switch (dst->type) {
  8228. case GGML_TYPE_F16:
  8229. {
  8230. ggml_compute_forward_im2col_f16(params, dst);
  8231. } break;
  8232. case GGML_TYPE_F32:
  8233. {
  8234. ggml_compute_forward_im2col_f32(params, dst);
  8235. } break;
  8236. default:
  8237. {
  8238. GGML_ABORT("fatal error");
  8239. }
  8240. }
  8241. }
  8242. // ggml_compute_forward_im2col_back_f32
  8243. static void ggml_compute_forward_im2col_back_f32(
  8244. const struct ggml_compute_params * params,
  8245. struct ggml_tensor * dst) {
  8246. const struct ggml_tensor * src0 = dst->src[0]; // gradients of forward pass output
  8247. const struct ggml_tensor * src1 = dst->src[1]; // convolution kernel
  8248. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8249. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8250. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8251. GGML_TENSOR_BINARY_OP_LOCALS;
  8252. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8253. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8254. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  8255. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  8256. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  8257. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  8258. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  8259. const int ith = params->ith;
  8260. const int nth = params->nth;
  8261. const int64_t N = is_2D ? ne3 : ne2;
  8262. const int64_t IC = is_2D ? ne2 : ne1;
  8263. const int64_t IH = is_2D ? ne1 : 1;
  8264. const int64_t IW = ne0;
  8265. const int64_t KH = is_2D ? ne11 : 1;
  8266. const int64_t KW = ne10;
  8267. const int64_t OH = is_2D ? ne02 : 1;
  8268. const int64_t OW = ne01;
  8269. int ofs0 = is_2D ? nb3 : nb2;
  8270. int ofs1 = is_2D ? nb2 : nb1;
  8271. GGML_ASSERT(nb0 == sizeof(float));
  8272. // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
  8273. {
  8274. float * const wdata = (float *) dst->data;
  8275. for (int64_t in = 0; in < N; in++) {
  8276. for (int64_t iic = ith; iic < IC; iic += nth) {
  8277. for (int64_t iih = 0; iih < IH; iih++) {
  8278. for (int64_t iiw = 0; iiw < IW; iiw++) {
  8279. // micro kernel
  8280. float grad = 0.0f;
  8281. for (int64_t ikh = 0; ikh < KH; ikh++) {
  8282. for (int64_t ikw = 0; ikw < KW; ikw++) {
  8283. // For s0 > 1 some values were skipped over in the forward pass.
  8284. // These values have tmpw % s0 != 0 and need to be skipped in the backwards pass as well.
  8285. const int64_t tmpw = (iiw + p0 - ikw*d0);
  8286. if (tmpw % s0 != 0) {
  8287. continue;
  8288. }
  8289. const int64_t iow = tmpw / s0;
  8290. // Equivalent logic as above except for s1.
  8291. int64_t ioh;
  8292. if (is_2D) {
  8293. const int64_t tmph = iih + p1 - ikh*d1;
  8294. if (tmph % s1 != 0) {
  8295. continue;
  8296. }
  8297. ioh = tmph / s1;
  8298. } else {
  8299. ioh = 0;
  8300. }
  8301. if (iow < 0 || iow >= OW || ioh < 0 || ioh >= OH) {
  8302. continue;
  8303. }
  8304. const float * const grad_in = (const float *) src0->data
  8305. + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW]
  8306. grad += grad_in[iic*(KH*KW) + ikh*KW + ikw];
  8307. }
  8308. }
  8309. float * dst_data = (float *)((char *) wdata + (in*ofs0 + iic*ofs1)); // [IH, IW]
  8310. dst_data[iih*IW + iiw] = grad;
  8311. }
  8312. }
  8313. }
  8314. }
  8315. }
  8316. }
  8317. // ggml_compute_forward_conv_transpose_2d
  8318. static void ggml_compute_forward_conv_transpose_2d(
  8319. const struct ggml_compute_params * params,
  8320. struct ggml_tensor * dst) {
  8321. const struct ggml_tensor * src0 = dst->src[0];
  8322. const struct ggml_tensor * src1 = dst->src[1];
  8323. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8324. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8325. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8326. GGML_TENSOR_BINARY_OP_LOCALS
  8327. const int ith = params->ith;
  8328. const int nth = params->nth;
  8329. const int nk = ne00*ne01*ne02*ne03;
  8330. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  8331. GGML_ASSERT(nb10 == sizeof(float));
  8332. if (ith == 0) {
  8333. memset(params->wdata, 0, params->wsize);
  8334. // permute kernel data (src0) from (Kw x Kh x Cout x Cin) to (Cin x Kw x Kh x Cout)
  8335. {
  8336. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  8337. for (int64_t i03 = 0; i03 < ne03; i03++) {
  8338. for (int64_t i02 = 0; i02 < ne02; i02++) {
  8339. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i03*nb03 + i02*nb02);
  8340. ggml_fp16_t * dst_data = wdata + i02*ne01*ne00*ne03;
  8341. for (int64_t i01 = 0; i01 < ne01; i01++) {
  8342. for (int64_t i00 = 0; i00 < ne00; i00++) {
  8343. dst_data[i01*ne00*ne03 + i00*ne03 + i03] = src[i01 * ne00 + i00];
  8344. }
  8345. }
  8346. }
  8347. }
  8348. }
  8349. // permute source data (src1) from (Sw x Sh x Cin) to (Cin x Sw x Sh)
  8350. {
  8351. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + nk;
  8352. for (int i12 = 0; i12 < ne12; i12++) {
  8353. for (int i11 = 0; i11 < ne11; i11++) {
  8354. const float * const src = (float *)((char *) src1->data + i12*nb12 + i11*nb11);
  8355. ggml_fp16_t * dst_data = wdata + i11*ne10*ne12;
  8356. for (int i10 = 0; i10 < ne10; i10++) {
  8357. dst_data[i10*ne12 + i12] = GGML_FP32_TO_FP16(src[i10]);
  8358. }
  8359. }
  8360. }
  8361. }
  8362. memset(dst->data, 0, ggml_nbytes(dst));
  8363. }
  8364. ggml_barrier(params->threadpool);
  8365. const int32_t stride = ggml_get_op_params_i32(dst, 0);
  8366. // total patches in dst
  8367. const int np = ne2;
  8368. // patches per thread
  8369. const int dp = (np + nth - 1)/nth;
  8370. // patch range for this thread
  8371. const int ip0 = dp*ith;
  8372. const int ip1 = MIN(ip0 + dp, np);
  8373. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  8374. ggml_fp16_t * const wdata_src = wdata + nk;
  8375. for (int i2 = ip0; i2 < ip1; i2++) { // Cout
  8376. float * dst_data = (float *)((char *) dst->data + i2*nb2);
  8377. ggml_fp16_t * wdata_kernel = wdata + i2*ne01*ne00*ne03;
  8378. for (int i11 = 0; i11 < ne11; i11++) {
  8379. for (int i10 = 0; i10 < ne10; i10++) {
  8380. const int i1n = i11*ne10*ne12 + i10*ne12;
  8381. for (int i01 = 0; i01 < ne01; i01++) {
  8382. for (int i00 = 0; i00 < ne00; i00++) {
  8383. float v = 0;
  8384. ggml_vec_dot_f16(ne03, &v, 0,
  8385. wdata_src + i1n, 0,
  8386. wdata_kernel + i01*ne00*ne03 + i00*ne03, 0, 1);
  8387. dst_data[(i11*stride + i01)*ne0 + i10*stride + i00] += v;
  8388. }
  8389. }
  8390. }
  8391. }
  8392. }
  8393. }
  8394. // ggml_compute_forward_pool_1d_sk_p0
  8395. static void ggml_compute_forward_pool_1d_sk_p0(
  8396. const struct ggml_compute_params * params,
  8397. const enum ggml_op_pool op,
  8398. const int k,
  8399. struct ggml_tensor * dst) {
  8400. const struct ggml_tensor * src = dst->src[0];
  8401. assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16);
  8402. if (params->ith != 0) {
  8403. return;
  8404. }
  8405. const char * cdata = (const char *)src->data;
  8406. const char * const data_end = cdata + ggml_nbytes(src);
  8407. float * drow = (float *)dst->data;
  8408. const int64_t rs = dst->ne[0];
  8409. while (cdata < data_end) {
  8410. const void * srow = (const void *)cdata;
  8411. int j = 0;
  8412. for (int64_t i = 0; i < rs; ++i) {
  8413. switch (op) {
  8414. case GGML_OP_POOL_AVG: drow[i] = 0; break;
  8415. case GGML_OP_POOL_MAX: drow[i] = -FLT_MAX; break;
  8416. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8417. }
  8418. for (int ki = 0; ki < k; ++ki) {
  8419. const float srow_j = (src->type == GGML_TYPE_F32) ? ((const float*)srow)[j] : GGML_FP16_TO_FP32(((const ggml_fp16_t*)srow)[j]);
  8420. switch (op) {
  8421. case GGML_OP_POOL_AVG: drow[i] += srow_j; break;
  8422. case GGML_OP_POOL_MAX: if (srow_j > drow[i]) drow[i] = srow_j; break;
  8423. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8424. }
  8425. ++j;
  8426. }
  8427. switch (op) {
  8428. case GGML_OP_POOL_AVG: drow[i] /= k; break;
  8429. case GGML_OP_POOL_MAX: break;
  8430. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8431. }
  8432. }
  8433. cdata += src->nb[1];
  8434. drow += rs;
  8435. }
  8436. }
  8437. // ggml_compute_forward_pool_1d
  8438. static void ggml_compute_forward_pool_1d(
  8439. const struct ggml_compute_params * params,
  8440. struct ggml_tensor * dst) {
  8441. const int32_t * opts = (const int32_t *)dst->op_params;
  8442. enum ggml_op_pool op = opts[0];
  8443. const int k0 = opts[1];
  8444. const int s0 = opts[2];
  8445. const int p0 = opts[3];
  8446. GGML_ASSERT(p0 == 0); // padding not supported
  8447. GGML_ASSERT(k0 == s0); // only s = k supported
  8448. ggml_compute_forward_pool_1d_sk_p0(params, op, k0, dst);
  8449. }
  8450. // ggml_compute_forward_pool_2d
  8451. static void ggml_compute_forward_pool_2d(
  8452. const struct ggml_compute_params * params,
  8453. struct ggml_tensor * dst) {
  8454. const struct ggml_tensor * src = dst->src[0];
  8455. assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16);
  8456. if (params->ith != 0) {
  8457. return;
  8458. }
  8459. const int32_t * opts = (const int32_t *)dst->op_params;
  8460. enum ggml_op_pool op = opts[0];
  8461. const int k0 = opts[1];
  8462. const int k1 = opts[2];
  8463. const int s0 = opts[3];
  8464. const int s1 = opts[4];
  8465. const int p0 = opts[5];
  8466. const int p1 = opts[6];
  8467. const char * cdata = (const char*)src->data;
  8468. const char * const data_end = cdata + ggml_nbytes(src);
  8469. const int64_t px = dst->ne[0];
  8470. const int64_t py = dst->ne[1];
  8471. const int64_t pa = px * py;
  8472. float * dplane = (float *)dst->data;
  8473. const int ka = k0 * k1;
  8474. const int offset0 = -p0;
  8475. const int offset1 = -p1;
  8476. while (cdata < data_end) {
  8477. for (int oy = 0; oy < py; ++oy) {
  8478. float * const drow = dplane + oy * px;
  8479. for (int ox = 0; ox < px; ++ox) {
  8480. float * const out = drow + ox;
  8481. switch (op) {
  8482. case GGML_OP_POOL_AVG: *out = 0; break;
  8483. case GGML_OP_POOL_MAX: *out = -FLT_MAX; break;
  8484. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8485. }
  8486. const int ix = offset0 + ox * s0;
  8487. const int iy = offset1 + oy * s1;
  8488. for (int ky = 0; ky < k1; ++ky) {
  8489. if (iy + ky < 0 || iy + ky >= src->ne[1]) continue;
  8490. const void * srow = (const void *)(cdata + src->nb[1] * (iy + ky));
  8491. for (int kx = 0; kx < k0; ++kx) {
  8492. int j = ix + kx;
  8493. if (j < 0 || j >= src->ne[0]) continue;
  8494. const float srow_j = (src->type == GGML_TYPE_F32) ? ((const float*)srow)[j] : GGML_FP16_TO_FP32(((const ggml_fp16_t*)srow)[j]);
  8495. switch (op) {
  8496. case GGML_OP_POOL_AVG: *out += srow_j; break;
  8497. case GGML_OP_POOL_MAX: if (srow_j > *out) *out = srow_j; break;
  8498. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8499. }
  8500. }
  8501. }
  8502. switch (op) {
  8503. case GGML_OP_POOL_AVG: *out /= ka; break;
  8504. case GGML_OP_POOL_MAX: break;
  8505. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8506. }
  8507. }
  8508. }
  8509. cdata += src->nb[2];
  8510. dplane += pa;
  8511. }
  8512. }
  8513. // ggml_compute_forward_pool_2d_back
  8514. static void ggml_compute_forward_pool_2d_back(
  8515. const struct ggml_compute_params * params,
  8516. struct ggml_tensor * dst) {
  8517. const struct ggml_tensor * src = dst->src[0];
  8518. const struct ggml_tensor * dstf = dst->src[1]; // forward tensor of dst
  8519. assert(dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  8520. if (params->ith != 0) {
  8521. return;
  8522. }
  8523. const int32_t * opts = (const int32_t *)dst->op_params;
  8524. enum ggml_op_pool op = opts[0];
  8525. const int k0 = opts[1];
  8526. const int k1 = opts[2];
  8527. const int s0 = opts[3];
  8528. const int s1 = opts[4];
  8529. const int p0 = opts[5];
  8530. const int p1 = opts[6];
  8531. char * cdata = (char *) dst->data;
  8532. const char * cdataf = (const char *) dstf->data;
  8533. const char * const data_end = cdata + ggml_nbytes(dst);
  8534. GGML_ASSERT(params->ith == 0);
  8535. memset(cdata, 0, ggml_nbytes(dst));
  8536. const int64_t px = src->ne[0];
  8537. const int64_t py = src->ne[1];
  8538. const int64_t pa = px * py;
  8539. const float * splane = (const float *) src->data;
  8540. const int ka = k0 * k1;
  8541. const int offset0 = -p0;
  8542. const int offset1 = -p1;
  8543. while (cdata < data_end) {
  8544. for (int oy = 0; oy < py; ++oy) {
  8545. const float * const srow = splane + oy * px;
  8546. for (int ox = 0; ox < px; ++ox) {
  8547. const float grad0 = srow[ox];
  8548. const int ix = offset0 + ox * s0;
  8549. const int iy = offset1 + oy * s1;
  8550. if (op == GGML_OP_POOL_MAX) {
  8551. float maxval = -FLT_MAX;
  8552. int kxmax = -1;
  8553. int kymax = -1;
  8554. for (int ky = 0; ky < k1; ++ky) {
  8555. if (iy + ky < 0 || iy + ky >= dst->ne[1]) {
  8556. continue;
  8557. }
  8558. const void * drowf = (const void *)(cdataf + dst->nb[1] * (iy + ky));
  8559. for (int kx = 0; kx < k0; ++kx) {
  8560. int j = ix + kx;
  8561. if (j < 0 || j >= dst->ne[0]) {
  8562. continue;
  8563. }
  8564. const float val = dst->type == GGML_TYPE_F32 ?
  8565. ((const float *) drowf)[j] : GGML_FP16_TO_FP32(((const ggml_fp16_t *) drowf)[j]);
  8566. if (val <= maxval) {
  8567. continue;
  8568. }
  8569. maxval = val;
  8570. kxmax = kx;
  8571. kymax = ky;
  8572. }
  8573. }
  8574. if (kxmax == -1 || kymax == -1) {
  8575. continue;
  8576. }
  8577. void * drow = (void *)(cdata + dst->nb[1] * (iy + kymax));
  8578. const int j = ix + kxmax;
  8579. if (dst->type == GGML_TYPE_F32) {
  8580. ((float *) drow)[j] += grad0;
  8581. } else {
  8582. ((ggml_fp16_t *) drow)[j] = GGML_FP32_TO_FP16(grad0 + GGML_FP16_TO_FP32(((const ggml_fp16_t *) drow)[j]));
  8583. }
  8584. } else if (op == GGML_OP_POOL_AVG) {
  8585. const float grad = grad0 / ka;
  8586. for (int ky = 0; ky < k1; ++ky) {
  8587. if (iy + ky < 0 || iy + ky >= dst->ne[1]) {
  8588. continue;
  8589. }
  8590. void * drow = (void *)(cdata + dst->nb[1] * (iy + ky));
  8591. for (int kx = 0; kx < k0; ++kx) {
  8592. int j = ix + kx;
  8593. if (j < 0 || j >= dst->ne[0]) {
  8594. continue;
  8595. }
  8596. if (dst->type == GGML_TYPE_F32) {
  8597. ((float *) drow)[j] += grad;
  8598. } else {
  8599. ((ggml_fp16_t *) drow)[j] += GGML_FP32_TO_FP16(grad);
  8600. }
  8601. }
  8602. }
  8603. } else {
  8604. GGML_ASSERT(false);
  8605. }
  8606. }
  8607. }
  8608. cdata += dst->nb[2];
  8609. cdataf += dst->nb[2];
  8610. splane += pa;
  8611. }
  8612. }
  8613. // ggml_compute_forward_upscale
  8614. static void ggml_compute_forward_upscale_f32(
  8615. const struct ggml_compute_params * params,
  8616. struct ggml_tensor * dst) {
  8617. const struct ggml_tensor * src0 = dst->src[0];
  8618. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8619. const int ith = params->ith;
  8620. const int nth = params->nth;
  8621. GGML_TENSOR_UNARY_OP_LOCALS
  8622. const float sf0 = (float)ne0/src0->ne[0];
  8623. const float sf1 = (float)ne1/src0->ne[1];
  8624. const float sf2 = (float)ne2/src0->ne[2];
  8625. const float sf3 = (float)ne3/src0->ne[3];
  8626. // TODO: optimize
  8627. for (int64_t i3 = 0; i3 < ne3; i3++) {
  8628. const int64_t i03 = i3 / sf3;
  8629. for (int64_t i2 = ith; i2 < ne2; i2 += nth) {
  8630. const int64_t i02 = i2 / sf2;
  8631. for (int64_t i1 = 0; i1 < ne1; i1++) {
  8632. const int64_t i01 = i1 / sf1;
  8633. for (int64_t i0 = 0; i0 < ne0; i0++) {
  8634. const int64_t i00 = i0 / sf0;
  8635. const float * x = (float *)((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  8636. float * y = (float *)((char *) dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3);
  8637. *y = *x;
  8638. }
  8639. }
  8640. }
  8641. }
  8642. }
  8643. static void ggml_compute_forward_upscale(
  8644. const struct ggml_compute_params * params,
  8645. struct ggml_tensor * dst) {
  8646. const struct ggml_tensor * src0 = dst->src[0];
  8647. switch (src0->type) {
  8648. case GGML_TYPE_F32:
  8649. {
  8650. ggml_compute_forward_upscale_f32(params, dst);
  8651. } break;
  8652. default:
  8653. {
  8654. GGML_ABORT("fatal error");
  8655. }
  8656. }
  8657. }
  8658. // ggml_compute_forward_pad
  8659. static void ggml_compute_forward_pad_f32(
  8660. const struct ggml_compute_params * params,
  8661. struct ggml_tensor * dst) {
  8662. const struct ggml_tensor * src0 = dst->src[0];
  8663. GGML_ASSERT(src0->nb[0] == sizeof(float));
  8664. GGML_ASSERT( dst->nb[0] == sizeof(float));
  8665. const int ith = params->ith;
  8666. const int nth = params->nth;
  8667. GGML_TENSOR_UNARY_OP_LOCALS
  8668. float * dst_ptr = (float *) dst->data;
  8669. // TODO: optimize
  8670. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  8671. for (int64_t i1 = ith; i1 < ne1; i1 += nth) {
  8672. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  8673. for (int64_t i3 = 0; i3 < ne3; ++i3) {
  8674. const int64_t dst_idx = i3*(ne0*ne1*ne2) + i2*(ne0*ne1) + i1*ne0 + i0;
  8675. const float * src_ptr = (const float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  8676. if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
  8677. dst_ptr[dst_idx] = *src_ptr;
  8678. } else {
  8679. dst_ptr[dst_idx] = 0;
  8680. }
  8681. }
  8682. }
  8683. }
  8684. }
  8685. }
  8686. static void ggml_compute_forward_pad(
  8687. const struct ggml_compute_params * params,
  8688. struct ggml_tensor * dst) {
  8689. const struct ggml_tensor * src0 = dst->src[0];
  8690. switch (src0->type) {
  8691. case GGML_TYPE_F32:
  8692. {
  8693. ggml_compute_forward_pad_f32(params, dst);
  8694. } break;
  8695. default:
  8696. {
  8697. GGML_ABORT("fatal error");
  8698. }
  8699. }
  8700. }
  8701. // ggml_compute_forward_pad_reflect_1d
  8702. static void ggml_compute_forward_pad_reflect_1d(
  8703. const struct ggml_compute_params * params,
  8704. struct ggml_tensor * dst) {
  8705. const struct ggml_tensor * src0 = dst->src[0];
  8706. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8707. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8708. const int ith = params->ith;
  8709. const int nth = params->nth;
  8710. const int32_t * opts = (const int32_t *) dst->op_params;
  8711. const int p0 = opts[0];
  8712. const int p1 = opts[1];
  8713. GGML_TENSOR_UNARY_OP_LOCALS
  8714. for (int64_t i3 = 0; i3 < ne3; i3++) {
  8715. for (int64_t i2 = 0; i2 < ne2; i2++) {
  8716. for (int64_t i1 = ith; i1 < ne1; i1 += nth) {
  8717. float * left = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + p0*nb0);
  8718. float * right = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + (ne0-p1-1)*nb0);
  8719. ggml_vec_cpy_f32(ne00, left, (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01));
  8720. for (int i0 = 1; i0 <= p0; i0++) { left[-i0] = left[i0]; }
  8721. for (int i0 = 1; i0 <= p1; i0++) { right[i0] = right[-i0]; }
  8722. }
  8723. }
  8724. }
  8725. }
  8726. // ggml_compute_forward_arange
  8727. static void ggml_compute_forward_arange_f32(
  8728. const struct ggml_compute_params * params,
  8729. struct ggml_tensor * dst) {
  8730. GGML_ASSERT(dst->nb[0] == sizeof(float));
  8731. const int ith = params->ith;
  8732. const int nth = params->nth;
  8733. const float start = ggml_get_op_params_f32(dst, 0);
  8734. const float stop = ggml_get_op_params_f32(dst, 1);
  8735. const float step = ggml_get_op_params_f32(dst, 2);
  8736. const int64_t steps = (int64_t) ceilf((stop - start) / step);
  8737. GGML_ASSERT(ggml_nelements(dst) == steps);
  8738. for (int64_t i = ith; i < steps; i+= nth) {
  8739. float value = start + step * i;
  8740. ((float *)dst->data)[i] = value;
  8741. }
  8742. }
  8743. static void ggml_compute_forward_arange(
  8744. const struct ggml_compute_params * params,
  8745. struct ggml_tensor * dst) {
  8746. switch (dst->type) {
  8747. case GGML_TYPE_F32:
  8748. {
  8749. ggml_compute_forward_arange_f32(params, dst);
  8750. } break;
  8751. default:
  8752. {
  8753. GGML_ABORT("fatal error");
  8754. }
  8755. }
  8756. }
  8757. static void ggml_compute_forward_timestep_embedding_f32(
  8758. const struct ggml_compute_params * params,
  8759. struct ggml_tensor * dst) {
  8760. const struct ggml_tensor * src0 = dst->src[0];
  8761. GGML_ASSERT(src0->nb[0] == sizeof(float));
  8762. const int ith = params->ith;
  8763. const int nth = params->nth;
  8764. GGML_TENSOR_UNARY_OP_LOCALS
  8765. const int dim = ggml_get_op_params_i32(dst, 0);
  8766. const int max_period = ggml_get_op_params_i32(dst, 1);
  8767. int half = dim / 2;
  8768. for (int64_t i = 0; i < ne00; i++) {
  8769. float * embed_data = (float *)((char *) dst->data + i*nb1);
  8770. for (int64_t j = ith; j < half; j += nth) {
  8771. float timestep = ((float *)src0->data)[i];
  8772. float freq = (float)expf(-logf(max_period) * j / half);
  8773. float arg = timestep * freq;
  8774. embed_data[j] = cosf(arg);
  8775. embed_data[j + half] = sinf(arg);
  8776. }
  8777. if (dim % 2 != 0 && ith == 0) {
  8778. embed_data[dim] = 0.f;
  8779. }
  8780. }
  8781. }
  8782. static void ggml_compute_forward_timestep_embedding(
  8783. const struct ggml_compute_params * params,
  8784. struct ggml_tensor * dst) {
  8785. const struct ggml_tensor * src0 = dst->src[0];
  8786. switch (src0->type) {
  8787. case GGML_TYPE_F32:
  8788. {
  8789. ggml_compute_forward_timestep_embedding_f32(params, dst);
  8790. } break;
  8791. default:
  8792. {
  8793. GGML_ABORT("fatal error");
  8794. }
  8795. }
  8796. }
  8797. // ggml_compute_forward_argsort
  8798. static void ggml_compute_forward_argsort_f32(
  8799. const struct ggml_compute_params * params,
  8800. struct ggml_tensor * dst) {
  8801. const struct ggml_tensor * src0 = dst->src[0];
  8802. GGML_TENSOR_UNARY_OP_LOCALS
  8803. GGML_ASSERT(nb0 == sizeof(float));
  8804. const int ith = params->ith;
  8805. const int nth = params->nth;
  8806. const int64_t nr = ggml_nrows(src0);
  8807. enum ggml_sort_order order = (enum ggml_sort_order) ggml_get_op_params_i32(dst, 0);
  8808. for (int64_t i = ith; i < nr; i += nth) {
  8809. int32_t * dst_data = (int32_t *)((char *) dst->data + i*nb1);
  8810. const float * src_data = (float *)((char *) src0->data + i*nb01);
  8811. for (int64_t j = 0; j < ne0; j++) {
  8812. dst_data[j] = j;
  8813. }
  8814. // C doesn't have a functional sort, so we do a bubble sort instead
  8815. for (int64_t j = 0; j < ne0; j++) {
  8816. for (int64_t k = j + 1; k < ne0; k++) {
  8817. if ((order == GGML_SORT_ORDER_ASC && src_data[dst_data[j]] > src_data[dst_data[k]]) ||
  8818. (order == GGML_SORT_ORDER_DESC && src_data[dst_data[j]] < src_data[dst_data[k]])) {
  8819. int32_t tmp = dst_data[j];
  8820. dst_data[j] = dst_data[k];
  8821. dst_data[k] = tmp;
  8822. }
  8823. }
  8824. }
  8825. }
  8826. }
  8827. static void ggml_compute_forward_argsort(
  8828. const struct ggml_compute_params * params,
  8829. struct ggml_tensor * dst) {
  8830. const struct ggml_tensor * src0 = dst->src[0];
  8831. switch (src0->type) {
  8832. case GGML_TYPE_F32:
  8833. {
  8834. ggml_compute_forward_argsort_f32(params, dst);
  8835. } break;
  8836. default:
  8837. {
  8838. GGML_ABORT("fatal error");
  8839. }
  8840. }
  8841. }
  8842. // ggml_compute_forward_flash_attn_ext
  8843. static void ggml_compute_forward_flash_attn_ext_f16(
  8844. const struct ggml_compute_params * params,
  8845. const struct ggml_tensor * q,
  8846. const struct ggml_tensor * k,
  8847. const struct ggml_tensor * v,
  8848. const struct ggml_tensor * mask,
  8849. struct ggml_tensor * dst) {
  8850. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  8851. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  8852. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  8853. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  8854. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  8855. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  8856. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  8857. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  8858. const int ith = params->ith;
  8859. const int nth = params->nth;
  8860. const int64_t D = neq0;
  8861. const int64_t N = neq1;
  8862. GGML_ASSERT(ne0 == D);
  8863. GGML_ASSERT(ne2 == N);
  8864. // input tensor rows must be contiguous
  8865. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  8866. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  8867. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  8868. GGML_ASSERT(neq0 == D);
  8869. GGML_ASSERT(nek0 == D);
  8870. GGML_ASSERT(nev0 == D);
  8871. GGML_ASSERT(neq1 == N);
  8872. GGML_ASSERT(nev0 == D);
  8873. // dst cannot be transposed or permuted
  8874. GGML_ASSERT(nb0 == sizeof(float));
  8875. GGML_ASSERT(nb0 <= nb1);
  8876. GGML_ASSERT(nb1 <= nb2);
  8877. GGML_ASSERT(nb2 <= nb3);
  8878. // broadcast factors
  8879. const int64_t rk2 = neq2/nek2;
  8880. const int64_t rk3 = neq3/nek3;
  8881. const int64_t rv2 = neq2/nev2;
  8882. const int64_t rv3 = neq3/nev3;
  8883. // parallelize by q rows using ggml_vec_dot_f32
  8884. // total rows in q
  8885. const int nr = neq1*neq2*neq3;
  8886. // rows per thread
  8887. const int dr = (nr + nth - 1)/nth;
  8888. // row range for this thread
  8889. const int ir0 = dr*ith;
  8890. const int ir1 = MIN(ir0 + dr, nr);
  8891. float scale = 1.0f;
  8892. float max_bias = 0.0f;
  8893. float logit_softcap = 0.0f;
  8894. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  8895. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  8896. memcpy(&logit_softcap, (float *) dst->op_params + 2, sizeof(float));
  8897. if (logit_softcap != 0) {
  8898. scale /= logit_softcap;
  8899. }
  8900. const uint32_t n_head = neq2;
  8901. const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head));
  8902. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  8903. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  8904. enum ggml_type const k_vec_dot_type = type_traits_cpu[k->type].vec_dot_type;
  8905. ggml_from_float_t const q_to_vec_dot = type_traits_cpu[k_vec_dot_type].from_float;
  8906. ggml_vec_dot_t const kq_vec_dot = type_traits_cpu[k->type].vec_dot;
  8907. ggml_to_float_t const v_to_float = ggml_get_type_traits(v->type)->to_float;
  8908. GGML_ASSERT(q_to_vec_dot && "fattn: unsupported K-type");
  8909. GGML_ASSERT(v_to_float && "fattn: unsupported V-type");
  8910. // loop over n_batch and n_head
  8911. for (int ir = ir0; ir < ir1; ++ir) {
  8912. // q indices
  8913. const int iq3 = ir/(neq2*neq1);
  8914. const int iq2 = (ir - iq3*neq2*neq1)/neq1;
  8915. const int iq1 = (ir - iq3*neq2*neq1 - iq2*neq1);
  8916. const uint32_t h = iq2; // head index
  8917. const float slope = (max_bias > 0.0f) ? h < n_head_log2 ? powf(m0, h + 1) : powf(m1, 2*(h - n_head_log2) + 1) : 1.0f;
  8918. float S = 0.0f; // sum
  8919. float M = -INFINITY; // maximum KQ value
  8920. float * VKQ32 = (float *) params->wdata + ith*(3*D + CACHE_LINE_SIZE_F32); // FP32 VKQ accumulator
  8921. float * V32 = (VKQ32 + 1*D); // (temporary) FP32 V buffer
  8922. ggml_fp16_t * VKQ16 = (ggml_fp16_t *) (VKQ32 + 1*D); // (temporary) FP16 VKQ accumulator
  8923. ggml_fp16_t * Q_q = (ggml_fp16_t *) (VKQ32 + 2*D); // (temporary) buffer for Q converted to quantized/FP16
  8924. if (v->type == GGML_TYPE_F16) {
  8925. memset(VKQ16, 0, D*sizeof(ggml_fp16_t));
  8926. } else {
  8927. memset(VKQ32, 0, D*sizeof(float));
  8928. }
  8929. const ggml_fp16_t * mp = mask ? (ggml_fp16_t *)((char *) mask->data + iq1*mask->nb[1]) : NULL;
  8930. // k indices
  8931. const int ik3 = iq3 / rk3;
  8932. const int ik2 = iq2 / rk2;
  8933. // v indices
  8934. const int iv3 = iq3 / rv3;
  8935. const int iv2 = iq2 / rv2;
  8936. const float * pq = (const float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3));
  8937. q_to_vec_dot(pq, Q_q, D);
  8938. // online softmax / attention
  8939. // loop over n_kv and n_head_kv
  8940. // ref: https://arxiv.org/pdf/2112.05682.pdf
  8941. for (int64_t ic = 0; ic < nek1; ++ic) {
  8942. const float mv = mp ? slope*GGML_FP16_TO_FP32(mp[ic]) : 0.0f;
  8943. if (mv == -INFINITY) {
  8944. continue;
  8945. }
  8946. float s; // KQ value
  8947. const char * k_data = (const char *) k->data + ( ic*nbk1 + ik2*nbk2 + ik3*nbk3);
  8948. kq_vec_dot(D, &s, 0, k_data, 0, Q_q, 0, 1);
  8949. s = s*scale; // scale KQ value
  8950. if (logit_softcap != 0.0f) {
  8951. s = logit_softcap*tanhf(s);
  8952. }
  8953. s += mv; // apply mask
  8954. const float Mold = M;
  8955. float ms = 1.0f; // upon new higher max val, scale VKQ and KQ sum with this value
  8956. float vs = 1.0f; // post-softmax KQ value, expf(s - M)
  8957. const char * v_data = ((const char *) v->data + (ic*nbv1 + iv2*nbv2 + iv3*nbv3));
  8958. if (v->type == GGML_TYPE_F16) {
  8959. if (s > M) {
  8960. // s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f
  8961. M = s;
  8962. ms = expf(Mold - M);
  8963. // V = V*expf(Mold - M)
  8964. ggml_vec_scale_f16(D, VKQ16, ms);
  8965. } else {
  8966. // no new maximum, ms == 1.0f, vs != 1.0f
  8967. vs = expf(s - M);
  8968. }
  8969. // V += v*expf(s - M)
  8970. ggml_vec_mad_f16(D, VKQ16, (const ggml_fp16_t *) v_data, vs);
  8971. } else {
  8972. if (s > M) {
  8973. // s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f
  8974. M = s;
  8975. ms = expf(Mold - M);
  8976. // V = V*expf(Mold - M)
  8977. ggml_vec_scale_f32(D, VKQ32, ms);
  8978. } else {
  8979. // no new maximum, ms == 1.0f, vs != 1.0f
  8980. vs = expf(s - M);
  8981. }
  8982. v_to_float(v_data, V32, D);
  8983. // V += v*expf(s - M)
  8984. ggml_vec_mad_f32(D, VKQ32, V32, vs);
  8985. }
  8986. S = S*ms + vs; // scale and increment sum with partial sum
  8987. }
  8988. if (v->type == GGML_TYPE_F16) {
  8989. for (int64_t d = 0; d < D; ++d) {
  8990. VKQ32[d] = GGML_FP16_TO_FP32(VKQ16[d]);
  8991. }
  8992. }
  8993. // V /= S
  8994. const float S_inv = 1.0f/S;
  8995. ggml_vec_scale_f32(D, VKQ32, S_inv);
  8996. // dst indices
  8997. const int i1 = iq1;
  8998. const int i2 = iq2;
  8999. const int i3 = iq3;
  9000. // original
  9001. //memcpy((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb3), V, nev0*sizeof(float));
  9002. // permute(0, 2, 1, 3)
  9003. memcpy((char *) dst->data + (i3*ne2*ne1 + i2 + i1*ne1)*nb1, VKQ32, nb1);
  9004. }
  9005. }
  9006. static void ggml_compute_forward_flash_attn_ext(
  9007. const struct ggml_compute_params * params,
  9008. const struct ggml_tensor * q,
  9009. const struct ggml_tensor * k,
  9010. const struct ggml_tensor * v,
  9011. const struct ggml_tensor * mask,
  9012. struct ggml_tensor * dst) {
  9013. switch (dst->op_params[3]) {
  9014. case GGML_PREC_DEFAULT:
  9015. case GGML_PREC_F32:
  9016. {
  9017. // uses F32 accumulators
  9018. ggml_compute_forward_flash_attn_ext_f16(params, q, k, v, mask, dst);
  9019. } break;
  9020. default:
  9021. {
  9022. GGML_ABORT("fatal error");
  9023. }
  9024. }
  9025. }
  9026. // ggml_compute_forward_flash_attn_back
  9027. static void ggml_compute_forward_flash_attn_back_f32(
  9028. const struct ggml_compute_params * params,
  9029. const bool masked,
  9030. struct ggml_tensor * dst) {
  9031. const struct ggml_tensor * q = dst->src[0];
  9032. const struct ggml_tensor * k = dst->src[1];
  9033. const struct ggml_tensor * v = dst->src[2];
  9034. const struct ggml_tensor * d = dst->src[3];
  9035. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  9036. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  9037. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  9038. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  9039. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  9040. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  9041. GGML_TENSOR_LOCALS(int64_t, ned, d, ne)
  9042. GGML_TENSOR_LOCALS(size_t, nbd, d, nb)
  9043. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  9044. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  9045. const int ith = params->ith;
  9046. const int nth = params->nth;
  9047. const int64_t D = neq0;
  9048. const int64_t N = neq1;
  9049. const int64_t P = nek1 - N;
  9050. const int64_t M = P + N;
  9051. const int Mup = ggml_up(M, GGML_SOFT_MAX_UNROLL);
  9052. const int mxDM = MAX(D, Mup);
  9053. // GGML_ASSERT(ne0 == D);
  9054. // GGML_ASSERT(ne1 == N);
  9055. GGML_ASSERT(P >= 0);
  9056. GGML_ASSERT(nbq0 == sizeof(float));
  9057. GGML_ASSERT(nbk0 == sizeof(float));
  9058. GGML_ASSERT(nbv0 == sizeof(float));
  9059. GGML_ASSERT(neq0 == D);
  9060. GGML_ASSERT(nek0 == D);
  9061. GGML_ASSERT(nev1 == D);
  9062. GGML_ASSERT(ned0 == D);
  9063. GGML_ASSERT(neq1 == N);
  9064. GGML_ASSERT(nek1 == N + P);
  9065. GGML_ASSERT(nev1 == D);
  9066. GGML_ASSERT(ned1 == N);
  9067. // dst cannot be transposed or permuted
  9068. GGML_ASSERT(nb0 == sizeof(float));
  9069. GGML_ASSERT(nb0 <= nb1);
  9070. GGML_ASSERT(nb1 <= nb2);
  9071. GGML_ASSERT(nb2 <= nb3);
  9072. if (ith == 0) {
  9073. memset(dst->data, 0, nb0*ne0*ne1*ne2*ne3);
  9074. }
  9075. ggml_barrier(params->threadpool);
  9076. const int64_t elem_q = ggml_nelements(q);
  9077. const int64_t elem_k = ggml_nelements(k);
  9078. enum ggml_type result_type = dst->type;
  9079. GGML_ASSERT(ggml_blck_size(result_type) == 1);
  9080. const size_t tsize = ggml_type_size(result_type);
  9081. const size_t offs_q = 0;
  9082. const size_t offs_k = offs_q + GGML_PAD(elem_q * tsize, GGML_MEM_ALIGN);
  9083. const size_t offs_v = offs_k + GGML_PAD(elem_k * tsize, GGML_MEM_ALIGN);
  9084. void * grad_q = (char *) dst->data;
  9085. void * grad_k = (char *) dst->data + offs_k;
  9086. void * grad_v = (char *) dst->data + offs_v;
  9087. const size_t nbgq1 = nb0*neq0;
  9088. const size_t nbgq2 = nb0*neq0*neq1;
  9089. const size_t nbgq3 = nb0*neq0*neq1*neq2;
  9090. const size_t nbgk1 = nb0*nek0;
  9091. const size_t nbgk2 = nb0*nek0*nek1;
  9092. const size_t nbgk3 = nb0*nek0*nek1*neq2;
  9093. const size_t nbgv1 = nb0*nev0;
  9094. const size_t nbgv2 = nb0*nev0*nev1;
  9095. const size_t nbgv3 = nb0*nev0*nev1*neq2;
  9096. // parallelize by k rows using ggml_vec_dot_f32
  9097. // total rows in k
  9098. const int nr = nek2*nek3;
  9099. // rows per thread
  9100. const int dr = (nr + nth - 1)/nth;
  9101. // row range for this thread
  9102. const int ir0 = dr*ith;
  9103. const int ir1 = MIN(ir0 + dr, nr);
  9104. const float scale = 1.0f/sqrtf(D);
  9105. //printf("P=%d N=%d D=%d ir0=%d ir1=%d scale = %f\n", P, N, D, ir0, ir1, scale);
  9106. // how often k2 (and v2) is repeated in q2
  9107. int nrep = neq2/nek2;
  9108. for (int ir = ir0; ir < ir1; ++ir) {
  9109. // q indices
  9110. const int ik3 = ir/(nek2);
  9111. const int ik2 = ir - ik3*nek2;
  9112. const int iq3 = ik3;
  9113. const int id3 = ik3;
  9114. const int iv3 = ik3;
  9115. const int iv2 = ik2;
  9116. for (int irep = 0; irep < nrep; ++irep) {
  9117. const int iq2 = ik2 + irep*nek2;
  9118. const int id2 = iq2;
  9119. // (ik2 + irep*nek2) % nek2 == ik2
  9120. for (int iq1 = 0; iq1 < neq1; ++iq1) {
  9121. const int id1 = iq1;
  9122. // not sure about CACHE_LINE_SIZE_F32..
  9123. // - maybe it must not be multiplied by 2 and excluded from .. in SM 1*(..) offset?
  9124. float * S = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 0*(mxDM+CACHE_LINE_SIZE_F32);
  9125. float * SM = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 1*(mxDM+CACHE_LINE_SIZE_F32);
  9126. for (int i = M; i < Mup; ++i) {
  9127. S[i] = -INFINITY;
  9128. }
  9129. const int64_t masked_begin = masked ? (P + iq1 + 1) : M;
  9130. for (int64_t ic = 0; ic < masked_begin; ++ic) {
  9131. // k indices
  9132. const int ik1 = ic;
  9133. // S indices
  9134. const int i1 = ik1;
  9135. ggml_vec_dot_f32(neq0,
  9136. S + i1, 0,
  9137. (float *) ((char *) k->data + (ik1*nbk1 + ik2*nbk2 + ik3*nbk3)), 0,
  9138. (float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)), 0, 1);
  9139. }
  9140. // scale
  9141. ggml_vec_scale_f32(masked_begin, S, scale);
  9142. for (int64_t i = masked_begin; i < M; i++) {
  9143. S[i] = -INFINITY;
  9144. }
  9145. // softmax
  9146. // exclude known -INF S[..] values from max and loop
  9147. // dont forget to set their SM values to zero
  9148. {
  9149. float max = -INFINITY;
  9150. ggml_vec_max_f32(masked_begin, &max, S);
  9151. ggml_float sum = 0.0;
  9152. {
  9153. #ifdef GGML_SOFT_MAX_ACCELERATE
  9154. max = -max;
  9155. vDSP_vsadd(SM, 1, &max, SM, 1, Mup);
  9156. vvexpf(SM, SM, &Mup);
  9157. ggml_vec_sum_f32(Mup, &sum, SM);
  9158. #else
  9159. sum = ggml_vec_soft_max_f32(Mup, SM, S, max);
  9160. #endif
  9161. }
  9162. assert(sum > 0.0);
  9163. sum = 1.0/sum;
  9164. ggml_vec_scale_f32(masked_begin, SM, sum);
  9165. }
  9166. // step-by-step explanation
  9167. {
  9168. // forward-process shape grads from backward process
  9169. // parallel_for ik2,ik3:
  9170. // for irep:
  9171. // iq2 = ik2 + irep*nek2
  9172. // k[:D,:M,:,:] [D,M,:,:] grad[k][:D,:M,ik2,ik3] += grad[kcur]
  9173. // q[:D,:N,:,:] [D,N,:,:] grad[q][:D,iq1,iq2,iq3] += grad[qcur]
  9174. // v[:M,:D,:,:] [M,D,:,:] grad[v][:M,:D,iv2,iv3] += grad[vcur]
  9175. // for iq1:
  9176. // kcur = k[:D,:M,ik2,ik3] [D,M,1,1] grad[kcur] = grad[S1].T @ qcur
  9177. // qcur = q[:D,iq1,iq2,iq3] [D,1,1,1] grad[qcur] = grad[S1] @ kcur
  9178. // vcur = v[:M,:D,iv2,iv3] [M,D,1,1] grad[vcur] = grad[S5].T @ S4
  9179. // S0 = -Inf [D,1,1,1]
  9180. // ~S1[i] = dot(kcur[:D,i], qcur)
  9181. // S1 = qcur @ kcur.T [M,1,1,1] grad[S1] = grad[S2] * scale
  9182. // S2 = S1 * scale [M,1,1,1] grad[S2] = diag_mask_zero(grad[S3], P)
  9183. // S3 = diag_mask_inf(S2, P) [M,1,1,1] grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4]))
  9184. // S4 = softmax(S3) [M,1,1,1] grad[S4] = grad[S5] @ vcur
  9185. // ~S5[i] = dot(vcur[:,i], S4)
  9186. // S5 = S4 @ vcur.T [D,1,1,1] grad[S5] = d[:D,id1,id2,id3]
  9187. // ~dst[i,iq1,iq2,iq3] = S5[i] ^
  9188. // dst[:D,iq1,iq2,iq3] = S5 | grad[dst[:D,iq1,iq2,iq3]] = d[:D,id1,id2,id3]
  9189. // dst backward-/ grad[dst] = d
  9190. //
  9191. // output gradients with their dependencies:
  9192. //
  9193. // grad[kcur] = grad[S1].T @ qcur
  9194. // grad[S1] = diag_mask_zero(grad[S3], P) * scale
  9195. // grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4]))
  9196. // grad[S4] = grad[S5] @ vcur
  9197. // grad[S4] = d[:D,id1,id2,id3] @ vcur
  9198. // grad[qcur] = grad[S1] @ kcur
  9199. // grad[vcur] = grad[S5].T @ S4
  9200. // grad[vcur] = d[:D,id1,id2,id3].T @ S4
  9201. //
  9202. // in post-order:
  9203. //
  9204. // S1 = qcur @ kcur.T
  9205. // S2 = S1 * scale
  9206. // S3 = diag_mask_inf(S2, P)
  9207. // S4 = softmax(S3)
  9208. // grad[S4] = d[:D,id1,id2,id3] @ vcur
  9209. // grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4]))
  9210. // grad[S1] = diag_mask_zero(grad[S3], P) * scale
  9211. // grad[qcur] = grad[S1] @ kcur
  9212. // grad[kcur] = grad[S1].T @ qcur
  9213. // grad[vcur] = d[:D,id1,id2,id3].T @ S4
  9214. //
  9215. // using less variables (SM=S4):
  9216. //
  9217. // S = diag_mask_inf(qcur @ kcur.T * scale, P)
  9218. // SM = softmax(S)
  9219. // S = d[:D,iq1,iq2,iq3] @ vcur
  9220. // dot_SM_gradSM = dot(SM, S)
  9221. // S = SM * (S - dot(SM, S))
  9222. // S = diag_mask_zero(S, P) * scale
  9223. //
  9224. // grad[q][:D,iq1,iq2,iq3] += S @ kcur
  9225. // grad[k][:D,:M,ik2,ik3] += S.T @ qcur
  9226. // grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM
  9227. }
  9228. // S = gradSM = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3]
  9229. // S = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3]
  9230. // for ic:
  9231. // S[:M] += vcur[:M,ic,iv2,iv3] * d[ic,id1,id2,id3]
  9232. // exclude known future zero S[..] values from operation
  9233. ggml_vec_set_f32(masked_begin, S, 0);
  9234. for (int64_t ic = 0; ic < D; ++ic) {
  9235. ggml_vec_mad_f32(masked_begin,
  9236. S,
  9237. (float *) ((char *) v->data + ( ic*nbv1 + iv2*nbv2 + iv3*nbv3)),
  9238. *(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3)));
  9239. }
  9240. // S = SM * (S - dot(SM, S))
  9241. float dot_SM_gradSM = 0;
  9242. ggml_vec_dot_f32 (masked_begin, &dot_SM_gradSM, 0, SM, 0, S, 0, 1);
  9243. ggml_vec_acc1_f32(M, S, -dot_SM_gradSM);
  9244. ggml_vec_mul_f32 (masked_begin, S, S, SM);
  9245. // S = diag_mask_zero(S, P) * scale
  9246. // already done by above ggml_vec_set_f32
  9247. // exclude known zero S[..] values from operation
  9248. ggml_vec_scale_f32(masked_begin, S, scale);
  9249. // S shape [M,1]
  9250. // SM shape [M,1]
  9251. // kcur shape [D,M]
  9252. // qcur shape [D,1]
  9253. // vcur shape [M,D]
  9254. // grad[q][:D,iq1,iq2,iq3] += S @ kcur
  9255. // grad[q][:D,iq1,iq2,iq3] += shape[M,1] @ shape[D,M]
  9256. // for ic:
  9257. // grad[q][:D,iq1,iq2,iq3] += S[ic] * kcur[:D,ic,ik2,ik3]
  9258. // exclude known zero S[..] values from loop
  9259. for (int64_t ic = 0; ic < masked_begin; ++ic) {
  9260. ggml_vec_mad_f32(D,
  9261. (float *) ((char *) grad_q + (iq1*nbgq1 + iq2*nbgq2 + iq3*nbgq3)),
  9262. (float *) ((char *) k->data + (ic*nbk1 + ik2*nbk2 + ik3*nbk3)),
  9263. S[ic]);
  9264. }
  9265. // grad[k][:D,:M,iq2,iq3] += S.T @ qcur
  9266. // for ic:
  9267. // grad[k][:D,ic,iq2,iq3] += S.T[0,ic] * qcur[:D,0]
  9268. // grad[k][:D,ic,iq2,iq3] += S[ic] * qcur[:D,0]
  9269. // exclude known zero S[..] values from loop
  9270. for (int64_t ic = 0; ic < masked_begin; ++ic) {
  9271. ggml_vec_mad_f32(D,
  9272. (float *) ((char *) grad_k + (ic*nbgk1 + ik2*nbgk2 + ik3*nbgk3)),
  9273. (float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)),
  9274. S[ic]);
  9275. }
  9276. // grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM
  9277. // for ic:
  9278. // grad[v][:M,ic,iv2,iv3] += d[:D,id1,id2,id3].T[0,ic] * SM[:M]
  9279. // grad[v][:M,ic,iv2,iv3] += d[ic,id1,id2,id3] * SM[:M]
  9280. // exclude known zero SM[..] values from mad
  9281. for (int64_t ic = 0; ic < D; ++ic) {
  9282. ggml_vec_mad_f32(masked_begin,
  9283. (float *) ((char *) grad_v + ( ic*nbgv1 + iv2*nbgv2 + iv3*nbgv3)),
  9284. SM,
  9285. *(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3)));
  9286. }
  9287. }
  9288. }
  9289. }
  9290. }
  9291. static void ggml_compute_forward_flash_attn_back(
  9292. const struct ggml_compute_params * params,
  9293. const bool masked,
  9294. struct ggml_tensor * dst) {
  9295. const struct ggml_tensor * q = dst->src[0];
  9296. switch (q->type) {
  9297. case GGML_TYPE_F32:
  9298. {
  9299. ggml_compute_forward_flash_attn_back_f32(params, masked, dst);
  9300. } break;
  9301. default:
  9302. {
  9303. GGML_ABORT("fatal error");
  9304. }
  9305. }
  9306. }
  9307. // ggml_compute_forward_ssm_conv
  9308. static void ggml_compute_forward_ssm_conv_f32(
  9309. const struct ggml_compute_params * params,
  9310. struct ggml_tensor * dst) {
  9311. const struct ggml_tensor * src0 = dst->src[0]; // conv_x
  9312. const struct ggml_tensor * src1 = dst->src[1]; // conv1d.weight
  9313. const int ith = params->ith;
  9314. const int nth = params->nth;
  9315. const int nc = src1->ne[0]; // d_conv
  9316. const int ncs = src0->ne[0]; // d_conv - 1 + n_t
  9317. const int nr = src0->ne[1]; // d_inner
  9318. const int n_t = dst->ne[1]; // tokens per sequence
  9319. const int n_s = dst->ne[2]; // number of sequences in the batch
  9320. GGML_ASSERT( dst->ne[0] == nr);
  9321. GGML_ASSERT(src0->nb[0] == sizeof(float));
  9322. GGML_ASSERT(src1->nb[0] == sizeof(float));
  9323. GGML_ASSERT(src0->nb[1] == src0->ne[0]*sizeof(float));
  9324. // rows per thread
  9325. const int dr = (nr + nth - 1)/nth;
  9326. // row range for this thread
  9327. const int ir0 = dr*ith;
  9328. const int ir1 = MIN(ir0 + dr, nr);
  9329. const int ir = ir1 - ir0;
  9330. for (int i3 = 0; i3 < n_s; ++i3) {
  9331. for (int i2 = 0; i2 < n_t; ++i2) {
  9332. // {d_conv - 1 + n_t, d_inner, n_seqs}
  9333. // sliding window
  9334. const float * s = (const float *) ((const char *) src0->data + ir0*(src0->nb[1]) + i2*(src0->nb[0]) + i3*(src0->nb[2])); // {d_conv, d_inner, n_s}
  9335. const float * c = (const float *) ((const char *) src1->data + ir0*(src1->nb[1])); // {d_conv, d_inner}
  9336. float * x = (float *) ((char *) dst->data + ir0*(dst->nb[0]) + i2*(dst->nb[1]) + i3*(dst->nb[2])); // {d_inner, n_t, n_s}
  9337. // TODO: transpose the output for smaller strides for big batches?
  9338. // d_inner
  9339. for (int i1 = 0; i1 < ir; ++i1) {
  9340. // rowwise dot product
  9341. // NOTE: not using ggml_vec_dot_f32, because its sum is in double precision
  9342. float sumf = 0.0f;
  9343. // d_conv
  9344. for (int i0 = 0; i0 < nc; ++i0) {
  9345. sumf += s[i0 + i1*ncs] * c[i0 + i1*nc];
  9346. }
  9347. x[i1] = sumf;
  9348. }
  9349. }
  9350. }
  9351. }
  9352. static void ggml_compute_forward_ssm_conv(
  9353. const struct ggml_compute_params * params,
  9354. struct ggml_tensor * dst) {
  9355. switch (dst->src[0]->type) {
  9356. case GGML_TYPE_F32:
  9357. {
  9358. ggml_compute_forward_ssm_conv_f32(params, dst);
  9359. } break;
  9360. default:
  9361. {
  9362. GGML_ABORT("fatal error");
  9363. }
  9364. }
  9365. }
  9366. // ggml_compute_forward_ssm_scan
  9367. static void ggml_compute_forward_ssm_scan_f32(
  9368. const struct ggml_compute_params * params,
  9369. struct ggml_tensor * dst) {
  9370. const struct ggml_tensor * src0 = dst->src[0]; // s
  9371. const struct ggml_tensor * src1 = dst->src[1]; // x
  9372. const struct ggml_tensor * src2 = dst->src[2]; // dt
  9373. const struct ggml_tensor * src3 = dst->src[3]; // A
  9374. const struct ggml_tensor * src4 = dst->src[4]; // B
  9375. const struct ggml_tensor * src5 = dst->src[5]; // C
  9376. const int ith = params->ith;
  9377. const int nth = params->nth;
  9378. const int64_t nc = src0->ne[0]; // d_state
  9379. const int64_t nr = src0->ne[1]; // d_inner
  9380. const int64_t n_t = src1->ne[1]; // number of tokens per sequence
  9381. const int64_t n_s = src0->ne[2]; // number of sequences in the batch
  9382. GGML_ASSERT(ggml_nelements(src1) + ggml_nelements(src0) == ggml_nelements(dst));
  9383. GGML_ASSERT(src0->nb[0] == sizeof(float));
  9384. GGML_ASSERT(src1->nb[0] == sizeof(float));
  9385. GGML_ASSERT(src2->nb[0] == sizeof(float));
  9386. GGML_ASSERT(src3->nb[0] == sizeof(float));
  9387. GGML_ASSERT(src4->nb[0] == sizeof(float));
  9388. GGML_ASSERT(src5->nb[0] == sizeof(float));
  9389. // required for the dot product between s and C
  9390. GGML_ASSERT(src0->nb[1] == src0->ne[0]*sizeof(float));
  9391. // required for per-sequence offsets for states
  9392. GGML_ASSERT(src0->nb[2] == src0->ne[0]*src0->ne[1]*sizeof(float));
  9393. // required to get correct offset for state destination (i.e. src1->nb[3])
  9394. GGML_ASSERT(src1->nb[3] == src1->ne[0]*src1->ne[1]*src1->ne[2]*sizeof(float));
  9395. // rows per thread
  9396. const int dr = (nr + nth - 1)/nth;
  9397. // row range for this thread
  9398. const int ir0 = dr*ith;
  9399. const int ir1 = MIN(ir0 + dr, nr);
  9400. const int ir = ir1 - ir0;
  9401. for (int i3 = 0; i3 < n_s; ++i3) {
  9402. for (int i2 = 0; i2 < n_t; ++i2) {
  9403. const float * s0 = (const float *) ((const char *) src0->data + ir0*(src0->nb[1]) + i3*(src0->nb[2])); // {d_state, d_inner, n_s}
  9404. const float * x = (const float *) ((const char *) src1->data + ir0*(src1->nb[0]) + i2*(src1->nb[1]) + i3*(src1->nb[2])); // {d_inner, n_t, n_s}
  9405. const float * dt = (const float *) ((const char *) src2->data + ir0*(src2->nb[0]) + i2*(src2->nb[1]) + i3*(src2->nb[2])); // {d_inner, n_t, n_s}
  9406. const float * A = (const float *) ((const char *) src3->data + ir0*(src3->nb[1])); // {d_state, d_inner}
  9407. const float * B = (const float *) ((const char *) src4->data + i2*(src4->nb[1]) + i3*(src4->nb[2])); // {d_state, n_t, n_s}
  9408. const float * C = (const float *) ((const char *) src5->data + i2*(src5->nb[1]) + i3*(src5->nb[2])); // {d_state, n_t, n_s}
  9409. float * y = ( float *) (( char *) dst->data + ir0*(src1->nb[0]) + i2*(src1->nb[1]) + i3*(src1->nb[2])); // {d_inner, n_t, n_s}
  9410. float * s = ( float *) (( char *) dst->data + ir0*(src0->nb[1]) + i3*(src0->nb[2]) + src1->nb[3]); // {d_state, d_inner, n_s}
  9411. // use the output as the source for the next token-wise iterations
  9412. if (i2 > 0) { s0 = s; }
  9413. // d_inner
  9414. for (int i1 = 0; i1 < ir; ++i1) {
  9415. // ref: https://github.com/state-spaces/mamba/blob/34076d664838588a3c97727b263478ab9f621a07/mamba_ssm/ops/triton/selective_state_update.py#L78
  9416. float dt_soft_plus = dt[i1] <= 20.0f ? log1pf(expf(dt[i1])) : dt[i1];
  9417. float x_dt = x[i1] * dt_soft_plus;
  9418. float sumf = 0.0f;
  9419. // d_state
  9420. for (int i0 = 0; i0 < nc; ++i0) {
  9421. int i = i0 + i1*nc;
  9422. // state = prev_state * dA + dB * x
  9423. float state = (s0[i] * expf(dt_soft_plus * A[i])) + (B[i0] * x_dt);
  9424. // y = rowwise_dotprod(state, C)
  9425. sumf += state * C[i0];
  9426. s[i] = state;
  9427. }
  9428. y[i1] = sumf;
  9429. }
  9430. }
  9431. }
  9432. }
  9433. static void ggml_compute_forward_ssm_scan(
  9434. const struct ggml_compute_params * params,
  9435. struct ggml_tensor * dst) {
  9436. switch (dst->src[0]->type) {
  9437. case GGML_TYPE_F32:
  9438. {
  9439. ggml_compute_forward_ssm_scan_f32(params, dst);
  9440. } break;
  9441. default:
  9442. {
  9443. GGML_ABORT("fatal error");
  9444. }
  9445. }
  9446. }
  9447. // ggml_compute_forward_win_part
  9448. static void ggml_compute_forward_win_part_f32(
  9449. const struct ggml_compute_params * params,
  9450. struct ggml_tensor * dst) {
  9451. UNUSED(params);
  9452. const struct ggml_tensor * src0 = dst->src[0];
  9453. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  9454. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  9455. const int32_t nep0 = ((const int32_t *)(dst->op_params))[0];
  9456. const int32_t nep1 = ((const int32_t *)(dst->op_params))[1];
  9457. const int32_t w = ((const int32_t *)(dst->op_params))[2];
  9458. assert(ne00 == ne0);
  9459. assert(ne3 == nep0*nep1);
  9460. // TODO: optimize / multi-thread
  9461. for (int py = 0; py < nep1; ++py) {
  9462. for (int px = 0; px < nep0; ++px) {
  9463. const int64_t i3 = py*nep0 + px;
  9464. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  9465. for (int64_t i1 = 0; i1 < ne1; ++i1) {
  9466. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  9467. const int64_t i02 = py*w + i2;
  9468. const int64_t i01 = px*w + i1;
  9469. const int64_t i00 = i0;
  9470. const int64_t i = i3*ne2*ne1*ne0 + i2*ne1*ne0 + i1*ne0 + i0;
  9471. const int64_t j = i02*ne01*ne00 + i01*ne00 + i00;
  9472. if (py*w + i2 >= ne02 || px*w + i1 >= ne01) {
  9473. ((float *) dst->data)[i] = 0.0f;
  9474. } else {
  9475. ((float *) dst->data)[i] = ((float *) src0->data)[j];
  9476. }
  9477. }
  9478. }
  9479. }
  9480. }
  9481. }
  9482. }
  9483. static void ggml_compute_forward_win_part(
  9484. const struct ggml_compute_params * params,
  9485. struct ggml_tensor * dst) {
  9486. const struct ggml_tensor * src0 = dst->src[0];
  9487. switch (src0->type) {
  9488. case GGML_TYPE_F32:
  9489. {
  9490. ggml_compute_forward_win_part_f32(params, dst);
  9491. } break;
  9492. default:
  9493. {
  9494. GGML_ABORT("fatal error");
  9495. }
  9496. }
  9497. }
  9498. // ggml_compute_forward_win_unpart
  9499. static void ggml_compute_forward_win_unpart_f32(
  9500. const struct ggml_compute_params * params,
  9501. struct ggml_tensor * dst) {
  9502. UNUSED(params);
  9503. const struct ggml_tensor * src0 = dst->src[0];
  9504. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  9505. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  9506. const int32_t w = ((const int32_t *)(dst->op_params))[0];
  9507. // padding
  9508. const int px = (w - ne1%w)%w;
  9509. //const int py = (w - ne2%w)%w;
  9510. const int npx = (px + ne1)/w;
  9511. //const int npy = (py + ne2)/w;
  9512. assert(ne0 == ne00);
  9513. // TODO: optimize / multi-thread
  9514. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  9515. for (int64_t i1 = 0; i1 < ne1; ++i1) {
  9516. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  9517. const int ip2 = i2/w;
  9518. const int ip1 = i1/w;
  9519. const int64_t i02 = i2%w;
  9520. const int64_t i01 = i1%w;
  9521. const int64_t i00 = i0;
  9522. const int64_t i = (ip2*npx + ip1)*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00 + i00;
  9523. const int64_t j = i2*ne1*ne0 + i1*ne0 + i0;
  9524. ((float *) dst->data)[j] = ((float *) src0->data)[i];
  9525. }
  9526. }
  9527. }
  9528. }
  9529. static void ggml_compute_forward_win_unpart(
  9530. const struct ggml_compute_params * params,
  9531. struct ggml_tensor * dst) {
  9532. const struct ggml_tensor * src0 = dst->src[0];
  9533. switch (src0->type) {
  9534. case GGML_TYPE_F32:
  9535. {
  9536. ggml_compute_forward_win_unpart_f32(params, dst);
  9537. } break;
  9538. default:
  9539. {
  9540. GGML_ABORT("fatal error");
  9541. }
  9542. }
  9543. }
  9544. //gmml_compute_forward_unary
  9545. static void ggml_compute_forward_unary(
  9546. const struct ggml_compute_params * params,
  9547. struct ggml_tensor * dst) {
  9548. const enum ggml_unary_op op = ggml_get_unary_op(dst);
  9549. switch (op) {
  9550. case GGML_UNARY_OP_ABS:
  9551. {
  9552. ggml_compute_forward_abs(params, dst);
  9553. } break;
  9554. case GGML_UNARY_OP_SGN:
  9555. {
  9556. ggml_compute_forward_sgn(params, dst);
  9557. } break;
  9558. case GGML_UNARY_OP_NEG:
  9559. {
  9560. ggml_compute_forward_neg(params, dst);
  9561. } break;
  9562. case GGML_UNARY_OP_STEP:
  9563. {
  9564. ggml_compute_forward_step(params, dst);
  9565. } break;
  9566. case GGML_UNARY_OP_TANH:
  9567. {
  9568. ggml_compute_forward_tanh(params, dst);
  9569. } break;
  9570. case GGML_UNARY_OP_ELU:
  9571. {
  9572. ggml_compute_forward_elu(params, dst);
  9573. } break;
  9574. case GGML_UNARY_OP_RELU:
  9575. {
  9576. ggml_compute_forward_relu(params, dst);
  9577. } break;
  9578. case GGML_UNARY_OP_SIGMOID:
  9579. {
  9580. ggml_compute_forward_sigmoid(params, dst);
  9581. } break;
  9582. case GGML_UNARY_OP_GELU:
  9583. {
  9584. ggml_compute_forward_gelu(params, dst);
  9585. } break;
  9586. case GGML_UNARY_OP_GELU_QUICK:
  9587. {
  9588. ggml_compute_forward_gelu_quick(params, dst);
  9589. } break;
  9590. case GGML_UNARY_OP_SILU:
  9591. {
  9592. ggml_compute_forward_silu(params, dst);
  9593. } break;
  9594. case GGML_UNARY_OP_HARDSWISH:
  9595. {
  9596. ggml_compute_forward_hardswish(params, dst);
  9597. } break;
  9598. case GGML_UNARY_OP_HARDSIGMOID:
  9599. {
  9600. ggml_compute_forward_hardsigmoid(params, dst);
  9601. } break;
  9602. case GGML_UNARY_OP_EXP:
  9603. {
  9604. ggml_compute_forward_exp(params, dst);
  9605. } break;
  9606. default:
  9607. {
  9608. GGML_ABORT("fatal error");
  9609. }
  9610. }
  9611. }
  9612. // ggml_compute_forward_get_rel_pos
  9613. static void ggml_compute_forward_get_rel_pos_f16(
  9614. const struct ggml_compute_params * params,
  9615. struct ggml_tensor * dst) {
  9616. UNUSED(params);
  9617. const struct ggml_tensor * src0 = dst->src[0];
  9618. // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L292-L322
  9619. GGML_TENSOR_UNARY_OP_LOCALS
  9620. const int64_t w = ne1;
  9621. ggml_fp16_t * src0_data = (ggml_fp16_t *) src0->data;
  9622. ggml_fp16_t * dst_data = (ggml_fp16_t *) dst->data;
  9623. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  9624. for (int64_t i1 = 0; i1 < ne1; ++i1) {
  9625. const int64_t pos = (w - i1 - 1) + i2;
  9626. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  9627. dst_data[i2*ne1*ne0 + i1*ne0 + i0] = src0_data[pos*ne00 + i0];
  9628. }
  9629. }
  9630. }
  9631. }
  9632. static void ggml_compute_forward_get_rel_pos(
  9633. const struct ggml_compute_params * params,
  9634. struct ggml_tensor * dst) {
  9635. const struct ggml_tensor * src0 = dst->src[0];
  9636. switch (src0->type) {
  9637. case GGML_TYPE_F16:
  9638. case GGML_TYPE_BF16:
  9639. {
  9640. ggml_compute_forward_get_rel_pos_f16(params, dst);
  9641. } break;
  9642. default:
  9643. {
  9644. GGML_ABORT("fatal error");
  9645. }
  9646. }
  9647. }
  9648. // ggml_compute_forward_add_rel_pos
  9649. static void ggml_compute_forward_add_rel_pos_f32(
  9650. const struct ggml_compute_params * params,
  9651. struct ggml_tensor * dst) {
  9652. const struct ggml_tensor * src0 = dst->src[0];
  9653. const struct ggml_tensor * src1 = dst->src[1];
  9654. const struct ggml_tensor * src2 = dst->src[2];
  9655. const bool inplace = (bool) ((int32_t *) dst->op_params)[0];
  9656. if (!inplace) {
  9657. if (params->ith == 0) {
  9658. memcpy((char *) dst->data, (char *) src0->data, ggml_nbytes(dst));
  9659. }
  9660. ggml_barrier(params->threadpool);
  9661. }
  9662. // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L357-L359
  9663. float * src1_data = (float *) src1->data;
  9664. float * src2_data = (float *) src2->data;
  9665. float * dst_data = (float *) dst->data;
  9666. const int64_t ne10 = src1->ne[0];
  9667. const int64_t ne11 = src1->ne[1];
  9668. const int64_t ne12 = src1->ne[2];
  9669. const int64_t ne13 = src1->ne[3];
  9670. const int ith = params->ith;
  9671. const int nth = params->nth;
  9672. // total patches in dst
  9673. const int np = ne13;
  9674. // patches per thread
  9675. const int dp = (np + nth - 1)/nth;
  9676. // patch range for this thread
  9677. const int ip0 = dp*ith;
  9678. const int ip1 = MIN(ip0 + dp, np);
  9679. for (int64_t i13 = ip0; i13 < ip1; ++i13) {
  9680. for (int64_t i12 = 0; i12 < ne12; ++i12) {
  9681. for (int64_t i11 = 0; i11 < ne11; ++i11) {
  9682. const int64_t jp1 = i13*ne12*ne11*ne10 + i12*ne11*ne10 + i11*ne10;
  9683. for (int64_t i10 = 0; i10 < ne10; ++i10) {
  9684. const int64_t jp0 = jp1 + i10;
  9685. const float src1_e = src1_data[jp0];
  9686. const float src2_e = src2_data[jp0];
  9687. const int64_t jdh = jp0 * ne10;
  9688. const int64_t jdw = jdh - (ne10 - 1) * i10;
  9689. for (int64_t j = 0; j < ne10; ++j) {
  9690. dst_data[jdh + j ] += src2_e;
  9691. dst_data[jdw + j*ne10] += src1_e;
  9692. }
  9693. }
  9694. }
  9695. }
  9696. }
  9697. }
  9698. static void ggml_compute_forward_add_rel_pos(
  9699. const struct ggml_compute_params * params,
  9700. struct ggml_tensor * dst) {
  9701. const struct ggml_tensor * src0 = dst->src[0];
  9702. switch (src0->type) {
  9703. case GGML_TYPE_F32:
  9704. {
  9705. ggml_compute_forward_add_rel_pos_f32(params, dst);
  9706. } break;
  9707. default:
  9708. {
  9709. GGML_ABORT("fatal error");
  9710. }
  9711. }
  9712. }
  9713. // ggml_compute_forward_rwkv_wkv6
  9714. static void ggml_compute_forward_rwkv_wkv6_f32(
  9715. const struct ggml_compute_params * params,
  9716. struct ggml_tensor * dst) {
  9717. const int64_t T = dst->src[1]->ne[2];
  9718. const int64_t C = dst->ne[0];
  9719. const int64_t HEADS = dst->src[1]->ne[1];
  9720. const int64_t n_seqs = dst->src[5]->ne[1];
  9721. const int64_t head_size = C / HEADS;
  9722. float * dst_data = (float *) dst->data;
  9723. float * state = ((float *) dst->data) + C * T;
  9724. const int ith = params->ith;
  9725. const int nth = params->nth;
  9726. if (ith >= HEADS) {
  9727. return;
  9728. }
  9729. const int h_start = (HEADS * ith) / nth;
  9730. const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ?
  9731. (HEADS * (ith + 1)) / nth : HEADS;
  9732. float * k = (float *) dst->src[0]->data;
  9733. float * v = (float *) dst->src[1]->data;
  9734. float * r = (float *) dst->src[2]->data;
  9735. float * time_faaaa = (float *) dst->src[3]->data;
  9736. float * time_decay = (float *) dst->src[4]->data;
  9737. size_t t_stride = HEADS * head_size; // Same to C
  9738. size_t h_stride = C / HEADS;
  9739. GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS
  9740. size_t h_stride_2d = head_size * head_size;
  9741. if (ith == 0) {
  9742. memset(dst_data, 0, T * C * sizeof(float));
  9743. }
  9744. ggml_barrier(params->threadpool);
  9745. #if defined(__AVX__) && !defined(__AVX512F__)
  9746. #define GGML_F32X GGML_F32x8
  9747. #define GGML_F32X_SET1 GGML_F32x8_SET1
  9748. #define GGML_F32X_LOAD GGML_F32x8_LOAD
  9749. #define GGML_F32X_STORE GGML_F32x8_STORE
  9750. #define GGML_F32X_MUL GGML_F32x8_MUL
  9751. #define GGML_F32X_FMA GGML_F32x8_FMA
  9752. #define WKV_VECTOR_SIZE 8
  9753. #elif defined(__AVX512F__)
  9754. #define GGML_F32X GGML_F32x16
  9755. #define GGML_F32X_SET1 GGML_F32x16_SET1
  9756. #define GGML_F32X_LOAD GGML_F32x16_LOAD
  9757. #define GGML_F32X_STORE GGML_F32x16_STORE
  9758. #define GGML_F32X_MUL GGML_F32x16_MUL
  9759. #define GGML_F32X_FMA GGML_F32x16_FMA
  9760. #define WKV_VECTOR_SIZE 16
  9761. #elif defined(__ARM_NEON) && defined(__aarch64__)
  9762. #define GGML_F32X GGML_F32x4
  9763. #define GGML_F32X_SET1 GGML_F32x4_SET1
  9764. #define GGML_F32X_LOAD GGML_F32x4_LOAD
  9765. #define GGML_F32X_STORE GGML_F32x4_STORE
  9766. #define GGML_F32X_MUL GGML_F32x4_MUL
  9767. #define GGML_F32X_FMA GGML_F32x4_FMA
  9768. #define WKV_VECTOR_SIZE 4
  9769. #endif
  9770. #ifdef WKV_VECTOR_SIZE
  9771. const int64_t vec_count = head_size / WKV_VECTOR_SIZE;
  9772. for (int64_t t = 0; t < T; t++) {
  9773. size_t t_offset = t * t_stride;
  9774. size_t state_offset = head_size * C * (t / (T / n_seqs));
  9775. float * state_cur = state + state_offset;
  9776. float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset;
  9777. for (int64_t h = h_start; h < h_end; h++) {
  9778. size_t h_offset = h * h_stride;
  9779. size_t t_h_offset = t_offset + h_offset;
  9780. size_t h_2d_offset = h * h_stride_2d;
  9781. for (int64_t i = 0; i < head_size; i++) {
  9782. size_t t_h_i_offset = t_h_offset + i;
  9783. size_t h_i_offset = h_offset + i;
  9784. size_t h_2d_i_offset = h_2d_offset + i * h_stride;
  9785. float k_val = k[t_h_i_offset];
  9786. float r_val = r[t_h_i_offset];
  9787. float time_faaaa_val = time_faaaa[h_i_offset];
  9788. float time_decay_val = time_decay[t_h_i_offset];
  9789. // Broadcast scalar values to vectors
  9790. GGML_F32X k_vec = GGML_F32X_SET1(k_val);
  9791. GGML_F32X r_vec = GGML_F32X_SET1(r_val);
  9792. GGML_F32X time_faaaa_vec = GGML_F32X_SET1(time_faaaa_val);
  9793. GGML_F32X time_decay_vec = GGML_F32X_SET1(time_decay_val);
  9794. for (int64_t j = 0; j < vec_count; j++) {
  9795. size_t base_j = j * WKV_VECTOR_SIZE;
  9796. size_t t_h_j_offset = t_h_offset + base_j;
  9797. size_t h_2d_i_j_offset = h_2d_i_offset + base_j;
  9798. // Load x elements at once
  9799. GGML_F32X v_vec = GGML_F32X_LOAD(&v[t_h_j_offset]);
  9800. GGML_F32X prev_state_vec = GGML_F32X_LOAD(&state_prev[h_2d_i_j_offset]);
  9801. GGML_F32X dst_vec = GGML_F32X_LOAD(&dst_data[t_h_j_offset]);
  9802. // Compute kv = v * k
  9803. GGML_F32X kv_vec = GGML_F32X_MUL(v_vec, k_vec);
  9804. // Compute temp = kv * time_faaaa + prev_state
  9805. GGML_F32X temp_vec = GGML_F32X_FMA(prev_state_vec, kv_vec, time_faaaa_vec);
  9806. // Update dst: dst += temp * r
  9807. dst_vec = GGML_F32X_FMA(dst_vec, temp_vec, r_vec);
  9808. GGML_F32X_STORE(&dst_data[t_h_j_offset], dst_vec);
  9809. // Update state: state = prev_state * time_decay + kv
  9810. GGML_F32X new_state_vec = GGML_F32X_FMA(kv_vec, prev_state_vec, time_decay_vec);
  9811. GGML_F32X_STORE(&state_cur[h_2d_i_j_offset], new_state_vec);
  9812. }
  9813. // Handle remaining elements, this will not be used.
  9814. for (int64_t j = vec_count * WKV_VECTOR_SIZE; j < head_size; j++) {
  9815. size_t t_h_j_offset = t_h_offset + j;
  9816. size_t h_2d_i_j_offset = h_2d_i_offset + j;
  9817. float v_val = v[t_h_j_offset];
  9818. float kv_val = v_val * k_val;
  9819. float prev_state_val = state_prev[h_2d_i_j_offset];
  9820. float temp_val = kv_val * time_faaaa_val + prev_state_val;
  9821. dst_data[t_h_j_offset] += temp_val * r_val;
  9822. state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val;
  9823. }
  9824. }
  9825. }
  9826. }
  9827. #else
  9828. // basically fused operations:
  9829. // dst = r @ (time_faaaa * (k @ v) + state),
  9830. // state = time_decay * state + (k @ v),
  9831. // recursive through each token
  9832. for (int64_t t = 0; t < T; t++) {
  9833. size_t t_offset = t * t_stride;
  9834. size_t state_offset = head_size * C * (t / (T / n_seqs));
  9835. float * state_cur = state + state_offset;
  9836. float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset;
  9837. for (int64_t h = h_start; h < h_end; h++) {
  9838. size_t h_offset = h * h_stride;
  9839. size_t t_h_offset = t_offset + h_offset;
  9840. size_t h_2d_offset = h * h_stride_2d;
  9841. for (int64_t i = 0; i < head_size; i++) {
  9842. size_t t_h_i_offset = t_h_offset + i;
  9843. size_t h_i_offset = h_offset + i;
  9844. size_t h_2d_i_offset = h_2d_offset + i * h_stride;
  9845. float k_val = k[t_h_i_offset];
  9846. float r_val = r[t_h_i_offset];
  9847. float time_faaaa_val = time_faaaa[h_i_offset];
  9848. // RWKV v6: different time_decay for each token.
  9849. float time_decay_val = time_decay[t_h_i_offset];
  9850. for (int64_t j = 0; j < head_size; j++) {
  9851. size_t t_h_j_offset = t_h_offset + j;
  9852. size_t h_2d_i_j_offset = h_2d_i_offset + j;
  9853. float v_val = v[t_h_j_offset];
  9854. float kv_val = v_val * k_val;
  9855. float prev_state_val = state_prev[h_2d_i_j_offset];
  9856. float temp_val = kv_val * time_faaaa_val + prev_state_val;
  9857. dst_data[t_h_j_offset] += temp_val * r_val;
  9858. state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val;
  9859. }
  9860. }
  9861. }
  9862. }
  9863. #endif
  9864. }
  9865. static void ggml_compute_forward_rwkv_wkv6(
  9866. const struct ggml_compute_params * params,
  9867. struct ggml_tensor * dst) {
  9868. const struct ggml_tensor * src0 = dst->src[0];
  9869. switch (src0->type) {
  9870. case GGML_TYPE_F32:
  9871. {
  9872. ggml_compute_forward_rwkv_wkv6_f32(params, dst);
  9873. } break;
  9874. default:
  9875. {
  9876. GGML_ABORT("fatal error");
  9877. }
  9878. }
  9879. }
  9880. // ggml_compute_forward_gla
  9881. static void ggml_compute_forward_gla_f32(
  9882. const struct ggml_compute_params * params,
  9883. struct ggml_tensor * dst) {
  9884. const int64_t T = dst->src[1]->ne[2];
  9885. const int64_t C = dst->ne[0];
  9886. const int64_t HEADS = dst->src[1]->ne[1];
  9887. const int64_t n_seqs = dst->src[4]->ne[1];
  9888. const int64_t head_size = C / HEADS;
  9889. const float scale = ggml_get_op_params_f32(dst, 0);
  9890. float * dst_data = (float *) dst->data;
  9891. float * state = ((float *) dst->data) + C * T;
  9892. const int ith = params->ith;
  9893. const int nth = params->nth;
  9894. if (ith >= HEADS) {
  9895. return;
  9896. }
  9897. const int h_start = (HEADS * ith) / nth;
  9898. const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ?
  9899. (HEADS * (ith + 1)) / nth : HEADS;
  9900. float * k = (float *) dst->src[0]->data;
  9901. float * v = (float *) dst->src[1]->data;
  9902. float * q = (float *) dst->src[2]->data;
  9903. float * g = (float *) dst->src[3]->data;
  9904. size_t t_stride = HEADS * head_size; // Same to C
  9905. size_t h_stride = C / HEADS;
  9906. GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS
  9907. size_t h_stride_2d = head_size * head_size;
  9908. if (ith == 0) {
  9909. memset(dst_data, 0, T * C * sizeof(float));
  9910. }
  9911. ggml_barrier(params->threadpool);
  9912. #if defined(__AVX__) && !defined(__AVX512F__)
  9913. #define GGML_F32X GGML_F32x8
  9914. #define GGML_F32X_SET1 GGML_F32x8_SET1
  9915. #define GGML_F32X_LOAD GGML_F32x8_LOAD
  9916. #define GGML_F32X_STORE GGML_F32x8_STORE
  9917. #define GGML_F32X_MUL GGML_F32x8_MUL
  9918. #define GGML_F32X_FMA GGML_F32x8_FMA
  9919. #define GLA_VECTOR_SIZE 8
  9920. #elif defined(__AVX512F__)
  9921. #define GGML_F32X GGML_F32x16
  9922. #define GGML_F32X_SET1 GGML_F32x16_SET1
  9923. #define GGML_F32X_LOAD GGML_F32x16_LOAD
  9924. #define GGML_F32X_STORE GGML_F32x16_STORE
  9925. #define GGML_F32X_MUL GGML_F32x16_MUL
  9926. #define GGML_F32X_FMA GGML_F32x16_FMA
  9927. #define GLA_VECTOR_SIZE 16
  9928. #elif defined(__ARM_NEON) && defined(__aarch64__)
  9929. #define GGML_F32X GGML_F32x4
  9930. #define GGML_F32X_SET1 GGML_F32x4_SET1
  9931. #define GGML_F32X_LOAD GGML_F32x4_LOAD
  9932. #define GGML_F32X_STORE GGML_F32x4_STORE
  9933. #define GGML_F32X_MUL GGML_F32x4_MUL
  9934. #define GGML_F32X_FMA GGML_F32x4_FMA
  9935. #define GLA_VECTOR_SIZE 4
  9936. #endif
  9937. #ifdef GLA_VECTOR_SIZE
  9938. const int64_t vec_count = head_size / GLA_VECTOR_SIZE;
  9939. for (int64_t t = 0; t < T; t++) {
  9940. size_t t_offset = t * t_stride;
  9941. size_t state_offset = head_size * C * (t / (T / n_seqs));
  9942. float * state_cur = state + state_offset;
  9943. float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[4]->data + state_offset;
  9944. for (int64_t h = h_start; h < h_end; h++) {
  9945. size_t h_offset = h * h_stride;
  9946. size_t t_h_offset = t_offset + h_offset;
  9947. size_t h_2d_offset = h * h_stride_2d;
  9948. for (int64_t i = 0; i < head_size; i++) {
  9949. size_t t_h_i_offset = t_h_offset + i;
  9950. size_t h_2d_i_offset = h_2d_offset + i * h_stride;
  9951. float k_val = k[t_h_i_offset];
  9952. float q_val = q[t_h_i_offset] * scale;
  9953. float g_val = g[t_h_i_offset];
  9954. // Broadcast scalar values to vectors
  9955. GGML_F32X k_vec = GGML_F32X_SET1(k_val);
  9956. GGML_F32X q_vec = GGML_F32X_SET1(q_val);
  9957. GGML_F32X g_vec = GGML_F32X_SET1(g_val);
  9958. for (int64_t j = 0; j < vec_count; j++) {
  9959. size_t base_j = j * GLA_VECTOR_SIZE;
  9960. size_t t_h_j_offset = t_h_offset + base_j;
  9961. size_t h_2d_i_j_offset = h_2d_i_offset + base_j;
  9962. // Load x elements at once
  9963. GGML_F32X v_vec = GGML_F32X_LOAD(&v[t_h_j_offset]);
  9964. GGML_F32X prev_state_vec = GGML_F32X_LOAD(&state_prev[h_2d_i_j_offset]);
  9965. GGML_F32X dst_vec = GGML_F32X_LOAD(&dst_data[t_h_j_offset]);
  9966. // Compute kv = v * k
  9967. GGML_F32X kv_vec = GGML_F32X_MUL(v_vec, k_vec);
  9968. // Compute temp = prev_state * g + kv
  9969. GGML_F32X temp_vec = GGML_F32X_FMA(kv_vec, prev_state_vec, g_vec);
  9970. // Update dst: dst += temp * q
  9971. dst_vec = GGML_F32X_FMA(dst_vec, temp_vec, q_vec);
  9972. GGML_F32X_STORE(&dst_data[t_h_j_offset], dst_vec);
  9973. // Update state
  9974. GGML_F32X_STORE(&state_cur[h_2d_i_j_offset], temp_vec);
  9975. }
  9976. // Handle remaining elements, this will not be used.
  9977. for (int64_t j = vec_count * GLA_VECTOR_SIZE; j < head_size; j++) {
  9978. size_t t_h_j_offset = t_h_offset + j;
  9979. size_t h_2d_i_j_offset = h_2d_i_offset + j;
  9980. float v_val = v[t_h_j_offset];
  9981. float kv_val = v_val * k_val;
  9982. float prev_state_val = state_prev[h_2d_i_j_offset];
  9983. float temp_val = kv_val + prev_state_val * g_val;
  9984. dst_data[t_h_j_offset] += temp_val * q_val;
  9985. state_cur[h_2d_i_j_offset] = temp_val;
  9986. }
  9987. }
  9988. }
  9989. }
  9990. #else
  9991. for (int64_t t = 0; t < T; t++) {
  9992. size_t t_offset = t * t_stride;
  9993. size_t state_offset = head_size * C * (t / (T / n_seqs));
  9994. float * state_cur = state + state_offset;
  9995. float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[4]->data + state_offset;
  9996. for (int64_t h = h_start; h < h_end; h++) {
  9997. size_t h_offset = h * h_stride;
  9998. size_t t_h_offset = t_offset + h_offset;
  9999. size_t h_2d_offset = h * h_stride_2d;
  10000. for (int64_t i = 0; i < head_size; i++) {
  10001. size_t t_h_i_offset = t_h_offset + i;
  10002. size_t h_2d_i_offset = h_2d_offset + i * h_stride;
  10003. float k_val = k[t_h_i_offset];
  10004. float q_val = q[t_h_i_offset] * scale;
  10005. float g_val = g[t_h_i_offset];
  10006. for (int64_t j = 0; j < head_size; j++) {
  10007. size_t t_h_j_offset = t_h_offset + j;
  10008. size_t h_2d_i_j_offset = h_2d_i_offset + j;
  10009. float v_val = v[t_h_j_offset];
  10010. float kv_val = v_val * k_val;
  10011. float prev_state_val = state_prev[h_2d_i_j_offset];
  10012. float temp_val = prev_state_val * g_val + kv_val;
  10013. dst_data[t_h_j_offset] += temp_val * q_val;
  10014. state_cur[h_2d_i_j_offset] = temp_val;
  10015. }
  10016. }
  10017. }
  10018. }
  10019. #endif
  10020. }
  10021. static void ggml_compute_forward_gla(
  10022. const struct ggml_compute_params * params,
  10023. struct ggml_tensor * dst) {
  10024. const struct ggml_tensor * src0 = dst->src[0];
  10025. switch (src0->type) {
  10026. case GGML_TYPE_F32:
  10027. {
  10028. ggml_compute_forward_gla_f32(params, dst);
  10029. } break;
  10030. default:
  10031. {
  10032. GGML_ABORT("fatal error");
  10033. }
  10034. }
  10035. }
  10036. // ggml_compute_forward_map_unary
  10037. static void ggml_compute_forward_map_unary_f32(
  10038. const struct ggml_compute_params * params,
  10039. struct ggml_tensor * dst,
  10040. const ggml_unary_op_f32_t fun) {
  10041. const struct ggml_tensor * src0 = dst->src[0];
  10042. if (params->ith != 0) {
  10043. return;
  10044. }
  10045. assert(ggml_is_contiguous_1(src0));
  10046. assert(ggml_is_contiguous_1(dst));
  10047. assert(ggml_are_same_shape(src0, dst));
  10048. const int n = ggml_nrows(src0);
  10049. const int nc = src0->ne[0];
  10050. for (int i = 0; i < n; i++) {
  10051. fun(nc,
  10052. (float *) ((char *) dst->data + i*( dst->nb[1])),
  10053. (float *) ((char *) src0->data + i*(src0->nb[1])));
  10054. }
  10055. }
  10056. static void ggml_compute_forward_map_unary(
  10057. const struct ggml_compute_params * params,
  10058. struct ggml_tensor * dst,
  10059. const ggml_unary_op_f32_t fun) {
  10060. const struct ggml_tensor * src0 = dst->src[0];
  10061. switch (src0->type) {
  10062. case GGML_TYPE_F32:
  10063. {
  10064. ggml_compute_forward_map_unary_f32(params, dst, fun);
  10065. } break;
  10066. default:
  10067. {
  10068. GGML_ABORT("fatal error");
  10069. }
  10070. }
  10071. }
  10072. // ggml_compute_forward_map_binary
  10073. static void ggml_compute_forward_map_binary_f32(
  10074. const struct ggml_compute_params * params,
  10075. struct ggml_tensor * dst,
  10076. const ggml_binary_op_f32_t fun) {
  10077. const struct ggml_tensor * src0 = dst->src[0];
  10078. const struct ggml_tensor * src1 = dst->src[1];
  10079. if (params->ith != 0) {
  10080. return;
  10081. }
  10082. assert(ggml_is_contiguous_1(src0));
  10083. assert(ggml_is_contiguous_1(src1));
  10084. assert(ggml_is_contiguous_1(dst));
  10085. assert(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  10086. const int n = ggml_nrows(src0);
  10087. const int nc = src0->ne[0];
  10088. for (int i = 0; i < n; i++) {
  10089. fun(nc,
  10090. (float *) ((char *) dst->data + i*( dst->nb[1])),
  10091. (float *) ((char *) src0->data + i*(src0->nb[1])),
  10092. (float *) ((char *) src1->data + i*(src1->nb[1])));
  10093. }
  10094. }
  10095. static void ggml_compute_forward_map_binary(
  10096. const struct ggml_compute_params * params,
  10097. struct ggml_tensor * dst,
  10098. const ggml_binary_op_f32_t fun) {
  10099. const struct ggml_tensor * src0 = dst->src[0];
  10100. switch (src0->type) {
  10101. case GGML_TYPE_F32:
  10102. {
  10103. ggml_compute_forward_map_binary_f32(params, dst, fun);
  10104. } break;
  10105. default:
  10106. {
  10107. GGML_ABORT("fatal error");
  10108. }
  10109. }
  10110. }
  10111. // ggml_compute_forward_map_custom1
  10112. static void ggml_compute_forward_map_custom1_f32(
  10113. const struct ggml_compute_params * params,
  10114. struct ggml_tensor * dst,
  10115. const ggml_custom1_op_f32_t fun) {
  10116. const struct ggml_tensor * a = dst->src[0];
  10117. if (params->ith != 0) {
  10118. return;
  10119. }
  10120. fun(dst, a);
  10121. }
  10122. // ggml_compute_forward_map_custom2
  10123. static void ggml_compute_forward_map_custom2_f32(
  10124. const struct ggml_compute_params * params,
  10125. struct ggml_tensor * dst,
  10126. const ggml_custom2_op_f32_t fun) {
  10127. const struct ggml_tensor * a = dst->src[0];
  10128. const struct ggml_tensor * b = dst->src[1];
  10129. if (params->ith != 0) {
  10130. return;
  10131. }
  10132. fun(dst, a, b);
  10133. }
  10134. // ggml_compute_forward_map_custom3
  10135. static void ggml_compute_forward_map_custom3_f32(
  10136. const struct ggml_compute_params * params,
  10137. struct ggml_tensor * dst,
  10138. const ggml_custom3_op_f32_t fun) {
  10139. const struct ggml_tensor * a = dst->src[0];
  10140. const struct ggml_tensor * b = dst->src[1];
  10141. const struct ggml_tensor * c = dst->src[1];
  10142. if (params->ith != 0) {
  10143. return;
  10144. }
  10145. fun(dst, a, b, c);
  10146. }
  10147. // ggml_compute_forward_map_custom1
  10148. static void ggml_compute_forward_map_custom1(
  10149. const struct ggml_compute_params * params,
  10150. struct ggml_tensor * dst) {
  10151. const struct ggml_tensor * a = dst->src[0];
  10152. struct ggml_map_custom1_op_params p;
  10153. memcpy(&p, dst->op_params, sizeof(p));
  10154. p.fun(dst, a, params->ith, params->nth, p.userdata);
  10155. }
  10156. // ggml_compute_forward_map_custom2
  10157. static void ggml_compute_forward_map_custom2(
  10158. const struct ggml_compute_params * params,
  10159. struct ggml_tensor * dst) {
  10160. const struct ggml_tensor * a = dst->src[0];
  10161. const struct ggml_tensor * b = dst->src[1];
  10162. struct ggml_map_custom2_op_params p;
  10163. memcpy(&p, dst->op_params, sizeof(p));
  10164. p.fun(dst, a, b, params->ith, params->nth, p.userdata);
  10165. }
  10166. // ggml_compute_forward_map_custom3
  10167. static void ggml_compute_forward_map_custom3(
  10168. const struct ggml_compute_params * params,
  10169. struct ggml_tensor * dst) {
  10170. const struct ggml_tensor * a = dst->src[0];
  10171. const struct ggml_tensor * b = dst->src[1];
  10172. const struct ggml_tensor * c = dst->src[2];
  10173. struct ggml_map_custom3_op_params p;
  10174. memcpy(&p, dst->op_params, sizeof(p));
  10175. p.fun(dst, a, b, c, params->ith, params->nth, p.userdata);
  10176. }
  10177. // ggml_compute_forward_cross_entropy_loss
  10178. static void ggml_compute_forward_cross_entropy_loss_f32(
  10179. const struct ggml_compute_params * params,
  10180. struct ggml_tensor * dst) {
  10181. const struct ggml_tensor * src0 = dst->src[0];
  10182. const struct ggml_tensor * src1 = dst->src[1];
  10183. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10184. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10185. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  10186. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  10187. GGML_ASSERT(ggml_are_same_shape(src0, src1));
  10188. GGML_ASSERT(ggml_is_scalar(dst));
  10189. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10190. // TODO: handle transposed/permuted matrices
  10191. const int64_t nc = src0->ne[0];
  10192. const int64_t nr = ggml_nrows(src0);
  10193. const int ith = params->ith;
  10194. const int nth = params->nth;
  10195. float * sums = (float *) params->wdata;
  10196. float * st = ((float *) params->wdata) + nth + ith*nc;
  10197. float sum_thread = 0.0f;
  10198. GGML_ASSERT(params->wsize >= sizeof(float) * (nth + nth * nc));
  10199. // rows per thread
  10200. const int64_t dr = (nr + nth - 1)/nth;
  10201. // row range for this thread
  10202. const int64_t ir0 = dr*ith;
  10203. const int64_t ir1 = MIN(ir0 + dr, nr);
  10204. for (int64_t i1 = ir0; i1 < ir1; ++i1) {
  10205. const float * s0 = (const float *)((const char *) src0->data + i1*src0->nb[1]);
  10206. const float * s1 = (const float *)((const char *) src1->data + i1*src1->nb[1]);
  10207. #ifndef NDEBUG
  10208. for (int64_t i = 0; i < nc; ++i) {
  10209. //printf("p[%d] = %f\n", i, p[i]);
  10210. assert(!isnan(s0[i]));
  10211. assert(!isnan(s1[i]));
  10212. }
  10213. #endif
  10214. float max = -INFINITY;
  10215. ggml_vec_max_f32(nc, &max, s0);
  10216. const ggml_float sum_softmax = ggml_vec_log_soft_max_f32(nc, st, s0, max);
  10217. assert(sum_softmax >= 0.0);
  10218. ggml_vec_add1_f32(nc, st, st, -sum_softmax);
  10219. ggml_vec_mul_f32(nc, st, st, s1);
  10220. float sum_st = 0.0f;
  10221. ggml_vec_sum_f32(nc, &sum_st, st);
  10222. sum_thread += sum_st;
  10223. #ifndef NDEBUG
  10224. for (int64_t i = 0; i < nc; ++i) {
  10225. assert(!isnan(st[i]));
  10226. assert(!isinf(st[i]));
  10227. }
  10228. #endif
  10229. }
  10230. sums[ith] = sum_thread;
  10231. ggml_barrier(params->threadpool);
  10232. if (ith == 0) {
  10233. float * dp = (float *) dst->data;
  10234. ggml_vec_sum_f32(nth, dp, sums);
  10235. dp[0] *= -1.0f / (float) nr;
  10236. }
  10237. }
  10238. static void ggml_compute_forward_cross_entropy_loss(
  10239. const struct ggml_compute_params * params,
  10240. struct ggml_tensor * dst) {
  10241. const struct ggml_tensor * src0 = dst->src[0];
  10242. switch (src0->type) {
  10243. case GGML_TYPE_F32:
  10244. {
  10245. ggml_compute_forward_cross_entropy_loss_f32(params, dst);
  10246. } break;
  10247. default:
  10248. {
  10249. GGML_ABORT("fatal error");
  10250. }
  10251. }
  10252. }
  10253. // ggml_compute_forward_cross_entropy_loss_back
  10254. static void ggml_compute_forward_cross_entropy_loss_back_f32(
  10255. const struct ggml_compute_params * params,
  10256. struct ggml_tensor * dst) {
  10257. const struct ggml_tensor * grad = dst->src[0]; // gradient of forward pass output
  10258. const struct ggml_tensor * src0f = dst->src[1]; // src0 of forward pass
  10259. const struct ggml_tensor * src1f = dst->src[2]; // src1 of forward pass
  10260. GGML_ASSERT(ggml_is_contiguous(dst));
  10261. GGML_ASSERT(ggml_is_contiguous(src0f));
  10262. GGML_ASSERT(ggml_is_contiguous(src1f));
  10263. GGML_ASSERT(ggml_is_contiguous(grad));
  10264. GGML_ASSERT(ggml_are_same_shape(src0f, src1f) && ggml_are_same_shape(src0f, dst));
  10265. const int64_t ith = params->ith;
  10266. const int64_t nth = params->nth;
  10267. // TODO: handle transposed/permuted matrices
  10268. const int64_t nc = src0f->ne[0];
  10269. const int64_t nr = ggml_nrows(src0f);
  10270. // rows per thread
  10271. const int64_t dr = (nr + nth - 1)/nth;
  10272. // row range for this thread
  10273. const int64_t ir0 = dr*ith;
  10274. const int64_t ir1 = MIN(ir0 + dr, nr);
  10275. const float d_by_nr = ((const float *) grad->data)[0] / (float) nr;
  10276. for (int64_t i1 = ir0; i1 < ir1; i1++) {
  10277. float * ds0 = (float *)((char *) dst->data + i1*dst->nb[1]);
  10278. const float * s0 = (const float *)((const char *) src0f->data + i1*src0f->nb[1]);
  10279. const float * s1 = (const float *)((const char *) src1f->data + i1*src1f->nb[1]);
  10280. #ifndef NDEBUG
  10281. for (int64_t i = 0; i < nc; ++i) {
  10282. //printf("p[%d] = %f\n", i, p[i]);
  10283. assert(!isnan(s0[i]));
  10284. assert(!isnan(s1[i]));
  10285. }
  10286. #endif
  10287. // soft_max
  10288. float max = -INFINITY;
  10289. ggml_vec_max_f32(nc, &max, s0);
  10290. const ggml_float sum = ggml_vec_soft_max_f32(nc, ds0, s0, max);
  10291. assert(sum > 0.0);
  10292. ggml_vec_scale_f32(nc, ds0, 1.0/sum);
  10293. // grad(src0f) = (softmax(src0f) - src1f) * grad(cross_entropy_loss(src0f, src1f)) / nr
  10294. ggml_vec_sub_f32(nc, ds0, ds0, s1);
  10295. ggml_vec_scale_f32(nc, ds0, d_by_nr);
  10296. #ifndef NDEBUG
  10297. for (int64_t i = 0; i < nc; ++i) {
  10298. assert(!isnan(ds0[i]));
  10299. assert(!isinf(ds0[i]));
  10300. }
  10301. #endif
  10302. }
  10303. }
  10304. static void ggml_compute_forward_cross_entropy_loss_back(
  10305. const struct ggml_compute_params * params,
  10306. struct ggml_tensor * dst) {
  10307. const struct ggml_tensor * src0 = dst->src[0];
  10308. switch (src0->type) {
  10309. case GGML_TYPE_F32:
  10310. {
  10311. ggml_compute_forward_cross_entropy_loss_back_f32(params, dst);
  10312. } break;
  10313. default:
  10314. {
  10315. GGML_ABORT("fatal error");
  10316. }
  10317. }
  10318. }
  10319. static void ggml_compute_forward_opt_step_adamw_f32(
  10320. const struct ggml_compute_params * params,
  10321. struct ggml_tensor * dst) {
  10322. const struct ggml_tensor * src0 = dst->src[0];
  10323. const struct ggml_tensor * src0_grad = dst->src[1];
  10324. const struct ggml_tensor * src0_grad_m = dst->src[2];
  10325. const struct ggml_tensor * src0_grad_v = dst->src[3];
  10326. const struct ggml_tensor * adamw_params = dst->src[4];
  10327. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad));
  10328. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_m));
  10329. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_v));
  10330. GGML_ASSERT(ggml_nelements(adamw_params) == 7);
  10331. const int ith = params->ith;
  10332. const int nth = params->nth;
  10333. const int nr = ggml_nrows(src0);
  10334. GGML_TENSOR_UNARY_OP_LOCALS
  10335. GGML_ASSERT(nb00 == sizeof(float));
  10336. // rows per thread
  10337. const int dr = (nr + nth - 1)/nth;
  10338. // row range for this thread
  10339. const int ir0 = dr*ith;
  10340. const int ir1 = MIN(ir0 + dr, nr);
  10341. const float * adamw_params_ptr = ggml_get_data_f32(adamw_params);
  10342. const float alpha = adamw_params_ptr[0];
  10343. const float beta1 = adamw_params_ptr[1];
  10344. const float beta2 = adamw_params_ptr[2];
  10345. const float eps = adamw_params_ptr[3];
  10346. const float wd = adamw_params_ptr[4];
  10347. const float beta1h = adamw_params_ptr[5];
  10348. const float beta2h = adamw_params_ptr[6];
  10349. for (int ir = ir0; ir < ir1; ++ir) {
  10350. const int64_t i03 = ir/(ne02*ne01);
  10351. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  10352. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  10353. const size_t offset = i03*nb03 + i02*nb02 + i01*nb01;
  10354. float * w = (float *) ((char *) src0->data + offset); // weight
  10355. const float * g = (const float *) ((const char *) src0_grad->data + offset); // grad
  10356. float * m = (float *) ((char *) src0_grad_m->data + offset);
  10357. float * v = (float *) ((char *) src0_grad_v->data + offset);
  10358. for (int i00 = 0; i00 < ne00; ++i00) {
  10359. m[i00] = m[i00]*beta1 + g[i00]*(1.0f - beta1);
  10360. v[i00] = v[i00]*beta2 + g[i00]*g[i00]*(1.0f - beta2);
  10361. const float mh = m[i00]*beta1h;
  10362. const float vh = sqrtf(v[i00]*beta2h) + eps;
  10363. // The weight decay is applied independently of the Adam momenta m and v.
  10364. // This is NOT equivalent to l2 regularization that adds w[i00]*w[i00] to the loss.
  10365. // See: https://arxiv.org/pdf/1711.05101v3.pdf
  10366. w[i00] = w[i00]*(1.0f - alpha*wd) - alpha*mh/vh;
  10367. }
  10368. }
  10369. }
  10370. static void ggml_compute_forward_opt_step_adamw(
  10371. const struct ggml_compute_params * params,
  10372. struct ggml_tensor * dst) {
  10373. const struct ggml_tensor * src0 = dst->src[0];
  10374. switch (src0->type) {
  10375. case GGML_TYPE_F32:
  10376. {
  10377. ggml_compute_forward_opt_step_adamw_f32(params, dst);
  10378. } break;
  10379. default:
  10380. {
  10381. GGML_ABORT("fatal error");
  10382. }
  10383. }
  10384. }
  10385. /////////////////////////////////
  10386. static void ggml_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  10387. GGML_ASSERT(params);
  10388. if (tensor->op == GGML_OP_NONE || ggml_is_empty(tensor)) {
  10389. return;
  10390. }
  10391. // extra_buffer op?
  10392. if (ggml_cpu_extra_compute_forward(params, tensor)) return;
  10393. switch (tensor->op) {
  10394. case GGML_OP_DUP:
  10395. {
  10396. ggml_compute_forward_dup(params, tensor);
  10397. } break;
  10398. case GGML_OP_ADD:
  10399. {
  10400. ggml_compute_forward_add(params, tensor);
  10401. } break;
  10402. case GGML_OP_ADD1:
  10403. {
  10404. ggml_compute_forward_add1(params, tensor);
  10405. } break;
  10406. case GGML_OP_ACC:
  10407. {
  10408. ggml_compute_forward_acc(params, tensor);
  10409. } break;
  10410. case GGML_OP_SUB:
  10411. {
  10412. ggml_compute_forward_sub(params, tensor);
  10413. } break;
  10414. case GGML_OP_MUL:
  10415. {
  10416. ggml_compute_forward_mul(params, tensor);
  10417. } break;
  10418. case GGML_OP_DIV:
  10419. {
  10420. ggml_compute_forward_div(params, tensor);
  10421. } break;
  10422. case GGML_OP_SQR:
  10423. {
  10424. ggml_compute_forward_sqr(params, tensor);
  10425. } break;
  10426. case GGML_OP_SQRT:
  10427. {
  10428. ggml_compute_forward_sqrt(params, tensor);
  10429. } break;
  10430. case GGML_OP_LOG:
  10431. {
  10432. ggml_compute_forward_log(params, tensor);
  10433. } break;
  10434. case GGML_OP_SIN:
  10435. {
  10436. ggml_compute_forward_sin(params, tensor);
  10437. } break;
  10438. case GGML_OP_COS:
  10439. {
  10440. ggml_compute_forward_cos(params, tensor);
  10441. } break;
  10442. case GGML_OP_SUM:
  10443. {
  10444. ggml_compute_forward_sum(params, tensor);
  10445. } break;
  10446. case GGML_OP_SUM_ROWS:
  10447. {
  10448. ggml_compute_forward_sum_rows(params, tensor);
  10449. } break;
  10450. case GGML_OP_MEAN:
  10451. {
  10452. ggml_compute_forward_mean(params, tensor);
  10453. } break;
  10454. case GGML_OP_ARGMAX:
  10455. {
  10456. ggml_compute_forward_argmax(params, tensor);
  10457. } break;
  10458. case GGML_OP_COUNT_EQUAL:
  10459. {
  10460. ggml_compute_forward_count_equal(params, tensor);
  10461. } break;
  10462. case GGML_OP_REPEAT:
  10463. {
  10464. ggml_compute_forward_repeat(params, tensor);
  10465. } break;
  10466. case GGML_OP_REPEAT_BACK:
  10467. {
  10468. ggml_compute_forward_repeat_back(params, tensor);
  10469. } break;
  10470. case GGML_OP_CONCAT:
  10471. {
  10472. ggml_compute_forward_concat(params, tensor);
  10473. } break;
  10474. case GGML_OP_SILU_BACK:
  10475. {
  10476. ggml_compute_forward_silu_back(params, tensor);
  10477. } break;
  10478. case GGML_OP_NORM:
  10479. {
  10480. ggml_compute_forward_norm(params, tensor);
  10481. } break;
  10482. case GGML_OP_RMS_NORM:
  10483. {
  10484. ggml_compute_forward_rms_norm(params, tensor);
  10485. } break;
  10486. case GGML_OP_RMS_NORM_BACK:
  10487. {
  10488. ggml_compute_forward_rms_norm_back(params, tensor);
  10489. } break;
  10490. case GGML_OP_GROUP_NORM:
  10491. {
  10492. ggml_compute_forward_group_norm(params, tensor);
  10493. } break;
  10494. case GGML_OP_MUL_MAT:
  10495. {
  10496. ggml_compute_forward_mul_mat(params, tensor);
  10497. } break;
  10498. case GGML_OP_MUL_MAT_ID:
  10499. {
  10500. ggml_compute_forward_mul_mat_id(params, tensor);
  10501. } break;
  10502. case GGML_OP_OUT_PROD:
  10503. {
  10504. ggml_compute_forward_out_prod(params, tensor);
  10505. } break;
  10506. case GGML_OP_SCALE:
  10507. {
  10508. ggml_compute_forward_scale(params, tensor);
  10509. } break;
  10510. case GGML_OP_SET:
  10511. {
  10512. ggml_compute_forward_set(params, tensor);
  10513. } break;
  10514. case GGML_OP_CPY:
  10515. {
  10516. ggml_compute_forward_cpy(params, tensor);
  10517. } break;
  10518. case GGML_OP_CONT:
  10519. {
  10520. ggml_compute_forward_cont(params, tensor);
  10521. } break;
  10522. case GGML_OP_RESHAPE:
  10523. {
  10524. ggml_compute_forward_reshape(params, tensor);
  10525. } break;
  10526. case GGML_OP_VIEW:
  10527. {
  10528. ggml_compute_forward_view(params, tensor);
  10529. } break;
  10530. case GGML_OP_PERMUTE:
  10531. {
  10532. ggml_compute_forward_permute(params, tensor);
  10533. } break;
  10534. case GGML_OP_TRANSPOSE:
  10535. {
  10536. ggml_compute_forward_transpose(params, tensor);
  10537. } break;
  10538. case GGML_OP_GET_ROWS:
  10539. {
  10540. ggml_compute_forward_get_rows(params, tensor);
  10541. } break;
  10542. case GGML_OP_GET_ROWS_BACK:
  10543. {
  10544. ggml_compute_forward_get_rows_back(params, tensor);
  10545. } break;
  10546. case GGML_OP_DIAG:
  10547. {
  10548. ggml_compute_forward_diag(params, tensor);
  10549. } break;
  10550. case GGML_OP_DIAG_MASK_INF:
  10551. {
  10552. ggml_compute_forward_diag_mask_inf(params, tensor);
  10553. } break;
  10554. case GGML_OP_DIAG_MASK_ZERO:
  10555. {
  10556. ggml_compute_forward_diag_mask_zero(params, tensor);
  10557. } break;
  10558. case GGML_OP_SOFT_MAX:
  10559. {
  10560. ggml_compute_forward_soft_max(params, tensor);
  10561. } break;
  10562. case GGML_OP_SOFT_MAX_BACK:
  10563. {
  10564. ggml_compute_forward_soft_max_ext_back(params, tensor);
  10565. } break;
  10566. case GGML_OP_ROPE:
  10567. {
  10568. ggml_compute_forward_rope(params, tensor);
  10569. } break;
  10570. case GGML_OP_ROPE_BACK:
  10571. {
  10572. ggml_compute_forward_rope_back(params, tensor);
  10573. } break;
  10574. case GGML_OP_CLAMP:
  10575. {
  10576. ggml_compute_forward_clamp(params, tensor);
  10577. } break;
  10578. case GGML_OP_CONV_TRANSPOSE_1D:
  10579. {
  10580. ggml_compute_forward_conv_transpose_1d(params, tensor);
  10581. } break;
  10582. case GGML_OP_IM2COL:
  10583. {
  10584. ggml_compute_forward_im2col(params, tensor);
  10585. } break;
  10586. case GGML_OP_IM2COL_BACK:
  10587. {
  10588. ggml_compute_forward_im2col_back_f32(params, tensor);
  10589. } break;
  10590. case GGML_OP_CONV_TRANSPOSE_2D:
  10591. {
  10592. ggml_compute_forward_conv_transpose_2d(params, tensor);
  10593. } break;
  10594. case GGML_OP_POOL_1D:
  10595. {
  10596. ggml_compute_forward_pool_1d(params, tensor);
  10597. } break;
  10598. case GGML_OP_POOL_2D:
  10599. {
  10600. ggml_compute_forward_pool_2d(params, tensor);
  10601. } break;
  10602. case GGML_OP_POOL_2D_BACK:
  10603. {
  10604. ggml_compute_forward_pool_2d_back(params, tensor);
  10605. } break;
  10606. case GGML_OP_UPSCALE:
  10607. {
  10608. ggml_compute_forward_upscale(params, tensor);
  10609. } break;
  10610. case GGML_OP_PAD:
  10611. {
  10612. ggml_compute_forward_pad(params, tensor);
  10613. } break;
  10614. case GGML_OP_PAD_REFLECT_1D:
  10615. {
  10616. ggml_compute_forward_pad_reflect_1d(params, tensor);
  10617. } break;
  10618. case GGML_OP_ARANGE:
  10619. {
  10620. ggml_compute_forward_arange(params, tensor);
  10621. } break;
  10622. case GGML_OP_TIMESTEP_EMBEDDING:
  10623. {
  10624. ggml_compute_forward_timestep_embedding(params, tensor);
  10625. } break;
  10626. case GGML_OP_ARGSORT:
  10627. {
  10628. ggml_compute_forward_argsort(params, tensor);
  10629. } break;
  10630. case GGML_OP_LEAKY_RELU:
  10631. {
  10632. ggml_compute_forward_leaky_relu(params, tensor);
  10633. } break;
  10634. case GGML_OP_FLASH_ATTN_EXT:
  10635. {
  10636. ggml_compute_forward_flash_attn_ext(params, tensor->src[0], tensor->src[1], tensor->src[2], tensor->src[3], tensor);
  10637. } break;
  10638. case GGML_OP_FLASH_ATTN_BACK:
  10639. {
  10640. int32_t t = ggml_get_op_params_i32(tensor, 0);
  10641. GGML_ASSERT(t == 0 || t == 1);
  10642. bool masked = t != 0;
  10643. ggml_compute_forward_flash_attn_back(params, masked, tensor);
  10644. } break;
  10645. case GGML_OP_SSM_CONV:
  10646. {
  10647. ggml_compute_forward_ssm_conv(params, tensor);
  10648. } break;
  10649. case GGML_OP_SSM_SCAN:
  10650. {
  10651. ggml_compute_forward_ssm_scan(params, tensor);
  10652. } break;
  10653. case GGML_OP_WIN_PART:
  10654. {
  10655. ggml_compute_forward_win_part(params, tensor);
  10656. } break;
  10657. case GGML_OP_WIN_UNPART:
  10658. {
  10659. ggml_compute_forward_win_unpart(params, tensor);
  10660. } break;
  10661. case GGML_OP_UNARY:
  10662. {
  10663. ggml_compute_forward_unary(params, tensor);
  10664. } break;
  10665. case GGML_OP_GET_REL_POS:
  10666. {
  10667. ggml_compute_forward_get_rel_pos(params, tensor);
  10668. } break;
  10669. case GGML_OP_ADD_REL_POS:
  10670. {
  10671. ggml_compute_forward_add_rel_pos(params, tensor);
  10672. } break;
  10673. case GGML_OP_RWKV_WKV6:
  10674. {
  10675. ggml_compute_forward_rwkv_wkv6(params, tensor);
  10676. } break;
  10677. case GGML_OP_GATED_LINEAR_ATTN:
  10678. {
  10679. ggml_compute_forward_gla(params, tensor);
  10680. } break;
  10681. case GGML_OP_MAP_UNARY:
  10682. {
  10683. ggml_unary_op_f32_t fun;
  10684. memcpy(&fun, tensor->op_params, sizeof(fun));
  10685. ggml_compute_forward_map_unary(params, tensor, fun);
  10686. }
  10687. break;
  10688. case GGML_OP_MAP_BINARY:
  10689. {
  10690. ggml_binary_op_f32_t fun;
  10691. memcpy(&fun, tensor->op_params, sizeof(fun));
  10692. ggml_compute_forward_map_binary(params, tensor, fun);
  10693. }
  10694. break;
  10695. case GGML_OP_MAP_CUSTOM1_F32:
  10696. {
  10697. ggml_custom1_op_f32_t fun;
  10698. memcpy(&fun, tensor->op_params, sizeof(fun));
  10699. ggml_compute_forward_map_custom1_f32(params, tensor, fun);
  10700. }
  10701. break;
  10702. case GGML_OP_MAP_CUSTOM2_F32:
  10703. {
  10704. ggml_custom2_op_f32_t fun;
  10705. memcpy(&fun, tensor->op_params, sizeof(fun));
  10706. ggml_compute_forward_map_custom2_f32(params, tensor, fun);
  10707. }
  10708. break;
  10709. case GGML_OP_MAP_CUSTOM3_F32:
  10710. {
  10711. ggml_custom3_op_f32_t fun;
  10712. memcpy(&fun, tensor->op_params, sizeof(fun));
  10713. ggml_compute_forward_map_custom3_f32(params, tensor, fun);
  10714. }
  10715. break;
  10716. case GGML_OP_MAP_CUSTOM1:
  10717. {
  10718. ggml_compute_forward_map_custom1(params, tensor);
  10719. }
  10720. break;
  10721. case GGML_OP_MAP_CUSTOM2:
  10722. {
  10723. ggml_compute_forward_map_custom2(params, tensor);
  10724. }
  10725. break;
  10726. case GGML_OP_MAP_CUSTOM3:
  10727. {
  10728. ggml_compute_forward_map_custom3(params, tensor);
  10729. }
  10730. break;
  10731. case GGML_OP_CROSS_ENTROPY_LOSS:
  10732. {
  10733. ggml_compute_forward_cross_entropy_loss(params, tensor);
  10734. }
  10735. break;
  10736. case GGML_OP_CROSS_ENTROPY_LOSS_BACK:
  10737. {
  10738. ggml_compute_forward_cross_entropy_loss_back(params, tensor);
  10739. }
  10740. break;
  10741. case GGML_OP_OPT_STEP_ADAMW:
  10742. {
  10743. ggml_compute_forward_opt_step_adamw(params, tensor);
  10744. }
  10745. break;
  10746. case GGML_OP_NONE:
  10747. {
  10748. // nop
  10749. } break;
  10750. case GGML_OP_COUNT:
  10751. {
  10752. GGML_ABORT("fatal error");
  10753. }
  10754. }
  10755. }
  10756. // Android's libc implementation "bionic" does not support setting affinity
  10757. #if defined(__gnu_linux__)
  10758. static void set_numa_thread_affinity(int thread_n) {
  10759. if (!ggml_is_numa()) {
  10760. return;
  10761. }
  10762. int node_num;
  10763. int rv;
  10764. size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus);
  10765. switch(g_state.numa.numa_strategy) {
  10766. case GGML_NUMA_STRATEGY_DISTRIBUTE:
  10767. // run thread on node_num thread_n / (threads per node)
  10768. node_num = thread_n % g_state.numa.n_nodes;
  10769. break;
  10770. case GGML_NUMA_STRATEGY_ISOLATE:
  10771. // run thread on current_node
  10772. node_num = g_state.numa.current_node;
  10773. break;
  10774. case GGML_NUMA_STRATEGY_NUMACTL:
  10775. // use the cpuset that numactl gave us
  10776. rv = pthread_setaffinity_np(pthread_self(), setsize, &g_state.numa.cpuset);
  10777. if (rv) {
  10778. fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n",strerror(rv));
  10779. }
  10780. return;
  10781. default:
  10782. return;
  10783. }
  10784. struct ggml_numa_node * node = &g_state.numa.nodes[node_num];
  10785. cpu_set_t * cpus = CPU_ALLOC(g_state.numa.total_cpus);
  10786. CPU_ZERO_S(setsize, cpus);
  10787. for (size_t i = 0; i < node->n_cpus; ++i) {
  10788. CPU_SET_S(node->cpus[i], setsize, cpus);
  10789. }
  10790. rv = pthread_setaffinity_np(pthread_self(), setsize, cpus);
  10791. if (rv) {
  10792. fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n", strerror(rv));
  10793. }
  10794. CPU_FREE(cpus);
  10795. }
  10796. static void clear_numa_thread_affinity(void) {
  10797. if (!ggml_is_numa()) {
  10798. return;
  10799. }
  10800. size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus);
  10801. cpu_set_t * cpus = CPU_ALLOC(g_state.numa.total_cpus);
  10802. CPU_ZERO_S(setsize, cpus);
  10803. for (unsigned i = 0; i < g_state.numa.total_cpus; ++i) {
  10804. CPU_SET_S(i, setsize, cpus);
  10805. }
  10806. int rv = pthread_setaffinity_np(pthread_self(), setsize, cpus);
  10807. if (rv) {
  10808. fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n", strerror(rv));
  10809. }
  10810. CPU_FREE(cpus);
  10811. }
  10812. #else
  10813. // TODO: Windows etc.
  10814. // (the linux implementation may also work on BSD, someone should test)
  10815. static void set_numa_thread_affinity(int thread_n) { UNUSED(thread_n); }
  10816. static void clear_numa_thread_affinity(void) {}
  10817. #endif
  10818. static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) {
  10819. int n_tasks = 0;
  10820. if (ggml_is_empty(node)) {
  10821. // no need to multi-thread a no-op
  10822. n_tasks = 1;
  10823. return n_tasks;
  10824. }
  10825. switch (node->op) {
  10826. case GGML_OP_CPY:
  10827. case GGML_OP_DUP:
  10828. case GGML_OP_CONT:
  10829. case GGML_OP_ADD:
  10830. case GGML_OP_ADD1:
  10831. case GGML_OP_ACC:
  10832. {
  10833. n_tasks = n_threads;
  10834. } break;
  10835. case GGML_OP_SUB:
  10836. case GGML_OP_SQR:
  10837. case GGML_OP_SQRT:
  10838. case GGML_OP_LOG:
  10839. case GGML_OP_SIN:
  10840. case GGML_OP_COS:
  10841. case GGML_OP_SUM:
  10842. case GGML_OP_SUM_ROWS:
  10843. case GGML_OP_MEAN:
  10844. case GGML_OP_ARGMAX:
  10845. {
  10846. n_tasks = 1;
  10847. } break;
  10848. case GGML_OP_COUNT_EQUAL:
  10849. {
  10850. n_tasks = n_threads;
  10851. } break;
  10852. case GGML_OP_REPEAT:
  10853. case GGML_OP_REPEAT_BACK:
  10854. case GGML_OP_LEAKY_RELU:
  10855. {
  10856. n_tasks = 1;
  10857. } break;
  10858. case GGML_OP_UNARY:
  10859. switch (ggml_get_unary_op(node)) {
  10860. case GGML_UNARY_OP_ABS:
  10861. case GGML_UNARY_OP_SGN:
  10862. case GGML_UNARY_OP_NEG:
  10863. case GGML_UNARY_OP_STEP:
  10864. case GGML_UNARY_OP_TANH:
  10865. case GGML_UNARY_OP_ELU:
  10866. case GGML_UNARY_OP_RELU:
  10867. case GGML_UNARY_OP_SIGMOID:
  10868. case GGML_UNARY_OP_HARDSWISH:
  10869. case GGML_UNARY_OP_HARDSIGMOID:
  10870. case GGML_UNARY_OP_EXP:
  10871. {
  10872. n_tasks = 1;
  10873. } break;
  10874. case GGML_UNARY_OP_GELU:
  10875. case GGML_UNARY_OP_GELU_QUICK:
  10876. case GGML_UNARY_OP_SILU:
  10877. {
  10878. n_tasks = n_threads;
  10879. } break;
  10880. default:
  10881. GGML_ABORT("fatal error");
  10882. }
  10883. break;
  10884. case GGML_OP_SILU_BACK:
  10885. case GGML_OP_MUL:
  10886. case GGML_OP_DIV:
  10887. case GGML_OP_NORM:
  10888. case GGML_OP_RMS_NORM:
  10889. case GGML_OP_RMS_NORM_BACK:
  10890. case GGML_OP_GROUP_NORM:
  10891. case GGML_OP_CONCAT:
  10892. case GGML_OP_MUL_MAT:
  10893. case GGML_OP_MUL_MAT_ID:
  10894. case GGML_OP_OUT_PROD:
  10895. {
  10896. n_tasks = n_threads;
  10897. } break;
  10898. case GGML_OP_GET_ROWS:
  10899. {
  10900. // FIXME: get_rows can use additional threads, but the cost of launching additional threads
  10901. // decreases performance with GPU offloading
  10902. //n_tasks = n_threads;
  10903. n_tasks = 1;
  10904. } break;
  10905. case GGML_OP_SCALE:
  10906. case GGML_OP_SET:
  10907. case GGML_OP_RESHAPE:
  10908. case GGML_OP_VIEW:
  10909. case GGML_OP_PERMUTE:
  10910. case GGML_OP_TRANSPOSE:
  10911. case GGML_OP_GET_ROWS_BACK:
  10912. case GGML_OP_DIAG:
  10913. {
  10914. n_tasks = 1;
  10915. } break;
  10916. case GGML_OP_DIAG_MASK_ZERO:
  10917. case GGML_OP_DIAG_MASK_INF:
  10918. case GGML_OP_SOFT_MAX_BACK:
  10919. case GGML_OP_ROPE:
  10920. case GGML_OP_ROPE_BACK:
  10921. case GGML_OP_ADD_REL_POS:
  10922. {
  10923. n_tasks = n_threads;
  10924. } break;
  10925. case GGML_OP_CLAMP:
  10926. {
  10927. n_tasks = 1; //TODO
  10928. } break;
  10929. case GGML_OP_SOFT_MAX:
  10930. {
  10931. n_tasks = MIN(n_threads, ggml_nrows(node->src[0]));
  10932. } break;
  10933. case GGML_OP_IM2COL:
  10934. case GGML_OP_IM2COL_BACK:
  10935. case GGML_OP_CONV_TRANSPOSE_1D:
  10936. case GGML_OP_CONV_TRANSPOSE_2D:
  10937. {
  10938. n_tasks = n_threads;
  10939. } break;
  10940. case GGML_OP_POOL_1D:
  10941. case GGML_OP_POOL_2D:
  10942. case GGML_OP_POOL_2D_BACK:
  10943. {
  10944. n_tasks = 1;
  10945. } break;
  10946. case GGML_OP_UPSCALE:
  10947. case GGML_OP_PAD:
  10948. case GGML_OP_PAD_REFLECT_1D:
  10949. case GGML_OP_ARANGE:
  10950. case GGML_OP_TIMESTEP_EMBEDDING:
  10951. case GGML_OP_ARGSORT:
  10952. case GGML_OP_FLASH_ATTN_EXT:
  10953. case GGML_OP_FLASH_ATTN_BACK:
  10954. case GGML_OP_SSM_CONV:
  10955. case GGML_OP_SSM_SCAN:
  10956. {
  10957. n_tasks = n_threads;
  10958. } break;
  10959. case GGML_OP_WIN_PART:
  10960. case GGML_OP_WIN_UNPART:
  10961. case GGML_OP_GET_REL_POS:
  10962. case GGML_OP_RWKV_WKV6:
  10963. case GGML_OP_GATED_LINEAR_ATTN:
  10964. case GGML_OP_MAP_UNARY:
  10965. case GGML_OP_MAP_BINARY:
  10966. case GGML_OP_MAP_CUSTOM1_F32:
  10967. case GGML_OP_MAP_CUSTOM2_F32:
  10968. case GGML_OP_MAP_CUSTOM3_F32:
  10969. {
  10970. n_tasks = 1;
  10971. } break;
  10972. case GGML_OP_MAP_CUSTOM1:
  10973. {
  10974. struct ggml_map_custom1_op_params p;
  10975. memcpy(&p, node->op_params, sizeof(p));
  10976. if (p.n_tasks == GGML_N_TASKS_MAX) {
  10977. n_tasks = n_threads;
  10978. } else {
  10979. n_tasks = MIN(p.n_tasks, n_threads);
  10980. }
  10981. } break;
  10982. case GGML_OP_MAP_CUSTOM2:
  10983. {
  10984. struct ggml_map_custom2_op_params p;
  10985. memcpy(&p, node->op_params, sizeof(p));
  10986. if (p.n_tasks == GGML_N_TASKS_MAX) {
  10987. n_tasks = n_threads;
  10988. } else {
  10989. n_tasks = MIN(p.n_tasks, n_threads);
  10990. }
  10991. } break;
  10992. case GGML_OP_MAP_CUSTOM3:
  10993. {
  10994. struct ggml_map_custom3_op_params p;
  10995. memcpy(&p, node->op_params, sizeof(p));
  10996. if (p.n_tasks == GGML_N_TASKS_MAX) {
  10997. n_tasks = n_threads;
  10998. } else {
  10999. n_tasks = MIN(p.n_tasks, n_threads);
  11000. }
  11001. } break;
  11002. case GGML_OP_CROSS_ENTROPY_LOSS:
  11003. case GGML_OP_CROSS_ENTROPY_LOSS_BACK:
  11004. case GGML_OP_OPT_STEP_ADAMW:
  11005. {
  11006. n_tasks = n_threads;
  11007. } break;
  11008. case GGML_OP_NONE:
  11009. {
  11010. n_tasks = 1;
  11011. } break;
  11012. case GGML_OP_COUNT:
  11013. {
  11014. GGML_ABORT("fatal error");
  11015. }
  11016. default:
  11017. {
  11018. fprintf(stderr, "%s: op not implemented: ", __func__);
  11019. if (node->op < GGML_OP_COUNT) {
  11020. fprintf(stderr, "%s\n", ggml_op_name(node->op));
  11021. } else {
  11022. fprintf(stderr, "%d\n", node->op);
  11023. }
  11024. GGML_ABORT("fatal error");
  11025. }
  11026. }
  11027. assert(n_tasks > 0);
  11028. return n_tasks;
  11029. }
  11030. static thread_ret_t ggml_graph_compute_secondary_thread(void* data);
  11031. #if defined(_WIN32)
  11032. #include "windows.h"
  11033. // TODO: support > 64 CPUs
  11034. static bool ggml_thread_apply_affinity(bool * mask) {
  11035. HANDLE h = GetCurrentThread();
  11036. uint64_t bitmask = 0ULL;
  11037. assert(GGML_MAX_N_THREADS >= 64);
  11038. for (int32_t i = 0; i < 8; i++) {
  11039. int32_t idx = i * 8;
  11040. uint8_t val = 0;
  11041. val |= mask[idx + 0] << 0;
  11042. val |= mask[idx + 1] << 1;
  11043. val |= mask[idx + 2] << 2;
  11044. val |= mask[idx + 3] << 3;
  11045. val |= mask[idx + 4] << 4;
  11046. val |= mask[idx + 5] << 5;
  11047. val |= mask[idx + 6] << 6;
  11048. val |= mask[idx + 7] << 7;
  11049. bitmask |= (uint64_t)val << idx;
  11050. }
  11051. for (int32_t i = 64; i < GGML_MAX_N_THREADS; i++) {
  11052. if (mask[i]) {
  11053. fprintf(stderr, "warn: setting thread-affinity for > 64 CPUs isn't supported on windows!\n");
  11054. break;
  11055. }
  11056. }
  11057. DWORD_PTR m = (DWORD_PTR)bitmask;
  11058. m = SetThreadAffinityMask(h, m);
  11059. return m != 0;
  11060. }
  11061. static bool ggml_thread_apply_priority(int32_t prio) {
  11062. // Note that on Windows the Process Priority Class must be updated in order to set Thread priority.
  11063. // This is up to the applications.
  11064. DWORD p = THREAD_PRIORITY_NORMAL;
  11065. switch (prio) {
  11066. case GGML_SCHED_PRIO_NORMAL: p = THREAD_PRIORITY_NORMAL; break;
  11067. case GGML_SCHED_PRIO_MEDIUM: p = THREAD_PRIORITY_ABOVE_NORMAL; break;
  11068. case GGML_SCHED_PRIO_HIGH: p = THREAD_PRIORITY_HIGHEST; break;
  11069. case GGML_SCHED_PRIO_REALTIME: p = THREAD_PRIORITY_TIME_CRITICAL; break;
  11070. }
  11071. if (prio == GGML_SCHED_PRIO_NORMAL) {
  11072. // Keep inherited policy/priority
  11073. return true;
  11074. }
  11075. if (!SetThreadPriority(GetCurrentThread(), p)) {
  11076. fprintf(stderr, "warn: failed to set thread priority %d : (%d)\n", prio, (int) GetLastError());
  11077. return false;
  11078. }
  11079. return true;
  11080. }
  11081. #elif defined(__APPLE__)
  11082. #include <sys/types.h>
  11083. #include <sys/resource.h>
  11084. static bool ggml_thread_apply_affinity(const bool * mask) {
  11085. // Not supported on Apple platforms
  11086. UNUSED(mask);
  11087. return true;
  11088. }
  11089. static bool ggml_thread_apply_priority(int32_t prio) {
  11090. struct sched_param p;
  11091. int32_t policy = SCHED_OTHER;
  11092. switch (prio) {
  11093. case GGML_SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
  11094. case GGML_SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
  11095. case GGML_SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
  11096. case GGML_SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
  11097. }
  11098. if (prio == GGML_SCHED_PRIO_NORMAL) {
  11099. // Keep inherited policy/priority
  11100. return true;
  11101. }
  11102. int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
  11103. if (err != 0) {
  11104. fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
  11105. return false;
  11106. }
  11107. return true;
  11108. }
  11109. #elif defined(__gnu_linux__)
  11110. // TODO: this may not work on BSD, to be verified
  11111. static bool ggml_thread_apply_affinity(const bool * mask) {
  11112. cpu_set_t cpuset;
  11113. int err;
  11114. CPU_ZERO(&cpuset);
  11115. for (uint32_t i = 0; i < GGML_MAX_N_THREADS; i++) {
  11116. if (mask[i]) {
  11117. GGML_PRINT_DEBUG("Thread %lx: adding %d to cpuset\n", pthread_self(), i);
  11118. CPU_SET(i, &cpuset);
  11119. }
  11120. }
  11121. #ifdef __ANDROID__
  11122. err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
  11123. if (err < 0) {
  11124. err = errno;
  11125. }
  11126. #else
  11127. err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
  11128. #endif
  11129. if (err != 0) {
  11130. fprintf(stderr, "warn: failed to set affinity mask 0x%llx : %s (%d)\n", (unsigned long long)mask, strerror(err), err);
  11131. return false;
  11132. }
  11133. return true;
  11134. }
  11135. static bool ggml_thread_apply_priority(int32_t prio) {
  11136. struct sched_param p;
  11137. int32_t policy = SCHED_OTHER;
  11138. switch (prio) {
  11139. case GGML_SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
  11140. case GGML_SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
  11141. case GGML_SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
  11142. case GGML_SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
  11143. }
  11144. if (prio == GGML_SCHED_PRIO_NORMAL) {
  11145. // Keep inherited policy/priority
  11146. return true;
  11147. }
  11148. int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
  11149. if (err != 0) {
  11150. fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
  11151. return false;
  11152. }
  11153. return true;
  11154. }
  11155. #else // unsupported platforms
  11156. static bool ggml_thread_apply_affinity(const bool * mask) {
  11157. UNUSED(mask);
  11158. return true;
  11159. }
  11160. static bool ggml_thread_apply_priority(int32_t prio) {
  11161. UNUSED(prio);
  11162. return true;
  11163. }
  11164. #endif
  11165. static bool ggml_thread_cpumask_is_valid(const bool * mask) {
  11166. for (int i = 0; i < GGML_MAX_N_THREADS; i++) {
  11167. if (mask[i]) { return true; }
  11168. }
  11169. return false;
  11170. }
  11171. static void ggml_thread_cpumask_next(const bool * global_mask, bool * local_mask, bool strict, int32_t* iter) {
  11172. if (!strict) {
  11173. memcpy(local_mask, global_mask, GGML_MAX_N_THREADS);
  11174. return;
  11175. } else {
  11176. memset(local_mask, 0, GGML_MAX_N_THREADS);
  11177. int32_t base_idx = *iter;
  11178. for (int32_t i = 0; i < GGML_MAX_N_THREADS; i++) {
  11179. int32_t idx = base_idx + i;
  11180. if (idx >= GGML_MAX_N_THREADS) {
  11181. // Just a cheaper modulo
  11182. idx -= GGML_MAX_N_THREADS;
  11183. }
  11184. if (global_mask[idx]) {
  11185. local_mask[idx] = 1;
  11186. *iter = idx + 1;
  11187. return;
  11188. }
  11189. }
  11190. }
  11191. }
  11192. void ggml_threadpool_free(struct ggml_threadpool* threadpool) {
  11193. if (!threadpool) return;
  11194. const int n_threads = threadpool->n_threads_max;
  11195. #ifndef GGML_USE_OPENMP
  11196. struct ggml_compute_state* workers = threadpool->workers;
  11197. ggml_mutex_lock(&threadpool->mutex);
  11198. threadpool->stop = true;
  11199. threadpool->pause = false;
  11200. ggml_cond_broadcast(&threadpool->cond);
  11201. ggml_mutex_unlock(&threadpool->mutex);
  11202. for (int j = 1; j < n_threads; j++) {
  11203. int32_t rc = ggml_thread_join(workers[j].thrd, NULL);
  11204. GGML_ASSERT(rc == GGML_EXIT_SUCCESS || rc == GGML_EXIT_ABORTED);
  11205. UNUSED(rc);
  11206. }
  11207. ggml_mutex_destroy(&threadpool->mutex);
  11208. ggml_cond_destroy(&threadpool->cond);
  11209. #endif // GGML_USE_OPENMP
  11210. const size_t workers_size = sizeof(struct ggml_compute_state) * n_threads;
  11211. ggml_aligned_free(threadpool->workers, workers_size);
  11212. ggml_aligned_free(threadpool, sizeof(struct ggml_threadpool));
  11213. }
  11214. #ifndef GGML_USE_OPENMP
  11215. // pause/resume must be called under mutex
  11216. static void ggml_threadpool_pause_locked(struct ggml_threadpool * threadpool) {
  11217. GGML_PRINT_DEBUG("Pausing threadpool\n");
  11218. threadpool->pause = true;
  11219. ggml_cond_broadcast(&threadpool->cond);
  11220. }
  11221. static void ggml_threadpool_resume_locked(struct ggml_threadpool * threadpool) {
  11222. GGML_PRINT_DEBUG("Resuming threadpool\n");
  11223. threadpool->pause = false;
  11224. ggml_cond_broadcast(&threadpool->cond);
  11225. }
  11226. #endif
  11227. void ggml_threadpool_pause(struct ggml_threadpool * threadpool) {
  11228. #ifndef GGML_USE_OPENMP
  11229. ggml_mutex_lock(&threadpool->mutex);
  11230. if (!threadpool->pause) {
  11231. ggml_threadpool_pause_locked(threadpool);
  11232. }
  11233. ggml_mutex_unlock(&threadpool->mutex);
  11234. #else
  11235. UNUSED(threadpool);
  11236. #endif
  11237. }
  11238. void ggml_threadpool_resume(struct ggml_threadpool * threadpool) {
  11239. #ifndef GGML_USE_OPENMP
  11240. ggml_mutex_lock(&threadpool->mutex);
  11241. if (threadpool->pause) {
  11242. ggml_threadpool_resume_locked(threadpool);
  11243. }
  11244. ggml_mutex_unlock(&threadpool->mutex);
  11245. #else
  11246. UNUSED(threadpool);
  11247. #endif
  11248. }
  11249. struct ggml_cplan ggml_graph_plan(
  11250. const struct ggml_cgraph * cgraph,
  11251. int n_threads,
  11252. struct ggml_threadpool * threadpool) {
  11253. if (threadpool == NULL) {
  11254. //GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads);
  11255. }
  11256. if (n_threads <= 0) {
  11257. n_threads = threadpool ? threadpool->n_threads_max : GGML_DEFAULT_N_THREADS;
  11258. }
  11259. size_t work_size = 0;
  11260. struct ggml_cplan cplan;
  11261. memset(&cplan, 0, sizeof(struct ggml_cplan));
  11262. int max_tasks = 1;
  11263. // thread scheduling for the different operations + work buffer size estimation
  11264. for (int i = 0; i < cgraph->n_nodes; i++) {
  11265. struct ggml_tensor * node = cgraph->nodes[i];
  11266. const int n_tasks = ggml_get_n_tasks(node, n_threads);
  11267. max_tasks = MAX(max_tasks, n_tasks);
  11268. size_t cur = 0;
  11269. if (!ggml_cpu_extra_work_size(n_threads, node, &cur)) {
  11270. switch (node->op) {
  11271. case GGML_OP_CPY:
  11272. case GGML_OP_DUP:
  11273. {
  11274. if (ggml_is_quantized(node->type) ||
  11275. // F16 -> BF16 and BF16 -> F16 copies go through intermediate F32
  11276. (node->src[0]->type == GGML_TYPE_F16 && node->src[1] && node->src[1]->type == GGML_TYPE_BF16) ||
  11277. (node->src[0]->type == GGML_TYPE_BF16 && node->src[1] && node->src[1]->type == GGML_TYPE_F16)) {
  11278. cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
  11279. }
  11280. } break;
  11281. case GGML_OP_ADD:
  11282. case GGML_OP_ADD1:
  11283. {
  11284. if (ggml_is_quantized(node->src[0]->type)) {
  11285. cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
  11286. }
  11287. } break;
  11288. case GGML_OP_ACC:
  11289. {
  11290. if (ggml_is_quantized(node->src[0]->type)) {
  11291. cur = ggml_type_size(GGML_TYPE_F32) * node->src[1]->ne[0] * n_tasks;
  11292. }
  11293. } break;
  11294. case GGML_OP_COUNT_EQUAL:
  11295. {
  11296. cur = ggml_type_size(node->type)*n_tasks;
  11297. } break;
  11298. case GGML_OP_MUL_MAT:
  11299. {
  11300. const enum ggml_type vec_dot_type = type_traits_cpu[node->src[0]->type].vec_dot_type;
  11301. if (node->src[1]->type != vec_dot_type) {
  11302. cur = ggml_row_size(vec_dot_type, ggml_nelements(node->src[1]));
  11303. }
  11304. } break;
  11305. case GGML_OP_MUL_MAT_ID:
  11306. {
  11307. cur = 0;
  11308. const struct ggml_tensor * src0 = node->src[0];
  11309. const struct ggml_tensor * src1 = node->src[1];
  11310. const enum ggml_type vec_dot_type = type_traits_cpu[src0->type].vec_dot_type;
  11311. if (src1->type != vec_dot_type) {
  11312. cur += ggml_row_size(vec_dot_type, ggml_nelements(src1));
  11313. }
  11314. const int n_as = src0->ne[2];
  11315. cur += GGML_PAD(cur, sizeof(int64_t)); // align
  11316. cur += n_as * sizeof(int64_t); // matrix_row_counts
  11317. cur += n_as * src1->ne[2] * sizeof(int64_t); // matrix_rows
  11318. } break;
  11319. case GGML_OP_OUT_PROD:
  11320. {
  11321. if (ggml_is_quantized(node->src[0]->type)) {
  11322. cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
  11323. }
  11324. } break;
  11325. case GGML_OP_SOFT_MAX:
  11326. case GGML_OP_ROPE:
  11327. case GGML_OP_ROPE_BACK:
  11328. {
  11329. cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
  11330. } break;
  11331. case GGML_OP_CONV_TRANSPOSE_1D:
  11332. {
  11333. GGML_ASSERT(node->src[0]->ne[3] == 1);
  11334. GGML_ASSERT(node->src[1]->ne[2] == 1);
  11335. GGML_ASSERT(node->src[1]->ne[3] == 1);
  11336. const int64_t ne00 = node->src[0]->ne[0]; // K
  11337. const int64_t ne01 = node->src[0]->ne[1]; // Cout
  11338. const int64_t ne02 = node->src[0]->ne[2]; // Cin
  11339. const int64_t ne10 = node->src[1]->ne[0]; // L
  11340. const int64_t ne11 = node->src[1]->ne[1]; // Cin
  11341. if ((node->src[0]->type == GGML_TYPE_F16 ||
  11342. node->src[0]->type == GGML_TYPE_BF16) &&
  11343. node->src[1]->type == GGML_TYPE_F32) {
  11344. cur += sizeof(ggml_fp16_t)*ne00*ne01*ne02;
  11345. cur += sizeof(ggml_fp16_t)*ne10*ne11;
  11346. } else if (node->src[0]->type == GGML_TYPE_F32 &&
  11347. node->src[1]->type == GGML_TYPE_F32) {
  11348. cur += sizeof(float)*ne00*ne01*ne02;
  11349. cur += sizeof(float)*ne10*ne11;
  11350. } else {
  11351. GGML_ABORT("fatal error");
  11352. }
  11353. } break;
  11354. case GGML_OP_CONV_TRANSPOSE_2D:
  11355. {
  11356. const int64_t ne00 = node->src[0]->ne[0]; // W
  11357. const int64_t ne01 = node->src[0]->ne[1]; // H
  11358. const int64_t ne02 = node->src[0]->ne[2]; // Channels Out
  11359. const int64_t ne03 = node->src[0]->ne[3]; // Channels In
  11360. const int64_t ne10 = node->src[1]->ne[0]; // W
  11361. const int64_t ne11 = node->src[1]->ne[1]; // H
  11362. const int64_t ne12 = node->src[1]->ne[2]; // Channels In
  11363. cur += sizeof(ggml_fp16_t)*ne00*ne01*ne02*ne03;
  11364. cur += sizeof(ggml_fp16_t)*ne10*ne11*ne12;
  11365. } break;
  11366. case GGML_OP_FLASH_ATTN_EXT:
  11367. {
  11368. const int64_t ne00 = node->src[0]->ne[0]; // D
  11369. cur = 3*sizeof(float)*ne00*n_tasks; // 3x head size/thread
  11370. } break;
  11371. case GGML_OP_FLASH_ATTN_BACK:
  11372. {
  11373. const int64_t D = node->src[0]->ne[0];
  11374. const int64_t ne11 = ggml_up(node->src[1]->ne[1], GGML_SOFT_MAX_UNROLL);
  11375. const int64_t mxDn = MAX(D, ne11) * 2; // *2 because of S and SM in ggml_compute_forward_flash_attn_back
  11376. if (node->src[1]->type == GGML_TYPE_F32) {
  11377. cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
  11378. cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
  11379. } else if (node->src[1]->type == GGML_TYPE_F16) {
  11380. cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
  11381. cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
  11382. } else if (node->src[1]->type == GGML_TYPE_BF16) {
  11383. cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
  11384. cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
  11385. }
  11386. } break;
  11387. case GGML_OP_CROSS_ENTROPY_LOSS:
  11388. {
  11389. cur = ggml_type_size(node->type)*(n_tasks + node->src[0]->ne[0]*n_tasks);
  11390. } break;
  11391. case GGML_OP_COUNT:
  11392. {
  11393. GGML_ABORT("fatal error");
  11394. }
  11395. default:
  11396. break;
  11397. }
  11398. }
  11399. work_size = MAX(work_size, cur);
  11400. }
  11401. if (work_size > 0) {
  11402. work_size += CACHE_LINE_SIZE*(n_threads);
  11403. }
  11404. cplan.threadpool = threadpool;
  11405. cplan.n_threads = MIN(max_tasks, n_threads);
  11406. cplan.work_size = work_size;
  11407. cplan.work_data = NULL;
  11408. return cplan;
  11409. }
  11410. static thread_ret_t ggml_graph_compute_thread(void * data) {
  11411. struct ggml_compute_state * state = (struct ggml_compute_state *) data;
  11412. struct ggml_threadpool * tp = state->threadpool;
  11413. const struct ggml_cgraph * cgraph = tp->cgraph;
  11414. const struct ggml_cplan * cplan = tp->cplan;
  11415. set_numa_thread_affinity(state->ith);
  11416. struct ggml_compute_params params = {
  11417. /*.ith =*/ state->ith,
  11418. /*.nth =*/ atomic_load_explicit(&tp->n_threads_cur, memory_order_relaxed),
  11419. /*.wsize =*/ cplan->work_size,
  11420. /*.wdata =*/ cplan->work_data,
  11421. /*.threadpool=*/ tp,
  11422. };
  11423. for (int node_n = 0; node_n < cgraph->n_nodes && !tp->abort; node_n++) {
  11424. struct ggml_tensor * node = cgraph->nodes[node_n];
  11425. ggml_compute_forward(&params, node);
  11426. if (state->ith == 0 && cplan->abort_callback &&
  11427. cplan->abort_callback(cplan->abort_callback_data)) {
  11428. tp->abort = true;
  11429. tp->ec = GGML_STATUS_ABORTED;
  11430. }
  11431. ggml_barrier(state->threadpool);
  11432. }
  11433. return 0;
  11434. }
  11435. #ifndef GGML_USE_OPENMP
  11436. // check if thread is active
  11437. static inline bool ggml_graph_compute_thread_active(struct ggml_compute_state * state) {
  11438. struct ggml_threadpool * threadpool = state->threadpool;
  11439. int n_threads = atomic_load_explicit(&threadpool->n_threads_cur, memory_order_relaxed);
  11440. return (state->ith < n_threads);
  11441. }
  11442. // check if thread is ready to proceed (exit from polling or sleeping)
  11443. static inline bool ggml_graph_compute_thread_ready(struct ggml_compute_state * state) {
  11444. struct ggml_threadpool * threadpool = state->threadpool;
  11445. if (state->pending || threadpool->stop || threadpool->pause) { return true; }
  11446. // check for new graph/work
  11447. int new_graph = atomic_load_explicit(&threadpool->n_graph, memory_order_relaxed);
  11448. if (new_graph != state->last_graph) {
  11449. state->pending = ggml_graph_compute_thread_active(state);
  11450. state->last_graph = new_graph;
  11451. }
  11452. return state->pending;
  11453. }
  11454. // sync thread state after polling
  11455. static inline void ggml_graph_compute_thread_sync(struct ggml_compute_state * state) {
  11456. // TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
  11457. #ifdef GGML_TSAN_ENABLED
  11458. atomic_fetch_add_explicit(&state->threadpool->n_graph, 0, memory_order_seq_cst);
  11459. #else
  11460. atomic_thread_fence(memory_order_seq_cst);
  11461. #endif
  11462. UNUSED(state);
  11463. }
  11464. static inline bool ggml_graph_compute_poll_for_work(struct ggml_compute_state * state) {
  11465. struct ggml_threadpool * threadpool = state->threadpool;
  11466. // Skip polling for unused threads
  11467. if (!ggml_graph_compute_thread_active(state)) {
  11468. return state->pending;
  11469. }
  11470. // This seems to make 0 ... 100 a decent range for polling level across modern processors.
  11471. // Perhaps, we can adjust it dynamically based on load and things.
  11472. const uint64_t n_rounds = 1024UL * 128 * threadpool->poll;
  11473. for (uint64_t i=0; !ggml_graph_compute_thread_ready(state) && i < n_rounds; i++) {
  11474. // No new work. Keep polling.
  11475. ggml_thread_cpu_relax();
  11476. }
  11477. return state->pending;
  11478. }
  11479. static inline bool ggml_graph_compute_check_for_work(struct ggml_compute_state * state) {
  11480. struct ggml_threadpool * threadpool = state->threadpool;
  11481. if (ggml_graph_compute_poll_for_work(state)) {
  11482. ggml_graph_compute_thread_sync(state);
  11483. return state->pending;
  11484. }
  11485. ggml_mutex_lock_shared(&threadpool->mutex);
  11486. while (!ggml_graph_compute_thread_ready(state)) {
  11487. // No new work. Wait for the signal.
  11488. GGML_PRINT_DEBUG("thread #%d waiting for work (sleeping)\n", state->ith);
  11489. ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
  11490. }
  11491. ggml_mutex_unlock_shared(&threadpool->mutex);
  11492. return state->pending;
  11493. }
  11494. static thread_ret_t ggml_graph_compute_secondary_thread(void* data) {
  11495. struct ggml_compute_state * state = (struct ggml_compute_state *) data;
  11496. struct ggml_threadpool * threadpool = state->threadpool;
  11497. ggml_thread_apply_priority(threadpool->prio);
  11498. if (ggml_thread_cpumask_is_valid(state->cpumask)) {
  11499. ggml_thread_apply_affinity(state->cpumask);
  11500. }
  11501. while (true) {
  11502. // Check if we need to sleep
  11503. while (threadpool->pause) {
  11504. GGML_PRINT_DEBUG("thread #%d inside pause loop\n", state->ith);
  11505. ggml_mutex_lock_shared(&threadpool->mutex);
  11506. if (threadpool->pause) {
  11507. ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
  11508. }
  11509. GGML_PRINT_DEBUG("thread #%d resuming after wait\n", state->ith);
  11510. ggml_mutex_unlock_shared(&threadpool->mutex);
  11511. }
  11512. // This needs to be checked for after the cond_wait
  11513. if (threadpool->stop) break;
  11514. // Check if there is new work
  11515. // The main thread is the only one that can dispatch new work
  11516. ggml_graph_compute_check_for_work(state);
  11517. if (state->pending) {
  11518. state->pending = false;
  11519. ggml_graph_compute_thread(state);
  11520. }
  11521. }
  11522. return (thread_ret_t) 0;
  11523. }
  11524. // Start processing new graph
  11525. static void ggml_graph_compute_kickoff(struct ggml_threadpool * threadpool, int n_threads)
  11526. {
  11527. // Always take the mutex here because the worker threads are doing hybrid poll/wait
  11528. ggml_mutex_lock(&threadpool->mutex);
  11529. GGML_PRINT_DEBUG("threadpool: n_threads_cur %d n_threads %d\n", threadpool->n_threads_cur, n_threads);
  11530. // Update the number of active threads
  11531. atomic_store_explicit(&threadpool->n_threads_cur, n_threads, memory_order_relaxed);
  11532. // Indicate the graph is ready to be processed
  11533. // We need the full seq-cst fence here because of the polling threads (used in thread_sync)
  11534. atomic_fetch_add_explicit(&threadpool->n_graph, 1, memory_order_seq_cst);
  11535. if (threadpool->pause) {
  11536. // Update main thread prio and affinity to match the threadpool settings
  11537. ggml_thread_apply_priority(threadpool->prio);
  11538. if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
  11539. ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
  11540. }
  11541. // resume does cond broadcast
  11542. ggml_threadpool_resume_locked(threadpool);
  11543. } else {
  11544. ggml_cond_broadcast(&threadpool->cond);
  11545. }
  11546. ggml_mutex_unlock(&threadpool->mutex);
  11547. }
  11548. #endif // GGML_USE_OPENMP
  11549. static struct ggml_threadpool * ggml_threadpool_new_impl(
  11550. struct ggml_threadpool_params * tpp,
  11551. struct ggml_cgraph * cgraph,
  11552. struct ggml_cplan * cplan) {
  11553. struct ggml_threadpool * threadpool =
  11554. ggml_aligned_malloc(sizeof(struct ggml_threadpool));
  11555. {
  11556. threadpool->cgraph = cgraph;
  11557. threadpool->cplan = cplan;
  11558. threadpool->n_graph = 0;
  11559. threadpool->n_barrier = 0;
  11560. threadpool->n_barrier_passed = 0;
  11561. threadpool->current_chunk = 0;
  11562. threadpool->stop = false;
  11563. threadpool->pause = tpp->paused;
  11564. threadpool->abort = false;
  11565. threadpool->workers = NULL;
  11566. threadpool->n_threads_max = tpp->n_threads;
  11567. threadpool->n_threads_cur = tpp->n_threads;
  11568. threadpool->poll = tpp->poll;
  11569. threadpool->prio = tpp->prio;
  11570. threadpool->ec = GGML_STATUS_SUCCESS;
  11571. }
  11572. // Allocate and init workers state
  11573. const size_t workers_size = sizeof(struct ggml_compute_state) * tpp->n_threads;
  11574. struct ggml_compute_state * workers = ggml_aligned_malloc(workers_size);
  11575. memset(workers, 0, workers_size);
  11576. for (int j = 0; j < tpp->n_threads; j++) {
  11577. workers[j].threadpool = threadpool;
  11578. workers[j].ith = j;
  11579. }
  11580. threadpool->workers = workers;
  11581. #ifndef GGML_USE_OPENMP
  11582. ggml_mutex_init(&threadpool->mutex);
  11583. ggml_cond_init(&threadpool->cond);
  11584. // Spin the threads for all workers, and update CPU placements.
  11585. // Place the main thread last (towards the higher numbered CPU cores).
  11586. int32_t cpumask_iter = 0;
  11587. for (int j = 1; j < tpp->n_threads; j++) {
  11588. ggml_thread_cpumask_next(tpp->cpumask, workers[j].cpumask, tpp->strict_cpu, &cpumask_iter);
  11589. int32_t rc = ggml_thread_create(&workers[j].thrd, NULL, ggml_graph_compute_secondary_thread, &workers[j]);
  11590. GGML_ASSERT(rc == 0);
  11591. }
  11592. ggml_thread_cpumask_next(tpp->cpumask, workers[0].cpumask, tpp->strict_cpu, &cpumask_iter);
  11593. if (!threadpool->pause) {
  11594. // Update main thread prio and affinity at the start, otherwise we'll do it in resume
  11595. ggml_thread_apply_priority(threadpool->prio);
  11596. if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
  11597. ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
  11598. }
  11599. }
  11600. #endif // GGML_USE_OPENMP
  11601. return threadpool;
  11602. }
  11603. struct ggml_threadpool * ggml_threadpool_new(struct ggml_threadpool_params * tpp) {
  11604. return ggml_threadpool_new_impl(tpp, NULL, NULL);
  11605. }
  11606. enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan) {
  11607. ggml_cpu_init();
  11608. GGML_ASSERT(cplan);
  11609. GGML_ASSERT(cplan->n_threads > 0);
  11610. GGML_ASSERT(cplan->work_size == 0 || cplan->work_data != NULL);
  11611. int n_threads = cplan->n_threads;
  11612. struct ggml_threadpool * threadpool = cplan->threadpool;
  11613. bool disposable_threadpool = false;
  11614. if (threadpool == NULL) {
  11615. //GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads);
  11616. disposable_threadpool = true;
  11617. struct ggml_threadpool_params ttp = ggml_threadpool_params_default(n_threads);
  11618. threadpool = ggml_threadpool_new_impl(&ttp, cgraph, cplan);
  11619. } else {
  11620. // Reset some of the parameters that need resetting
  11621. // No worker threads should be accessing the parameters below at this stage
  11622. threadpool->cgraph = cgraph;
  11623. threadpool->cplan = cplan;
  11624. threadpool->current_chunk = 0;
  11625. threadpool->abort = false;
  11626. threadpool->ec = GGML_STATUS_SUCCESS;
  11627. }
  11628. #ifdef GGML_USE_OPENMP
  11629. if (n_threads > 1) {
  11630. #pragma omp parallel num_threads(n_threads)
  11631. {
  11632. #pragma omp single
  11633. {
  11634. // update the number of threads from the actual number of threads that we got from OpenMP
  11635. n_threads = omp_get_num_threads();
  11636. atomic_store_explicit(&threadpool->n_threads_cur, n_threads, memory_order_relaxed);
  11637. }
  11638. ggml_graph_compute_thread(&threadpool->workers[omp_get_thread_num()]);
  11639. }
  11640. } else {
  11641. atomic_store_explicit(&threadpool->n_threads_cur, 1, memory_order_relaxed);
  11642. ggml_graph_compute_thread(&threadpool->workers[0]);
  11643. }
  11644. #else
  11645. if (n_threads > threadpool->n_threads_max) {
  11646. GGML_LOG_WARN("cplan requested more threads (%d) than available (%d)\n", n_threads, threadpool->n_threads_max);
  11647. n_threads = threadpool->n_threads_max;
  11648. }
  11649. // Kick all threads to start the new graph
  11650. ggml_graph_compute_kickoff(threadpool, n_threads);
  11651. // This is a work thread too
  11652. ggml_graph_compute_thread(&threadpool->workers[0]);
  11653. #endif
  11654. // don't leave affinity set on the main thread
  11655. clear_numa_thread_affinity();
  11656. enum ggml_status ret = threadpool->ec;
  11657. if (disposable_threadpool) {
  11658. ggml_threadpool_free(threadpool);
  11659. }
  11660. return ret;
  11661. }
  11662. enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads) {
  11663. struct ggml_cplan cplan = ggml_graph_plan(cgraph, n_threads, NULL);
  11664. cplan.work_data = (uint8_t *)ggml_new_buffer(ctx, cplan.work_size);
  11665. return ggml_graph_compute(cgraph, &cplan);
  11666. }
  11667. int ggml_cpu_has_avx(void) {
  11668. #if defined(__AVX__)
  11669. return 1;
  11670. #else
  11671. return 0;
  11672. #endif
  11673. }
  11674. int ggml_cpu_has_avx_vnni(void) {
  11675. #if defined(__AVXVNNI__)
  11676. return 1;
  11677. #else
  11678. return 0;
  11679. #endif
  11680. }
  11681. int ggml_cpu_has_avx2(void) {
  11682. #if defined(__AVX2__)
  11683. return 1;
  11684. #else
  11685. return 0;
  11686. #endif
  11687. }
  11688. int ggml_cpu_has_avx512(void) {
  11689. #if defined(__AVX512F__)
  11690. return 1;
  11691. #else
  11692. return 0;
  11693. #endif
  11694. }
  11695. int ggml_cpu_has_avx512_vbmi(void) {
  11696. #if defined(__AVX512VBMI__)
  11697. return 1;
  11698. #else
  11699. return 0;
  11700. #endif
  11701. }
  11702. int ggml_cpu_has_avx512_vnni(void) {
  11703. #if defined(__AVX512VNNI__)
  11704. return 1;
  11705. #else
  11706. return 0;
  11707. #endif
  11708. }
  11709. int ggml_cpu_has_avx512_bf16(void) {
  11710. #if defined(__AVX512BF16__)
  11711. return 1;
  11712. #else
  11713. return 0;
  11714. #endif
  11715. }
  11716. int ggml_cpu_has_amx_int8(void) {
  11717. #if defined(__AMX_INT8__)
  11718. return 1;
  11719. #else
  11720. return 0;
  11721. #endif
  11722. }
  11723. int ggml_cpu_has_fma(void) {
  11724. #if defined(__FMA__)
  11725. return 1;
  11726. #else
  11727. return 0;
  11728. #endif
  11729. }
  11730. int ggml_cpu_has_arm_fma(void) {
  11731. #if defined(__ARM_FEATURE_FMA)
  11732. return 1;
  11733. #else
  11734. return 0;
  11735. #endif
  11736. }
  11737. int ggml_cpu_has_riscv_v(void) {
  11738. #if defined(__riscv_v_intrinsic)
  11739. return 1;
  11740. #else
  11741. return 0;
  11742. #endif
  11743. }
  11744. int ggml_cpu_has_f16c(void) {
  11745. #if defined(__F16C__)
  11746. return 1;
  11747. #else
  11748. return 0;
  11749. #endif
  11750. }
  11751. int ggml_cpu_has_fp16_va(void) {
  11752. #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
  11753. return 1;
  11754. #else
  11755. return 0;
  11756. #endif
  11757. }
  11758. int ggml_cpu_has_wasm_simd(void) {
  11759. #if defined(__wasm_simd128__)
  11760. return 1;
  11761. #else
  11762. return 0;
  11763. #endif
  11764. }
  11765. int ggml_cpu_has_llamafile(void) {
  11766. #if defined(GGML_USE_LLAMAFILE)
  11767. return 1;
  11768. #else
  11769. return 0;
  11770. #endif
  11771. }
  11772. int ggml_cpu_has_sse3(void) {
  11773. #if defined(__SSE3__)
  11774. return 1;
  11775. #else
  11776. return 0;
  11777. #endif
  11778. }
  11779. int ggml_cpu_has_ssse3(void) {
  11780. #if defined(__SSSE3__)
  11781. return 1;
  11782. #else
  11783. return 0;
  11784. #endif
  11785. }
  11786. int ggml_cpu_has_vsx(void) {
  11787. #if defined(__POWER9_VECTOR__)
  11788. return 1;
  11789. #else
  11790. return 0;
  11791. #endif
  11792. }
  11793. int ggml_cpu_has_neon(void) {
  11794. #if defined(__ARM_ARCH) && defined(__ARM_NEON)
  11795. return ggml_arm_arch_features.has_neon;
  11796. #else
  11797. return 0;
  11798. #endif
  11799. }
  11800. int ggml_cpu_has_dotprod(void) {
  11801. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_DOTPROD)
  11802. return ggml_arm_arch_features.has_dotprod;
  11803. #else
  11804. return 0;
  11805. #endif
  11806. }
  11807. int ggml_cpu_has_sve(void) {
  11808. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SVE)
  11809. return ggml_arm_arch_features.has_sve;
  11810. #else
  11811. return 0;
  11812. #endif
  11813. }
  11814. int ggml_cpu_has_matmul_int8(void) {
  11815. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_MATMUL_INT8)
  11816. return ggml_arm_arch_features.has_i8mm;
  11817. #else
  11818. return 0;
  11819. #endif
  11820. }
  11821. int ggml_cpu_get_sve_cnt(void) {
  11822. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SVE)
  11823. return ggml_arm_arch_features.sve_cnt;
  11824. #else
  11825. return 0;
  11826. #endif
  11827. }
  11828. void ggml_cpu_init(void) {
  11829. // needed to initialize f16 tables
  11830. {
  11831. struct ggml_init_params params = { 0, NULL, false };
  11832. struct ggml_context * ctx = ggml_init(params);
  11833. ggml_free(ctx);
  11834. }
  11835. ggml_critical_section_start();
  11836. static bool is_first_call = true;
  11837. if (is_first_call) {
  11838. // initialize GELU, Quick GELU, SILU and EXP F32 tables
  11839. {
  11840. const uint64_t t_start = ggml_time_us(); UNUSED(t_start);
  11841. for (int i = 0; i < (1 << 16); ++i) {
  11842. union {
  11843. uint16_t u16;
  11844. ggml_fp16_t fp16;
  11845. } u = {i};
  11846. float f = GGML_FP16_TO_FP32(u.fp16);
  11847. ggml_table_gelu_f16[i] = GGML_FP32_TO_FP16(ggml_gelu_f32(f));
  11848. ggml_table_gelu_quick_f16[i] = GGML_FP32_TO_FP16(ggml_gelu_quick_f32(f));
  11849. }
  11850. const uint64_t t_end = ggml_time_us(); UNUSED(t_end);
  11851. GGML_PRINT_DEBUG("%s: GELU, Quick GELU, SILU and EXP tables initialized in %f ms\n", __func__, (t_end - t_start)/1000.0);
  11852. }
  11853. #if defined(__ARM_ARCH)
  11854. ggml_init_arm_arch_features();
  11855. #endif
  11856. is_first_call = false;
  11857. }
  11858. ggml_critical_section_end();
  11859. }