ggml-cpu.c 479 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390
  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 LASX, 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. __m256i a;
  946. memcpy(&a, x, sizeof(ggml_fp16_t) * 8);
  947. a = __lasx_xvpermi_d(a, 0 | (1 << 4));
  948. return __lasx_xvfcvtl_s_h(a);
  949. }
  950. static inline void __lasx_f32cx8_store(ggml_fp16_t * x, __m256 y) {
  951. __m256i a = __lasx_xvfcvt_h_s(y, y);
  952. a = __lasx_xvpermi_d(a, 0 | (2 << 2));
  953. memcpy(x, &a, sizeof(ggml_fp16_t) * 8);
  954. }
  955. #define GGML_F32Cx8_LOAD(x) __lasx_f32cx8_load(x)
  956. #define GGML_F32Cx8_STORE(x, y) __lasx_f32cx8_store(x, y)
  957. #define GGML_F32Cx8_FMA GGML_F32x8_FMA
  958. #define GGML_F32Cx8_ADD __lasx_xvfadd_s
  959. #define GGML_F32Cx8_MUL __lasx_xvfmul_s
  960. #define GGML_F32Cx8_REDUCE GGML_F32x8_REDUCE
  961. #define GGML_F16_VEC GGML_F32Cx8
  962. #define GGML_F16_VEC_ZERO GGML_F32Cx8_ZERO
  963. #define GGML_F16_VEC_SET1 GGML_F32Cx8_SET1
  964. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx8_LOAD(p)
  965. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx8_STORE(p, r[i])
  966. #define GGML_F16_VEC_FMA GGML_F32Cx8_FMA
  967. #define GGML_F16_VEC_ADD GGML_F32Cx8_ADD
  968. #define GGML_F16_VEC_MUL GGML_F32Cx8_MUL
  969. #define GGML_F16_VEC_REDUCE GGML_F32Cx8_REDUCE
  970. #elif defined(__loongarch_sx)
  971. #define GGML_SIMD
  972. // F32 LSX
  973. #define GGML_F32_STEP 32
  974. #define GGML_F32_EPR 4
  975. #define GGML_F32x4 __m128
  976. #define GGML_F32x4_ZERO __lsx_vldi(0)
  977. #define GGML_F32x4_SET1(x) __lsx_vinsgr2vr_w(__lsx_vldi(0),(x), 0)
  978. #define GGML_F32x4_LOAD(x) __lsx_vld((x), 0)
  979. #define GGML_F32x4_STORE((x),(y)) __lsx_vst((y), (x), 0)
  980. #define GGML_F32x4_FMA(a, b, c) __lsx_vfmadd_s(b, c, a)
  981. #define GGML_F32x4_ADD __lsx_vfadd_s
  982. #define GGML_F32x4_MUL __lsx_vfmul_s
  983. #define GGML_F32x4_REDUCE(res, x) \
  984. { \
  985. int offset = GGML_F32_ARR >> 1; \
  986. for (int i = 0; i < offset; ++i) { \
  987. x[i] = __lsx_vfadd_s(x[i], x[offset + i]); \
  988. } \
  989. offset >>= 1; \
  990. for (int i = 0; i < offset; ++i) { \
  991. x[i] = __lsx_vfadd_s(x[i], x[offset + i]); \
  992. } \
  993. offset >>= 1; \
  994. for (int i = 0; i < offset; ++i) { \
  995. x[i] = __lsx_vfadd_s(x[i], x[offset + i]); \
  996. } \
  997. __m128i tmp = __lsx_vsrli_d((__m128i) x[0], 32); \
  998. tmp = (__m128i) __lsx_vfadd_s((__m128) tmp, x[0]); \
  999. tmp = __lsx_vpickev_w(__lsx_vldi(0), tmp); \
  1000. const __m128 t0 = __lsx_vshuf4i_w(tmp, 0x88); \
  1001. tmp = __lsx_vsrli_d((__m128i) t0, 32); \
  1002. tmp = (__m128i) __lsx_vfadd_s((__m128) tmp, t0); \
  1003. tmp = __lsx_vpickev_w(__lsx_vldi(0), tmp); \
  1004. res = (ggml_float) __lsx_vpickve2gr_w(__lsx_vshuf4i_w(tmp, 0x88), 0); \
  1005. }
  1006. #define GGML_F32_VEC GGML_F32x4
  1007. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  1008. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  1009. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  1010. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  1011. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  1012. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  1013. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  1014. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  1015. // F16 LSX
  1016. #define GGML_F16_STEP 32
  1017. #define GGML_F16_EPR 4
  1018. static inline __m128 __lsx_f16x4_load(const ggml_fp16_t * x) {
  1019. float tmp[4];
  1020. tmp[0] = GGML_FP16_TO_FP32(x[0]);
  1021. tmp[1] = GGML_FP16_TO_FP32(x[1]);
  1022. tmp[2] = GGML_FP16_TO_FP32(x[2]);
  1023. tmp[3] = GGML_FP16_TO_FP32(x[3]);
  1024. return __lsx_vld(tmp, 0);
  1025. }
  1026. static inline void __lsx_f16x4_store(ggml_fp16_t * x, __m128 y) {
  1027. float arr[4];
  1028. __lsx_vst(y, arr, 0);
  1029. x[0] = GGML_FP32_TO_FP16(arr[0]);
  1030. x[1] = GGML_FP32_TO_FP16(arr[1]);
  1031. x[2] = GGML_FP32_TO_FP16(arr[2]);
  1032. x[3] = GGML_FP32_TO_FP16(arr[3]);
  1033. }
  1034. #define GGML_F32Cx4 __m128
  1035. #define GGML_F32Cx4_ZERO __lsx_vldi(0)
  1036. #define GGML_F32Cx4_SET1(x) __lsx_vinsgr2vr_w(__lsx_vldi(0),(x), 0)
  1037. #define GGML_F32Cx4_LOAD(x) __lsx_f16x4_load(x)
  1038. #define GGML_F32Cx4_STORE(x, y) __lsx_f16x4_store(x, y)
  1039. #define GGML_F32Cx4_FMA GGML_F32x4_FMA
  1040. #define GGML_F32Cx4_ADD __lsx_vfadd_s
  1041. #define GGML_F32Cx4_MUL __lsx_vfmul_s
  1042. #define GGML_F32Cx4_REDUCE GGML_F32x4_REDUCE
  1043. #define GGML_F16_VEC GGML_F32Cx4
  1044. #define GGML_F16_VEC_ZERO GGML_F32Cx4_ZERO
  1045. #define GGML_F16_VEC_SET1 GGML_F32Cx4_SET1
  1046. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx4_LOAD(p)
  1047. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx4_STORE(p, r[i])
  1048. #define GGML_F16_VEC_FMA GGML_F32Cx4_FMA
  1049. #define GGML_F16_VEC_ADD GGML_F32Cx4_ADD
  1050. #define GGML_F16_VEC_MUL GGML_F32Cx4_MUL
  1051. #define GGML_F16_VEC_REDUCE GGML_F32Cx4_REDUCE
  1052. #endif
  1053. // GGML_F32_ARR / GGML_F16_ARR
  1054. // number of registers to use per step
  1055. #ifdef GGML_SIMD
  1056. #define GGML_F32_ARR (GGML_F32_STEP/GGML_F32_EPR)
  1057. #define GGML_F16_ARR (GGML_F16_STEP/GGML_F16_EPR)
  1058. #endif
  1059. //
  1060. // Threading defs
  1061. //
  1062. typedef pthread_t ggml_thread_t;
  1063. #if defined(_WIN32)
  1064. typedef CONDITION_VARIABLE ggml_cond_t;
  1065. typedef SRWLOCK ggml_mutex_t;
  1066. #define ggml_mutex_init(m) InitializeSRWLock(m)
  1067. #define ggml_mutex_destroy(m)
  1068. #define ggml_mutex_lock(m) AcquireSRWLockExclusive(m)
  1069. #define ggml_mutex_unlock(m) ReleaseSRWLockExclusive(m)
  1070. #define ggml_mutex_lock_shared(m) AcquireSRWLockShared(m)
  1071. #define ggml_mutex_unlock_shared(m) ReleaseSRWLockShared(m)
  1072. #define ggml_cond_init(c) InitializeConditionVariable(c)
  1073. #define ggml_cond_destroy(c)
  1074. #define ggml_cond_wait(c, m) SleepConditionVariableSRW(c, m, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED)
  1075. #define ggml_cond_broadcast(c) WakeAllConditionVariable(c)
  1076. #define ggml_thread_create pthread_create
  1077. #define ggml_thread_join pthread_join
  1078. #else
  1079. typedef pthread_cond_t ggml_cond_t;
  1080. typedef pthread_mutex_t ggml_mutex_t;
  1081. #define ggml_mutex_init(m) pthread_mutex_init(m, NULL)
  1082. #define ggml_mutex_destroy(m) pthread_mutex_destroy(m)
  1083. #define ggml_mutex_lock(m) pthread_mutex_lock(m)
  1084. #define ggml_mutex_unlock(m) pthread_mutex_unlock(m)
  1085. #define ggml_mutex_lock_shared(m) pthread_mutex_lock(m)
  1086. #define ggml_mutex_unlock_shared(m) pthread_mutex_unlock(m)
  1087. #define ggml_lock_init(x) UNUSED(x)
  1088. #define ggml_lock_destroy(x) UNUSED(x)
  1089. #if defined(__x86_64__) || (defined(_MSC_VER) && defined(_M_AMD64))
  1090. #define ggml_lock_lock(x) _mm_pause()
  1091. #else
  1092. #define ggml_lock_lock(x) UNUSED(x)
  1093. #endif
  1094. #define ggml_lock_unlock(x) UNUSED(x)
  1095. #define GGML_LOCK_INITIALIZER 0
  1096. #define ggml_cond_init(c) pthread_cond_init(c, NULL)
  1097. #define ggml_cond_destroy(c) pthread_cond_destroy(c)
  1098. #define ggml_cond_wait(c, m) pthread_cond_wait(c, m)
  1099. #define ggml_cond_broadcast(c) pthread_cond_broadcast(c)
  1100. #define ggml_thread_create pthread_create
  1101. #define ggml_thread_join pthread_join
  1102. #endif
  1103. // Threadpool def
  1104. struct ggml_threadpool {
  1105. ggml_mutex_t mutex; // mutex for cond.var
  1106. ggml_cond_t cond; // cond.var for waiting for new work
  1107. struct ggml_cgraph * cgraph;
  1108. struct ggml_cplan * cplan;
  1109. // synchronization primitives
  1110. atomic_int n_graph; // incremented when there is work to be done (i.e each graph)
  1111. atomic_int GGML_CACHE_ALIGN n_barrier;
  1112. atomic_int GGML_CACHE_ALIGN n_barrier_passed;
  1113. atomic_int current_chunk; // currently processing chunk during Mat_Mul, shared between all the threads.
  1114. // these are atomic as an annotation for thread-sanitizer
  1115. atomic_bool stop; // Used for stopping the threadpool altogether
  1116. atomic_bool pause; // Used for pausing the threadpool or individual threads
  1117. atomic_int abort; // Used for aborting processing of a graph
  1118. struct ggml_compute_state * workers; // per thread state
  1119. int n_threads_max; // number of threads in the pool
  1120. atomic_int n_threads_cur; // number of threads used in the current graph
  1121. int32_t prio; // Scheduling priority
  1122. uint32_t poll; // Polling level (0 - no polling)
  1123. enum ggml_status ec;
  1124. };
  1125. // Per-thread state
  1126. struct ggml_compute_state {
  1127. #ifndef GGML_USE_OPENMP
  1128. ggml_thread_t thrd;
  1129. bool cpumask[GGML_MAX_N_THREADS];
  1130. int last_graph;
  1131. bool pending;
  1132. #endif
  1133. struct ggml_threadpool * threadpool;
  1134. int ith;
  1135. };
  1136. //
  1137. // fundamental operations
  1138. //
  1139. 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; }
  1140. 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; }
  1141. 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; }
  1142. 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]; }
  1143. 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; }
  1144. 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; }
  1145. 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]; }
  1146. 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; }
  1147. 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]; }
  1148. 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; }
  1149. 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]; }
  1150. 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; }
  1151. 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]; }
  1152. 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]; }
  1153. 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]; }
  1154. 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]; }
  1155. 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) {
  1156. assert(nrc == 1);
  1157. UNUSED(nrc);
  1158. UNUSED(bx);
  1159. UNUSED(by);
  1160. UNUSED(bs);
  1161. #if defined(GGML_SIMD)
  1162. float sumf = 0.0f;
  1163. const int np = (n & ~(GGML_F32_STEP - 1));
  1164. GGML_F32_VEC sum[GGML_F32_ARR] = { GGML_F32_VEC_ZERO };
  1165. GGML_F32_VEC ax[GGML_F32_ARR];
  1166. GGML_F32_VEC ay[GGML_F32_ARR];
  1167. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1168. for (int j = 0; j < GGML_F32_ARR; j++) {
  1169. ax[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
  1170. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1171. sum[j] = GGML_F32_VEC_FMA(sum[j], ax[j], ay[j]);
  1172. }
  1173. }
  1174. // reduce sum0..sum3 to sum0
  1175. GGML_F32_VEC_REDUCE(sumf, sum);
  1176. // leftovers
  1177. for (int i = np; i < n; ++i) {
  1178. sumf += x[i]*y[i];
  1179. }
  1180. #else
  1181. // scalar
  1182. ggml_float sumf = 0.0;
  1183. for (int i = 0; i < n; ++i) {
  1184. sumf += (ggml_float)(x[i]*y[i]);
  1185. }
  1186. #endif
  1187. *s = sumf;
  1188. }
  1189. 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) {
  1190. assert(nrc == 1);
  1191. UNUSED(nrc);
  1192. UNUSED(bx);
  1193. UNUSED(by);
  1194. UNUSED(bs);
  1195. int i = 0;
  1196. ggml_float sumf = 0;
  1197. #if defined(__AVX512BF16__)
  1198. __m512 c1 = _mm512_setzero_ps();
  1199. __m512 c2 = _mm512_setzero_ps();
  1200. for (; i + 64 <= n; i += 64) {
  1201. c1 = _mm512_dpbf16_ps(c1, m512bh(_mm512_loadu_si512((x + i))),
  1202. m512bh(_mm512_loadu_si512((y + i))));
  1203. c2 = _mm512_dpbf16_ps(c2, m512bh(_mm512_loadu_si512((x + i + 32))),
  1204. m512bh(_mm512_loadu_si512((y + i + 32))));
  1205. }
  1206. sumf += (ggml_float)_mm512_reduce_add_ps(c1);
  1207. sumf += (ggml_float)_mm512_reduce_add_ps(c2);
  1208. #elif defined(__AVX512F__)
  1209. #define LOAD(p) _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(_mm256_loadu_si256((const __m256i *)(p))), 16))
  1210. __m512 c1 = _mm512_setzero_ps();
  1211. __m512 c2 = _mm512_setzero_ps();
  1212. for (; i + 32 <= n; i += 32) {
  1213. c1 = _mm512_add_ps(_mm512_mul_ps(LOAD(x + i), LOAD(y + i)), c1);
  1214. c2 = _mm512_add_ps(_mm512_mul_ps(LOAD(x + i + 16), LOAD(y + i + 16)), c2);
  1215. }
  1216. sumf += (ggml_float)_mm512_reduce_add_ps(c1);
  1217. sumf += (ggml_float)_mm512_reduce_add_ps(c2);
  1218. #undef LOAD
  1219. #elif defined(__AVX2__) || defined(__AVX__)
  1220. #if defined(__AVX2__)
  1221. #define LOAD(p) _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i *)(p))), 16))
  1222. #else
  1223. #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))
  1224. #endif
  1225. __m256 c1 = _mm256_setzero_ps();
  1226. __m256 c2 = _mm256_setzero_ps();
  1227. __m256 c3 = _mm256_setzero_ps();
  1228. __m256 c4 = _mm256_setzero_ps();
  1229. for (; i + 32 <= n; i += 32) {
  1230. c1 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i), LOAD(y + i)), c1);
  1231. c2 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 8), LOAD(y + i + 8)), c2);
  1232. c3 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 16), LOAD(y + i + 16)), c3);
  1233. c4 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 24), LOAD(y + i + 24)), c4);
  1234. }
  1235. __m128 g;
  1236. c1 = _mm256_add_ps(_mm256_add_ps(c1, c3),
  1237. _mm256_add_ps(c2, c4));
  1238. g = _mm_add_ps(_mm256_extractf128_ps(c1, 1),
  1239. _mm256_castps256_ps128(c1));
  1240. g = _mm_add_ps(g, _mm_movehl_ps(g, g));
  1241. g = _mm_add_ss(g, _mm_movehdup_ps(g));
  1242. sumf += (ggml_float)_mm_cvtss_f32(g);
  1243. #undef LOAD
  1244. #endif
  1245. for (; i < n; ++i) {
  1246. sumf += (ggml_float)(GGML_BF16_TO_FP32(x[i]) *
  1247. GGML_BF16_TO_FP32(y[i]));
  1248. }
  1249. *s = sumf;
  1250. }
  1251. 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) {
  1252. assert(nrc == 1);
  1253. UNUSED(nrc);
  1254. UNUSED(bx);
  1255. UNUSED(by);
  1256. UNUSED(bs);
  1257. ggml_float sumf = 0.0;
  1258. #if defined(GGML_SIMD)
  1259. const int np = (n & ~(GGML_F16_STEP - 1));
  1260. GGML_F16_VEC sum[GGML_F16_ARR] = { GGML_F16_VEC_ZERO };
  1261. GGML_F16_VEC ax[GGML_F16_ARR];
  1262. GGML_F16_VEC ay[GGML_F16_ARR];
  1263. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1264. for (int j = 0; j < GGML_F16_ARR; j++) {
  1265. ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j);
  1266. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1267. sum[j] = GGML_F16_VEC_FMA(sum[j], ax[j], ay[j]);
  1268. }
  1269. }
  1270. // reduce sum0..sum3 to sum0
  1271. GGML_F16_VEC_REDUCE(sumf, sum);
  1272. // leftovers
  1273. for (int i = np; i < n; ++i) {
  1274. sumf += (ggml_float)(GGML_FP16_TO_FP32(x[i])*GGML_FP16_TO_FP32(y[i]));
  1275. }
  1276. #else
  1277. for (int i = 0; i < n; ++i) {
  1278. sumf += (ggml_float)(GGML_FP16_TO_FP32(x[i])*GGML_FP16_TO_FP32(y[i]));
  1279. }
  1280. #endif
  1281. *s = sumf;
  1282. }
  1283. // compute GGML_VEC_DOT_UNROLL dot products at once
  1284. // xs - x row stride in bytes
  1285. 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) {
  1286. ggml_float sumf[GGML_VEC_DOT_UNROLL] = { 0.0 };
  1287. ggml_fp16_t * restrict x[GGML_VEC_DOT_UNROLL];
  1288. for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
  1289. x[i] = (ggml_fp16_t *) ((char *) xv + i*xs);
  1290. }
  1291. #if defined(GGML_SIMD)
  1292. const int np = (n & ~(GGML_F16_STEP - 1));
  1293. GGML_F16_VEC sum[GGML_VEC_DOT_UNROLL][GGML_F16_ARR] = { { GGML_F16_VEC_ZERO } };
  1294. GGML_F16_VEC ax[GGML_F16_ARR];
  1295. GGML_F16_VEC ay[GGML_F16_ARR];
  1296. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1297. for (int j = 0; j < GGML_F16_ARR; j++) {
  1298. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1299. for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
  1300. ax[j] = GGML_F16_VEC_LOAD(x[k] + i + j*GGML_F16_EPR, j);
  1301. sum[k][j] = GGML_F16_VEC_FMA(sum[k][j], ax[j], ay[j]);
  1302. }
  1303. }
  1304. }
  1305. // reduce sum0..sum3 to sum0
  1306. for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
  1307. GGML_F16_VEC_REDUCE(sumf[k], sum[k]);
  1308. }
  1309. // leftovers
  1310. for (int i = np; i < n; ++i) {
  1311. for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
  1312. sumf[j] += (ggml_float)(GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]));
  1313. }
  1314. }
  1315. #else
  1316. for (int i = 0; i < n; ++i) {
  1317. for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
  1318. sumf[j] += (ggml_float)(GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]));
  1319. }
  1320. }
  1321. #endif
  1322. for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
  1323. s[i] = sumf[i];
  1324. }
  1325. }
  1326. inline static void ggml_vec_mad_f32(const int n, float * restrict y, const float * restrict x, const float v) {
  1327. #if defined(GGML_SIMD)
  1328. const int np = (n & ~(GGML_F32_STEP - 1));
  1329. GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
  1330. GGML_F32_VEC ax[GGML_F32_ARR];
  1331. GGML_F32_VEC ay[GGML_F32_ARR];
  1332. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1333. for (int j = 0; j < GGML_F32_ARR; j++) {
  1334. ax[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
  1335. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1336. ay[j] = GGML_F32_VEC_FMA(ay[j], ax[j], vx);
  1337. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  1338. }
  1339. }
  1340. // leftovers
  1341. for (int i = np; i < n; ++i) {
  1342. y[i] += x[i]*v;
  1343. }
  1344. #else
  1345. // scalar
  1346. for (int i = 0; i < n; ++i) {
  1347. y[i] += x[i]*v;
  1348. }
  1349. #endif
  1350. }
  1351. inline static void ggml_vec_mad_f16(const int n, ggml_fp16_t * restrict y, const ggml_fp16_t * restrict x, const float v) {
  1352. #if defined(GGML_SIMD)
  1353. const int np = (n & ~(GGML_F16_STEP - 1));
  1354. GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
  1355. GGML_F16_VEC ax[GGML_F16_ARR];
  1356. GGML_F16_VEC ay[GGML_F16_ARR];
  1357. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1358. for (int j = 0; j < GGML_F16_ARR; j++) {
  1359. ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j);
  1360. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1361. ay[j] = GGML_F16_VEC_FMA(ay[j], ax[j], vx);
  1362. GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
  1363. }
  1364. }
  1365. // leftovers
  1366. for (int i = np; i < n; ++i) {
  1367. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i]) + GGML_FP16_TO_FP32(x[i])*v);
  1368. }
  1369. #else
  1370. // scalar
  1371. for (int i = 0; i < n; ++i) {
  1372. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i]) + GGML_FP16_TO_FP32(x[i])*v);
  1373. }
  1374. #endif
  1375. }
  1376. // xs and vs are byte strides of x and v
  1377. 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) {
  1378. const float * restrict x[GGML_VEC_MAD_UNROLL];
  1379. const float * restrict v[GGML_VEC_MAD_UNROLL];
  1380. for (int i = 0; i < GGML_VEC_MAD_UNROLL; ++i) {
  1381. x[i] = (const float *) ((const char *) xv + i*xs);
  1382. v[i] = (const float *) ((const char *) vv + i*vs);
  1383. }
  1384. #if defined(GGML_SIMD)
  1385. const int np = (n & ~(GGML_F32_STEP - 1));
  1386. GGML_F32_VEC vx[GGML_VEC_MAD_UNROLL];
  1387. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1388. vx[k] = GGML_F32_VEC_SET1(v[k][0]);
  1389. }
  1390. GGML_F32_VEC ax[GGML_VEC_MAD_UNROLL][GGML_F32_ARR];
  1391. GGML_F32_VEC ay[GGML_F32_ARR];
  1392. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1393. for (int j = 0; j < GGML_F32_ARR; j++) {
  1394. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1395. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1396. ax[k][j] = GGML_F32_VEC_LOAD(x[k] + i + j*GGML_F32_EPR);
  1397. ay[j] = GGML_F32_VEC_FMA(ay[j], ax[k][j], vx[k]);
  1398. }
  1399. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  1400. }
  1401. }
  1402. // leftovers
  1403. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1404. for (int i = np; i < n; ++i) {
  1405. y[i] += x[k][i]*v[k][0];
  1406. }
  1407. }
  1408. #else
  1409. // scalar
  1410. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1411. for (int i = 0; i < n; ++i) {
  1412. y[i] += x[k][i]*v[k][0];
  1413. }
  1414. }
  1415. #endif
  1416. }
  1417. //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; }
  1418. inline static void ggml_vec_scale_f32(const int n, float * y, const float v) {
  1419. #if defined(GGML_USE_ACCELERATE)
  1420. vDSP_vsmul(y, 1, &v, y, 1, n);
  1421. #elif defined(GGML_SIMD)
  1422. const int np = (n & ~(GGML_F32_STEP - 1));
  1423. GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
  1424. GGML_F32_VEC ay[GGML_F32_ARR];
  1425. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1426. for (int j = 0; j < GGML_F32_ARR; j++) {
  1427. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1428. ay[j] = GGML_F32_VEC_MUL(ay[j], vx);
  1429. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  1430. }
  1431. }
  1432. // leftovers
  1433. for (int i = np; i < n; ++i) {
  1434. y[i] *= v;
  1435. }
  1436. #else
  1437. // scalar
  1438. for (int i = 0; i < n; ++i) {
  1439. y[i] *= v;
  1440. }
  1441. #endif
  1442. }
  1443. inline static void ggml_vec_scale_f16(const int n, ggml_fp16_t * y, const float v) {
  1444. #if defined(GGML_SIMD)
  1445. const int np = (n & ~(GGML_F16_STEP - 1));
  1446. GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
  1447. GGML_F16_VEC ay[GGML_F16_ARR];
  1448. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1449. for (int j = 0; j < GGML_F16_ARR; j++) {
  1450. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1451. ay[j] = GGML_F16_VEC_MUL(ay[j], vx);
  1452. GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
  1453. }
  1454. }
  1455. // leftovers
  1456. for (int i = np; i < n; ++i) {
  1457. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i])*v);
  1458. }
  1459. #else
  1460. // scalar
  1461. for (int i = 0; i < n; ++i) {
  1462. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i])*v);
  1463. }
  1464. #endif
  1465. }
  1466. 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); }
  1467. 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]; }
  1468. 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]); }
  1469. 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]); }
  1470. 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]); }
  1471. 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]); }
  1472. 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]); }
  1473. 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); }
  1474. 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; }
  1475. 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]); }
  1476. 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]); }
  1477. 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; }
  1478. 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); }
  1479. 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])); }
  1480. // TODO: optimize performance
  1481. 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)); }
  1482. 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)); }
  1483. 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]); }
  1484. static const float GELU_COEF_A = 0.044715f;
  1485. static const float GELU_QUICK_COEF = -1.702f;
  1486. static const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  1487. inline static float ggml_gelu_f32(float x) {
  1488. return 0.5f*x*(1.0f + tanhf(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  1489. }
  1490. inline static void ggml_vec_gelu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  1491. const uint16_t * i16 = (const uint16_t *) x;
  1492. for (int i = 0; i < n; ++i) {
  1493. y[i] = ggml_table_gelu_f16[i16[i]];
  1494. }
  1495. }
  1496. #ifdef GGML_GELU_FP16
  1497. inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
  1498. uint16_t t;
  1499. for (int i = 0; i < n; ++i) {
  1500. if (x[i] <= -10.0f) {
  1501. y[i] = 0.0f;
  1502. } else if (x[i] >= 10.0f) {
  1503. y[i] = x[i];
  1504. } else {
  1505. ggml_fp16_t fp16 = GGML_FP32_TO_FP16(x[i]);
  1506. memcpy(&t, &fp16, sizeof(uint16_t));
  1507. y[i] = GGML_FP16_TO_FP32(ggml_table_gelu_f16[t]);
  1508. }
  1509. }
  1510. }
  1511. #else
  1512. inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
  1513. for (int i = 0; i < n; ++i) {
  1514. y[i] = ggml_gelu_f32(x[i]);
  1515. }
  1516. }
  1517. #endif
  1518. inline static float ggml_gelu_quick_f32(float x) {
  1519. return x*(1.0f/(1.0f+expf(GELU_QUICK_COEF*x)));
  1520. }
  1521. //inline static void ggml_vec_gelu_quick_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  1522. // const uint16_t * i16 = (const uint16_t *) x;
  1523. // for (int i = 0; i < n; ++i) {
  1524. // y[i] = ggml_table_gelu_quick_f16[i16[i]];
  1525. // }
  1526. //}
  1527. #ifdef GGML_GELU_QUICK_FP16
  1528. inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) {
  1529. uint16_t t;
  1530. for (int i = 0; i < n; ++i) {
  1531. ggml_fp16_t fp16 = GGML_FP32_TO_FP16(x[i]);
  1532. memcpy(&t, &fp16, sizeof(uint16_t));
  1533. y[i] = GGML_FP16_TO_FP32(ggml_table_gelu_quick_f16[t]);
  1534. }
  1535. }
  1536. #else
  1537. inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) {
  1538. for (int i = 0; i < n; ++i) {
  1539. y[i] = ggml_gelu_quick_f32(x[i]);
  1540. }
  1541. }
  1542. #endif
  1543. // Sigmoid Linear Unit (SiLU) function
  1544. inline static float ggml_silu_f32(float x) {
  1545. return x/(1.0f + expf(-x));
  1546. }
  1547. #if __FINITE_MATH_ONLY__
  1548. #error "some routines in ggml.c require non-finite math arithmetics -- pass -fno-finite-math-only to the compiler to fix"
  1549. #error "ref: https://github.com/ggerganov/llama.cpp/pull/7154#issuecomment-2143844461"
  1550. #endif
  1551. #if defined(__ARM_NEON) && defined(__aarch64__)
  1552. // adapted from arm limited optimized routine
  1553. // the maximum error is 1.45358 plus 0.5 ulps
  1554. // numbers above 88.38 will flush to infinity
  1555. // numbers beneath -103.97 will flush to zero
  1556. inline static float32x4_t ggml_v_expf(float32x4_t x) {
  1557. const float32x4_t r = vdupq_n_f32(0x1.8p23f);
  1558. const float32x4_t z = vfmaq_f32(r, x, vdupq_n_f32(0x1.715476p+0f));
  1559. const float32x4_t n = vsubq_f32(z, r);
  1560. const float32x4_t b = vfmsq_f32(vfmsq_f32(x, n, vdupq_n_f32(0x1.62e4p-1f)), n,
  1561. vdupq_n_f32(0x1.7f7d1cp-20f));
  1562. const uint32x4_t e = vshlq_n_u32(vreinterpretq_u32_f32(z), 23);
  1563. const float32x4_t k = vreinterpretq_f32_u32(vaddq_u32(e, vreinterpretq_u32_f32(vdupq_n_f32(1))));
  1564. const uint32x4_t c = vcagtq_f32(n, vdupq_n_f32(126));
  1565. const float32x4_t u = vmulq_f32(b, b);
  1566. const float32x4_t j = vfmaq_f32(
  1567. vmulq_f32(vdupq_n_f32(0x1.ffffecp-1f), b),
  1568. vfmaq_f32(vfmaq_f32(vdupq_n_f32(0x1.fffdb6p-2f), vdupq_n_f32(0x1.555e66p-3f), b),
  1569. vfmaq_f32(vdupq_n_f32(0x1.573e2ep-5f), vdupq_n_f32(0x1.0e4020p-7f), b), u), u);
  1570. if (!vpaddd_u64(vreinterpretq_u64_u32(c)))
  1571. return vfmaq_f32(k, j, k);
  1572. const uint32x4_t d = vandq_u32(vclezq_f32(n), vdupq_n_u32(0x82000000));
  1573. const float32x4_t s1 = vreinterpretq_f32_u32(vaddq_u32(d, vdupq_n_u32(0x7f000000)));
  1574. const float32x4_t s2 = vreinterpretq_f32_u32(vsubq_u32(e, d));
  1575. return vbslq_f32(vcagtq_f32(n, vdupq_n_f32(192)), vmulq_f32(s1, s1),
  1576. vbslq_f32(c, vmulq_f32(vfmaq_f32(s2, s2, j), s1), vfmaq_f32(k, k, j)));
  1577. }
  1578. // computes silu x/(1+exp(-x)) in single precision vector
  1579. inline static float32x4_t ggml_v_silu(float32x4_t x) {
  1580. const float32x4_t one = vdupq_n_f32(1.0f);
  1581. const float32x4_t zero = vdupq_n_f32(0.0f);
  1582. const float32x4_t neg_x = vsubq_f32(zero, x);
  1583. const float32x4_t exp_neg_x = ggml_v_expf(neg_x);
  1584. const float32x4_t one_plus_exp_neg_x = vaddq_f32(one, exp_neg_x);
  1585. return vdivq_f32(x, one_plus_exp_neg_x);
  1586. }
  1587. #elif defined(__AVX512F__) && defined(__AVX512DQ__)
  1588. // adapted from arm limited optimized routine
  1589. // the maximum error is 1.45358 plus 0.5 ulps
  1590. // numbers above 88.38 will flush to infinity
  1591. // numbers beneath -103.97 will flush to zero
  1592. inline static __m512 ggml_v_expf(__m512 x) {
  1593. const __m512 r = _mm512_set1_ps(0x1.8p23f);
  1594. const __m512 z = _mm512_fmadd_ps(x, _mm512_set1_ps(0x1.715476p+0f), r);
  1595. const __m512 n = _mm512_sub_ps(z, r);
  1596. const __m512 b =
  1597. _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.7f7d1cp-20f),
  1598. _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.62e4p-1f), x));
  1599. const __mmask16 d =
  1600. _mm512_cmp_ps_mask(_mm512_abs_ps(n), _mm512_set1_ps(192), _CMP_GT_OQ);
  1601. const __m512 u = _mm512_mul_ps(b, b);
  1602. const __m512 j = _mm512_fmadd_ps(
  1603. _mm512_fmadd_ps(_mm512_fmadd_ps(_mm512_set1_ps(0x1.0e4020p-7f), b,
  1604. _mm512_set1_ps(0x1.573e2ep-5f)),
  1605. u,
  1606. _mm512_fmadd_ps(_mm512_set1_ps(0x1.555e66p-3f), b,
  1607. _mm512_set1_ps(0x1.fffdb6p-2f))),
  1608. u,
  1609. _mm512_fmadd_ps(_mm512_set1_ps(0x1.ffffecp-1f), b, _mm512_set1_ps(1.0F)));
  1610. const __m512 res = _mm512_scalef_ps(j, n);
  1611. if (_mm512_kortestz(d, d))
  1612. return res;
  1613. const __m512 zero = _mm512_setzero_ps();
  1614. const __m512 alt = _mm512_mask_blend_ps(
  1615. _mm512_cmp_ps_mask(n, zero, _CMP_LE_OQ), _mm512_set1_ps(INFINITY), zero);
  1616. return _mm512_mask_blend_ps(d, res, alt);
  1617. }
  1618. // computes silu x/(1+exp(-x)) in single precision vector
  1619. inline static __m512 ggml_v_silu(__m512 x) {
  1620. const __m512 one = _mm512_set1_ps(1);
  1621. const __m512 zero = _mm512_setzero_ps();
  1622. const __m512 neg_x = _mm512_sub_ps(zero, x);
  1623. const __m512 exp_neg_x = ggml_v_expf(neg_x);
  1624. const __m512 one_plus_exp_neg_x = _mm512_add_ps(one, exp_neg_x);
  1625. return _mm512_div_ps(x, one_plus_exp_neg_x);
  1626. }
  1627. #elif defined(__AVX2__) && defined(__FMA__)
  1628. // adapted from arm limited optimized routine
  1629. // the maximum error is 1.45358 plus 0.5 ulps
  1630. // numbers above 88.38 will flush to infinity
  1631. // numbers beneath -103.97 will flush to zero
  1632. inline static __m256 ggml_v_expf(__m256 x) {
  1633. const __m256 r = _mm256_set1_ps(0x1.8p23f);
  1634. const __m256 z = _mm256_fmadd_ps(x, _mm256_set1_ps(0x1.715476p+0f), r);
  1635. const __m256 n = _mm256_sub_ps(z, r);
  1636. const __m256 b = _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.7f7d1cp-20f),
  1637. _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.62e4p-1f), x));
  1638. const __m256i e = _mm256_slli_epi32(_mm256_castps_si256(z), 23);
  1639. const __m256 k = _mm256_castsi256_ps(
  1640. _mm256_add_epi32(e, _mm256_castps_si256(_mm256_set1_ps(1))));
  1641. const __m256i c = _mm256_castps_si256(
  1642. _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n),
  1643. _mm256_set1_ps(126), _CMP_GT_OQ));
  1644. const __m256 u = _mm256_mul_ps(b, b);
  1645. const __m256 j = _mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_set1_ps(0x1.0e4020p-7f), b,
  1646. _mm256_set1_ps(0x1.573e2ep-5f)), u,
  1647. _mm256_fmadd_ps(_mm256_set1_ps(0x1.555e66p-3f), b,
  1648. _mm256_set1_ps(0x1.fffdb6p-2f))),
  1649. u, _mm256_mul_ps(_mm256_set1_ps(0x1.ffffecp-1f), b));
  1650. if (!_mm256_movemask_ps(_mm256_castsi256_ps(c)))
  1651. return _mm256_fmadd_ps(j, k, k);
  1652. const __m256i g = _mm256_and_si256(
  1653. _mm256_castps_si256(_mm256_cmp_ps(n, _mm256_setzero_ps(), _CMP_LE_OQ)),
  1654. _mm256_set1_epi32(0x82000000u));
  1655. const __m256 s1 =
  1656. _mm256_castsi256_ps(_mm256_add_epi32(g, _mm256_set1_epi32(0x7f000000u)));
  1657. const __m256 s2 = _mm256_castsi256_ps(_mm256_sub_epi32(e, g));
  1658. const __m256i d = _mm256_castps_si256(
  1659. _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n),
  1660. _mm256_set1_ps(192), _CMP_GT_OQ));
  1661. return _mm256_or_ps(
  1662. _mm256_and_ps(_mm256_castsi256_ps(d), _mm256_mul_ps(s1, s1)),
  1663. _mm256_andnot_ps(
  1664. _mm256_castsi256_ps(d),
  1665. _mm256_or_ps(
  1666. _mm256_and_ps(_mm256_castsi256_ps(c),
  1667. _mm256_mul_ps(_mm256_fmadd_ps(s2, j, s2), s1)),
  1668. _mm256_andnot_ps(_mm256_castsi256_ps(c), _mm256_fmadd_ps(k, j, k)))));
  1669. }
  1670. // computes silu x/(1+exp(-x)) in single precision vector
  1671. inline static __m256 ggml_v_silu(__m256 x) {
  1672. const __m256 one = _mm256_set1_ps(1);
  1673. const __m256 zero = _mm256_setzero_ps();
  1674. const __m256 neg_x = _mm256_sub_ps(zero, x);
  1675. const __m256 exp_neg_x = ggml_v_expf(neg_x);
  1676. const __m256 one_plus_exp_neg_x = _mm256_add_ps(one, exp_neg_x);
  1677. return _mm256_div_ps(x, one_plus_exp_neg_x);
  1678. }
  1679. #elif defined(__SSE2__) // __AVX2__ / __ARM_NEON
  1680. #if defined(__FMA__)
  1681. #define MADD128(x, y, z) _mm_fmadd_ps(x, y, z)
  1682. #define NMADD128(x, y, z) _mm_fnmadd_ps(x, y, z)
  1683. #else
  1684. #define MADD128(x, y, z) _mm_add_ps(_mm_mul_ps(x, y), z)
  1685. #define NMADD128(x, y, z) _mm_sub_ps(z, _mm_mul_ps(x, y))
  1686. #endif
  1687. // adapted from arm limited optimized routine
  1688. // the maximum error is 1.45358 plus 0.5 ulps
  1689. // numbers above 88.38 will flush to infinity
  1690. // numbers beneath -103.97 will flush to zero
  1691. inline static __m128 ggml_v_expf(__m128 x) {
  1692. const __m128 r = _mm_set1_ps(0x1.8p23f);
  1693. const __m128 z = MADD128(x, _mm_set1_ps(0x1.715476p+0f), r);
  1694. const __m128 n = _mm_sub_ps(z, r);
  1695. const __m128 b =
  1696. NMADD128(n, _mm_set1_ps(0x1.7f7d1cp-20f), NMADD128(n, _mm_set1_ps(0x1.62e4p-1f), x));
  1697. const __m128i e = _mm_slli_epi32(_mm_castps_si128(z), 23);
  1698. const __m128 k = _mm_castsi128_ps(_mm_add_epi32(e, _mm_castps_si128(_mm_set1_ps(1))));
  1699. const __m128i c =
  1700. _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(126)));
  1701. const __m128 u = _mm_mul_ps(b, b);
  1702. const __m128 j =
  1703. MADD128(MADD128(MADD128(_mm_set1_ps(0x1.0e4020p-7f), b, _mm_set1_ps(0x1.573e2ep-5f)), u,
  1704. MADD128(_mm_set1_ps(0x1.555e66p-3f), b, _mm_set1_ps(0x1.fffdb6p-2f))),
  1705. u, _mm_mul_ps(_mm_set1_ps(0x1.ffffecp-1f), b));
  1706. if (!_mm_movemask_epi8(c))
  1707. return MADD128(j, k, k);
  1708. const __m128i g = _mm_and_si128(_mm_castps_si128(_mm_cmple_ps(n, _mm_setzero_ps())),
  1709. _mm_set1_epi32(0x82000000u));
  1710. const __m128 s1 = _mm_castsi128_ps(_mm_add_epi32(g, _mm_set1_epi32(0x7f000000u)));
  1711. const __m128 s2 = _mm_castsi128_ps(_mm_sub_epi32(e, g));
  1712. const __m128i d =
  1713. _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(192)));
  1714. return _mm_or_ps(
  1715. _mm_and_ps(_mm_castsi128_ps(d), _mm_mul_ps(s1, s1)),
  1716. _mm_andnot_ps(_mm_castsi128_ps(d),
  1717. _mm_or_ps(_mm_and_ps(_mm_castsi128_ps(c), _mm_mul_ps(MADD128(s2, j, s2), s1)),
  1718. _mm_andnot_ps(_mm_castsi128_ps(c), MADD128(k, j, k)))));
  1719. }
  1720. // computes silu x/(1+exp(-x)) in single precision vector
  1721. inline static __m128 ggml_v_silu(__m128 x) {
  1722. const __m128 one = _mm_set1_ps(1);
  1723. const __m128 zero = _mm_setzero_ps();
  1724. const __m128 neg_x = _mm_sub_ps(zero, x);
  1725. const __m128 exp_neg_x = ggml_v_expf(neg_x);
  1726. const __m128 one_plus_exp_neg_x = _mm_add_ps(one, exp_neg_x);
  1727. return _mm_div_ps(x, one_plus_exp_neg_x);
  1728. }
  1729. #endif // __ARM_NEON / __AVX2__ / __SSE2__
  1730. static void ggml_vec_silu_f32(const int n, float * y, const float * x) {
  1731. int i = 0;
  1732. #if defined(__AVX512F__) && defined(__AVX512DQ__)
  1733. for (; i + 15 < n; i += 16) {
  1734. _mm512_storeu_ps(y + i, ggml_v_silu(_mm512_loadu_ps(x + i)));
  1735. }
  1736. #elif defined(__AVX2__) && defined(__FMA__)
  1737. for (; i + 7 < n; i += 8) {
  1738. _mm256_storeu_ps(y + i, ggml_v_silu(_mm256_loadu_ps(x + i)));
  1739. }
  1740. #elif defined(__SSE2__)
  1741. for (; i + 3 < n; i += 4) {
  1742. _mm_storeu_ps(y + i, ggml_v_silu(_mm_loadu_ps(x + i)));
  1743. }
  1744. #elif defined(__ARM_NEON) && defined(__aarch64__)
  1745. for (; i + 3 < n; i += 4) {
  1746. vst1q_f32(y + i, ggml_v_silu(vld1q_f32(x + i)));
  1747. }
  1748. #endif
  1749. for (; i < n; ++i) {
  1750. y[i] = ggml_silu_f32(x[i]);
  1751. }
  1752. }
  1753. static ggml_float ggml_vec_soft_max_f32(const int n, float * y, const float * x, float max) {
  1754. int i = 0;
  1755. ggml_float sum = 0;
  1756. #if defined(__AVX512F__) && defined(__AVX512DQ__)
  1757. for (; i + 15 < n; i += 16) {
  1758. __m512 val = ggml_v_expf(_mm512_sub_ps(_mm512_loadu_ps(x + i),
  1759. _mm512_set1_ps(max)));
  1760. _mm512_storeu_ps(y + i, val);
  1761. sum += (ggml_float)_mm512_reduce_add_ps(val);
  1762. }
  1763. #elif defined(__AVX2__) && defined(__FMA__)
  1764. for (; i + 7 < n; i += 8) {
  1765. __m256 val = ggml_v_expf(_mm256_sub_ps(_mm256_loadu_ps(x + i),
  1766. _mm256_set1_ps(max)));
  1767. _mm256_storeu_ps(y + i, val);
  1768. __m128 val2 = _mm_add_ps(_mm256_extractf128_ps(val, 1),
  1769. _mm256_castps256_ps128(val));
  1770. val2 = _mm_add_ps(val2, _mm_movehl_ps(val2, val2));
  1771. val2 = _mm_add_ss(val2, _mm_movehdup_ps(val2));
  1772. sum += (ggml_float)_mm_cvtss_f32(val2);
  1773. }
  1774. #elif defined(__SSE2__)
  1775. for (; i + 3 < n; i += 4) {
  1776. __m128 val = ggml_v_expf(_mm_sub_ps(_mm_loadu_ps(x + i),
  1777. _mm_set1_ps(max)));
  1778. _mm_storeu_ps(y + i, val);
  1779. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__)
  1780. val = _mm_add_ps(val, _mm_movehl_ps(val, val));
  1781. val = _mm_add_ss(val, _mm_movehdup_ps(val));
  1782. #else
  1783. __m128 tmp = _mm_shuffle_ps(val, val, _MM_SHUFFLE(2, 3, 0, 1));
  1784. val = _mm_add_ps(val, tmp);
  1785. tmp = _mm_movehl_ps(tmp, val);
  1786. val = _mm_add_ss(val, tmp);
  1787. #endif
  1788. sum += (ggml_float)_mm_cvtss_f32(val);
  1789. }
  1790. #elif defined(__ARM_NEON) && defined(__aarch64__)
  1791. for (; i + 3 < n; i += 4) {
  1792. float32x4_t val = ggml_v_expf(vsubq_f32(vld1q_f32(x + i),
  1793. vdupq_n_f32(max)));
  1794. vst1q_f32(y + i, val);
  1795. sum += (ggml_float)vaddvq_f32(val);
  1796. }
  1797. #endif
  1798. for (; i < n; ++i) {
  1799. float val = expf(x[i] - max);
  1800. sum += (ggml_float)val;
  1801. y[i] = val;
  1802. }
  1803. return sum;
  1804. }
  1805. static ggml_float ggml_vec_log_soft_max_f32(const int n, float * y, const float * x, float max) {
  1806. // 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)
  1807. int i = 0;
  1808. ggml_float sum = 0;
  1809. for (; i < n; ++i) {
  1810. float val = x[i] - max;
  1811. y[i] = val;
  1812. sum += (ggml_float)expf(val);
  1813. }
  1814. return sum = (ggml_float)logf(sum);
  1815. }
  1816. inline static float ggml_silu_backward_f32(float x, float dy) {
  1817. const float s = 1.0f/(1.0f + expf(-x));
  1818. return dy*s*(1.0f + x*(1.0f - s));
  1819. }
  1820. inline static void ggml_vec_silu_backward_f32(const int n, float * dx, const float * x, const float * dy) {
  1821. for (int i = 0; i < n; ++i) {
  1822. dx[i] = ggml_silu_backward_f32(x[i], dy[i]);
  1823. }
  1824. }
  1825. inline static void ggml_vec_sum_f32(const int n, float * s, const float * x) {
  1826. #ifndef GGML_USE_ACCELERATE
  1827. ggml_float sum = 0.0;
  1828. for (int i = 0; i < n; ++i) {
  1829. sum += (ggml_float)x[i];
  1830. }
  1831. *s = sum;
  1832. #else
  1833. vDSP_sve(x, 1, s, n);
  1834. #endif
  1835. }
  1836. inline static void ggml_vec_sum_f32_ggf(const int n, ggml_float * s, const float * x) {
  1837. ggml_float sum = 0.0;
  1838. for (int i = 0; i < n; ++i) {
  1839. sum += (ggml_float)x[i];
  1840. }
  1841. *s = sum;
  1842. }
  1843. inline static void ggml_vec_sum_f16_ggf(const int n, float * s, const ggml_fp16_t * x) {
  1844. float sum = 0.0f;
  1845. for (int i = 0; i < n; ++i) {
  1846. sum += GGML_FP16_TO_FP32(x[i]);
  1847. }
  1848. *s = sum;
  1849. }
  1850. inline static void ggml_vec_sum_bf16_ggf(const int n, float * s, const ggml_bf16_t * x) {
  1851. float sum = 0.0f;
  1852. for (int i = 0; i < n; ++i) {
  1853. sum += GGML_BF16_TO_FP32(x[i]);
  1854. }
  1855. *s = sum;
  1856. }
  1857. inline static void ggml_vec_max_f32(const int n, float * s, const float * x) {
  1858. #ifndef GGML_USE_ACCELERATE
  1859. float max = -INFINITY;
  1860. for (int i = 0; i < n; ++i) {
  1861. max = MAX(max, x[i]);
  1862. }
  1863. *s = max;
  1864. #else
  1865. vDSP_maxv(x, 1, s, n);
  1866. #endif
  1867. }
  1868. inline static void ggml_vec_norm_inv_f32(const int n, float * s, const float * x) {
  1869. ggml_vec_norm_f32(n, s, x);
  1870. *s = 1.f/(*s);
  1871. }
  1872. inline static void ggml_vec_argmax_f32(const int n, int * s, const float * x) {
  1873. float max = -INFINITY;
  1874. int idx = 0;
  1875. for (int i = 0; i < n; ++i) {
  1876. max = MAX(max, x[i]);
  1877. if (max == x[i]) { idx = i; }
  1878. }
  1879. *s = idx;
  1880. }
  1881. // Helpers for polling loops
  1882. #if defined(__aarch64__) && ( defined(__clang__) || defined(__GNUC__) )
  1883. static inline void ggml_thread_cpu_relax(void) {
  1884. __asm__ volatile("yield" ::: "memory");
  1885. }
  1886. #elif defined(__x86_64__)
  1887. static inline void ggml_thread_cpu_relax(void) {
  1888. _mm_pause();
  1889. }
  1890. #else
  1891. static inline void ggml_thread_cpu_relax(void) {;}
  1892. #endif
  1893. //
  1894. // NUMA support
  1895. //
  1896. #define GGML_NUMA_MAX_NODES 8
  1897. #define GGML_NUMA_MAX_CPUS 512
  1898. struct ggml_numa_node {
  1899. uint32_t cpus[GGML_NUMA_MAX_CPUS]; // hardware threads on this node
  1900. uint32_t n_cpus;
  1901. };
  1902. struct ggml_numa_nodes {
  1903. enum ggml_numa_strategy numa_strategy;
  1904. struct ggml_numa_node nodes[GGML_NUMA_MAX_NODES];
  1905. uint32_t n_nodes;
  1906. uint32_t total_cpus; // hardware threads on system
  1907. uint32_t current_node; // node on which main process is execting
  1908. #if defined(__gnu_linux__)
  1909. cpu_set_t cpuset; // cpuset from numactl
  1910. #else
  1911. uint32_t cpuset; // no NUMA support outside of Linux at this time. Use a portable datatype
  1912. #endif
  1913. };
  1914. //
  1915. // ggml state
  1916. //
  1917. struct ggml_state {
  1918. struct ggml_numa_nodes numa;
  1919. };
  1920. static struct ggml_state g_state = {0};
  1921. void ggml_barrier(struct ggml_threadpool * tp) {
  1922. int n_threads = atomic_load_explicit(&tp->n_threads_cur, memory_order_relaxed);
  1923. if (n_threads == 1) {
  1924. return;
  1925. }
  1926. #ifdef GGML_USE_OPENMP
  1927. #pragma omp barrier
  1928. #else
  1929. int n_passed = atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed);
  1930. // enter barrier (full seq-cst fence)
  1931. int n_barrier = atomic_fetch_add_explicit(&tp->n_barrier, 1, memory_order_seq_cst);
  1932. if (n_barrier == (n_threads - 1)) {
  1933. // last thread
  1934. atomic_store_explicit(&tp->n_barrier, 0, memory_order_relaxed);
  1935. // exit barrier (fill seq-cst fence)
  1936. atomic_fetch_add_explicit(&tp->n_barrier_passed, 1, memory_order_seq_cst);
  1937. return;
  1938. }
  1939. // wait for other threads
  1940. while (atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed) == n_passed) {
  1941. ggml_thread_cpu_relax();
  1942. }
  1943. // exit barrier (full seq-cst fence)
  1944. // TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
  1945. #ifdef GGML_TSAN_ENABLED
  1946. atomic_fetch_add_explicit(&tp->n_barrier_passed, 0, memory_order_seq_cst);
  1947. #else
  1948. atomic_thread_fence(memory_order_seq_cst);
  1949. #endif
  1950. #endif
  1951. }
  1952. #if defined(__gnu_linux__)
  1953. static cpu_set_t ggml_get_numa_affinity(void) {
  1954. cpu_set_t cpuset;
  1955. pthread_t thread;
  1956. thread = pthread_self();
  1957. CPU_ZERO(&cpuset);
  1958. pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
  1959. return cpuset;
  1960. }
  1961. #else
  1962. static uint32_t ggml_get_numa_affinity(void) {
  1963. return 0; // no NUMA support
  1964. }
  1965. #endif
  1966. void ggml_numa_init(enum ggml_numa_strategy numa_flag) {
  1967. if (g_state.numa.n_nodes > 0) {
  1968. fprintf(stderr, "ggml_numa_init: NUMA already initialized\n");
  1969. return;
  1970. }
  1971. #if defined(__gnu_linux__)
  1972. struct stat st;
  1973. char path[256];
  1974. int rv;
  1975. // set numa scheme
  1976. g_state.numa.numa_strategy = numa_flag;
  1977. GGML_PRINT_DEBUG("numa strategy %u\n",g_state.numa.numa_strategy);
  1978. g_state.numa.cpuset = ggml_get_numa_affinity();
  1979. // enumerate nodes
  1980. while (g_state.numa.n_nodes < GGML_NUMA_MAX_NODES) {
  1981. rv = snprintf(path, sizeof(path), "/sys/devices/system/node/node%u", g_state.numa.n_nodes);
  1982. GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
  1983. if (stat(path, &st) != 0) { break; }
  1984. ++g_state.numa.n_nodes;
  1985. }
  1986. // enumerate CPUs
  1987. while (g_state.numa.total_cpus < GGML_NUMA_MAX_CPUS) {
  1988. rv = snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u", g_state.numa.total_cpus);
  1989. GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
  1990. if (stat(path, &st) != 0) { break; }
  1991. ++g_state.numa.total_cpus;
  1992. }
  1993. GGML_PRINT_DEBUG("found %u numa nodes, %u CPUs\n", g_state.numa.n_nodes, g_state.numa.total_cpus);
  1994. // figure out which node we're on
  1995. uint current_cpu;
  1996. int getcpu_ret = 0;
  1997. #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 33) || defined(__COSMOPOLITAN__)
  1998. getcpu_ret = getcpu(&current_cpu, &g_state.numa.current_node);
  1999. #else
  2000. // old glibc doesn't have a wrapper for this call. Fall back on direct syscall
  2001. # if !defined(SYS_getcpu) && defined(SYS_get_cpu)
  2002. # define SYS_getcpu SYS_get_cpu // some older glibc versions use this name
  2003. # endif
  2004. getcpu_ret = syscall(SYS_getcpu, &current_cpu, &g_state.numa.current_node);
  2005. #endif
  2006. if (g_state.numa.n_nodes < 1 || g_state.numa.total_cpus < 1 || getcpu_ret != 0) {
  2007. g_state.numa.n_nodes = 0;
  2008. return;
  2009. }
  2010. GGML_PRINT_DEBUG("found our process on numa node %u, CPU %u\n", g_state.numa.current_node, current_cpu);
  2011. for (uint32_t n = 0; n < g_state.numa.n_nodes; ++n) {
  2012. struct ggml_numa_node * node = &g_state.numa.nodes[n];
  2013. GGML_PRINT_DEBUG("CPUs on node %u:", n);
  2014. node->n_cpus = 0;
  2015. for (uint32_t c = 0; c < g_state.numa.total_cpus; ++c) {
  2016. rv = snprintf(path, sizeof(path), "/sys/devices/system/node/node%u/cpu%u", n, c);
  2017. GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
  2018. if (stat(path, &st) == 0) {
  2019. node->cpus[node->n_cpus++] = c;
  2020. GGML_PRINT_DEBUG(" %u", c);
  2021. }
  2022. }
  2023. GGML_PRINT_DEBUG("\n");
  2024. }
  2025. if (ggml_is_numa()) {
  2026. FILE *fptr = fopen("/proc/sys/kernel/numa_balancing", "r");
  2027. if (fptr != NULL) {
  2028. char buf[42];
  2029. if (fgets(buf, sizeof(buf), fptr) && strncmp(buf, "0\n", sizeof(buf)) != 0) {
  2030. GGML_LOG_WARN("/proc/sys/kernel/numa_balancing is enabled, this has been observed to impair performance\n");
  2031. }
  2032. fclose(fptr);
  2033. }
  2034. }
  2035. #else
  2036. UNUSED(numa_flag);
  2037. // TODO
  2038. #endif
  2039. }
  2040. bool ggml_is_numa(void) {
  2041. return g_state.numa.n_nodes > 1;
  2042. }
  2043. #if defined(__ARM_ARCH)
  2044. #if defined(__linux__) && defined(__aarch64__)
  2045. #include <sys/auxv.h>
  2046. #elif defined(__APPLE__)
  2047. #include <sys/sysctl.h>
  2048. #endif
  2049. #if !defined(HWCAP2_I8MM)
  2050. #define HWCAP2_I8MM (1 << 13)
  2051. #endif
  2052. static void ggml_init_arm_arch_features(void) {
  2053. #if defined(__linux__) && defined(__aarch64__)
  2054. uint32_t hwcap = getauxval(AT_HWCAP);
  2055. uint32_t hwcap2 = getauxval(AT_HWCAP2);
  2056. ggml_arm_arch_features.has_neon = !!(hwcap & HWCAP_ASIMD);
  2057. ggml_arm_arch_features.has_dotprod = !!(hwcap & HWCAP_ASIMDDP);
  2058. ggml_arm_arch_features.has_i8mm = !!(hwcap2 & HWCAP2_I8MM);
  2059. ggml_arm_arch_features.has_sve = !!(hwcap & HWCAP_SVE);
  2060. #if defined(__ARM_FEATURE_SVE)
  2061. ggml_arm_arch_features.sve_cnt = PR_SVE_VL_LEN_MASK & prctl(PR_SVE_GET_VL);
  2062. #endif
  2063. #elif defined(__APPLE__)
  2064. int oldp = 0;
  2065. size_t size = sizeof(oldp);
  2066. if (sysctlbyname("hw.optional.AdvSIMD", &oldp, &size, NULL, 0) != 0) {
  2067. oldp = 0;
  2068. }
  2069. ggml_arm_arch_features.has_neon = oldp;
  2070. if (sysctlbyname("hw.optional.arm.FEAT_DotProd", &oldp, &size, NULL, 0) != 0) {
  2071. oldp = 0;
  2072. }
  2073. ggml_arm_arch_features.has_dotprod = oldp;
  2074. if (sysctlbyname("hw.optional.arm.FEAT_I8MM", &oldp, &size, NULL, 0) != 0) {
  2075. oldp = 0;
  2076. }
  2077. ggml_arm_arch_features.has_i8mm = oldp;
  2078. ggml_arm_arch_features.has_sve = 0;
  2079. ggml_arm_arch_features.sve_cnt = 0;
  2080. #else
  2081. // Run-time CPU feature detection not implemented for this platform, fallback to compile time
  2082. #if defined(__ARM_NEON)
  2083. ggml_arm_arch_features.has_neon = 1;
  2084. #else
  2085. ggml_arm_arch_features.has_neon = 0;
  2086. #endif
  2087. #if defined(__ARM_FEATURE_MATMUL_INT8)
  2088. ggml_arm_arch_features.has_i8mm = 1;
  2089. #else
  2090. ggml_arm_arch_features.has_i8mm = 0;
  2091. #endif
  2092. #if defined(__ARM_FEATURE_SVE)
  2093. ggml_arm_arch_features.has_sve = 1;
  2094. ggml_arm_arch_features.sve_cnt = 16;
  2095. #else
  2096. ggml_arm_arch_features.has_sve = 0;
  2097. ggml_arm_arch_features.sve_cnt = 0;
  2098. #endif
  2099. #endif
  2100. }
  2101. #endif
  2102. struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value) {
  2103. GGML_ASSERT(!ggml_get_no_alloc(ctx));
  2104. struct ggml_tensor * result = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 1);
  2105. ggml_set_i32(result, value);
  2106. return result;
  2107. }
  2108. struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value) {
  2109. GGML_ASSERT(!ggml_get_no_alloc(ctx));
  2110. struct ggml_tensor * result = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
  2111. ggml_set_f32(result, value);
  2112. return result;
  2113. }
  2114. struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value) {
  2115. const int n = ggml_nrows(tensor);
  2116. const int nc = tensor->ne[0];
  2117. const size_t n1 = tensor->nb[1];
  2118. char * const data = tensor->data;
  2119. switch (tensor->type) {
  2120. case GGML_TYPE_I8:
  2121. {
  2122. assert(tensor->nb[0] == sizeof(int8_t));
  2123. for (int i = 0; i < n; i++) {
  2124. ggml_vec_set_i8(nc, (int8_t *)(data + i*n1), value);
  2125. }
  2126. } break;
  2127. case GGML_TYPE_I16:
  2128. {
  2129. assert(tensor->nb[0] == sizeof(int16_t));
  2130. for (int i = 0; i < n; i++) {
  2131. ggml_vec_set_i16(nc, (int16_t *)(data + i*n1), value);
  2132. }
  2133. } break;
  2134. case GGML_TYPE_I32:
  2135. {
  2136. assert(tensor->nb[0] == sizeof(int32_t));
  2137. for (int i = 0; i < n; i++) {
  2138. ggml_vec_set_i32(nc, (int32_t *)(data + i*n1), value);
  2139. }
  2140. } break;
  2141. case GGML_TYPE_F16:
  2142. {
  2143. assert(tensor->nb[0] == sizeof(ggml_fp16_t));
  2144. for (int i = 0; i < n; i++) {
  2145. ggml_vec_set_f16(nc, (ggml_fp16_t *)(data + i*n1), GGML_FP32_TO_FP16(value));
  2146. }
  2147. } break;
  2148. case GGML_TYPE_BF16:
  2149. {
  2150. assert(tensor->nb[0] == sizeof(ggml_fp16_t));
  2151. for (int i = 0; i < n; i++) {
  2152. ggml_vec_set_bf16(nc, (ggml_bf16_t *)(data + i*n1), GGML_FP32_TO_BF16(value));
  2153. }
  2154. } break;
  2155. case GGML_TYPE_F32:
  2156. {
  2157. assert(tensor->nb[0] == sizeof(float));
  2158. for (int i = 0; i < n; i++) {
  2159. ggml_vec_set_f32(nc, (float *)(data + i*n1), value);
  2160. }
  2161. } break;
  2162. default:
  2163. {
  2164. GGML_ABORT("fatal error");
  2165. }
  2166. }
  2167. return tensor;
  2168. }
  2169. struct ggml_tensor * ggml_set_f32(struct ggml_tensor * tensor, float value) {
  2170. const int n = ggml_nrows(tensor);
  2171. const int nc = tensor->ne[0];
  2172. const size_t n1 = tensor->nb[1];
  2173. char * const data = tensor->data;
  2174. switch (tensor->type) {
  2175. case GGML_TYPE_I8:
  2176. {
  2177. assert(tensor->nb[0] == sizeof(int8_t));
  2178. for (int i = 0; i < n; i++) {
  2179. ggml_vec_set_i8(nc, (int8_t *)(data + i*n1), value);
  2180. }
  2181. } break;
  2182. case GGML_TYPE_I16:
  2183. {
  2184. assert(tensor->nb[0] == sizeof(int16_t));
  2185. for (int i = 0; i < n; i++) {
  2186. ggml_vec_set_i16(nc, (int16_t *)(data + i*n1), value);
  2187. }
  2188. } break;
  2189. case GGML_TYPE_I32:
  2190. {
  2191. assert(tensor->nb[0] == sizeof(int32_t));
  2192. for (int i = 0; i < n; i++) {
  2193. ggml_vec_set_i32(nc, (int32_t *)(data + i*n1), value);
  2194. }
  2195. } break;
  2196. case GGML_TYPE_F16:
  2197. {
  2198. assert(tensor->nb[0] == sizeof(ggml_fp16_t));
  2199. for (int i = 0; i < n; i++) {
  2200. ggml_vec_set_f16(nc, (ggml_fp16_t *)(data + i*n1), GGML_FP32_TO_FP16(value));
  2201. }
  2202. } break;
  2203. case GGML_TYPE_BF16:
  2204. {
  2205. assert(tensor->nb[0] == sizeof(ggml_bf16_t));
  2206. for (int i = 0; i < n; i++) {
  2207. ggml_vec_set_bf16(nc, (ggml_bf16_t *)(data + i*n1), GGML_FP32_TO_BF16(value));
  2208. }
  2209. } break;
  2210. case GGML_TYPE_F32:
  2211. {
  2212. assert(tensor->nb[0] == sizeof(float));
  2213. for (int i = 0; i < n; i++) {
  2214. ggml_vec_set_f32(nc, (float *)(data + i*n1), value);
  2215. }
  2216. } break;
  2217. default:
  2218. {
  2219. GGML_ABORT("fatal error");
  2220. }
  2221. }
  2222. return tensor;
  2223. }
  2224. int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i) {
  2225. if (!ggml_is_contiguous(tensor)) {
  2226. int64_t id[4] = { 0, 0, 0, 0 };
  2227. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2228. return ggml_get_i32_nd(tensor, id[0], id[1], id[2], id[3]);
  2229. }
  2230. switch (tensor->type) {
  2231. case GGML_TYPE_I8:
  2232. {
  2233. GGML_ASSERT(tensor->nb[0] == sizeof(int8_t));
  2234. return ((int8_t *)(tensor->data))[i];
  2235. }
  2236. case GGML_TYPE_I16:
  2237. {
  2238. GGML_ASSERT(tensor->nb[0] == sizeof(int16_t));
  2239. return ((int16_t *)(tensor->data))[i];
  2240. }
  2241. case GGML_TYPE_I32:
  2242. {
  2243. GGML_ASSERT(tensor->nb[0] == sizeof(int32_t));
  2244. return ((int32_t *)(tensor->data))[i];
  2245. }
  2246. case GGML_TYPE_F16:
  2247. {
  2248. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t));
  2249. return GGML_FP16_TO_FP32(((ggml_fp16_t *)(tensor->data))[i]);
  2250. }
  2251. case GGML_TYPE_BF16:
  2252. {
  2253. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_bf16_t));
  2254. return GGML_BF16_TO_FP32(((ggml_bf16_t *)(tensor->data))[i]);
  2255. }
  2256. case GGML_TYPE_F32:
  2257. {
  2258. GGML_ASSERT(tensor->nb[0] == sizeof(float));
  2259. return ((float *)(tensor->data))[i];
  2260. }
  2261. default:
  2262. {
  2263. GGML_ABORT("fatal error");
  2264. }
  2265. }
  2266. }
  2267. void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value) {
  2268. if (!ggml_is_contiguous(tensor)) {
  2269. int64_t id[4] = { 0, 0, 0, 0 };
  2270. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2271. ggml_set_i32_nd(tensor, id[0], id[1], id[2], id[3], value);
  2272. return;
  2273. }
  2274. switch (tensor->type) {
  2275. case GGML_TYPE_I8:
  2276. {
  2277. GGML_ASSERT(tensor->nb[0] == sizeof(int8_t));
  2278. ((int8_t *)(tensor->data))[i] = value;
  2279. } break;
  2280. case GGML_TYPE_I16:
  2281. {
  2282. GGML_ASSERT(tensor->nb[0] == sizeof(int16_t));
  2283. ((int16_t *)(tensor->data))[i] = value;
  2284. } break;
  2285. case GGML_TYPE_I32:
  2286. {
  2287. GGML_ASSERT(tensor->nb[0] == sizeof(int32_t));
  2288. ((int32_t *)(tensor->data))[i] = value;
  2289. } break;
  2290. case GGML_TYPE_F16:
  2291. {
  2292. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t));
  2293. ((ggml_fp16_t *)(tensor->data))[i] = GGML_FP32_TO_FP16(value);
  2294. } break;
  2295. case GGML_TYPE_BF16:
  2296. {
  2297. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_bf16_t));
  2298. ((ggml_bf16_t *)(tensor->data))[i] = GGML_FP32_TO_BF16(value);
  2299. } break;
  2300. case GGML_TYPE_F32:
  2301. {
  2302. GGML_ASSERT(tensor->nb[0] == sizeof(float));
  2303. ((float *)(tensor->data))[i] = value;
  2304. } break;
  2305. default:
  2306. {
  2307. GGML_ABORT("fatal error");
  2308. }
  2309. }
  2310. }
  2311. int32_t ggml_get_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  2312. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2313. switch (tensor->type) {
  2314. case GGML_TYPE_I8:
  2315. return ((int8_t *) data)[0];
  2316. case GGML_TYPE_I16:
  2317. return ((int16_t *) data)[0];
  2318. case GGML_TYPE_I32:
  2319. return ((int32_t *) data)[0];
  2320. case GGML_TYPE_F16:
  2321. return GGML_FP16_TO_FP32(((ggml_fp16_t *) data)[0]);
  2322. case GGML_TYPE_BF16:
  2323. return GGML_BF16_TO_FP32(((ggml_bf16_t *) data)[0]);
  2324. case GGML_TYPE_F32:
  2325. return ((float *) data)[0];
  2326. default:
  2327. GGML_ABORT("fatal error");
  2328. }
  2329. }
  2330. void ggml_set_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, int32_t value) {
  2331. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2332. switch (tensor->type) {
  2333. case GGML_TYPE_I8:
  2334. {
  2335. ((int8_t *)(data))[0] = value;
  2336. } break;
  2337. case GGML_TYPE_I16:
  2338. {
  2339. ((int16_t *)(data))[0] = value;
  2340. } break;
  2341. case GGML_TYPE_I32:
  2342. {
  2343. ((int32_t *)(data))[0] = value;
  2344. } break;
  2345. case GGML_TYPE_F16:
  2346. {
  2347. ((ggml_fp16_t *)(data))[0] = GGML_FP32_TO_FP16(value);
  2348. } break;
  2349. case GGML_TYPE_BF16:
  2350. {
  2351. ((ggml_bf16_t *)(data))[0] = GGML_FP32_TO_BF16(value);
  2352. } break;
  2353. case GGML_TYPE_F32:
  2354. {
  2355. ((float *)(data))[0] = value;
  2356. } break;
  2357. default:
  2358. {
  2359. GGML_ABORT("fatal error");
  2360. }
  2361. }
  2362. }
  2363. float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i) {
  2364. if (!ggml_is_contiguous(tensor)) {
  2365. int64_t id[4] = { 0, 0, 0, 0 };
  2366. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2367. return ggml_get_f32_nd(tensor, id[0], id[1], id[2], id[3]);
  2368. }
  2369. switch (tensor->type) {
  2370. case GGML_TYPE_I8:
  2371. {
  2372. return ((int8_t *)(tensor->data))[i];
  2373. }
  2374. case GGML_TYPE_I16:
  2375. {
  2376. return ((int16_t *)(tensor->data))[i];
  2377. }
  2378. case GGML_TYPE_I32:
  2379. {
  2380. return ((int32_t *)(tensor->data))[i];
  2381. }
  2382. case GGML_TYPE_F16:
  2383. {
  2384. return GGML_FP16_TO_FP32(((ggml_fp16_t *)(tensor->data))[i]);
  2385. }
  2386. case GGML_TYPE_BF16:
  2387. {
  2388. return GGML_BF16_TO_FP32(((ggml_bf16_t *)(tensor->data))[i]);
  2389. }
  2390. case GGML_TYPE_F32:
  2391. {
  2392. return ((float *)(tensor->data))[i];
  2393. }
  2394. default:
  2395. {
  2396. GGML_ABORT("fatal error");
  2397. }
  2398. }
  2399. }
  2400. void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value) {
  2401. if (!ggml_is_contiguous(tensor)) {
  2402. int64_t id[4] = { 0, 0, 0, 0 };
  2403. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2404. ggml_set_f32_nd(tensor, id[0], id[1], id[2], id[3], value);
  2405. return;
  2406. }
  2407. switch (tensor->type) {
  2408. case GGML_TYPE_I8:
  2409. {
  2410. ((int8_t *)(tensor->data))[i] = value;
  2411. } break;
  2412. case GGML_TYPE_I16:
  2413. {
  2414. ((int16_t *)(tensor->data))[i] = value;
  2415. } break;
  2416. case GGML_TYPE_I32:
  2417. {
  2418. ((int32_t *)(tensor->data))[i] = value;
  2419. } break;
  2420. case GGML_TYPE_F16:
  2421. {
  2422. ((ggml_fp16_t *)(tensor->data))[i] = GGML_FP32_TO_FP16(value);
  2423. } break;
  2424. case GGML_TYPE_BF16:
  2425. {
  2426. ((ggml_bf16_t *)(tensor->data))[i] = GGML_FP32_TO_BF16(value);
  2427. } break;
  2428. case GGML_TYPE_F32:
  2429. {
  2430. ((float *)(tensor->data))[i] = value;
  2431. } break;
  2432. default:
  2433. {
  2434. GGML_ABORT("fatal error");
  2435. }
  2436. }
  2437. }
  2438. float ggml_get_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  2439. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2440. switch (tensor->type) {
  2441. case GGML_TYPE_I8:
  2442. return ((int8_t *) data)[0];
  2443. case GGML_TYPE_I16:
  2444. return ((int16_t *) data)[0];
  2445. case GGML_TYPE_I32:
  2446. return ((int32_t *) data)[0];
  2447. case GGML_TYPE_F16:
  2448. return GGML_FP16_TO_FP32(((ggml_fp16_t *) data)[0]);
  2449. case GGML_TYPE_BF16:
  2450. return GGML_BF16_TO_FP32(((ggml_bf16_t *) data)[0]);
  2451. case GGML_TYPE_F32:
  2452. return ((float *) data)[0];
  2453. default:
  2454. GGML_ABORT("fatal error");
  2455. }
  2456. }
  2457. void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, float value) {
  2458. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2459. switch (tensor->type) {
  2460. case GGML_TYPE_I8:
  2461. {
  2462. ((int8_t *)(data))[0] = value;
  2463. } break;
  2464. case GGML_TYPE_I16:
  2465. {
  2466. ((int16_t *)(data))[0] = value;
  2467. } break;
  2468. case GGML_TYPE_I32:
  2469. {
  2470. ((int32_t *)(data))[0] = value;
  2471. } break;
  2472. case GGML_TYPE_F16:
  2473. {
  2474. ((ggml_fp16_t *)(data))[0] = GGML_FP32_TO_FP16(value);
  2475. } break;
  2476. case GGML_TYPE_BF16:
  2477. {
  2478. ((ggml_bf16_t *)(data))[0] = GGML_FP32_TO_BF16(value);
  2479. } break;
  2480. case GGML_TYPE_F32:
  2481. {
  2482. ((float *)(data))[0] = value;
  2483. } break;
  2484. default:
  2485. {
  2486. GGML_ABORT("fatal error");
  2487. }
  2488. }
  2489. }
  2490. ////////////////////////////////////////////////////////////////////////////////
  2491. // ggml_compute_forward_dup
  2492. static void ggml_compute_forward_dup_same_cont(
  2493. const struct ggml_compute_params * params,
  2494. struct ggml_tensor * dst) {
  2495. const struct ggml_tensor * src0 = dst->src[0];
  2496. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  2497. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  2498. GGML_ASSERT(src0->type == dst->type);
  2499. const size_t nb0 = ggml_type_size(src0->type);
  2500. const int ith = params->ith; // thread index
  2501. const int nth = params->nth; // number of threads
  2502. // parallelize by elements
  2503. const int ne = ggml_nelements(dst);
  2504. const int dr = (ne + nth - 1) / nth;
  2505. const int ie0 = dr * ith;
  2506. const int ie1 = MIN(ie0 + dr, ne);
  2507. if (ie0 < ie1) {
  2508. memcpy(
  2509. ((char *) dst->data + ie0*nb0),
  2510. ((char *) src0->data + ie0*nb0),
  2511. (ie1 - ie0) * nb0);
  2512. }
  2513. }
  2514. static void ggml_compute_forward_dup_f16(
  2515. const struct ggml_compute_params * params,
  2516. struct ggml_tensor * dst) {
  2517. const struct ggml_tensor * src0 = dst->src[0];
  2518. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  2519. GGML_TENSOR_UNARY_OP_LOCALS
  2520. const int ith = params->ith; // thread index
  2521. const int nth = params->nth; // number of threads
  2522. // parallelize by rows
  2523. const int nr = ne01;
  2524. // number of rows per thread
  2525. const int dr = (nr + nth - 1) / nth;
  2526. // row range for this thread
  2527. const int ir0 = dr * ith;
  2528. const int ir1 = MIN(ir0 + dr, nr);
  2529. if (src0->type == dst->type &&
  2530. ne00 == ne0 &&
  2531. nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
  2532. // copy by rows
  2533. const size_t rs = ne00*nb00;
  2534. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2535. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2536. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2537. memcpy(
  2538. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  2539. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  2540. rs);
  2541. }
  2542. }
  2543. }
  2544. return;
  2545. }
  2546. // TODO: add more special-case implementations for tensor shapes/strides that can benefit from memcpy
  2547. if (ggml_is_contiguous(dst)) {
  2548. if (nb00 == sizeof(ggml_fp16_t)) {
  2549. if (dst->type == GGML_TYPE_F16) {
  2550. size_t id = 0;
  2551. const size_t rs = ne00 * nb00;
  2552. char * dst_ptr = (char *) dst->data;
  2553. for (int i03 = 0; i03 < ne03; i03++) {
  2554. for (int i02 = 0; i02 < ne02; i02++) {
  2555. id += rs * ir0;
  2556. for (int i01 = ir0; i01 < ir1; i01++) {
  2557. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  2558. memcpy(dst_ptr + id, src0_ptr, rs);
  2559. id += rs;
  2560. }
  2561. id += rs * (ne01 - ir1);
  2562. }
  2563. }
  2564. } else if (dst->type == GGML_TYPE_F32) {
  2565. size_t id = 0;
  2566. float * dst_ptr = (float *) dst->data;
  2567. for (int i03 = 0; i03 < ne03; i03++) {
  2568. for (int i02 = 0; i02 < ne02; i02++) {
  2569. id += ne00 * ir0;
  2570. for (int i01 = ir0; i01 < ir1; i01++) {
  2571. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2572. for (int i00 = 0; i00 < ne00; i00++) {
  2573. dst_ptr[id] = GGML_FP16_TO_FP32(src0_ptr[i00]);
  2574. id++;
  2575. }
  2576. }
  2577. id += ne00 * (ne01 - ir1);
  2578. }
  2579. }
  2580. } else if (ggml_get_type_traits_cpu(dst->type)->from_float) {
  2581. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float;
  2582. float * src0_f32 = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
  2583. size_t id = 0;
  2584. size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
  2585. char * dst_ptr = (char *) dst->data;
  2586. for (int i03 = 0; i03 < ne03; i03++) {
  2587. for (int i02 = 0; i02 < ne02; i02++) {
  2588. id += rs * ir0;
  2589. for (int i01 = ir0; i01 < ir1; i01++) {
  2590. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2591. for (int i00 = 0; i00 < ne00; i00++) {
  2592. src0_f32[i00] = GGML_FP16_TO_FP32(src0_ptr[i00]);
  2593. }
  2594. quantize_row_q(src0_f32, dst_ptr + id, ne00);
  2595. id += rs;
  2596. }
  2597. id += rs * (ne01 - ir1);
  2598. }
  2599. }
  2600. } else {
  2601. GGML_ABORT("fatal error"); // TODO: implement
  2602. }
  2603. } else {
  2604. //printf("%s: this is not optimal - fix me\n", __func__);
  2605. if (dst->type == GGML_TYPE_F32) {
  2606. size_t id = 0;
  2607. float * dst_ptr = (float *) dst->data;
  2608. for (int i03 = 0; i03 < ne03; i03++) {
  2609. for (int i02 = 0; i02 < ne02; i02++) {
  2610. id += ne00 * ir0;
  2611. for (int i01 = ir0; i01 < ir1; i01++) {
  2612. for (int i00 = 0; i00 < ne00; i00++) {
  2613. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2614. dst_ptr[id] = GGML_FP16_TO_FP32(*src0_ptr);
  2615. id++;
  2616. }
  2617. }
  2618. id += ne00 * (ne01 - ir1);
  2619. }
  2620. }
  2621. } else if (dst->type == GGML_TYPE_F16) {
  2622. size_t id = 0;
  2623. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  2624. for (int i03 = 0; i03 < ne03; i03++) {
  2625. for (int i02 = 0; i02 < ne02; i02++) {
  2626. id += ne00 * ir0;
  2627. for (int i01 = ir0; i01 < ir1; i01++) {
  2628. for (int i00 = 0; i00 < ne00; i00++) {
  2629. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2630. dst_ptr[id] = *src0_ptr;
  2631. id++;
  2632. }
  2633. }
  2634. id += ne00 * (ne01 - ir1);
  2635. }
  2636. }
  2637. } else {
  2638. GGML_ABORT("fatal error"); // TODO: implement
  2639. }
  2640. }
  2641. return;
  2642. }
  2643. // dst counters
  2644. int64_t i10 = 0;
  2645. int64_t i11 = 0;
  2646. int64_t i12 = 0;
  2647. int64_t i13 = 0;
  2648. if (dst->type == GGML_TYPE_F16) {
  2649. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2650. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2651. i10 += ne00 * ir0;
  2652. while (i10 >= ne0) {
  2653. i10 -= ne0;
  2654. if (++i11 == ne1) {
  2655. i11 = 0;
  2656. if (++i12 == ne2) {
  2657. i12 = 0;
  2658. if (++i13 == ne3) {
  2659. i13 = 0;
  2660. }
  2661. }
  2662. }
  2663. }
  2664. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2665. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2666. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2667. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2668. memcpy(dst_ptr, src0_ptr, sizeof(ggml_fp16_t));
  2669. if (++i10 == ne00) {
  2670. i10 = 0;
  2671. if (++i11 == ne01) {
  2672. i11 = 0;
  2673. if (++i12 == ne02) {
  2674. i12 = 0;
  2675. if (++i13 == ne03) {
  2676. i13 = 0;
  2677. }
  2678. }
  2679. }
  2680. }
  2681. }
  2682. }
  2683. i10 += ne00 * (ne01 - ir1);
  2684. while (i10 >= ne0) {
  2685. i10 -= ne0;
  2686. if (++i11 == ne1) {
  2687. i11 = 0;
  2688. if (++i12 == ne2) {
  2689. i12 = 0;
  2690. if (++i13 == ne3) {
  2691. i13 = 0;
  2692. }
  2693. }
  2694. }
  2695. }
  2696. }
  2697. }
  2698. } else if (dst->type == GGML_TYPE_F32) {
  2699. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2700. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2701. i10 += ne00 * ir0;
  2702. while (i10 >= ne0) {
  2703. i10 -= ne0;
  2704. if (++i11 == ne1) {
  2705. i11 = 0;
  2706. if (++i12 == ne2) {
  2707. i12 = 0;
  2708. if (++i13 == ne3) {
  2709. i13 = 0;
  2710. }
  2711. }
  2712. }
  2713. }
  2714. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2715. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2716. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2717. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2718. *(float *) dst_ptr = GGML_FP16_TO_FP32(*(const ggml_fp16_t *) src0_ptr);
  2719. if (++i10 == ne0) {
  2720. i10 = 0;
  2721. if (++i11 == ne1) {
  2722. i11 = 0;
  2723. if (++i12 == ne2) {
  2724. i12 = 0;
  2725. if (++i13 == ne3) {
  2726. i13 = 0;
  2727. }
  2728. }
  2729. }
  2730. }
  2731. }
  2732. }
  2733. i10 += ne00 * (ne01 - ir1);
  2734. while (i10 >= ne0) {
  2735. i10 -= ne0;
  2736. if (++i11 == ne1) {
  2737. i11 = 0;
  2738. if (++i12 == ne2) {
  2739. i12 = 0;
  2740. if (++i13 == ne3) {
  2741. i13 = 0;
  2742. }
  2743. }
  2744. }
  2745. }
  2746. }
  2747. }
  2748. } else {
  2749. GGML_ABORT("fatal error"); // TODO: implement
  2750. }
  2751. }
  2752. static void ggml_compute_forward_dup_bf16(
  2753. const struct ggml_compute_params * params,
  2754. struct ggml_tensor * dst) {
  2755. const struct ggml_tensor * src0 = dst->src[0];
  2756. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  2757. GGML_TENSOR_UNARY_OP_LOCALS
  2758. const int ith = params->ith; // thread index
  2759. const int nth = params->nth; // number of threads
  2760. // parallelize by rows
  2761. const int nr = ne01;
  2762. // number of rows per thread
  2763. const int dr = (nr + nth - 1) / nth;
  2764. // row range for this thread
  2765. const int ir0 = dr * ith;
  2766. const int ir1 = MIN(ir0 + dr, nr);
  2767. if (src0->type == dst->type &&
  2768. ne00 == ne0 &&
  2769. nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
  2770. // copy by rows
  2771. const size_t rs = ne00*nb00;
  2772. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2773. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2774. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2775. memcpy(
  2776. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  2777. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  2778. rs);
  2779. }
  2780. }
  2781. }
  2782. return;
  2783. }
  2784. // TODO: add more special-case implementations for tensor shapes/strides that can benefit from memcpy
  2785. if (ggml_is_contiguous(dst)) {
  2786. if (nb00 == sizeof(ggml_bf16_t)) {
  2787. if (dst->type == GGML_TYPE_BF16) {
  2788. size_t id = 0;
  2789. const size_t rs = ne00 * nb00;
  2790. char * dst_ptr = (char *) dst->data;
  2791. for (int i03 = 0; i03 < ne03; i03++) {
  2792. for (int i02 = 0; i02 < ne02; i02++) {
  2793. id += rs * ir0;
  2794. for (int i01 = ir0; i01 < ir1; i01++) {
  2795. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  2796. memcpy(dst_ptr + id, src0_ptr, rs);
  2797. id += rs;
  2798. }
  2799. id += rs * (ne01 - ir1);
  2800. }
  2801. }
  2802. } else if (dst->type == GGML_TYPE_F16) {
  2803. size_t id = 0;
  2804. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  2805. for (int i03 = 0; i03 < ne03; i03++) {
  2806. for (int i02 = 0; i02 < ne02; i02++) {
  2807. id += ne00 * ir0;
  2808. for (int i01 = ir0; i01 < ir1; i01++) {
  2809. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2810. for (int i00 = 0; i00 < ne00; i00++) {
  2811. dst_ptr[id] = GGML_FP32_TO_FP16(GGML_BF16_TO_FP32(src0_ptr[i00]));
  2812. id++;
  2813. }
  2814. }
  2815. id += ne00 * (ne01 - ir1);
  2816. }
  2817. }
  2818. } else if (dst->type == GGML_TYPE_F32) {
  2819. size_t id = 0;
  2820. float * dst_ptr = (float *) dst->data;
  2821. for (int i03 = 0; i03 < ne03; i03++) {
  2822. for (int i02 = 0; i02 < ne02; i02++) {
  2823. id += ne00 * ir0;
  2824. for (int i01 = ir0; i01 < ir1; i01++) {
  2825. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2826. for (int i00 = 0; i00 < ne00; i00++) {
  2827. dst_ptr[id] = GGML_BF16_TO_FP32(src0_ptr[i00]);
  2828. id++;
  2829. }
  2830. }
  2831. id += ne00 * (ne01 - ir1);
  2832. }
  2833. }
  2834. } else if (ggml_get_type_traits_cpu(dst->type)->from_float) {
  2835. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float;
  2836. float * src0_f32 = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
  2837. size_t id = 0;
  2838. size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
  2839. char * dst_ptr = (char *) dst->data;
  2840. for (int i03 = 0; i03 < ne03; i03++) {
  2841. for (int i02 = 0; i02 < ne02; i02++) {
  2842. id += rs * ir0;
  2843. for (int i01 = ir0; i01 < ir1; i01++) {
  2844. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2845. for (int i00 = 0; i00 < ne00; i00++) {
  2846. src0_f32[i00] = GGML_BF16_TO_FP32(src0_ptr[i00]);
  2847. }
  2848. quantize_row_q(src0_f32, dst_ptr + id, ne00);
  2849. id += rs;
  2850. }
  2851. id += rs * (ne01 - ir1);
  2852. }
  2853. }
  2854. } else {
  2855. GGML_ABORT("fatal error"); // TODO: implement
  2856. }
  2857. } else {
  2858. //printf("%s: this is not optimal - fix me\n", __func__);
  2859. if (dst->type == GGML_TYPE_F32) {
  2860. size_t id = 0;
  2861. float * dst_ptr = (float *) dst->data;
  2862. for (int i03 = 0; i03 < ne03; i03++) {
  2863. for (int i02 = 0; i02 < ne02; i02++) {
  2864. id += ne00 * ir0;
  2865. for (int i01 = ir0; i01 < ir1; i01++) {
  2866. for (int i00 = 0; i00 < ne00; i00++) {
  2867. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2868. dst_ptr[id] = GGML_BF16_TO_FP32(*src0_ptr);
  2869. id++;
  2870. }
  2871. }
  2872. id += ne00 * (ne01 - ir1);
  2873. }
  2874. }
  2875. } else if (dst->type == GGML_TYPE_BF16) {
  2876. size_t id = 0;
  2877. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) dst->data;
  2878. for (int i03 = 0; i03 < ne03; i03++) {
  2879. for (int i02 = 0; i02 < ne02; i02++) {
  2880. id += ne00 * ir0;
  2881. for (int i01 = ir0; i01 < ir1; i01++) {
  2882. for (int i00 = 0; i00 < ne00; i00++) {
  2883. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2884. dst_ptr[id] = *src0_ptr;
  2885. id++;
  2886. }
  2887. }
  2888. id += ne00 * (ne01 - ir1);
  2889. }
  2890. }
  2891. } else if (dst->type == GGML_TYPE_F16) {
  2892. size_t id = 0;
  2893. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  2894. for (int i03 = 0; i03 < ne03; i03++) {
  2895. for (int i02 = 0; i02 < ne02; i02++) {
  2896. id += ne00 * ir0;
  2897. for (int i01 = ir0; i01 < ir1; i01++) {
  2898. for (int i00 = 0; i00 < ne00; i00++) {
  2899. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2900. dst_ptr[id] = GGML_FP32_TO_FP16(GGML_BF16_TO_FP32(*src0_ptr));
  2901. id++;
  2902. }
  2903. }
  2904. id += ne00 * (ne01 - ir1);
  2905. }
  2906. }
  2907. } else {
  2908. GGML_ABORT("fatal error"); // TODO: implement
  2909. }
  2910. }
  2911. return;
  2912. }
  2913. // dst counters
  2914. int64_t i10 = 0;
  2915. int64_t i11 = 0;
  2916. int64_t i12 = 0;
  2917. int64_t i13 = 0;
  2918. if (dst->type == GGML_TYPE_BF16) {
  2919. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2920. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2921. i10 += ne00 * ir0;
  2922. while (i10 >= ne0) {
  2923. i10 -= ne0;
  2924. if (++i11 == ne1) {
  2925. i11 = 0;
  2926. if (++i12 == ne2) {
  2927. i12 = 0;
  2928. if (++i13 == ne3) {
  2929. i13 = 0;
  2930. }
  2931. }
  2932. }
  2933. }
  2934. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2935. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2936. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2937. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2938. memcpy(dst_ptr, src0_ptr, sizeof(ggml_bf16_t));
  2939. if (++i10 == ne00) {
  2940. i10 = 0;
  2941. if (++i11 == ne01) {
  2942. i11 = 0;
  2943. if (++i12 == ne02) {
  2944. i12 = 0;
  2945. if (++i13 == ne03) {
  2946. i13 = 0;
  2947. }
  2948. }
  2949. }
  2950. }
  2951. }
  2952. }
  2953. i10 += ne00 * (ne01 - ir1);
  2954. while (i10 >= ne0) {
  2955. i10 -= ne0;
  2956. if (++i11 == ne1) {
  2957. i11 = 0;
  2958. if (++i12 == ne2) {
  2959. i12 = 0;
  2960. if (++i13 == ne3) {
  2961. i13 = 0;
  2962. }
  2963. }
  2964. }
  2965. }
  2966. }
  2967. }
  2968. } else if (dst->type == GGML_TYPE_F16) {
  2969. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2970. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2971. i10 += ne00 * ir0;
  2972. while (i10 >= ne0) {
  2973. i10 -= ne0;
  2974. if (++i11 == ne1) {
  2975. i11 = 0;
  2976. if (++i12 == ne2) {
  2977. i12 = 0;
  2978. if (++i13 == ne3) {
  2979. i13 = 0;
  2980. }
  2981. }
  2982. }
  2983. }
  2984. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2985. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2986. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2987. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2988. *(ggml_fp16_t *) dst_ptr = GGML_FP32_TO_FP16(GGML_BF16_TO_FP32(*(const ggml_bf16_t *) src0_ptr));
  2989. if (++i10 == ne0) {
  2990. i10 = 0;
  2991. if (++i11 == ne1) {
  2992. i11 = 0;
  2993. if (++i12 == ne2) {
  2994. i12 = 0;
  2995. if (++i13 == ne3) {
  2996. i13 = 0;
  2997. }
  2998. }
  2999. }
  3000. }
  3001. }
  3002. }
  3003. i10 += ne00 * (ne01 - ir1);
  3004. while (i10 >= ne0) {
  3005. i10 -= ne0;
  3006. if (++i11 == ne1) {
  3007. i11 = 0;
  3008. if (++i12 == ne2) {
  3009. i12 = 0;
  3010. if (++i13 == ne3) {
  3011. i13 = 0;
  3012. }
  3013. }
  3014. }
  3015. }
  3016. }
  3017. }
  3018. } else if (dst->type == GGML_TYPE_F32) {
  3019. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3020. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3021. i10 += ne00 * ir0;
  3022. while (i10 >= ne0) {
  3023. i10 -= ne0;
  3024. if (++i11 == ne1) {
  3025. i11 = 0;
  3026. if (++i12 == ne2) {
  3027. i12 = 0;
  3028. if (++i13 == ne3) {
  3029. i13 = 0;
  3030. }
  3031. }
  3032. }
  3033. }
  3034. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3035. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3036. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3037. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3038. *(float *) dst_ptr = GGML_BF16_TO_FP32(*(const ggml_bf16_t *) src0_ptr);
  3039. if (++i10 == ne0) {
  3040. i10 = 0;
  3041. if (++i11 == ne1) {
  3042. i11 = 0;
  3043. if (++i12 == ne2) {
  3044. i12 = 0;
  3045. if (++i13 == ne3) {
  3046. i13 = 0;
  3047. }
  3048. }
  3049. }
  3050. }
  3051. }
  3052. }
  3053. i10 += ne00 * (ne01 - ir1);
  3054. while (i10 >= ne0) {
  3055. i10 -= ne0;
  3056. if (++i11 == ne1) {
  3057. i11 = 0;
  3058. if (++i12 == ne2) {
  3059. i12 = 0;
  3060. if (++i13 == ne3) {
  3061. i13 = 0;
  3062. }
  3063. }
  3064. }
  3065. }
  3066. }
  3067. }
  3068. } else {
  3069. GGML_ABORT("fatal error"); // TODO: implement
  3070. }
  3071. }
  3072. static void ggml_compute_forward_dup_f32(
  3073. const struct ggml_compute_params * params,
  3074. struct ggml_tensor * dst) {
  3075. const struct ggml_tensor * src0 = dst->src[0];
  3076. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  3077. GGML_TENSOR_UNARY_OP_LOCALS
  3078. const int ith = params->ith; // thread index
  3079. const int nth = params->nth; // number of threads
  3080. // parallelize by rows
  3081. const int nr = ne01;
  3082. // number of rows per thread
  3083. const int dr = (nr + nth - 1) / nth;
  3084. // row range for this thread
  3085. const int ir0 = dr * ith;
  3086. const int ir1 = MIN(ir0 + dr, nr);
  3087. if (src0->type == dst->type &&
  3088. ne00 == ne0 &&
  3089. nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
  3090. // copy by rows
  3091. const size_t rs = ne00*nb00;
  3092. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3093. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3094. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3095. memcpy(
  3096. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  3097. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  3098. rs);
  3099. }
  3100. }
  3101. }
  3102. return;
  3103. }
  3104. if (ggml_is_contiguous(dst)) {
  3105. // TODO: simplify
  3106. if (nb00 == sizeof(float)) {
  3107. if (dst->type == GGML_TYPE_F32) {
  3108. size_t id = 0;
  3109. const size_t rs = ne00 * nb00;
  3110. char * dst_ptr = (char *) dst->data;
  3111. for (int i03 = 0; i03 < ne03; i03++) {
  3112. for (int i02 = 0; i02 < ne02; i02++) {
  3113. id += rs * ir0;
  3114. for (int i01 = ir0; i01 < ir1; i01++) {
  3115. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  3116. memcpy(dst_ptr + id, src0_ptr, rs);
  3117. id += rs;
  3118. }
  3119. id += rs * (ne01 - ir1);
  3120. }
  3121. }
  3122. } else if (ggml_get_type_traits_cpu(dst->type)->from_float) {
  3123. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float;
  3124. size_t id = 0;
  3125. size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
  3126. char * dst_ptr = (char *) dst->data;
  3127. for (int i03 = 0; i03 < ne03; i03++) {
  3128. for (int i02 = 0; i02 < ne02; i02++) {
  3129. id += rs * ir0;
  3130. for (int i01 = ir0; i01 < ir1; i01++) {
  3131. const float * src0_ptr = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  3132. quantize_row_q(src0_ptr, dst_ptr + id, ne00);
  3133. id += rs;
  3134. }
  3135. id += rs * (ne01 - ir1);
  3136. }
  3137. }
  3138. } else {
  3139. GGML_ABORT("fatal error"); // TODO: implement
  3140. }
  3141. } else {
  3142. //printf("%s: this is not optimal - fix me\n", __func__);
  3143. if (dst->type == GGML_TYPE_F32) {
  3144. size_t id = 0;
  3145. float * dst_ptr = (float *) dst->data;
  3146. for (int i03 = 0; i03 < ne03; i03++) {
  3147. for (int i02 = 0; i02 < ne02; i02++) {
  3148. id += ne00 * ir0;
  3149. for (int i01 = ir0; i01 < ir1; i01++) {
  3150. for (int i00 = 0; i00 < ne00; i00++) {
  3151. const float * src0_ptr = (float *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3152. dst_ptr[id] = *src0_ptr;
  3153. id++;
  3154. }
  3155. }
  3156. id += ne00 * (ne01 - ir1);
  3157. }
  3158. }
  3159. } else if (dst->type == GGML_TYPE_F16) {
  3160. size_t id = 0;
  3161. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  3162. for (int i03 = 0; i03 < ne03; i03++) {
  3163. for (int i02 = 0; i02 < ne02; i02++) {
  3164. id += ne00 * ir0;
  3165. for (int i01 = ir0; i01 < ir1; i01++) {
  3166. for (int i00 = 0; i00 < ne00; i00++) {
  3167. const float * src0_ptr = (float *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3168. dst_ptr[id] = GGML_FP32_TO_FP16(*src0_ptr);
  3169. id++;
  3170. }
  3171. }
  3172. id += ne00 * (ne01 - ir1);
  3173. }
  3174. }
  3175. } else if (dst->type == GGML_TYPE_BF16) {
  3176. size_t id = 0;
  3177. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) dst->data;
  3178. for (int i03 = 0; i03 < ne03; i03++) {
  3179. for (int i02 = 0; i02 < ne02; i02++) {
  3180. id += ne00 * ir0;
  3181. for (int i01 = ir0; i01 < ir1; i01++) {
  3182. for (int i00 = 0; i00 < ne00; i00++) {
  3183. const float * src0_ptr = (float *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3184. dst_ptr[id] = GGML_FP32_TO_BF16(*src0_ptr);
  3185. id++;
  3186. }
  3187. }
  3188. id += ne00 * (ne01 - ir1);
  3189. }
  3190. }
  3191. } else {
  3192. GGML_ABORT("fatal error"); // TODO: implement
  3193. }
  3194. }
  3195. return;
  3196. }
  3197. // dst counters
  3198. int64_t i10 = 0;
  3199. int64_t i11 = 0;
  3200. int64_t i12 = 0;
  3201. int64_t i13 = 0;
  3202. if (dst->type == GGML_TYPE_F32) {
  3203. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3204. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3205. i10 += ne00 * ir0;
  3206. while (i10 >= ne0) {
  3207. i10 -= ne0;
  3208. if (++i11 == ne1) {
  3209. i11 = 0;
  3210. if (++i12 == ne2) {
  3211. i12 = 0;
  3212. if (++i13 == ne3) {
  3213. i13 = 0;
  3214. }
  3215. }
  3216. }
  3217. }
  3218. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3219. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3220. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3221. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3222. memcpy(dst_ptr, src0_ptr, sizeof(float));
  3223. if (++i10 == ne0) {
  3224. i10 = 0;
  3225. if (++i11 == ne1) {
  3226. i11 = 0;
  3227. if (++i12 == ne2) {
  3228. i12 = 0;
  3229. if (++i13 == ne3) {
  3230. i13 = 0;
  3231. }
  3232. }
  3233. }
  3234. }
  3235. }
  3236. }
  3237. i10 += ne00 * (ne01 - ir1);
  3238. while (i10 >= ne0) {
  3239. i10 -= ne0;
  3240. if (++i11 == ne1) {
  3241. i11 = 0;
  3242. if (++i12 == ne2) {
  3243. i12 = 0;
  3244. if (++i13 == ne3) {
  3245. i13 = 0;
  3246. }
  3247. }
  3248. }
  3249. }
  3250. }
  3251. }
  3252. } else if (dst->type == GGML_TYPE_F16) {
  3253. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3254. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3255. i10 += ne00 * ir0;
  3256. while (i10 >= ne0) {
  3257. i10 -= ne0;
  3258. if (++i11 == ne1) {
  3259. i11 = 0;
  3260. if (++i12 == ne2) {
  3261. i12 = 0;
  3262. if (++i13 == ne3) {
  3263. i13 = 0;
  3264. }
  3265. }
  3266. }
  3267. }
  3268. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3269. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3270. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3271. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3272. *(ggml_fp16_t *) dst_ptr = GGML_FP32_TO_FP16(*(const float *) src0_ptr);
  3273. if (++i10 == ne0) {
  3274. i10 = 0;
  3275. if (++i11 == ne1) {
  3276. i11 = 0;
  3277. if (++i12 == ne2) {
  3278. i12 = 0;
  3279. if (++i13 == ne3) {
  3280. i13 = 0;
  3281. }
  3282. }
  3283. }
  3284. }
  3285. }
  3286. }
  3287. i10 += ne00 * (ne01 - ir1);
  3288. while (i10 >= ne0) {
  3289. i10 -= ne0;
  3290. if (++i11 == ne1) {
  3291. i11 = 0;
  3292. if (++i12 == ne2) {
  3293. i12 = 0;
  3294. if (++i13 == ne3) {
  3295. i13 = 0;
  3296. }
  3297. }
  3298. }
  3299. }
  3300. }
  3301. }
  3302. } else if (dst->type == GGML_TYPE_BF16) {
  3303. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3304. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3305. i10 += ne00 * ir0;
  3306. while (i10 >= ne0) {
  3307. i10 -= ne0;
  3308. if (++i11 == ne1) {
  3309. i11 = 0;
  3310. if (++i12 == ne2) {
  3311. i12 = 0;
  3312. if (++i13 == ne3) {
  3313. i13 = 0;
  3314. }
  3315. }
  3316. }
  3317. }
  3318. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3319. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3320. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3321. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3322. *(ggml_bf16_t *) dst_ptr = GGML_FP32_TO_BF16(*(const float *) src0_ptr);
  3323. if (++i10 == ne0) {
  3324. i10 = 0;
  3325. if (++i11 == ne1) {
  3326. i11 = 0;
  3327. if (++i12 == ne2) {
  3328. i12 = 0;
  3329. if (++i13 == ne3) {
  3330. i13 = 0;
  3331. }
  3332. }
  3333. }
  3334. }
  3335. }
  3336. }
  3337. i10 += ne00 * (ne01 - ir1);
  3338. while (i10 >= ne0) {
  3339. i10 -= ne0;
  3340. if (++i11 == ne1) {
  3341. i11 = 0;
  3342. if (++i12 == ne2) {
  3343. i12 = 0;
  3344. if (++i13 == ne3) {
  3345. i13 = 0;
  3346. }
  3347. }
  3348. }
  3349. }
  3350. }
  3351. }
  3352. } else {
  3353. GGML_ABORT("fatal error"); // TODO: implement
  3354. }
  3355. }
  3356. // A simplified version of ggml_compute_forward_dup that doesn't do float upcasting, and just plain old memcpy.
  3357. static void ggml_compute_forward_dup_bytes(
  3358. const struct ggml_compute_params * params,
  3359. struct ggml_tensor * dst) {
  3360. const struct ggml_tensor * src0 = dst->src[0];
  3361. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  3362. GGML_ASSERT(src0->type == dst->type);
  3363. GGML_TENSOR_UNARY_OP_LOCALS;
  3364. if (ggml_is_contiguous(src0) && ggml_is_contiguous(dst)) {
  3365. ggml_compute_forward_dup_same_cont(params, dst);
  3366. return;
  3367. }
  3368. const size_t type_size = ggml_type_size(src0->type);
  3369. const int ith = params->ith; // thread index
  3370. const int nth = params->nth; // number of threads
  3371. // parallelize by rows
  3372. const int nr = ne01;
  3373. // number of rows per thread
  3374. const int dr = (nr + nth - 1) / nth;
  3375. // row range for this thread
  3376. const int ir0 = dr * ith;
  3377. const int ir1 = MIN(ir0 + dr, nr);
  3378. if (src0->type == dst->type &&
  3379. ne00 == ne0 &&
  3380. nb00 == type_size && nb0 == type_size) {
  3381. // copy by rows
  3382. const size_t rs = ne00 * type_size;
  3383. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3384. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3385. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3386. memcpy(
  3387. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  3388. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  3389. rs);
  3390. }
  3391. }
  3392. }
  3393. return;
  3394. }
  3395. if (ggml_is_contiguous(dst)) {
  3396. size_t id = 0;
  3397. char * dst_ptr = (char *) dst->data;
  3398. const size_t rs = ne00 * type_size;
  3399. if (nb00 == type_size) {
  3400. // src0 is contigous on first dimension, copy by rows
  3401. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3402. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3403. id += rs * ir0;
  3404. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3405. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  3406. memcpy(dst_ptr + id, src0_ptr, rs);
  3407. id += rs;
  3408. }
  3409. id += rs * (ne01 - ir1);
  3410. }
  3411. }
  3412. } else {
  3413. //printf("%s: this is not optimal - fix me\n", __func__);
  3414. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3415. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3416. id += rs * ir0;
  3417. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3418. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3419. const char * src0_ptr = (char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03;
  3420. memcpy(dst_ptr + id, src0_ptr, type_size);
  3421. id += type_size;
  3422. }
  3423. }
  3424. id += rs * (ne01 - ir1);
  3425. }
  3426. }
  3427. }
  3428. return;
  3429. }
  3430. // dst counters
  3431. int64_t i10 = 0;
  3432. int64_t i11 = 0;
  3433. int64_t i12 = 0;
  3434. int64_t i13 = 0;
  3435. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3436. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3437. i10 += ne00 * ir0;
  3438. while (i10 >= ne0) {
  3439. i10 -= ne0;
  3440. if (++i11 == ne1) {
  3441. i11 = 0;
  3442. if (++i12 == ne2) {
  3443. i12 = 0;
  3444. if (++i13 == ne3) {
  3445. i13 = 0;
  3446. }
  3447. }
  3448. }
  3449. }
  3450. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3451. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3452. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3453. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3454. memcpy(dst_ptr, src0_ptr, type_size);
  3455. if (++i10 == ne0) {
  3456. i10 = 0;
  3457. if (++i11 == ne1) {
  3458. i11 = 0;
  3459. if (++i12 == ne2) {
  3460. i12 = 0;
  3461. if (++i13 == ne3) {
  3462. i13 = 0;
  3463. }
  3464. }
  3465. }
  3466. }
  3467. }
  3468. }
  3469. i10 += ne00 * (ne01 - ir1);
  3470. while (i10 >= ne0) {
  3471. i10 -= ne0;
  3472. if (++i11 == ne1) {
  3473. i11 = 0;
  3474. if (++i12 == ne2) {
  3475. i12 = 0;
  3476. if (++i13 == ne3) {
  3477. i13 = 0;
  3478. }
  3479. }
  3480. }
  3481. }
  3482. }
  3483. }
  3484. }
  3485. static void ggml_compute_forward_dup_q(
  3486. const struct ggml_compute_params * params,
  3487. struct ggml_tensor * dst) {
  3488. const struct ggml_tensor * src0 = dst->src[0];
  3489. const struct ggml_tensor * src1 = dst->src[1];
  3490. GGML_TENSOR_BINARY_OP_LOCALS
  3491. const enum ggml_type type = src0->type;
  3492. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  3493. size_t qk = ggml_blck_size(type);
  3494. const int64_t nr = ggml_nelements(src1) / qk;
  3495. // destination must be contiguous in the first dimension
  3496. GGML_ASSERT(nb10 == ggml_type_size(dst->type));
  3497. // must either have first dimension large enough to hold a row, or fully contiguous
  3498. GGML_ASSERT((ne10 % qk) == 0 || ggml_is_contiguous(dst));
  3499. const int ith = params->ith;
  3500. const int nth = params->nth;
  3501. const int dr = (nr + nth - 1)/nth;
  3502. // row range for this thread
  3503. const int ir0 = dr*ith;
  3504. const int ir1 = MIN(ir0 + dr, nr);
  3505. for (int64_t ir = ir0; ir < ir1; ++ir) {
  3506. uint32_t i = ir * qk;
  3507. const int64_t i03 = i/(ne00 * ne01 * ne02);
  3508. const int64_t i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
  3509. const int64_t i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
  3510. const int64_t i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
  3511. const int64_t x_offset = (i00/qk)*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
  3512. const int64_t i13 = i/(ne10 * ne11 * ne12);
  3513. const int64_t i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
  3514. const int64_t i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
  3515. const int64_t i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
  3516. const int64_t dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
  3517. dequantize_row_q(
  3518. (const void *) ((char *) src0->data + x_offset),
  3519. (float *) ((char *) dst->data + dst_offset), qk);
  3520. }
  3521. }
  3522. static void ggml_compute_forward_dup(
  3523. const struct ggml_compute_params * params,
  3524. struct ggml_tensor * dst) {
  3525. const struct ggml_tensor * src0 = dst->src[0];
  3526. if (src0->type == dst->type) {
  3527. ggml_compute_forward_dup_bytes(params, dst);
  3528. return;
  3529. }
  3530. switch (src0->type) {
  3531. case GGML_TYPE_F16:
  3532. {
  3533. ggml_compute_forward_dup_f16(params, dst);
  3534. } break;
  3535. case GGML_TYPE_BF16:
  3536. {
  3537. ggml_compute_forward_dup_bf16(params, dst);
  3538. } break;
  3539. case GGML_TYPE_F32:
  3540. {
  3541. ggml_compute_forward_dup_f32(params, dst);
  3542. } break;
  3543. default:
  3544. {
  3545. if (ggml_is_quantized(src0->type) && dst->type == GGML_TYPE_F32) {
  3546. ggml_compute_forward_dup_q(params, dst);
  3547. break;
  3548. }
  3549. GGML_ABORT("fatal error");
  3550. }
  3551. }
  3552. }
  3553. // ggml_compute_forward_add
  3554. static void ggml_compute_forward_add_f32(
  3555. const struct ggml_compute_params * params,
  3556. struct ggml_tensor * dst) {
  3557. const struct ggml_tensor * src0 = dst->src[0];
  3558. const struct ggml_tensor * src1 = dst->src[1];
  3559. GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  3560. const int ith = params->ith;
  3561. const int nth = params->nth;
  3562. const int nr = ggml_nrows(src0);
  3563. GGML_TENSOR_BINARY_OP_LOCALS
  3564. GGML_ASSERT( nb0 == sizeof(float));
  3565. GGML_ASSERT(nb00 == sizeof(float));
  3566. // rows per thread
  3567. const int dr = (nr + nth - 1)/nth;
  3568. // row range for this thread
  3569. const int ir0 = dr*ith;
  3570. const int ir1 = MIN(ir0 + dr, nr);
  3571. if (nb10 == sizeof(float)) {
  3572. for (int ir = ir0; ir < ir1; ++ir) {
  3573. // src1 is broadcastable across src0 and dst in i1, i2, i3
  3574. const int64_t i03 = ir/(ne02*ne01);
  3575. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  3576. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  3577. const int64_t i13 = i03 % ne13;
  3578. const int64_t i12 = i02 % ne12;
  3579. const int64_t i11 = i01 % ne11;
  3580. const int64_t nr0 = ne00 / ne10;
  3581. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  3582. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  3583. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  3584. for (int64_t r = 0; r < nr0; ++r) {
  3585. #ifdef GGML_USE_ACCELERATE
  3586. vDSP_vadd(src0_ptr + r*ne10, 1, src1_ptr, 1, dst_ptr + r*ne10, 1, ne10);
  3587. #else
  3588. ggml_vec_add_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  3589. #endif
  3590. }
  3591. }
  3592. } else {
  3593. // src1 is not contiguous
  3594. for (int ir = ir0; ir < ir1; ++ir) {
  3595. // src1 is broadcastable across src0 and dst in i1, i2, i3
  3596. const int64_t i03 = ir/(ne02*ne01);
  3597. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  3598. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  3599. const int64_t i13 = i03 % ne13;
  3600. const int64_t i12 = i02 % ne12;
  3601. const int64_t i11 = i01 % ne11;
  3602. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  3603. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  3604. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  3605. const int64_t i10 = i0 % ne10;
  3606. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  3607. dst_ptr[i0] = src0_ptr[i0] + *src1_ptr;
  3608. }
  3609. }
  3610. }
  3611. }
  3612. static void ggml_compute_forward_add_f16_f32(
  3613. const struct ggml_compute_params * params,
  3614. struct ggml_tensor * dst) {
  3615. const struct ggml_tensor * src0 = dst->src[0];
  3616. const struct ggml_tensor * src1 = dst->src[1];
  3617. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3618. const int ith = params->ith;
  3619. const int nth = params->nth;
  3620. const int nr = ggml_nrows(src0);
  3621. GGML_TENSOR_BINARY_OP_LOCALS
  3622. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3623. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3624. if (dst->type == GGML_TYPE_F32) {
  3625. GGML_ASSERT( nb0 == sizeof(float));
  3626. }
  3627. else {
  3628. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3629. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3630. }
  3631. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3632. // rows per thread
  3633. const int dr = (nr + nth - 1)/nth;
  3634. // row range for this thread
  3635. const int ir0 = dr*ith;
  3636. const int ir1 = MIN(ir0 + dr, nr);
  3637. if (nb10 == sizeof(float)) {
  3638. if (dst->type == GGML_TYPE_F16) {
  3639. for (int ir = ir0; ir < ir1; ++ir) {
  3640. // src0, src1 and dst are same shape => same indices
  3641. const int i3 = ir/(ne2*ne1);
  3642. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3643. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3644. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3645. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3646. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3647. for (int i = 0; i < ne0; i++) {
  3648. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + src1_ptr[i]);
  3649. }
  3650. }
  3651. } else {
  3652. for (int ir = ir0; ir < ir1; ++ir) {
  3653. // src0, src1 and dst are same shape => same indices
  3654. const int i3 = ir/(ne2*ne1);
  3655. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3656. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3657. float * dst_ptr = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3658. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3659. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3660. for (int i = 0; i < ne0; i++) {
  3661. dst_ptr[i] = GGML_FP16_TO_FP32(src0_ptr[i]) + src1_ptr[i];
  3662. }
  3663. }
  3664. }
  3665. }
  3666. else {
  3667. // src1 is not contiguous
  3668. GGML_ABORT("fatal error");
  3669. }
  3670. }
  3671. static void ggml_compute_forward_add_bf16_f32(
  3672. const struct ggml_compute_params * params,
  3673. struct ggml_tensor * dst) {
  3674. const struct ggml_tensor * src0 = dst->src[0];
  3675. const struct ggml_tensor * src1 = dst->src[1];
  3676. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3677. const int ith = params->ith;
  3678. const int nth = params->nth;
  3679. const int nr = ggml_nrows(src0);
  3680. GGML_TENSOR_BINARY_OP_LOCALS
  3681. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  3682. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3683. if (dst->type == GGML_TYPE_F32) {
  3684. GGML_ASSERT( nb0 == sizeof(float));
  3685. }
  3686. else {
  3687. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  3688. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  3689. }
  3690. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  3691. // rows per thread
  3692. const int dr = (nr + nth - 1)/nth;
  3693. // row range for this thread
  3694. const int ir0 = dr*ith;
  3695. const int ir1 = MIN(ir0 + dr, nr);
  3696. if (nb10 == sizeof(float)) {
  3697. if (dst->type == GGML_TYPE_BF16) {
  3698. for (int ir = ir0; ir < ir1; ++ir) {
  3699. // src0, src1 and dst are same shape => same indices
  3700. const int i3 = ir/(ne2*ne1);
  3701. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3702. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3703. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3704. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3705. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3706. for (int i = 0; i < ne0; i++) {
  3707. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + src1_ptr[i]);
  3708. }
  3709. }
  3710. } else {
  3711. for (int ir = ir0; ir < ir1; ++ir) {
  3712. // src0, src1 and dst are same shape => same indices
  3713. const int i3 = ir/(ne2*ne1);
  3714. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3715. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3716. float * dst_ptr = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3717. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3718. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3719. for (int i = 0; i < ne0; i++) {
  3720. dst_ptr[i] = GGML_BF16_TO_FP32(src0_ptr[i]) + src1_ptr[i];
  3721. }
  3722. }
  3723. }
  3724. }
  3725. else {
  3726. // src1 is not contiguous
  3727. GGML_ABORT("fatal error");
  3728. }
  3729. }
  3730. static void ggml_compute_forward_add_f16_f16(
  3731. const struct ggml_compute_params * params,
  3732. struct ggml_tensor * dst) {
  3733. const struct ggml_tensor * src0 = dst->src[0];
  3734. const struct ggml_tensor * src1 = dst->src[1];
  3735. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3736. const int ith = params->ith;
  3737. const int nth = params->nth;
  3738. const int nr = ggml_nrows(src0);
  3739. GGML_TENSOR_BINARY_OP_LOCALS
  3740. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3741. GGML_ASSERT(src1->type == GGML_TYPE_F16);
  3742. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3743. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3744. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3745. // rows per thread
  3746. const int dr = (nr + nth - 1)/nth;
  3747. // row range for this thread
  3748. const int ir0 = dr*ith;
  3749. const int ir1 = MIN(ir0 + dr, nr);
  3750. if (nb10 == sizeof(ggml_fp16_t)) {
  3751. for (int ir = ir0; ir < ir1; ++ir) {
  3752. // src0, src1 and dst are same shape => same indices
  3753. const int i3 = ir/(ne2*ne1);
  3754. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3755. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3756. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3757. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3758. ggml_fp16_t * src1_ptr = (ggml_fp16_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3759. for (int i = 0; i < ne0; i++) {
  3760. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + GGML_FP16_TO_FP32(src1_ptr[i]));
  3761. }
  3762. }
  3763. }
  3764. else {
  3765. // src1 is not contiguous
  3766. GGML_ABORT("fatal error");
  3767. }
  3768. }
  3769. static void ggml_compute_forward_add_bf16_bf16(
  3770. const struct ggml_compute_params * params,
  3771. struct ggml_tensor * dst) {
  3772. const struct ggml_tensor * src0 = dst->src[0];
  3773. const struct ggml_tensor * src1 = dst->src[1];
  3774. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3775. const int ith = params->ith;
  3776. const int nth = params->nth;
  3777. const int nr = ggml_nrows(src0);
  3778. GGML_TENSOR_BINARY_OP_LOCALS
  3779. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  3780. GGML_ASSERT(src1->type == GGML_TYPE_BF16);
  3781. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  3782. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  3783. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  3784. // rows per thread
  3785. const int dr = (nr + nth - 1)/nth;
  3786. // row range for this thread
  3787. const int ir0 = dr*ith;
  3788. const int ir1 = MIN(ir0 + dr, nr);
  3789. if (nb10 == sizeof(ggml_bf16_t)) {
  3790. for (int ir = ir0; ir < ir1; ++ir) {
  3791. // src0, src1 and dst are same shape => same indices
  3792. const int i3 = ir/(ne2*ne1);
  3793. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3794. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3795. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3796. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3797. ggml_bf16_t * src1_ptr = (ggml_bf16_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3798. for (int i = 0; i < ne0; i++) {
  3799. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + GGML_BF16_TO_FP32(src1_ptr[i]));
  3800. }
  3801. }
  3802. }
  3803. else {
  3804. // src1 is not contiguous
  3805. GGML_ABORT("fatal error");
  3806. }
  3807. }
  3808. static void ggml_compute_forward_add_q_f32(
  3809. const struct ggml_compute_params * params,
  3810. struct ggml_tensor * dst) {
  3811. const struct ggml_tensor * src0 = dst->src[0];
  3812. const struct ggml_tensor * src1 = dst->src[1];
  3813. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3814. const int nr = ggml_nrows(src0);
  3815. GGML_TENSOR_BINARY_OP_LOCALS
  3816. const int ith = params->ith;
  3817. const int nth = params->nth;
  3818. const enum ggml_type type = src0->type;
  3819. const enum ggml_type dtype = dst->type;
  3820. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  3821. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dtype)->from_float;
  3822. // we don't support permuted src0 or src1
  3823. GGML_ASSERT(nb00 == ggml_type_size(type));
  3824. GGML_ASSERT(nb10 == sizeof(float));
  3825. // dst cannot be transposed or permuted
  3826. GGML_ASSERT(nb0 <= nb1);
  3827. GGML_ASSERT(nb1 <= nb2);
  3828. GGML_ASSERT(nb2 <= nb3);
  3829. GGML_ASSERT(ggml_is_quantized(src0->type));
  3830. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3831. // rows per thread
  3832. const int dr = (nr + nth - 1)/nth;
  3833. // row range for this thread
  3834. const int ir0 = dr*ith;
  3835. const int ir1 = MIN(ir0 + dr, nr);
  3836. float * wdata = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
  3837. for (int ir = ir0; ir < ir1; ++ir) {
  3838. // src0 indices
  3839. const int i03 = ir/(ne02*ne01);
  3840. const int i02 = (ir - i03*ne02*ne01)/ne01;
  3841. const int i01 = (ir - i03*ne02*ne01 - i02*ne01);
  3842. // src1 and dst are same shape as src0 => same indices
  3843. const int i13 = i03;
  3844. const int i12 = i02;
  3845. const int i11 = i01;
  3846. const int i3 = i03;
  3847. const int i2 = i02;
  3848. const int i1 = i01;
  3849. void * src0_row = (void *) ((char *) src0->data + (i01*nb01 + i02*nb02 + i03*nb03));
  3850. float * src1_row = (float *)((char *) src1->data + (i11*nb11 + i12*nb12 + i13*nb13));
  3851. void * dst_row = (void *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  3852. assert(ne00 % 32 == 0);
  3853. // unquantize row from src0 to temp buffer
  3854. dequantize_row_q(src0_row, wdata, ne00);
  3855. // add src1
  3856. ggml_vec_acc_f32(ne00, wdata, src1_row);
  3857. // quantize row to dst
  3858. if (quantize_row_q != NULL) {
  3859. quantize_row_q(wdata, dst_row, ne00);
  3860. } else {
  3861. memcpy(dst_row, wdata, ne0*nb0);
  3862. }
  3863. }
  3864. }
  3865. static void ggml_compute_forward_add(
  3866. const struct ggml_compute_params * params,
  3867. struct ggml_tensor * dst) {
  3868. const struct ggml_tensor * src0 = dst->src[0];
  3869. const struct ggml_tensor * src1 = dst->src[1];
  3870. switch (src0->type) {
  3871. case GGML_TYPE_F32:
  3872. {
  3873. if (src1->type == GGML_TYPE_F32) {
  3874. ggml_compute_forward_add_f32(params, dst);
  3875. }
  3876. else {
  3877. GGML_ABORT("fatal error");
  3878. }
  3879. } break;
  3880. case GGML_TYPE_F16:
  3881. {
  3882. if (src1->type == GGML_TYPE_F16) {
  3883. ggml_compute_forward_add_f16_f16(params, dst);
  3884. }
  3885. else if (src1->type == GGML_TYPE_F32) {
  3886. ggml_compute_forward_add_f16_f32(params, dst);
  3887. }
  3888. else {
  3889. GGML_ABORT("fatal error");
  3890. }
  3891. } break;
  3892. case GGML_TYPE_BF16:
  3893. {
  3894. if (src1->type == GGML_TYPE_BF16) {
  3895. ggml_compute_forward_add_bf16_bf16(params, dst);
  3896. }
  3897. else if (src1->type == GGML_TYPE_F32) {
  3898. ggml_compute_forward_add_bf16_f32(params, dst);
  3899. }
  3900. else {
  3901. GGML_ABORT("fatal error");
  3902. }
  3903. } break;
  3904. case GGML_TYPE_Q4_0:
  3905. case GGML_TYPE_Q4_1:
  3906. case GGML_TYPE_Q5_0:
  3907. case GGML_TYPE_Q5_1:
  3908. case GGML_TYPE_Q8_0:
  3909. case GGML_TYPE_Q2_K:
  3910. case GGML_TYPE_Q3_K:
  3911. case GGML_TYPE_Q4_K:
  3912. case GGML_TYPE_Q5_K:
  3913. case GGML_TYPE_Q6_K:
  3914. case GGML_TYPE_TQ1_0:
  3915. case GGML_TYPE_TQ2_0:
  3916. case GGML_TYPE_IQ2_XXS:
  3917. case GGML_TYPE_IQ2_XS:
  3918. case GGML_TYPE_IQ3_XXS:
  3919. case GGML_TYPE_IQ1_S:
  3920. case GGML_TYPE_IQ1_M:
  3921. case GGML_TYPE_IQ4_NL:
  3922. case GGML_TYPE_IQ4_XS:
  3923. case GGML_TYPE_IQ3_S:
  3924. case GGML_TYPE_IQ2_S:
  3925. {
  3926. ggml_compute_forward_add_q_f32(params, dst);
  3927. } break;
  3928. default:
  3929. {
  3930. GGML_ABORT("fatal error");
  3931. }
  3932. }
  3933. }
  3934. // ggml_compute_forward_add1
  3935. static void ggml_compute_forward_add1_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. const int ith = params->ith;
  3943. const int nth = params->nth;
  3944. const int nr = ggml_nrows(src0);
  3945. GGML_TENSOR_UNARY_OP_LOCALS
  3946. GGML_ASSERT( nb0 == sizeof(float));
  3947. GGML_ASSERT(nb00 == sizeof(float));
  3948. // rows per thread
  3949. const int dr = (nr + nth - 1)/nth;
  3950. // row range for this thread
  3951. const int ir0 = dr*ith;
  3952. const int ir1 = MIN(ir0 + dr, nr);
  3953. for (int ir = ir0; ir < ir1; ++ir) {
  3954. // src0 and dst are same shape => same indices
  3955. const int i3 = ir/(ne2*ne1);
  3956. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3957. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3958. #ifdef GGML_USE_ACCELERATE
  3959. UNUSED(ggml_vec_add1_f32);
  3960. vDSP_vadd(
  3961. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), 1,
  3962. (float *) ((char *) src1->data), 0,
  3963. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), 1,
  3964. ne0);
  3965. #else
  3966. ggml_vec_add1_f32(ne0,
  3967. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ),
  3968. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01),
  3969. *(float *) src1->data);
  3970. #endif
  3971. }
  3972. }
  3973. static void ggml_compute_forward_add1_f16_f32(
  3974. const struct ggml_compute_params * params,
  3975. struct ggml_tensor * dst) {
  3976. const struct ggml_tensor * src0 = dst->src[0];
  3977. const struct ggml_tensor * src1 = dst->src[1];
  3978. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  3979. GGML_ASSERT(ggml_is_scalar(src1));
  3980. // scalar to add
  3981. const float v = *(float *) src1->data;
  3982. const int ith = params->ith;
  3983. const int nth = params->nth;
  3984. const int nr = ggml_nrows(src0);
  3985. GGML_TENSOR_UNARY_OP_LOCALS
  3986. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3987. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3988. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3989. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3990. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3991. // rows per thread
  3992. const int dr = (nr + nth - 1)/nth;
  3993. // row range for this thread
  3994. const int ir0 = dr*ith;
  3995. const int ir1 = MIN(ir0 + dr, nr);
  3996. for (int ir = ir0; ir < ir1; ++ir) {
  3997. // src0 and dst are same shape => same indices
  3998. const int i3 = ir/(ne2*ne1);
  3999. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4000. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4001. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  4002. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4003. for (int i = 0; i < ne0; i++) {
  4004. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + v);
  4005. }
  4006. }
  4007. }
  4008. static void ggml_compute_forward_add1_f16_f16(
  4009. const struct ggml_compute_params * params,
  4010. struct ggml_tensor * dst) {
  4011. const struct ggml_tensor * src0 = dst->src[0];
  4012. const struct ggml_tensor * src1 = dst->src[1];
  4013. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4014. GGML_ASSERT(ggml_is_scalar(src1));
  4015. // scalar to add
  4016. const float v = GGML_FP16_TO_FP32(*(ggml_fp16_t *) src1->data);
  4017. const int ith = params->ith;
  4018. const int nth = params->nth;
  4019. const int nr = ggml_nrows(src0);
  4020. GGML_TENSOR_UNARY_OP_LOCALS
  4021. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4022. GGML_ASSERT(src1->type == GGML_TYPE_F16);
  4023. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  4024. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  4025. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  4026. // rows per thread
  4027. const int dr = (nr + nth - 1)/nth;
  4028. // row range for this thread
  4029. const int ir0 = dr*ith;
  4030. const int ir1 = MIN(ir0 + dr, nr);
  4031. for (int ir = ir0; ir < ir1; ++ir) {
  4032. // src0 and dst are same shape => same indices
  4033. const int i3 = ir/(ne2*ne1);
  4034. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4035. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4036. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  4037. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4038. for (int i = 0; i < ne0; i++) {
  4039. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + v);
  4040. }
  4041. }
  4042. }
  4043. static void ggml_compute_forward_add1_q_f32(
  4044. const struct ggml_compute_params * params,
  4045. struct ggml_tensor * dst) {
  4046. const struct ggml_tensor * src0 = dst->src[0];
  4047. const struct ggml_tensor * src1 = dst->src[1];
  4048. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4049. GGML_ASSERT(ggml_is_scalar(src1));
  4050. // scalar to add
  4051. const float v = *(float *) src1->data;
  4052. const int ith = params->ith;
  4053. const int nth = params->nth;
  4054. const int nr = ggml_nrows(src0);
  4055. GGML_TENSOR_UNARY_OP_LOCALS
  4056. const enum ggml_type type = src0->type;
  4057. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  4058. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(type)->from_float;
  4059. // we don't support permuted src0
  4060. GGML_ASSERT(nb00 == ggml_type_size(type));
  4061. // dst cannot be transposed or permuted
  4062. GGML_ASSERT(nb0 <= nb1);
  4063. GGML_ASSERT(nb1 <= nb2);
  4064. GGML_ASSERT(nb2 <= nb3);
  4065. GGML_ASSERT(ggml_is_quantized(src0->type));
  4066. GGML_ASSERT(dst->type == src0->type);
  4067. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4068. // rows per thread
  4069. const int dr = (nr + nth - 1)/nth;
  4070. // row range for this thread
  4071. const int ir0 = dr*ith;
  4072. const int ir1 = MIN(ir0 + dr, nr);
  4073. float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith;
  4074. for (int ir = ir0; ir < ir1; ++ir) {
  4075. // src0 and dst are same shape => same indices
  4076. const int i3 = ir/(ne2*ne1);
  4077. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4078. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4079. void * src0_row = (void *) ((char *) src0->data + (i1*nb01 + i2*nb02 + i3*nb03));
  4080. void * dst_row = (void *) ((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb0 ));
  4081. assert(ne0 % 32 == 0);
  4082. // unquantize row from src0 to temp buffer
  4083. dequantize_row_q(src0_row, wdata, ne0);
  4084. // add src1
  4085. ggml_vec_acc1_f32(ne0, wdata, v);
  4086. // quantize row to dst
  4087. quantize_row_q(wdata, dst_row, ne0);
  4088. }
  4089. }
  4090. static void ggml_compute_forward_add1_bf16_f32(
  4091. const struct ggml_compute_params * params,
  4092. struct ggml_tensor * dst) {
  4093. const struct ggml_tensor * src0 = dst->src[0];
  4094. const struct ggml_tensor * src1 = dst->src[1];
  4095. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4096. GGML_ASSERT(ggml_is_scalar(src1));
  4097. // scalar to add
  4098. const float v = *(float *) src1->data;
  4099. const int ith = params->ith;
  4100. const int nth = params->nth;
  4101. const int nr = ggml_nrows(src0);
  4102. GGML_TENSOR_UNARY_OP_LOCALS
  4103. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  4104. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4105. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  4106. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  4107. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  4108. // rows per thread
  4109. const int dr = (nr + nth - 1)/nth;
  4110. // row range for this thread
  4111. const int ir0 = dr*ith;
  4112. const int ir1 = MIN(ir0 + dr, nr);
  4113. for (int ir = ir0; ir < ir1; ++ir) {
  4114. // src0 and dst are same shape => same indices
  4115. const int i3 = ir/(ne2*ne1);
  4116. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4117. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4118. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  4119. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4120. for (int i = 0; i < ne0; i++) {
  4121. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v);
  4122. }
  4123. }
  4124. }
  4125. static void ggml_compute_forward_add1_bf16_bf16(
  4126. const struct ggml_compute_params * params,
  4127. struct ggml_tensor * dst) {
  4128. const struct ggml_tensor * src0 = dst->src[0];
  4129. const struct ggml_tensor * src1 = dst->src[1];
  4130. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4131. GGML_ASSERT(ggml_is_scalar(src1));
  4132. // scalar to add
  4133. const float v = GGML_BF16_TO_FP32(*(ggml_bf16_t *) src1->data);
  4134. const int ith = params->ith;
  4135. const int nth = params->nth;
  4136. const int nr = ggml_nrows(src0);
  4137. GGML_TENSOR_UNARY_OP_LOCALS
  4138. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  4139. GGML_ASSERT(src1->type == GGML_TYPE_BF16);
  4140. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  4141. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  4142. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  4143. // rows per thread
  4144. const int dr = (nr + nth - 1)/nth;
  4145. // row range for this thread
  4146. const int ir0 = dr*ith;
  4147. const int ir1 = MIN(ir0 + dr, nr);
  4148. for (int ir = ir0; ir < ir1; ++ir) {
  4149. // src0 and dst are same shape => same indices
  4150. const int i3 = ir/(ne2*ne1);
  4151. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4152. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4153. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  4154. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4155. for (int i = 0; i < ne0; i++) {
  4156. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v);
  4157. }
  4158. }
  4159. }
  4160. static void ggml_compute_forward_add1(
  4161. const struct ggml_compute_params * params,
  4162. struct ggml_tensor * dst) {
  4163. const struct ggml_tensor * src0 = dst->src[0];
  4164. const struct ggml_tensor * src1 = dst->src[1];
  4165. switch (src0->type) {
  4166. case GGML_TYPE_F32:
  4167. {
  4168. ggml_compute_forward_add1_f32(params, dst);
  4169. } break;
  4170. case GGML_TYPE_F16:
  4171. {
  4172. if (src1->type == GGML_TYPE_F16) {
  4173. ggml_compute_forward_add1_f16_f16(params, dst);
  4174. }
  4175. else if (src1->type == GGML_TYPE_F32) {
  4176. ggml_compute_forward_add1_f16_f32(params, dst);
  4177. }
  4178. else {
  4179. GGML_ABORT("fatal error");
  4180. }
  4181. } break;
  4182. case GGML_TYPE_BF16:
  4183. {
  4184. if (src1->type == GGML_TYPE_BF16) {
  4185. ggml_compute_forward_add1_bf16_bf16(params, dst);
  4186. }
  4187. else if (src1->type == GGML_TYPE_F32) {
  4188. ggml_compute_forward_add1_bf16_f32(params, dst);
  4189. }
  4190. else {
  4191. GGML_ABORT("fatal error");
  4192. }
  4193. } break;
  4194. case GGML_TYPE_Q4_0:
  4195. case GGML_TYPE_Q4_1:
  4196. case GGML_TYPE_Q5_0:
  4197. case GGML_TYPE_Q5_1:
  4198. case GGML_TYPE_Q8_0:
  4199. case GGML_TYPE_Q8_1:
  4200. case GGML_TYPE_Q2_K:
  4201. case GGML_TYPE_Q3_K:
  4202. case GGML_TYPE_Q4_K:
  4203. case GGML_TYPE_Q5_K:
  4204. case GGML_TYPE_Q6_K:
  4205. case GGML_TYPE_TQ1_0:
  4206. case GGML_TYPE_TQ2_0:
  4207. case GGML_TYPE_IQ2_XXS:
  4208. case GGML_TYPE_IQ2_XS:
  4209. case GGML_TYPE_IQ3_XXS:
  4210. case GGML_TYPE_IQ1_S:
  4211. case GGML_TYPE_IQ1_M:
  4212. case GGML_TYPE_IQ4_NL:
  4213. case GGML_TYPE_IQ4_XS:
  4214. case GGML_TYPE_IQ3_S:
  4215. case GGML_TYPE_IQ2_S:
  4216. {
  4217. ggml_compute_forward_add1_q_f32(params, dst);
  4218. } break;
  4219. default:
  4220. {
  4221. GGML_ABORT("fatal error");
  4222. }
  4223. }
  4224. }
  4225. // ggml_compute_forward_acc
  4226. static void ggml_compute_forward_acc_f32(
  4227. const struct ggml_compute_params * params,
  4228. struct ggml_tensor * dst) {
  4229. const struct ggml_tensor * src0 = dst->src[0];
  4230. const struct ggml_tensor * src1 = dst->src[1];
  4231. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4232. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  4233. // view src0 and dst with these strides and data offset inbytes during acc
  4234. // nb0 is implicitly element_size because src0 and dst are contiguous
  4235. size_t nb1 = ((int32_t *) dst->op_params)[0];
  4236. size_t nb2 = ((int32_t *) dst->op_params)[1];
  4237. size_t nb3 = ((int32_t *) dst->op_params)[2];
  4238. size_t offset = ((int32_t *) dst->op_params)[3];
  4239. bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  4240. if (!inplace) {
  4241. if (params->ith == 0) {
  4242. // memcpy needs to be synchronized across threads to avoid race conditions.
  4243. // => do it in INIT phase
  4244. memcpy(
  4245. ((char *) dst->data),
  4246. ((char *) src0->data),
  4247. ggml_nbytes(dst));
  4248. }
  4249. ggml_barrier(params->threadpool);
  4250. }
  4251. const int ith = params->ith;
  4252. const int nth = params->nth;
  4253. const int nr = ggml_nrows(src1);
  4254. const int nc = src1->ne[0];
  4255. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne)
  4256. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  4257. // src0 and dst as viewed during acc
  4258. const size_t nb0 = ggml_element_size(src0);
  4259. const size_t nb00 = nb0;
  4260. const size_t nb01 = nb1;
  4261. const size_t nb02 = nb2;
  4262. const size_t nb03 = nb3;
  4263. 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));
  4264. 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));
  4265. GGML_ASSERT(nb10 == sizeof(float));
  4266. // rows per thread
  4267. const int dr = (nr + nth - 1)/nth;
  4268. // row range for this thread
  4269. const int ir0 = dr*ith;
  4270. const int ir1 = MIN(ir0 + dr, nr);
  4271. for (int ir = ir0; ir < ir1; ++ir) {
  4272. // src0 and dst are viewed with shape of src1 and offset
  4273. // => same indices
  4274. const int i3 = ir/(ne12*ne11);
  4275. const int i2 = (ir - i3*ne12*ne11)/ne11;
  4276. const int i1 = (ir - i3*ne12*ne11 - i2*ne11);
  4277. #ifdef GGML_USE_ACCELERATE
  4278. vDSP_vadd(
  4279. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset), 1,
  4280. (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11), 1,
  4281. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), 1, nc);
  4282. #else
  4283. ggml_vec_add_f32(nc,
  4284. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset),
  4285. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset),
  4286. (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
  4287. #endif
  4288. }
  4289. }
  4290. static void ggml_compute_forward_acc(
  4291. const struct ggml_compute_params * params,
  4292. struct ggml_tensor * dst) {
  4293. const struct ggml_tensor * src0 = dst->src[0];
  4294. switch (src0->type) {
  4295. case GGML_TYPE_F32:
  4296. {
  4297. ggml_compute_forward_acc_f32(params, dst);
  4298. } break;
  4299. case GGML_TYPE_F16:
  4300. case GGML_TYPE_BF16:
  4301. case GGML_TYPE_Q4_0:
  4302. case GGML_TYPE_Q4_1:
  4303. case GGML_TYPE_Q5_0:
  4304. case GGML_TYPE_Q5_1:
  4305. case GGML_TYPE_Q8_0:
  4306. case GGML_TYPE_Q8_1:
  4307. case GGML_TYPE_Q2_K:
  4308. case GGML_TYPE_Q3_K:
  4309. case GGML_TYPE_Q4_K:
  4310. case GGML_TYPE_Q5_K:
  4311. case GGML_TYPE_Q6_K:
  4312. case GGML_TYPE_TQ1_0:
  4313. case GGML_TYPE_TQ2_0:
  4314. case GGML_TYPE_IQ2_XXS:
  4315. case GGML_TYPE_IQ2_XS:
  4316. case GGML_TYPE_IQ3_XXS:
  4317. case GGML_TYPE_IQ1_S:
  4318. case GGML_TYPE_IQ1_M:
  4319. case GGML_TYPE_IQ4_NL:
  4320. case GGML_TYPE_IQ4_XS:
  4321. case GGML_TYPE_IQ3_S:
  4322. case GGML_TYPE_IQ2_S:
  4323. default:
  4324. {
  4325. GGML_ABORT("fatal error");
  4326. }
  4327. }
  4328. }
  4329. // ggml_compute_forward_sub
  4330. static void ggml_compute_forward_sub_f32(
  4331. const struct ggml_compute_params * params,
  4332. struct ggml_tensor * dst) {
  4333. const struct ggml_tensor * src0 = dst->src[0];
  4334. const struct ggml_tensor * src1 = dst->src[1];
  4335. assert(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  4336. const int ith = params->ith;
  4337. const int nth = params->nth;
  4338. const int nr = ggml_nrows(src0);
  4339. GGML_TENSOR_BINARY_OP_LOCALS
  4340. GGML_ASSERT( nb0 == sizeof(float));
  4341. GGML_ASSERT(nb00 == sizeof(float));
  4342. // rows per thread
  4343. const int dr = (nr + nth - 1)/nth;
  4344. // row range for this thread
  4345. const int ir0 = dr*ith;
  4346. const int ir1 = MIN(ir0 + dr, nr);
  4347. if (nb10 == sizeof(float)) {
  4348. for (int ir = ir0; ir < ir1; ++ir) {
  4349. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4350. const int64_t i03 = ir/(ne02*ne01);
  4351. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4352. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4353. const int64_t i13 = i03 % ne13;
  4354. const int64_t i12 = i02 % ne12;
  4355. const int64_t i11 = i01 % ne11;
  4356. const int64_t nr0 = ne00 / ne10;
  4357. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4358. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4359. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  4360. for (int64_t r = 0; r < nr0; ++r) {
  4361. #ifdef GGML_USE_ACCELERATE
  4362. vDSP_vsub(src1_ptr, 1, src0_ptr + r*ne10, 1, dst_ptr + r*ne10, 1, ne10);
  4363. #else
  4364. ggml_vec_sub_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  4365. #endif
  4366. }
  4367. }
  4368. } else {
  4369. // src1 is not contiguous
  4370. for (int ir = ir0; ir < ir1; ++ir) {
  4371. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4372. const int64_t i03 = ir/(ne02*ne01);
  4373. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4374. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4375. const int64_t i13 = i03 % ne13;
  4376. const int64_t i12 = i02 % ne12;
  4377. const int64_t i11 = i01 % ne11;
  4378. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4379. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4380. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  4381. const int64_t i10 = i0 % ne10;
  4382. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  4383. dst_ptr[i0] = src0_ptr[i0] - *src1_ptr;
  4384. }
  4385. }
  4386. }
  4387. }
  4388. static void ggml_compute_forward_sub(
  4389. const struct ggml_compute_params * params,
  4390. struct ggml_tensor * dst) {
  4391. const struct ggml_tensor * src0 = dst->src[0];
  4392. switch (src0->type) {
  4393. case GGML_TYPE_F32:
  4394. {
  4395. ggml_compute_forward_sub_f32(params, dst);
  4396. } break;
  4397. default:
  4398. {
  4399. GGML_ABORT("fatal error");
  4400. }
  4401. }
  4402. }
  4403. // ggml_compute_forward_mul
  4404. static void ggml_compute_forward_mul_f32(
  4405. const struct ggml_compute_params * params,
  4406. struct ggml_tensor * dst) {
  4407. const struct ggml_tensor * src0 = dst->src[0];
  4408. const struct ggml_tensor * src1 = dst->src[1];
  4409. GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  4410. const int ith = params->ith;
  4411. const int nth = params->nth;
  4412. const int64_t nr = ggml_nrows(src0);
  4413. GGML_TENSOR_BINARY_OP_LOCALS
  4414. GGML_ASSERT( nb0 == sizeof(float));
  4415. GGML_ASSERT(nb00 == sizeof(float));
  4416. if (nb10 == sizeof(float)) {
  4417. for (int64_t ir = ith; ir < nr; ir += nth) {
  4418. // src0 and dst are same shape => same indices
  4419. const int64_t i03 = ir/(ne02*ne01);
  4420. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4421. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4422. const int64_t i13 = i03 % ne13;
  4423. const int64_t i12 = i02 % ne12;
  4424. const int64_t i11 = i01 % ne11;
  4425. const int64_t nr0 = ne00 / ne10;
  4426. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4427. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4428. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  4429. for (int64_t r = 0 ; r < nr0; ++r) {
  4430. #ifdef GGML_USE_ACCELERATE
  4431. UNUSED(ggml_vec_mul_f32);
  4432. vDSP_vmul(src0_ptr + r*ne10, 1, src1_ptr, 1, dst_ptr + r*ne10, 1, ne10);
  4433. #else
  4434. ggml_vec_mul_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  4435. #endif
  4436. }
  4437. }
  4438. } else {
  4439. // src1 is not contiguous
  4440. for (int64_t ir = ith; ir < nr; ir += nth) {
  4441. // src0 and dst are same shape => same indices
  4442. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4443. const int64_t i03 = ir/(ne02*ne01);
  4444. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4445. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4446. const int64_t i13 = i03 % ne13;
  4447. const int64_t i12 = i02 % ne12;
  4448. const int64_t i11 = i01 % ne11;
  4449. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4450. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4451. for (int64_t i0 = 0; i0 < ne00; ++i0) {
  4452. const int64_t i10 = i0 % ne10;
  4453. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  4454. dst_ptr[i0] = src0_ptr[i0] * (*src1_ptr);
  4455. }
  4456. }
  4457. }
  4458. }
  4459. static void ggml_compute_forward_mul(
  4460. const struct ggml_compute_params * params,
  4461. struct ggml_tensor * dst) {
  4462. const struct ggml_tensor * src0 = dst->src[0];
  4463. const struct ggml_tensor * src1 = dst->src[1];
  4464. GGML_ASSERT(src1->type == GGML_TYPE_F32 && "only f32 src1 supported for now");
  4465. switch (src0->type) {
  4466. case GGML_TYPE_F32:
  4467. {
  4468. ggml_compute_forward_mul_f32(params, dst);
  4469. } break;
  4470. default:
  4471. {
  4472. GGML_ABORT("fatal error");
  4473. }
  4474. }
  4475. }
  4476. // ggml_compute_forward_div
  4477. static void ggml_compute_forward_div_f32(
  4478. const struct ggml_compute_params * params,
  4479. struct ggml_tensor * dst) {
  4480. const struct ggml_tensor * src0 = dst->src[0];
  4481. const struct ggml_tensor * src1 = dst->src[1];
  4482. GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  4483. const int ith = params->ith;
  4484. const int nth = params->nth;
  4485. const int64_t nr = ggml_nrows(src0);
  4486. GGML_TENSOR_BINARY_OP_LOCALS
  4487. GGML_ASSERT( nb0 == sizeof(float));
  4488. GGML_ASSERT(nb00 == sizeof(float));
  4489. if (nb10 == sizeof(float)) {
  4490. for (int64_t ir = ith; ir < nr; ir += nth) {
  4491. // src0 and dst are same shape => same indices
  4492. const int64_t i03 = ir/(ne02*ne01);
  4493. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4494. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4495. const int64_t i13 = i03 % ne13;
  4496. const int64_t i12 = i02 % ne12;
  4497. const int64_t i11 = i01 % ne11;
  4498. const int64_t nr0 = ne00 / ne10;
  4499. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4500. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4501. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  4502. for (int64_t r = 0; r < nr0; ++r) {
  4503. #ifdef GGML_USE_ACCELERATE
  4504. UNUSED(ggml_vec_div_f32);
  4505. vDSP_vdiv(src1_ptr, 1, src0_ptr + r*ne10, 1, dst_ptr + r*ne10, 1, ne10);
  4506. #else
  4507. ggml_vec_div_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  4508. #endif
  4509. }
  4510. }
  4511. } else {
  4512. // src1 is not contiguous
  4513. for (int64_t ir = ith; ir < nr; ir += nth) {
  4514. // src0 and dst are same shape => same indices
  4515. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4516. const int64_t i03 = ir/(ne02*ne01);
  4517. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4518. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4519. const int64_t i13 = i03 % ne13;
  4520. const int64_t i12 = i02 % ne12;
  4521. const int64_t i11 = i01 % ne11;
  4522. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4523. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4524. for (int64_t i0 = 0; i0 < ne00; ++i0) {
  4525. const int64_t i10 = i0 % ne10;
  4526. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  4527. dst_ptr[i0] = src0_ptr[i0] / (*src1_ptr);
  4528. }
  4529. }
  4530. }
  4531. }
  4532. static void ggml_compute_forward_div(
  4533. const struct ggml_compute_params * params,
  4534. struct ggml_tensor * dst) {
  4535. const struct ggml_tensor * src0 = dst->src[0];
  4536. switch (src0->type) {
  4537. case GGML_TYPE_F32:
  4538. {
  4539. ggml_compute_forward_div_f32(params, dst);
  4540. } break;
  4541. default:
  4542. {
  4543. GGML_ABORT("fatal error");
  4544. }
  4545. }
  4546. }
  4547. // ggml_compute_forward_sqr
  4548. static void ggml_compute_forward_sqr_f32(
  4549. const struct ggml_compute_params * params,
  4550. struct ggml_tensor * dst) {
  4551. const struct ggml_tensor * src0 = dst->src[0];
  4552. if (params->ith != 0) {
  4553. return;
  4554. }
  4555. assert(ggml_are_same_shape(src0, dst));
  4556. const int n = ggml_nrows(src0);
  4557. const int nc = src0->ne[0];
  4558. assert( dst->nb[0] == sizeof(float));
  4559. assert(src0->nb[0] == sizeof(float));
  4560. for (int i = 0; i < n; i++) {
  4561. ggml_vec_sqr_f32(nc,
  4562. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4563. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4564. }
  4565. }
  4566. static void ggml_compute_forward_sqr(
  4567. const struct ggml_compute_params * params,
  4568. struct ggml_tensor * dst) {
  4569. const struct ggml_tensor * src0 = dst->src[0];
  4570. switch (src0->type) {
  4571. case GGML_TYPE_F32:
  4572. {
  4573. ggml_compute_forward_sqr_f32(params, dst);
  4574. } break;
  4575. default:
  4576. {
  4577. GGML_ABORT("fatal error");
  4578. }
  4579. }
  4580. }
  4581. // ggml_compute_forward_sqrt
  4582. static void ggml_compute_forward_sqrt_f32(
  4583. const struct ggml_compute_params * params,
  4584. struct ggml_tensor * dst) {
  4585. const struct ggml_tensor * src0 = dst->src[0];
  4586. if (params->ith != 0) {
  4587. return;
  4588. }
  4589. assert(ggml_are_same_shape(src0, dst));
  4590. const int n = ggml_nrows(src0);
  4591. const int nc = src0->ne[0];
  4592. assert( dst->nb[0] == sizeof(float));
  4593. assert(src0->nb[0] == sizeof(float));
  4594. for (int i = 0; i < n; i++) {
  4595. ggml_vec_sqrt_f32(nc,
  4596. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4597. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4598. }
  4599. }
  4600. static void ggml_compute_forward_sqrt(
  4601. const struct ggml_compute_params * params,
  4602. struct ggml_tensor * dst) {
  4603. const struct ggml_tensor * src0 = dst->src[0];
  4604. switch (src0->type) {
  4605. case GGML_TYPE_F32:
  4606. {
  4607. ggml_compute_forward_sqrt_f32(params, dst);
  4608. } break;
  4609. default:
  4610. {
  4611. GGML_ABORT("fatal error");
  4612. }
  4613. }
  4614. }
  4615. // ggml_compute_forward_log
  4616. static void ggml_compute_forward_log_f32(
  4617. const struct ggml_compute_params * params,
  4618. struct ggml_tensor * dst) {
  4619. const struct ggml_tensor * src0 = dst->src[0];
  4620. if (params->ith != 0) {
  4621. return;
  4622. }
  4623. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4624. const int n = ggml_nrows(src0);
  4625. const int nc = src0->ne[0];
  4626. GGML_ASSERT( dst->nb[0] == sizeof(float));
  4627. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4628. for (int i = 0; i < n; i++) {
  4629. ggml_vec_log_f32(nc,
  4630. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4631. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4632. }
  4633. }
  4634. static void ggml_compute_forward_log(
  4635. const struct ggml_compute_params * params,
  4636. struct ggml_tensor * dst) {
  4637. const struct ggml_tensor * src0 = dst->src[0];
  4638. switch (src0->type) {
  4639. case GGML_TYPE_F32:
  4640. {
  4641. ggml_compute_forward_log_f32(params, dst);
  4642. } break;
  4643. default:
  4644. {
  4645. GGML_ABORT("fatal error");
  4646. }
  4647. }
  4648. }
  4649. // ggml_compute_forward_sin
  4650. static void ggml_compute_forward_sin_f32(
  4651. const struct ggml_compute_params * params,
  4652. struct ggml_tensor * dst) {
  4653. const struct ggml_tensor * src0 = dst->src[0];
  4654. if (params->ith != 0) {
  4655. return;
  4656. }
  4657. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4658. const int n = ggml_nrows(src0);
  4659. const int nc = src0->ne[0];
  4660. GGML_ASSERT( dst->nb[0] == sizeof(float));
  4661. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4662. for (int i = 0; i < n; i++) {
  4663. ggml_vec_sin_f32(nc,
  4664. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4665. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4666. }
  4667. }
  4668. static void ggml_compute_forward_sin(
  4669. const struct ggml_compute_params * params,
  4670. struct ggml_tensor * dst) {
  4671. const struct ggml_tensor * src0 = dst->src[0];
  4672. switch (src0->type) {
  4673. case GGML_TYPE_F32:
  4674. {
  4675. ggml_compute_forward_sin_f32(params, dst);
  4676. } break;
  4677. default:
  4678. {
  4679. GGML_ABORT("fatal error");
  4680. }
  4681. }
  4682. }
  4683. // ggml_compute_forward_cos
  4684. static void ggml_compute_forward_cos_f32(
  4685. const struct ggml_compute_params * params,
  4686. struct ggml_tensor * dst) {
  4687. const struct ggml_tensor * src0 = dst->src[0];
  4688. if (params->ith != 0) {
  4689. return;
  4690. }
  4691. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4692. const int n = ggml_nrows(src0);
  4693. const int nc = src0->ne[0];
  4694. GGML_ASSERT( dst->nb[0] == sizeof(float));
  4695. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4696. for (int i = 0; i < n; i++) {
  4697. ggml_vec_cos_f32(nc,
  4698. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4699. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4700. }
  4701. }
  4702. static void ggml_compute_forward_cos(
  4703. const struct ggml_compute_params * params,
  4704. struct ggml_tensor * dst) {
  4705. const struct ggml_tensor * src0 = dst->src[0];
  4706. switch (src0->type) {
  4707. case GGML_TYPE_F32:
  4708. {
  4709. ggml_compute_forward_cos_f32(params, dst);
  4710. } break;
  4711. default:
  4712. {
  4713. GGML_ABORT("fatal error");
  4714. }
  4715. }
  4716. }
  4717. // ggml_compute_forward_sum
  4718. static void ggml_compute_forward_sum_f32(
  4719. const struct ggml_compute_params * params,
  4720. struct ggml_tensor * dst) {
  4721. const struct ggml_tensor * src0 = dst->src[0];
  4722. if (params->ith != 0) {
  4723. return;
  4724. }
  4725. assert(ggml_is_scalar(dst));
  4726. assert(src0->nb[0] == sizeof(float));
  4727. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  4728. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb)
  4729. ggml_float sum = 0;
  4730. ggml_float row_sum = 0;
  4731. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4732. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4733. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4734. ggml_vec_sum_f32_ggf(ne00,
  4735. &row_sum,
  4736. (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03));
  4737. sum += row_sum;
  4738. }
  4739. }
  4740. }
  4741. ((float *) dst->data)[0] = sum;
  4742. }
  4743. static void ggml_compute_forward_sum_f16(
  4744. const struct ggml_compute_params * params,
  4745. struct ggml_tensor * dst) {
  4746. const struct ggml_tensor * src0 = dst->src[0];
  4747. if (params->ith != 0) {
  4748. return;
  4749. }
  4750. assert(ggml_is_scalar(dst));
  4751. assert(src0->nb[0] == sizeof(ggml_fp16_t));
  4752. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  4753. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb)
  4754. float sum = 0;
  4755. float row_sum = 0;
  4756. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4757. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4758. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4759. ggml_vec_sum_f16_ggf(ne00,
  4760. &row_sum,
  4761. (ggml_fp16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03));
  4762. sum += row_sum;
  4763. }
  4764. }
  4765. }
  4766. ((ggml_fp16_t *) dst->data)[0] = GGML_FP32_TO_FP16(sum);
  4767. }
  4768. static void ggml_compute_forward_sum_bf16(
  4769. const struct ggml_compute_params * params,
  4770. struct ggml_tensor * dst) {
  4771. const struct ggml_tensor * src0 = dst->src[0];
  4772. if (params->ith != 0) {
  4773. return;
  4774. }
  4775. assert(ggml_is_scalar(dst));
  4776. assert(src0->nb[0] == sizeof(ggml_bf16_t));
  4777. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  4778. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb)
  4779. float sum = 0;
  4780. float row_sum = 0;
  4781. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4782. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4783. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4784. ggml_vec_sum_bf16_ggf(ne00,
  4785. &row_sum,
  4786. (ggml_bf16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03));
  4787. sum += row_sum;
  4788. }
  4789. }
  4790. }
  4791. ((ggml_bf16_t *) dst->data)[0] = GGML_FP32_TO_BF16(sum);
  4792. }
  4793. static void ggml_compute_forward_sum(
  4794. const struct ggml_compute_params * params,
  4795. struct ggml_tensor * dst) {
  4796. const struct ggml_tensor * src0 = dst->src[0];
  4797. switch (src0->type) {
  4798. case GGML_TYPE_F32:
  4799. {
  4800. ggml_compute_forward_sum_f32(params, dst);
  4801. } break;
  4802. case GGML_TYPE_F16:
  4803. {
  4804. ggml_compute_forward_sum_f16(params, dst);
  4805. } break;
  4806. case GGML_TYPE_BF16:
  4807. {
  4808. ggml_compute_forward_sum_bf16(params, dst);
  4809. } break;
  4810. default:
  4811. {
  4812. GGML_ABORT("fatal error");
  4813. }
  4814. }
  4815. }
  4816. // ggml_compute_forward_sum_rows
  4817. static void ggml_compute_forward_sum_rows_f32(
  4818. const struct ggml_compute_params * params,
  4819. struct ggml_tensor * dst) {
  4820. const struct ggml_tensor * src0 = dst->src[0];
  4821. if (params->ith != 0) {
  4822. return;
  4823. }
  4824. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4825. GGML_ASSERT(dst->nb[0] == sizeof(float));
  4826. GGML_TENSOR_UNARY_OP_LOCALS
  4827. GGML_ASSERT(ne0 == 1);
  4828. GGML_ASSERT(ne1 == ne01);
  4829. GGML_ASSERT(ne2 == ne02);
  4830. GGML_ASSERT(ne3 == ne03);
  4831. for (int64_t i3 = 0; i3 < ne03; i3++) {
  4832. for (int64_t i2 = 0; i2 < ne02; i2++) {
  4833. for (int64_t i1 = 0; i1 < ne01; i1++) {
  4834. float * src_row = (float *) ((char *) src0->data + i1*nb01 + i2*nb02 + i3*nb03);
  4835. float * dst_row = (float *) ((char *) dst->data + i1*nb1 + i2*nb2 + i3*nb3);
  4836. float row_sum = 0;
  4837. ggml_vec_sum_f32(ne00, &row_sum, src_row);
  4838. dst_row[0] = row_sum;
  4839. }
  4840. }
  4841. }
  4842. }
  4843. static void ggml_compute_forward_sum_rows(
  4844. const struct ggml_compute_params * params,
  4845. struct ggml_tensor * dst) {
  4846. const struct ggml_tensor * src0 = dst->src[0];
  4847. switch (src0->type) {
  4848. case GGML_TYPE_F32:
  4849. {
  4850. ggml_compute_forward_sum_rows_f32(params, dst);
  4851. } break;
  4852. default:
  4853. {
  4854. GGML_ABORT("fatal error");
  4855. }
  4856. }
  4857. }
  4858. // ggml_compute_forward_mean
  4859. static void ggml_compute_forward_mean_f32(
  4860. const struct ggml_compute_params * params,
  4861. struct ggml_tensor * dst) {
  4862. const struct ggml_tensor * src0 = dst->src[0];
  4863. if (params->ith != 0) {
  4864. return;
  4865. }
  4866. assert(src0->nb[0] == sizeof(float));
  4867. GGML_TENSOR_UNARY_OP_LOCALS
  4868. assert(ne0 == 1);
  4869. assert(ne1 == ne01);
  4870. assert(ne2 == ne02);
  4871. assert(ne3 == ne03);
  4872. UNUSED(ne0);
  4873. UNUSED(ne1);
  4874. UNUSED(ne2);
  4875. UNUSED(ne3);
  4876. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4877. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4878. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4879. ggml_vec_sum_f32(ne00,
  4880. (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  4881. (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03));
  4882. *(float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3) /= (float) ne00;
  4883. }
  4884. }
  4885. }
  4886. }
  4887. static void ggml_compute_forward_mean(
  4888. const struct ggml_compute_params * params,
  4889. struct ggml_tensor * dst) {
  4890. const struct ggml_tensor * src0 = dst->src[0];
  4891. switch (src0->type) {
  4892. case GGML_TYPE_F32:
  4893. {
  4894. ggml_compute_forward_mean_f32(params, dst);
  4895. } break;
  4896. default:
  4897. {
  4898. GGML_ABORT("fatal error");
  4899. }
  4900. }
  4901. }
  4902. // ggml_compute_forward_argmax
  4903. static void ggml_compute_forward_argmax_f32(
  4904. const struct ggml_compute_params * params,
  4905. struct ggml_tensor * dst) {
  4906. const struct ggml_tensor * src0 = dst->src[0];
  4907. if (params->ith != 0) {
  4908. return;
  4909. }
  4910. assert(src0->nb[0] == sizeof(float));
  4911. assert(dst->nb[0] == sizeof(float));
  4912. const int64_t ne00 = src0->ne[0];
  4913. const int64_t ne01 = src0->ne[1];
  4914. const size_t nb01 = src0->nb[1];
  4915. const size_t nb0 = dst->nb[0];
  4916. for (int64_t i1 = 0; i1 < ne01; i1++) {
  4917. float * src = (float *) ((char *) src0->data + i1*nb01);
  4918. int32_t * dst_ = (int32_t *) ((char *) dst->data + i1*nb0);
  4919. int v = 0;
  4920. ggml_vec_argmax_f32(ne00, &v, src);
  4921. dst_[0] = v;
  4922. }
  4923. }
  4924. static void ggml_compute_forward_argmax(
  4925. const struct ggml_compute_params * params,
  4926. struct ggml_tensor * dst) {
  4927. const struct ggml_tensor * src0 = dst->src[0];
  4928. switch (src0->type) {
  4929. case GGML_TYPE_F32:
  4930. {
  4931. ggml_compute_forward_argmax_f32(params, dst);
  4932. } break;
  4933. default:
  4934. {
  4935. GGML_ABORT("fatal error");
  4936. }
  4937. }
  4938. }
  4939. // ggml_compute_forward_count_equal
  4940. static void ggml_compute_forward_count_equal_i32(
  4941. const struct ggml_compute_params * params,
  4942. struct ggml_tensor * dst) {
  4943. const struct ggml_tensor * src0 = dst->src[0];
  4944. const struct ggml_tensor * src1 = dst->src[1];
  4945. GGML_TENSOR_BINARY_OP_LOCALS;
  4946. GGML_ASSERT(src0->type == GGML_TYPE_I32);
  4947. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  4948. GGML_ASSERT(ggml_are_same_shape(src0, src1));
  4949. GGML_ASSERT(ggml_is_scalar(dst));
  4950. GGML_ASSERT(dst->type == GGML_TYPE_I64);
  4951. const int64_t nr = ggml_nrows(src0);
  4952. const int ith = params->ith;
  4953. const int nth = params->nth;
  4954. int64_t * sums = (int64_t *) params->wdata;
  4955. int64_t sum_thread = 0;
  4956. // rows per thread
  4957. const int64_t dr = (nr + nth - 1)/nth;
  4958. // row range for this thread
  4959. const int64_t ir0 = dr*ith;
  4960. const int64_t ir1 = MIN(ir0 + dr, nr);
  4961. for (int64_t ir = ir0; ir < ir1; ++ir) {
  4962. const int64_t i03 = ir / (ne02*ne01);
  4963. const int64_t i02 = (ir - i03*ne03) / ne01;
  4964. const int64_t i01 = ir - i03*ne03 - i02*ne02;
  4965. const char * data0 = (const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01;
  4966. const char * data1 = (const char *) src1->data + i03*nb13 + i02*nb12 + i01*nb11;
  4967. for (int64_t i00 = 0; i00 < ne00; ++i00) {
  4968. const int32_t val0 = *((const int32_t *) (data0 + i00*nb00));
  4969. const int32_t val1 = *((const int32_t *) (data1 + i00*nb10));
  4970. sum_thread += val0 == val1;
  4971. }
  4972. }
  4973. if (ith != 0) {
  4974. sums[ith] = sum_thread;
  4975. }
  4976. ggml_barrier(params->threadpool);
  4977. if (ith != 0) {
  4978. return;
  4979. }
  4980. for (int ith_other = 1; ith_other < nth; ++ith_other) {
  4981. sum_thread += sums[ith_other];
  4982. }
  4983. *((int64_t *) dst->data) = sum_thread;
  4984. }
  4985. static void ggml_compute_forward_count_equal(
  4986. const struct ggml_compute_params * params,
  4987. struct ggml_tensor * dst) {
  4988. const struct ggml_tensor * src0 = dst->src[0];
  4989. switch (src0->type) {
  4990. case GGML_TYPE_I32:
  4991. {
  4992. ggml_compute_forward_count_equal_i32(params, dst);
  4993. } break;
  4994. default:
  4995. {
  4996. GGML_ABORT("fatal error");
  4997. }
  4998. }
  4999. }
  5000. // ggml_compute_forward_repeat
  5001. static void ggml_compute_forward_repeat_f32(
  5002. const struct ggml_compute_params * params,
  5003. struct ggml_tensor * dst) {
  5004. const struct ggml_tensor * src0 = dst->src[0];
  5005. if (params->ith != 0) {
  5006. return;
  5007. }
  5008. GGML_ASSERT(ggml_can_repeat(src0, dst));
  5009. GGML_TENSOR_UNARY_OP_LOCALS
  5010. // guaranteed to be an integer due to the check in ggml_can_repeat
  5011. const int nr0 = (int)(ne0/ne00);
  5012. const int nr1 = (int)(ne1/ne01);
  5013. const int nr2 = (int)(ne2/ne02);
  5014. const int nr3 = (int)(ne3/ne03);
  5015. // TODO: support for transposed / permuted tensors
  5016. GGML_ASSERT(nb0 == sizeof(float));
  5017. GGML_ASSERT(nb00 == sizeof(float));
  5018. // TODO: maybe this is not optimal?
  5019. for (int i3 = 0; i3 < nr3; i3++) {
  5020. for (int k3 = 0; k3 < ne03; k3++) {
  5021. for (int i2 = 0; i2 < nr2; i2++) {
  5022. for (int k2 = 0; k2 < ne02; k2++) {
  5023. for (int i1 = 0; i1 < nr1; i1++) {
  5024. for (int k1 = 0; k1 < ne01; k1++) {
  5025. for (int i0 = 0; i0 < nr0; i0++) {
  5026. ggml_vec_cpy_f32(ne00,
  5027. (float *) ((char *) dst->data + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0),
  5028. (float *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01));
  5029. }
  5030. }
  5031. }
  5032. }
  5033. }
  5034. }
  5035. }
  5036. }
  5037. static void ggml_compute_forward_repeat_f16(
  5038. const struct ggml_compute_params * params,
  5039. struct ggml_tensor * dst) {
  5040. const struct ggml_tensor * src0 = dst->src[0];
  5041. if (params->ith != 0) {
  5042. return;
  5043. }
  5044. GGML_ASSERT(ggml_can_repeat(src0, dst));
  5045. GGML_TENSOR_UNARY_OP_LOCALS
  5046. // guaranteed to be an integer due to the check in ggml_can_repeat
  5047. const int nr0 = (int)(ne0/ne00);
  5048. const int nr1 = (int)(ne1/ne01);
  5049. const int nr2 = (int)(ne2/ne02);
  5050. const int nr3 = (int)(ne3/ne03);
  5051. // TODO: support for transposed / permuted tensors
  5052. GGML_ASSERT(nb0 == sizeof(ggml_fp16_t));
  5053. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  5054. // TODO: maybe this is not optimal?
  5055. for (int i3 = 0; i3 < nr3; i3++) {
  5056. for (int k3 = 0; k3 < ne03; k3++) {
  5057. for (int i2 = 0; i2 < nr2; i2++) {
  5058. for (int k2 = 0; k2 < ne02; k2++) {
  5059. for (int i1 = 0; i1 < nr1; i1++) {
  5060. for (int k1 = 0; k1 < ne01; k1++) {
  5061. for (int i0 = 0; i0 < nr0; i0++) {
  5062. 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);
  5063. ggml_fp16_t * x = (ggml_fp16_t *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01);
  5064. // ggml_vec_cpy_f16(ne00, y, x)
  5065. for (int i = 0; i < ne00; ++i) {
  5066. y[i] = x[i];
  5067. }
  5068. }
  5069. }
  5070. }
  5071. }
  5072. }
  5073. }
  5074. }
  5075. }
  5076. static void ggml_compute_forward_repeat(
  5077. const struct ggml_compute_params * params,
  5078. struct ggml_tensor * dst) {
  5079. const struct ggml_tensor * src0 = dst->src[0];
  5080. switch (src0->type) {
  5081. case GGML_TYPE_F16:
  5082. case GGML_TYPE_BF16:
  5083. case GGML_TYPE_I16:
  5084. {
  5085. ggml_compute_forward_repeat_f16(params, dst);
  5086. } break;
  5087. case GGML_TYPE_F32:
  5088. case GGML_TYPE_I32:
  5089. {
  5090. ggml_compute_forward_repeat_f32(params, dst);
  5091. } break;
  5092. default:
  5093. {
  5094. GGML_ABORT("fatal error");
  5095. }
  5096. }
  5097. }
  5098. // ggml_compute_forward_repeat_back
  5099. static void ggml_compute_forward_repeat_back_f32(
  5100. const struct ggml_compute_params * params,
  5101. struct ggml_tensor * dst) {
  5102. const struct ggml_tensor * src0 = dst->src[0];
  5103. if (params->ith != 0) {
  5104. return;
  5105. }
  5106. GGML_ASSERT(ggml_can_repeat(dst, src0));
  5107. GGML_TENSOR_UNARY_OP_LOCALS
  5108. // guaranteed to be an integer due to the check in ggml_can_repeat
  5109. const int nr0 = (int)(ne00/ne0);
  5110. const int nr1 = (int)(ne01/ne1);
  5111. const int nr2 = (int)(ne02/ne2);
  5112. const int nr3 = (int)(ne03/ne3);
  5113. // TODO: support for transposed / permuted tensors
  5114. GGML_ASSERT(nb0 == sizeof(float));
  5115. GGML_ASSERT(nb00 == sizeof(float));
  5116. if (ggml_is_contiguous(dst)) {
  5117. ggml_vec_set_f32(ne0*ne1*ne2*ne3, dst->data, 0);
  5118. } else {
  5119. for (int k3 = 0; k3 < ne3; k3++) {
  5120. for (int k2 = 0; k2 < ne2; k2++) {
  5121. for (int k1 = 0; k1 < ne1; k1++) {
  5122. ggml_vec_set_f32(ne0,
  5123. (float *) ((char *) dst->data + k1*nb1 + k2*nb2 + k3*nb3),
  5124. 0);
  5125. }
  5126. }
  5127. }
  5128. }
  5129. // TODO: maybe this is not optimal?
  5130. for (int i3 = 0; i3 < nr3; i3++) {
  5131. for (int k3 = 0; k3 < ne3; k3++) {
  5132. for (int i2 = 0; i2 < nr2; i2++) {
  5133. for (int k2 = 0; k2 < ne2; k2++) {
  5134. for (int i1 = 0; i1 < nr1; i1++) {
  5135. for (int k1 = 0; k1 < ne1; k1++) {
  5136. for (int i0 = 0; i0 < nr0; i0++) {
  5137. ggml_vec_acc_f32(ne0,
  5138. (float *) ((char *) dst->data + ( k3)*nb3 + ( k2)*nb2 + ( k1)*nb1),
  5139. (float *) ((char *) src0->data + (i3*ne3 + k3)*nb03 + (i2*ne2 + k2)*nb02 + (i1*ne1 + k1)*nb01 + (i0*ne0)*nb00));
  5140. }
  5141. }
  5142. }
  5143. }
  5144. }
  5145. }
  5146. }
  5147. }
  5148. static void ggml_compute_forward_repeat_back(
  5149. const struct ggml_compute_params * params,
  5150. struct ggml_tensor * dst) {
  5151. const struct ggml_tensor * src0 = dst->src[0];
  5152. switch (src0->type) {
  5153. case GGML_TYPE_F32:
  5154. {
  5155. ggml_compute_forward_repeat_back_f32(params, dst);
  5156. } break;
  5157. default:
  5158. {
  5159. GGML_ABORT("fatal error");
  5160. }
  5161. }
  5162. }
  5163. // ggml_compute_forward_concat
  5164. static void ggml_compute_forward_concat_f32(
  5165. const struct ggml_compute_params * params,
  5166. struct ggml_tensor * dst) {
  5167. const struct ggml_tensor * src0 = dst->src[0];
  5168. const struct ggml_tensor * src1 = dst->src[1];
  5169. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5170. const int ith = params->ith;
  5171. const int nth = params->nth;
  5172. GGML_TENSOR_BINARY_OP_LOCALS
  5173. const int32_t dim = ggml_get_op_params_i32(dst, 0);
  5174. GGML_ASSERT(dim >= 0 && dim < 4);
  5175. int64_t o[4] = {0, 0, 0, 0};
  5176. o[dim] = src0->ne[dim];
  5177. const float * x;
  5178. // TODO: smarter multi-theading
  5179. for (int i3 = 0; i3 < ne3; i3++) {
  5180. for (int i2 = ith; i2 < ne2; i2 += nth) {
  5181. for (int i1 = 0; i1 < ne1; i1++) {
  5182. for (int i0 = 0; i0 < ne0; i0++) {
  5183. if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
  5184. x = (const float *) ((const char *)src0->data + (i0 )*nb00 + (i1 )*nb01 + (i2 )*nb02 + (i3 )*nb03);
  5185. } else {
  5186. x = (const float *) ((const char *)src1->data + (i0 - o[0])*nb10 + (i1 - o[1])*nb11 + (i2 - o[2])*nb12 + (i3 - o[3])*nb13);
  5187. }
  5188. float * y = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3);
  5189. *y = *x;
  5190. }
  5191. }
  5192. }
  5193. }
  5194. }
  5195. static void ggml_compute_forward_concat(
  5196. const struct ggml_compute_params * params,
  5197. struct ggml_tensor * dst) {
  5198. const struct ggml_tensor * src0 = dst->src[0];
  5199. switch (src0->type) {
  5200. case GGML_TYPE_F32:
  5201. case GGML_TYPE_I32:
  5202. {
  5203. ggml_compute_forward_concat_f32(params, dst);
  5204. } break;
  5205. default:
  5206. {
  5207. GGML_ABORT("fatal error");
  5208. }
  5209. }
  5210. }
  5211. // ggml_compute_forward_abs
  5212. static void ggml_compute_forward_abs_f32(
  5213. const struct ggml_compute_params * params,
  5214. struct ggml_tensor * dst) {
  5215. const struct ggml_tensor * src0 = dst->src[0];
  5216. if (params->ith != 0) {
  5217. return;
  5218. }
  5219. assert(ggml_is_contiguous_1(src0));
  5220. assert(ggml_is_contiguous_1(dst));
  5221. assert(ggml_are_same_shape(src0, dst));
  5222. const int n = ggml_nrows(src0);
  5223. const int nc = src0->ne[0];
  5224. for (int i = 0; i < n; i++) {
  5225. ggml_vec_abs_f32(nc,
  5226. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5227. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5228. }
  5229. }
  5230. static void ggml_compute_forward_abs(
  5231. const struct ggml_compute_params * params,
  5232. struct ggml_tensor * dst) {
  5233. const struct ggml_tensor * src0 = dst->src[0];
  5234. switch (src0->type) {
  5235. case GGML_TYPE_F32:
  5236. {
  5237. ggml_compute_forward_abs_f32(params, dst);
  5238. } break;
  5239. default:
  5240. {
  5241. GGML_ABORT("fatal error");
  5242. }
  5243. }
  5244. }
  5245. // ggml_compute_forward_sgn
  5246. static void ggml_compute_forward_sgn_f32(
  5247. const struct ggml_compute_params * params,
  5248. struct ggml_tensor * dst) {
  5249. const struct ggml_tensor * src0 = dst->src[0];
  5250. if (params->ith != 0) {
  5251. return;
  5252. }
  5253. assert(ggml_is_contiguous_1(src0));
  5254. assert(ggml_is_contiguous_1(dst));
  5255. assert(ggml_are_same_shape(src0, dst));
  5256. const int n = ggml_nrows(src0);
  5257. const int nc = src0->ne[0];
  5258. for (int i = 0; i < n; i++) {
  5259. ggml_vec_sgn_f32(nc,
  5260. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5261. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5262. }
  5263. }
  5264. static void ggml_compute_forward_sgn(
  5265. const struct ggml_compute_params * params,
  5266. struct ggml_tensor * dst) {
  5267. const struct ggml_tensor * src0 = dst->src[0];
  5268. switch (src0->type) {
  5269. case GGML_TYPE_F32:
  5270. {
  5271. ggml_compute_forward_sgn_f32(params, dst);
  5272. } break;
  5273. default:
  5274. {
  5275. GGML_ABORT("fatal error");
  5276. }
  5277. }
  5278. }
  5279. // ggml_compute_forward_neg
  5280. static void ggml_compute_forward_neg_f32(
  5281. const struct ggml_compute_params * params,
  5282. struct ggml_tensor * dst) {
  5283. const struct ggml_tensor * src0 = dst->src[0];
  5284. if (params->ith != 0) {
  5285. return;
  5286. }
  5287. assert(ggml_is_contiguous_1(src0));
  5288. assert(ggml_is_contiguous_1(dst));
  5289. assert(ggml_are_same_shape(src0, dst));
  5290. const int n = ggml_nrows(src0);
  5291. const int nc = src0->ne[0];
  5292. for (int i = 0; i < n; i++) {
  5293. ggml_vec_neg_f32(nc,
  5294. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5295. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5296. }
  5297. }
  5298. static void ggml_compute_forward_neg(
  5299. const struct ggml_compute_params * params,
  5300. struct ggml_tensor * dst) {
  5301. const struct ggml_tensor * src0 = dst->src[0];
  5302. switch (src0->type) {
  5303. case GGML_TYPE_F32:
  5304. {
  5305. ggml_compute_forward_neg_f32(params, dst);
  5306. } break;
  5307. default:
  5308. {
  5309. GGML_ABORT("fatal error");
  5310. }
  5311. }
  5312. }
  5313. // ggml_compute_forward_step
  5314. static void ggml_compute_forward_step_f32(
  5315. const struct ggml_compute_params * params,
  5316. struct ggml_tensor * dst) {
  5317. const struct ggml_tensor * src0 = dst->src[0];
  5318. if (params->ith != 0) {
  5319. return;
  5320. }
  5321. assert(ggml_is_contiguous_1(src0));
  5322. assert(ggml_is_contiguous_1(dst));
  5323. assert(ggml_are_same_shape(src0, dst));
  5324. const int n = ggml_nrows(src0);
  5325. const int nc = src0->ne[0];
  5326. for (int i = 0; i < n; i++) {
  5327. ggml_vec_step_f32(nc,
  5328. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5329. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5330. }
  5331. }
  5332. static void ggml_compute_forward_step(
  5333. const struct ggml_compute_params * params,
  5334. struct ggml_tensor * dst) {
  5335. const struct ggml_tensor * src0 = dst->src[0];
  5336. switch (src0->type) {
  5337. case GGML_TYPE_F32:
  5338. {
  5339. ggml_compute_forward_step_f32(params, dst);
  5340. } break;
  5341. default:
  5342. {
  5343. GGML_ABORT("fatal error");
  5344. }
  5345. }
  5346. }
  5347. // ggml_compute_forward_tanh
  5348. static void ggml_compute_forward_tanh_f32(
  5349. const struct ggml_compute_params * params,
  5350. struct ggml_tensor * dst) {
  5351. const struct ggml_tensor * src0 = dst->src[0];
  5352. if (params->ith != 0) {
  5353. return;
  5354. }
  5355. assert(ggml_is_contiguous_1(src0));
  5356. assert(ggml_is_contiguous_1(dst));
  5357. assert(ggml_are_same_shape(src0, dst));
  5358. const int n = ggml_nrows(src0);
  5359. const int nc = src0->ne[0];
  5360. for (int i = 0; i < n; i++) {
  5361. ggml_vec_tanh_f32(nc,
  5362. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5363. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5364. }
  5365. }
  5366. static void ggml_compute_forward_tanh(
  5367. const struct ggml_compute_params * params,
  5368. struct ggml_tensor * dst) {
  5369. const struct ggml_tensor * src0 = dst->src[0];
  5370. switch (src0->type) {
  5371. case GGML_TYPE_F32:
  5372. {
  5373. ggml_compute_forward_tanh_f32(params, dst);
  5374. } break;
  5375. default:
  5376. {
  5377. GGML_ABORT("fatal error");
  5378. }
  5379. }
  5380. }
  5381. // ggml_compute_forward_elu
  5382. static void ggml_compute_forward_elu_f32(
  5383. const struct ggml_compute_params * params,
  5384. struct ggml_tensor * dst) {
  5385. const struct ggml_tensor * src0 = dst->src[0];
  5386. if (params->ith != 0) {
  5387. return;
  5388. }
  5389. assert(ggml_is_contiguous_1(src0));
  5390. assert(ggml_is_contiguous_1(dst));
  5391. assert(ggml_are_same_shape(src0, dst));
  5392. const int n = ggml_nrows(src0);
  5393. const int nc = src0->ne[0];
  5394. for (int i = 0; i < n; i++) {
  5395. ggml_vec_elu_f32(nc,
  5396. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5397. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5398. }
  5399. }
  5400. static void ggml_compute_forward_elu(
  5401. const struct ggml_compute_params * params,
  5402. struct ggml_tensor * dst) {
  5403. const struct ggml_tensor * src0 = dst->src[0];
  5404. switch (src0->type) {
  5405. case GGML_TYPE_F32:
  5406. {
  5407. ggml_compute_forward_elu_f32(params, dst);
  5408. } break;
  5409. default:
  5410. {
  5411. GGML_ABORT("fatal error");
  5412. }
  5413. }
  5414. }
  5415. // ggml_compute_forward_relu
  5416. static void ggml_compute_forward_relu_f32(
  5417. const struct ggml_compute_params * params,
  5418. struct ggml_tensor * dst) {
  5419. const struct ggml_tensor * src0 = dst->src[0];
  5420. if (params->ith != 0) {
  5421. return;
  5422. }
  5423. assert(ggml_is_contiguous_1(src0));
  5424. assert(ggml_is_contiguous_1(dst));
  5425. assert(ggml_are_same_shape(src0, dst));
  5426. const int n = ggml_nrows(src0);
  5427. const int nc = src0->ne[0];
  5428. for (int i = 0; i < n; i++) {
  5429. ggml_vec_relu_f32(nc,
  5430. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5431. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5432. }
  5433. }
  5434. static void ggml_compute_forward_relu(
  5435. const struct ggml_compute_params * params,
  5436. struct ggml_tensor * dst) {
  5437. const struct ggml_tensor * src0 = dst->src[0];
  5438. switch (src0->type) {
  5439. case GGML_TYPE_F32:
  5440. {
  5441. ggml_compute_forward_relu_f32(params, dst);
  5442. } break;
  5443. default:
  5444. {
  5445. GGML_ABORT("fatal error");
  5446. }
  5447. }
  5448. }
  5449. // ggml_compute_forward_sigmoid
  5450. static void ggml_compute_forward_sigmoid_f32(
  5451. const struct ggml_compute_params * params,
  5452. struct ggml_tensor * dst) {
  5453. const struct ggml_tensor * src0 = dst->src[0];
  5454. if (params->ith != 0) {
  5455. return;
  5456. }
  5457. assert(ggml_is_contiguous_1(src0));
  5458. assert(ggml_is_contiguous_1(dst));
  5459. assert(ggml_are_same_shape(src0, dst));
  5460. const int n = ggml_nrows(src0);
  5461. const int nc = src0->ne[0];
  5462. for (int i = 0; i < n; i++) {
  5463. ggml_vec_sigmoid_f32(nc,
  5464. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5465. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5466. }
  5467. }
  5468. static void ggml_compute_forward_sigmoid(
  5469. const struct ggml_compute_params * params,
  5470. struct ggml_tensor * dst) {
  5471. const struct ggml_tensor * src0 = dst->src[0];
  5472. switch (src0->type) {
  5473. case GGML_TYPE_F32:
  5474. {
  5475. ggml_compute_forward_sigmoid_f32(params, dst);
  5476. } break;
  5477. default:
  5478. {
  5479. GGML_ABORT("fatal error");
  5480. }
  5481. }
  5482. }
  5483. // ggml_compute_forward_gelu
  5484. static void ggml_compute_forward_gelu_f32(
  5485. const struct ggml_compute_params * params,
  5486. struct ggml_tensor * dst) {
  5487. const struct ggml_tensor * src0 = dst->src[0];
  5488. assert(ggml_is_contiguous_1(src0));
  5489. assert(ggml_is_contiguous_1(dst));
  5490. assert(ggml_are_same_shape(src0, dst));
  5491. const int ith = params->ith;
  5492. const int nth = params->nth;
  5493. const int nc = src0->ne[0];
  5494. const int nr = ggml_nrows(src0);
  5495. // rows per thread
  5496. const int dr = (nr + nth - 1)/nth;
  5497. // row range for this thread
  5498. const int ir0 = dr*ith;
  5499. const int ir1 = MIN(ir0 + dr, nr);
  5500. for (int i1 = ir0; i1 < ir1; i1++) {
  5501. ggml_vec_gelu_f32(nc,
  5502. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5503. (float *) ((char *) src0->data + i1*(src0->nb[1])));
  5504. #ifndef NDEBUG
  5505. for (int k = 0; k < nc; k++) {
  5506. const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k];
  5507. UNUSED(x);
  5508. assert(!isnan(x));
  5509. assert(!isinf(x));
  5510. }
  5511. #endif
  5512. }
  5513. }
  5514. static void ggml_compute_forward_gelu(
  5515. const struct ggml_compute_params * params,
  5516. struct ggml_tensor * dst) {
  5517. const struct ggml_tensor * src0 = dst->src[0];
  5518. switch (src0->type) {
  5519. case GGML_TYPE_F32:
  5520. {
  5521. ggml_compute_forward_gelu_f32(params, dst);
  5522. } break;
  5523. default:
  5524. {
  5525. GGML_ABORT("fatal error");
  5526. }
  5527. }
  5528. }
  5529. // ggml_compute_forward_gelu_quick
  5530. static void ggml_compute_forward_gelu_quick_f32(
  5531. const struct ggml_compute_params * params,
  5532. struct ggml_tensor * dst) {
  5533. const struct ggml_tensor * src0 = dst->src[0];
  5534. assert(ggml_is_contiguous_1(src0));
  5535. assert(ggml_is_contiguous_1(dst));
  5536. assert(ggml_are_same_shape(src0, dst));
  5537. const int ith = params->ith;
  5538. const int nth = params->nth;
  5539. const int nc = src0->ne[0];
  5540. const int nr = ggml_nrows(src0);
  5541. // rows per thread
  5542. const int dr = (nr + nth - 1)/nth;
  5543. // row range for this thread
  5544. const int ir0 = dr*ith;
  5545. const int ir1 = MIN(ir0 + dr, nr);
  5546. for (int i1 = ir0; i1 < ir1; i1++) {
  5547. ggml_vec_gelu_quick_f32(nc,
  5548. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5549. (float *) ((char *) src0->data + i1*(src0->nb[1])));
  5550. #ifndef NDEBUG
  5551. for (int k = 0; k < nc; k++) {
  5552. const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k];
  5553. UNUSED(x);
  5554. assert(!isnan(x));
  5555. assert(!isinf(x));
  5556. }
  5557. #endif
  5558. }
  5559. }
  5560. static void ggml_compute_forward_gelu_quick(
  5561. const struct ggml_compute_params * params,
  5562. struct ggml_tensor * dst) {
  5563. const struct ggml_tensor * src0 = dst->src[0];
  5564. switch (src0->type) {
  5565. case GGML_TYPE_F32:
  5566. {
  5567. ggml_compute_forward_gelu_quick_f32(params, dst);
  5568. } break;
  5569. default:
  5570. {
  5571. GGML_ABORT("fatal error");
  5572. }
  5573. }
  5574. }
  5575. // ggml_compute_forward_silu
  5576. static void ggml_compute_forward_silu_f32(
  5577. const struct ggml_compute_params * params,
  5578. struct ggml_tensor * dst) {
  5579. const struct ggml_tensor * src0 = dst->src[0];
  5580. assert(ggml_is_contiguous_1(src0));
  5581. assert(ggml_is_contiguous_1(dst));
  5582. assert(ggml_are_same_shape(src0, dst));
  5583. const int ith = params->ith;
  5584. const int nth = params->nth;
  5585. const int nc = src0->ne[0];
  5586. const int nr = ggml_nrows(src0);
  5587. // rows per thread
  5588. const int dr = (nr + nth - 1)/nth;
  5589. // row range for this thread
  5590. const int ir0 = dr*ith;
  5591. const int ir1 = MIN(ir0 + dr, nr);
  5592. for (int i1 = ir0; i1 < ir1; i1++) {
  5593. ggml_vec_silu_f32(nc,
  5594. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5595. (float *) ((char *) src0->data + i1*(src0->nb[1])));
  5596. #ifndef NDEBUG
  5597. for (int k = 0; k < nc; k++) {
  5598. const float x = ((float *) ((char *) dst->data + i1*(dst->nb[1])))[k];
  5599. UNUSED(x);
  5600. assert(!isnan(x));
  5601. assert(!isinf(x));
  5602. }
  5603. #endif
  5604. }
  5605. }
  5606. static void ggml_compute_forward_silu(
  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_silu_f32(params, dst);
  5614. } break;
  5615. default:
  5616. {
  5617. GGML_ABORT("fatal error");
  5618. }
  5619. }
  5620. }
  5621. // ggml_compute_forward_leaky_relu
  5622. static void ggml_compute_forward_leaky_relu_f32(
  5623. const struct ggml_compute_params * params,
  5624. struct ggml_tensor * dst) {
  5625. const struct ggml_tensor * src0 = dst->src[0];
  5626. if (params->ith != 0) {
  5627. return;
  5628. }
  5629. assert(ggml_is_contiguous_1(src0));
  5630. assert(ggml_is_contiguous_1(dst));
  5631. assert(ggml_are_same_shape(src0, dst));
  5632. const int n = ggml_nrows(src0);
  5633. const int nc = src0->ne[0];
  5634. float negative_slope;
  5635. memcpy(&negative_slope, dst->op_params, sizeof(float));
  5636. assert(dst->nb[0] == sizeof(float));
  5637. assert(src0->nb[0] == sizeof(float));
  5638. for (int i = 0; i < n; i++) {
  5639. ggml_vec_leaky_relu_f32(nc,
  5640. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5641. (float *) ((char *) src0->data + i*(src0->nb[1])), negative_slope);
  5642. }
  5643. }
  5644. static void ggml_compute_forward_leaky_relu(
  5645. const struct ggml_compute_params * params,
  5646. struct ggml_tensor * dst) {
  5647. const struct ggml_tensor * src0 = dst->src[0];
  5648. switch (src0->type) {
  5649. case GGML_TYPE_F32:
  5650. {
  5651. ggml_compute_forward_leaky_relu_f32(params, dst);
  5652. } break;
  5653. default:
  5654. {
  5655. GGML_ABORT("fatal error");
  5656. }
  5657. }
  5658. }
  5659. // ggml_compute_forward_silu_back
  5660. static void ggml_compute_forward_silu_back_f32(
  5661. const struct ggml_compute_params * params,
  5662. struct ggml_tensor * dst) {
  5663. const struct ggml_tensor * grad = dst->src[0];
  5664. const struct ggml_tensor * src1 = dst->src[1];
  5665. assert(ggml_is_contiguous_1(grad));
  5666. assert(ggml_is_contiguous_1(src1));
  5667. assert(ggml_is_contiguous_1(dst));
  5668. assert(ggml_are_same_shape(src1, dst));
  5669. assert(ggml_are_same_shape(src1, grad));
  5670. const int ith = params->ith;
  5671. const int nth = params->nth;
  5672. const int nc = src1->ne[0];
  5673. const int nr = ggml_nrows(src1);
  5674. // rows per thread
  5675. const int dr = (nr + nth - 1)/nth;
  5676. // row range for this thread
  5677. const int ir0 = dr*ith;
  5678. const int ir1 = MIN(ir0 + dr, nr);
  5679. for (int i1 = ir0; i1 < ir1; i1++) {
  5680. ggml_vec_silu_backward_f32(nc,
  5681. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5682. (float *) ((char *) src1->data + i1*(src1->nb[1])),
  5683. (float *) ((char *) grad->data + i1*(grad->nb[1])));
  5684. #ifndef NDEBUG
  5685. for (int k = 0; k < nc; k++) {
  5686. const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k];
  5687. UNUSED(x);
  5688. assert(!isnan(x));
  5689. assert(!isinf(x));
  5690. }
  5691. #endif
  5692. }
  5693. }
  5694. static void ggml_compute_forward_silu_back(
  5695. const struct ggml_compute_params * params,
  5696. struct ggml_tensor * dst) {
  5697. const struct ggml_tensor * src0 = dst->src[0];
  5698. switch (src0->type) {
  5699. case GGML_TYPE_F32:
  5700. {
  5701. ggml_compute_forward_silu_back_f32(params, dst);
  5702. } break;
  5703. default:
  5704. {
  5705. GGML_ABORT("fatal error");
  5706. }
  5707. }
  5708. }
  5709. static void ggml_compute_forward_hardswish_f32(
  5710. const struct ggml_compute_params * params,
  5711. struct ggml_tensor * dst) {
  5712. const struct ggml_tensor * src0 = dst->src[0];
  5713. if (params->ith != 0) {
  5714. return;
  5715. }
  5716. assert(ggml_is_contiguous_1(src0));
  5717. assert(ggml_is_contiguous_1(dst));
  5718. assert(ggml_are_same_shape(src0, dst));
  5719. const int n = ggml_nrows(src0);
  5720. const int nc = src0->ne[0];
  5721. for (int i = 0; i < n; i++) {
  5722. ggml_vec_hardswish_f32(nc,
  5723. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5724. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5725. }
  5726. }
  5727. static void ggml_compute_forward_hardswish(
  5728. const struct ggml_compute_params * params,
  5729. struct ggml_tensor * dst) {
  5730. const struct ggml_tensor * src0 = dst->src[0];
  5731. switch (src0->type) {
  5732. case GGML_TYPE_F32:
  5733. {
  5734. ggml_compute_forward_hardswish_f32(params, dst);
  5735. } break;
  5736. default:
  5737. {
  5738. GGML_ABORT("fatal error");
  5739. }
  5740. }
  5741. }
  5742. static void ggml_compute_forward_hardsigmoid_f32(
  5743. const struct ggml_compute_params * params,
  5744. struct ggml_tensor * dst) {
  5745. const struct ggml_tensor * src0 = dst->src[0];
  5746. if (params->ith != 0) {
  5747. return;
  5748. }
  5749. assert(ggml_is_contiguous_1(src0));
  5750. assert(ggml_is_contiguous_1(dst));
  5751. assert(ggml_are_same_shape(src0, dst));
  5752. const int n = ggml_nrows(src0);
  5753. const int nc = src0->ne[0];
  5754. for (int i = 0; i < n; i++) {
  5755. ggml_vec_hardsigmoid_f32(nc,
  5756. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5757. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5758. }
  5759. }
  5760. static void ggml_compute_forward_hardsigmoid(
  5761. const struct ggml_compute_params * params,
  5762. struct ggml_tensor * dst) {
  5763. const struct ggml_tensor * src0 = dst->src[0];
  5764. switch (src0->type) {
  5765. case GGML_TYPE_F32:
  5766. {
  5767. ggml_compute_forward_hardsigmoid_f32(params, dst);
  5768. } break;
  5769. default:
  5770. {
  5771. GGML_ABORT("fatal error");
  5772. }
  5773. }
  5774. }
  5775. static void ggml_compute_forward_exp_f32(
  5776. const struct ggml_compute_params * params,
  5777. struct ggml_tensor * dst) {
  5778. const struct ggml_tensor * src0 = dst->src[0];
  5779. if (params->ith != 0) {
  5780. return;
  5781. }
  5782. assert(ggml_is_contiguous_1(src0));
  5783. assert(ggml_is_contiguous_1(dst));
  5784. assert(ggml_are_same_shape(src0, dst));
  5785. const int n = ggml_nrows(src0);
  5786. const int nc = src0->ne[0];
  5787. for (int i = 0; i < n; i++) {
  5788. ggml_vec_exp_f32(nc,
  5789. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5790. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5791. }
  5792. }
  5793. static void ggml_compute_forward_exp(
  5794. const struct ggml_compute_params * params,
  5795. struct ggml_tensor * dst) {
  5796. const struct ggml_tensor * src0 = dst->src[0];
  5797. switch (src0->type) {
  5798. case GGML_TYPE_F32:
  5799. {
  5800. ggml_compute_forward_exp_f32(params, dst);
  5801. } break;
  5802. default:
  5803. {
  5804. GGML_ABORT("fatal error");
  5805. }
  5806. }
  5807. }
  5808. // ggml_compute_forward_norm
  5809. static void ggml_compute_forward_norm_f32(
  5810. const struct ggml_compute_params * params,
  5811. struct ggml_tensor * dst) {
  5812. const struct ggml_tensor * src0 = dst->src[0];
  5813. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  5814. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5815. const int ith = params->ith;
  5816. const int nth = params->nth;
  5817. GGML_TENSOR_UNARY_OP_LOCALS
  5818. float eps;
  5819. memcpy(&eps, dst->op_params, sizeof(float));
  5820. GGML_ASSERT(eps >= 0.0f);
  5821. // TODO: optimize
  5822. for (int64_t i03 = 0; i03 < ne03; i03++) {
  5823. for (int64_t i02 = 0; i02 < ne02; i02++) {
  5824. for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
  5825. const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  5826. ggml_float sum = 0.0;
  5827. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5828. sum += (ggml_float)x[i00];
  5829. }
  5830. float mean = sum/ne00;
  5831. float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
  5832. ggml_float sum2 = 0.0;
  5833. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5834. float v = x[i00] - mean;
  5835. y[i00] = v;
  5836. sum2 += (ggml_float)(v*v);
  5837. }
  5838. float variance = sum2/ne00;
  5839. const float scale = 1.0f/sqrtf(variance + eps);
  5840. ggml_vec_scale_f32(ne00, y, scale);
  5841. }
  5842. }
  5843. }
  5844. }
  5845. static void ggml_compute_forward_norm(
  5846. const struct ggml_compute_params * params,
  5847. struct ggml_tensor * dst) {
  5848. const struct ggml_tensor * src0 = dst->src[0];
  5849. switch (src0->type) {
  5850. case GGML_TYPE_F32:
  5851. {
  5852. ggml_compute_forward_norm_f32(params, dst);
  5853. } break;
  5854. default:
  5855. {
  5856. GGML_ABORT("fatal error");
  5857. }
  5858. }
  5859. }
  5860. // ggml_compute_forward_group_rms_norm
  5861. static void ggml_compute_forward_rms_norm_f32(
  5862. const struct ggml_compute_params * params,
  5863. struct ggml_tensor * dst) {
  5864. const struct ggml_tensor * src0 = dst->src[0];
  5865. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  5866. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5867. const int ith = params->ith;
  5868. const int nth = params->nth;
  5869. GGML_TENSOR_UNARY_OP_LOCALS
  5870. float eps;
  5871. memcpy(&eps, dst->op_params, sizeof(float));
  5872. GGML_ASSERT(eps >= 0.0f);
  5873. // TODO: optimize
  5874. for (int64_t i03 = 0; i03 < ne03; i03++) {
  5875. for (int64_t i02 = 0; i02 < ne02; i02++) {
  5876. for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
  5877. const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  5878. ggml_float sum = 0.0;
  5879. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5880. sum += (ggml_float)(x[i00] * x[i00]);
  5881. }
  5882. const float mean = sum/ne00;
  5883. float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
  5884. memcpy(y, x, ne00 * sizeof(float));
  5885. // for (int i00 = 0; i00 < ne00; i00++) {
  5886. // y[i00] = x[i00];
  5887. // }
  5888. const float scale = 1.0f/sqrtf(mean + eps);
  5889. ggml_vec_scale_f32(ne00, y, scale);
  5890. }
  5891. }
  5892. }
  5893. }
  5894. static void ggml_compute_forward_rms_norm(
  5895. const struct ggml_compute_params * params,
  5896. struct ggml_tensor * dst) {
  5897. const struct ggml_tensor * src0 = dst->src[0];
  5898. switch (src0->type) {
  5899. case GGML_TYPE_F32:
  5900. {
  5901. ggml_compute_forward_rms_norm_f32(params, dst);
  5902. } break;
  5903. default:
  5904. {
  5905. GGML_ABORT("fatal error");
  5906. }
  5907. }
  5908. }
  5909. static void ggml_compute_forward_rms_norm_back_f32(
  5910. const struct ggml_compute_params * params,
  5911. struct ggml_tensor * dst) {
  5912. const struct ggml_tensor * src0 = dst->src[0]; // gradients from forward pass output
  5913. const struct ggml_tensor * src1 = dst->src[1]; // src1 from forward pass
  5914. GGML_ASSERT(ggml_are_same_shape(src0, dst) && ggml_are_same_shape(src0, src1));
  5915. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5916. GGML_ASSERT(src1->nb[0] == sizeof(float));
  5917. const int ith = params->ith;
  5918. const int nth = params->nth;
  5919. GGML_TENSOR_BINARY_OP_LOCALS
  5920. float eps;
  5921. memcpy(&eps, dst->op_params, sizeof(float));
  5922. // TODO: optimize
  5923. for (int64_t i03 = 0; i03 < ne03; i03++) {
  5924. for (int64_t i02 = 0; i02 < ne02; i02++) {
  5925. for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
  5926. // src1 is same shape as src0 => same indices
  5927. const int64_t i11 = i01;
  5928. const int64_t i12 = i02;
  5929. const int64_t i13 = i03;
  5930. const float * dz = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  5931. const float * x = (float *) ((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13);
  5932. ggml_float sum_xx = 0.0;
  5933. ggml_float sum_xdz = 0.0;
  5934. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5935. sum_xx += (ggml_float)(x[i00] * x[i00]);
  5936. sum_xdz += (ggml_float)(x[i00] * dz[i00]);
  5937. }
  5938. //const float mean = (float)(sum_xx)/ne00;
  5939. const float mean_eps = (float)(sum_xx)/ne00 + eps;
  5940. const float sum_eps = (float)(sum_xx) + eps*ne00;
  5941. //const float mean_xdz = (float)(sum_xdz)/ne00;
  5942. // we could cache rms from forward pass to improve performance.
  5943. // to do this implement ggml_rms and compose ggml_rms_norm using ggml_rms.
  5944. //const float rms = sqrtf(mean_eps);
  5945. const float rrms = 1.0f / sqrtf(mean_eps);
  5946. //const float scale = -rrms/(ne00 * mean_eps); // -1/(n*rms**3)
  5947. {
  5948. // z = rms_norm(x)
  5949. //
  5950. // rms_norm(src1) =
  5951. // scale(
  5952. // src1,
  5953. // div(
  5954. // 1,
  5955. // sqrt(
  5956. // add(
  5957. // scale(
  5958. // sum(
  5959. // sqr(
  5960. // src1)),
  5961. // (1.0/N)),
  5962. // eps))));
  5963. // postorder:
  5964. // ## op args grad
  5965. // 00 param src1 grad[#00]
  5966. // 01 const 1
  5967. // 02 sqr (#00) grad[#02]
  5968. // 03 sum (#02) grad[#03]
  5969. // 04 const 1/N
  5970. // 05 scale (#03, #04) grad[#05]
  5971. // 06 const eps
  5972. // 07 add (#05, #06) grad[#07]
  5973. // 08 sqrt (#07) grad[#08]
  5974. // 09 div (#01,#08) grad[#09]
  5975. // 10 scale (#00,#09) grad[#10]
  5976. //
  5977. // backward pass, given grad[#10]
  5978. // #10: scale
  5979. // grad[#00] += scale(grad[#10],#09)
  5980. // grad[#09] += sum(mul(grad[#10],#00))
  5981. // #09: div
  5982. // grad[#08] += neg(mul(grad[#09], div(#09,#08)))
  5983. // #08: sqrt
  5984. // grad[#07] += mul(grad[#08], div(0.5, #08))
  5985. // #07: add
  5986. // grad[#05] += grad[#07]
  5987. // #05: scale
  5988. // grad[#03] += scale(grad[#05],#04)
  5989. // #03: sum
  5990. // grad[#02] += repeat(grad[#03], #02)
  5991. // #02:
  5992. // grad[#00] += scale(mul(#00, grad[#02]), 2.0)
  5993. //
  5994. // substitute and simplify:
  5995. // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0)
  5996. // grad[#02] = repeat(grad[#03], #02)
  5997. // grad[#02] = repeat(scale(grad[#05],#04), #02)
  5998. // grad[#02] = repeat(scale(grad[#07],#04), #02)
  5999. // grad[#02] = repeat(scale(mul(grad[#08], div(0.5, #08)),#04), #02)
  6000. // grad[#02] = repeat(scale(mul(neg(mul(grad[#09], div(#09,#08))), div(0.5, #08)),#04), #02)
  6001. // grad[#02] = repeat(scale(mul(neg(mul(sum(mul(grad[#10],#00)), div(#09,#08))), div(0.5, #08)),#04), #02)
  6002. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(#09,#08) * div(0.5, #08) * (1/N)), #02)
  6003. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(div(#01,#08),#08) * div(0.5, #08) * (1/N)), #02)
  6004. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#08*#08) * div(0.5, #08) * (1/N)), #02)
  6005. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N)), #02)
  6006. // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0)
  6007. // 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)
  6008. // 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)
  6009. // grad[#00] = scale(grad(#10), #09) + scale(#00, -(sum(mul(grad[#10],#00)) * div(1,#07) * div(1,#08) * (1/N)))
  6010. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N))
  6011. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N))
  6012. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,mean_eps*rms) * (-1/N))
  6013. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*mean_eps))
  6014. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*(sum_xx/N+eps)))
  6015. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*sum_xx+rms*N*eps))
  6016. // grad[#00] = scale(dz, rrms) + scale(x, sum(mul(dz,x)) * div(-1,rms*N*mean_eps))
  6017. // grad[#00] = scale(dz, rrms) + scale(x, sum_xdz * div(-1,rms*N*mean_eps))
  6018. // a = b*c + d*e
  6019. // a = b*c*f/f + d*e*f/f
  6020. // a = (b*c*f + d*e*f)*(1/f)
  6021. // a = (b*c*(1/c) + d*e*(1/c))*(1/(1/c))
  6022. // a = (b + d*e/c)*c
  6023. // b = dz, c = rrms, d = x, e = sum_xdz * div(-1,rms*N*mean_eps)
  6024. // a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)/rrms)*rrms
  6025. // a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)*rms)*rrms
  6026. // a = (dz + x*sum_xdz * div(-rms,rms*N*mean_eps))*rrms
  6027. // a = (dz + x*sum_xdz * div(-1,N*mean_eps))*rrms
  6028. // a = (dz + x*div(-sum_xdz,N*mean_eps))*rrms
  6029. // a = (dz + x*div(-mean_xdz,mean_eps))*rrms
  6030. // grad[#00] = scale(dz + scale(x, div(-mean_xdz,mean_eps)),rrms)
  6031. // grad[#00] = scale(dz + scale(x, -mean_xdz/mean_eps),rrms)
  6032. // dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms)
  6033. }
  6034. // dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms)
  6035. // post-order:
  6036. // dx := x
  6037. // dx := scale(dx,-mean_xdz/mean_eps)
  6038. // dx := add(dx, dz)
  6039. // dx := scale(dx, rrms)
  6040. float * dx = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
  6041. // dx[i00] = (x*(-sum_xdz/sum_eps) + dz) / sqrtf(mean_eps)
  6042. ggml_vec_cpy_f32 (ne00, dx, x);
  6043. // ggml_vec_scale_f32(ne00, dx, -mean_xdz/mean_eps);
  6044. ggml_vec_scale_f32(ne00, dx, (float)(-sum_xdz)/sum_eps);
  6045. ggml_vec_acc_f32 (ne00, dx, dz);
  6046. ggml_vec_scale_f32(ne00, dx, rrms);
  6047. }
  6048. }
  6049. }
  6050. }
  6051. static void ggml_compute_forward_rms_norm_back(
  6052. const struct ggml_compute_params * params,
  6053. struct ggml_tensor * dst) {
  6054. const struct ggml_tensor * src0 = dst->src[0];
  6055. switch (src0->type) {
  6056. case GGML_TYPE_F32:
  6057. {
  6058. ggml_compute_forward_rms_norm_back_f32(params, dst);
  6059. } break;
  6060. default:
  6061. {
  6062. GGML_ABORT("fatal error");
  6063. }
  6064. }
  6065. }
  6066. // ggml_compute_forward_group_norm
  6067. static void ggml_compute_forward_group_norm_f32(
  6068. const struct ggml_compute_params * params,
  6069. struct ggml_tensor * dst) {
  6070. const struct ggml_tensor * src0 = dst->src[0];
  6071. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6072. GGML_ASSERT(src0->nb[0] == sizeof(float));
  6073. const int ith = params->ith;
  6074. const int nth = params->nth;
  6075. GGML_TENSOR_UNARY_OP_LOCALS
  6076. // TODO: optimize
  6077. float eps;
  6078. memcpy(&eps, dst->op_params + 1, sizeof(float));
  6079. int n_channels = src0->ne[2];
  6080. int n_groups = dst->op_params[0];
  6081. int n_channels_per_group = (n_channels + n_groups - 1) / n_groups;
  6082. for (int i = ith; i < n_groups; i += nth) {
  6083. int start = i * n_channels_per_group;
  6084. int end = start + n_channels_per_group;
  6085. if (end > n_channels) {
  6086. end = n_channels;
  6087. }
  6088. int step = end - start;
  6089. for (int64_t i03 = 0; i03 < ne03; i03++) {
  6090. ggml_float sum = 0.0;
  6091. for (int64_t i02 = start; i02 < end; i02++) {
  6092. for (int64_t i01 = 0; i01 < ne01; i01++) {
  6093. const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03);
  6094. ggml_float sumr = 0.0;
  6095. for (int64_t i00 = 0; i00 < ne00; i00++) {
  6096. sumr += (ggml_float)x[i00];
  6097. }
  6098. sum += sumr;
  6099. }
  6100. }
  6101. const float mean = sum / (ne00 * ne01 * step);
  6102. ggml_float sum2 = 0.0;
  6103. for (int64_t i02 = start; i02 < end; i02++) {
  6104. for (int64_t i01 = 0; i01 < ne01; i01++) {
  6105. const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03);
  6106. float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3);
  6107. ggml_float sumr = 0.0;
  6108. for (int64_t i00 = 0; i00 < ne00; i00++) {
  6109. float v = x[i00] - mean;
  6110. y[i00] = v;
  6111. sumr += (ggml_float)(v * v);
  6112. }
  6113. sum2 += sumr;
  6114. }
  6115. }
  6116. const float variance = sum2 / (ne00 * ne01 * step);
  6117. const float scale = 1.0f / sqrtf(variance + eps);
  6118. for (int64_t i02 = start; i02 < end; i02++) {
  6119. for (int64_t i01 = 0; i01 < ne01; i01++) {
  6120. float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3);
  6121. ggml_vec_scale_f32(ne00, y, scale);
  6122. }
  6123. }
  6124. }
  6125. }
  6126. }
  6127. static void ggml_compute_forward_group_norm(
  6128. const struct ggml_compute_params * params,
  6129. struct ggml_tensor * dst) {
  6130. const struct ggml_tensor * src0 = dst->src[0];
  6131. switch (src0->type) {
  6132. case GGML_TYPE_F32:
  6133. {
  6134. ggml_compute_forward_group_norm_f32(params, dst);
  6135. } break;
  6136. default:
  6137. {
  6138. GGML_ABORT("fatal error");
  6139. }
  6140. }
  6141. }
  6142. // ggml_compute_forward_mul_mat
  6143. static void ggml_compute_forward_mul_mat_one_chunk(
  6144. const struct ggml_compute_params * params,
  6145. struct ggml_tensor * dst,
  6146. const enum ggml_type type,
  6147. const int64_t num_rows_per_vec_dot,
  6148. const int64_t ir0_start,
  6149. const int64_t ir0_end,
  6150. const int64_t ir1_start,
  6151. const int64_t ir1_end) {
  6152. const struct ggml_tensor * src0 = dst->src[0];
  6153. const struct ggml_tensor * src1 = dst->src[1];
  6154. GGML_TENSOR_BINARY_OP_LOCALS
  6155. const bool src1_cont = ggml_is_contiguous(src1);
  6156. ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot;
  6157. enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type;
  6158. // broadcast factors
  6159. const int64_t r2 = ne12 / ne02;
  6160. const int64_t r3 = ne13 / ne03;
  6161. //printf("ir0_start = %6lld, ir0_end = %6lld, ir1_start = %6lld, ir1_end = %6lld\n", ir0_start, ir0_end, ir1_start, ir1_end);
  6162. // threads with no work simply yield (not sure if it helps)
  6163. if (ir0_start >= ir0_end || ir1_start >= ir1_end) {
  6164. return;
  6165. }
  6166. const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
  6167. const size_t row_size = ggml_row_size(vec_dot_type, ne10);
  6168. assert(ne12 % ne02 == 0);
  6169. assert(ne13 % ne03 == 0);
  6170. // block-tiling attempt
  6171. const int64_t blck_0 = 16;
  6172. const int64_t blck_1 = 16;
  6173. const size_t src1_col_stride = src1_cont || src1->type != vec_dot_type ? row_size : nb11;
  6174. // attempt to reduce false-sharing (does not seem to make a difference)
  6175. // 16 * 2, accounting for mmla kernels
  6176. float tmp[32];
  6177. for (int64_t iir1 = ir1_start; iir1 < ir1_end; iir1 += blck_1) {
  6178. for (int64_t iir0 = ir0_start; iir0 < ir0_end; iir0 += blck_0) {
  6179. for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir1_end; ir1 += num_rows_per_vec_dot) {
  6180. const int64_t i13 = (ir1 / (ne12 * ne1));
  6181. const int64_t i12 = (ir1 - i13 * ne12 * ne1) / ne1;
  6182. const int64_t i11 = (ir1 - i13 * ne12 * ne1 - i12 * ne1);
  6183. // broadcast src0 into src1
  6184. const int64_t i03 = i13 / r3;
  6185. const int64_t i02 = i12 / r2;
  6186. const int64_t i1 = i11;
  6187. const int64_t i2 = i12;
  6188. const int64_t i3 = i13;
  6189. const char * src0_row = (const char*)src0->data + (0 + i02 * nb02 + i03 * nb03);
  6190. // desc: when src1 is not a contiguous memory block we have to calculate the offset using the strides
  6191. // if it is, then we have either copied the data to params->wdata and made it contiguous or we are using
  6192. // the original src1 data pointer, so we should index using the indices directly
  6193. // TODO: this is a bit of a hack, we should probably have a better way to handle this
  6194. const char * src1_col = (const char*)wdata +
  6195. (src1_cont || src1->type != vec_dot_type
  6196. ? (i11 + i12 * ne11 + i13 * ne12 * ne11) * row_size
  6197. : (i11 * nb11 + i12 * nb12 + i13 * nb13));
  6198. float * dst_col = (float*)((char*)dst->data + (i1 * nb1 + i2 * nb2 + i3 * nb3));
  6199. //for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ++ir0) {
  6200. // vec_dot(ne00, &dst_col[ir0], src0_row + ir0*nb01, src1_col);
  6201. //}
  6202. for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ir0 += num_rows_per_vec_dot) {
  6203. 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);
  6204. }
  6205. for (int cn = 0; cn < num_rows_per_vec_dot; ++cn) {
  6206. memcpy(&dst_col[iir0 + cn * nb1 / nb0], tmp + (cn * 16), (MIN(iir0 + blck_0, ir0_end) - iir0) * sizeof(float));
  6207. }
  6208. }
  6209. }
  6210. }
  6211. }
  6212. static void ggml_compute_forward_mul_mat(
  6213. const struct ggml_compute_params * params,
  6214. struct ggml_tensor * dst) {
  6215. const struct ggml_tensor * src0 = dst->src[0];
  6216. const struct ggml_tensor * src1 = dst->src[1];
  6217. GGML_TENSOR_BINARY_OP_LOCALS
  6218. const int ith = params->ith;
  6219. const int nth = params->nth;
  6220. enum ggml_type const vec_dot_type = type_traits_cpu[src0->type].vec_dot_type;
  6221. ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float;
  6222. int64_t const vec_dot_num_rows = type_traits_cpu[src0->type].nrows;
  6223. GGML_ASSERT(ne0 == ne01);
  6224. GGML_ASSERT(ne1 == ne11);
  6225. GGML_ASSERT(ne2 == ne12);
  6226. GGML_ASSERT(ne3 == ne13);
  6227. // we don't support permuted src0 or src1
  6228. GGML_ASSERT(nb00 == ggml_type_size(src0->type));
  6229. GGML_ASSERT(nb10 == ggml_type_size(src1->type));
  6230. // dst cannot be transposed or permuted
  6231. GGML_ASSERT(nb0 == sizeof(float));
  6232. GGML_ASSERT(nb0 <= nb1);
  6233. GGML_ASSERT(nb1 <= nb2);
  6234. GGML_ASSERT(nb2 <= nb3);
  6235. // nb01 >= nb00 - src0 is not transposed
  6236. // compute by src0 rows
  6237. // TODO: extract to "extra_op"
  6238. #if GGML_USE_LLAMAFILE
  6239. // broadcast factors
  6240. const int64_t r2 = ne12 / ne02;
  6241. const int64_t r3 = ne13 / ne03;
  6242. const bool src1_cont = ggml_is_contiguous(src1);
  6243. if (src1_cont) {
  6244. for (int64_t i13 = 0; i13 < ne13; i13++)
  6245. for (int64_t i12 = 0; i12 < ne12; i12++)
  6246. if (!llamafile_sgemm(params,
  6247. ne01, ne11, ne00/ggml_blck_size(src0->type),
  6248. (const char *)src0->data + i12/r2*nb02 + i13/r3*nb03,
  6249. nb01/ggml_type_size(src0->type),
  6250. (const char *)src1->data + i12*nb12 + i13*nb13,
  6251. nb11/ggml_type_size(src1->type),
  6252. (char *)dst->data + i12*nb2 + i13*nb3,
  6253. nb1/ggml_type_size(dst->type),
  6254. src0->type,
  6255. src1->type,
  6256. dst->type))
  6257. goto UseGgmlGemm1;
  6258. return;
  6259. }
  6260. UseGgmlGemm1:;
  6261. #endif
  6262. if (src1->type != vec_dot_type) {
  6263. char * wdata = params->wdata;
  6264. const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
  6265. const size_t nbw2 = nbw1*ne11;
  6266. const size_t nbw3 = nbw2*ne12;
  6267. assert(params->wsize >= ne13*nbw3);
  6268. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6269. for (int64_t i13 = 0; i13 < ne13; ++i13) {
  6270. for (int64_t i12 = 0; i12 < ne12; ++i12) {
  6271. for (int64_t i11 = ith; i11 < ne11; i11 += nth) {
  6272. from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11),
  6273. (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1),
  6274. ne10);
  6275. }
  6276. }
  6277. }
  6278. }
  6279. if (ith == 0) {
  6280. // Every thread starts at ith, so the first unprocessed chunk is nth. This save a bit of coordination right at the start.
  6281. atomic_store_explicit(&params->threadpool->current_chunk, nth, memory_order_relaxed);
  6282. }
  6283. ggml_barrier(params->threadpool);
  6284. #if GGML_USE_LLAMAFILE
  6285. if (src1->type != vec_dot_type) {
  6286. const void* wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
  6287. const size_t row_size = ggml_row_size(vec_dot_type, ne10);
  6288. for (int64_t i13 = 0; i13 < ne13; i13++)
  6289. for (int64_t i12 = 0; i12 < ne12; i12++)
  6290. if (!llamafile_sgemm(params,
  6291. ne01, ne11, ne00/ggml_blck_size(src0->type),
  6292. (const char *)src0->data + i12/r2*nb02 + i13/r3*nb03,
  6293. nb01/ggml_type_size(src0->type),
  6294. (const char *)wdata + (i12*ne11 + i13*ne12*ne11)*row_size,
  6295. row_size/ggml_type_size(vec_dot_type),
  6296. (char *)dst->data + i12*nb2 + i13*nb3,
  6297. nb1/ggml_type_size(dst->type),
  6298. src0->type,
  6299. vec_dot_type,
  6300. dst->type))
  6301. goto UseGgmlGemm2;
  6302. return;
  6303. }
  6304. UseGgmlGemm2:;
  6305. #endif
  6306. // 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)
  6307. const int64_t nr0 = ne0;
  6308. // This is the size of the rest of the dimensions of the result
  6309. const int64_t nr1 = ne1 * ne2 * ne3;
  6310. // Now select a reasonable chunk size.
  6311. int chunk_size = 16;
  6312. // We need to step up the size if it's small
  6313. if (nr0 == 1 || nr1 == 1) {
  6314. chunk_size = 64;
  6315. }
  6316. // distribute the work across the inner or outer loop based on which one is larger
  6317. // The number of chunks in the 0/1 dim.
  6318. // CEIL(nr0/chunk_size)
  6319. int64_t nchunk0 = (nr0 + chunk_size - 1) / chunk_size;
  6320. int64_t nchunk1 = (nr1 + chunk_size - 1) / chunk_size;
  6321. // If the chunking is poor for the number of threads on this setup, scrap the whole plan. Re-chunk it by thread.
  6322. // Also, chunking by thread was measured to have perform better on NUMA systems. See https://github.com/ggerganov/llama.cpp/pull/6915
  6323. // In theory, chunking should be just as useful on NUMA and non NUMA systems, but testing disagreed with that.
  6324. if (nchunk0 * nchunk1 < nth * 4 || ggml_is_numa()) {
  6325. // distribute the thread work across the inner or outer loop based on which one is larger
  6326. nchunk0 = nr0 > nr1 ? nth : 1; // parallelize by src0 rows
  6327. nchunk1 = nr0 > nr1 ? 1 : nth; // parallelize by src1 rows
  6328. }
  6329. // The number of elements in each chunk
  6330. const int64_t dr0 = (nr0 + nchunk0 - 1) / nchunk0;
  6331. const int64_t dr1 = (nr1 + nchunk1 - 1) / nchunk1;
  6332. // The first chunk comes from our thread_id, the rest will get auto-assigned.
  6333. int current_chunk = ith;
  6334. while (current_chunk < nchunk0 * nchunk1) {
  6335. const int64_t ith0 = current_chunk % nchunk0;
  6336. const int64_t ith1 = current_chunk / nchunk0;
  6337. const int64_t ir0_start = dr0 * ith0;
  6338. const int64_t ir0_end = MIN(ir0_start + dr0, nr0);
  6339. const int64_t ir1_start = dr1 * ith1;
  6340. const int64_t ir1_end = MIN(ir1_start + dr1, nr1);
  6341. // dot kernels can handle 1 row and col at a time, but mmla kernels can process 2 rows and cols
  6342. int64_t num_rows_per_vec_dot = vec_dot_num_rows;
  6343. // these checks are needed to avoid crossing dim1 boundaries
  6344. // can be optimized, but the logic would become more complicated, so keeping it like this for simplicity
  6345. if ((nr0 % 2 != 0) || (ne11 % 2 != 0) || ((ir0_end - ir0_start) % 2 != 0) || ((ir1_end - ir1_start) % 2 != 0)) {
  6346. num_rows_per_vec_dot = 1;
  6347. }
  6348. ggml_compute_forward_mul_mat_one_chunk(params, dst, src0->type, num_rows_per_vec_dot, ir0_start, ir0_end, ir1_start, ir1_end);
  6349. if (nth >= nchunk0 * nchunk1) {
  6350. break;
  6351. }
  6352. current_chunk = atomic_fetch_add_explicit(&params->threadpool->current_chunk, 1, memory_order_relaxed);
  6353. }
  6354. }
  6355. // ggml_compute_forward_mul_mat_id
  6356. static void ggml_compute_forward_mul_mat_id(
  6357. const struct ggml_compute_params * params,
  6358. struct ggml_tensor * dst) {
  6359. const struct ggml_tensor * src0 = dst->src[0];
  6360. const struct ggml_tensor * src1 = dst->src[1];
  6361. const struct ggml_tensor * ids = dst->src[2];
  6362. GGML_TENSOR_BINARY_OP_LOCALS
  6363. const int ith = params->ith;
  6364. const int nth = params->nth;
  6365. const enum ggml_type type = src0->type;
  6366. const bool src1_cont = ggml_is_contiguous(src1);
  6367. ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot;
  6368. enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type;
  6369. ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float;
  6370. // we don't support permuted src0 or src1
  6371. GGML_ASSERT(nb00 == ggml_type_size(type));
  6372. GGML_ASSERT(nb10 == ggml_type_size(src1->type));
  6373. // dst cannot be transposed or permuted
  6374. GGML_ASSERT(nb0 == sizeof(float));
  6375. GGML_ASSERT(nb0 <= nb1);
  6376. GGML_ASSERT(nb1 <= nb2);
  6377. GGML_ASSERT(nb2 <= nb3);
  6378. // row groups
  6379. const int n_ids = ids->ne[0]; // n_expert_used
  6380. const int n_as = ne02; // n_expert
  6381. char * wdata_src1_end = (src1->type == vec_dot_type) ?
  6382. (char *) params->wdata :
  6383. (char *) params->wdata + GGML_PAD(ggml_row_size(vec_dot_type, ggml_nelements(src1)), sizeof(int64_t));
  6384. struct mmid_row_mapping {
  6385. int32_t i1;
  6386. int32_t i2;
  6387. };
  6388. int64_t * matrix_row_counts = (int64_t *) (wdata_src1_end); // [n_as]
  6389. struct mmid_row_mapping * matrix_rows = (struct mmid_row_mapping *)(matrix_row_counts + n_as); // [n_as][ne11]
  6390. if (src1->type != vec_dot_type) {
  6391. char * wdata = params->wdata;
  6392. const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
  6393. const size_t nbw2 = nbw1*ne11;
  6394. const size_t nbw3 = nbw2*ne12;
  6395. assert(params->wsize >= ne13*nbw3);
  6396. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6397. for (int64_t i13 = 0; i13 < ne13; ++i13) {
  6398. for (int64_t i12 = 0; i12 < ne12; ++i12) {
  6399. for (int64_t i11 = ith; i11 < ne11; i11 += nth) {
  6400. from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11),
  6401. (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1),
  6402. ne10);
  6403. }
  6404. }
  6405. }
  6406. }
  6407. #define MMID_MATRIX_ROW(row_id, i1) matrix_rows[(row_id)*ne12 + (i1)]
  6408. if (ith == 0) {
  6409. // initialize matrix_row_counts
  6410. memset(matrix_row_counts, 0, n_as*sizeof(int64_t));
  6411. // group rows by src0 matrix
  6412. for (int64_t iid1 = 0; iid1 < ids->ne[1]; ++iid1) {
  6413. for (int id = 0; id < n_ids; ++id) {
  6414. const int32_t i02 = *(const int32_t *) ((const char *) ids->data + iid1*ids->nb[1] + id*ids->nb[0]);
  6415. assert(i02 >= 0 && i02 < n_as);
  6416. MMID_MATRIX_ROW(i02, matrix_row_counts[i02]) = (struct mmid_row_mapping) {id, iid1};
  6417. matrix_row_counts[i02] += 1;
  6418. }
  6419. }
  6420. }
  6421. ggml_barrier(params->threadpool);
  6422. // compute each matrix multiplication in sequence
  6423. for (int cur_a = 0; cur_a < n_as; ++cur_a) {
  6424. const int64_t cne1 = matrix_row_counts[cur_a];
  6425. if (cne1 == 0) {
  6426. continue;
  6427. }
  6428. const char * src0_cur = (const char *) src0->data + cur_a*nb02;
  6429. const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
  6430. const size_t row_size = ggml_row_size(vec_dot_type, ne10);
  6431. const int64_t nr0 = ne01; // src0 rows
  6432. const int64_t nr1 = cne1; // src1 rows
  6433. // distribute the thread work across the inner or outer loop based on which one is larger
  6434. const int64_t nth0 = nr0 > nr1 ? nth : 1; // parallelize by src0 rows
  6435. const int64_t nth1 = nr0 > nr1 ? 1 : nth; // parallelize by src1 rows
  6436. const int64_t ith0 = ith % nth0;
  6437. const int64_t ith1 = ith / nth0;
  6438. const int64_t dr0 = (nr0 + nth0 - 1)/nth0;
  6439. const int64_t dr1 = (nr1 + nth1 - 1)/nth1;
  6440. const int64_t ir010 = dr0*ith0;
  6441. const int64_t ir011 = MIN(ir010 + dr0, nr0);
  6442. const int64_t ir110 = dr1*ith1;
  6443. const int64_t ir111 = MIN(ir110 + dr1, nr1);
  6444. // threads with no work simply yield (not sure if it helps)
  6445. //if (ir010 >= ir011 || ir110 >= ir111) {
  6446. // sched_yield();
  6447. // continue;
  6448. //}
  6449. // block-tiling attempt
  6450. const int64_t blck_0 = 16;
  6451. const int64_t blck_1 = 16;
  6452. // attempt to reduce false-sharing (does not seem to make a difference)
  6453. float tmp[16];
  6454. for (int64_t iir1 = ir110; iir1 < ir111; iir1 += blck_1) {
  6455. for (int64_t iir0 = ir010; iir0 < ir011; iir0 += blck_0) {
  6456. for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir111; ++ir1) {
  6457. const int64_t _i12 = ir1; // logical row index for this expert
  6458. struct mmid_row_mapping row_mapping = MMID_MATRIX_ROW(cur_a, _i12);
  6459. const int id = row_mapping.i1; // selected expert index
  6460. const int64_t i11 = id % ne11;
  6461. const int64_t i12 = row_mapping.i2; // row index in src1
  6462. const int64_t i1 = id; // selected expert index
  6463. const int64_t i2 = i12; // row
  6464. // desc: when src1 is not a contiguous memory block we have to calculate the offset using the strides
  6465. // if it is, then we have either copied the data to params->wdata and made it contiguous or we are using
  6466. // the original src1 data pointer, so we should index using the indices directly
  6467. // TODO: this is a bit of a hack, we should probably have a better way to handle this
  6468. const char * src1_col = (const char *) wdata +
  6469. (src1_cont || src1->type != vec_dot_type
  6470. ? (i11 + i12*ne11)*row_size
  6471. : (i11*nb11 + i12*nb12));
  6472. float * dst_col = (float *) ((char *) dst->data + (i1*nb1 + i2*nb2));
  6473. //for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir011; ++ir0) {
  6474. // vec_dot(ne00, &dst_col[ir0], src0_row + ir0*nb01, src1_col);
  6475. //}
  6476. for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir011; ++ir0) {
  6477. vec_dot(ne00, &tmp[ir0 - iir0], 0, src0_cur + ir0*nb01, 0, src1_col, 0, 1);
  6478. }
  6479. memcpy(&dst_col[iir0], tmp, (MIN(iir0 + blck_0, ir011) - iir0)*sizeof(float));
  6480. }
  6481. }
  6482. }
  6483. }
  6484. #undef MMID_MATRIX_ROW
  6485. }
  6486. // ggml_compute_forward_out_prod
  6487. static void ggml_compute_forward_out_prod_f32(
  6488. const struct ggml_compute_params * params,
  6489. struct ggml_tensor * dst) {
  6490. const struct ggml_tensor * src0 = dst->src[0];
  6491. const struct ggml_tensor * src1 = dst->src[1];
  6492. GGML_TENSOR_BINARY_OP_LOCALS
  6493. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6494. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6495. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6496. const int ith = params->ith;
  6497. const int nth = params->nth;
  6498. GGML_ASSERT(ne0 == ne00);
  6499. GGML_ASSERT(ne1 == ne10);
  6500. GGML_ASSERT(ne2 == ne12);
  6501. GGML_ASSERT(ne3 == ne13);
  6502. GGML_ASSERT(ne2 % ne02 == 0);
  6503. GGML_ASSERT(ne3 % ne03 == 0);
  6504. // we don't support permuted src0 or src1
  6505. GGML_ASSERT(nb00 == sizeof(float));
  6506. // dst cannot be transposed or permuted
  6507. GGML_ASSERT(nb0 == sizeof(float));
  6508. // GGML_ASSERT(nb0 <= nb1);
  6509. // GGML_ASSERT(nb1 <= nb2);
  6510. // GGML_ASSERT(nb2 <= nb3);
  6511. // nb01 >= nb00 - src0 is not transposed
  6512. // compute by src0 rows
  6513. if (ith == 0) {
  6514. ggml_vec_set_f32(ne0*ne1*ne2*ne3, dst->data, 0);
  6515. }
  6516. ggml_barrier(params->threadpool);
  6517. // dst[:,:,:,:] = 0
  6518. // for i2,i3:
  6519. // for i1:
  6520. // for i01:
  6521. // for i0:
  6522. // dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3]
  6523. // parallelize by last three dimensions
  6524. // total rows in dst
  6525. const int64_t nr = ne1*ne2*ne3;
  6526. // rows per thread
  6527. const int64_t dr = (nr + nth - 1)/nth;
  6528. // row range for this thread
  6529. const int64_t ir0 = dr*ith;
  6530. const int64_t ir1 = MIN(ir0 + dr, nr);
  6531. // block-tiling attempt
  6532. const int64_t blck_0 = MAX(GGML_VEC_MAD_UNROLL, 32);
  6533. const int64_t blck_1 = 16;
  6534. // dps == dst per src0, used for group query attention
  6535. const int64_t dps2 = ne2 / ne02;
  6536. const int64_t dps3 = ne3 / ne03;
  6537. for (int64_t bir = ir0; bir < ir1; bir += blck_1) {
  6538. const int64_t bir1 = MIN(bir + blck_1, ir1);
  6539. for (int64_t bi01 = 0; bi01 < ne01; bi01 += blck_0) {
  6540. const int64_t bne01 = MIN(bi01 + blck_0, ne01);
  6541. for (int64_t ir = bir; ir < bir1; ++ir) {
  6542. // dst indices
  6543. const int64_t i3 = ir/(ne2*ne1);
  6544. const int64_t i2 = (ir - i3*ne2*ne1)/ne1;
  6545. const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1);
  6546. const int64_t i02 = i2 / dps2;
  6547. const int64_t i03 = i3 / dps3;
  6548. //const int64_t i10 = i1;
  6549. const int64_t i12 = i2;
  6550. const int64_t i13 = i3;
  6551. #if GGML_VEC_MAD_UNROLL > 2
  6552. const int64_t bne01_unroll = bne01 - (bne01 % GGML_VEC_MAD_UNROLL);
  6553. for (int64_t i01 = bi01; i01 < bne01_unroll; i01 += GGML_VEC_MAD_UNROLL) {
  6554. const int64_t i11 = i01;
  6555. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6556. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6557. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6558. ggml_vec_mad_f32_unroll(ne0, nb01, nb11, d, s0, s1);
  6559. }
  6560. for (int64_t i01 = bne01_unroll; i01 < bne01; ++i01) {
  6561. const int64_t i11 = i01;
  6562. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6563. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6564. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6565. ggml_vec_mad_f32(ne0, d, s0, *s1);
  6566. }
  6567. #else
  6568. for (int64_t i01 = bi01; i01 < bne01; ++i01) {
  6569. const int64_t i11 = i01;
  6570. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6571. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6572. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6573. ggml_vec_mad_f32(ne0, d, s0, *s1);
  6574. }
  6575. #endif
  6576. }
  6577. }
  6578. }
  6579. }
  6580. static void ggml_compute_forward_out_prod_q_f32(
  6581. const struct ggml_compute_params * params,
  6582. struct ggml_tensor * dst) {
  6583. const struct ggml_tensor * src0 = dst->src[0];
  6584. const struct ggml_tensor * src1 = dst->src[1];
  6585. GGML_TENSOR_BINARY_OP_LOCALS;
  6586. const int ith = params->ith;
  6587. const int nth = params->nth;
  6588. const enum ggml_type type = src0->type;
  6589. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  6590. GGML_ASSERT(ne02 == ne12);
  6591. GGML_ASSERT(ne03 == ne13);
  6592. GGML_ASSERT(ne2 == ne12);
  6593. GGML_ASSERT(ne3 == ne13);
  6594. // we don't support permuted src0 dim0
  6595. GGML_ASSERT(nb00 == ggml_type_size(type));
  6596. // dst dim0 cannot be transposed or permuted
  6597. GGML_ASSERT(nb0 == sizeof(float));
  6598. // GGML_ASSERT(nb0 <= nb1);
  6599. // GGML_ASSERT(nb1 <= nb2);
  6600. // GGML_ASSERT(nb2 <= nb3);
  6601. GGML_ASSERT(ne0 == ne00);
  6602. GGML_ASSERT(ne1 == ne10);
  6603. GGML_ASSERT(ne2 == ne02);
  6604. GGML_ASSERT(ne3 == ne03);
  6605. // nb01 >= nb00 - src0 is not transposed
  6606. // compute by src0 rows
  6607. if (ith == 0) {
  6608. ggml_vec_set_f32(ne0*ne1*ne2*ne3, dst->data, 0);
  6609. }
  6610. ggml_barrier(params->threadpool);
  6611. // parallelize by last three dimensions
  6612. // total rows in dst
  6613. const int64_t nr = ne1*ne2*ne3;
  6614. // rows per thread
  6615. const int64_t dr = (nr + nth - 1)/nth;
  6616. // row range for this thread
  6617. const int64_t ir0 = dr*ith;
  6618. const int64_t ir1 = MIN(ir0 + dr, nr);
  6619. // dst[:,:,:,:] = 0
  6620. // for i2,i3:
  6621. // for i1:
  6622. // for i01:
  6623. // for i0:
  6624. // dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3]
  6625. float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith;
  6626. for (int64_t ir = ir0; ir < ir1; ++ir) {
  6627. // dst indices
  6628. const int64_t i3 = ir/(ne2*ne1);
  6629. const int64_t i2 = (ir - i3*ne2*ne1)/ne1;
  6630. const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1);
  6631. const int64_t i02 = i2;
  6632. const int64_t i03 = i3;
  6633. //const int64_t i10 = i1;
  6634. const int64_t i12 = i2;
  6635. const int64_t i13 = i3;
  6636. for (int64_t i01 = 0; i01 < ne01; ++i01) {
  6637. const int64_t i11 = i01;
  6638. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6639. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6640. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6641. dequantize_row_q(s0, wdata, ne0);
  6642. ggml_vec_mad_f32(ne0, d, wdata, *s1);
  6643. }
  6644. }
  6645. }
  6646. static void ggml_compute_forward_out_prod(
  6647. const struct ggml_compute_params * params,
  6648. struct ggml_tensor * dst) {
  6649. const struct ggml_tensor * src0 = dst->src[0];
  6650. switch (src0->type) {
  6651. case GGML_TYPE_Q4_0:
  6652. case GGML_TYPE_Q4_1:
  6653. case GGML_TYPE_Q5_0:
  6654. case GGML_TYPE_Q5_1:
  6655. case GGML_TYPE_Q8_0:
  6656. case GGML_TYPE_Q2_K:
  6657. case GGML_TYPE_Q3_K:
  6658. case GGML_TYPE_Q4_K:
  6659. case GGML_TYPE_Q5_K:
  6660. case GGML_TYPE_Q6_K:
  6661. case GGML_TYPE_TQ1_0:
  6662. case GGML_TYPE_TQ2_0:
  6663. case GGML_TYPE_IQ2_XXS:
  6664. case GGML_TYPE_IQ2_XS:
  6665. case GGML_TYPE_IQ3_XXS:
  6666. case GGML_TYPE_IQ1_S:
  6667. case GGML_TYPE_IQ1_M:
  6668. case GGML_TYPE_IQ4_NL:
  6669. case GGML_TYPE_IQ4_XS:
  6670. case GGML_TYPE_IQ3_S:
  6671. case GGML_TYPE_IQ2_S:
  6672. {
  6673. ggml_compute_forward_out_prod_q_f32(params, dst);
  6674. } break;
  6675. case GGML_TYPE_F16:
  6676. {
  6677. GGML_ABORT("fatal error"); // todo
  6678. // ggml_compute_forward_out_prod_f16_f32(params, dst);
  6679. }
  6680. case GGML_TYPE_F32:
  6681. {
  6682. ggml_compute_forward_out_prod_f32(params, dst);
  6683. } break;
  6684. default:
  6685. {
  6686. GGML_ABORT("fatal error");
  6687. }
  6688. }
  6689. }
  6690. // ggml_compute_forward_scale
  6691. static void ggml_compute_forward_scale_f32(
  6692. const struct ggml_compute_params * params,
  6693. struct ggml_tensor * dst) {
  6694. const struct ggml_tensor * src0 = dst->src[0];
  6695. GGML_ASSERT(ggml_is_contiguous(src0));
  6696. GGML_ASSERT(ggml_is_contiguous(dst));
  6697. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6698. // scale factor
  6699. float v;
  6700. memcpy(&v, dst->op_params, sizeof(float));
  6701. const int ith = params->ith;
  6702. const int nth = params->nth;
  6703. const int nc = src0->ne[0];
  6704. const int nr = ggml_nrows(src0);
  6705. // rows per thread
  6706. const int dr = (nr + nth - 1)/nth;
  6707. // row range for this thread
  6708. const int ir0 = dr*ith;
  6709. const int ir1 = MIN(ir0 + dr, nr);
  6710. const size_t nb01 = src0->nb[1];
  6711. const size_t nb1 = dst->nb[1];
  6712. for (int i1 = ir0; i1 < ir1; i1++) {
  6713. if (dst->data != src0->data) {
  6714. // src0 is same shape as dst => same indices
  6715. memcpy((char *)dst->data + i1*nb1, (char *)src0->data + i1*nb01, nc * sizeof(float));
  6716. }
  6717. ggml_vec_scale_f32(nc, (float *) ((char *) dst->data + i1*nb1), v);
  6718. }
  6719. }
  6720. static void ggml_compute_forward_scale(
  6721. const struct ggml_compute_params * params,
  6722. struct ggml_tensor * dst) {
  6723. const struct ggml_tensor * src0 = dst->src[0];
  6724. switch (src0->type) {
  6725. case GGML_TYPE_F32:
  6726. {
  6727. ggml_compute_forward_scale_f32(params, dst);
  6728. } break;
  6729. default:
  6730. {
  6731. GGML_ABORT("fatal error");
  6732. }
  6733. }
  6734. }
  6735. // ggml_compute_forward_set
  6736. static void ggml_compute_forward_set_f32(
  6737. const struct ggml_compute_params * params,
  6738. struct ggml_tensor * dst) {
  6739. const struct ggml_tensor * src0 = dst->src[0];
  6740. const struct ggml_tensor * src1 = dst->src[1];
  6741. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6742. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  6743. // view src0 and dst with these strides and data offset inbytes during set
  6744. // nb0 is implicitly element_size because src0 and dst are contiguous
  6745. size_t nb1 = ((int32_t *) dst->op_params)[0];
  6746. size_t nb2 = ((int32_t *) dst->op_params)[1];
  6747. size_t nb3 = ((int32_t *) dst->op_params)[2];
  6748. size_t offset = ((int32_t *) dst->op_params)[3];
  6749. bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  6750. if (!inplace) {
  6751. if (params->ith == 0) {
  6752. // memcpy needs to be synchronized across threads to avoid race conditions.
  6753. // => do it in INIT phase
  6754. memcpy(
  6755. ((char *) dst->data),
  6756. ((char *) src0->data),
  6757. ggml_nbytes(dst));
  6758. }
  6759. ggml_barrier(params->threadpool);
  6760. }
  6761. const int ith = params->ith;
  6762. const int nth = params->nth;
  6763. const int nr = ggml_nrows(src1);
  6764. const int nc = src1->ne[0];
  6765. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne)
  6766. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  6767. // src0 and dst as viewed during set
  6768. const size_t nb0 = ggml_element_size(src0);
  6769. const int im0 = (ne10 == 0 ? 0 : ne10-1);
  6770. const int im1 = (ne11 == 0 ? 0 : ne11-1);
  6771. const int im2 = (ne12 == 0 ? 0 : ne12-1);
  6772. const int im3 = (ne13 == 0 ? 0 : ne13-1);
  6773. GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst));
  6774. GGML_ASSERT(nb10 == sizeof(float));
  6775. // rows per thread
  6776. const int dr = (nr + nth - 1)/nth;
  6777. // row range for this thread
  6778. const int ir0 = dr*ith;
  6779. const int ir1 = MIN(ir0 + dr, nr);
  6780. for (int ir = ir0; ir < ir1; ++ir) {
  6781. // src0 and dst are viewed with shape of src1 and offset
  6782. // => same indices
  6783. const int i3 = ir/(ne12*ne11);
  6784. const int i2 = (ir - i3*ne12*ne11)/ne11;
  6785. const int i1 = (ir - i3*ne12*ne11 - i2*ne11);
  6786. ggml_vec_cpy_f32(nc,
  6787. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset),
  6788. (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
  6789. }
  6790. }
  6791. static void ggml_compute_forward_set_i32(
  6792. const struct ggml_compute_params * params,
  6793. struct ggml_tensor * dst) {
  6794. const struct ggml_tensor * src0 = dst->src[0];
  6795. const struct ggml_tensor * src1 = dst->src[1];
  6796. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6797. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  6798. // view src0 and dst with these strides and data offset inbytes during set
  6799. // nb0 is implicitly element_size because src0 and dst are contiguous
  6800. size_t nb1 = ((int32_t *) dst->op_params)[0];
  6801. size_t nb2 = ((int32_t *) dst->op_params)[1];
  6802. size_t nb3 = ((int32_t *) dst->op_params)[2];
  6803. size_t offset = ((int32_t *) dst->op_params)[3];
  6804. bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  6805. if (!inplace) {
  6806. if (params->ith == 0) {
  6807. // memcpy needs to be synchronized across threads to avoid race conditions.
  6808. // => do it in INIT phase
  6809. memcpy(
  6810. ((char *) dst->data),
  6811. ((char *) src0->data),
  6812. ggml_nbytes(dst));
  6813. }
  6814. ggml_barrier(params->threadpool);
  6815. }
  6816. const int ith = params->ith;
  6817. const int nth = params->nth;
  6818. const int nr = ggml_nrows(src1);
  6819. const int nc = src1->ne[0];
  6820. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne)
  6821. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  6822. // src0 and dst as viewed during set
  6823. const size_t nb0 = ggml_element_size(src0);
  6824. const int im0 = (ne10 == 0 ? 0 : ne10-1);
  6825. const int im1 = (ne11 == 0 ? 0 : ne11-1);
  6826. const int im2 = (ne12 == 0 ? 0 : ne12-1);
  6827. const int im3 = (ne13 == 0 ? 0 : ne13-1);
  6828. GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst));
  6829. GGML_ASSERT(nb10 == sizeof(int32_t));
  6830. // rows per thread
  6831. const int dr = (nr + nth - 1)/nth;
  6832. // row range for this thread
  6833. const int ir0 = dr*ith;
  6834. const int ir1 = MIN(ir0 + dr, nr);
  6835. for (int ir = ir0; ir < ir1; ++ir) {
  6836. // src0 and dst are viewed with shape of src1 and offset
  6837. // => same indices
  6838. const int i3 = ir/(ne12*ne11);
  6839. const int i2 = (ir - i3*ne12*ne11)/ne11;
  6840. const int i1 = (ir - i3*ne12*ne11 - i2*ne11);
  6841. ggml_vec_cpy_i32(nc,
  6842. (int32_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset),
  6843. (int32_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
  6844. }
  6845. }
  6846. static void ggml_compute_forward_set(
  6847. const struct ggml_compute_params * params,
  6848. struct ggml_tensor * dst) {
  6849. const struct ggml_tensor * src0 = dst->src[0];
  6850. switch (src0->type) {
  6851. case GGML_TYPE_F32:
  6852. {
  6853. ggml_compute_forward_set_f32(params, dst);
  6854. } break;
  6855. case GGML_TYPE_I32:
  6856. {
  6857. ggml_compute_forward_set_i32(params, dst);
  6858. } break;
  6859. case GGML_TYPE_F16:
  6860. case GGML_TYPE_BF16:
  6861. case GGML_TYPE_Q4_0:
  6862. case GGML_TYPE_Q4_1:
  6863. case GGML_TYPE_Q5_0:
  6864. case GGML_TYPE_Q5_1:
  6865. case GGML_TYPE_Q8_0:
  6866. case GGML_TYPE_Q8_1:
  6867. case GGML_TYPE_Q2_K:
  6868. case GGML_TYPE_Q3_K:
  6869. case GGML_TYPE_Q4_K:
  6870. case GGML_TYPE_Q5_K:
  6871. case GGML_TYPE_Q6_K:
  6872. case GGML_TYPE_TQ1_0:
  6873. case GGML_TYPE_TQ2_0:
  6874. case GGML_TYPE_IQ2_XXS:
  6875. case GGML_TYPE_IQ2_XS:
  6876. case GGML_TYPE_IQ3_XXS:
  6877. case GGML_TYPE_IQ1_S:
  6878. case GGML_TYPE_IQ1_M:
  6879. case GGML_TYPE_IQ4_NL:
  6880. case GGML_TYPE_IQ4_XS:
  6881. case GGML_TYPE_IQ3_S:
  6882. case GGML_TYPE_IQ2_S:
  6883. default:
  6884. {
  6885. GGML_ABORT("fatal error");
  6886. }
  6887. }
  6888. }
  6889. // ggml_compute_forward_cpy
  6890. static void ggml_compute_forward_cpy(
  6891. const struct ggml_compute_params * params,
  6892. struct ggml_tensor * dst) {
  6893. ggml_compute_forward_dup(params, dst);
  6894. }
  6895. // ggml_compute_forward_cont
  6896. static void ggml_compute_forward_cont(
  6897. const struct ggml_compute_params * params,
  6898. struct ggml_tensor * dst) {
  6899. ggml_compute_forward_dup(params, dst);
  6900. }
  6901. // ggml_compute_forward_reshape
  6902. static void ggml_compute_forward_reshape(
  6903. const struct ggml_compute_params * params,
  6904. struct ggml_tensor * dst) {
  6905. // NOP
  6906. UNUSED(params);
  6907. UNUSED(dst);
  6908. }
  6909. // ggml_compute_forward_view
  6910. static void ggml_compute_forward_view(
  6911. const struct ggml_compute_params * params,
  6912. const struct ggml_tensor * dst) {
  6913. // NOP
  6914. UNUSED(params);
  6915. UNUSED(dst);
  6916. }
  6917. // ggml_compute_forward_permute
  6918. static void ggml_compute_forward_permute(
  6919. const struct ggml_compute_params * params,
  6920. const struct ggml_tensor * dst) {
  6921. // NOP
  6922. UNUSED(params);
  6923. UNUSED(dst);
  6924. }
  6925. // ggml_compute_forward_transpose
  6926. static void ggml_compute_forward_transpose(
  6927. const struct ggml_compute_params * params,
  6928. const struct ggml_tensor * dst) {
  6929. // NOP
  6930. UNUSED(params);
  6931. UNUSED(dst);
  6932. }
  6933. // ggml_compute_forward_get_rows
  6934. static void ggml_compute_forward_get_rows_q(
  6935. const struct ggml_compute_params * params,
  6936. struct ggml_tensor * dst) {
  6937. const struct ggml_tensor * src0 = dst->src[0];
  6938. const struct ggml_tensor * src1 = dst->src[1];
  6939. GGML_TENSOR_BINARY_OP_LOCALS
  6940. const int64_t nc = ne00;
  6941. const int64_t nr = ggml_nelements(src1);
  6942. const enum ggml_type type = src0->type;
  6943. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  6944. assert(ne0 == nc);
  6945. assert(ne02 == ne11);
  6946. assert(nb00 == ggml_type_size(type));
  6947. assert(ggml_nrows(dst) == nr);
  6948. const int ith = params->ith;
  6949. const int nth = params->nth;
  6950. // rows per thread
  6951. const int dr = (nr + nth - 1)/nth;
  6952. // row range for this thread
  6953. const int ir0 = dr*ith;
  6954. const int ir1 = MIN(ir0 + dr, nr);
  6955. for (int64_t i = ir0; i < ir1; ++i) {
  6956. const int64_t i12 = i/(ne11*ne10);
  6957. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  6958. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  6959. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  6960. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  6961. dequantize_row_q(
  6962. (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03),
  6963. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc);
  6964. }
  6965. }
  6966. static void ggml_compute_forward_get_rows_f16(
  6967. const struct ggml_compute_params * params,
  6968. struct ggml_tensor * dst) {
  6969. const struct ggml_tensor * src0 = dst->src[0];
  6970. const struct ggml_tensor * src1 = dst->src[1];
  6971. GGML_TENSOR_BINARY_OP_LOCALS
  6972. const int64_t nc = ne00;
  6973. const int64_t nr = ggml_nelements(src1);
  6974. assert(ne0 == nc);
  6975. assert(ne02 == ne11);
  6976. assert(nb00 == sizeof(ggml_fp16_t));
  6977. assert(ggml_nrows(dst) == nr);
  6978. const int ith = params->ith;
  6979. const int nth = params->nth;
  6980. // rows per thread
  6981. const int dr = (nr + nth - 1)/nth;
  6982. // row range for this thread
  6983. const int ir0 = dr*ith;
  6984. const int ir1 = MIN(ir0 + dr, nr);
  6985. for (int64_t i = ir0; i < ir1; ++i) {
  6986. const int64_t i12 = i/(ne11*ne10);
  6987. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  6988. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  6989. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  6990. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  6991. ggml_fp16_to_fp32_row(
  6992. (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03),
  6993. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc);
  6994. }
  6995. }
  6996. static void ggml_compute_forward_get_rows_bf16(
  6997. const struct ggml_compute_params * params,
  6998. struct ggml_tensor * dst) {
  6999. const struct ggml_tensor * src0 = dst->src[0];
  7000. const struct ggml_tensor * src1 = dst->src[1];
  7001. GGML_TENSOR_BINARY_OP_LOCALS
  7002. const int64_t nc = ne00;
  7003. const int64_t nr = ggml_nelements(src1);
  7004. assert(ne0 == nc);
  7005. assert(ne02 == ne11);
  7006. assert(nb00 == sizeof(ggml_bf16_t));
  7007. assert(ggml_nrows(dst) == nr);
  7008. const int ith = params->ith;
  7009. const int nth = params->nth;
  7010. // rows per thread
  7011. const int dr = (nr + nth - 1)/nth;
  7012. // row range for this thread
  7013. const int ir0 = dr*ith;
  7014. const int ir1 = MIN(ir0 + dr, nr);
  7015. for (int64_t i = ir0; i < ir1; ++i) {
  7016. const int64_t i12 = i/(ne11*ne10);
  7017. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  7018. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  7019. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  7020. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  7021. ggml_bf16_to_fp32_row(
  7022. (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03),
  7023. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc);
  7024. }
  7025. }
  7026. static void ggml_compute_forward_get_rows_f32(
  7027. const struct ggml_compute_params * params,
  7028. struct ggml_tensor * dst) {
  7029. const struct ggml_tensor * src0 = dst->src[0];
  7030. const struct ggml_tensor * src1 = dst->src[1];
  7031. GGML_TENSOR_BINARY_OP_LOCALS
  7032. const int64_t nc = ne00;
  7033. const int64_t nr = ggml_nelements(src1);
  7034. assert(ne0 == nc);
  7035. assert(ne02 == ne11);
  7036. assert(nb00 == sizeof(float));
  7037. assert(ggml_nrows(dst) == nr);
  7038. const int ith = params->ith;
  7039. const int nth = params->nth;
  7040. // rows per thread
  7041. const int dr = (nr + nth - 1)/nth;
  7042. // row range for this thread
  7043. const int ir0 = dr*ith;
  7044. const int ir1 = MIN(ir0 + dr, nr);
  7045. for (int64_t i = ir0; i < ir1; ++i) {
  7046. const int64_t i12 = i/(ne11*ne10);
  7047. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  7048. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  7049. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  7050. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  7051. ggml_vec_cpy_f32(nc,
  7052. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3),
  7053. (float *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03));
  7054. }
  7055. }
  7056. static void ggml_compute_forward_get_rows(
  7057. const struct ggml_compute_params * params,
  7058. struct ggml_tensor * dst) {
  7059. const struct ggml_tensor * src0 = dst->src[0];
  7060. switch (src0->type) {
  7061. case GGML_TYPE_Q4_0:
  7062. case GGML_TYPE_Q4_1:
  7063. case GGML_TYPE_Q5_0:
  7064. case GGML_TYPE_Q5_1:
  7065. case GGML_TYPE_Q8_0:
  7066. case GGML_TYPE_Q8_1:
  7067. case GGML_TYPE_Q2_K:
  7068. case GGML_TYPE_Q3_K:
  7069. case GGML_TYPE_Q4_K:
  7070. case GGML_TYPE_Q5_K:
  7071. case GGML_TYPE_Q6_K:
  7072. case GGML_TYPE_TQ1_0:
  7073. case GGML_TYPE_TQ2_0:
  7074. case GGML_TYPE_IQ2_XXS:
  7075. case GGML_TYPE_IQ2_XS:
  7076. case GGML_TYPE_IQ3_XXS:
  7077. case GGML_TYPE_IQ1_S:
  7078. case GGML_TYPE_IQ1_M:
  7079. case GGML_TYPE_IQ4_NL:
  7080. case GGML_TYPE_IQ4_XS:
  7081. case GGML_TYPE_IQ3_S:
  7082. case GGML_TYPE_IQ2_S:
  7083. {
  7084. ggml_compute_forward_get_rows_q(params, dst);
  7085. } break;
  7086. case GGML_TYPE_F16:
  7087. {
  7088. ggml_compute_forward_get_rows_f16(params, dst);
  7089. } break;
  7090. case GGML_TYPE_BF16:
  7091. {
  7092. ggml_compute_forward_get_rows_bf16(params, dst);
  7093. } break;
  7094. case GGML_TYPE_F32:
  7095. case GGML_TYPE_I32:
  7096. {
  7097. ggml_compute_forward_get_rows_f32(params, dst);
  7098. } break;
  7099. default:
  7100. {
  7101. GGML_ABORT("fatal error");
  7102. }
  7103. }
  7104. //static bool first = true;
  7105. //printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]);
  7106. //if (first) {
  7107. // first = false;
  7108. //} else {
  7109. // for (int k = 0; k < dst->ne[1]; ++k) {
  7110. // for (int j = 0; j < dst->ne[0]/16; ++j) {
  7111. // for (int i = 0; i < 16; ++i) {
  7112. // printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]);
  7113. // }
  7114. // printf("\n");
  7115. // }
  7116. // printf("\n");
  7117. // }
  7118. // printf("\n");
  7119. // exit(0);
  7120. //}
  7121. }
  7122. // ggml_compute_forward_get_rows_back
  7123. static void ggml_compute_forward_get_rows_back_f32_f16(
  7124. const struct ggml_compute_params * params,
  7125. struct ggml_tensor * dst) {
  7126. const struct ggml_tensor * src0 = dst->src[0];
  7127. const struct ggml_tensor * src1 = dst->src[1];
  7128. if (params->ith != 0) {
  7129. return;
  7130. }
  7131. GGML_ASSERT(ggml_is_contiguous(dst));
  7132. // ggml_compute_forward_dup_same_cont(params, opt0, dst);
  7133. memset(dst->data, 0, ggml_nbytes(dst));
  7134. const int nc = src0->ne[0];
  7135. const int nr = ggml_nelements(src1);
  7136. GGML_ASSERT( dst->ne[0] == nc);
  7137. GGML_ASSERT(src0->nb[0] == sizeof(ggml_fp16_t));
  7138. for (int i = 0; i < nr; ++i) {
  7139. const int r = ((int32_t *) src1->data)[i];
  7140. for (int j = 0; j < nc; ++j) {
  7141. ggml_fp16_t v = ((ggml_fp16_t *) ((char *) src0->data + i*src0->nb[1]))[j];
  7142. ((float *) ((char *) dst->data + r*dst->nb[1]))[j] += GGML_FP16_TO_FP32(v);
  7143. }
  7144. }
  7145. }
  7146. static void ggml_compute_forward_get_rows_back_f32(
  7147. const struct ggml_compute_params * params,
  7148. struct ggml_tensor * dst) {
  7149. const struct ggml_tensor * src0 = dst->src[0];
  7150. const struct ggml_tensor * src1 = dst->src[1];
  7151. if (params->ith != 0) {
  7152. return;
  7153. }
  7154. GGML_ASSERT(ggml_is_contiguous(dst));
  7155. // ggml_compute_forward_dup_same_cont(params, opt0, dst);
  7156. memset(dst->data, 0, ggml_nbytes(dst));
  7157. const int nc = src0->ne[0];
  7158. const int nr = ggml_nelements(src1);
  7159. GGML_ASSERT( dst->ne[0] == nc);
  7160. GGML_ASSERT(src0->nb[0] == sizeof(float));
  7161. for (int i = 0; i < nr; ++i) {
  7162. const int r = ((int32_t *) src1->data)[i];
  7163. ggml_vec_add_f32(nc,
  7164. (float *) ((char *) dst->data + r*dst->nb[1]),
  7165. (float *) ((char *) dst->data + r*dst->nb[1]),
  7166. (float *) ((char *) src0->data + i*src0->nb[1]));
  7167. }
  7168. }
  7169. static void ggml_compute_forward_get_rows_back(
  7170. const struct ggml_compute_params * params,
  7171. struct ggml_tensor * dst) {
  7172. const struct ggml_tensor * src0 = dst->src[0];
  7173. switch (src0->type) {
  7174. case GGML_TYPE_F16:
  7175. {
  7176. ggml_compute_forward_get_rows_back_f32_f16(params, dst);
  7177. } break;
  7178. case GGML_TYPE_F32:
  7179. {
  7180. ggml_compute_forward_get_rows_back_f32(params, dst);
  7181. } break;
  7182. default:
  7183. {
  7184. GGML_ABORT("fatal error");
  7185. }
  7186. }
  7187. //static bool first = true;
  7188. //printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]);
  7189. //if (first) {
  7190. // first = false;
  7191. //} else {
  7192. // for (int k = 0; k < dst->ne[1]; ++k) {
  7193. // for (int j = 0; j < dst->ne[0]/16; ++j) {
  7194. // for (int i = 0; i < 16; ++i) {
  7195. // printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]);
  7196. // }
  7197. // printf("\n");
  7198. // }
  7199. // printf("\n");
  7200. // }
  7201. // printf("\n");
  7202. // exit(0);
  7203. //}
  7204. }
  7205. // ggml_compute_forward_diag
  7206. static void ggml_compute_forward_diag_f32(
  7207. const struct ggml_compute_params * params,
  7208. struct ggml_tensor * dst) {
  7209. const struct ggml_tensor * src0 = dst->src[0];
  7210. if (params->ith != 0) {
  7211. return;
  7212. }
  7213. // TODO: handle transposed/permuted matrices
  7214. GGML_TENSOR_UNARY_OP_LOCALS
  7215. GGML_ASSERT(ne00 == ne0);
  7216. GGML_ASSERT(ne00 == ne1);
  7217. GGML_ASSERT(ne01 == 1);
  7218. GGML_ASSERT(ne02 == ne2);
  7219. GGML_ASSERT(ne03 == ne3);
  7220. GGML_ASSERT(nb00 == sizeof(float));
  7221. GGML_ASSERT(nb0 == sizeof(float));
  7222. for (int i3 = 0; i3 < ne3; i3++) {
  7223. for (int i2 = 0; i2 < ne2; i2++) {
  7224. for (int i1 = 0; i1 < ne1; i1++) {
  7225. float * d = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  7226. float * s = (float *)((char *) src0->data + i3*nb03 + i2*nb02);
  7227. for (int i0 = 0; i0 < i1; i0++) {
  7228. d[i0] = 0;
  7229. }
  7230. d[i1] = s[i1];
  7231. for (int i0 = i1+1; i0 < ne0; i0++) {
  7232. d[i0] = 0;
  7233. }
  7234. }
  7235. }
  7236. }
  7237. }
  7238. static void ggml_compute_forward_diag(
  7239. const struct ggml_compute_params * params,
  7240. struct ggml_tensor * dst) {
  7241. const struct ggml_tensor * src0 = dst->src[0];
  7242. switch (src0->type) {
  7243. case GGML_TYPE_F32:
  7244. {
  7245. ggml_compute_forward_diag_f32(params, dst);
  7246. } break;
  7247. default:
  7248. {
  7249. GGML_ABORT("fatal error");
  7250. }
  7251. }
  7252. }
  7253. // ggml_compute_forward_diag_mask_inf
  7254. static void ggml_compute_forward_diag_mask_f32(
  7255. const struct ggml_compute_params * params,
  7256. struct ggml_tensor * dst,
  7257. const float value) {
  7258. const struct ggml_tensor * src0 = dst->src[0];
  7259. const int ith = params->ith;
  7260. const int nth = params->nth;
  7261. const int n_past = ((int32_t *) dst->op_params)[0];
  7262. const bool inplace = src0->data == dst->data;
  7263. GGML_ASSERT(n_past >= 0);
  7264. if (!inplace) {
  7265. if (ith == 0) {
  7266. // memcpy needs to be synchronized across threads to avoid race conditions.
  7267. // => do it in INIT phase
  7268. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  7269. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  7270. memcpy(
  7271. ((char *) dst->data),
  7272. ((char *) src0->data),
  7273. ggml_nbytes(dst));
  7274. }
  7275. ggml_barrier(params->threadpool);
  7276. }
  7277. // TODO: handle transposed/permuted matrices
  7278. const int n = ggml_nrows(src0);
  7279. const int nc = src0->ne[0];
  7280. const int nr = src0->ne[1];
  7281. const int nz = n/nr;
  7282. GGML_ASSERT( dst->nb[0] == sizeof(float));
  7283. GGML_ASSERT(src0->nb[0] == sizeof(float));
  7284. for (int k = 0; k < nz; k++) {
  7285. for (int j = ith; j < nr; j += nth) {
  7286. for (int i = n_past; i < nc; i++) {
  7287. if (i > n_past + j) {
  7288. *(float *)((char *) dst->data + k*dst->nb[2] + j*dst->nb[1] + i*dst->nb[0]) = value;
  7289. }
  7290. }
  7291. }
  7292. }
  7293. }
  7294. static void ggml_compute_forward_diag_mask_inf(
  7295. const struct ggml_compute_params * params,
  7296. struct ggml_tensor * dst) {
  7297. const struct ggml_tensor * src0 = dst->src[0];
  7298. switch (src0->type) {
  7299. case GGML_TYPE_F32:
  7300. {
  7301. ggml_compute_forward_diag_mask_f32(params, dst, -INFINITY);
  7302. } break;
  7303. default:
  7304. {
  7305. GGML_ABORT("fatal error");
  7306. }
  7307. }
  7308. }
  7309. static void ggml_compute_forward_diag_mask_zero(
  7310. const struct ggml_compute_params * params,
  7311. struct ggml_tensor * dst) {
  7312. const struct ggml_tensor * src0 = dst->src[0];
  7313. switch (src0->type) {
  7314. case GGML_TYPE_F32:
  7315. {
  7316. ggml_compute_forward_diag_mask_f32(params, dst, 0);
  7317. } break;
  7318. default:
  7319. {
  7320. GGML_ABORT("fatal error");
  7321. }
  7322. }
  7323. }
  7324. // ggml_compute_forward_soft_max
  7325. static void ggml_compute_forward_soft_max_f32(
  7326. const struct ggml_compute_params * params,
  7327. struct ggml_tensor * dst) {
  7328. const struct ggml_tensor * src0 = dst->src[0];
  7329. const struct ggml_tensor * src1 = dst->src[1];
  7330. assert(ggml_is_contiguous(dst));
  7331. assert(ggml_are_same_shape(src0, dst));
  7332. float scale = 1.0f;
  7333. float max_bias = 0.0f;
  7334. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  7335. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  7336. // TODO: handle transposed/permuted matrices
  7337. const int ith = params->ith;
  7338. const int nth = params->nth;
  7339. GGML_TENSOR_UNARY_OP_LOCALS
  7340. //const int64_t ne11 = src1 ? src1->ne[1] : 1;
  7341. // TODO: is this supposed to be ceil instead of floor?
  7342. // https://huggingface.co/mosaicml/mpt-7b/blob/main/attention.py#L370
  7343. const uint32_t n_head = ne02;
  7344. const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head));
  7345. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  7346. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  7347. const int nc = src0->ne[0];
  7348. const int nr = ggml_nrows(src0);
  7349. // rows per thread
  7350. const int dr = (nr + nth - 1)/nth;
  7351. // row range for this thread
  7352. const int ir0 = dr*ith;
  7353. const int ir1 = MIN(ir0 + dr, nr);
  7354. float * wp = (float *) params->wdata + (nc + CACHE_LINE_SIZE_F32) * ith;
  7355. const bool use_f16 = (src1 && src1->type == GGML_TYPE_F16);
  7356. for (int i1 = ir0; i1 < ir1; i1++) {
  7357. // ALiBi
  7358. const uint32_t h = (i1/ne01)%ne02; // head
  7359. 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;
  7360. float * sp = (float *)((char *) src0->data + i1*src0->nb[1]);
  7361. float * dp = (float *)((char *) dst->data + i1*dst->nb[1]);
  7362. // broadcast the mask across rows
  7363. ggml_fp16_t * mp_f16 = src1 ? (ggml_fp16_t *)((char *) src1->data) + (i1%ne01)*ne00 : NULL;
  7364. float * mp_f32 = src1 ? (float *)((char *) src1->data) + (i1%ne01)*ne00 : NULL;
  7365. ggml_vec_cpy_f32 (nc, wp, sp);
  7366. ggml_vec_scale_f32(nc, wp, scale);
  7367. if (mp_f32) {
  7368. if (use_f16) {
  7369. for (int i = 0; i < nc; ++i) {
  7370. wp[i] += slope*GGML_FP16_TO_FP32(mp_f16[i]);
  7371. }
  7372. } else {
  7373. for (int i = 0; i < nc; ++i) {
  7374. wp[i] += slope*mp_f32[i];
  7375. }
  7376. }
  7377. }
  7378. #ifndef NDEBUG
  7379. for (int i = 0; i < nc; ++i) {
  7380. //printf("p[%d] = %f\n", i, p[i]);
  7381. assert(!isnan(wp[i]));
  7382. }
  7383. #endif
  7384. float max = -INFINITY;
  7385. ggml_vec_max_f32(nc, &max, wp);
  7386. ggml_float sum = ggml_vec_soft_max_f32(nc, dp, wp, max);
  7387. assert(sum > 0.0);
  7388. sum = 1.0/sum;
  7389. ggml_vec_scale_f32(nc, dp, sum);
  7390. #ifndef NDEBUG
  7391. for (int i = 0; i < nc; ++i) {
  7392. assert(!isnan(dp[i]));
  7393. assert(!isinf(dp[i]));
  7394. }
  7395. #endif
  7396. }
  7397. }
  7398. static void ggml_compute_forward_soft_max(
  7399. const struct ggml_compute_params * params,
  7400. struct ggml_tensor * dst) {
  7401. const struct ggml_tensor * src0 = dst->src[0];
  7402. switch (src0->type) {
  7403. case GGML_TYPE_F32:
  7404. {
  7405. ggml_compute_forward_soft_max_f32(params, dst);
  7406. } break;
  7407. default:
  7408. {
  7409. GGML_ABORT("fatal error");
  7410. }
  7411. }
  7412. }
  7413. // ggml_compute_forward_soft_max_ext_back
  7414. static void ggml_compute_forward_soft_max_ext_back_f32(
  7415. const struct ggml_compute_params * params,
  7416. struct ggml_tensor * dst) {
  7417. const struct ggml_tensor * src0 = dst->src[0];
  7418. const struct ggml_tensor * src1 = dst->src[1];
  7419. GGML_ASSERT(ggml_is_contiguous(src0));
  7420. GGML_ASSERT(ggml_is_contiguous(src1));
  7421. GGML_ASSERT(ggml_is_contiguous(dst));
  7422. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  7423. GGML_ASSERT(ggml_are_same_shape(src1, dst));
  7424. float scale = 1.0f;
  7425. float max_bias = 0.0f;
  7426. memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));
  7427. memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
  7428. GGML_ASSERT(max_bias == 0.0f);
  7429. // TODO: handle transposed/permuted matrices
  7430. const int ith = params->ith;
  7431. const int nth = params->nth;
  7432. const int nc = src0->ne[0];
  7433. const int nr = ggml_nrows(src0);
  7434. // rows per thread
  7435. const int dr = (nr + nth - 1)/nth;
  7436. // row range for this thread
  7437. const int ir0 = dr*ith;
  7438. const int ir1 = MIN(ir0 + dr, nr);
  7439. for (int i1 = ir0; i1 < ir1; i1++) {
  7440. float *dy = (float *)((char *) src0->data + i1*src0->nb[1]);
  7441. float *y = (float *)((char *) src1->data + i1*src1->nb[1]);
  7442. float *dx = (float *)((char *) dst->data + i1*dst->nb[1]);
  7443. #ifndef NDEBUG
  7444. for (int i = 0; i < nc; ++i) {
  7445. //printf("p[%d] = %f\n", i, p[i]);
  7446. assert(!isnan(dy[i]));
  7447. assert(!isnan(y[i]));
  7448. }
  7449. #endif
  7450. // Jii = yi - yi*yi
  7451. // Jij = -yi*yj
  7452. // J = diag(y)-y.T*y
  7453. // dx = J * dy
  7454. // dxk = sum_i(Jki * dyi)
  7455. // dxk = sum_i(-yk*yi * dyi) - (-yk*yk)*dyk + (yk - yk*yk)*dyk
  7456. // dxk = sum_i(-yk*yi * dyi) + yk*yk*dyk + yk*dyk - yk*yk*dyk
  7457. // dxk = sum_i(-yk*yi * dyi) + yk*dyk
  7458. // dxk = -yk * sum_i(yi * dyi) + yk*dyk
  7459. // dxk = -yk * dot(y, dy) + yk*dyk
  7460. // dxk = yk * (- dot(y, dy) + dyk)
  7461. // dxk = yk * (dyk - dot(y, dy))
  7462. //
  7463. // post-order:
  7464. // dot_y_dy := dot(y, dy)
  7465. // dx := dy
  7466. // dx := dx - dot_y_dy
  7467. // dx := dx * y
  7468. // linear runtime, no additional memory
  7469. float dot_y_dy = 0;
  7470. ggml_vec_dot_f32 (nc, &dot_y_dy, 0, y, 0, dy, 0, 1);
  7471. ggml_vec_cpy_f32 (nc, dx, dy);
  7472. ggml_vec_acc1_f32 (nc, dx, -dot_y_dy);
  7473. ggml_vec_mul_f32 (nc, dx, dx, y);
  7474. ggml_vec_scale_f32(nc, dx, scale);
  7475. #ifndef NDEBUG
  7476. for (int i = 0; i < nc; ++i) {
  7477. assert(!isnan(dx[i]));
  7478. assert(!isinf(dx[i]));
  7479. }
  7480. #endif
  7481. }
  7482. }
  7483. static void ggml_compute_forward_soft_max_ext_back(
  7484. const struct ggml_compute_params * params,
  7485. struct ggml_tensor * dst) {
  7486. const struct ggml_tensor * src0 = dst->src[0];
  7487. switch (src0->type) {
  7488. case GGML_TYPE_F32:
  7489. {
  7490. ggml_compute_forward_soft_max_ext_back_f32(params, dst);
  7491. } break;
  7492. default:
  7493. {
  7494. GGML_ABORT("fatal error");
  7495. }
  7496. }
  7497. }
  7498. // ggml_compute_forward_clamp
  7499. static void ggml_compute_forward_clamp_f32(
  7500. const struct ggml_compute_params * params,
  7501. struct ggml_tensor * dst) {
  7502. const struct ggml_tensor * src0 = dst->src[0];
  7503. if (params->ith != 0) {
  7504. return;
  7505. }
  7506. float min;
  7507. float max;
  7508. memcpy(&min, (float *) dst->op_params + 0, sizeof(float));
  7509. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  7510. const int ith = params->ith;
  7511. const int nth = params->nth;
  7512. const int n = ggml_nrows(src0);
  7513. const int nc = src0->ne[0];
  7514. const size_t nb00 = src0->nb[0];
  7515. const size_t nb01 = src0->nb[1];
  7516. const size_t nb0 = dst->nb[0];
  7517. const size_t nb1 = dst->nb[1];
  7518. GGML_ASSERT( nb0 == sizeof(float));
  7519. GGML_ASSERT(nb00 == sizeof(float));
  7520. for (int j = ith; j < n; j += nth) {
  7521. float * dst_ptr = (float *) ((char *) dst->data + j*nb1);
  7522. float * src0_ptr = (float *) ((char *) src0->data + j*nb01);
  7523. for (int i = 0; i < nc; i++) {
  7524. dst_ptr[i] = MAX(MIN(src0_ptr[i], max), min);
  7525. }
  7526. }
  7527. }
  7528. static void ggml_compute_forward_clamp(
  7529. const struct ggml_compute_params * params,
  7530. struct ggml_tensor * dst) {
  7531. const struct ggml_tensor * src0 = dst->src[0];
  7532. switch (src0->type) {
  7533. case GGML_TYPE_F32:
  7534. {
  7535. ggml_compute_forward_clamp_f32(params, dst);
  7536. } break;
  7537. case GGML_TYPE_F16:
  7538. case GGML_TYPE_BF16:
  7539. case GGML_TYPE_Q4_0:
  7540. case GGML_TYPE_Q4_1:
  7541. case GGML_TYPE_Q5_0:
  7542. case GGML_TYPE_Q5_1:
  7543. case GGML_TYPE_Q8_0:
  7544. case GGML_TYPE_Q8_1:
  7545. case GGML_TYPE_Q2_K:
  7546. case GGML_TYPE_Q3_K:
  7547. case GGML_TYPE_Q4_K:
  7548. case GGML_TYPE_Q5_K:
  7549. case GGML_TYPE_Q6_K:
  7550. case GGML_TYPE_TQ1_0:
  7551. case GGML_TYPE_TQ2_0:
  7552. case GGML_TYPE_IQ2_XXS:
  7553. case GGML_TYPE_IQ2_XS:
  7554. case GGML_TYPE_IQ3_XXS:
  7555. case GGML_TYPE_IQ1_S:
  7556. case GGML_TYPE_IQ1_M:
  7557. case GGML_TYPE_IQ4_NL:
  7558. case GGML_TYPE_IQ4_XS:
  7559. case GGML_TYPE_IQ3_S:
  7560. case GGML_TYPE_IQ2_S:
  7561. case GGML_TYPE_Q8_K:
  7562. case GGML_TYPE_I8:
  7563. case GGML_TYPE_I16:
  7564. case GGML_TYPE_I32:
  7565. case GGML_TYPE_I64:
  7566. case GGML_TYPE_F64:
  7567. case GGML_TYPE_COUNT:
  7568. {
  7569. GGML_ABORT("fatal error");
  7570. }
  7571. }
  7572. }
  7573. // ggml_compute_forward_rope
  7574. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  7575. const float y = (i0 / 2 - low) / MAX(0.001f, high - low);
  7576. return 1 - MIN(1, MAX(0, y));
  7577. }
  7578. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  7579. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  7580. static void rope_yarn(
  7581. float theta_extrap, float freq_scale, float corr_dims[2], int64_t i0, float ext_factor, float mscale,
  7582. float * cos_theta, float * sin_theta) {
  7583. // Get n-d rotational scaling corrected for extrapolation
  7584. float theta_interp = freq_scale * theta_extrap;
  7585. float theta = theta_interp;
  7586. if (ext_factor != 0.0f) {
  7587. float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor;
  7588. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  7589. // Get n-d magnitude scaling corrected for interpolation
  7590. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  7591. }
  7592. *cos_theta = cosf(theta) * mscale;
  7593. *sin_theta = sinf(theta) * mscale;
  7594. }
  7595. static void ggml_rope_cache_init(
  7596. float theta_base, float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale,
  7597. float * cache, float sin_sign, float theta_scale) {
  7598. // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py
  7599. float theta = theta_base;
  7600. for (int64_t i0 = 0; i0 < ne0; i0 += 2) {
  7601. const float ff = freq_factors ? freq_factors[i0/2] : 1.0f;
  7602. rope_yarn(
  7603. theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1]
  7604. );
  7605. cache[i0 + 1] *= sin_sign;
  7606. theta *= theta_scale;
  7607. }
  7608. }
  7609. static void ggml_mrope_cache_init(
  7610. float theta_base_t, float theta_base_h, float theta_base_w, float theta_base_e, int sections[4], bool indep_sects,
  7611. float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale,
  7612. float * cache, float sin_sign, float theta_scale) {
  7613. // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py
  7614. float theta_t = theta_base_t;
  7615. float theta_h = theta_base_h;
  7616. float theta_w = theta_base_w;
  7617. float theta_e = theta_base_e; // extra position id for vision encoder
  7618. int sect_dims = sections[0] + sections[1] + sections[2] + sections[3];
  7619. int sec_w = sections[1] + sections[0];
  7620. int sec_e = sections[2] + sec_w;
  7621. GGML_ASSERT(sect_dims <= ne0);
  7622. for (int64_t i0 = 0; i0 < ne0; i0 += 2) {
  7623. const float ff = freq_factors ? freq_factors[i0/2] : 1.0f;
  7624. int sector = (i0 / 2) % sect_dims;
  7625. if (indep_sects) {
  7626. // compute theta independently for each dim sections
  7627. // (i.e. reset corresponding theta when `i0` go from one section to another)
  7628. if (sector == 0) {
  7629. theta_t = theta_base_t;
  7630. }
  7631. else if (sector == sections[0]) {
  7632. theta_h = theta_base_h;;
  7633. }
  7634. else if (sector == sec_w) {
  7635. theta_w = theta_base_w;
  7636. }
  7637. else if (sector == sec_e) {
  7638. theta_e = theta_base_e;
  7639. }
  7640. }
  7641. float theta = theta_t;
  7642. if (sector >= sections[0] && sector < sec_w) {
  7643. theta = theta_h;
  7644. }
  7645. else if (sector >= sec_w && sector < sec_w + sections[2]) {
  7646. theta = theta_w;
  7647. }
  7648. else if (sector >= sec_w + sections[2]) {
  7649. theta = theta_e;
  7650. }
  7651. rope_yarn(
  7652. theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1]
  7653. );
  7654. cache[i0 + 1] *= sin_sign;
  7655. theta_t *= theta_scale;
  7656. theta_w *= theta_scale;
  7657. theta_h *= theta_scale;
  7658. theta_e *= theta_scale;
  7659. }
  7660. }
  7661. static void ggml_compute_forward_rope_f32(
  7662. const struct ggml_compute_params * params,
  7663. struct ggml_tensor * dst,
  7664. const bool forward) {
  7665. const struct ggml_tensor * src0 = dst->src[0];
  7666. const struct ggml_tensor * src1 = dst->src[1];
  7667. const struct ggml_tensor * src2 = dst->src[2];
  7668. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7669. int sections[4];
  7670. //const int n_past = ((int32_t *) dst->op_params)[0];
  7671. const int n_dims = ((int32_t *) dst->op_params)[1];
  7672. const int mode = ((int32_t *) dst->op_params)[2];
  7673. //const int n_ctx = ((int32_t *) dst->op_params)[3];
  7674. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  7675. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7676. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7677. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7678. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7679. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7680. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7681. memcpy(&sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  7682. GGML_TENSOR_UNARY_OP_LOCALS
  7683. //printf("ne0: %d, ne1: %d, ne2: %d, ne3: %d\n", ne0, ne1, ne2, ne3);
  7684. //printf("n_past = %d, ne2 = %d\n", n_past, ne2);
  7685. GGML_ASSERT(nb00 == sizeof(float));
  7686. const int ith = params->ith;
  7687. const int nth = params->nth;
  7688. const int nr = ggml_nrows(dst);
  7689. GGML_ASSERT(n_dims <= ne0);
  7690. GGML_ASSERT(n_dims % 2 == 0);
  7691. // rows per thread
  7692. const int dr = (nr + nth - 1)/nth;
  7693. // row range for this thread
  7694. const int ir0 = dr*ith;
  7695. const int ir1 = MIN(ir0 + dr, nr);
  7696. // row index used to determine which thread to use
  7697. int ir = 0;
  7698. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  7699. float corr_dims[2];
  7700. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  7701. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  7702. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE; // ggml_rope_multi, multimodal rotary position embedding
  7703. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  7704. if (is_mrope) {
  7705. GGML_ASSERT(sections[0] > 0 || sections[1] > 0 || sections[2] > 0);
  7706. }
  7707. if (is_vision) {
  7708. GGML_ASSERT(n_dims == ne0/2);
  7709. }
  7710. const float * freq_factors = NULL;
  7711. if (src2 != NULL) {
  7712. GGML_ASSERT(src2->type == GGML_TYPE_F32);
  7713. GGML_ASSERT(src2->ne[0] >= n_dims / 2);
  7714. freq_factors = (const float *) src2->data;
  7715. }
  7716. // backward process uses inverse rotation by cos and sin.
  7717. // cos and sin build a rotation matrix, where the inverse is the transpose.
  7718. // this essentially just switches the sign of sin.
  7719. const float sin_sign = forward ? 1.0f : -1.0f;
  7720. const int32_t * pos = (const int32_t *) src1->data;
  7721. for (int64_t i3 = 0; i3 < ne3; i3++) { // batch
  7722. for (int64_t i2 = 0; i2 < ne2; i2++) { // seq-len
  7723. float * cache = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32)*ith;
  7724. if (!is_mrope) {
  7725. const int64_t p = pos[i2];
  7726. ggml_rope_cache_init(p, freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7727. }
  7728. else {
  7729. const int64_t p_t = pos[i2];
  7730. const int64_t p_h = pos[i2 + ne2];
  7731. const int64_t p_w = pos[i2 + ne2 * 2];
  7732. const int64_t p_e = pos[i2 + ne2 * 3];
  7733. ggml_mrope_cache_init(
  7734. p_t, p_h, p_w, p_e, sections, is_vision,
  7735. freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7736. }
  7737. for (int64_t i1 = 0; i1 < ne1; i1++) { // attn-heads
  7738. if (ir++ < ir0) continue;
  7739. if (ir > ir1) break;
  7740. if (is_neox || is_mrope) {
  7741. if (is_vision){
  7742. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7743. const int64_t ic = i0/2;
  7744. const float cos_theta = cache[i0 + 0];
  7745. const float sin_theta = cache[i0 + 1];
  7746. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7747. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7748. const float x0 = src[0];
  7749. const float x1 = src[n_dims];
  7750. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7751. dst_data[n_dims] = x0*sin_theta + x1*cos_theta;
  7752. }
  7753. } else {
  7754. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7755. const int64_t ic = i0/2;
  7756. const float cos_theta = cache[i0 + 0];
  7757. const float sin_theta = cache[i0 + 1];
  7758. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7759. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7760. const float x0 = src[0];
  7761. const float x1 = src[n_dims/2];
  7762. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7763. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  7764. }
  7765. }
  7766. } else {
  7767. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7768. const float cos_theta = cache[i0 + 0];
  7769. const float sin_theta = cache[i0 + 1];
  7770. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7771. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7772. const float x0 = src[0];
  7773. const float x1 = src[1];
  7774. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7775. dst_data[1] = x0*sin_theta + x1*cos_theta;
  7776. }
  7777. }
  7778. if (is_vision) {
  7779. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7780. const int64_t ic = i0/2;
  7781. const float cos_theta = cache[i0 + 0];
  7782. const float sin_theta = cache[i0 + 1];
  7783. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7784. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7785. const float x0 = src[0];
  7786. const float x1 = src[n_dims];
  7787. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7788. dst_data[n_dims] = x0*sin_theta + x1*cos_theta;
  7789. }
  7790. } else {
  7791. // fill the remain channels with data from src tensor
  7792. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7793. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7794. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7795. dst_data[0] = src[0];
  7796. dst_data[1] = src[1];
  7797. }
  7798. }
  7799. }
  7800. }
  7801. }
  7802. }
  7803. // TODO: deduplicate f16/f32 code
  7804. static void ggml_compute_forward_rope_f16(
  7805. const struct ggml_compute_params * params,
  7806. struct ggml_tensor * dst,
  7807. const bool forward) {
  7808. const struct ggml_tensor * src0 = dst->src[0];
  7809. const struct ggml_tensor * src1 = dst->src[1];
  7810. const struct ggml_tensor * src2 = dst->src[2];
  7811. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7812. int sections[4];
  7813. //const int n_past = ((int32_t *) dst->op_params)[0];
  7814. const int n_dims = ((int32_t *) dst->op_params)[1];
  7815. const int mode = ((int32_t *) dst->op_params)[2];
  7816. //const int n_ctx = ((int32_t *) dst->op_params)[3];
  7817. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  7818. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7819. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7820. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7821. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7822. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7823. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7824. memcpy(&sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  7825. GGML_TENSOR_UNARY_OP_LOCALS
  7826. //printf("ne0: %d, ne1: %d, ne2: %d, ne3: %d\n", ne0, ne1, ne2, ne3);
  7827. //printf("n_past = %d, ne2 = %d\n", n_past, ne2);
  7828. GGML_ASSERT(nb0 == sizeof(ggml_fp16_t));
  7829. const int ith = params->ith;
  7830. const int nth = params->nth;
  7831. const int nr = ggml_nrows(dst);
  7832. GGML_ASSERT(n_dims <= ne0);
  7833. GGML_ASSERT(n_dims % 2 == 0);
  7834. // rows per thread
  7835. const int dr = (nr + nth - 1)/nth;
  7836. // row range for this thread
  7837. const int ir0 = dr*ith;
  7838. const int ir1 = MIN(ir0 + dr, nr);
  7839. // row index used to determine which thread to use
  7840. int ir = 0;
  7841. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  7842. float corr_dims[2];
  7843. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  7844. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  7845. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  7846. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  7847. if (is_mrope) {
  7848. GGML_ASSERT(sections[0] > 0 || sections[1] > 0 || sections[2] > 0);
  7849. }
  7850. if (is_vision) {
  7851. GGML_ASSERT(n_dims == ne0/2);
  7852. }
  7853. const float * freq_factors = NULL;
  7854. if (src2 != NULL) {
  7855. GGML_ASSERT(src2->type == GGML_TYPE_F32);
  7856. GGML_ASSERT(src2->ne[0] >= n_dims / 2);
  7857. freq_factors = (const float *) src2->data;
  7858. }
  7859. // backward process uses inverse rotation by cos and sin.
  7860. // cos and sin build a rotation matrix, where the inverse is the transpose.
  7861. // this essentially just switches the sign of sin.
  7862. const float sin_sign = forward ? 1.0f : -1.0f;
  7863. const int32_t * pos = (const int32_t *) src1->data;
  7864. for (int64_t i3 = 0; i3 < ne3; i3++) {
  7865. for (int64_t i2 = 0; i2 < ne2; i2++) {
  7866. float * cache = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32)*ith;
  7867. if (!is_mrope) {
  7868. const int64_t p = pos[i2];
  7869. ggml_rope_cache_init(p, freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7870. }
  7871. else {
  7872. const int64_t p_t = pos[i2];
  7873. const int64_t p_h = pos[i2 + ne2];
  7874. const int64_t p_w = pos[i2 + ne2 * 2];
  7875. const int64_t p_e = pos[i2 + ne2 * 3];
  7876. ggml_mrope_cache_init(
  7877. p_t, p_h, p_w, p_e, sections, is_vision,
  7878. freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7879. }
  7880. for (int64_t i1 = 0; i1 < ne1; i1++) {
  7881. if (ir++ < ir0) continue;
  7882. if (ir > ir1) break;
  7883. if (is_neox || is_mrope) {
  7884. if (is_vision) {
  7885. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7886. const int64_t ic = i0/2;
  7887. const float cos_theta = cache[i0 + 0];
  7888. const float sin_theta = cache[i0 + 1];
  7889. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7890. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7891. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7892. const float x1 = GGML_FP16_TO_FP32(src[n_dims]);
  7893. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7894. dst_data[n_dims] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7895. }
  7896. } else {
  7897. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7898. const int64_t ic = i0/2;
  7899. const float cos_theta = cache[i0 + 0];
  7900. const float sin_theta = cache[i0 + 1];
  7901. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7902. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7903. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7904. const float x1 = GGML_FP16_TO_FP32(src[n_dims/2]);
  7905. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7906. dst_data[n_dims/2] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7907. }
  7908. }
  7909. } else {
  7910. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7911. const float cos_theta = cache[i0 + 0];
  7912. const float sin_theta = cache[i0 + 1];
  7913. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7914. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7915. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7916. const float x1 = GGML_FP16_TO_FP32(src[1]);
  7917. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7918. dst_data[1] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7919. }
  7920. }
  7921. if (is_vision) {
  7922. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7923. const int64_t ic = i0/2;
  7924. const float cos_theta = cache[i0 + 0];
  7925. const float sin_theta = cache[i0 + 1];
  7926. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7927. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7928. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7929. const float x1 = GGML_FP16_TO_FP32(src[n_dims]);
  7930. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7931. dst_data[n_dims] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7932. }
  7933. } else {
  7934. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7935. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7936. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7937. dst_data[0] = src[0];
  7938. dst_data[1] = src[1];
  7939. }
  7940. }
  7941. }
  7942. }
  7943. }
  7944. }
  7945. static void ggml_compute_forward_rope(
  7946. const struct ggml_compute_params * params,
  7947. struct ggml_tensor * dst) {
  7948. const struct ggml_tensor * src0 = dst->src[0];
  7949. switch (src0->type) {
  7950. case GGML_TYPE_F16:
  7951. {
  7952. ggml_compute_forward_rope_f16(params, dst, true);
  7953. } break;
  7954. case GGML_TYPE_F32:
  7955. {
  7956. ggml_compute_forward_rope_f32(params, dst, true);
  7957. } break;
  7958. default:
  7959. {
  7960. GGML_ABORT("fatal error");
  7961. }
  7962. }
  7963. }
  7964. // ggml_compute_forward_rope_back
  7965. static void ggml_compute_forward_rope_back(
  7966. const struct ggml_compute_params * params,
  7967. struct ggml_tensor * dst) {
  7968. const struct ggml_tensor * src0 = dst->src[0];
  7969. switch (src0->type) {
  7970. case GGML_TYPE_F16:
  7971. {
  7972. ggml_compute_forward_rope_f16(params, dst, false);
  7973. } break;
  7974. case GGML_TYPE_F32:
  7975. {
  7976. ggml_compute_forward_rope_f32(params, dst, false);
  7977. } break;
  7978. default:
  7979. {
  7980. GGML_ABORT("fatal error");
  7981. }
  7982. }
  7983. }
  7984. // ggml_compute_forward_conv_transpose_1d
  7985. static void ggml_compute_forward_conv_transpose_1d_f16_f32(
  7986. const struct ggml_compute_params * params,
  7987. struct ggml_tensor * dst) {
  7988. const struct ggml_tensor * src0 = dst->src[0];
  7989. const struct ggml_tensor * src1 = dst->src[1];
  7990. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7991. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7992. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7993. GGML_TENSOR_BINARY_OP_LOCALS
  7994. const int ith = params->ith;
  7995. const int nth = params->nth;
  7996. const int nk = ne00*ne01*ne02;
  7997. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  7998. GGML_ASSERT(nb10 == sizeof(float));
  7999. if (ith == 0) {
  8000. memset(params->wdata, 0, params->wsize);
  8001. // permute kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout)
  8002. {
  8003. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  8004. for (int64_t i02 = 0; i02 < ne02; i02++) {
  8005. for (int64_t i01 = 0; i01 < ne01; i01++) {
  8006. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i02*nb02 + i01*nb01);
  8007. ggml_fp16_t * dst_data = wdata + i01*ne00*ne02;
  8008. for (int64_t i00 = 0; i00 < ne00; i00++) {
  8009. dst_data[i00*ne02 + i02] = src[i00];
  8010. }
  8011. }
  8012. }
  8013. }
  8014. // permute source data (src1) from (L x Cin) to (Cin x L)
  8015. {
  8016. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + nk;
  8017. ggml_fp16_t * dst_data = wdata;
  8018. for (int64_t i11 = 0; i11 < ne11; i11++) {
  8019. const float * const src = (float *)((char *) src1->data + i11*nb11);
  8020. for (int64_t i10 = 0; i10 < ne10; i10++) {
  8021. dst_data[i10*ne11 + i11] = GGML_FP32_TO_FP16(src[i10]);
  8022. }
  8023. }
  8024. }
  8025. // need to zero dst since we are accumulating into it
  8026. memset(dst->data, 0, ggml_nbytes(dst));
  8027. }
  8028. ggml_barrier(params->threadpool);
  8029. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  8030. // total rows in dst
  8031. const int nr = ne1;
  8032. // rows per thread
  8033. const int dr = (nr + nth - 1)/nth;
  8034. // row range for this thread
  8035. const int ir0 = dr*ith;
  8036. const int ir1 = MIN(ir0 + dr, nr);
  8037. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  8038. ggml_fp16_t * const wdata_src = wdata + nk;
  8039. for (int i1 = ir0; i1 < ir1; i1++) {
  8040. float * dst_data = (float *)((char *) dst->data + i1*nb1);
  8041. ggml_fp16_t * wdata_kernel = wdata + i1*ne02*ne00;
  8042. for (int i10 = 0; i10 < ne10; i10++) {
  8043. const int i1n = i10*ne11;
  8044. for (int i00 = 0; i00 < ne00; i00++) {
  8045. float v = 0;
  8046. ggml_vec_dot_f16(ne02, &v, 0,
  8047. (ggml_fp16_t *) wdata_src + i1n, 0,
  8048. (ggml_fp16_t *) wdata_kernel + i00*ne02, 0, 1);
  8049. dst_data[i10*s0 + i00] += v;
  8050. }
  8051. }
  8052. }
  8053. }
  8054. static void ggml_compute_forward_conv_transpose_1d_f32(
  8055. const struct ggml_compute_params * params,
  8056. struct ggml_tensor * dst) {
  8057. const struct ggml_tensor * src0 = dst->src[0];
  8058. const struct ggml_tensor * src1 = dst->src[1];
  8059. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8060. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8061. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8062. GGML_TENSOR_BINARY_OP_LOCALS
  8063. const int ith = params->ith;
  8064. const int nth = params->nth;
  8065. const int nk = ne00*ne01*ne02;
  8066. GGML_ASSERT(nb00 == sizeof(float));
  8067. GGML_ASSERT(nb10 == sizeof(float));
  8068. if (ith == 0) {
  8069. memset(params->wdata, 0, params->wsize);
  8070. // prepare kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout)
  8071. {
  8072. float * const wdata = (float *) params->wdata + 0;
  8073. for (int64_t i02 = 0; i02 < ne02; i02++) {
  8074. for (int64_t i01 = 0; i01 < ne01; i01++) {
  8075. const float * const src = (float *)((char *) src0->data + i02*nb02 + i01*nb01);
  8076. float * dst_data = wdata + i01*ne00*ne02;
  8077. for (int64_t i00 = 0; i00 < ne00; i00++) {
  8078. dst_data[i00*ne02 + i02] = src[i00];
  8079. }
  8080. }
  8081. }
  8082. }
  8083. // prepare source data (src1)
  8084. {
  8085. float * const wdata = (float *) params->wdata + nk;
  8086. float * dst_data = wdata;
  8087. for (int64_t i11 = 0; i11 < ne11; i11++) {
  8088. const float * const src = (float *)((char *) src1->data + i11*nb11);
  8089. for (int64_t i10 = 0; i10 < ne10; i10++) {
  8090. dst_data[i10*ne11 + i11] = src[i10];
  8091. }
  8092. }
  8093. }
  8094. // need to zero dst since we are accumulating into it
  8095. memset(dst->data, 0, ggml_nbytes(dst));
  8096. }
  8097. ggml_barrier(params->threadpool);
  8098. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  8099. // total rows in dst
  8100. const int nr = ne1;
  8101. // rows per thread
  8102. const int dr = (nr + nth - 1)/nth;
  8103. // row range for this thread
  8104. const int ir0 = dr*ith;
  8105. const int ir1 = MIN(ir0 + dr, nr);
  8106. float * const wdata = (float *) params->wdata + 0;
  8107. float * const wdata_src = wdata + nk;
  8108. for (int i1 = ir0; i1 < ir1; i1++) {
  8109. float * dst_data = (float *)((char *) dst->data + i1*nb1);
  8110. float * wdata_kernel = wdata + i1*ne02*ne00;
  8111. for (int i10 = 0; i10 < ne10; i10++) {
  8112. const int i1n = i10*ne11;
  8113. for (int i00 = 0; i00 < ne00; i00++) {
  8114. float v = 0;
  8115. ggml_vec_dot_f32(ne02, &v, 0,
  8116. wdata_src + i1n, 0,
  8117. wdata_kernel + i00*ne02, 0, 1);
  8118. dst_data[i10*s0 + i00] += v;
  8119. }
  8120. }
  8121. }
  8122. }
  8123. static void ggml_compute_forward_conv_transpose_1d(
  8124. const struct ggml_compute_params * params,
  8125. struct ggml_tensor * dst) {
  8126. const struct ggml_tensor * src0 = dst->src[0];
  8127. switch (src0->type) {
  8128. case GGML_TYPE_F16:
  8129. {
  8130. ggml_compute_forward_conv_transpose_1d_f16_f32(params, dst);
  8131. } break;
  8132. case GGML_TYPE_F32:
  8133. {
  8134. ggml_compute_forward_conv_transpose_1d_f32(params, dst);
  8135. } break;
  8136. default:
  8137. {
  8138. GGML_ABORT("fatal error");
  8139. }
  8140. }
  8141. }
  8142. // ggml_compute_forward_im2col_f32
  8143. // src0: kernel [OC, IC, KH, KW]
  8144. // src1: image [N, IC, IH, IW]
  8145. // dst: result [N, OH, OW, IC*KH*KW]
  8146. static void ggml_compute_forward_im2col_f32(
  8147. const struct ggml_compute_params * params,
  8148. struct ggml_tensor * dst) {
  8149. const struct ggml_tensor * src0 = dst->src[0];
  8150. const struct ggml_tensor * src1 = dst->src[1];
  8151. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8152. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8153. GGML_TENSOR_BINARY_OP_LOCALS;
  8154. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8155. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8156. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  8157. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  8158. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  8159. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  8160. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  8161. const int ith = params->ith;
  8162. const int nth = params->nth;
  8163. const int64_t N = is_2D ? ne13 : ne12;
  8164. const int64_t IC = is_2D ? ne12 : ne11;
  8165. const int64_t IH = is_2D ? ne11 : 1;
  8166. const int64_t IW = ne10;
  8167. const int64_t KH = is_2D ? ne01 : 1;
  8168. const int64_t KW = ne00;
  8169. const int64_t OH = is_2D ? ne2 : 1;
  8170. const int64_t OW = ne1;
  8171. int ofs0 = is_2D ? nb13 : nb12;
  8172. int ofs1 = is_2D ? nb12 : nb11;
  8173. GGML_ASSERT(nb10 == sizeof(float));
  8174. // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
  8175. {
  8176. float * const wdata = (float *) dst->data;
  8177. for (int64_t in = 0; in < N; in++) {
  8178. for (int64_t ioh = 0; ioh < OH; ioh++) { // 1
  8179. for (int64_t iow = 0; iow < OW; iow++) {
  8180. for (int64_t iic = ith; iic < IC; iic += nth) {
  8181. // micro kernel
  8182. float * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW]
  8183. const float * const src_data = (float *)((char *) src1->data + in*ofs0 + iic*ofs1); // [IH, IW]
  8184. for (int64_t ikh = 0; ikh < KH; ikh++) { // 1
  8185. for (int64_t ikw = 0; ikw < KW; ikw++) {
  8186. const int64_t iiw = iow*s0 + ikw*d0 - p0;
  8187. const int64_t iih = ioh*s1 + ikh*d1 - p1;
  8188. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  8189. dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0;
  8190. } else {
  8191. dst_data[iic*(KH*KW) + ikh*KW + ikw] = (src_data[iih*IW + iiw]);
  8192. }
  8193. }
  8194. }
  8195. }
  8196. }
  8197. }
  8198. }
  8199. }
  8200. }
  8201. // ggml_compute_forward_im2col_f16
  8202. // src0: kernel [OC, IC, KH, KW]
  8203. // src1: image [N, IC, IH, IW]
  8204. // dst: result [N, OH, OW, IC*KH*KW]
  8205. static void ggml_compute_forward_im2col_f16(
  8206. const struct ggml_compute_params * params,
  8207. struct ggml_tensor * dst) {
  8208. const struct ggml_tensor * src0 = dst->src[0];
  8209. const struct ggml_tensor * src1 = dst->src[1];
  8210. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8211. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8212. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  8213. GGML_TENSOR_BINARY_OP_LOCALS;
  8214. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8215. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8216. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  8217. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  8218. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  8219. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  8220. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  8221. const int ith = params->ith;
  8222. const int nth = params->nth;
  8223. const int64_t N = is_2D ? ne13 : ne12;
  8224. const int64_t IC = is_2D ? ne12 : ne11;
  8225. const int64_t IH = is_2D ? ne11 : 1;
  8226. const int64_t IW = ne10;
  8227. const int64_t KH = is_2D ? ne01 : 1;
  8228. const int64_t KW = ne00;
  8229. const int64_t OH = is_2D ? ne2 : 1;
  8230. const int64_t OW = ne1;
  8231. int ofs0 = is_2D ? nb13 : nb12;
  8232. int ofs1 = is_2D ? nb12 : nb11;
  8233. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  8234. GGML_ASSERT(nb10 == sizeof(float));
  8235. // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
  8236. {
  8237. ggml_fp16_t * const wdata = (ggml_fp16_t *) dst->data;
  8238. for (int64_t in = 0; in < N; in++) {
  8239. for (int64_t ioh = 0; ioh < OH; ioh++) { // 1
  8240. for (int64_t iow = 0; iow < OW; iow++) {
  8241. for (int64_t iic = ith; iic < IC; iic += nth) {
  8242. // micro kernel
  8243. ggml_fp16_t * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW]
  8244. const float * const src_data = (float *)((char *) src1->data + in*ofs0 + iic*ofs1); // [IH, IW]
  8245. for (int64_t ikh = 0; ikh < KH; ikh++) { // 1
  8246. for (int64_t ikw = 0; ikw < KW; ikw++) {
  8247. const int64_t iiw = iow*s0 + ikw*d0 - p0;
  8248. const int64_t iih = ioh*s1 + ikh*d1 - p1;
  8249. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  8250. dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0;
  8251. } else {
  8252. dst_data[iic*(KH*KW) + ikh*KW + ikw] = GGML_FP32_TO_FP16(src_data[iih*IW + iiw]);
  8253. }
  8254. }
  8255. }
  8256. }
  8257. }
  8258. }
  8259. }
  8260. }
  8261. }
  8262. static void ggml_compute_forward_im2col(
  8263. const struct ggml_compute_params * params,
  8264. struct ggml_tensor * dst) {
  8265. switch (dst->type) {
  8266. case GGML_TYPE_F16:
  8267. {
  8268. ggml_compute_forward_im2col_f16(params, dst);
  8269. } break;
  8270. case GGML_TYPE_F32:
  8271. {
  8272. ggml_compute_forward_im2col_f32(params, dst);
  8273. } break;
  8274. default:
  8275. {
  8276. GGML_ABORT("fatal error");
  8277. }
  8278. }
  8279. }
  8280. // ggml_compute_forward_im2col_back_f32
  8281. static void ggml_compute_forward_im2col_back_f32(
  8282. const struct ggml_compute_params * params,
  8283. struct ggml_tensor * dst) {
  8284. const struct ggml_tensor * src0 = dst->src[0]; // gradients of forward pass output
  8285. const struct ggml_tensor * src1 = dst->src[1]; // convolution kernel
  8286. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8287. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8288. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8289. GGML_TENSOR_BINARY_OP_LOCALS;
  8290. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8291. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8292. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  8293. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  8294. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  8295. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  8296. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  8297. const int ith = params->ith;
  8298. const int nth = params->nth;
  8299. const int64_t N = is_2D ? ne3 : ne2;
  8300. const int64_t IC = is_2D ? ne2 : ne1;
  8301. const int64_t IH = is_2D ? ne1 : 1;
  8302. const int64_t IW = ne0;
  8303. const int64_t KH = is_2D ? ne11 : 1;
  8304. const int64_t KW = ne10;
  8305. const int64_t OH = is_2D ? ne02 : 1;
  8306. const int64_t OW = ne01;
  8307. int ofs0 = is_2D ? nb3 : nb2;
  8308. int ofs1 = is_2D ? nb2 : nb1;
  8309. GGML_ASSERT(nb0 == sizeof(float));
  8310. // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
  8311. {
  8312. float * const wdata = (float *) dst->data;
  8313. for (int64_t in = 0; in < N; in++) {
  8314. for (int64_t iic = ith; iic < IC; iic += nth) {
  8315. for (int64_t iih = 0; iih < IH; iih++) {
  8316. for (int64_t iiw = 0; iiw < IW; iiw++) {
  8317. // micro kernel
  8318. float grad = 0.0f;
  8319. for (int64_t ikh = 0; ikh < KH; ikh++) {
  8320. for (int64_t ikw = 0; ikw < KW; ikw++) {
  8321. // For s0 > 1 some values were skipped over in the forward pass.
  8322. // These values have tmpw % s0 != 0 and need to be skipped in the backwards pass as well.
  8323. const int64_t tmpw = (iiw + p0 - ikw*d0);
  8324. if (tmpw % s0 != 0) {
  8325. continue;
  8326. }
  8327. const int64_t iow = tmpw / s0;
  8328. // Equivalent logic as above except for s1.
  8329. int64_t ioh;
  8330. if (is_2D) {
  8331. const int64_t tmph = iih + p1 - ikh*d1;
  8332. if (tmph % s1 != 0) {
  8333. continue;
  8334. }
  8335. ioh = tmph / s1;
  8336. } else {
  8337. ioh = 0;
  8338. }
  8339. if (iow < 0 || iow >= OW || ioh < 0 || ioh >= OH) {
  8340. continue;
  8341. }
  8342. const float * const grad_in = (const float *) src0->data
  8343. + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW]
  8344. grad += grad_in[iic*(KH*KW) + ikh*KW + ikw];
  8345. }
  8346. }
  8347. float * dst_data = (float *)((char *) wdata + (in*ofs0 + iic*ofs1)); // [IH, IW]
  8348. dst_data[iih*IW + iiw] = grad;
  8349. }
  8350. }
  8351. }
  8352. }
  8353. }
  8354. }
  8355. // ggml_compute_forward_conv_transpose_2d
  8356. static void ggml_compute_forward_conv_transpose_2d(
  8357. const struct ggml_compute_params * params,
  8358. struct ggml_tensor * dst) {
  8359. const struct ggml_tensor * src0 = dst->src[0];
  8360. const struct ggml_tensor * src1 = dst->src[1];
  8361. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8362. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8363. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8364. GGML_TENSOR_BINARY_OP_LOCALS
  8365. const int ith = params->ith;
  8366. const int nth = params->nth;
  8367. const int nk = ne00*ne01*ne02*ne03;
  8368. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  8369. GGML_ASSERT(nb10 == sizeof(float));
  8370. if (ith == 0) {
  8371. memset(params->wdata, 0, params->wsize);
  8372. // permute kernel data (src0) from (Kw x Kh x Cout x Cin) to (Cin x Kw x Kh x Cout)
  8373. {
  8374. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  8375. for (int64_t i03 = 0; i03 < ne03; i03++) {
  8376. for (int64_t i02 = 0; i02 < ne02; i02++) {
  8377. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i03*nb03 + i02*nb02);
  8378. ggml_fp16_t * dst_data = wdata + i02*ne01*ne00*ne03;
  8379. for (int64_t i01 = 0; i01 < ne01; i01++) {
  8380. for (int64_t i00 = 0; i00 < ne00; i00++) {
  8381. dst_data[i01*ne00*ne03 + i00*ne03 + i03] = src[i01 * ne00 + i00];
  8382. }
  8383. }
  8384. }
  8385. }
  8386. }
  8387. // permute source data (src1) from (Sw x Sh x Cin) to (Cin x Sw x Sh)
  8388. {
  8389. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + nk;
  8390. for (int i12 = 0; i12 < ne12; i12++) {
  8391. for (int i11 = 0; i11 < ne11; i11++) {
  8392. const float * const src = (float *)((char *) src1->data + i12*nb12 + i11*nb11);
  8393. ggml_fp16_t * dst_data = wdata + i11*ne10*ne12;
  8394. for (int i10 = 0; i10 < ne10; i10++) {
  8395. dst_data[i10*ne12 + i12] = GGML_FP32_TO_FP16(src[i10]);
  8396. }
  8397. }
  8398. }
  8399. }
  8400. memset(dst->data, 0, ggml_nbytes(dst));
  8401. }
  8402. ggml_barrier(params->threadpool);
  8403. const int32_t stride = ggml_get_op_params_i32(dst, 0);
  8404. // total patches in dst
  8405. const int np = ne2;
  8406. // patches per thread
  8407. const int dp = (np + nth - 1)/nth;
  8408. // patch range for this thread
  8409. const int ip0 = dp*ith;
  8410. const int ip1 = MIN(ip0 + dp, np);
  8411. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  8412. ggml_fp16_t * const wdata_src = wdata + nk;
  8413. for (int i2 = ip0; i2 < ip1; i2++) { // Cout
  8414. float * dst_data = (float *)((char *) dst->data + i2*nb2);
  8415. ggml_fp16_t * wdata_kernel = wdata + i2*ne01*ne00*ne03;
  8416. for (int i11 = 0; i11 < ne11; i11++) {
  8417. for (int i10 = 0; i10 < ne10; i10++) {
  8418. const int i1n = i11*ne10*ne12 + i10*ne12;
  8419. for (int i01 = 0; i01 < ne01; i01++) {
  8420. for (int i00 = 0; i00 < ne00; i00++) {
  8421. float v = 0;
  8422. ggml_vec_dot_f16(ne03, &v, 0,
  8423. wdata_src + i1n, 0,
  8424. wdata_kernel + i01*ne00*ne03 + i00*ne03, 0, 1);
  8425. dst_data[(i11*stride + i01)*ne0 + i10*stride + i00] += v;
  8426. }
  8427. }
  8428. }
  8429. }
  8430. }
  8431. }
  8432. // ggml_compute_forward_pool_1d_sk_p0
  8433. static void ggml_compute_forward_pool_1d_sk_p0(
  8434. const struct ggml_compute_params * params,
  8435. const enum ggml_op_pool op,
  8436. const int k,
  8437. struct ggml_tensor * dst) {
  8438. const struct ggml_tensor * src = dst->src[0];
  8439. assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16);
  8440. if (params->ith != 0) {
  8441. return;
  8442. }
  8443. const char * cdata = (const char *)src->data;
  8444. const char * const data_end = cdata + ggml_nbytes(src);
  8445. float * drow = (float *)dst->data;
  8446. const int64_t rs = dst->ne[0];
  8447. while (cdata < data_end) {
  8448. const void * srow = (const void *)cdata;
  8449. int j = 0;
  8450. for (int64_t i = 0; i < rs; ++i) {
  8451. switch (op) {
  8452. case GGML_OP_POOL_AVG: drow[i] = 0; break;
  8453. case GGML_OP_POOL_MAX: drow[i] = -FLT_MAX; break;
  8454. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8455. }
  8456. for (int ki = 0; ki < k; ++ki) {
  8457. const float srow_j = (src->type == GGML_TYPE_F32) ? ((const float*)srow)[j] : GGML_FP16_TO_FP32(((const ggml_fp16_t*)srow)[j]);
  8458. switch (op) {
  8459. case GGML_OP_POOL_AVG: drow[i] += srow_j; break;
  8460. case GGML_OP_POOL_MAX: if (srow_j > drow[i]) drow[i] = srow_j; break;
  8461. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8462. }
  8463. ++j;
  8464. }
  8465. switch (op) {
  8466. case GGML_OP_POOL_AVG: drow[i] /= k; break;
  8467. case GGML_OP_POOL_MAX: break;
  8468. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8469. }
  8470. }
  8471. cdata += src->nb[1];
  8472. drow += rs;
  8473. }
  8474. }
  8475. // ggml_compute_forward_pool_1d
  8476. static void ggml_compute_forward_pool_1d(
  8477. const struct ggml_compute_params * params,
  8478. struct ggml_tensor * dst) {
  8479. const int32_t * opts = (const int32_t *)dst->op_params;
  8480. enum ggml_op_pool op = opts[0];
  8481. const int k0 = opts[1];
  8482. const int s0 = opts[2];
  8483. const int p0 = opts[3];
  8484. GGML_ASSERT(p0 == 0); // padding not supported
  8485. GGML_ASSERT(k0 == s0); // only s = k supported
  8486. ggml_compute_forward_pool_1d_sk_p0(params, op, k0, dst);
  8487. }
  8488. // ggml_compute_forward_pool_2d
  8489. static void ggml_compute_forward_pool_2d(
  8490. const struct ggml_compute_params * params,
  8491. struct ggml_tensor * dst) {
  8492. const struct ggml_tensor * src = dst->src[0];
  8493. assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16);
  8494. if (params->ith != 0) {
  8495. return;
  8496. }
  8497. const int32_t * opts = (const int32_t *)dst->op_params;
  8498. enum ggml_op_pool op = opts[0];
  8499. const int k0 = opts[1];
  8500. const int k1 = opts[2];
  8501. const int s0 = opts[3];
  8502. const int s1 = opts[4];
  8503. const int p0 = opts[5];
  8504. const int p1 = opts[6];
  8505. const char * cdata = (const char*)src->data;
  8506. const char * const data_end = cdata + ggml_nbytes(src);
  8507. const int64_t px = dst->ne[0];
  8508. const int64_t py = dst->ne[1];
  8509. const int64_t pa = px * py;
  8510. float * dplane = (float *)dst->data;
  8511. const int ka = k0 * k1;
  8512. const int offset0 = -p0;
  8513. const int offset1 = -p1;
  8514. while (cdata < data_end) {
  8515. for (int oy = 0; oy < py; ++oy) {
  8516. float * const drow = dplane + oy * px;
  8517. for (int ox = 0; ox < px; ++ox) {
  8518. float * const out = drow + ox;
  8519. switch (op) {
  8520. case GGML_OP_POOL_AVG: *out = 0; break;
  8521. case GGML_OP_POOL_MAX: *out = -FLT_MAX; break;
  8522. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8523. }
  8524. const int ix = offset0 + ox * s0;
  8525. const int iy = offset1 + oy * s1;
  8526. for (int ky = 0; ky < k1; ++ky) {
  8527. if (iy + ky < 0 || iy + ky >= src->ne[1]) continue;
  8528. const void * srow = (const void *)(cdata + src->nb[1] * (iy + ky));
  8529. for (int kx = 0; kx < k0; ++kx) {
  8530. int j = ix + kx;
  8531. if (j < 0 || j >= src->ne[0]) continue;
  8532. const float srow_j = (src->type == GGML_TYPE_F32) ? ((const float*)srow)[j] : GGML_FP16_TO_FP32(((const ggml_fp16_t*)srow)[j]);
  8533. switch (op) {
  8534. case GGML_OP_POOL_AVG: *out += srow_j; break;
  8535. case GGML_OP_POOL_MAX: if (srow_j > *out) *out = srow_j; break;
  8536. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8537. }
  8538. }
  8539. }
  8540. switch (op) {
  8541. case GGML_OP_POOL_AVG: *out /= ka; break;
  8542. case GGML_OP_POOL_MAX: break;
  8543. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8544. }
  8545. }
  8546. }
  8547. cdata += src->nb[2];
  8548. dplane += pa;
  8549. }
  8550. }
  8551. // ggml_compute_forward_pool_2d_back
  8552. static void ggml_compute_forward_pool_2d_back(
  8553. const struct ggml_compute_params * params,
  8554. struct ggml_tensor * dst) {
  8555. const struct ggml_tensor * src = dst->src[0];
  8556. const struct ggml_tensor * dstf = dst->src[1]; // forward tensor of dst
  8557. assert(dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  8558. if (params->ith != 0) {
  8559. return;
  8560. }
  8561. const int32_t * opts = (const int32_t *)dst->op_params;
  8562. enum ggml_op_pool op = opts[0];
  8563. const int k0 = opts[1];
  8564. const int k1 = opts[2];
  8565. const int s0 = opts[3];
  8566. const int s1 = opts[4];
  8567. const int p0 = opts[5];
  8568. const int p1 = opts[6];
  8569. char * cdata = (char *) dst->data;
  8570. const char * cdataf = (const char *) dstf->data;
  8571. const char * const data_end = cdata + ggml_nbytes(dst);
  8572. GGML_ASSERT(params->ith == 0);
  8573. memset(cdata, 0, ggml_nbytes(dst));
  8574. const int64_t px = src->ne[0];
  8575. const int64_t py = src->ne[1];
  8576. const int64_t pa = px * py;
  8577. const float * splane = (const float *) src->data;
  8578. const int ka = k0 * k1;
  8579. const int offset0 = -p0;
  8580. const int offset1 = -p1;
  8581. while (cdata < data_end) {
  8582. for (int oy = 0; oy < py; ++oy) {
  8583. const float * const srow = splane + oy * px;
  8584. for (int ox = 0; ox < px; ++ox) {
  8585. const float grad0 = srow[ox];
  8586. const int ix = offset0 + ox * s0;
  8587. const int iy = offset1 + oy * s1;
  8588. if (op == GGML_OP_POOL_MAX) {
  8589. float maxval = -FLT_MAX;
  8590. int kxmax = -1;
  8591. int kymax = -1;
  8592. for (int ky = 0; ky < k1; ++ky) {
  8593. if (iy + ky < 0 || iy + ky >= dst->ne[1]) {
  8594. continue;
  8595. }
  8596. const void * drowf = (const void *)(cdataf + dst->nb[1] * (iy + ky));
  8597. for (int kx = 0; kx < k0; ++kx) {
  8598. int j = ix + kx;
  8599. if (j < 0 || j >= dst->ne[0]) {
  8600. continue;
  8601. }
  8602. const float val = dst->type == GGML_TYPE_F32 ?
  8603. ((const float *) drowf)[j] : GGML_FP16_TO_FP32(((const ggml_fp16_t *) drowf)[j]);
  8604. if (val <= maxval) {
  8605. continue;
  8606. }
  8607. maxval = val;
  8608. kxmax = kx;
  8609. kymax = ky;
  8610. }
  8611. }
  8612. if (kxmax == -1 || kymax == -1) {
  8613. continue;
  8614. }
  8615. void * drow = (void *)(cdata + dst->nb[1] * (iy + kymax));
  8616. const int j = ix + kxmax;
  8617. if (dst->type == GGML_TYPE_F32) {
  8618. ((float *) drow)[j] += grad0;
  8619. } else {
  8620. ((ggml_fp16_t *) drow)[j] = GGML_FP32_TO_FP16(grad0 + GGML_FP16_TO_FP32(((const ggml_fp16_t *) drow)[j]));
  8621. }
  8622. } else if (op == GGML_OP_POOL_AVG) {
  8623. const float grad = grad0 / ka;
  8624. for (int ky = 0; ky < k1; ++ky) {
  8625. if (iy + ky < 0 || iy + ky >= dst->ne[1]) {
  8626. continue;
  8627. }
  8628. void * drow = (void *)(cdata + dst->nb[1] * (iy + ky));
  8629. for (int kx = 0; kx < k0; ++kx) {
  8630. int j = ix + kx;
  8631. if (j < 0 || j >= dst->ne[0]) {
  8632. continue;
  8633. }
  8634. if (dst->type == GGML_TYPE_F32) {
  8635. ((float *) drow)[j] += grad;
  8636. } else {
  8637. ((ggml_fp16_t *) drow)[j] += GGML_FP32_TO_FP16(grad);
  8638. }
  8639. }
  8640. }
  8641. } else {
  8642. GGML_ASSERT(false);
  8643. }
  8644. }
  8645. }
  8646. cdata += dst->nb[2];
  8647. cdataf += dst->nb[2];
  8648. splane += pa;
  8649. }
  8650. }
  8651. // ggml_compute_forward_upscale
  8652. static void ggml_compute_forward_upscale_f32(
  8653. const struct ggml_compute_params * params,
  8654. struct ggml_tensor * dst) {
  8655. const struct ggml_tensor * src0 = dst->src[0];
  8656. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8657. const int ith = params->ith;
  8658. const int nth = params->nth;
  8659. GGML_TENSOR_UNARY_OP_LOCALS
  8660. const float sf0 = (float)ne0/src0->ne[0];
  8661. const float sf1 = (float)ne1/src0->ne[1];
  8662. const float sf2 = (float)ne2/src0->ne[2];
  8663. const float sf3 = (float)ne3/src0->ne[3];
  8664. // TODO: optimize
  8665. for (int64_t i3 = 0; i3 < ne3; i3++) {
  8666. const int64_t i03 = i3 / sf3;
  8667. for (int64_t i2 = ith; i2 < ne2; i2 += nth) {
  8668. const int64_t i02 = i2 / sf2;
  8669. for (int64_t i1 = 0; i1 < ne1; i1++) {
  8670. const int64_t i01 = i1 / sf1;
  8671. for (int64_t i0 = 0; i0 < ne0; i0++) {
  8672. const int64_t i00 = i0 / sf0;
  8673. const float * x = (float *)((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  8674. float * y = (float *)((char *) dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3);
  8675. *y = *x;
  8676. }
  8677. }
  8678. }
  8679. }
  8680. }
  8681. static void ggml_compute_forward_upscale(
  8682. const struct ggml_compute_params * params,
  8683. struct ggml_tensor * dst) {
  8684. const struct ggml_tensor * src0 = dst->src[0];
  8685. switch (src0->type) {
  8686. case GGML_TYPE_F32:
  8687. {
  8688. ggml_compute_forward_upscale_f32(params, dst);
  8689. } break;
  8690. default:
  8691. {
  8692. GGML_ABORT("fatal error");
  8693. }
  8694. }
  8695. }
  8696. // ggml_compute_forward_pad
  8697. static void ggml_compute_forward_pad_f32(
  8698. const struct ggml_compute_params * params,
  8699. struct ggml_tensor * dst) {
  8700. const struct ggml_tensor * src0 = dst->src[0];
  8701. GGML_ASSERT(src0->nb[0] == sizeof(float));
  8702. GGML_ASSERT( dst->nb[0] == sizeof(float));
  8703. const int ith = params->ith;
  8704. const int nth = params->nth;
  8705. GGML_TENSOR_UNARY_OP_LOCALS
  8706. float * dst_ptr = (float *) dst->data;
  8707. // TODO: optimize
  8708. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  8709. for (int64_t i1 = ith; i1 < ne1; i1 += nth) {
  8710. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  8711. for (int64_t i3 = 0; i3 < ne3; ++i3) {
  8712. const int64_t dst_idx = i3*(ne0*ne1*ne2) + i2*(ne0*ne1) + i1*ne0 + i0;
  8713. const float * src_ptr = (const float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  8714. if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
  8715. dst_ptr[dst_idx] = *src_ptr;
  8716. } else {
  8717. dst_ptr[dst_idx] = 0;
  8718. }
  8719. }
  8720. }
  8721. }
  8722. }
  8723. }
  8724. static void ggml_compute_forward_pad(
  8725. const struct ggml_compute_params * params,
  8726. struct ggml_tensor * dst) {
  8727. const struct ggml_tensor * src0 = dst->src[0];
  8728. switch (src0->type) {
  8729. case GGML_TYPE_F32:
  8730. {
  8731. ggml_compute_forward_pad_f32(params, dst);
  8732. } break;
  8733. default:
  8734. {
  8735. GGML_ABORT("fatal error");
  8736. }
  8737. }
  8738. }
  8739. // ggml_compute_forward_pad_reflect_1d
  8740. static void ggml_compute_forward_pad_reflect_1d(
  8741. const struct ggml_compute_params * params,
  8742. struct ggml_tensor * dst) {
  8743. const struct ggml_tensor * src0 = dst->src[0];
  8744. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8745. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8746. const int ith = params->ith;
  8747. const int nth = params->nth;
  8748. const int32_t * opts = (const int32_t *) dst->op_params;
  8749. const int p0 = opts[0];
  8750. const int p1 = opts[1];
  8751. GGML_TENSOR_UNARY_OP_LOCALS
  8752. for (int64_t i3 = 0; i3 < ne3; i3++) {
  8753. for (int64_t i2 = 0; i2 < ne2; i2++) {
  8754. for (int64_t i1 = ith; i1 < ne1; i1 += nth) {
  8755. float * left = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + p0*nb0);
  8756. float * right = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + (ne0-p1-1)*nb0);
  8757. ggml_vec_cpy_f32(ne00, left, (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01));
  8758. for (int i0 = 1; i0 <= p0; i0++) { left[-i0] = left[i0]; }
  8759. for (int i0 = 1; i0 <= p1; i0++) { right[i0] = right[-i0]; }
  8760. }
  8761. }
  8762. }
  8763. }
  8764. // ggml_compute_forward_arange
  8765. static void ggml_compute_forward_arange_f32(
  8766. const struct ggml_compute_params * params,
  8767. struct ggml_tensor * dst) {
  8768. GGML_ASSERT(dst->nb[0] == sizeof(float));
  8769. const int ith = params->ith;
  8770. const int nth = params->nth;
  8771. const float start = ggml_get_op_params_f32(dst, 0);
  8772. const float stop = ggml_get_op_params_f32(dst, 1);
  8773. const float step = ggml_get_op_params_f32(dst, 2);
  8774. const int64_t steps = (int64_t) ceilf((stop - start) / step);
  8775. GGML_ASSERT(ggml_nelements(dst) == steps);
  8776. for (int64_t i = ith; i < steps; i+= nth) {
  8777. float value = start + step * i;
  8778. ((float *)dst->data)[i] = value;
  8779. }
  8780. }
  8781. static void ggml_compute_forward_arange(
  8782. const struct ggml_compute_params * params,
  8783. struct ggml_tensor * dst) {
  8784. switch (dst->type) {
  8785. case GGML_TYPE_F32:
  8786. {
  8787. ggml_compute_forward_arange_f32(params, dst);
  8788. } break;
  8789. default:
  8790. {
  8791. GGML_ABORT("fatal error");
  8792. }
  8793. }
  8794. }
  8795. static void ggml_compute_forward_timestep_embedding_f32(
  8796. const struct ggml_compute_params * params,
  8797. struct ggml_tensor * dst) {
  8798. const struct ggml_tensor * src0 = dst->src[0];
  8799. GGML_ASSERT(src0->nb[0] == sizeof(float));
  8800. const int ith = params->ith;
  8801. const int nth = params->nth;
  8802. GGML_TENSOR_UNARY_OP_LOCALS
  8803. const int dim = ggml_get_op_params_i32(dst, 0);
  8804. const int max_period = ggml_get_op_params_i32(dst, 1);
  8805. int half = dim / 2;
  8806. for (int64_t i = 0; i < ne00; i++) {
  8807. float * embed_data = (float *)((char *) dst->data + i*nb1);
  8808. for (int64_t j = ith; j < half; j += nth) {
  8809. float timestep = ((float *)src0->data)[i];
  8810. float freq = (float)expf(-logf(max_period) * j / half);
  8811. float arg = timestep * freq;
  8812. embed_data[j] = cosf(arg);
  8813. embed_data[j + half] = sinf(arg);
  8814. }
  8815. if (dim % 2 != 0 && ith == 0) {
  8816. embed_data[dim] = 0.f;
  8817. }
  8818. }
  8819. }
  8820. static void ggml_compute_forward_timestep_embedding(
  8821. const struct ggml_compute_params * params,
  8822. struct ggml_tensor * dst) {
  8823. const struct ggml_tensor * src0 = dst->src[0];
  8824. switch (src0->type) {
  8825. case GGML_TYPE_F32:
  8826. {
  8827. ggml_compute_forward_timestep_embedding_f32(params, dst);
  8828. } break;
  8829. default:
  8830. {
  8831. GGML_ABORT("fatal error");
  8832. }
  8833. }
  8834. }
  8835. // ggml_compute_forward_argsort
  8836. static void ggml_compute_forward_argsort_f32(
  8837. const struct ggml_compute_params * params,
  8838. struct ggml_tensor * dst) {
  8839. const struct ggml_tensor * src0 = dst->src[0];
  8840. GGML_TENSOR_UNARY_OP_LOCALS
  8841. GGML_ASSERT(nb0 == sizeof(float));
  8842. const int ith = params->ith;
  8843. const int nth = params->nth;
  8844. const int64_t nr = ggml_nrows(src0);
  8845. enum ggml_sort_order order = (enum ggml_sort_order) ggml_get_op_params_i32(dst, 0);
  8846. for (int64_t i = ith; i < nr; i += nth) {
  8847. int32_t * dst_data = (int32_t *)((char *) dst->data + i*nb1);
  8848. const float * src_data = (float *)((char *) src0->data + i*nb01);
  8849. for (int64_t j = 0; j < ne0; j++) {
  8850. dst_data[j] = j;
  8851. }
  8852. // C doesn't have a functional sort, so we do a bubble sort instead
  8853. for (int64_t j = 0; j < ne0; j++) {
  8854. for (int64_t k = j + 1; k < ne0; k++) {
  8855. if ((order == GGML_SORT_ORDER_ASC && src_data[dst_data[j]] > src_data[dst_data[k]]) ||
  8856. (order == GGML_SORT_ORDER_DESC && src_data[dst_data[j]] < src_data[dst_data[k]])) {
  8857. int32_t tmp = dst_data[j];
  8858. dst_data[j] = dst_data[k];
  8859. dst_data[k] = tmp;
  8860. }
  8861. }
  8862. }
  8863. }
  8864. }
  8865. static void ggml_compute_forward_argsort(
  8866. const struct ggml_compute_params * params,
  8867. struct ggml_tensor * dst) {
  8868. const struct ggml_tensor * src0 = dst->src[0];
  8869. switch (src0->type) {
  8870. case GGML_TYPE_F32:
  8871. {
  8872. ggml_compute_forward_argsort_f32(params, dst);
  8873. } break;
  8874. default:
  8875. {
  8876. GGML_ABORT("fatal error");
  8877. }
  8878. }
  8879. }
  8880. // ggml_compute_forward_flash_attn_ext
  8881. static void ggml_compute_forward_flash_attn_ext_f16(
  8882. const struct ggml_compute_params * params,
  8883. const struct ggml_tensor * q,
  8884. const struct ggml_tensor * k,
  8885. const struct ggml_tensor * v,
  8886. const struct ggml_tensor * mask,
  8887. struct ggml_tensor * dst) {
  8888. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  8889. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  8890. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  8891. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  8892. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  8893. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  8894. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  8895. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  8896. const int ith = params->ith;
  8897. const int nth = params->nth;
  8898. const int64_t D = neq0;
  8899. const int64_t N = neq1;
  8900. GGML_ASSERT(ne0 == D);
  8901. GGML_ASSERT(ne2 == N);
  8902. // input tensor rows must be contiguous
  8903. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  8904. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  8905. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  8906. GGML_ASSERT(neq0 == D);
  8907. GGML_ASSERT(nek0 == D);
  8908. GGML_ASSERT(nev0 == D);
  8909. GGML_ASSERT(neq1 == N);
  8910. GGML_ASSERT(nev0 == D);
  8911. // dst cannot be transposed or permuted
  8912. GGML_ASSERT(nb0 == sizeof(float));
  8913. GGML_ASSERT(nb0 <= nb1);
  8914. GGML_ASSERT(nb1 <= nb2);
  8915. GGML_ASSERT(nb2 <= nb3);
  8916. // broadcast factors
  8917. const int64_t rk2 = neq2/nek2;
  8918. const int64_t rk3 = neq3/nek3;
  8919. const int64_t rv2 = neq2/nev2;
  8920. const int64_t rv3 = neq3/nev3;
  8921. // parallelize by q rows using ggml_vec_dot_f32
  8922. // total rows in q
  8923. const int nr = neq1*neq2*neq3;
  8924. // rows per thread
  8925. const int dr = (nr + nth - 1)/nth;
  8926. // row range for this thread
  8927. const int ir0 = dr*ith;
  8928. const int ir1 = MIN(ir0 + dr, nr);
  8929. float scale = 1.0f;
  8930. float max_bias = 0.0f;
  8931. float logit_softcap = 0.0f;
  8932. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  8933. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  8934. memcpy(&logit_softcap, (float *) dst->op_params + 2, sizeof(float));
  8935. if (logit_softcap != 0) {
  8936. scale /= logit_softcap;
  8937. }
  8938. const uint32_t n_head = neq2;
  8939. const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head));
  8940. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  8941. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  8942. enum ggml_type const k_vec_dot_type = type_traits_cpu[k->type].vec_dot_type;
  8943. ggml_from_float_t const q_to_vec_dot = type_traits_cpu[k_vec_dot_type].from_float;
  8944. ggml_vec_dot_t const kq_vec_dot = type_traits_cpu[k->type].vec_dot;
  8945. ggml_to_float_t const v_to_float = ggml_get_type_traits(v->type)->to_float;
  8946. GGML_ASSERT(q_to_vec_dot && "fattn: unsupported K-type");
  8947. GGML_ASSERT(v_to_float && "fattn: unsupported V-type");
  8948. // loop over n_batch and n_head
  8949. for (int ir = ir0; ir < ir1; ++ir) {
  8950. // q indices
  8951. const int iq3 = ir/(neq2*neq1);
  8952. const int iq2 = (ir - iq3*neq2*neq1)/neq1;
  8953. const int iq1 = (ir - iq3*neq2*neq1 - iq2*neq1);
  8954. const uint32_t h = iq2; // head index
  8955. 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;
  8956. float S = 0.0f; // sum
  8957. float M = -INFINITY; // maximum KQ value
  8958. float * VKQ32 = (float *) params->wdata + ith*(3*D + CACHE_LINE_SIZE_F32); // FP32 VKQ accumulator
  8959. float * V32 = (VKQ32 + 1*D); // (temporary) FP32 V buffer
  8960. ggml_fp16_t * VKQ16 = (ggml_fp16_t *) (VKQ32 + 1*D); // (temporary) FP16 VKQ accumulator
  8961. ggml_fp16_t * Q_q = (ggml_fp16_t *) (VKQ32 + 2*D); // (temporary) buffer for Q converted to quantized/FP16
  8962. if (v->type == GGML_TYPE_F16) {
  8963. memset(VKQ16, 0, D*sizeof(ggml_fp16_t));
  8964. } else {
  8965. memset(VKQ32, 0, D*sizeof(float));
  8966. }
  8967. const ggml_fp16_t * mp = mask ? (ggml_fp16_t *)((char *) mask->data + iq1*mask->nb[1]) : NULL;
  8968. // k indices
  8969. const int ik3 = iq3 / rk3;
  8970. const int ik2 = iq2 / rk2;
  8971. // v indices
  8972. const int iv3 = iq3 / rv3;
  8973. const int iv2 = iq2 / rv2;
  8974. const float * pq = (const float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3));
  8975. q_to_vec_dot(pq, Q_q, D);
  8976. // online softmax / attention
  8977. // loop over n_kv and n_head_kv
  8978. // ref: https://arxiv.org/pdf/2112.05682.pdf
  8979. for (int64_t ic = 0; ic < nek1; ++ic) {
  8980. const float mv = mp ? slope*GGML_FP16_TO_FP32(mp[ic]) : 0.0f;
  8981. if (mv == -INFINITY) {
  8982. continue;
  8983. }
  8984. float s; // KQ value
  8985. const char * k_data = (const char *) k->data + ( ic*nbk1 + ik2*nbk2 + ik3*nbk3);
  8986. kq_vec_dot(D, &s, 0, k_data, 0, Q_q, 0, 1);
  8987. s = s*scale; // scale KQ value
  8988. if (logit_softcap != 0.0f) {
  8989. s = logit_softcap*tanhf(s);
  8990. }
  8991. s += mv; // apply mask
  8992. const float Mold = M;
  8993. float ms = 1.0f; // upon new higher max val, scale VKQ and KQ sum with this value
  8994. float vs = 1.0f; // post-softmax KQ value, expf(s - M)
  8995. const char * v_data = ((const char *) v->data + (ic*nbv1 + iv2*nbv2 + iv3*nbv3));
  8996. if (v->type == GGML_TYPE_F16) {
  8997. if (s > M) {
  8998. // s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f
  8999. M = s;
  9000. ms = expf(Mold - M);
  9001. // V = V*expf(Mold - M)
  9002. ggml_vec_scale_f16(D, VKQ16, ms);
  9003. } else {
  9004. // no new maximum, ms == 1.0f, vs != 1.0f
  9005. vs = expf(s - M);
  9006. }
  9007. // V += v*expf(s - M)
  9008. ggml_vec_mad_f16(D, VKQ16, (const ggml_fp16_t *) v_data, vs);
  9009. } else {
  9010. if (s > M) {
  9011. // s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f
  9012. M = s;
  9013. ms = expf(Mold - M);
  9014. // V = V*expf(Mold - M)
  9015. ggml_vec_scale_f32(D, VKQ32, ms);
  9016. } else {
  9017. // no new maximum, ms == 1.0f, vs != 1.0f
  9018. vs = expf(s - M);
  9019. }
  9020. v_to_float(v_data, V32, D);
  9021. // V += v*expf(s - M)
  9022. ggml_vec_mad_f32(D, VKQ32, V32, vs);
  9023. }
  9024. S = S*ms + vs; // scale and increment sum with partial sum
  9025. }
  9026. if (v->type == GGML_TYPE_F16) {
  9027. for (int64_t d = 0; d < D; ++d) {
  9028. VKQ32[d] = GGML_FP16_TO_FP32(VKQ16[d]);
  9029. }
  9030. }
  9031. // V /= S
  9032. const float S_inv = 1.0f/S;
  9033. ggml_vec_scale_f32(D, VKQ32, S_inv);
  9034. // dst indices
  9035. const int i1 = iq1;
  9036. const int i2 = iq2;
  9037. const int i3 = iq3;
  9038. // original
  9039. //memcpy((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb3), V, nev0*sizeof(float));
  9040. // permute(0, 2, 1, 3)
  9041. memcpy((char *) dst->data + (i3*ne2*ne1 + i2 + i1*ne1)*nb1, VKQ32, nb1);
  9042. }
  9043. }
  9044. static void ggml_compute_forward_flash_attn_ext(
  9045. const struct ggml_compute_params * params,
  9046. const struct ggml_tensor * q,
  9047. const struct ggml_tensor * k,
  9048. const struct ggml_tensor * v,
  9049. const struct ggml_tensor * mask,
  9050. struct ggml_tensor * dst) {
  9051. switch (dst->op_params[3]) {
  9052. case GGML_PREC_DEFAULT:
  9053. case GGML_PREC_F32:
  9054. {
  9055. // uses F32 accumulators
  9056. ggml_compute_forward_flash_attn_ext_f16(params, q, k, v, mask, dst);
  9057. } break;
  9058. default:
  9059. {
  9060. GGML_ABORT("fatal error");
  9061. }
  9062. }
  9063. }
  9064. // ggml_compute_forward_flash_attn_back
  9065. static void ggml_compute_forward_flash_attn_back_f32(
  9066. const struct ggml_compute_params * params,
  9067. const bool masked,
  9068. struct ggml_tensor * dst) {
  9069. const struct ggml_tensor * q = dst->src[0];
  9070. const struct ggml_tensor * k = dst->src[1];
  9071. const struct ggml_tensor * v = dst->src[2];
  9072. const struct ggml_tensor * d = dst->src[3];
  9073. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  9074. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  9075. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  9076. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  9077. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  9078. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  9079. GGML_TENSOR_LOCALS(int64_t, ned, d, ne)
  9080. GGML_TENSOR_LOCALS(size_t, nbd, d, nb)
  9081. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  9082. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  9083. const int ith = params->ith;
  9084. const int nth = params->nth;
  9085. const int64_t D = neq0;
  9086. const int64_t N = neq1;
  9087. const int64_t P = nek1 - N;
  9088. const int64_t M = P + N;
  9089. const int Mup = ggml_up(M, GGML_SOFT_MAX_UNROLL);
  9090. const int mxDM = MAX(D, Mup);
  9091. // GGML_ASSERT(ne0 == D);
  9092. // GGML_ASSERT(ne1 == N);
  9093. GGML_ASSERT(P >= 0);
  9094. GGML_ASSERT(nbq0 == sizeof(float));
  9095. GGML_ASSERT(nbk0 == sizeof(float));
  9096. GGML_ASSERT(nbv0 == sizeof(float));
  9097. GGML_ASSERT(neq0 == D);
  9098. GGML_ASSERT(nek0 == D);
  9099. GGML_ASSERT(nev1 == D);
  9100. GGML_ASSERT(ned0 == D);
  9101. GGML_ASSERT(neq1 == N);
  9102. GGML_ASSERT(nek1 == N + P);
  9103. GGML_ASSERT(nev1 == D);
  9104. GGML_ASSERT(ned1 == N);
  9105. // dst cannot be transposed or permuted
  9106. GGML_ASSERT(nb0 == sizeof(float));
  9107. GGML_ASSERT(nb0 <= nb1);
  9108. GGML_ASSERT(nb1 <= nb2);
  9109. GGML_ASSERT(nb2 <= nb3);
  9110. if (ith == 0) {
  9111. memset(dst->data, 0, nb0*ne0*ne1*ne2*ne3);
  9112. }
  9113. ggml_barrier(params->threadpool);
  9114. const int64_t elem_q = ggml_nelements(q);
  9115. const int64_t elem_k = ggml_nelements(k);
  9116. enum ggml_type result_type = dst->type;
  9117. GGML_ASSERT(ggml_blck_size(result_type) == 1);
  9118. const size_t tsize = ggml_type_size(result_type);
  9119. const size_t offs_q = 0;
  9120. const size_t offs_k = offs_q + GGML_PAD(elem_q * tsize, GGML_MEM_ALIGN);
  9121. const size_t offs_v = offs_k + GGML_PAD(elem_k * tsize, GGML_MEM_ALIGN);
  9122. void * grad_q = (char *) dst->data;
  9123. void * grad_k = (char *) dst->data + offs_k;
  9124. void * grad_v = (char *) dst->data + offs_v;
  9125. const size_t nbgq1 = nb0*neq0;
  9126. const size_t nbgq2 = nb0*neq0*neq1;
  9127. const size_t nbgq3 = nb0*neq0*neq1*neq2;
  9128. const size_t nbgk1 = nb0*nek0;
  9129. const size_t nbgk2 = nb0*nek0*nek1;
  9130. const size_t nbgk3 = nb0*nek0*nek1*neq2;
  9131. const size_t nbgv1 = nb0*nev0;
  9132. const size_t nbgv2 = nb0*nev0*nev1;
  9133. const size_t nbgv3 = nb0*nev0*nev1*neq2;
  9134. // parallelize by k rows using ggml_vec_dot_f32
  9135. // total rows in k
  9136. const int nr = nek2*nek3;
  9137. // rows per thread
  9138. const int dr = (nr + nth - 1)/nth;
  9139. // row range for this thread
  9140. const int ir0 = dr*ith;
  9141. const int ir1 = MIN(ir0 + dr, nr);
  9142. const float scale = 1.0f/sqrtf(D);
  9143. //printf("P=%d N=%d D=%d ir0=%d ir1=%d scale = %f\n", P, N, D, ir0, ir1, scale);
  9144. // how often k2 (and v2) is repeated in q2
  9145. int nrep = neq2/nek2;
  9146. for (int ir = ir0; ir < ir1; ++ir) {
  9147. // q indices
  9148. const int ik3 = ir/(nek2);
  9149. const int ik2 = ir - ik3*nek2;
  9150. const int iq3 = ik3;
  9151. const int id3 = ik3;
  9152. const int iv3 = ik3;
  9153. const int iv2 = ik2;
  9154. for (int irep = 0; irep < nrep; ++irep) {
  9155. const int iq2 = ik2 + irep*nek2;
  9156. const int id2 = iq2;
  9157. // (ik2 + irep*nek2) % nek2 == ik2
  9158. for (int iq1 = 0; iq1 < neq1; ++iq1) {
  9159. const int id1 = iq1;
  9160. // not sure about CACHE_LINE_SIZE_F32..
  9161. // - maybe it must not be multiplied by 2 and excluded from .. in SM 1*(..) offset?
  9162. float * S = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 0*(mxDM+CACHE_LINE_SIZE_F32);
  9163. float * SM = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 1*(mxDM+CACHE_LINE_SIZE_F32);
  9164. for (int i = M; i < Mup; ++i) {
  9165. S[i] = -INFINITY;
  9166. }
  9167. const int64_t masked_begin = masked ? (P + iq1 + 1) : M;
  9168. for (int64_t ic = 0; ic < masked_begin; ++ic) {
  9169. // k indices
  9170. const int ik1 = ic;
  9171. // S indices
  9172. const int i1 = ik1;
  9173. ggml_vec_dot_f32(neq0,
  9174. S + i1, 0,
  9175. (float *) ((char *) k->data + (ik1*nbk1 + ik2*nbk2 + ik3*nbk3)), 0,
  9176. (float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)), 0, 1);
  9177. }
  9178. // scale
  9179. ggml_vec_scale_f32(masked_begin, S, scale);
  9180. for (int64_t i = masked_begin; i < M; i++) {
  9181. S[i] = -INFINITY;
  9182. }
  9183. // softmax
  9184. // exclude known -INF S[..] values from max and loop
  9185. // dont forget to set their SM values to zero
  9186. {
  9187. float max = -INFINITY;
  9188. ggml_vec_max_f32(masked_begin, &max, S);
  9189. ggml_float sum = 0.0;
  9190. {
  9191. #ifdef GGML_SOFT_MAX_ACCELERATE
  9192. max = -max;
  9193. vDSP_vsadd(SM, 1, &max, SM, 1, Mup);
  9194. vvexpf(SM, SM, &Mup);
  9195. ggml_vec_sum_f32(Mup, &sum, SM);
  9196. #else
  9197. sum = ggml_vec_soft_max_f32(Mup, SM, S, max);
  9198. #endif
  9199. }
  9200. assert(sum > 0.0);
  9201. sum = 1.0/sum;
  9202. ggml_vec_scale_f32(masked_begin, SM, sum);
  9203. }
  9204. // step-by-step explanation
  9205. {
  9206. // forward-process shape grads from backward process
  9207. // parallel_for ik2,ik3:
  9208. // for irep:
  9209. // iq2 = ik2 + irep*nek2
  9210. // k[:D,:M,:,:] [D,M,:,:] grad[k][:D,:M,ik2,ik3] += grad[kcur]
  9211. // q[:D,:N,:,:] [D,N,:,:] grad[q][:D,iq1,iq2,iq3] += grad[qcur]
  9212. // v[:M,:D,:,:] [M,D,:,:] grad[v][:M,:D,iv2,iv3] += grad[vcur]
  9213. // for iq1:
  9214. // kcur = k[:D,:M,ik2,ik3] [D,M,1,1] grad[kcur] = grad[S1].T @ qcur
  9215. // qcur = q[:D,iq1,iq2,iq3] [D,1,1,1] grad[qcur] = grad[S1] @ kcur
  9216. // vcur = v[:M,:D,iv2,iv3] [M,D,1,1] grad[vcur] = grad[S5].T @ S4
  9217. // S0 = -Inf [D,1,1,1]
  9218. // ~S1[i] = dot(kcur[:D,i], qcur)
  9219. // S1 = qcur @ kcur.T [M,1,1,1] grad[S1] = grad[S2] * scale
  9220. // S2 = S1 * scale [M,1,1,1] grad[S2] = diag_mask_zero(grad[S3], P)
  9221. // S3 = diag_mask_inf(S2, P) [M,1,1,1] grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4]))
  9222. // S4 = softmax(S3) [M,1,1,1] grad[S4] = grad[S5] @ vcur
  9223. // ~S5[i] = dot(vcur[:,i], S4)
  9224. // S5 = S4 @ vcur.T [D,1,1,1] grad[S5] = d[:D,id1,id2,id3]
  9225. // ~dst[i,iq1,iq2,iq3] = S5[i] ^
  9226. // dst[:D,iq1,iq2,iq3] = S5 | grad[dst[:D,iq1,iq2,iq3]] = d[:D,id1,id2,id3]
  9227. // dst backward-/ grad[dst] = d
  9228. //
  9229. // output gradients with their dependencies:
  9230. //
  9231. // grad[kcur] = grad[S1].T @ qcur
  9232. // grad[S1] = diag_mask_zero(grad[S3], P) * scale
  9233. // grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4]))
  9234. // grad[S4] = grad[S5] @ vcur
  9235. // grad[S4] = d[:D,id1,id2,id3] @ vcur
  9236. // grad[qcur] = grad[S1] @ kcur
  9237. // grad[vcur] = grad[S5].T @ S4
  9238. // grad[vcur] = d[:D,id1,id2,id3].T @ S4
  9239. //
  9240. // in post-order:
  9241. //
  9242. // S1 = qcur @ kcur.T
  9243. // S2 = S1 * scale
  9244. // S3 = diag_mask_inf(S2, P)
  9245. // S4 = softmax(S3)
  9246. // grad[S4] = d[:D,id1,id2,id3] @ vcur
  9247. // grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4]))
  9248. // grad[S1] = diag_mask_zero(grad[S3], P) * scale
  9249. // grad[qcur] = grad[S1] @ kcur
  9250. // grad[kcur] = grad[S1].T @ qcur
  9251. // grad[vcur] = d[:D,id1,id2,id3].T @ S4
  9252. //
  9253. // using less variables (SM=S4):
  9254. //
  9255. // S = diag_mask_inf(qcur @ kcur.T * scale, P)
  9256. // SM = softmax(S)
  9257. // S = d[:D,iq1,iq2,iq3] @ vcur
  9258. // dot_SM_gradSM = dot(SM, S)
  9259. // S = SM * (S - dot(SM, S))
  9260. // S = diag_mask_zero(S, P) * scale
  9261. //
  9262. // grad[q][:D,iq1,iq2,iq3] += S @ kcur
  9263. // grad[k][:D,:M,ik2,ik3] += S.T @ qcur
  9264. // grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM
  9265. }
  9266. // S = gradSM = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3]
  9267. // S = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3]
  9268. // for ic:
  9269. // S[:M] += vcur[:M,ic,iv2,iv3] * d[ic,id1,id2,id3]
  9270. // exclude known future zero S[..] values from operation
  9271. ggml_vec_set_f32(masked_begin, S, 0);
  9272. for (int64_t ic = 0; ic < D; ++ic) {
  9273. ggml_vec_mad_f32(masked_begin,
  9274. S,
  9275. (float *) ((char *) v->data + ( ic*nbv1 + iv2*nbv2 + iv3*nbv3)),
  9276. *(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3)));
  9277. }
  9278. // S = SM * (S - dot(SM, S))
  9279. float dot_SM_gradSM = 0;
  9280. ggml_vec_dot_f32 (masked_begin, &dot_SM_gradSM, 0, SM, 0, S, 0, 1);
  9281. ggml_vec_acc1_f32(M, S, -dot_SM_gradSM);
  9282. ggml_vec_mul_f32 (masked_begin, S, S, SM);
  9283. // S = diag_mask_zero(S, P) * scale
  9284. // already done by above ggml_vec_set_f32
  9285. // exclude known zero S[..] values from operation
  9286. ggml_vec_scale_f32(masked_begin, S, scale);
  9287. // S shape [M,1]
  9288. // SM shape [M,1]
  9289. // kcur shape [D,M]
  9290. // qcur shape [D,1]
  9291. // vcur shape [M,D]
  9292. // grad[q][:D,iq1,iq2,iq3] += S @ kcur
  9293. // grad[q][:D,iq1,iq2,iq3] += shape[M,1] @ shape[D,M]
  9294. // for ic:
  9295. // grad[q][:D,iq1,iq2,iq3] += S[ic] * kcur[:D,ic,ik2,ik3]
  9296. // exclude known zero S[..] values from loop
  9297. for (int64_t ic = 0; ic < masked_begin; ++ic) {
  9298. ggml_vec_mad_f32(D,
  9299. (float *) ((char *) grad_q + (iq1*nbgq1 + iq2*nbgq2 + iq3*nbgq3)),
  9300. (float *) ((char *) k->data + (ic*nbk1 + ik2*nbk2 + ik3*nbk3)),
  9301. S[ic]);
  9302. }
  9303. // grad[k][:D,:M,iq2,iq3] += S.T @ qcur
  9304. // for ic:
  9305. // grad[k][:D,ic,iq2,iq3] += S.T[0,ic] * qcur[:D,0]
  9306. // grad[k][:D,ic,iq2,iq3] += S[ic] * qcur[:D,0]
  9307. // exclude known zero S[..] values from loop
  9308. for (int64_t ic = 0; ic < masked_begin; ++ic) {
  9309. ggml_vec_mad_f32(D,
  9310. (float *) ((char *) grad_k + (ic*nbgk1 + ik2*nbgk2 + ik3*nbgk3)),
  9311. (float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)),
  9312. S[ic]);
  9313. }
  9314. // grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM
  9315. // for ic:
  9316. // grad[v][:M,ic,iv2,iv3] += d[:D,id1,id2,id3].T[0,ic] * SM[:M]
  9317. // grad[v][:M,ic,iv2,iv3] += d[ic,id1,id2,id3] * SM[:M]
  9318. // exclude known zero SM[..] values from mad
  9319. for (int64_t ic = 0; ic < D; ++ic) {
  9320. ggml_vec_mad_f32(masked_begin,
  9321. (float *) ((char *) grad_v + ( ic*nbgv1 + iv2*nbgv2 + iv3*nbgv3)),
  9322. SM,
  9323. *(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3)));
  9324. }
  9325. }
  9326. }
  9327. }
  9328. }
  9329. static void ggml_compute_forward_flash_attn_back(
  9330. const struct ggml_compute_params * params,
  9331. const bool masked,
  9332. struct ggml_tensor * dst) {
  9333. const struct ggml_tensor * q = dst->src[0];
  9334. switch (q->type) {
  9335. case GGML_TYPE_F32:
  9336. {
  9337. ggml_compute_forward_flash_attn_back_f32(params, masked, dst);
  9338. } break;
  9339. default:
  9340. {
  9341. GGML_ABORT("fatal error");
  9342. }
  9343. }
  9344. }
  9345. // ggml_compute_forward_ssm_conv
  9346. static void ggml_compute_forward_ssm_conv_f32(
  9347. const struct ggml_compute_params * params,
  9348. struct ggml_tensor * dst) {
  9349. const struct ggml_tensor * src0 = dst->src[0]; // conv_x
  9350. const struct ggml_tensor * src1 = dst->src[1]; // conv1d.weight
  9351. const int ith = params->ith;
  9352. const int nth = params->nth;
  9353. const int nc = src1->ne[0]; // d_conv
  9354. const int ncs = src0->ne[0]; // d_conv - 1 + n_t
  9355. const int nr = src0->ne[1]; // d_inner
  9356. const int n_t = dst->ne[1]; // tokens per sequence
  9357. const int n_s = dst->ne[2]; // number of sequences in the batch
  9358. GGML_ASSERT( dst->ne[0] == nr);
  9359. GGML_ASSERT(src0->nb[0] == sizeof(float));
  9360. GGML_ASSERT(src1->nb[0] == sizeof(float));
  9361. GGML_ASSERT(src0->nb[1] == src0->ne[0]*sizeof(float));
  9362. // rows per thread
  9363. const int dr = (nr + nth - 1)/nth;
  9364. // row range for this thread
  9365. const int ir0 = dr*ith;
  9366. const int ir1 = MIN(ir0 + dr, nr);
  9367. const int ir = ir1 - ir0;
  9368. for (int i3 = 0; i3 < n_s; ++i3) {
  9369. for (int i2 = 0; i2 < n_t; ++i2) {
  9370. // {d_conv - 1 + n_t, d_inner, n_seqs}
  9371. // sliding window
  9372. 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}
  9373. const float * c = (const float *) ((const char *) src1->data + ir0*(src1->nb[1])); // {d_conv, d_inner}
  9374. float * x = (float *) ((char *) dst->data + ir0*(dst->nb[0]) + i2*(dst->nb[1]) + i3*(dst->nb[2])); // {d_inner, n_t, n_s}
  9375. // TODO: transpose the output for smaller strides for big batches?
  9376. // d_inner
  9377. for (int i1 = 0; i1 < ir; ++i1) {
  9378. // rowwise dot product
  9379. // NOTE: not using ggml_vec_dot_f32, because its sum is in double precision
  9380. float sumf = 0.0f;
  9381. // d_conv
  9382. for (int i0 = 0; i0 < nc; ++i0) {
  9383. sumf += s[i0 + i1*ncs] * c[i0 + i1*nc];
  9384. }
  9385. x[i1] = sumf;
  9386. }
  9387. }
  9388. }
  9389. }
  9390. static void ggml_compute_forward_ssm_conv(
  9391. const struct ggml_compute_params * params,
  9392. struct ggml_tensor * dst) {
  9393. switch (dst->src[0]->type) {
  9394. case GGML_TYPE_F32:
  9395. {
  9396. ggml_compute_forward_ssm_conv_f32(params, dst);
  9397. } break;
  9398. default:
  9399. {
  9400. GGML_ABORT("fatal error");
  9401. }
  9402. }
  9403. }
  9404. // ggml_compute_forward_ssm_scan
  9405. static void ggml_compute_forward_ssm_scan_f32(
  9406. const struct ggml_compute_params * params,
  9407. struct ggml_tensor * dst) {
  9408. const struct ggml_tensor * src0 = dst->src[0]; // s
  9409. const struct ggml_tensor * src1 = dst->src[1]; // x
  9410. const struct ggml_tensor * src2 = dst->src[2]; // dt
  9411. const struct ggml_tensor * src3 = dst->src[3]; // A
  9412. const struct ggml_tensor * src4 = dst->src[4]; // B
  9413. const struct ggml_tensor * src5 = dst->src[5]; // C
  9414. const int ith = params->ith;
  9415. const int nth = params->nth;
  9416. const int64_t nc = src0->ne[0]; // d_state
  9417. const int64_t nr = src0->ne[1]; // d_inner
  9418. const int64_t n_t = src1->ne[1]; // number of tokens per sequence
  9419. const int64_t n_s = src0->ne[2]; // number of sequences in the batch
  9420. GGML_ASSERT(ggml_nelements(src1) + ggml_nelements(src0) == ggml_nelements(dst));
  9421. GGML_ASSERT(src0->nb[0] == sizeof(float));
  9422. GGML_ASSERT(src1->nb[0] == sizeof(float));
  9423. GGML_ASSERT(src2->nb[0] == sizeof(float));
  9424. GGML_ASSERT(src3->nb[0] == sizeof(float));
  9425. GGML_ASSERT(src4->nb[0] == sizeof(float));
  9426. GGML_ASSERT(src5->nb[0] == sizeof(float));
  9427. // required for the dot product between s and C
  9428. GGML_ASSERT(src0->nb[1] == src0->ne[0]*sizeof(float));
  9429. // required for per-sequence offsets for states
  9430. GGML_ASSERT(src0->nb[2] == src0->ne[0]*src0->ne[1]*sizeof(float));
  9431. // required to get correct offset for state destination (i.e. src1->nb[3])
  9432. GGML_ASSERT(src1->nb[3] == src1->ne[0]*src1->ne[1]*src1->ne[2]*sizeof(float));
  9433. // rows per thread
  9434. const int dr = (nr + nth - 1)/nth;
  9435. // row range for this thread
  9436. const int ir0 = dr*ith;
  9437. const int ir1 = MIN(ir0 + dr, nr);
  9438. const int ir = ir1 - ir0;
  9439. for (int i3 = 0; i3 < n_s; ++i3) {
  9440. for (int i2 = 0; i2 < n_t; ++i2) {
  9441. const float * s0 = (const float *) ((const char *) src0->data + ir0*(src0->nb[1]) + i3*(src0->nb[2])); // {d_state, d_inner, n_s}
  9442. 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}
  9443. 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}
  9444. const float * A = (const float *) ((const char *) src3->data + ir0*(src3->nb[1])); // {d_state, d_inner}
  9445. const float * B = (const float *) ((const char *) src4->data + i2*(src4->nb[1]) + i3*(src4->nb[2])); // {d_state, n_t, n_s}
  9446. const float * C = (const float *) ((const char *) src5->data + i2*(src5->nb[1]) + i3*(src5->nb[2])); // {d_state, n_t, n_s}
  9447. float * y = ( float *) (( char *) dst->data + ir0*(src1->nb[0]) + i2*(src1->nb[1]) + i3*(src1->nb[2])); // {d_inner, n_t, n_s}
  9448. float * s = ( float *) (( char *) dst->data + ir0*(src0->nb[1]) + i3*(src0->nb[2]) + src1->nb[3]); // {d_state, d_inner, n_s}
  9449. // use the output as the source for the next token-wise iterations
  9450. if (i2 > 0) { s0 = s; }
  9451. // d_inner
  9452. for (int i1 = 0; i1 < ir; ++i1) {
  9453. // ref: https://github.com/state-spaces/mamba/blob/34076d664838588a3c97727b263478ab9f621a07/mamba_ssm/ops/triton/selective_state_update.py#L78
  9454. float dt_soft_plus = dt[i1] <= 20.0f ? log1pf(expf(dt[i1])) : dt[i1];
  9455. float x_dt = x[i1] * dt_soft_plus;
  9456. float sumf = 0.0f;
  9457. // d_state
  9458. for (int i0 = 0; i0 < nc; ++i0) {
  9459. int i = i0 + i1*nc;
  9460. // state = prev_state * dA + dB * x
  9461. float state = (s0[i] * expf(dt_soft_plus * A[i])) + (B[i0] * x_dt);
  9462. // y = rowwise_dotprod(state, C)
  9463. sumf += state * C[i0];
  9464. s[i] = state;
  9465. }
  9466. y[i1] = sumf;
  9467. }
  9468. }
  9469. }
  9470. }
  9471. static void ggml_compute_forward_ssm_scan(
  9472. const struct ggml_compute_params * params,
  9473. struct ggml_tensor * dst) {
  9474. switch (dst->src[0]->type) {
  9475. case GGML_TYPE_F32:
  9476. {
  9477. ggml_compute_forward_ssm_scan_f32(params, dst);
  9478. } break;
  9479. default:
  9480. {
  9481. GGML_ABORT("fatal error");
  9482. }
  9483. }
  9484. }
  9485. // ggml_compute_forward_win_part
  9486. static void ggml_compute_forward_win_part_f32(
  9487. const struct ggml_compute_params * params,
  9488. struct ggml_tensor * dst) {
  9489. UNUSED(params);
  9490. const struct ggml_tensor * src0 = dst->src[0];
  9491. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  9492. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  9493. const int32_t nep0 = ((const int32_t *)(dst->op_params))[0];
  9494. const int32_t nep1 = ((const int32_t *)(dst->op_params))[1];
  9495. const int32_t w = ((const int32_t *)(dst->op_params))[2];
  9496. assert(ne00 == ne0);
  9497. assert(ne3 == nep0*nep1);
  9498. // TODO: optimize / multi-thread
  9499. for (int py = 0; py < nep1; ++py) {
  9500. for (int px = 0; px < nep0; ++px) {
  9501. const int64_t i3 = py*nep0 + px;
  9502. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  9503. for (int64_t i1 = 0; i1 < ne1; ++i1) {
  9504. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  9505. const int64_t i02 = py*w + i2;
  9506. const int64_t i01 = px*w + i1;
  9507. const int64_t i00 = i0;
  9508. const int64_t i = i3*ne2*ne1*ne0 + i2*ne1*ne0 + i1*ne0 + i0;
  9509. const int64_t j = i02*ne01*ne00 + i01*ne00 + i00;
  9510. if (py*w + i2 >= ne02 || px*w + i1 >= ne01) {
  9511. ((float *) dst->data)[i] = 0.0f;
  9512. } else {
  9513. ((float *) dst->data)[i] = ((float *) src0->data)[j];
  9514. }
  9515. }
  9516. }
  9517. }
  9518. }
  9519. }
  9520. }
  9521. static void ggml_compute_forward_win_part(
  9522. const struct ggml_compute_params * params,
  9523. struct ggml_tensor * dst) {
  9524. const struct ggml_tensor * src0 = dst->src[0];
  9525. switch (src0->type) {
  9526. case GGML_TYPE_F32:
  9527. {
  9528. ggml_compute_forward_win_part_f32(params, dst);
  9529. } break;
  9530. default:
  9531. {
  9532. GGML_ABORT("fatal error");
  9533. }
  9534. }
  9535. }
  9536. // ggml_compute_forward_win_unpart
  9537. static void ggml_compute_forward_win_unpart_f32(
  9538. const struct ggml_compute_params * params,
  9539. struct ggml_tensor * dst) {
  9540. UNUSED(params);
  9541. const struct ggml_tensor * src0 = dst->src[0];
  9542. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  9543. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  9544. const int32_t w = ((const int32_t *)(dst->op_params))[0];
  9545. // padding
  9546. const int px = (w - ne1%w)%w;
  9547. //const int py = (w - ne2%w)%w;
  9548. const int npx = (px + ne1)/w;
  9549. //const int npy = (py + ne2)/w;
  9550. assert(ne0 == ne00);
  9551. // TODO: optimize / multi-thread
  9552. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  9553. for (int64_t i1 = 0; i1 < ne1; ++i1) {
  9554. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  9555. const int ip2 = i2/w;
  9556. const int ip1 = i1/w;
  9557. const int64_t i02 = i2%w;
  9558. const int64_t i01 = i1%w;
  9559. const int64_t i00 = i0;
  9560. const int64_t i = (ip2*npx + ip1)*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00 + i00;
  9561. const int64_t j = i2*ne1*ne0 + i1*ne0 + i0;
  9562. ((float *) dst->data)[j] = ((float *) src0->data)[i];
  9563. }
  9564. }
  9565. }
  9566. }
  9567. static void ggml_compute_forward_win_unpart(
  9568. const struct ggml_compute_params * params,
  9569. struct ggml_tensor * dst) {
  9570. const struct ggml_tensor * src0 = dst->src[0];
  9571. switch (src0->type) {
  9572. case GGML_TYPE_F32:
  9573. {
  9574. ggml_compute_forward_win_unpart_f32(params, dst);
  9575. } break;
  9576. default:
  9577. {
  9578. GGML_ABORT("fatal error");
  9579. }
  9580. }
  9581. }
  9582. //gmml_compute_forward_unary
  9583. static void ggml_compute_forward_unary(
  9584. const struct ggml_compute_params * params,
  9585. struct ggml_tensor * dst) {
  9586. const enum ggml_unary_op op = ggml_get_unary_op(dst);
  9587. switch (op) {
  9588. case GGML_UNARY_OP_ABS:
  9589. {
  9590. ggml_compute_forward_abs(params, dst);
  9591. } break;
  9592. case GGML_UNARY_OP_SGN:
  9593. {
  9594. ggml_compute_forward_sgn(params, dst);
  9595. } break;
  9596. case GGML_UNARY_OP_NEG:
  9597. {
  9598. ggml_compute_forward_neg(params, dst);
  9599. } break;
  9600. case GGML_UNARY_OP_STEP:
  9601. {
  9602. ggml_compute_forward_step(params, dst);
  9603. } break;
  9604. case GGML_UNARY_OP_TANH:
  9605. {
  9606. ggml_compute_forward_tanh(params, dst);
  9607. } break;
  9608. case GGML_UNARY_OP_ELU:
  9609. {
  9610. ggml_compute_forward_elu(params, dst);
  9611. } break;
  9612. case GGML_UNARY_OP_RELU:
  9613. {
  9614. ggml_compute_forward_relu(params, dst);
  9615. } break;
  9616. case GGML_UNARY_OP_SIGMOID:
  9617. {
  9618. ggml_compute_forward_sigmoid(params, dst);
  9619. } break;
  9620. case GGML_UNARY_OP_GELU:
  9621. {
  9622. ggml_compute_forward_gelu(params, dst);
  9623. } break;
  9624. case GGML_UNARY_OP_GELU_QUICK:
  9625. {
  9626. ggml_compute_forward_gelu_quick(params, dst);
  9627. } break;
  9628. case GGML_UNARY_OP_SILU:
  9629. {
  9630. ggml_compute_forward_silu(params, dst);
  9631. } break;
  9632. case GGML_UNARY_OP_HARDSWISH:
  9633. {
  9634. ggml_compute_forward_hardswish(params, dst);
  9635. } break;
  9636. case GGML_UNARY_OP_HARDSIGMOID:
  9637. {
  9638. ggml_compute_forward_hardsigmoid(params, dst);
  9639. } break;
  9640. case GGML_UNARY_OP_EXP:
  9641. {
  9642. ggml_compute_forward_exp(params, dst);
  9643. } break;
  9644. default:
  9645. {
  9646. GGML_ABORT("fatal error");
  9647. }
  9648. }
  9649. }
  9650. // ggml_compute_forward_get_rel_pos
  9651. static void ggml_compute_forward_get_rel_pos_f16(
  9652. const struct ggml_compute_params * params,
  9653. struct ggml_tensor * dst) {
  9654. UNUSED(params);
  9655. const struct ggml_tensor * src0 = dst->src[0];
  9656. // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L292-L322
  9657. GGML_TENSOR_UNARY_OP_LOCALS
  9658. const int64_t w = ne1;
  9659. ggml_fp16_t * src0_data = (ggml_fp16_t *) src0->data;
  9660. ggml_fp16_t * dst_data = (ggml_fp16_t *) dst->data;
  9661. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  9662. for (int64_t i1 = 0; i1 < ne1; ++i1) {
  9663. const int64_t pos = (w - i1 - 1) + i2;
  9664. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  9665. dst_data[i2*ne1*ne0 + i1*ne0 + i0] = src0_data[pos*ne00 + i0];
  9666. }
  9667. }
  9668. }
  9669. }
  9670. static void ggml_compute_forward_get_rel_pos(
  9671. const struct ggml_compute_params * params,
  9672. struct ggml_tensor * dst) {
  9673. const struct ggml_tensor * src0 = dst->src[0];
  9674. switch (src0->type) {
  9675. case GGML_TYPE_F16:
  9676. case GGML_TYPE_BF16:
  9677. {
  9678. ggml_compute_forward_get_rel_pos_f16(params, dst);
  9679. } break;
  9680. default:
  9681. {
  9682. GGML_ABORT("fatal error");
  9683. }
  9684. }
  9685. }
  9686. // ggml_compute_forward_add_rel_pos
  9687. static void ggml_compute_forward_add_rel_pos_f32(
  9688. const struct ggml_compute_params * params,
  9689. struct ggml_tensor * dst) {
  9690. const struct ggml_tensor * src0 = dst->src[0];
  9691. const struct ggml_tensor * src1 = dst->src[1];
  9692. const struct ggml_tensor * src2 = dst->src[2];
  9693. const bool inplace = (bool) ((int32_t *) dst->op_params)[0];
  9694. if (!inplace) {
  9695. if (params->ith == 0) {
  9696. memcpy((char *) dst->data, (char *) src0->data, ggml_nbytes(dst));
  9697. }
  9698. ggml_barrier(params->threadpool);
  9699. }
  9700. // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L357-L359
  9701. float * src1_data = (float *) src1->data;
  9702. float * src2_data = (float *) src2->data;
  9703. float * dst_data = (float *) dst->data;
  9704. const int64_t ne10 = src1->ne[0];
  9705. const int64_t ne11 = src1->ne[1];
  9706. const int64_t ne12 = src1->ne[2];
  9707. const int64_t ne13 = src1->ne[3];
  9708. const int ith = params->ith;
  9709. const int nth = params->nth;
  9710. // total patches in dst
  9711. const int np = ne13;
  9712. // patches per thread
  9713. const int dp = (np + nth - 1)/nth;
  9714. // patch range for this thread
  9715. const int ip0 = dp*ith;
  9716. const int ip1 = MIN(ip0 + dp, np);
  9717. for (int64_t i13 = ip0; i13 < ip1; ++i13) {
  9718. for (int64_t i12 = 0; i12 < ne12; ++i12) {
  9719. for (int64_t i11 = 0; i11 < ne11; ++i11) {
  9720. const int64_t jp1 = i13*ne12*ne11*ne10 + i12*ne11*ne10 + i11*ne10;
  9721. for (int64_t i10 = 0; i10 < ne10; ++i10) {
  9722. const int64_t jp0 = jp1 + i10;
  9723. const float src1_e = src1_data[jp0];
  9724. const float src2_e = src2_data[jp0];
  9725. const int64_t jdh = jp0 * ne10;
  9726. const int64_t jdw = jdh - (ne10 - 1) * i10;
  9727. for (int64_t j = 0; j < ne10; ++j) {
  9728. dst_data[jdh + j ] += src2_e;
  9729. dst_data[jdw + j*ne10] += src1_e;
  9730. }
  9731. }
  9732. }
  9733. }
  9734. }
  9735. }
  9736. static void ggml_compute_forward_add_rel_pos(
  9737. const struct ggml_compute_params * params,
  9738. struct ggml_tensor * dst) {
  9739. const struct ggml_tensor * src0 = dst->src[0];
  9740. switch (src0->type) {
  9741. case GGML_TYPE_F32:
  9742. {
  9743. ggml_compute_forward_add_rel_pos_f32(params, dst);
  9744. } break;
  9745. default:
  9746. {
  9747. GGML_ABORT("fatal error");
  9748. }
  9749. }
  9750. }
  9751. // ggml_compute_forward_rwkv_wkv6
  9752. static void ggml_compute_forward_rwkv_wkv6_f32(
  9753. const struct ggml_compute_params * params,
  9754. struct ggml_tensor * dst) {
  9755. const int64_t T = dst->src[1]->ne[2];
  9756. const int64_t C = dst->ne[0];
  9757. const int64_t HEADS = dst->src[1]->ne[1];
  9758. const int64_t n_seqs = dst->src[5]->ne[1];
  9759. const int64_t head_size = C / HEADS;
  9760. float * dst_data = (float *) dst->data;
  9761. float * state = ((float *) dst->data) + C * T;
  9762. const int ith = params->ith;
  9763. const int nth = params->nth;
  9764. if (ith >= HEADS) {
  9765. return;
  9766. }
  9767. const int h_start = (HEADS * ith) / nth;
  9768. const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ?
  9769. (HEADS * (ith + 1)) / nth : HEADS;
  9770. float * k = (float *) dst->src[0]->data;
  9771. float * v = (float *) dst->src[1]->data;
  9772. float * r = (float *) dst->src[2]->data;
  9773. float * time_faaaa = (float *) dst->src[3]->data;
  9774. float * time_decay = (float *) dst->src[4]->data;
  9775. size_t t_stride = HEADS * head_size; // Same to C
  9776. size_t h_stride = C / HEADS;
  9777. GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS
  9778. size_t h_stride_2d = head_size * head_size;
  9779. if (ith == 0) {
  9780. memset(dst_data, 0, T * C * sizeof(float));
  9781. }
  9782. ggml_barrier(params->threadpool);
  9783. #if defined(__AVX__) && !defined(__AVX512F__)
  9784. #define GGML_F32X GGML_F32x8
  9785. #define GGML_F32X_SET1 GGML_F32x8_SET1
  9786. #define GGML_F32X_LOAD GGML_F32x8_LOAD
  9787. #define GGML_F32X_STORE GGML_F32x8_STORE
  9788. #define GGML_F32X_MUL GGML_F32x8_MUL
  9789. #define GGML_F32X_FMA GGML_F32x8_FMA
  9790. #define WKV_VECTOR_SIZE 8
  9791. #elif defined(__AVX512F__)
  9792. #define GGML_F32X GGML_F32x16
  9793. #define GGML_F32X_SET1 GGML_F32x16_SET1
  9794. #define GGML_F32X_LOAD GGML_F32x16_LOAD
  9795. #define GGML_F32X_STORE GGML_F32x16_STORE
  9796. #define GGML_F32X_MUL GGML_F32x16_MUL
  9797. #define GGML_F32X_FMA GGML_F32x16_FMA
  9798. #define WKV_VECTOR_SIZE 16
  9799. #elif defined(__ARM_NEON) && defined(__aarch64__)
  9800. #define GGML_F32X GGML_F32x4
  9801. #define GGML_F32X_SET1 GGML_F32x4_SET1
  9802. #define GGML_F32X_LOAD GGML_F32x4_LOAD
  9803. #define GGML_F32X_STORE GGML_F32x4_STORE
  9804. #define GGML_F32X_MUL GGML_F32x4_MUL
  9805. #define GGML_F32X_FMA GGML_F32x4_FMA
  9806. #define WKV_VECTOR_SIZE 4
  9807. #endif
  9808. #ifdef WKV_VECTOR_SIZE
  9809. const int64_t vec_count = head_size / WKV_VECTOR_SIZE;
  9810. for (int64_t t = 0; t < T; t++) {
  9811. size_t t_offset = t * t_stride;
  9812. size_t state_offset = head_size * C * (t / (T / n_seqs));
  9813. float * state_cur = state + state_offset;
  9814. float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset;
  9815. for (int64_t h = h_start; h < h_end; h++) {
  9816. size_t h_offset = h * h_stride;
  9817. size_t t_h_offset = t_offset + h_offset;
  9818. size_t h_2d_offset = h * h_stride_2d;
  9819. for (int64_t i = 0; i < head_size; i++) {
  9820. size_t t_h_i_offset = t_h_offset + i;
  9821. size_t h_i_offset = h_offset + i;
  9822. size_t h_2d_i_offset = h_2d_offset + i * h_stride;
  9823. float k_val = k[t_h_i_offset];
  9824. float r_val = r[t_h_i_offset];
  9825. float time_faaaa_val = time_faaaa[h_i_offset];
  9826. float time_decay_val = time_decay[t_h_i_offset];
  9827. // Broadcast scalar values to vectors
  9828. GGML_F32X k_vec = GGML_F32X_SET1(k_val);
  9829. GGML_F32X r_vec = GGML_F32X_SET1(r_val);
  9830. GGML_F32X time_faaaa_vec = GGML_F32X_SET1(time_faaaa_val);
  9831. GGML_F32X time_decay_vec = GGML_F32X_SET1(time_decay_val);
  9832. for (int64_t j = 0; j < vec_count; j++) {
  9833. size_t base_j = j * WKV_VECTOR_SIZE;
  9834. size_t t_h_j_offset = t_h_offset + base_j;
  9835. size_t h_2d_i_j_offset = h_2d_i_offset + base_j;
  9836. // Load x elements at once
  9837. GGML_F32X v_vec = GGML_F32X_LOAD(&v[t_h_j_offset]);
  9838. GGML_F32X prev_state_vec = GGML_F32X_LOAD(&state_prev[h_2d_i_j_offset]);
  9839. GGML_F32X dst_vec = GGML_F32X_LOAD(&dst_data[t_h_j_offset]);
  9840. // Compute kv = v * k
  9841. GGML_F32X kv_vec = GGML_F32X_MUL(v_vec, k_vec);
  9842. // Compute temp = kv * time_faaaa + prev_state
  9843. GGML_F32X temp_vec = GGML_F32X_FMA(prev_state_vec, kv_vec, time_faaaa_vec);
  9844. // Update dst: dst += temp * r
  9845. dst_vec = GGML_F32X_FMA(dst_vec, temp_vec, r_vec);
  9846. GGML_F32X_STORE(&dst_data[t_h_j_offset], dst_vec);
  9847. // Update state: state = prev_state * time_decay + kv
  9848. GGML_F32X new_state_vec = GGML_F32X_FMA(kv_vec, prev_state_vec, time_decay_vec);
  9849. GGML_F32X_STORE(&state_cur[h_2d_i_j_offset], new_state_vec);
  9850. }
  9851. // Handle remaining elements, this will not be used.
  9852. for (int64_t j = vec_count * WKV_VECTOR_SIZE; j < head_size; j++) {
  9853. size_t t_h_j_offset = t_h_offset + j;
  9854. size_t h_2d_i_j_offset = h_2d_i_offset + j;
  9855. float v_val = v[t_h_j_offset];
  9856. float kv_val = v_val * k_val;
  9857. float prev_state_val = state_prev[h_2d_i_j_offset];
  9858. float temp_val = kv_val * time_faaaa_val + prev_state_val;
  9859. dst_data[t_h_j_offset] += temp_val * r_val;
  9860. state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val;
  9861. }
  9862. }
  9863. }
  9864. }
  9865. #else
  9866. // basically fused operations:
  9867. // dst = r @ (time_faaaa * (k @ v) + state),
  9868. // state = time_decay * state + (k @ v),
  9869. // recursive through each token
  9870. for (int64_t t = 0; t < T; t++) {
  9871. size_t t_offset = t * t_stride;
  9872. size_t state_offset = head_size * C * (t / (T / n_seqs));
  9873. float * state_cur = state + state_offset;
  9874. float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset;
  9875. for (int64_t h = h_start; h < h_end; h++) {
  9876. size_t h_offset = h * h_stride;
  9877. size_t t_h_offset = t_offset + h_offset;
  9878. size_t h_2d_offset = h * h_stride_2d;
  9879. for (int64_t i = 0; i < head_size; i++) {
  9880. size_t t_h_i_offset = t_h_offset + i;
  9881. size_t h_i_offset = h_offset + i;
  9882. size_t h_2d_i_offset = h_2d_offset + i * h_stride;
  9883. float k_val = k[t_h_i_offset];
  9884. float r_val = r[t_h_i_offset];
  9885. float time_faaaa_val = time_faaaa[h_i_offset];
  9886. // RWKV v6: different time_decay for each token.
  9887. float time_decay_val = time_decay[t_h_i_offset];
  9888. for (int64_t j = 0; j < head_size; j++) {
  9889. size_t t_h_j_offset = t_h_offset + j;
  9890. size_t h_2d_i_j_offset = h_2d_i_offset + j;
  9891. float v_val = v[t_h_j_offset];
  9892. float kv_val = v_val * k_val;
  9893. float prev_state_val = state_prev[h_2d_i_j_offset];
  9894. float temp_val = kv_val * time_faaaa_val + prev_state_val;
  9895. dst_data[t_h_j_offset] += temp_val * r_val;
  9896. state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val;
  9897. }
  9898. }
  9899. }
  9900. }
  9901. #endif
  9902. }
  9903. static void ggml_compute_forward_rwkv_wkv6(
  9904. const struct ggml_compute_params * params,
  9905. struct ggml_tensor * dst) {
  9906. const struct ggml_tensor * src0 = dst->src[0];
  9907. switch (src0->type) {
  9908. case GGML_TYPE_F32:
  9909. {
  9910. ggml_compute_forward_rwkv_wkv6_f32(params, dst);
  9911. } break;
  9912. default:
  9913. {
  9914. GGML_ABORT("fatal error");
  9915. }
  9916. }
  9917. }
  9918. // ggml_compute_forward_gla
  9919. static void ggml_compute_forward_gla_f32(
  9920. const struct ggml_compute_params * params,
  9921. struct ggml_tensor * dst) {
  9922. const int64_t T = dst->src[1]->ne[2];
  9923. const int64_t C = dst->ne[0];
  9924. const int64_t HEADS = dst->src[1]->ne[1];
  9925. const int64_t n_seqs = dst->src[4]->ne[1];
  9926. const int64_t head_size = C / HEADS;
  9927. const float scale = ggml_get_op_params_f32(dst, 0);
  9928. float * dst_data = (float *) dst->data;
  9929. float * state = ((float *) dst->data) + C * T;
  9930. const int ith = params->ith;
  9931. const int nth = params->nth;
  9932. if (ith >= HEADS) {
  9933. return;
  9934. }
  9935. const int h_start = (HEADS * ith) / nth;
  9936. const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ?
  9937. (HEADS * (ith + 1)) / nth : HEADS;
  9938. float * k = (float *) dst->src[0]->data;
  9939. float * v = (float *) dst->src[1]->data;
  9940. float * q = (float *) dst->src[2]->data;
  9941. float * g = (float *) dst->src[3]->data;
  9942. size_t t_stride = HEADS * head_size; // Same to C
  9943. size_t h_stride = C / HEADS;
  9944. GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS
  9945. size_t h_stride_2d = head_size * head_size;
  9946. if (ith == 0) {
  9947. memset(dst_data, 0, T * C * sizeof(float));
  9948. }
  9949. ggml_barrier(params->threadpool);
  9950. #if defined(__AVX__) && !defined(__AVX512F__)
  9951. #define GGML_F32X GGML_F32x8
  9952. #define GGML_F32X_SET1 GGML_F32x8_SET1
  9953. #define GGML_F32X_LOAD GGML_F32x8_LOAD
  9954. #define GGML_F32X_STORE GGML_F32x8_STORE
  9955. #define GGML_F32X_MUL GGML_F32x8_MUL
  9956. #define GGML_F32X_FMA GGML_F32x8_FMA
  9957. #define GLA_VECTOR_SIZE 8
  9958. #elif defined(__AVX512F__)
  9959. #define GGML_F32X GGML_F32x16
  9960. #define GGML_F32X_SET1 GGML_F32x16_SET1
  9961. #define GGML_F32X_LOAD GGML_F32x16_LOAD
  9962. #define GGML_F32X_STORE GGML_F32x16_STORE
  9963. #define GGML_F32X_MUL GGML_F32x16_MUL
  9964. #define GGML_F32X_FMA GGML_F32x16_FMA
  9965. #define GLA_VECTOR_SIZE 16
  9966. #elif defined(__ARM_NEON) && defined(__aarch64__)
  9967. #define GGML_F32X GGML_F32x4
  9968. #define GGML_F32X_SET1 GGML_F32x4_SET1
  9969. #define GGML_F32X_LOAD GGML_F32x4_LOAD
  9970. #define GGML_F32X_STORE GGML_F32x4_STORE
  9971. #define GGML_F32X_MUL GGML_F32x4_MUL
  9972. #define GGML_F32X_FMA GGML_F32x4_FMA
  9973. #define GLA_VECTOR_SIZE 4
  9974. #endif
  9975. #ifdef GLA_VECTOR_SIZE
  9976. const int64_t vec_count = head_size / GLA_VECTOR_SIZE;
  9977. for (int64_t t = 0; t < T; t++) {
  9978. size_t t_offset = t * t_stride;
  9979. size_t state_offset = head_size * C * (t / (T / n_seqs));
  9980. float * state_cur = state + state_offset;
  9981. float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[4]->data + state_offset;
  9982. for (int64_t h = h_start; h < h_end; h++) {
  9983. size_t h_offset = h * h_stride;
  9984. size_t t_h_offset = t_offset + h_offset;
  9985. size_t h_2d_offset = h * h_stride_2d;
  9986. for (int64_t i = 0; i < head_size; i++) {
  9987. size_t t_h_i_offset = t_h_offset + i;
  9988. size_t h_2d_i_offset = h_2d_offset + i * h_stride;
  9989. float k_val = k[t_h_i_offset];
  9990. float q_val = q[t_h_i_offset] * scale;
  9991. float g_val = g[t_h_i_offset];
  9992. // Broadcast scalar values to vectors
  9993. GGML_F32X k_vec = GGML_F32X_SET1(k_val);
  9994. GGML_F32X q_vec = GGML_F32X_SET1(q_val);
  9995. GGML_F32X g_vec = GGML_F32X_SET1(g_val);
  9996. for (int64_t j = 0; j < vec_count; j++) {
  9997. size_t base_j = j * GLA_VECTOR_SIZE;
  9998. size_t t_h_j_offset = t_h_offset + base_j;
  9999. size_t h_2d_i_j_offset = h_2d_i_offset + base_j;
  10000. // Load x elements at once
  10001. GGML_F32X v_vec = GGML_F32X_LOAD(&v[t_h_j_offset]);
  10002. GGML_F32X prev_state_vec = GGML_F32X_LOAD(&state_prev[h_2d_i_j_offset]);
  10003. GGML_F32X dst_vec = GGML_F32X_LOAD(&dst_data[t_h_j_offset]);
  10004. // Compute kv = v * k
  10005. GGML_F32X kv_vec = GGML_F32X_MUL(v_vec, k_vec);
  10006. // Compute temp = prev_state * g + kv
  10007. GGML_F32X temp_vec = GGML_F32X_FMA(kv_vec, prev_state_vec, g_vec);
  10008. // Update dst: dst += temp * q
  10009. dst_vec = GGML_F32X_FMA(dst_vec, temp_vec, q_vec);
  10010. GGML_F32X_STORE(&dst_data[t_h_j_offset], dst_vec);
  10011. // Update state
  10012. GGML_F32X_STORE(&state_cur[h_2d_i_j_offset], temp_vec);
  10013. }
  10014. // Handle remaining elements, this will not be used.
  10015. for (int64_t j = vec_count * GLA_VECTOR_SIZE; j < head_size; j++) {
  10016. size_t t_h_j_offset = t_h_offset + j;
  10017. size_t h_2d_i_j_offset = h_2d_i_offset + j;
  10018. float v_val = v[t_h_j_offset];
  10019. float kv_val = v_val * k_val;
  10020. float prev_state_val = state_prev[h_2d_i_j_offset];
  10021. float temp_val = kv_val + prev_state_val * g_val;
  10022. dst_data[t_h_j_offset] += temp_val * q_val;
  10023. state_cur[h_2d_i_j_offset] = temp_val;
  10024. }
  10025. }
  10026. }
  10027. }
  10028. #else
  10029. for (int64_t t = 0; t < T; t++) {
  10030. size_t t_offset = t * t_stride;
  10031. size_t state_offset = head_size * C * (t / (T / n_seqs));
  10032. float * state_cur = state + state_offset;
  10033. float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[4]->data + state_offset;
  10034. for (int64_t h = h_start; h < h_end; h++) {
  10035. size_t h_offset = h * h_stride;
  10036. size_t t_h_offset = t_offset + h_offset;
  10037. size_t h_2d_offset = h * h_stride_2d;
  10038. for (int64_t i = 0; i < head_size; i++) {
  10039. size_t t_h_i_offset = t_h_offset + i;
  10040. size_t h_2d_i_offset = h_2d_offset + i * h_stride;
  10041. float k_val = k[t_h_i_offset];
  10042. float q_val = q[t_h_i_offset] * scale;
  10043. float g_val = g[t_h_i_offset];
  10044. for (int64_t j = 0; j < head_size; j++) {
  10045. size_t t_h_j_offset = t_h_offset + j;
  10046. size_t h_2d_i_j_offset = h_2d_i_offset + j;
  10047. float v_val = v[t_h_j_offset];
  10048. float kv_val = v_val * k_val;
  10049. float prev_state_val = state_prev[h_2d_i_j_offset];
  10050. float temp_val = prev_state_val * g_val + kv_val;
  10051. dst_data[t_h_j_offset] += temp_val * q_val;
  10052. state_cur[h_2d_i_j_offset] = temp_val;
  10053. }
  10054. }
  10055. }
  10056. }
  10057. #endif
  10058. }
  10059. static void ggml_compute_forward_gla(
  10060. const struct ggml_compute_params * params,
  10061. struct ggml_tensor * dst) {
  10062. const struct ggml_tensor * src0 = dst->src[0];
  10063. switch (src0->type) {
  10064. case GGML_TYPE_F32:
  10065. {
  10066. ggml_compute_forward_gla_f32(params, dst);
  10067. } break;
  10068. default:
  10069. {
  10070. GGML_ABORT("fatal error");
  10071. }
  10072. }
  10073. }
  10074. // ggml_compute_forward_map_unary
  10075. static void ggml_compute_forward_map_unary_f32(
  10076. const struct ggml_compute_params * params,
  10077. struct ggml_tensor * dst,
  10078. const ggml_unary_op_f32_t fun) {
  10079. const struct ggml_tensor * src0 = dst->src[0];
  10080. if (params->ith != 0) {
  10081. return;
  10082. }
  10083. assert(ggml_is_contiguous_1(src0));
  10084. assert(ggml_is_contiguous_1(dst));
  10085. assert(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. }
  10093. }
  10094. static void ggml_compute_forward_map_unary(
  10095. const struct ggml_compute_params * params,
  10096. struct ggml_tensor * dst,
  10097. const ggml_unary_op_f32_t fun) {
  10098. const struct ggml_tensor * src0 = dst->src[0];
  10099. switch (src0->type) {
  10100. case GGML_TYPE_F32:
  10101. {
  10102. ggml_compute_forward_map_unary_f32(params, dst, fun);
  10103. } break;
  10104. default:
  10105. {
  10106. GGML_ABORT("fatal error");
  10107. }
  10108. }
  10109. }
  10110. // ggml_compute_forward_map_binary
  10111. static void ggml_compute_forward_map_binary_f32(
  10112. const struct ggml_compute_params * params,
  10113. struct ggml_tensor * dst,
  10114. const ggml_binary_op_f32_t fun) {
  10115. const struct ggml_tensor * src0 = dst->src[0];
  10116. const struct ggml_tensor * src1 = dst->src[1];
  10117. if (params->ith != 0) {
  10118. return;
  10119. }
  10120. assert(ggml_is_contiguous_1(src0));
  10121. assert(ggml_is_contiguous_1(src1));
  10122. assert(ggml_is_contiguous_1(dst));
  10123. assert(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  10124. const int n = ggml_nrows(src0);
  10125. const int nc = src0->ne[0];
  10126. for (int i = 0; i < n; i++) {
  10127. fun(nc,
  10128. (float *) ((char *) dst->data + i*( dst->nb[1])),
  10129. (float *) ((char *) src0->data + i*(src0->nb[1])),
  10130. (float *) ((char *) src1->data + i*(src1->nb[1])));
  10131. }
  10132. }
  10133. static void ggml_compute_forward_map_binary(
  10134. const struct ggml_compute_params * params,
  10135. struct ggml_tensor * dst,
  10136. const ggml_binary_op_f32_t fun) {
  10137. const struct ggml_tensor * src0 = dst->src[0];
  10138. switch (src0->type) {
  10139. case GGML_TYPE_F32:
  10140. {
  10141. ggml_compute_forward_map_binary_f32(params, dst, fun);
  10142. } break;
  10143. default:
  10144. {
  10145. GGML_ABORT("fatal error");
  10146. }
  10147. }
  10148. }
  10149. // ggml_compute_forward_map_custom1
  10150. static void ggml_compute_forward_map_custom1_f32(
  10151. const struct ggml_compute_params * params,
  10152. struct ggml_tensor * dst,
  10153. const ggml_custom1_op_f32_t fun) {
  10154. const struct ggml_tensor * a = dst->src[0];
  10155. if (params->ith != 0) {
  10156. return;
  10157. }
  10158. fun(dst, a);
  10159. }
  10160. // ggml_compute_forward_map_custom2
  10161. static void ggml_compute_forward_map_custom2_f32(
  10162. const struct ggml_compute_params * params,
  10163. struct ggml_tensor * dst,
  10164. const ggml_custom2_op_f32_t fun) {
  10165. const struct ggml_tensor * a = dst->src[0];
  10166. const struct ggml_tensor * b = dst->src[1];
  10167. if (params->ith != 0) {
  10168. return;
  10169. }
  10170. fun(dst, a, b);
  10171. }
  10172. // ggml_compute_forward_map_custom3
  10173. static void ggml_compute_forward_map_custom3_f32(
  10174. const struct ggml_compute_params * params,
  10175. struct ggml_tensor * dst,
  10176. const ggml_custom3_op_f32_t fun) {
  10177. const struct ggml_tensor * a = dst->src[0];
  10178. const struct ggml_tensor * b = dst->src[1];
  10179. const struct ggml_tensor * c = dst->src[1];
  10180. if (params->ith != 0) {
  10181. return;
  10182. }
  10183. fun(dst, a, b, c);
  10184. }
  10185. // ggml_compute_forward_map_custom1
  10186. static void ggml_compute_forward_map_custom1(
  10187. const struct ggml_compute_params * params,
  10188. struct ggml_tensor * dst) {
  10189. const struct ggml_tensor * a = dst->src[0];
  10190. struct ggml_map_custom1_op_params p;
  10191. memcpy(&p, dst->op_params, sizeof(p));
  10192. p.fun(dst, a, params->ith, params->nth, p.userdata);
  10193. }
  10194. // ggml_compute_forward_map_custom2
  10195. static void ggml_compute_forward_map_custom2(
  10196. const struct ggml_compute_params * params,
  10197. struct ggml_tensor * dst) {
  10198. const struct ggml_tensor * a = dst->src[0];
  10199. const struct ggml_tensor * b = dst->src[1];
  10200. struct ggml_map_custom2_op_params p;
  10201. memcpy(&p, dst->op_params, sizeof(p));
  10202. p.fun(dst, a, b, params->ith, params->nth, p.userdata);
  10203. }
  10204. // ggml_compute_forward_map_custom3
  10205. static void ggml_compute_forward_map_custom3(
  10206. const struct ggml_compute_params * params,
  10207. struct ggml_tensor * dst) {
  10208. const struct ggml_tensor * a = dst->src[0];
  10209. const struct ggml_tensor * b = dst->src[1];
  10210. const struct ggml_tensor * c = dst->src[2];
  10211. struct ggml_map_custom3_op_params p;
  10212. memcpy(&p, dst->op_params, sizeof(p));
  10213. p.fun(dst, a, b, c, params->ith, params->nth, p.userdata);
  10214. }
  10215. // ggml_compute_forward_cross_entropy_loss
  10216. static void ggml_compute_forward_cross_entropy_loss_f32(
  10217. const struct ggml_compute_params * params,
  10218. struct ggml_tensor * dst) {
  10219. const struct ggml_tensor * src0 = dst->src[0];
  10220. const struct ggml_tensor * src1 = dst->src[1];
  10221. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10222. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10223. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  10224. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  10225. GGML_ASSERT(ggml_are_same_shape(src0, src1));
  10226. GGML_ASSERT(ggml_is_scalar(dst));
  10227. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10228. // TODO: handle transposed/permuted matrices
  10229. const int64_t nc = src0->ne[0];
  10230. const int64_t nr = ggml_nrows(src0);
  10231. const int ith = params->ith;
  10232. const int nth = params->nth;
  10233. float * sums = (float *) params->wdata;
  10234. float * st = ((float *) params->wdata) + nth + ith*nc;
  10235. float sum_thread = 0.0f;
  10236. GGML_ASSERT(params->wsize >= sizeof(float) * (nth + nth * nc));
  10237. // rows per thread
  10238. const int64_t dr = (nr + nth - 1)/nth;
  10239. // row range for this thread
  10240. const int64_t ir0 = dr*ith;
  10241. const int64_t ir1 = MIN(ir0 + dr, nr);
  10242. for (int64_t i1 = ir0; i1 < ir1; ++i1) {
  10243. const float * s0 = (const float *)((const char *) src0->data + i1*src0->nb[1]);
  10244. const float * s1 = (const float *)((const char *) src1->data + i1*src1->nb[1]);
  10245. #ifndef NDEBUG
  10246. for (int64_t i = 0; i < nc; ++i) {
  10247. //printf("p[%d] = %f\n", i, p[i]);
  10248. assert(!isnan(s0[i]));
  10249. assert(!isnan(s1[i]));
  10250. }
  10251. #endif
  10252. float max = -INFINITY;
  10253. ggml_vec_max_f32(nc, &max, s0);
  10254. const ggml_float sum_softmax = ggml_vec_log_soft_max_f32(nc, st, s0, max);
  10255. assert(sum_softmax >= 0.0);
  10256. ggml_vec_add1_f32(nc, st, st, -sum_softmax);
  10257. ggml_vec_mul_f32(nc, st, st, s1);
  10258. float sum_st = 0.0f;
  10259. ggml_vec_sum_f32(nc, &sum_st, st);
  10260. sum_thread += sum_st;
  10261. #ifndef NDEBUG
  10262. for (int64_t i = 0; i < nc; ++i) {
  10263. assert(!isnan(st[i]));
  10264. assert(!isinf(st[i]));
  10265. }
  10266. #endif
  10267. }
  10268. sums[ith] = sum_thread;
  10269. ggml_barrier(params->threadpool);
  10270. if (ith == 0) {
  10271. float * dp = (float *) dst->data;
  10272. ggml_vec_sum_f32(nth, dp, sums);
  10273. dp[0] *= -1.0f / (float) nr;
  10274. }
  10275. }
  10276. static void ggml_compute_forward_cross_entropy_loss(
  10277. const struct ggml_compute_params * params,
  10278. struct ggml_tensor * dst) {
  10279. const struct ggml_tensor * src0 = dst->src[0];
  10280. switch (src0->type) {
  10281. case GGML_TYPE_F32:
  10282. {
  10283. ggml_compute_forward_cross_entropy_loss_f32(params, dst);
  10284. } break;
  10285. default:
  10286. {
  10287. GGML_ABORT("fatal error");
  10288. }
  10289. }
  10290. }
  10291. // ggml_compute_forward_cross_entropy_loss_back
  10292. static void ggml_compute_forward_cross_entropy_loss_back_f32(
  10293. const struct ggml_compute_params * params,
  10294. struct ggml_tensor * dst) {
  10295. const struct ggml_tensor * grad = dst->src[0]; // gradient of forward pass output
  10296. const struct ggml_tensor * src0f = dst->src[1]; // src0 of forward pass
  10297. const struct ggml_tensor * src1f = dst->src[2]; // src1 of forward pass
  10298. GGML_ASSERT(ggml_is_contiguous(dst));
  10299. GGML_ASSERT(ggml_is_contiguous(src0f));
  10300. GGML_ASSERT(ggml_is_contiguous(src1f));
  10301. GGML_ASSERT(ggml_is_contiguous(grad));
  10302. GGML_ASSERT(ggml_are_same_shape(src0f, src1f) && ggml_are_same_shape(src0f, dst));
  10303. const int64_t ith = params->ith;
  10304. const int64_t nth = params->nth;
  10305. // TODO: handle transposed/permuted matrices
  10306. const int64_t nc = src0f->ne[0];
  10307. const int64_t nr = ggml_nrows(src0f);
  10308. // rows per thread
  10309. const int64_t dr = (nr + nth - 1)/nth;
  10310. // row range for this thread
  10311. const int64_t ir0 = dr*ith;
  10312. const int64_t ir1 = MIN(ir0 + dr, nr);
  10313. const float d_by_nr = ((const float *) grad->data)[0] / (float) nr;
  10314. for (int64_t i1 = ir0; i1 < ir1; i1++) {
  10315. float * ds0 = (float *)((char *) dst->data + i1*dst->nb[1]);
  10316. const float * s0 = (const float *)((const char *) src0f->data + i1*src0f->nb[1]);
  10317. const float * s1 = (const float *)((const char *) src1f->data + i1*src1f->nb[1]);
  10318. #ifndef NDEBUG
  10319. for (int64_t i = 0; i < nc; ++i) {
  10320. //printf("p[%d] = %f\n", i, p[i]);
  10321. assert(!isnan(s0[i]));
  10322. assert(!isnan(s1[i]));
  10323. }
  10324. #endif
  10325. // soft_max
  10326. float max = -INFINITY;
  10327. ggml_vec_max_f32(nc, &max, s0);
  10328. const ggml_float sum = ggml_vec_soft_max_f32(nc, ds0, s0, max);
  10329. assert(sum > 0.0);
  10330. ggml_vec_scale_f32(nc, ds0, 1.0/sum);
  10331. // grad(src0f) = (softmax(src0f) - src1f) * grad(cross_entropy_loss(src0f, src1f)) / nr
  10332. ggml_vec_sub_f32(nc, ds0, ds0, s1);
  10333. ggml_vec_scale_f32(nc, ds0, d_by_nr);
  10334. #ifndef NDEBUG
  10335. for (int64_t i = 0; i < nc; ++i) {
  10336. assert(!isnan(ds0[i]));
  10337. assert(!isinf(ds0[i]));
  10338. }
  10339. #endif
  10340. }
  10341. }
  10342. static void ggml_compute_forward_cross_entropy_loss_back(
  10343. const struct ggml_compute_params * params,
  10344. struct ggml_tensor * dst) {
  10345. const struct ggml_tensor * src0 = dst->src[0];
  10346. switch (src0->type) {
  10347. case GGML_TYPE_F32:
  10348. {
  10349. ggml_compute_forward_cross_entropy_loss_back_f32(params, dst);
  10350. } break;
  10351. default:
  10352. {
  10353. GGML_ABORT("fatal error");
  10354. }
  10355. }
  10356. }
  10357. static void ggml_compute_forward_opt_step_adamw_f32(
  10358. const struct ggml_compute_params * params,
  10359. struct ggml_tensor * dst) {
  10360. const struct ggml_tensor * src0 = dst->src[0];
  10361. const struct ggml_tensor * src0_grad = dst->src[1];
  10362. const struct ggml_tensor * src0_grad_m = dst->src[2];
  10363. const struct ggml_tensor * src0_grad_v = dst->src[3];
  10364. const struct ggml_tensor * adamw_params = dst->src[4];
  10365. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad));
  10366. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_m));
  10367. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_v));
  10368. GGML_ASSERT(ggml_nelements(adamw_params) == 7);
  10369. const int ith = params->ith;
  10370. const int nth = params->nth;
  10371. const int nr = ggml_nrows(src0);
  10372. GGML_TENSOR_UNARY_OP_LOCALS
  10373. GGML_ASSERT(nb00 == sizeof(float));
  10374. // rows per thread
  10375. const int dr = (nr + nth - 1)/nth;
  10376. // row range for this thread
  10377. const int ir0 = dr*ith;
  10378. const int ir1 = MIN(ir0 + dr, nr);
  10379. const float * adamw_params_ptr = ggml_get_data_f32(adamw_params);
  10380. const float alpha = adamw_params_ptr[0];
  10381. const float beta1 = adamw_params_ptr[1];
  10382. const float beta2 = adamw_params_ptr[2];
  10383. const float eps = adamw_params_ptr[3];
  10384. const float wd = adamw_params_ptr[4];
  10385. const float beta1h = adamw_params_ptr[5];
  10386. const float beta2h = adamw_params_ptr[6];
  10387. for (int ir = ir0; ir < ir1; ++ir) {
  10388. const int64_t i03 = ir/(ne02*ne01);
  10389. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  10390. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  10391. const size_t offset = i03*nb03 + i02*nb02 + i01*nb01;
  10392. float * w = (float *) ((char *) src0->data + offset); // weight
  10393. const float * g = (const float *) ((const char *) src0_grad->data + offset); // grad
  10394. float * m = (float *) ((char *) src0_grad_m->data + offset);
  10395. float * v = (float *) ((char *) src0_grad_v->data + offset);
  10396. for (int i00 = 0; i00 < ne00; ++i00) {
  10397. m[i00] = m[i00]*beta1 + g[i00]*(1.0f - beta1);
  10398. v[i00] = v[i00]*beta2 + g[i00]*g[i00]*(1.0f - beta2);
  10399. const float mh = m[i00]*beta1h;
  10400. const float vh = sqrtf(v[i00]*beta2h) + eps;
  10401. // The weight decay is applied independently of the Adam momenta m and v.
  10402. // This is NOT equivalent to l2 regularization that adds w[i00]*w[i00] to the loss.
  10403. // See: https://arxiv.org/pdf/1711.05101v3.pdf
  10404. w[i00] = w[i00]*(1.0f - alpha*wd) - alpha*mh/vh;
  10405. }
  10406. }
  10407. }
  10408. static void ggml_compute_forward_opt_step_adamw(
  10409. const struct ggml_compute_params * params,
  10410. struct ggml_tensor * dst) {
  10411. const struct ggml_tensor * src0 = dst->src[0];
  10412. switch (src0->type) {
  10413. case GGML_TYPE_F32:
  10414. {
  10415. ggml_compute_forward_opt_step_adamw_f32(params, dst);
  10416. } break;
  10417. default:
  10418. {
  10419. GGML_ABORT("fatal error");
  10420. }
  10421. }
  10422. }
  10423. /////////////////////////////////
  10424. static void ggml_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  10425. GGML_ASSERT(params);
  10426. if (tensor->op == GGML_OP_NONE || ggml_is_empty(tensor)) {
  10427. return;
  10428. }
  10429. // extra_buffer op?
  10430. if (ggml_cpu_extra_compute_forward(params, tensor)) return;
  10431. switch (tensor->op) {
  10432. case GGML_OP_DUP:
  10433. {
  10434. ggml_compute_forward_dup(params, tensor);
  10435. } break;
  10436. case GGML_OP_ADD:
  10437. {
  10438. ggml_compute_forward_add(params, tensor);
  10439. } break;
  10440. case GGML_OP_ADD1:
  10441. {
  10442. ggml_compute_forward_add1(params, tensor);
  10443. } break;
  10444. case GGML_OP_ACC:
  10445. {
  10446. ggml_compute_forward_acc(params, tensor);
  10447. } break;
  10448. case GGML_OP_SUB:
  10449. {
  10450. ggml_compute_forward_sub(params, tensor);
  10451. } break;
  10452. case GGML_OP_MUL:
  10453. {
  10454. ggml_compute_forward_mul(params, tensor);
  10455. } break;
  10456. case GGML_OP_DIV:
  10457. {
  10458. ggml_compute_forward_div(params, tensor);
  10459. } break;
  10460. case GGML_OP_SQR:
  10461. {
  10462. ggml_compute_forward_sqr(params, tensor);
  10463. } break;
  10464. case GGML_OP_SQRT:
  10465. {
  10466. ggml_compute_forward_sqrt(params, tensor);
  10467. } break;
  10468. case GGML_OP_LOG:
  10469. {
  10470. ggml_compute_forward_log(params, tensor);
  10471. } break;
  10472. case GGML_OP_SIN:
  10473. {
  10474. ggml_compute_forward_sin(params, tensor);
  10475. } break;
  10476. case GGML_OP_COS:
  10477. {
  10478. ggml_compute_forward_cos(params, tensor);
  10479. } break;
  10480. case GGML_OP_SUM:
  10481. {
  10482. ggml_compute_forward_sum(params, tensor);
  10483. } break;
  10484. case GGML_OP_SUM_ROWS:
  10485. {
  10486. ggml_compute_forward_sum_rows(params, tensor);
  10487. } break;
  10488. case GGML_OP_MEAN:
  10489. {
  10490. ggml_compute_forward_mean(params, tensor);
  10491. } break;
  10492. case GGML_OP_ARGMAX:
  10493. {
  10494. ggml_compute_forward_argmax(params, tensor);
  10495. } break;
  10496. case GGML_OP_COUNT_EQUAL:
  10497. {
  10498. ggml_compute_forward_count_equal(params, tensor);
  10499. } break;
  10500. case GGML_OP_REPEAT:
  10501. {
  10502. ggml_compute_forward_repeat(params, tensor);
  10503. } break;
  10504. case GGML_OP_REPEAT_BACK:
  10505. {
  10506. ggml_compute_forward_repeat_back(params, tensor);
  10507. } break;
  10508. case GGML_OP_CONCAT:
  10509. {
  10510. ggml_compute_forward_concat(params, tensor);
  10511. } break;
  10512. case GGML_OP_SILU_BACK:
  10513. {
  10514. ggml_compute_forward_silu_back(params, tensor);
  10515. } break;
  10516. case GGML_OP_NORM:
  10517. {
  10518. ggml_compute_forward_norm(params, tensor);
  10519. } break;
  10520. case GGML_OP_RMS_NORM:
  10521. {
  10522. ggml_compute_forward_rms_norm(params, tensor);
  10523. } break;
  10524. case GGML_OP_RMS_NORM_BACK:
  10525. {
  10526. ggml_compute_forward_rms_norm_back(params, tensor);
  10527. } break;
  10528. case GGML_OP_GROUP_NORM:
  10529. {
  10530. ggml_compute_forward_group_norm(params, tensor);
  10531. } break;
  10532. case GGML_OP_MUL_MAT:
  10533. {
  10534. ggml_compute_forward_mul_mat(params, tensor);
  10535. } break;
  10536. case GGML_OP_MUL_MAT_ID:
  10537. {
  10538. ggml_compute_forward_mul_mat_id(params, tensor);
  10539. } break;
  10540. case GGML_OP_OUT_PROD:
  10541. {
  10542. ggml_compute_forward_out_prod(params, tensor);
  10543. } break;
  10544. case GGML_OP_SCALE:
  10545. {
  10546. ggml_compute_forward_scale(params, tensor);
  10547. } break;
  10548. case GGML_OP_SET:
  10549. {
  10550. ggml_compute_forward_set(params, tensor);
  10551. } break;
  10552. case GGML_OP_CPY:
  10553. {
  10554. ggml_compute_forward_cpy(params, tensor);
  10555. } break;
  10556. case GGML_OP_CONT:
  10557. {
  10558. ggml_compute_forward_cont(params, tensor);
  10559. } break;
  10560. case GGML_OP_RESHAPE:
  10561. {
  10562. ggml_compute_forward_reshape(params, tensor);
  10563. } break;
  10564. case GGML_OP_VIEW:
  10565. {
  10566. ggml_compute_forward_view(params, tensor);
  10567. } break;
  10568. case GGML_OP_PERMUTE:
  10569. {
  10570. ggml_compute_forward_permute(params, tensor);
  10571. } break;
  10572. case GGML_OP_TRANSPOSE:
  10573. {
  10574. ggml_compute_forward_transpose(params, tensor);
  10575. } break;
  10576. case GGML_OP_GET_ROWS:
  10577. {
  10578. ggml_compute_forward_get_rows(params, tensor);
  10579. } break;
  10580. case GGML_OP_GET_ROWS_BACK:
  10581. {
  10582. ggml_compute_forward_get_rows_back(params, tensor);
  10583. } break;
  10584. case GGML_OP_DIAG:
  10585. {
  10586. ggml_compute_forward_diag(params, tensor);
  10587. } break;
  10588. case GGML_OP_DIAG_MASK_INF:
  10589. {
  10590. ggml_compute_forward_diag_mask_inf(params, tensor);
  10591. } break;
  10592. case GGML_OP_DIAG_MASK_ZERO:
  10593. {
  10594. ggml_compute_forward_diag_mask_zero(params, tensor);
  10595. } break;
  10596. case GGML_OP_SOFT_MAX:
  10597. {
  10598. ggml_compute_forward_soft_max(params, tensor);
  10599. } break;
  10600. case GGML_OP_SOFT_MAX_BACK:
  10601. {
  10602. ggml_compute_forward_soft_max_ext_back(params, tensor);
  10603. } break;
  10604. case GGML_OP_ROPE:
  10605. {
  10606. ggml_compute_forward_rope(params, tensor);
  10607. } break;
  10608. case GGML_OP_ROPE_BACK:
  10609. {
  10610. ggml_compute_forward_rope_back(params, tensor);
  10611. } break;
  10612. case GGML_OP_CLAMP:
  10613. {
  10614. ggml_compute_forward_clamp(params, tensor);
  10615. } break;
  10616. case GGML_OP_CONV_TRANSPOSE_1D:
  10617. {
  10618. ggml_compute_forward_conv_transpose_1d(params, tensor);
  10619. } break;
  10620. case GGML_OP_IM2COL:
  10621. {
  10622. ggml_compute_forward_im2col(params, tensor);
  10623. } break;
  10624. case GGML_OP_IM2COL_BACK:
  10625. {
  10626. ggml_compute_forward_im2col_back_f32(params, tensor);
  10627. } break;
  10628. case GGML_OP_CONV_TRANSPOSE_2D:
  10629. {
  10630. ggml_compute_forward_conv_transpose_2d(params, tensor);
  10631. } break;
  10632. case GGML_OP_POOL_1D:
  10633. {
  10634. ggml_compute_forward_pool_1d(params, tensor);
  10635. } break;
  10636. case GGML_OP_POOL_2D:
  10637. {
  10638. ggml_compute_forward_pool_2d(params, tensor);
  10639. } break;
  10640. case GGML_OP_POOL_2D_BACK:
  10641. {
  10642. ggml_compute_forward_pool_2d_back(params, tensor);
  10643. } break;
  10644. case GGML_OP_UPSCALE:
  10645. {
  10646. ggml_compute_forward_upscale(params, tensor);
  10647. } break;
  10648. case GGML_OP_PAD:
  10649. {
  10650. ggml_compute_forward_pad(params, tensor);
  10651. } break;
  10652. case GGML_OP_PAD_REFLECT_1D:
  10653. {
  10654. ggml_compute_forward_pad_reflect_1d(params, tensor);
  10655. } break;
  10656. case GGML_OP_ARANGE:
  10657. {
  10658. ggml_compute_forward_arange(params, tensor);
  10659. } break;
  10660. case GGML_OP_TIMESTEP_EMBEDDING:
  10661. {
  10662. ggml_compute_forward_timestep_embedding(params, tensor);
  10663. } break;
  10664. case GGML_OP_ARGSORT:
  10665. {
  10666. ggml_compute_forward_argsort(params, tensor);
  10667. } break;
  10668. case GGML_OP_LEAKY_RELU:
  10669. {
  10670. ggml_compute_forward_leaky_relu(params, tensor);
  10671. } break;
  10672. case GGML_OP_FLASH_ATTN_EXT:
  10673. {
  10674. ggml_compute_forward_flash_attn_ext(params, tensor->src[0], tensor->src[1], tensor->src[2], tensor->src[3], tensor);
  10675. } break;
  10676. case GGML_OP_FLASH_ATTN_BACK:
  10677. {
  10678. int32_t t = ggml_get_op_params_i32(tensor, 0);
  10679. GGML_ASSERT(t == 0 || t == 1);
  10680. bool masked = t != 0;
  10681. ggml_compute_forward_flash_attn_back(params, masked, tensor);
  10682. } break;
  10683. case GGML_OP_SSM_CONV:
  10684. {
  10685. ggml_compute_forward_ssm_conv(params, tensor);
  10686. } break;
  10687. case GGML_OP_SSM_SCAN:
  10688. {
  10689. ggml_compute_forward_ssm_scan(params, tensor);
  10690. } break;
  10691. case GGML_OP_WIN_PART:
  10692. {
  10693. ggml_compute_forward_win_part(params, tensor);
  10694. } break;
  10695. case GGML_OP_WIN_UNPART:
  10696. {
  10697. ggml_compute_forward_win_unpart(params, tensor);
  10698. } break;
  10699. case GGML_OP_UNARY:
  10700. {
  10701. ggml_compute_forward_unary(params, tensor);
  10702. } break;
  10703. case GGML_OP_GET_REL_POS:
  10704. {
  10705. ggml_compute_forward_get_rel_pos(params, tensor);
  10706. } break;
  10707. case GGML_OP_ADD_REL_POS:
  10708. {
  10709. ggml_compute_forward_add_rel_pos(params, tensor);
  10710. } break;
  10711. case GGML_OP_RWKV_WKV6:
  10712. {
  10713. ggml_compute_forward_rwkv_wkv6(params, tensor);
  10714. } break;
  10715. case GGML_OP_GATED_LINEAR_ATTN:
  10716. {
  10717. ggml_compute_forward_gla(params, tensor);
  10718. } break;
  10719. case GGML_OP_MAP_UNARY:
  10720. {
  10721. ggml_unary_op_f32_t fun;
  10722. memcpy(&fun, tensor->op_params, sizeof(fun));
  10723. ggml_compute_forward_map_unary(params, tensor, fun);
  10724. }
  10725. break;
  10726. case GGML_OP_MAP_BINARY:
  10727. {
  10728. ggml_binary_op_f32_t fun;
  10729. memcpy(&fun, tensor->op_params, sizeof(fun));
  10730. ggml_compute_forward_map_binary(params, tensor, fun);
  10731. }
  10732. break;
  10733. case GGML_OP_MAP_CUSTOM1_F32:
  10734. {
  10735. ggml_custom1_op_f32_t fun;
  10736. memcpy(&fun, tensor->op_params, sizeof(fun));
  10737. ggml_compute_forward_map_custom1_f32(params, tensor, fun);
  10738. }
  10739. break;
  10740. case GGML_OP_MAP_CUSTOM2_F32:
  10741. {
  10742. ggml_custom2_op_f32_t fun;
  10743. memcpy(&fun, tensor->op_params, sizeof(fun));
  10744. ggml_compute_forward_map_custom2_f32(params, tensor, fun);
  10745. }
  10746. break;
  10747. case GGML_OP_MAP_CUSTOM3_F32:
  10748. {
  10749. ggml_custom3_op_f32_t fun;
  10750. memcpy(&fun, tensor->op_params, sizeof(fun));
  10751. ggml_compute_forward_map_custom3_f32(params, tensor, fun);
  10752. }
  10753. break;
  10754. case GGML_OP_MAP_CUSTOM1:
  10755. {
  10756. ggml_compute_forward_map_custom1(params, tensor);
  10757. }
  10758. break;
  10759. case GGML_OP_MAP_CUSTOM2:
  10760. {
  10761. ggml_compute_forward_map_custom2(params, tensor);
  10762. }
  10763. break;
  10764. case GGML_OP_MAP_CUSTOM3:
  10765. {
  10766. ggml_compute_forward_map_custom3(params, tensor);
  10767. }
  10768. break;
  10769. case GGML_OP_CROSS_ENTROPY_LOSS:
  10770. {
  10771. ggml_compute_forward_cross_entropy_loss(params, tensor);
  10772. }
  10773. break;
  10774. case GGML_OP_CROSS_ENTROPY_LOSS_BACK:
  10775. {
  10776. ggml_compute_forward_cross_entropy_loss_back(params, tensor);
  10777. }
  10778. break;
  10779. case GGML_OP_OPT_STEP_ADAMW:
  10780. {
  10781. ggml_compute_forward_opt_step_adamw(params, tensor);
  10782. }
  10783. break;
  10784. case GGML_OP_NONE:
  10785. {
  10786. // nop
  10787. } break;
  10788. case GGML_OP_COUNT:
  10789. {
  10790. GGML_ABORT("fatal error");
  10791. }
  10792. }
  10793. }
  10794. // Android's libc implementation "bionic" does not support setting affinity
  10795. #if defined(__gnu_linux__)
  10796. static void set_numa_thread_affinity(int thread_n) {
  10797. if (!ggml_is_numa()) {
  10798. return;
  10799. }
  10800. int node_num;
  10801. int rv;
  10802. size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus);
  10803. switch(g_state.numa.numa_strategy) {
  10804. case GGML_NUMA_STRATEGY_DISTRIBUTE:
  10805. // run thread on node_num thread_n / (threads per node)
  10806. node_num = thread_n % g_state.numa.n_nodes;
  10807. break;
  10808. case GGML_NUMA_STRATEGY_ISOLATE:
  10809. // run thread on current_node
  10810. node_num = g_state.numa.current_node;
  10811. break;
  10812. case GGML_NUMA_STRATEGY_NUMACTL:
  10813. // use the cpuset that numactl gave us
  10814. rv = pthread_setaffinity_np(pthread_self(), setsize, &g_state.numa.cpuset);
  10815. if (rv) {
  10816. fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n",strerror(rv));
  10817. }
  10818. return;
  10819. default:
  10820. return;
  10821. }
  10822. struct ggml_numa_node * node = &g_state.numa.nodes[node_num];
  10823. cpu_set_t * cpus = CPU_ALLOC(g_state.numa.total_cpus);
  10824. CPU_ZERO_S(setsize, cpus);
  10825. for (size_t i = 0; i < node->n_cpus; ++i) {
  10826. CPU_SET_S(node->cpus[i], setsize, cpus);
  10827. }
  10828. rv = pthread_setaffinity_np(pthread_self(), setsize, cpus);
  10829. if (rv) {
  10830. fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n", strerror(rv));
  10831. }
  10832. CPU_FREE(cpus);
  10833. }
  10834. static void clear_numa_thread_affinity(void) {
  10835. if (!ggml_is_numa()) {
  10836. return;
  10837. }
  10838. size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus);
  10839. cpu_set_t * cpus = CPU_ALLOC(g_state.numa.total_cpus);
  10840. CPU_ZERO_S(setsize, cpus);
  10841. for (unsigned i = 0; i < g_state.numa.total_cpus; ++i) {
  10842. CPU_SET_S(i, setsize, cpus);
  10843. }
  10844. int rv = pthread_setaffinity_np(pthread_self(), setsize, cpus);
  10845. if (rv) {
  10846. fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n", strerror(rv));
  10847. }
  10848. CPU_FREE(cpus);
  10849. }
  10850. #else
  10851. // TODO: Windows etc.
  10852. // (the linux implementation may also work on BSD, someone should test)
  10853. static void set_numa_thread_affinity(int thread_n) { UNUSED(thread_n); }
  10854. static void clear_numa_thread_affinity(void) {}
  10855. #endif
  10856. static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) {
  10857. int n_tasks = 0;
  10858. if (ggml_is_empty(node)) {
  10859. // no need to multi-thread a no-op
  10860. n_tasks = 1;
  10861. return n_tasks;
  10862. }
  10863. switch (node->op) {
  10864. case GGML_OP_CPY:
  10865. case GGML_OP_DUP:
  10866. case GGML_OP_CONT:
  10867. case GGML_OP_ADD:
  10868. case GGML_OP_ADD1:
  10869. case GGML_OP_ACC:
  10870. {
  10871. n_tasks = n_threads;
  10872. } break;
  10873. case GGML_OP_SUB:
  10874. case GGML_OP_SQR:
  10875. case GGML_OP_SQRT:
  10876. case GGML_OP_LOG:
  10877. case GGML_OP_SIN:
  10878. case GGML_OP_COS:
  10879. case GGML_OP_SUM:
  10880. case GGML_OP_SUM_ROWS:
  10881. case GGML_OP_MEAN:
  10882. case GGML_OP_ARGMAX:
  10883. {
  10884. n_tasks = 1;
  10885. } break;
  10886. case GGML_OP_COUNT_EQUAL:
  10887. {
  10888. n_tasks = n_threads;
  10889. } break;
  10890. case GGML_OP_REPEAT:
  10891. case GGML_OP_REPEAT_BACK:
  10892. case GGML_OP_LEAKY_RELU:
  10893. {
  10894. n_tasks = 1;
  10895. } break;
  10896. case GGML_OP_UNARY:
  10897. switch (ggml_get_unary_op(node)) {
  10898. case GGML_UNARY_OP_ABS:
  10899. case GGML_UNARY_OP_SGN:
  10900. case GGML_UNARY_OP_NEG:
  10901. case GGML_UNARY_OP_STEP:
  10902. case GGML_UNARY_OP_TANH:
  10903. case GGML_UNARY_OP_ELU:
  10904. case GGML_UNARY_OP_RELU:
  10905. case GGML_UNARY_OP_SIGMOID:
  10906. case GGML_UNARY_OP_HARDSWISH:
  10907. case GGML_UNARY_OP_HARDSIGMOID:
  10908. case GGML_UNARY_OP_EXP:
  10909. {
  10910. n_tasks = 1;
  10911. } break;
  10912. case GGML_UNARY_OP_GELU:
  10913. case GGML_UNARY_OP_GELU_QUICK:
  10914. case GGML_UNARY_OP_SILU:
  10915. {
  10916. n_tasks = n_threads;
  10917. } break;
  10918. default:
  10919. GGML_ABORT("fatal error");
  10920. }
  10921. break;
  10922. case GGML_OP_SILU_BACK:
  10923. case GGML_OP_MUL:
  10924. case GGML_OP_DIV:
  10925. case GGML_OP_NORM:
  10926. case GGML_OP_RMS_NORM:
  10927. case GGML_OP_RMS_NORM_BACK:
  10928. case GGML_OP_GROUP_NORM:
  10929. case GGML_OP_CONCAT:
  10930. case GGML_OP_MUL_MAT:
  10931. case GGML_OP_MUL_MAT_ID:
  10932. case GGML_OP_OUT_PROD:
  10933. {
  10934. n_tasks = n_threads;
  10935. } break;
  10936. case GGML_OP_GET_ROWS:
  10937. {
  10938. // FIXME: get_rows can use additional threads, but the cost of launching additional threads
  10939. // decreases performance with GPU offloading
  10940. //n_tasks = n_threads;
  10941. n_tasks = 1;
  10942. } break;
  10943. case GGML_OP_SCALE:
  10944. case GGML_OP_SET:
  10945. case GGML_OP_RESHAPE:
  10946. case GGML_OP_VIEW:
  10947. case GGML_OP_PERMUTE:
  10948. case GGML_OP_TRANSPOSE:
  10949. case GGML_OP_GET_ROWS_BACK:
  10950. case GGML_OP_DIAG:
  10951. {
  10952. n_tasks = 1;
  10953. } break;
  10954. case GGML_OP_DIAG_MASK_ZERO:
  10955. case GGML_OP_DIAG_MASK_INF:
  10956. case GGML_OP_SOFT_MAX_BACK:
  10957. case GGML_OP_ROPE:
  10958. case GGML_OP_ROPE_BACK:
  10959. case GGML_OP_ADD_REL_POS:
  10960. {
  10961. n_tasks = n_threads;
  10962. } break;
  10963. case GGML_OP_CLAMP:
  10964. {
  10965. n_tasks = 1; //TODO
  10966. } break;
  10967. case GGML_OP_SOFT_MAX:
  10968. {
  10969. n_tasks = MIN(n_threads, ggml_nrows(node->src[0]));
  10970. } break;
  10971. case GGML_OP_IM2COL:
  10972. case GGML_OP_IM2COL_BACK:
  10973. case GGML_OP_CONV_TRANSPOSE_1D:
  10974. case GGML_OP_CONV_TRANSPOSE_2D:
  10975. {
  10976. n_tasks = n_threads;
  10977. } break;
  10978. case GGML_OP_POOL_1D:
  10979. case GGML_OP_POOL_2D:
  10980. case GGML_OP_POOL_2D_BACK:
  10981. {
  10982. n_tasks = 1;
  10983. } break;
  10984. case GGML_OP_UPSCALE:
  10985. case GGML_OP_PAD:
  10986. case GGML_OP_PAD_REFLECT_1D:
  10987. case GGML_OP_ARANGE:
  10988. case GGML_OP_TIMESTEP_EMBEDDING:
  10989. case GGML_OP_ARGSORT:
  10990. case GGML_OP_FLASH_ATTN_EXT:
  10991. case GGML_OP_FLASH_ATTN_BACK:
  10992. case GGML_OP_SSM_CONV:
  10993. case GGML_OP_SSM_SCAN:
  10994. {
  10995. n_tasks = n_threads;
  10996. } break;
  10997. case GGML_OP_WIN_PART:
  10998. case GGML_OP_WIN_UNPART:
  10999. case GGML_OP_GET_REL_POS:
  11000. case GGML_OP_RWKV_WKV6:
  11001. case GGML_OP_GATED_LINEAR_ATTN:
  11002. case GGML_OP_MAP_UNARY:
  11003. case GGML_OP_MAP_BINARY:
  11004. case GGML_OP_MAP_CUSTOM1_F32:
  11005. case GGML_OP_MAP_CUSTOM2_F32:
  11006. case GGML_OP_MAP_CUSTOM3_F32:
  11007. {
  11008. n_tasks = 1;
  11009. } break;
  11010. case GGML_OP_MAP_CUSTOM1:
  11011. {
  11012. struct ggml_map_custom1_op_params p;
  11013. memcpy(&p, node->op_params, sizeof(p));
  11014. if (p.n_tasks == GGML_N_TASKS_MAX) {
  11015. n_tasks = n_threads;
  11016. } else {
  11017. n_tasks = MIN(p.n_tasks, n_threads);
  11018. }
  11019. } break;
  11020. case GGML_OP_MAP_CUSTOM2:
  11021. {
  11022. struct ggml_map_custom2_op_params p;
  11023. memcpy(&p, node->op_params, sizeof(p));
  11024. if (p.n_tasks == GGML_N_TASKS_MAX) {
  11025. n_tasks = n_threads;
  11026. } else {
  11027. n_tasks = MIN(p.n_tasks, n_threads);
  11028. }
  11029. } break;
  11030. case GGML_OP_MAP_CUSTOM3:
  11031. {
  11032. struct ggml_map_custom3_op_params p;
  11033. memcpy(&p, node->op_params, sizeof(p));
  11034. if (p.n_tasks == GGML_N_TASKS_MAX) {
  11035. n_tasks = n_threads;
  11036. } else {
  11037. n_tasks = MIN(p.n_tasks, n_threads);
  11038. }
  11039. } break;
  11040. case GGML_OP_CROSS_ENTROPY_LOSS:
  11041. case GGML_OP_CROSS_ENTROPY_LOSS_BACK:
  11042. case GGML_OP_OPT_STEP_ADAMW:
  11043. {
  11044. n_tasks = n_threads;
  11045. } break;
  11046. case GGML_OP_NONE:
  11047. {
  11048. n_tasks = 1;
  11049. } break;
  11050. case GGML_OP_COUNT:
  11051. {
  11052. GGML_ABORT("fatal error");
  11053. }
  11054. default:
  11055. {
  11056. fprintf(stderr, "%s: op not implemented: ", __func__);
  11057. if (node->op < GGML_OP_COUNT) {
  11058. fprintf(stderr, "%s\n", ggml_op_name(node->op));
  11059. } else {
  11060. fprintf(stderr, "%d\n", node->op);
  11061. }
  11062. GGML_ABORT("fatal error");
  11063. }
  11064. }
  11065. assert(n_tasks > 0);
  11066. return n_tasks;
  11067. }
  11068. static thread_ret_t ggml_graph_compute_secondary_thread(void* data);
  11069. #if defined(_WIN32)
  11070. #include "windows.h"
  11071. // TODO: support > 64 CPUs
  11072. static bool ggml_thread_apply_affinity(bool * mask) {
  11073. HANDLE h = GetCurrentThread();
  11074. uint64_t bitmask = 0ULL;
  11075. assert(GGML_MAX_N_THREADS >= 64);
  11076. for (int32_t i = 0; i < 8; i++) {
  11077. int32_t idx = i * 8;
  11078. uint8_t val = 0;
  11079. val |= mask[idx + 0] << 0;
  11080. val |= mask[idx + 1] << 1;
  11081. val |= mask[idx + 2] << 2;
  11082. val |= mask[idx + 3] << 3;
  11083. val |= mask[idx + 4] << 4;
  11084. val |= mask[idx + 5] << 5;
  11085. val |= mask[idx + 6] << 6;
  11086. val |= mask[idx + 7] << 7;
  11087. bitmask |= (uint64_t)val << idx;
  11088. }
  11089. for (int32_t i = 64; i < GGML_MAX_N_THREADS; i++) {
  11090. if (mask[i]) {
  11091. fprintf(stderr, "warn: setting thread-affinity for > 64 CPUs isn't supported on windows!\n");
  11092. break;
  11093. }
  11094. }
  11095. DWORD_PTR m = (DWORD_PTR)bitmask;
  11096. m = SetThreadAffinityMask(h, m);
  11097. return m != 0;
  11098. }
  11099. static bool ggml_thread_apply_priority(int32_t prio) {
  11100. // Note that on Windows the Process Priority Class must be updated in order to set Thread priority.
  11101. // This is up to the applications.
  11102. DWORD p = THREAD_PRIORITY_NORMAL;
  11103. switch (prio) {
  11104. case GGML_SCHED_PRIO_NORMAL: p = THREAD_PRIORITY_NORMAL; break;
  11105. case GGML_SCHED_PRIO_MEDIUM: p = THREAD_PRIORITY_ABOVE_NORMAL; break;
  11106. case GGML_SCHED_PRIO_HIGH: p = THREAD_PRIORITY_HIGHEST; break;
  11107. case GGML_SCHED_PRIO_REALTIME: p = THREAD_PRIORITY_TIME_CRITICAL; break;
  11108. }
  11109. if (prio == GGML_SCHED_PRIO_NORMAL) {
  11110. // Keep inherited policy/priority
  11111. return true;
  11112. }
  11113. if (!SetThreadPriority(GetCurrentThread(), p)) {
  11114. fprintf(stderr, "warn: failed to set thread priority %d : (%d)\n", prio, (int) GetLastError());
  11115. return false;
  11116. }
  11117. return true;
  11118. }
  11119. #elif defined(__APPLE__)
  11120. #include <sys/types.h>
  11121. #include <sys/resource.h>
  11122. static bool ggml_thread_apply_affinity(const bool * mask) {
  11123. // Not supported on Apple platforms
  11124. UNUSED(mask);
  11125. return true;
  11126. }
  11127. static bool ggml_thread_apply_priority(int32_t prio) {
  11128. struct sched_param p;
  11129. int32_t policy = SCHED_OTHER;
  11130. switch (prio) {
  11131. case GGML_SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
  11132. case GGML_SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
  11133. case GGML_SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
  11134. case GGML_SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
  11135. }
  11136. if (prio == GGML_SCHED_PRIO_NORMAL) {
  11137. // Keep inherited policy/priority
  11138. return true;
  11139. }
  11140. int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
  11141. if (err != 0) {
  11142. fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
  11143. return false;
  11144. }
  11145. return true;
  11146. }
  11147. #elif defined(__gnu_linux__)
  11148. // TODO: this may not work on BSD, to be verified
  11149. static bool ggml_thread_apply_affinity(const bool * mask) {
  11150. cpu_set_t cpuset;
  11151. int err;
  11152. CPU_ZERO(&cpuset);
  11153. for (uint32_t i = 0; i < GGML_MAX_N_THREADS; i++) {
  11154. if (mask[i]) {
  11155. GGML_PRINT_DEBUG("Thread %lx: adding %d to cpuset\n", pthread_self(), i);
  11156. CPU_SET(i, &cpuset);
  11157. }
  11158. }
  11159. #ifdef __ANDROID__
  11160. err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
  11161. if (err < 0) {
  11162. err = errno;
  11163. }
  11164. #else
  11165. err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
  11166. #endif
  11167. if (err != 0) {
  11168. fprintf(stderr, "warn: failed to set affinity mask 0x%llx : %s (%d)\n", (unsigned long long)mask, strerror(err), err);
  11169. return false;
  11170. }
  11171. return true;
  11172. }
  11173. static bool ggml_thread_apply_priority(int32_t prio) {
  11174. struct sched_param p;
  11175. int32_t policy = SCHED_OTHER;
  11176. switch (prio) {
  11177. case GGML_SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
  11178. case GGML_SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
  11179. case GGML_SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
  11180. case GGML_SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
  11181. }
  11182. if (prio == GGML_SCHED_PRIO_NORMAL) {
  11183. // Keep inherited policy/priority
  11184. return true;
  11185. }
  11186. int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
  11187. if (err != 0) {
  11188. fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
  11189. return false;
  11190. }
  11191. return true;
  11192. }
  11193. #else // unsupported platforms
  11194. static bool ggml_thread_apply_affinity(const bool * mask) {
  11195. UNUSED(mask);
  11196. return true;
  11197. }
  11198. static bool ggml_thread_apply_priority(int32_t prio) {
  11199. UNUSED(prio);
  11200. return true;
  11201. }
  11202. #endif
  11203. static bool ggml_thread_cpumask_is_valid(const bool * mask) {
  11204. for (int i = 0; i < GGML_MAX_N_THREADS; i++) {
  11205. if (mask[i]) { return true; }
  11206. }
  11207. return false;
  11208. }
  11209. static void ggml_thread_cpumask_next(const bool * global_mask, bool * local_mask, bool strict, int32_t* iter) {
  11210. if (!strict) {
  11211. memcpy(local_mask, global_mask, GGML_MAX_N_THREADS);
  11212. return;
  11213. } else {
  11214. memset(local_mask, 0, GGML_MAX_N_THREADS);
  11215. int32_t base_idx = *iter;
  11216. for (int32_t i = 0; i < GGML_MAX_N_THREADS; i++) {
  11217. int32_t idx = base_idx + i;
  11218. if (idx >= GGML_MAX_N_THREADS) {
  11219. // Just a cheaper modulo
  11220. idx -= GGML_MAX_N_THREADS;
  11221. }
  11222. if (global_mask[idx]) {
  11223. local_mask[idx] = 1;
  11224. *iter = idx + 1;
  11225. return;
  11226. }
  11227. }
  11228. }
  11229. }
  11230. void ggml_threadpool_free(struct ggml_threadpool* threadpool) {
  11231. if (!threadpool) return;
  11232. const int n_threads = threadpool->n_threads_max;
  11233. #ifndef GGML_USE_OPENMP
  11234. struct ggml_compute_state* workers = threadpool->workers;
  11235. ggml_mutex_lock(&threadpool->mutex);
  11236. threadpool->stop = true;
  11237. threadpool->pause = false;
  11238. ggml_cond_broadcast(&threadpool->cond);
  11239. ggml_mutex_unlock(&threadpool->mutex);
  11240. for (int j = 1; j < n_threads; j++) {
  11241. int32_t rc = ggml_thread_join(workers[j].thrd, NULL);
  11242. GGML_ASSERT(rc == GGML_EXIT_SUCCESS || rc == GGML_EXIT_ABORTED);
  11243. UNUSED(rc);
  11244. }
  11245. ggml_mutex_destroy(&threadpool->mutex);
  11246. ggml_cond_destroy(&threadpool->cond);
  11247. #endif // GGML_USE_OPENMP
  11248. const size_t workers_size = sizeof(struct ggml_compute_state) * n_threads;
  11249. ggml_aligned_free(threadpool->workers, workers_size);
  11250. ggml_aligned_free(threadpool, sizeof(struct ggml_threadpool));
  11251. }
  11252. #ifndef GGML_USE_OPENMP
  11253. // pause/resume must be called under mutex
  11254. static void ggml_threadpool_pause_locked(struct ggml_threadpool * threadpool) {
  11255. GGML_PRINT_DEBUG("Pausing threadpool\n");
  11256. threadpool->pause = true;
  11257. ggml_cond_broadcast(&threadpool->cond);
  11258. }
  11259. static void ggml_threadpool_resume_locked(struct ggml_threadpool * threadpool) {
  11260. GGML_PRINT_DEBUG("Resuming threadpool\n");
  11261. threadpool->pause = false;
  11262. ggml_cond_broadcast(&threadpool->cond);
  11263. }
  11264. #endif
  11265. void ggml_threadpool_pause(struct ggml_threadpool * threadpool) {
  11266. #ifndef GGML_USE_OPENMP
  11267. ggml_mutex_lock(&threadpool->mutex);
  11268. if (!threadpool->pause) {
  11269. ggml_threadpool_pause_locked(threadpool);
  11270. }
  11271. ggml_mutex_unlock(&threadpool->mutex);
  11272. #else
  11273. UNUSED(threadpool);
  11274. #endif
  11275. }
  11276. void ggml_threadpool_resume(struct ggml_threadpool * threadpool) {
  11277. #ifndef GGML_USE_OPENMP
  11278. ggml_mutex_lock(&threadpool->mutex);
  11279. if (threadpool->pause) {
  11280. ggml_threadpool_resume_locked(threadpool);
  11281. }
  11282. ggml_mutex_unlock(&threadpool->mutex);
  11283. #else
  11284. UNUSED(threadpool);
  11285. #endif
  11286. }
  11287. struct ggml_cplan ggml_graph_plan(
  11288. const struct ggml_cgraph * cgraph,
  11289. int n_threads,
  11290. struct ggml_threadpool * threadpool) {
  11291. if (threadpool == NULL) {
  11292. //GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads);
  11293. }
  11294. if (n_threads <= 0) {
  11295. n_threads = threadpool ? threadpool->n_threads_max : GGML_DEFAULT_N_THREADS;
  11296. }
  11297. size_t work_size = 0;
  11298. struct ggml_cplan cplan;
  11299. memset(&cplan, 0, sizeof(struct ggml_cplan));
  11300. int max_tasks = 1;
  11301. // thread scheduling for the different operations + work buffer size estimation
  11302. for (int i = 0; i < cgraph->n_nodes; i++) {
  11303. struct ggml_tensor * node = cgraph->nodes[i];
  11304. const int n_tasks = ggml_get_n_tasks(node, n_threads);
  11305. max_tasks = MAX(max_tasks, n_tasks);
  11306. size_t cur = 0;
  11307. if (!ggml_cpu_extra_work_size(n_threads, node, &cur)) {
  11308. switch (node->op) {
  11309. case GGML_OP_CPY:
  11310. case GGML_OP_DUP:
  11311. {
  11312. if (ggml_is_quantized(node->type) ||
  11313. // F16 -> BF16 and BF16 -> F16 copies go through intermediate F32
  11314. (node->src[0]->type == GGML_TYPE_F16 && node->src[1] && node->src[1]->type == GGML_TYPE_BF16) ||
  11315. (node->src[0]->type == GGML_TYPE_BF16 && node->src[1] && node->src[1]->type == GGML_TYPE_F16)) {
  11316. cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
  11317. }
  11318. } break;
  11319. case GGML_OP_ADD:
  11320. case GGML_OP_ADD1:
  11321. {
  11322. if (ggml_is_quantized(node->src[0]->type)) {
  11323. cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
  11324. }
  11325. } break;
  11326. case GGML_OP_ACC:
  11327. {
  11328. if (ggml_is_quantized(node->src[0]->type)) {
  11329. cur = ggml_type_size(GGML_TYPE_F32) * node->src[1]->ne[0] * n_tasks;
  11330. }
  11331. } break;
  11332. case GGML_OP_COUNT_EQUAL:
  11333. {
  11334. cur = ggml_type_size(node->type)*n_tasks;
  11335. } break;
  11336. case GGML_OP_MUL_MAT:
  11337. {
  11338. const enum ggml_type vec_dot_type = type_traits_cpu[node->src[0]->type].vec_dot_type;
  11339. if (node->src[1]->type != vec_dot_type) {
  11340. cur = ggml_row_size(vec_dot_type, ggml_nelements(node->src[1]));
  11341. }
  11342. } break;
  11343. case GGML_OP_MUL_MAT_ID:
  11344. {
  11345. cur = 0;
  11346. const struct ggml_tensor * src0 = node->src[0];
  11347. const struct ggml_tensor * src1 = node->src[1];
  11348. const enum ggml_type vec_dot_type = type_traits_cpu[src0->type].vec_dot_type;
  11349. if (src1->type != vec_dot_type) {
  11350. cur += ggml_row_size(vec_dot_type, ggml_nelements(src1));
  11351. }
  11352. const int n_as = src0->ne[2];
  11353. cur += GGML_PAD(cur, sizeof(int64_t)); // align
  11354. cur += n_as * sizeof(int64_t); // matrix_row_counts
  11355. cur += n_as * src1->ne[2] * sizeof(int64_t); // matrix_rows
  11356. } break;
  11357. case GGML_OP_OUT_PROD:
  11358. {
  11359. if (ggml_is_quantized(node->src[0]->type)) {
  11360. cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
  11361. }
  11362. } break;
  11363. case GGML_OP_SOFT_MAX:
  11364. case GGML_OP_ROPE:
  11365. case GGML_OP_ROPE_BACK:
  11366. {
  11367. cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
  11368. } break;
  11369. case GGML_OP_CONV_TRANSPOSE_1D:
  11370. {
  11371. GGML_ASSERT(node->src[0]->ne[3] == 1);
  11372. GGML_ASSERT(node->src[1]->ne[2] == 1);
  11373. GGML_ASSERT(node->src[1]->ne[3] == 1);
  11374. const int64_t ne00 = node->src[0]->ne[0]; // K
  11375. const int64_t ne01 = node->src[0]->ne[1]; // Cout
  11376. const int64_t ne02 = node->src[0]->ne[2]; // Cin
  11377. const int64_t ne10 = node->src[1]->ne[0]; // L
  11378. const int64_t ne11 = node->src[1]->ne[1]; // Cin
  11379. if ((node->src[0]->type == GGML_TYPE_F16 ||
  11380. node->src[0]->type == GGML_TYPE_BF16) &&
  11381. node->src[1]->type == GGML_TYPE_F32) {
  11382. cur += sizeof(ggml_fp16_t)*ne00*ne01*ne02;
  11383. cur += sizeof(ggml_fp16_t)*ne10*ne11;
  11384. } else if (node->src[0]->type == GGML_TYPE_F32 &&
  11385. node->src[1]->type == GGML_TYPE_F32) {
  11386. cur += sizeof(float)*ne00*ne01*ne02;
  11387. cur += sizeof(float)*ne10*ne11;
  11388. } else {
  11389. GGML_ABORT("fatal error");
  11390. }
  11391. } break;
  11392. case GGML_OP_CONV_TRANSPOSE_2D:
  11393. {
  11394. const int64_t ne00 = node->src[0]->ne[0]; // W
  11395. const int64_t ne01 = node->src[0]->ne[1]; // H
  11396. const int64_t ne02 = node->src[0]->ne[2]; // Channels Out
  11397. const int64_t ne03 = node->src[0]->ne[3]; // Channels In
  11398. const int64_t ne10 = node->src[1]->ne[0]; // W
  11399. const int64_t ne11 = node->src[1]->ne[1]; // H
  11400. const int64_t ne12 = node->src[1]->ne[2]; // Channels In
  11401. cur += sizeof(ggml_fp16_t)*ne00*ne01*ne02*ne03;
  11402. cur += sizeof(ggml_fp16_t)*ne10*ne11*ne12;
  11403. } break;
  11404. case GGML_OP_FLASH_ATTN_EXT:
  11405. {
  11406. const int64_t ne00 = node->src[0]->ne[0]; // D
  11407. cur = 3*sizeof(float)*ne00*n_tasks; // 3x head size/thread
  11408. } break;
  11409. case GGML_OP_FLASH_ATTN_BACK:
  11410. {
  11411. const int64_t D = node->src[0]->ne[0];
  11412. const int64_t ne11 = ggml_up(node->src[1]->ne[1], GGML_SOFT_MAX_UNROLL);
  11413. const int64_t mxDn = MAX(D, ne11) * 2; // *2 because of S and SM in ggml_compute_forward_flash_attn_back
  11414. if (node->src[1]->type == GGML_TYPE_F32) {
  11415. cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
  11416. cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
  11417. } else if (node->src[1]->type == GGML_TYPE_F16) {
  11418. cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
  11419. cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
  11420. } else if (node->src[1]->type == GGML_TYPE_BF16) {
  11421. cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
  11422. cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
  11423. }
  11424. } break;
  11425. case GGML_OP_CROSS_ENTROPY_LOSS:
  11426. {
  11427. cur = ggml_type_size(node->type)*(n_tasks + node->src[0]->ne[0]*n_tasks);
  11428. } break;
  11429. case GGML_OP_COUNT:
  11430. {
  11431. GGML_ABORT("fatal error");
  11432. }
  11433. default:
  11434. break;
  11435. }
  11436. }
  11437. work_size = MAX(work_size, cur);
  11438. }
  11439. if (work_size > 0) {
  11440. work_size += CACHE_LINE_SIZE*(n_threads);
  11441. }
  11442. cplan.threadpool = threadpool;
  11443. cplan.n_threads = MIN(max_tasks, n_threads);
  11444. cplan.work_size = work_size;
  11445. cplan.work_data = NULL;
  11446. return cplan;
  11447. }
  11448. static thread_ret_t ggml_graph_compute_thread(void * data) {
  11449. struct ggml_compute_state * state = (struct ggml_compute_state *) data;
  11450. struct ggml_threadpool * tp = state->threadpool;
  11451. const struct ggml_cgraph * cgraph = tp->cgraph;
  11452. const struct ggml_cplan * cplan = tp->cplan;
  11453. set_numa_thread_affinity(state->ith);
  11454. struct ggml_compute_params params = {
  11455. /*.ith =*/ state->ith,
  11456. /*.nth =*/ atomic_load_explicit(&tp->n_threads_cur, memory_order_relaxed),
  11457. /*.wsize =*/ cplan->work_size,
  11458. /*.wdata =*/ cplan->work_data,
  11459. /*.threadpool=*/ tp,
  11460. };
  11461. for (int node_n = 0; node_n < cgraph->n_nodes && atomic_load_explicit(&tp->abort, memory_order_relaxed) != node_n; node_n++) {
  11462. struct ggml_tensor * node = cgraph->nodes[node_n];
  11463. ggml_compute_forward(&params, node);
  11464. if (state->ith == 0 && cplan->abort_callback &&
  11465. cplan->abort_callback(cplan->abort_callback_data)) {
  11466. atomic_store_explicit(&tp->abort, node_n + 1, memory_order_relaxed);
  11467. tp->ec = GGML_STATUS_ABORTED;
  11468. }
  11469. if (node_n + 1 < cgraph->n_nodes) {
  11470. ggml_barrier(state->threadpool);
  11471. }
  11472. }
  11473. ggml_barrier(state->threadpool);
  11474. return 0;
  11475. }
  11476. #ifndef GGML_USE_OPENMP
  11477. // check if thread is active
  11478. static inline bool ggml_graph_compute_thread_active(struct ggml_compute_state * state) {
  11479. struct ggml_threadpool * threadpool = state->threadpool;
  11480. int n_threads = atomic_load_explicit(&threadpool->n_threads_cur, memory_order_relaxed);
  11481. return (state->ith < n_threads);
  11482. }
  11483. // check if thread is ready to proceed (exit from polling or sleeping)
  11484. static inline bool ggml_graph_compute_thread_ready(struct ggml_compute_state * state) {
  11485. struct ggml_threadpool * threadpool = state->threadpool;
  11486. if (state->pending || threadpool->stop || threadpool->pause) { return true; }
  11487. // check for new graph/work
  11488. int new_graph = atomic_load_explicit(&threadpool->n_graph, memory_order_relaxed);
  11489. if (new_graph != state->last_graph) {
  11490. state->pending = ggml_graph_compute_thread_active(state);
  11491. state->last_graph = new_graph;
  11492. }
  11493. return state->pending;
  11494. }
  11495. // sync thread state after polling
  11496. static inline void ggml_graph_compute_thread_sync(struct ggml_compute_state * state) {
  11497. // TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
  11498. #ifdef GGML_TSAN_ENABLED
  11499. atomic_fetch_add_explicit(&state->threadpool->n_graph, 0, memory_order_seq_cst);
  11500. #else
  11501. atomic_thread_fence(memory_order_seq_cst);
  11502. #endif
  11503. UNUSED(state);
  11504. }
  11505. static inline bool ggml_graph_compute_poll_for_work(struct ggml_compute_state * state) {
  11506. struct ggml_threadpool * threadpool = state->threadpool;
  11507. // Skip polling for unused threads
  11508. if (!ggml_graph_compute_thread_active(state)) {
  11509. return state->pending;
  11510. }
  11511. // This seems to make 0 ... 100 a decent range for polling level across modern processors.
  11512. // Perhaps, we can adjust it dynamically based on load and things.
  11513. const uint64_t n_rounds = 1024UL * 128 * threadpool->poll;
  11514. for (uint64_t i=0; !ggml_graph_compute_thread_ready(state) && i < n_rounds; i++) {
  11515. // No new work. Keep polling.
  11516. ggml_thread_cpu_relax();
  11517. }
  11518. return state->pending;
  11519. }
  11520. static inline bool ggml_graph_compute_check_for_work(struct ggml_compute_state * state) {
  11521. struct ggml_threadpool * threadpool = state->threadpool;
  11522. if (ggml_graph_compute_poll_for_work(state)) {
  11523. ggml_graph_compute_thread_sync(state);
  11524. return state->pending;
  11525. }
  11526. ggml_mutex_lock_shared(&threadpool->mutex);
  11527. while (!ggml_graph_compute_thread_ready(state)) {
  11528. // No new work. Wait for the signal.
  11529. GGML_PRINT_DEBUG("thread #%d waiting for work (sleeping)\n", state->ith);
  11530. ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
  11531. }
  11532. ggml_mutex_unlock_shared(&threadpool->mutex);
  11533. return state->pending;
  11534. }
  11535. static thread_ret_t ggml_graph_compute_secondary_thread(void* data) {
  11536. struct ggml_compute_state * state = (struct ggml_compute_state *) data;
  11537. struct ggml_threadpool * threadpool = state->threadpool;
  11538. ggml_thread_apply_priority(threadpool->prio);
  11539. if (ggml_thread_cpumask_is_valid(state->cpumask)) {
  11540. ggml_thread_apply_affinity(state->cpumask);
  11541. }
  11542. while (true) {
  11543. // Check if we need to sleep
  11544. while (threadpool->pause) {
  11545. GGML_PRINT_DEBUG("thread #%d inside pause loop\n", state->ith);
  11546. ggml_mutex_lock_shared(&threadpool->mutex);
  11547. if (threadpool->pause) {
  11548. ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
  11549. }
  11550. GGML_PRINT_DEBUG("thread #%d resuming after wait\n", state->ith);
  11551. ggml_mutex_unlock_shared(&threadpool->mutex);
  11552. }
  11553. // This needs to be checked for after the cond_wait
  11554. if (threadpool->stop) break;
  11555. // Check if there is new work
  11556. // The main thread is the only one that can dispatch new work
  11557. ggml_graph_compute_check_for_work(state);
  11558. if (state->pending) {
  11559. state->pending = false;
  11560. ggml_graph_compute_thread(state);
  11561. }
  11562. }
  11563. return (thread_ret_t) 0;
  11564. }
  11565. // Start processing new graph
  11566. static void ggml_graph_compute_kickoff(struct ggml_threadpool * threadpool, int n_threads)
  11567. {
  11568. // Always take the mutex here because the worker threads are doing hybrid poll/wait
  11569. ggml_mutex_lock(&threadpool->mutex);
  11570. GGML_PRINT_DEBUG("threadpool: n_threads_cur %d n_threads %d\n", threadpool->n_threads_cur, n_threads);
  11571. // Update the number of active threads
  11572. atomic_store_explicit(&threadpool->n_threads_cur, n_threads, memory_order_relaxed);
  11573. // Indicate the graph is ready to be processed
  11574. // We need the full seq-cst fence here because of the polling threads (used in thread_sync)
  11575. atomic_fetch_add_explicit(&threadpool->n_graph, 1, memory_order_seq_cst);
  11576. if (threadpool->pause) {
  11577. // Update main thread prio and affinity to match the threadpool settings
  11578. ggml_thread_apply_priority(threadpool->prio);
  11579. if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
  11580. ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
  11581. }
  11582. // resume does cond broadcast
  11583. ggml_threadpool_resume_locked(threadpool);
  11584. } else {
  11585. ggml_cond_broadcast(&threadpool->cond);
  11586. }
  11587. ggml_mutex_unlock(&threadpool->mutex);
  11588. }
  11589. #endif // GGML_USE_OPENMP
  11590. static struct ggml_threadpool * ggml_threadpool_new_impl(
  11591. struct ggml_threadpool_params * tpp,
  11592. struct ggml_cgraph * cgraph,
  11593. struct ggml_cplan * cplan) {
  11594. struct ggml_threadpool * threadpool =
  11595. ggml_aligned_malloc(sizeof(struct ggml_threadpool));
  11596. {
  11597. threadpool->cgraph = cgraph;
  11598. threadpool->cplan = cplan;
  11599. threadpool->n_graph = 0;
  11600. threadpool->n_barrier = 0;
  11601. threadpool->n_barrier_passed = 0;
  11602. threadpool->current_chunk = 0;
  11603. threadpool->stop = false;
  11604. threadpool->pause = tpp->paused;
  11605. threadpool->abort = -1;
  11606. threadpool->workers = NULL;
  11607. threadpool->n_threads_max = tpp->n_threads;
  11608. threadpool->n_threads_cur = tpp->n_threads;
  11609. threadpool->poll = tpp->poll;
  11610. threadpool->prio = tpp->prio;
  11611. threadpool->ec = GGML_STATUS_SUCCESS;
  11612. }
  11613. // Allocate and init workers state
  11614. const size_t workers_size = sizeof(struct ggml_compute_state) * tpp->n_threads;
  11615. struct ggml_compute_state * workers = ggml_aligned_malloc(workers_size);
  11616. memset(workers, 0, workers_size);
  11617. for (int j = 0; j < tpp->n_threads; j++) {
  11618. workers[j].threadpool = threadpool;
  11619. workers[j].ith = j;
  11620. }
  11621. threadpool->workers = workers;
  11622. #ifndef GGML_USE_OPENMP
  11623. ggml_mutex_init(&threadpool->mutex);
  11624. ggml_cond_init(&threadpool->cond);
  11625. // Spin the threads for all workers, and update CPU placements.
  11626. // Place the main thread last (towards the higher numbered CPU cores).
  11627. int32_t cpumask_iter = 0;
  11628. for (int j = 1; j < tpp->n_threads; j++) {
  11629. ggml_thread_cpumask_next(tpp->cpumask, workers[j].cpumask, tpp->strict_cpu, &cpumask_iter);
  11630. int32_t rc = ggml_thread_create(&workers[j].thrd, NULL, ggml_graph_compute_secondary_thread, &workers[j]);
  11631. GGML_ASSERT(rc == 0);
  11632. }
  11633. ggml_thread_cpumask_next(tpp->cpumask, workers[0].cpumask, tpp->strict_cpu, &cpumask_iter);
  11634. if (!threadpool->pause) {
  11635. // Update main thread prio and affinity at the start, otherwise we'll do it in resume
  11636. ggml_thread_apply_priority(threadpool->prio);
  11637. if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
  11638. ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
  11639. }
  11640. }
  11641. #endif // GGML_USE_OPENMP
  11642. return threadpool;
  11643. }
  11644. struct ggml_threadpool * ggml_threadpool_new(struct ggml_threadpool_params * tpp) {
  11645. return ggml_threadpool_new_impl(tpp, NULL, NULL);
  11646. }
  11647. enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan) {
  11648. ggml_cpu_init();
  11649. GGML_ASSERT(cplan);
  11650. GGML_ASSERT(cplan->n_threads > 0);
  11651. GGML_ASSERT(cplan->work_size == 0 || cplan->work_data != NULL);
  11652. int n_threads = cplan->n_threads;
  11653. struct ggml_threadpool * threadpool = cplan->threadpool;
  11654. bool disposable_threadpool = false;
  11655. if (threadpool == NULL) {
  11656. //GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads);
  11657. disposable_threadpool = true;
  11658. struct ggml_threadpool_params ttp = ggml_threadpool_params_default(n_threads);
  11659. threadpool = ggml_threadpool_new_impl(&ttp, cgraph, cplan);
  11660. } else {
  11661. // Reset some of the parameters that need resetting
  11662. // No worker threads should be accessing the parameters below at this stage
  11663. threadpool->cgraph = cgraph;
  11664. threadpool->cplan = cplan;
  11665. threadpool->current_chunk = 0;
  11666. threadpool->abort = -1;
  11667. threadpool->ec = GGML_STATUS_SUCCESS;
  11668. }
  11669. #ifdef GGML_USE_OPENMP
  11670. if (n_threads > 1) {
  11671. #pragma omp parallel num_threads(n_threads)
  11672. {
  11673. #pragma omp single
  11674. {
  11675. // update the number of threads from the actual number of threads that we got from OpenMP
  11676. n_threads = omp_get_num_threads();
  11677. atomic_store_explicit(&threadpool->n_threads_cur, n_threads, memory_order_relaxed);
  11678. }
  11679. ggml_graph_compute_thread(&threadpool->workers[omp_get_thread_num()]);
  11680. }
  11681. } else {
  11682. atomic_store_explicit(&threadpool->n_threads_cur, 1, memory_order_relaxed);
  11683. ggml_graph_compute_thread(&threadpool->workers[0]);
  11684. }
  11685. #else
  11686. if (n_threads > threadpool->n_threads_max) {
  11687. GGML_LOG_WARN("cplan requested more threads (%d) than available (%d)\n", n_threads, threadpool->n_threads_max);
  11688. n_threads = threadpool->n_threads_max;
  11689. }
  11690. // Kick all threads to start the new graph
  11691. ggml_graph_compute_kickoff(threadpool, n_threads);
  11692. // This is a work thread too
  11693. ggml_graph_compute_thread(&threadpool->workers[0]);
  11694. #endif
  11695. // don't leave affinity set on the main thread
  11696. clear_numa_thread_affinity();
  11697. enum ggml_status ret = threadpool->ec;
  11698. if (disposable_threadpool) {
  11699. ggml_threadpool_free(threadpool);
  11700. }
  11701. return ret;
  11702. }
  11703. enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads) {
  11704. struct ggml_cplan cplan = ggml_graph_plan(cgraph, n_threads, NULL);
  11705. cplan.work_data = (uint8_t *)ggml_new_buffer(ctx, cplan.work_size);
  11706. return ggml_graph_compute(cgraph, &cplan);
  11707. }
  11708. int ggml_cpu_has_avx(void) {
  11709. #if defined(__AVX__)
  11710. return 1;
  11711. #else
  11712. return 0;
  11713. #endif
  11714. }
  11715. int ggml_cpu_has_avx_vnni(void) {
  11716. #if defined(__AVXVNNI__)
  11717. return 1;
  11718. #else
  11719. return 0;
  11720. #endif
  11721. }
  11722. int ggml_cpu_has_avx2(void) {
  11723. #if defined(__AVX2__)
  11724. return 1;
  11725. #else
  11726. return 0;
  11727. #endif
  11728. }
  11729. int ggml_cpu_has_avx512(void) {
  11730. #if defined(__AVX512F__)
  11731. return 1;
  11732. #else
  11733. return 0;
  11734. #endif
  11735. }
  11736. int ggml_cpu_has_avx512_vbmi(void) {
  11737. #if defined(__AVX512VBMI__)
  11738. return 1;
  11739. #else
  11740. return 0;
  11741. #endif
  11742. }
  11743. int ggml_cpu_has_avx512_vnni(void) {
  11744. #if defined(__AVX512VNNI__)
  11745. return 1;
  11746. #else
  11747. return 0;
  11748. #endif
  11749. }
  11750. int ggml_cpu_has_avx512_bf16(void) {
  11751. #if defined(__AVX512BF16__)
  11752. return 1;
  11753. #else
  11754. return 0;
  11755. #endif
  11756. }
  11757. int ggml_cpu_has_amx_int8(void) {
  11758. #if defined(__AMX_INT8__)
  11759. return 1;
  11760. #else
  11761. return 0;
  11762. #endif
  11763. }
  11764. int ggml_cpu_has_fma(void) {
  11765. #if defined(__FMA__)
  11766. return 1;
  11767. #else
  11768. return 0;
  11769. #endif
  11770. }
  11771. int ggml_cpu_has_arm_fma(void) {
  11772. #if defined(__ARM_FEATURE_FMA)
  11773. return 1;
  11774. #else
  11775. return 0;
  11776. #endif
  11777. }
  11778. int ggml_cpu_has_riscv_v(void) {
  11779. #if defined(__riscv_v_intrinsic)
  11780. return 1;
  11781. #else
  11782. return 0;
  11783. #endif
  11784. }
  11785. int ggml_cpu_has_f16c(void) {
  11786. #if defined(__F16C__)
  11787. return 1;
  11788. #else
  11789. return 0;
  11790. #endif
  11791. }
  11792. int ggml_cpu_has_fp16_va(void) {
  11793. #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
  11794. return 1;
  11795. #else
  11796. return 0;
  11797. #endif
  11798. }
  11799. int ggml_cpu_has_wasm_simd(void) {
  11800. #if defined(__wasm_simd128__)
  11801. return 1;
  11802. #else
  11803. return 0;
  11804. #endif
  11805. }
  11806. int ggml_cpu_has_llamafile(void) {
  11807. #if defined(GGML_USE_LLAMAFILE)
  11808. return 1;
  11809. #else
  11810. return 0;
  11811. #endif
  11812. }
  11813. int ggml_cpu_has_sse3(void) {
  11814. #if defined(__SSE3__)
  11815. return 1;
  11816. #else
  11817. return 0;
  11818. #endif
  11819. }
  11820. int ggml_cpu_has_ssse3(void) {
  11821. #if defined(__SSSE3__)
  11822. return 1;
  11823. #else
  11824. return 0;
  11825. #endif
  11826. }
  11827. int ggml_cpu_has_vsx(void) {
  11828. #if defined(__POWER9_VECTOR__)
  11829. return 1;
  11830. #else
  11831. return 0;
  11832. #endif
  11833. }
  11834. int ggml_cpu_has_neon(void) {
  11835. #if defined(__ARM_ARCH) && defined(__ARM_NEON)
  11836. return ggml_arm_arch_features.has_neon;
  11837. #else
  11838. return 0;
  11839. #endif
  11840. }
  11841. int ggml_cpu_has_dotprod(void) {
  11842. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_DOTPROD)
  11843. return ggml_arm_arch_features.has_dotprod;
  11844. #else
  11845. return 0;
  11846. #endif
  11847. }
  11848. int ggml_cpu_has_sve(void) {
  11849. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SVE)
  11850. return ggml_arm_arch_features.has_sve;
  11851. #else
  11852. return 0;
  11853. #endif
  11854. }
  11855. int ggml_cpu_has_matmul_int8(void) {
  11856. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_MATMUL_INT8)
  11857. return ggml_arm_arch_features.has_i8mm;
  11858. #else
  11859. return 0;
  11860. #endif
  11861. }
  11862. int ggml_cpu_get_sve_cnt(void) {
  11863. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SVE)
  11864. return ggml_arm_arch_features.sve_cnt;
  11865. #else
  11866. return 0;
  11867. #endif
  11868. }
  11869. void ggml_cpu_init(void) {
  11870. // needed to initialize f16 tables
  11871. {
  11872. struct ggml_init_params params = { 0, NULL, false };
  11873. struct ggml_context * ctx = ggml_init(params);
  11874. ggml_free(ctx);
  11875. }
  11876. ggml_critical_section_start();
  11877. static bool is_first_call = true;
  11878. if (is_first_call) {
  11879. // initialize GELU, Quick GELU, SILU and EXP F32 tables
  11880. {
  11881. const uint64_t t_start = ggml_time_us(); UNUSED(t_start);
  11882. for (int i = 0; i < (1 << 16); ++i) {
  11883. union {
  11884. uint16_t u16;
  11885. ggml_fp16_t fp16;
  11886. } u = {i};
  11887. float f = GGML_FP16_TO_FP32(u.fp16);
  11888. ggml_table_gelu_f16[i] = GGML_FP32_TO_FP16(ggml_gelu_f32(f));
  11889. ggml_table_gelu_quick_f16[i] = GGML_FP32_TO_FP16(ggml_gelu_quick_f32(f));
  11890. }
  11891. const uint64_t t_end = ggml_time_us(); UNUSED(t_end);
  11892. GGML_PRINT_DEBUG("%s: GELU, Quick GELU, SILU and EXP tables initialized in %f ms\n", __func__, (t_end - t_start)/1000.0);
  11893. }
  11894. #if defined(__ARM_ARCH)
  11895. ggml_init_arm_arch_features();
  11896. #endif
  11897. is_first_call = false;
  11898. }
  11899. ggml_critical_section_end();
  11900. }