ggml-cpu.c 471 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207
  1. /**
  2. * llama.cpp - commit 081b29bd2a3d91e7772e3910ce223dd63b8d7d26 - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #define _CRT_SECURE_NO_DEPRECATE // Disables "unsafe" warnings on Windows
  27. #define _USE_MATH_DEFINES // For M_PI on MSVC
  28. #include "ggml-backend-impl.h"
  29. #include "ggml-backend.h"
  30. #include "ggml-cpu-traits.h"
  31. #include "ggml-cpu-impl.h"
  32. #include "ggml-cpu.h"
  33. #include "ggml-impl.h"
  34. #include "ggml-quants.h"
  35. #include "ggml-cpu-quants.h"
  36. #include "ggml-threading.h"
  37. #include "amx.h"
  38. #include "ggml.h"
  39. #if defined(_MSC_VER) || defined(__MINGW32__)
  40. #include <malloc.h> // using malloc.h with MSC/MINGW
  41. #elif !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
  42. #include <alloca.h>
  43. #endif
  44. #include <assert.h>
  45. #include <errno.h>
  46. #include <time.h>
  47. #include <math.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <stdint.h>
  51. #include <inttypes.h>
  52. #include <stdio.h>
  53. #include <float.h>
  54. #include <limits.h>
  55. #include <stdarg.h>
  56. #include <signal.h>
  57. #if defined(__gnu_linux__)
  58. #include <syscall.h>
  59. #endif
  60. #ifdef GGML_USE_OPENMP
  61. #include <omp.h>
  62. #endif
  63. #if defined(__ARM_FEATURE_SVE) || defined(__ARM_FEATURE_MATMUL_INT8)
  64. #undef GGML_USE_LLAMAFILE
  65. #endif
  66. #ifdef GGML_USE_LLAMAFILE
  67. #include "llamafile/sgemm.h"
  68. #endif
  69. #if defined(_MSC_VER)
  70. // disable "possible loss of data" to avoid hundreds of casts
  71. // we should just be careful :)
  72. #pragma warning(disable: 4244 4267)
  73. // disable POSIX deprecation warnings
  74. // these functions are never going away, anyway
  75. #pragma warning(disable: 4996)
  76. // unreachable code because of multiple instances of code after GGML_ABORT
  77. #pragma warning(disable: 4702)
  78. #endif
  79. // Note: once we move threading into a separate C++ file
  80. // will use std::hardware_destructive_interference_size instead of hardcoding it here
  81. // and we'll use C++ attribute syntax.
  82. #define GGML_CACHE_LINE 64
  83. #if defined(__clang__) || defined(__GNUC__)
  84. #define GGML_CACHE_ALIGN __attribute__((aligned(GGML_CACHE_LINE)))
  85. #endif
  86. #if defined(__has_feature)
  87. #if __has_feature(thread_sanitizer)
  88. #define GGML_TSAN_ENABLED 1
  89. #endif
  90. #else // __has_feature
  91. #if defined(__SANITIZE_THREAD__)
  92. #define GGML_TSAN_ENABLED 1
  93. #endif
  94. #endif // __has_feature
  95. #define UNUSED GGML_UNUSED
  96. #define SWAP(x, y, T) do { T SWAP = x; (x) = y; (y) = SWAP; } while (0)
  97. #if defined(GGML_USE_ACCELERATE)
  98. #include <Accelerate/Accelerate.h>
  99. #endif
  100. // floating point type used to accumulate sums
  101. typedef double ggml_float;
  102. #define GGML_GELU_FP16
  103. #define GGML_GELU_QUICK_FP16
  104. #define GGML_SOFT_MAX_UNROLL 4
  105. #define GGML_VEC_DOT_UNROLL 2
  106. #define GGML_VEC_MAD_UNROLL 32
  107. //
  108. // global data
  109. //
  110. // precomputed gelu table for f16 (128 KB)
  111. static ggml_fp16_t ggml_table_gelu_f16[1 << 16];
  112. // precomputed quick gelu table for f16 (128 KB)
  113. static ggml_fp16_t ggml_table_gelu_quick_f16[1 << 16];
  114. #if defined(__ARM_ARCH)
  115. struct ggml_arm_arch_features_type {
  116. int has_neon;
  117. int has_dotprod;
  118. int has_i8mm;
  119. int has_sve;
  120. int sve_cnt;
  121. } ggml_arm_arch_features = {-1, -1, -1, -1, 0};
  122. #endif
  123. #if defined(_WIN32)
  124. #define WIN32_LEAN_AND_MEAN
  125. #ifndef NOMINMAX
  126. #define NOMINMAX
  127. #endif
  128. #include <windows.h>
  129. #if defined(_MSC_VER) && !defined(__clang__)
  130. #define GGML_CACHE_ALIGN __declspec(align(GGML_CACHE_LINE))
  131. typedef volatile LONG atomic_int;
  132. typedef atomic_int atomic_bool;
  133. typedef atomic_int atomic_flag;
  134. #define ATOMIC_FLAG_INIT 0
  135. typedef enum {
  136. memory_order_relaxed,
  137. memory_order_consume,
  138. memory_order_acquire,
  139. memory_order_release,
  140. memory_order_acq_rel,
  141. memory_order_seq_cst
  142. } memory_order;
  143. static void atomic_store(atomic_int * ptr, LONG val) {
  144. InterlockedExchange(ptr, val);
  145. }
  146. static void atomic_store_explicit(atomic_int * ptr, LONG val, memory_order mo) {
  147. // TODO: add support for explicit memory order
  148. InterlockedExchange(ptr, val);
  149. }
  150. static LONG atomic_load(atomic_int * ptr) {
  151. return InterlockedCompareExchange(ptr, 0, 0);
  152. }
  153. static LONG atomic_load_explicit(atomic_int * ptr, memory_order mo) {
  154. // TODO: add support for explicit memory order
  155. return InterlockedCompareExchange(ptr, 0, 0);
  156. }
  157. static LONG atomic_fetch_add(atomic_int * ptr, LONG inc) {
  158. return InterlockedExchangeAdd(ptr, inc);
  159. }
  160. static LONG atomic_fetch_add_explicit(atomic_int * ptr, LONG inc, memory_order mo) {
  161. // TODO: add support for explicit memory order
  162. return InterlockedExchangeAdd(ptr, inc);
  163. }
  164. static atomic_bool atomic_flag_test_and_set(atomic_flag * ptr) {
  165. return InterlockedExchange(ptr, 1);
  166. }
  167. static void atomic_flag_clear(atomic_flag * ptr) {
  168. InterlockedExchange(ptr, 0);
  169. }
  170. static void atomic_thread_fence(memory_order mo) {
  171. MemoryBarrier();
  172. }
  173. #else // clang
  174. #include <stdatomic.h>
  175. #endif
  176. typedef HANDLE pthread_t;
  177. typedef DWORD thread_ret_t;
  178. static int pthread_create(pthread_t * out, void * unused, thread_ret_t(*func)(void *), void * arg) {
  179. (void) unused;
  180. HANDLE handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL);
  181. if (handle == NULL)
  182. {
  183. return EAGAIN;
  184. }
  185. *out = handle;
  186. return 0;
  187. }
  188. static int pthread_join(pthread_t thread, void * unused) {
  189. (void) unused;
  190. int ret = (int) WaitForSingleObject(thread, INFINITE);
  191. CloseHandle(thread);
  192. return ret;
  193. }
  194. static int sched_yield (void) {
  195. Sleep (0);
  196. return 0;
  197. }
  198. #else
  199. #include <pthread.h>
  200. #include <stdatomic.h>
  201. #include <sched.h>
  202. #if defined(__FreeBSD__)
  203. #include <pthread_np.h>
  204. #endif
  205. typedef void * thread_ret_t;
  206. #include <sys/types.h>
  207. #include <sys/stat.h>
  208. #include <unistd.h>
  209. #endif
  210. typedef pthread_t ggml_thread_t;
  211. #if defined(__APPLE__)
  212. #include <unistd.h>
  213. #include <mach/mach.h>
  214. #include <TargetConditionals.h>
  215. #endif
  216. //
  217. // cache line
  218. //
  219. #if defined(__cpp_lib_hardware_interference_size)
  220. #define CACHE_LINE_SIZE hardware_destructive_interference_size
  221. #else
  222. #if defined(__POWER9_VECTOR__)
  223. #define CACHE_LINE_SIZE 128
  224. #else
  225. #define CACHE_LINE_SIZE 64
  226. #endif
  227. #endif
  228. static const size_t CACHE_LINE_SIZE_F32 = CACHE_LINE_SIZE/sizeof(float);
  229. 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);
  230. 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);
  231. 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);
  232. static const struct ggml_type_traits_cpu type_traits_cpu[GGML_TYPE_COUNT] = {
  233. [GGML_TYPE_F32] = {
  234. .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f32,
  235. .vec_dot_type = GGML_TYPE_F32,
  236. .nrows = 1,
  237. },
  238. [GGML_TYPE_F16] = {
  239. .from_float = (ggml_from_float_t) ggml_fp32_to_fp16_row,
  240. .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f16,
  241. .vec_dot_type = GGML_TYPE_F16,
  242. .nrows = 1,
  243. },
  244. [GGML_TYPE_Q4_0] = {
  245. .from_float = quantize_row_q4_0,
  246. .vec_dot = ggml_vec_dot_q4_0_q8_0,
  247. .vec_dot_type = GGML_TYPE_Q8_0,
  248. #if defined (__ARM_FEATURE_MATMUL_INT8)
  249. .nrows = 2,
  250. #else
  251. .nrows = 1,
  252. #endif
  253. },
  254. [GGML_TYPE_Q4_1] = {
  255. .from_float = quantize_row_q4_1,
  256. .vec_dot = ggml_vec_dot_q4_1_q8_1,
  257. .vec_dot_type = GGML_TYPE_Q8_1,
  258. #if defined (__ARM_FEATURE_MATMUL_INT8)
  259. .nrows = 2,
  260. #else
  261. .nrows = 1,
  262. #endif
  263. },
  264. [GGML_TYPE_Q5_0] = {
  265. .from_float = quantize_row_q5_0,
  266. .vec_dot = ggml_vec_dot_q5_0_q8_0,
  267. .vec_dot_type = GGML_TYPE_Q8_0,
  268. .nrows = 1,
  269. },
  270. [GGML_TYPE_Q5_1] = {
  271. .from_float = quantize_row_q5_1,
  272. .vec_dot = ggml_vec_dot_q5_1_q8_1,
  273. .vec_dot_type = GGML_TYPE_Q8_1,
  274. .nrows = 1,
  275. },
  276. [GGML_TYPE_Q8_0] = {
  277. .from_float = quantize_row_q8_0,
  278. .vec_dot = ggml_vec_dot_q8_0_q8_0,
  279. .vec_dot_type = GGML_TYPE_Q8_0,
  280. #if defined (__ARM_FEATURE_MATMUL_INT8)
  281. .nrows = 2,
  282. #else
  283. .nrows = 1,
  284. #endif
  285. },
  286. [GGML_TYPE_Q8_1] = {
  287. .from_float = quantize_row_q8_1,
  288. .vec_dot_type = GGML_TYPE_Q8_1,
  289. .nrows = 1,
  290. },
  291. [GGML_TYPE_Q2_K] = {
  292. .from_float = quantize_row_q2_K,
  293. .vec_dot = ggml_vec_dot_q2_K_q8_K,
  294. .vec_dot_type = GGML_TYPE_Q8_K,
  295. .nrows = 1,
  296. },
  297. [GGML_TYPE_Q3_K] = {
  298. .from_float = quantize_row_q3_K,
  299. .vec_dot = ggml_vec_dot_q3_K_q8_K,
  300. .vec_dot_type = GGML_TYPE_Q8_K,
  301. .nrows = 1,
  302. },
  303. [GGML_TYPE_Q4_K] = {
  304. .from_float = quantize_row_q4_K,
  305. .vec_dot = ggml_vec_dot_q4_K_q8_K,
  306. .vec_dot_type = GGML_TYPE_Q8_K,
  307. .nrows = 1,
  308. },
  309. [GGML_TYPE_Q5_K] = {
  310. .from_float = quantize_row_q5_K,
  311. .vec_dot = ggml_vec_dot_q5_K_q8_K,
  312. .vec_dot_type = GGML_TYPE_Q8_K,
  313. .nrows = 1,
  314. },
  315. [GGML_TYPE_Q6_K] = {
  316. .from_float = quantize_row_q6_K,
  317. .vec_dot = ggml_vec_dot_q6_K_q8_K,
  318. .vec_dot_type = GGML_TYPE_Q8_K,
  319. .nrows = 1,
  320. },
  321. [GGML_TYPE_IQ2_XXS] = {
  322. .from_float = NULL,
  323. .vec_dot = ggml_vec_dot_iq2_xxs_q8_K,
  324. .vec_dot_type = GGML_TYPE_Q8_K,
  325. .nrows = 1,
  326. },
  327. [GGML_TYPE_IQ2_XS] = {
  328. .from_float = NULL,
  329. .vec_dot = ggml_vec_dot_iq2_xs_q8_K,
  330. .vec_dot_type = GGML_TYPE_Q8_K,
  331. .nrows = 1,
  332. },
  333. [GGML_TYPE_IQ3_XXS] = {
  334. // NOTE: from_float for iq3 and iq2_s was removed because these quants require initialization in ggml_quantize_init
  335. //.from_float = quantize_row_iq3_xxs,
  336. .vec_dot = ggml_vec_dot_iq3_xxs_q8_K,
  337. .vec_dot_type = GGML_TYPE_Q8_K,
  338. .nrows = 1,
  339. },
  340. [GGML_TYPE_IQ3_S] = {
  341. //.from_float = quantize_row_iq3_s,
  342. .vec_dot = ggml_vec_dot_iq3_s_q8_K,
  343. .vec_dot_type = GGML_TYPE_Q8_K,
  344. .nrows = 1,
  345. },
  346. [GGML_TYPE_IQ2_S] = {
  347. //.from_float = quantize_row_iq2_s,
  348. .vec_dot = ggml_vec_dot_iq2_s_q8_K,
  349. .vec_dot_type = GGML_TYPE_Q8_K,
  350. .nrows = 1,
  351. },
  352. [GGML_TYPE_IQ1_S] = {
  353. .from_float = NULL,
  354. .vec_dot = ggml_vec_dot_iq1_s_q8_K,
  355. .vec_dot_type = GGML_TYPE_Q8_K,
  356. .nrows = 1,
  357. },
  358. [GGML_TYPE_IQ1_M] = {
  359. .from_float = NULL,
  360. .vec_dot = ggml_vec_dot_iq1_m_q8_K,
  361. .vec_dot_type = GGML_TYPE_Q8_K,
  362. .nrows = 1,
  363. },
  364. [GGML_TYPE_IQ4_NL] = {
  365. .from_float = quantize_row_iq4_nl,
  366. .vec_dot = ggml_vec_dot_iq4_nl_q8_0,
  367. .vec_dot_type = GGML_TYPE_Q8_0,
  368. .nrows = 1,
  369. },
  370. [GGML_TYPE_IQ4_XS] = {
  371. .from_float = quantize_row_iq4_xs,
  372. .vec_dot = ggml_vec_dot_iq4_xs_q8_K,
  373. .vec_dot_type = GGML_TYPE_Q8_K,
  374. .nrows = 1,
  375. },
  376. [GGML_TYPE_Q8_K] = {
  377. .from_float = quantize_row_q8_K,
  378. },
  379. [GGML_TYPE_BF16] = {
  380. .from_float = (ggml_from_float_t) ggml_fp32_to_bf16_row,
  381. .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_bf16,
  382. .vec_dot_type = GGML_TYPE_BF16,
  383. .nrows = 1,
  384. },
  385. [GGML_TYPE_TQ1_0] = {
  386. .from_float = quantize_row_tq1_0,
  387. .vec_dot = ggml_vec_dot_tq1_0_q8_K,
  388. .vec_dot_type = GGML_TYPE_Q8_K,
  389. .nrows = 1,
  390. },
  391. [GGML_TYPE_TQ2_0] = {
  392. .from_float = quantize_row_tq2_0,
  393. .vec_dot = ggml_vec_dot_tq2_0_q8_K,
  394. .vec_dot_type = GGML_TYPE_Q8_K,
  395. .nrows = 1,
  396. },
  397. };
  398. const struct ggml_type_traits_cpu * ggml_get_type_traits_cpu(enum ggml_type type) {
  399. return &type_traits_cpu[type];
  400. }
  401. //
  402. // simd mappings
  403. //
  404. // we define a common set of C macros which map to specific intrinsics based on the current architecture
  405. // we then implement the fundamental computation operations below using only these macros
  406. // adding support for new architectures requires to define the corresponding SIMD macros
  407. //
  408. // GGML_F32_STEP / GGML_F16_STEP
  409. // number of elements to process in a single step
  410. //
  411. // GGML_F32_EPR / GGML_F16_EPR
  412. // number of elements to fit in a single register
  413. //
  414. #if defined(__ARM_NEON) && defined(__ARM_FEATURE_FMA)
  415. #define GGML_SIMD
  416. // F32 NEON
  417. #define GGML_F32_STEP 16
  418. #define GGML_F32_EPR 4
  419. #define GGML_F32x4 float32x4_t
  420. #define GGML_F32x4_ZERO vdupq_n_f32(0.0f)
  421. #define GGML_F32x4_SET1(x) vdupq_n_f32(x)
  422. #define GGML_F32x4_LOAD vld1q_f32
  423. #define GGML_F32x4_STORE vst1q_f32
  424. #define GGML_F32x4_FMA(a, b, c) vfmaq_f32(a, b, c)
  425. #define GGML_F32x4_ADD vaddq_f32
  426. #define GGML_F32x4_MUL vmulq_f32
  427. #define GGML_F32x4_REDUCE_ONE(x) vaddvq_f32(x)
  428. #define GGML_F32x4_REDUCE(res, x) \
  429. { \
  430. int offset = GGML_F32_ARR >> 1; \
  431. for (int i = 0; i < offset; ++i) { \
  432. (x)[i] = vaddq_f32((x)[i], (x)[offset+i]); \
  433. } \
  434. offset >>= 1; \
  435. for (int i = 0; i < offset; ++i) { \
  436. (x)[i] = vaddq_f32((x)[i], (x)[offset+i]); \
  437. } \
  438. offset >>= 1; \
  439. for (int i = 0; i < offset; ++i) { \
  440. (x)[i] = vaddq_f32((x)[i], (x)[offset+i]); \
  441. } \
  442. (res) = (ggml_float) GGML_F32x4_REDUCE_ONE((x)[0]); \
  443. }
  444. #define GGML_F32_VEC GGML_F32x4
  445. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  446. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  447. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  448. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  449. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  450. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  451. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  452. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  453. // F16 NEON
  454. #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
  455. #define GGML_F16_STEP 32
  456. #define GGML_F16_EPR 8
  457. #define GGML_F16x8 float16x8_t
  458. #define GGML_F16x8_ZERO vdupq_n_f16(0.0f)
  459. #define GGML_F16x8_SET1(x) vdupq_n_f16(x)
  460. #define GGML_F16x8_LOAD(x) vld1q_f16((const ggml_fp16_internal_t *)(x))
  461. #define GGML_F16x8_STORE vst1q_f16
  462. #define GGML_F16x8_FMA(a, b, c) vfmaq_f16(a, b, c)
  463. #define GGML_F16x8_ADD vaddq_f16
  464. #define GGML_F16x8_MUL vmulq_f16
  465. #define GGML_F16x8_REDUCE(res, x) \
  466. do { \
  467. int offset = GGML_F16_ARR >> 1; \
  468. for (int i = 0; i < offset; ++i) { \
  469. (x)[i] = vaddq_f16((x)[i], (x)[offset+i]); \
  470. } \
  471. offset >>= 1; \
  472. for (int i = 0; i < offset; ++i) { \
  473. (x)[i] = vaddq_f16((x)[i], (x)[offset+i]); \
  474. } \
  475. offset >>= 1; \
  476. for (int i = 0; i < offset; ++i) { \
  477. (x)[i] = vaddq_f16((x)[i], (x)[offset+i]); \
  478. } \
  479. const float32x4_t t0 = vcvt_f32_f16(vget_low_f16 ((x)[0])); \
  480. const float32x4_t t1 = vcvt_f32_f16(vget_high_f16((x)[0])); \
  481. (res) = (ggml_float) vaddvq_f32(vaddq_f32(t0, t1)); \
  482. } while (0)
  483. #define GGML_F16_VEC GGML_F16x8
  484. #define GGML_F16_VEC_ZERO GGML_F16x8_ZERO
  485. #define GGML_F16_VEC_SET1 GGML_F16x8_SET1
  486. #define GGML_F16_VEC_LOAD(p, i) GGML_F16x8_LOAD(p)
  487. #define GGML_F16_VEC_STORE(p, r, i) GGML_F16x8_STORE((ggml_fp16_internal_t *)(p), (r)[i])
  488. #define GGML_F16_VEC_FMA GGML_F16x8_FMA
  489. #define GGML_F16_VEC_ADD GGML_F16x8_ADD
  490. #define GGML_F16_VEC_MUL GGML_F16x8_MUL
  491. #define GGML_F16_VEC_REDUCE GGML_F16x8_REDUCE
  492. #else
  493. // if FP16 vector arithmetic is not supported, we use FP32 instead
  494. // and take advantage of the vcvt_ functions to convert to/from FP16
  495. #define GGML_F16_STEP 16
  496. #define GGML_F16_EPR 4
  497. #define GGML_F32Cx4 float32x4_t
  498. #define GGML_F32Cx4_ZERO vdupq_n_f32(0.0f)
  499. #define GGML_F32Cx4_SET1(x) vdupq_n_f32(x)
  500. #define GGML_F32Cx4_LOAD(x) vcvt_f32_f16(vld1_f16((const ggml_fp16_internal_t *)(x)))
  501. #define GGML_F32Cx4_STORE(x, y) vst1_f16(x, vcvt_f16_f32(y))
  502. #define GGML_F32Cx4_FMA(a, b, c) vfmaq_f32(a, b, c)
  503. #define GGML_F32Cx4_ADD vaddq_f32
  504. #define GGML_F32Cx4_MUL vmulq_f32
  505. #define GGML_F32Cx4_REDUCE GGML_F32x4_REDUCE
  506. #define GGML_F16_VEC GGML_F32Cx4
  507. #define GGML_F16_VEC_ZERO GGML_F32Cx4_ZERO
  508. #define GGML_F16_VEC_SET1 GGML_F32Cx4_SET1
  509. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx4_LOAD(p)
  510. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx4_STORE((ggml_fp16_internal_t *)(p), r[i])
  511. #define GGML_F16_VEC_FMA GGML_F32Cx4_FMA
  512. #define GGML_F16_VEC_ADD GGML_F32Cx4_ADD
  513. #define GGML_F16_VEC_MUL GGML_F32Cx4_MUL
  514. #define GGML_F16_VEC_REDUCE GGML_F32Cx4_REDUCE
  515. #endif
  516. #elif defined(__AVX512F__)
  517. #define GGML_SIMD
  518. // F32 AVX512
  519. #define GGML_F32_STEP 64
  520. #define GGML_F32_EPR 16
  521. #define GGML_F32x16 __m512
  522. #define GGML_F32x16_ZERO _mm512_setzero_ps()
  523. #define GGML_F32x16_SET1(x) _mm512_set1_ps(x)
  524. #define GGML_F32x16_LOAD _mm512_loadu_ps
  525. #define GGML_F32x16_STORE _mm512_storeu_ps
  526. // _mm512_fmadd_ps is defined in AVX512F so no guard is required
  527. #define GGML_F32x16_FMA(a, b, c) _mm512_fmadd_ps(b, c, a)
  528. #define GGML_F32x16_ADD _mm512_add_ps
  529. #define GGML_F32x16_MUL _mm512_mul_ps
  530. #define GGML_F32x16_REDUCE(res, x) \
  531. do { \
  532. int offset = GGML_F32_ARR >> 1; \
  533. for (int i = 0; i < offset; ++i) { \
  534. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  535. } \
  536. offset >>= 1; \
  537. for (int i = 0; i < offset; ++i) { \
  538. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  539. } \
  540. offset >>= 1; \
  541. for (int i = 0; i < offset; ++i) { \
  542. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  543. } \
  544. res = (ggml_float) _mm512_reduce_add_ps(x[0]); \
  545. } while (0)
  546. // TODO: is this optimal ?
  547. #define GGML_F32_VEC GGML_F32x16
  548. #define GGML_F32_VEC_ZERO GGML_F32x16_ZERO
  549. #define GGML_F32_VEC_SET1 GGML_F32x16_SET1
  550. #define GGML_F32_VEC_LOAD GGML_F32x16_LOAD
  551. #define GGML_F32_VEC_STORE GGML_F32x16_STORE
  552. #define GGML_F32_VEC_FMA GGML_F32x16_FMA
  553. #define GGML_F32_VEC_ADD GGML_F32x16_ADD
  554. #define GGML_F32_VEC_MUL GGML_F32x16_MUL
  555. #define GGML_F32_VEC_REDUCE GGML_F32x16_REDUCE
  556. // F16 AVX512
  557. // F16 AVX
  558. #define GGML_F16_STEP 64
  559. #define GGML_F16_EPR 16
  560. // AVX512 has FP16 extension (AVX512_FP16) but I don't have it on my machine so I use FP32 instead
  561. #define GGML_F32Cx16 __m512
  562. #define GGML_F32Cx16_ZERO _mm512_setzero_ps()
  563. #define GGML_F32Cx16_SET1(x) _mm512_set1_ps(x)
  564. // unlike _mm256_cvt intrinsics that require F16C, _mm512_cvt is defined in AVX512F
  565. // so F16C guard isn't required
  566. #define GGML_F32Cx16_LOAD(x) _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(x)))
  567. #define GGML_F32Cx16_STORE(x, y) _mm256_storeu_si256((__m256i *)(x), _mm512_cvtps_ph(y, 0))
  568. #define GGML_F32Cx16_FMA(a, b, c) _mm512_fmadd_ps(b, c, a)
  569. #define GGML_F32Cx16_ADD _mm512_add_ps
  570. #define GGML_F32Cx16_MUL _mm512_mul_ps
  571. #define GGML_F32Cx16_REDUCE(res, x) \
  572. do { \
  573. int offset = GGML_F32_ARR >> 1; \
  574. for (int i = 0; i < offset; ++i) { \
  575. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  576. } \
  577. offset >>= 1; \
  578. for (int i = 0; i < offset; ++i) { \
  579. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  580. } \
  581. offset >>= 1; \
  582. for (int i = 0; i < offset; ++i) { \
  583. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  584. } \
  585. res = (ggml_float) _mm512_reduce_add_ps(x[0]); \
  586. } while (0)
  587. #define GGML_F16_VEC GGML_F32Cx16
  588. #define GGML_F16_VEC_ZERO GGML_F32Cx16_ZERO
  589. #define GGML_F16_VEC_SET1 GGML_F32Cx16_SET1
  590. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx16_LOAD(p)
  591. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx16_STORE(p, r[i])
  592. #define GGML_F16_VEC_FMA GGML_F32Cx16_FMA
  593. #define GGML_F16_VEC_ADD GGML_F32Cx16_ADD
  594. #define GGML_F16_VEC_MUL GGML_F32Cx16_MUL
  595. #define GGML_F16_VEC_REDUCE GGML_F32Cx16_REDUCE
  596. #elif defined(__AVX__)
  597. #define GGML_SIMD
  598. // F32 AVX
  599. #define GGML_F32_STEP 32
  600. #define GGML_F32_EPR 8
  601. #define GGML_F32x8 __m256
  602. #define GGML_F32x8_ZERO _mm256_setzero_ps()
  603. #define GGML_F32x8_SET1(x) _mm256_set1_ps(x)
  604. #define GGML_F32x8_LOAD _mm256_loadu_ps
  605. #define GGML_F32x8_STORE _mm256_storeu_ps
  606. #if defined(__FMA__)
  607. #define GGML_F32x8_FMA(a, b, c) _mm256_fmadd_ps(b, c, a)
  608. #else
  609. #define GGML_F32x8_FMA(a, b, c) _mm256_add_ps(_mm256_mul_ps(b, c), a)
  610. #endif
  611. #define GGML_F32x8_ADD _mm256_add_ps
  612. #define GGML_F32x8_MUL _mm256_mul_ps
  613. #define GGML_F32x8_REDUCE(res, x) \
  614. do { \
  615. int offset = GGML_F32_ARR >> 1; \
  616. for (int i = 0; i < offset; ++i) { \
  617. x[i] = _mm256_add_ps(x[i], x[offset+i]); \
  618. } \
  619. offset >>= 1; \
  620. for (int i = 0; i < offset; ++i) { \
  621. x[i] = _mm256_add_ps(x[i], x[offset+i]); \
  622. } \
  623. offset >>= 1; \
  624. for (int i = 0; i < offset; ++i) { \
  625. x[i] = _mm256_add_ps(x[i], x[offset+i]); \
  626. } \
  627. const __m128 t0 = _mm_add_ps(_mm256_castps256_ps128(x[0]), \
  628. _mm256_extractf128_ps(x[0], 1)); \
  629. const __m128 t1 = _mm_hadd_ps(t0, t0); \
  630. res = (ggml_float) _mm_cvtss_f32(_mm_hadd_ps(t1, t1)); \
  631. } while (0)
  632. // TODO: is this optimal ?
  633. #define GGML_F32_VEC GGML_F32x8
  634. #define GGML_F32_VEC_ZERO GGML_F32x8_ZERO
  635. #define GGML_F32_VEC_SET1 GGML_F32x8_SET1
  636. #define GGML_F32_VEC_LOAD GGML_F32x8_LOAD
  637. #define GGML_F32_VEC_STORE GGML_F32x8_STORE
  638. #define GGML_F32_VEC_FMA GGML_F32x8_FMA
  639. #define GGML_F32_VEC_ADD GGML_F32x8_ADD
  640. #define GGML_F32_VEC_MUL GGML_F32x8_MUL
  641. #define GGML_F32_VEC_REDUCE GGML_F32x8_REDUCE
  642. // F16 AVX
  643. #define GGML_F16_STEP 32
  644. #define GGML_F16_EPR 8
  645. // F16 arithmetic is not supported by AVX, so we use F32 instead
  646. #define GGML_F32Cx8 __m256
  647. #define GGML_F32Cx8_ZERO _mm256_setzero_ps()
  648. #define GGML_F32Cx8_SET1(x) _mm256_set1_ps(x)
  649. #if defined(__F16C__)
  650. // the _mm256_cvt intrinsics require F16C
  651. #define GGML_F32Cx8_LOAD(x) _mm256_cvtph_ps(_mm_loadu_si128((const __m128i *)(x)))
  652. #define GGML_F32Cx8_STORE(x, y) _mm_storeu_si128((__m128i *)(x), _mm256_cvtps_ph(y, 0))
  653. #else
  654. static inline __m256 __avx_f32cx8_load(const ggml_fp16_t * x) {
  655. float tmp[8];
  656. for (int i = 0; i < 8; i++) {
  657. tmp[i] = GGML_FP16_TO_FP32(x[i]);
  658. }
  659. return _mm256_loadu_ps(tmp);
  660. }
  661. static inline void __avx_f32cx8_store(ggml_fp16_t *x, __m256 y) {
  662. float arr[8];
  663. _mm256_storeu_ps(arr, y);
  664. for (int i = 0; i < 8; i++)
  665. x[i] = GGML_FP32_TO_FP16(arr[i]);
  666. }
  667. #define GGML_F32Cx8_LOAD(x) __avx_f32cx8_load(x)
  668. #define GGML_F32Cx8_STORE(x, y) __avx_f32cx8_store(x, y)
  669. #endif
  670. #define GGML_F32Cx8_FMA GGML_F32x8_FMA
  671. #define GGML_F32Cx8_ADD _mm256_add_ps
  672. #define GGML_F32Cx8_MUL _mm256_mul_ps
  673. #define GGML_F32Cx8_REDUCE GGML_F32x8_REDUCE
  674. #define GGML_F16_VEC GGML_F32Cx8
  675. #define GGML_F16_VEC_ZERO GGML_F32Cx8_ZERO
  676. #define GGML_F16_VEC_SET1 GGML_F32Cx8_SET1
  677. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx8_LOAD(p)
  678. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx8_STORE(p, r[i])
  679. #define GGML_F16_VEC_FMA GGML_F32Cx8_FMA
  680. #define GGML_F16_VEC_ADD GGML_F32Cx8_ADD
  681. #define GGML_F16_VEC_MUL GGML_F32Cx8_MUL
  682. #define GGML_F16_VEC_REDUCE GGML_F32Cx8_REDUCE
  683. #elif defined(__POWER9_VECTOR__)
  684. #define GGML_SIMD
  685. // F32 POWER9
  686. #define GGML_F32_STEP 32
  687. #define GGML_F32_EPR 4
  688. #define GGML_F32x4 vector float
  689. #define GGML_F32x4_ZERO 0.0f
  690. #define GGML_F32x4_SET1 vec_splats
  691. #define GGML_F32x4_LOAD(p) vec_xl(0, p)
  692. #define GGML_F32x4_STORE(p, r) vec_xst(r, 0, p)
  693. #define GGML_F32x4_FMA(a, b, c) vec_madd(b, c, a)
  694. #define GGML_F32x4_ADD vec_add
  695. #define GGML_F32x4_MUL vec_mul
  696. #define GGML_F32x4_REDUCE(res, x) \
  697. { \
  698. int offset = GGML_F32_ARR >> 1; \
  699. for (int i = 0; i < offset; ++i) { \
  700. x[i] = vec_add(x[i], x[offset+i]); \
  701. } \
  702. offset >>= 1; \
  703. for (int i = 0; i < offset; ++i) { \
  704. x[i] = vec_add(x[i], x[offset+i]); \
  705. } \
  706. offset >>= 1; \
  707. for (int i = 0; i < offset; ++i) { \
  708. x[i] = vec_add(x[i], x[offset+i]); \
  709. } \
  710. res = vec_extract(x[0], 0) + \
  711. vec_extract(x[0], 1) + \
  712. vec_extract(x[0], 2) + \
  713. vec_extract(x[0], 3); \
  714. }
  715. #define GGML_F32_VEC GGML_F32x4
  716. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  717. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  718. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  719. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  720. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  721. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  722. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  723. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  724. // F16 POWER9
  725. #define GGML_F16_STEP GGML_F32_STEP
  726. #define GGML_F16_EPR GGML_F32_EPR
  727. #define GGML_F16_VEC GGML_F32x4
  728. #define GGML_F16_VEC_ZERO GGML_F32x4_ZERO
  729. #define GGML_F16_VEC_SET1 GGML_F32x4_SET1
  730. #define GGML_F16_VEC_FMA GGML_F32x4_FMA
  731. #define GGML_F16_VEC_ADD GGML_F32x4_ADD
  732. #define GGML_F16_VEC_MUL GGML_F32x4_MUL
  733. #define GGML_F16_VEC_REDUCE GGML_F32x4_REDUCE
  734. // Use vec_xl, not vec_ld, in case the load address is not aligned.
  735. #define GGML_F16_VEC_LOAD(p, i) (i & 0x1) ? \
  736. vec_extract_fp32_from_shorth(vec_xl(0, p - GGML_F16_EPR)) : \
  737. vec_extract_fp32_from_shortl(vec_xl(0, p))
  738. #define GGML_ENDIAN_BYTE(i) ((unsigned char *)&(uint16_t){1})[i]
  739. #define GGML_F16_VEC_STORE(p, r, i) \
  740. if (i & 0x1) \
  741. vec_xst(vec_pack_to_short_fp32(r[i - GGML_ENDIAN_BYTE(1)], \
  742. r[i - GGML_ENDIAN_BYTE(0)]), \
  743. 0, p - GGML_F16_EPR)
  744. #elif defined(__wasm_simd128__)
  745. #define GGML_SIMD
  746. // F32 WASM
  747. #define GGML_F32_STEP 16
  748. #define GGML_F32_EPR 4
  749. #define GGML_F32x4 v128_t
  750. #define GGML_F32x4_ZERO wasm_f32x4_splat(0.0f)
  751. #define GGML_F32x4_SET1(x) wasm_f32x4_splat(x)
  752. #define GGML_F32x4_LOAD wasm_v128_load
  753. #define GGML_F32x4_STORE wasm_v128_store
  754. #define GGML_F32x4_FMA(a, b, c) wasm_f32x4_add(wasm_f32x4_mul(b, c), a)
  755. #define GGML_F32x4_ADD wasm_f32x4_add
  756. #define GGML_F32x4_MUL wasm_f32x4_mul
  757. #define GGML_F32x4_REDUCE(res, x) \
  758. { \
  759. int offset = GGML_F32_ARR >> 1; \
  760. for (int i = 0; i < offset; ++i) { \
  761. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  762. } \
  763. offset >>= 1; \
  764. for (int i = 0; i < offset; ++i) { \
  765. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  766. } \
  767. offset >>= 1; \
  768. for (int i = 0; i < offset; ++i) { \
  769. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  770. } \
  771. res = wasm_f32x4_extract_lane(x[0], 0) + \
  772. wasm_f32x4_extract_lane(x[0], 1) + \
  773. wasm_f32x4_extract_lane(x[0], 2) + \
  774. wasm_f32x4_extract_lane(x[0], 3); \
  775. }
  776. #define GGML_F32_VEC GGML_F32x4
  777. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  778. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  779. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  780. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  781. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  782. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  783. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  784. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  785. // F16 WASM
  786. #define GGML_F16_STEP 16
  787. #define GGML_F16_EPR 4
  788. inline static v128_t __wasm_f16x4_load(const ggml_fp16_t * p) {
  789. float tmp[4];
  790. tmp[0] = GGML_FP16_TO_FP32(p[0]);
  791. tmp[1] = GGML_FP16_TO_FP32(p[1]);
  792. tmp[2] = GGML_FP16_TO_FP32(p[2]);
  793. tmp[3] = GGML_FP16_TO_FP32(p[3]);
  794. return wasm_v128_load(tmp);
  795. }
  796. inline static void __wasm_f16x4_store(ggml_fp16_t * p, v128_t x) {
  797. float tmp[4];
  798. wasm_v128_store(tmp, x);
  799. p[0] = GGML_FP32_TO_FP16(tmp[0]);
  800. p[1] = GGML_FP32_TO_FP16(tmp[1]);
  801. p[2] = GGML_FP32_TO_FP16(tmp[2]);
  802. p[3] = GGML_FP32_TO_FP16(tmp[3]);
  803. }
  804. #define GGML_F16x4 v128_t
  805. #define GGML_F16x4_ZERO wasm_f32x4_splat(0.0f)
  806. #define GGML_F16x4_SET1(x) wasm_f32x4_splat(x)
  807. #define GGML_F16x4_LOAD(x) __wasm_f16x4_load(x)
  808. #define GGML_F16x4_STORE(x, y) __wasm_f16x4_store(x, y)
  809. #define GGML_F16x4_FMA GGML_F32x4_FMA
  810. #define GGML_F16x4_ADD wasm_f32x4_add
  811. #define GGML_F16x4_MUL wasm_f32x4_mul
  812. #define GGML_F16x4_REDUCE(res, x) \
  813. { \
  814. int offset = GGML_F16_ARR >> 1; \
  815. for (int i = 0; i < offset; ++i) { \
  816. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  817. } \
  818. offset >>= 1; \
  819. for (int i = 0; i < offset; ++i) { \
  820. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  821. } \
  822. offset >>= 1; \
  823. for (int i = 0; i < offset; ++i) { \
  824. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  825. } \
  826. res = wasm_f32x4_extract_lane(x[0], 0) + \
  827. wasm_f32x4_extract_lane(x[0], 1) + \
  828. wasm_f32x4_extract_lane(x[0], 2) + \
  829. wasm_f32x4_extract_lane(x[0], 3); \
  830. }
  831. #define GGML_F16_VEC GGML_F16x4
  832. #define GGML_F16_VEC_ZERO GGML_F16x4_ZERO
  833. #define GGML_F16_VEC_SET1 GGML_F16x4_SET1
  834. #define GGML_F16_VEC_LOAD(p, i) GGML_F16x4_LOAD(p)
  835. #define GGML_F16_VEC_STORE(p, r, i) GGML_F16x4_STORE(p, r[i])
  836. #define GGML_F16_VEC_FMA GGML_F16x4_FMA
  837. #define GGML_F16_VEC_ADD GGML_F16x4_ADD
  838. #define GGML_F16_VEC_MUL GGML_F16x4_MUL
  839. #define GGML_F16_VEC_REDUCE GGML_F16x4_REDUCE
  840. #elif defined(__SSE3__)
  841. #define GGML_SIMD
  842. // F32 SSE
  843. #define GGML_F32_STEP 32
  844. #define GGML_F32_EPR 4
  845. #define GGML_F32x4 __m128
  846. #define GGML_F32x4_ZERO _mm_setzero_ps()
  847. #define GGML_F32x4_SET1(x) _mm_set1_ps(x)
  848. #define GGML_F32x4_LOAD _mm_loadu_ps
  849. #define GGML_F32x4_STORE _mm_storeu_ps
  850. #if defined(__FMA__)
  851. // TODO: Does this work?
  852. #define GGML_F32x4_FMA(a, b, c) _mm_fmadd_ps(b, c, a)
  853. #else
  854. #define GGML_F32x4_FMA(a, b, c) _mm_add_ps(_mm_mul_ps(b, c), a)
  855. #endif
  856. #define GGML_F32x4_ADD _mm_add_ps
  857. #define GGML_F32x4_MUL _mm_mul_ps
  858. #define GGML_F32x4_REDUCE(res, x) \
  859. { \
  860. int offset = GGML_F32_ARR >> 1; \
  861. for (int i = 0; i < offset; ++i) { \
  862. x[i] = _mm_add_ps(x[i], x[offset+i]); \
  863. } \
  864. offset >>= 1; \
  865. for (int i = 0; i < offset; ++i) { \
  866. x[i] = _mm_add_ps(x[i], x[offset+i]); \
  867. } \
  868. offset >>= 1; \
  869. for (int i = 0; i < offset; ++i) { \
  870. x[i] = _mm_add_ps(x[i], x[offset+i]); \
  871. } \
  872. const __m128 t0 = _mm_hadd_ps(x[0], x[0]); \
  873. res = (ggml_float) _mm_cvtss_f32(_mm_hadd_ps(t0, t0)); \
  874. }
  875. // TODO: is this optimal ?
  876. #define GGML_F32_VEC GGML_F32x4
  877. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  878. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  879. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  880. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  881. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  882. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  883. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  884. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  885. // F16 SSE
  886. #define GGML_F16_STEP 32
  887. #define GGML_F16_EPR 4
  888. static inline __m128 __sse_f16x4_load(ggml_fp16_t *x) {
  889. float tmp[4];
  890. tmp[0] = GGML_FP16_TO_FP32(x[0]);
  891. tmp[1] = GGML_FP16_TO_FP32(x[1]);
  892. tmp[2] = GGML_FP16_TO_FP32(x[2]);
  893. tmp[3] = GGML_FP16_TO_FP32(x[3]);
  894. return _mm_loadu_ps(tmp);
  895. }
  896. static inline void __sse_f16x4_store(ggml_fp16_t *x, __m128 y) {
  897. float arr[4];
  898. _mm_storeu_ps(arr, y);
  899. x[0] = GGML_FP32_TO_FP16(arr[0]);
  900. x[1] = GGML_FP32_TO_FP16(arr[1]);
  901. x[2] = GGML_FP32_TO_FP16(arr[2]);
  902. x[3] = GGML_FP32_TO_FP16(arr[3]);
  903. }
  904. #define GGML_F32Cx4 __m128
  905. #define GGML_F32Cx4_ZERO _mm_setzero_ps()
  906. #define GGML_F32Cx4_SET1(x) _mm_set1_ps(x)
  907. #define GGML_F32Cx4_LOAD(x) __sse_f16x4_load(x)
  908. #define GGML_F32Cx4_STORE(x, y) __sse_f16x4_store(x, y)
  909. #define GGML_F32Cx4_FMA GGML_F32x4_FMA
  910. #define GGML_F32Cx4_ADD _mm_add_ps
  911. #define GGML_F32Cx4_MUL _mm_mul_ps
  912. #define GGML_F32Cx4_REDUCE GGML_F32x4_REDUCE
  913. #define GGML_F16_VEC GGML_F32Cx4
  914. #define GGML_F16_VEC_ZERO GGML_F32Cx4_ZERO
  915. #define GGML_F16_VEC_SET1 GGML_F32Cx4_SET1
  916. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx4_LOAD(p)
  917. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx4_STORE(p, r[i])
  918. #define GGML_F16_VEC_FMA GGML_F32Cx4_FMA
  919. #define GGML_F16_VEC_ADD GGML_F32Cx4_ADD
  920. #define GGML_F16_VEC_MUL GGML_F32Cx4_MUL
  921. #define GGML_F16_VEC_REDUCE GGML_F32Cx4_REDUCE
  922. #elif defined(__loongarch_asx)
  923. #define GGML_SIMD
  924. // F32 LASX
  925. #define GGML_F32_STEP 32
  926. #define GGML_F32_EPR 8
  927. #define GGML_F32x8 __m256
  928. #define GGML_F32x8_ZERO (__m256)__lasx_xvldi(0)
  929. #define GGML_F32x8_SET1(x) (__m256)__lasx_xvreplfr2vr_s((x))
  930. #define GGML_F32x8_LOAD(x) (__m256)__lasx_xvld((x), 0)
  931. #define GGML_F32x8_STORE(x,y) __lasx_xvst((y), (x), 0)
  932. #define GGML_F32x8_FMA(a, b, c) __lasx_xvfmadd_s(b, c, a)
  933. #define GGML_F32x8_ADD __lasx_xvfadd_s
  934. #define GGML_F32x8_MUL __lasx_xvfmul_s
  935. #define GGML_F32x8_REDUCE(res, x) \
  936. do { \
  937. int offset = GGML_F32_ARR >> 1; \
  938. for (int i = 0; i < offset; ++i) { \
  939. x[i] = __lasx_xvfadd_s(x[i], x[offset+i]); \
  940. } \
  941. offset >>= 1; \
  942. for (int i = 0; i < offset; ++i) { \
  943. x[i] = __lasx_xvfadd_s(x[i], x[offset+i]); \
  944. } \
  945. offset >>= 1; \
  946. for (int i = 0; i < offset; ++i) { \
  947. x[i] = __lasx_xvfadd_s(x[i], x[offset+i]); \
  948. } \
  949. float *tmp_p = (float *)&x[0]; \
  950. 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]; \
  951. } while (0)
  952. // TODO: is this optimal ?
  953. #define GGML_F32_VEC GGML_F32x8
  954. #define GGML_F32_VEC_ZERO GGML_F32x8_ZERO
  955. #define GGML_F32_VEC_SET1 GGML_F32x8_SET1
  956. #define GGML_F32_VEC_LOAD GGML_F32x8_LOAD
  957. #define GGML_F32_VEC_STORE GGML_F32x8_STORE
  958. #define GGML_F32_VEC_FMA GGML_F32x8_FMA
  959. #define GGML_F32_VEC_ADD GGML_F32x8_ADD
  960. #define GGML_F32_VEC_MUL GGML_F32x8_MUL
  961. #define GGML_F32_VEC_REDUCE GGML_F32x8_REDUCE
  962. // F16 LASX
  963. #define GGML_F16_STEP 32
  964. #define GGML_F16_EPR 8
  965. // F16 arithmetic is not supported by AVX, so we use F32 instead
  966. #define GGML_F32Cx8 __m256
  967. #define GGML_F32Cx8_ZERO (__m256)__lasx_xvldi(0)
  968. #define GGML_F32Cx8_SET1(x) (__m256)__lasx_xvreplgr2vr_w((x))
  969. static inline __m256 __lasx_f32cx8_load(const ggml_fp16_t * x) {
  970. float tmp[8];
  971. for (int i = 0; i < 8; i++) {
  972. tmp[i] = GGML_FP16_TO_FP32(x[i]);
  973. }
  974. return (__m256)__lasx_xvld(tmp, 0);
  975. }
  976. static inline void __lasx_f32cx8_store(ggml_fp16_t * x, __m256 y) {
  977. float arr[8];
  978. __lasx_xvst(y, arr, 0);
  979. for (int i = 0; i < 8; i++) {
  980. x[i] = GGML_FP32_TO_FP16(arr[i]);
  981. }
  982. }
  983. #define GGML_F32Cx8_LOAD(x) __lasx_f32cx8_load(x)
  984. #define GGML_F32Cx8_STORE(x, y) __lasx_f32cx8_store(x, y)
  985. #define GGML_F32Cx8_FMA GGML_F32x8_FMA
  986. #define GGML_F32Cx8_ADD __lasx_xvfadd_s
  987. #define GGML_F32Cx8_MUL __lasx_xvfmul_s
  988. #define GGML_F32Cx8_REDUCE GGML_F32x8_REDUCE
  989. #define GGML_F16_VEC GGML_F32Cx8
  990. #define GGML_F16_VEC_ZERO GGML_F32Cx8_ZERO
  991. #define GGML_F16_VEC_SET1 GGML_F32Cx8_SET1
  992. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx8_LOAD(p)
  993. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx8_STORE(p, r[i])
  994. #define GGML_F16_VEC_FMA GGML_F32Cx8_FMA
  995. #define GGML_F16_VEC_ADD GGML_F32Cx8_ADD
  996. #define GGML_F16_VEC_MUL GGML_F32Cx8_MUL
  997. #define GGML_F16_VEC_REDUCE GGML_F32Cx8_REDUCE
  998. #elif defined(__loongarch_sx)
  999. #define GGML_SIMD
  1000. // F32 LSX
  1001. #define GGML_F32_STEP 32
  1002. #define GGML_F32_EPR 4
  1003. #define GGML_F32x4 __m128
  1004. #define GGML_F32x4_ZERO __lsx_vldi(0)
  1005. #define GGML_F32x4_SET1(x) __lsx_vinsgr2vr_w(__lsx_vldi(0),(x), 0)
  1006. #define GGML_F32x4_LOAD(x) __lsx_vld((x), 0)
  1007. #define GGML_F32x4_STORE((x),(y)) __lsx_vst((y), (x), 0)
  1008. #define GGML_F32x4_FMA(a, b, c) __lsx_vfmadd_s(b, c, a)
  1009. #define GGML_F32x4_ADD __lsx_vfadd_s
  1010. #define GGML_F32x4_MUL __lsx_vfmul_s
  1011. #define GGML_F32x4_REDUCE(res, x) \
  1012. { \
  1013. int offset = GGML_F32_ARR >> 1; \
  1014. for (int i = 0; i < offset; ++i) { \
  1015. x[i] = __lsx_vfadd_s(x[i], x[offset + i]); \
  1016. } \
  1017. offset >>= 1; \
  1018. for (int i = 0; i < offset; ++i) { \
  1019. x[i] = __lsx_vfadd_s(x[i], x[offset + i]); \
  1020. } \
  1021. offset >>= 1; \
  1022. for (int i = 0; i < offset; ++i) { \
  1023. x[i] = __lsx_vfadd_s(x[i], x[offset + i]); \
  1024. } \
  1025. __m128i tmp = __lsx_vsrli_d((__m128i) x[0], 32); \
  1026. tmp = (__m128i) __lsx_vfadd_s((__m128) tmp, x[0]); \
  1027. tmp = __lsx_vpickev_w(__lsx_vldi(0), tmp); \
  1028. const __m128 t0 = __lsx_vshuf4i_w(tmp, 0x88); \
  1029. tmp = __lsx_vsrli_d((__m128i) t0, 32); \
  1030. tmp = (__m128i) __lsx_vfadd_s((__m128) tmp, t0); \
  1031. tmp = __lsx_vpickev_w(__lsx_vldi(0), tmp); \
  1032. res = (ggml_float) __lsx_vpickve2gr_w(__lsx_vshuf4i_w(tmp, 0x88), 0); \
  1033. }
  1034. #define GGML_F32_VEC GGML_F32x4
  1035. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  1036. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  1037. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  1038. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  1039. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  1040. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  1041. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  1042. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  1043. // F16 LSX
  1044. #define GGML_F16_STEP 32
  1045. #define GGML_F16_EPR 4
  1046. static inline __m128 __lsx_f16x4_load(const ggml_fp16_t * x) {
  1047. float tmp[4];
  1048. tmp[0] = GGML_FP16_TO_FP32(x[0]);
  1049. tmp[1] = GGML_FP16_TO_FP32(x[1]);
  1050. tmp[2] = GGML_FP16_TO_FP32(x[2]);
  1051. tmp[3] = GGML_FP16_TO_FP32(x[3]);
  1052. return __lsx_vld(tmp, 0);
  1053. }
  1054. static inline void __lsx_f16x4_store(ggml_fp16_t * x, __m128 y) {
  1055. float arr[4];
  1056. __lsx_vst(y, arr, 0);
  1057. x[0] = GGML_FP32_TO_FP16(arr[0]);
  1058. x[1] = GGML_FP32_TO_FP16(arr[1]);
  1059. x[2] = GGML_FP32_TO_FP16(arr[2]);
  1060. x[3] = GGML_FP32_TO_FP16(arr[3]);
  1061. }
  1062. #define GGML_F32Cx4 __m128
  1063. #define GGML_F32Cx4_ZERO __lsx_vldi(0)
  1064. #define GGML_F32Cx4_SET1(x) __lsx_vinsgr2vr_w(__lsx_vldi(0),(x), 0)
  1065. #define GGML_F32Cx4_LOAD(x) __lsx_f16x4_load(x)
  1066. #define GGML_F32Cx4_STORE(x, y) __lsx_f16x4_store(x, y)
  1067. #define GGML_F32Cx4_FMA GGML_F32x4_FMA
  1068. #define GGML_F32Cx4_ADD __lsx_vfadd_s
  1069. #define GGML_F32Cx4_MUL __lsx_vfmul_s
  1070. #define GGML_F32Cx4_REDUCE GGML_F32x4_REDUCE
  1071. #define GGML_F16_VEC GGML_F32Cx4
  1072. #define GGML_F16_VEC_ZERO GGML_F32Cx4_ZERO
  1073. #define GGML_F16_VEC_SET1 GGML_F32Cx4_SET1
  1074. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx4_LOAD(p)
  1075. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx4_STORE(p, r[i])
  1076. #define GGML_F16_VEC_FMA GGML_F32Cx4_FMA
  1077. #define GGML_F16_VEC_ADD GGML_F32Cx4_ADD
  1078. #define GGML_F16_VEC_MUL GGML_F32Cx4_MUL
  1079. #define GGML_F16_VEC_REDUCE GGML_F32Cx4_REDUCE
  1080. #endif
  1081. // GGML_F32_ARR / GGML_F16_ARR
  1082. // number of registers to use per step
  1083. #ifdef GGML_SIMD
  1084. #define GGML_F32_ARR (GGML_F32_STEP/GGML_F32_EPR)
  1085. #define GGML_F16_ARR (GGML_F16_STEP/GGML_F16_EPR)
  1086. #endif
  1087. //
  1088. // Threading defs
  1089. //
  1090. typedef pthread_t ggml_thread_t;
  1091. #if defined(_WIN32)
  1092. typedef CONDITION_VARIABLE ggml_cond_t;
  1093. typedef SRWLOCK ggml_mutex_t;
  1094. #define ggml_mutex_init(m) InitializeSRWLock(m)
  1095. #define ggml_mutex_destroy(m)
  1096. #define ggml_mutex_lock(m) AcquireSRWLockExclusive(m)
  1097. #define ggml_mutex_unlock(m) ReleaseSRWLockExclusive(m)
  1098. #define ggml_mutex_lock_shared(m) AcquireSRWLockShared(m)
  1099. #define ggml_mutex_unlock_shared(m) ReleaseSRWLockShared(m)
  1100. #define ggml_cond_init(c) InitializeConditionVariable(c)
  1101. #define ggml_cond_destroy(c)
  1102. #define ggml_cond_wait(c, m) SleepConditionVariableSRW(c, m, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED)
  1103. #define ggml_cond_broadcast(c) WakeAllConditionVariable(c)
  1104. #define ggml_thread_create pthread_create
  1105. #define ggml_thread_join pthread_join
  1106. #else
  1107. typedef pthread_cond_t ggml_cond_t;
  1108. typedef pthread_mutex_t ggml_mutex_t;
  1109. #define ggml_mutex_init(m) pthread_mutex_init(m, NULL)
  1110. #define ggml_mutex_destroy(m) pthread_mutex_destroy(m)
  1111. #define ggml_mutex_lock(m) pthread_mutex_lock(m)
  1112. #define ggml_mutex_unlock(m) pthread_mutex_unlock(m)
  1113. #define ggml_mutex_lock_shared(m) pthread_mutex_lock(m)
  1114. #define ggml_mutex_unlock_shared(m) pthread_mutex_unlock(m)
  1115. #define ggml_lock_init(x) UNUSED(x)
  1116. #define ggml_lock_destroy(x) UNUSED(x)
  1117. #if defined(__x86_64__) || (defined(_MSC_VER) && defined(_M_AMD64))
  1118. #define ggml_lock_lock(x) _mm_pause()
  1119. #else
  1120. #define ggml_lock_lock(x) UNUSED(x)
  1121. #endif
  1122. #define ggml_lock_unlock(x) UNUSED(x)
  1123. #define GGML_LOCK_INITIALIZER 0
  1124. #define ggml_cond_init(c) pthread_cond_init(c, NULL)
  1125. #define ggml_cond_destroy(c) pthread_cond_destroy(c)
  1126. #define ggml_cond_wait(c, m) pthread_cond_wait(c, m)
  1127. #define ggml_cond_broadcast(c) pthread_cond_broadcast(c)
  1128. #define ggml_thread_create pthread_create
  1129. #define ggml_thread_join pthread_join
  1130. #endif
  1131. // Threadpool def
  1132. struct ggml_threadpool {
  1133. ggml_mutex_t mutex; // mutex for cond.var
  1134. ggml_cond_t cond; // cond.var for waiting for new work
  1135. struct ggml_cgraph * cgraph;
  1136. struct ggml_cplan * cplan;
  1137. // synchronization primitives
  1138. atomic_int n_graph; // incremented when there is work to be done (i.e each graph)
  1139. atomic_int GGML_CACHE_ALIGN n_barrier;
  1140. atomic_int GGML_CACHE_ALIGN n_barrier_passed;
  1141. atomic_int current_chunk; // currently processing chunk during Mat_Mul, shared between all the threads.
  1142. // these are atomic as an annotation for thread-sanitizer
  1143. atomic_bool stop; // Used for stopping the threadpool altogether
  1144. atomic_bool pause; // Used for pausing the threadpool or individual threads
  1145. atomic_bool abort; // Used for aborting processing of a graph
  1146. struct ggml_compute_state * workers; // per thread state
  1147. int n_threads_max; // number of threads in the pool
  1148. atomic_int n_threads_cur; // number of threads used in the current graph
  1149. int32_t prio; // Scheduling priority
  1150. uint32_t poll; // Polling level (0 - no polling)
  1151. enum ggml_status ec;
  1152. };
  1153. // Per-thread state
  1154. struct ggml_compute_state {
  1155. #ifndef GGML_USE_OPENMP
  1156. ggml_thread_t thrd;
  1157. bool cpumask[GGML_MAX_N_THREADS];
  1158. int last_graph;
  1159. bool pending;
  1160. #endif
  1161. struct ggml_threadpool * threadpool;
  1162. int ith;
  1163. };
  1164. //
  1165. // fundamental operations
  1166. //
  1167. 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; }
  1168. 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; }
  1169. 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; }
  1170. 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]; }
  1171. 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; }
  1172. 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; }
  1173. 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]; }
  1174. 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; }
  1175. 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]; }
  1176. 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; }
  1177. 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]; }
  1178. 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; }
  1179. 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]; }
  1180. 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]; }
  1181. 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]; }
  1182. 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]; }
  1183. 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) {
  1184. assert(nrc == 1);
  1185. UNUSED(nrc);
  1186. UNUSED(bx);
  1187. UNUSED(by);
  1188. UNUSED(bs);
  1189. #if defined(GGML_SIMD)
  1190. float sumf = 0.0f;
  1191. const int np = (n & ~(GGML_F32_STEP - 1));
  1192. GGML_F32_VEC sum[GGML_F32_ARR] = { GGML_F32_VEC_ZERO };
  1193. GGML_F32_VEC ax[GGML_F32_ARR];
  1194. GGML_F32_VEC ay[GGML_F32_ARR];
  1195. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1196. for (int j = 0; j < GGML_F32_ARR; j++) {
  1197. ax[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
  1198. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1199. sum[j] = GGML_F32_VEC_FMA(sum[j], ax[j], ay[j]);
  1200. }
  1201. }
  1202. // reduce sum0..sum3 to sum0
  1203. GGML_F32_VEC_REDUCE(sumf, sum);
  1204. // leftovers
  1205. for (int i = np; i < n; ++i) {
  1206. sumf += x[i]*y[i];
  1207. }
  1208. #else
  1209. // scalar
  1210. ggml_float sumf = 0.0;
  1211. for (int i = 0; i < n; ++i) {
  1212. sumf += (ggml_float)(x[i]*y[i]);
  1213. }
  1214. #endif
  1215. *s = sumf;
  1216. }
  1217. 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) {
  1218. assert(nrc == 1);
  1219. UNUSED(nrc);
  1220. UNUSED(bx);
  1221. UNUSED(by);
  1222. UNUSED(bs);
  1223. int i = 0;
  1224. ggml_float sumf = 0;
  1225. #if defined(__AVX512BF16__)
  1226. __m512 c1 = _mm512_setzero_ps();
  1227. __m512 c2 = _mm512_setzero_ps();
  1228. for (; i + 64 <= n; i += 64) {
  1229. c1 = _mm512_dpbf16_ps(c1, m512bh(_mm512_loadu_si512((x + i))),
  1230. m512bh(_mm512_loadu_si512((y + i))));
  1231. c2 = _mm512_dpbf16_ps(c2, m512bh(_mm512_loadu_si512((x + i + 32))),
  1232. m512bh(_mm512_loadu_si512((y + i + 32))));
  1233. }
  1234. sumf += (ggml_float)_mm512_reduce_add_ps(c1);
  1235. sumf += (ggml_float)_mm512_reduce_add_ps(c2);
  1236. #elif defined(__AVX512F__)
  1237. #define LOAD(p) _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(_mm256_loadu_si256((const __m256i *)(p))), 16))
  1238. __m512 c1 = _mm512_setzero_ps();
  1239. __m512 c2 = _mm512_setzero_ps();
  1240. for (; i + 32 <= n; i += 32) {
  1241. c1 = _mm512_add_ps(_mm512_mul_ps(LOAD(x + i), LOAD(y + i)), c1);
  1242. c2 = _mm512_add_ps(_mm512_mul_ps(LOAD(x + i + 16), LOAD(y + i + 16)), c2);
  1243. }
  1244. sumf += (ggml_float)_mm512_reduce_add_ps(c1);
  1245. sumf += (ggml_float)_mm512_reduce_add_ps(c2);
  1246. #undef LOAD
  1247. #elif defined(__AVX2__) || defined(__AVX__)
  1248. #if defined(__AVX2__)
  1249. #define LOAD(p) _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i *)(p))), 16))
  1250. #else
  1251. #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))
  1252. #endif
  1253. __m256 c1 = _mm256_setzero_ps();
  1254. __m256 c2 = _mm256_setzero_ps();
  1255. __m256 c3 = _mm256_setzero_ps();
  1256. __m256 c4 = _mm256_setzero_ps();
  1257. for (; i + 32 <= n; i += 32) {
  1258. c1 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i), LOAD(y + i)), c1);
  1259. c2 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 8), LOAD(y + i + 8)), c2);
  1260. c3 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 16), LOAD(y + i + 16)), c3);
  1261. c4 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 24), LOAD(y + i + 24)), c4);
  1262. }
  1263. __m128 g;
  1264. c1 = _mm256_add_ps(_mm256_add_ps(c1, c3),
  1265. _mm256_add_ps(c2, c4));
  1266. g = _mm_add_ps(_mm256_extractf128_ps(c1, 1),
  1267. _mm256_castps256_ps128(c1));
  1268. g = _mm_add_ps(g, _mm_movehl_ps(g, g));
  1269. g = _mm_add_ss(g, _mm_movehdup_ps(g));
  1270. sumf += (ggml_float)_mm_cvtss_f32(g);
  1271. #undef LOAD
  1272. #endif
  1273. for (; i < n; ++i) {
  1274. sumf += (ggml_float)(GGML_BF16_TO_FP32(x[i]) *
  1275. GGML_BF16_TO_FP32(y[i]));
  1276. }
  1277. *s = sumf;
  1278. }
  1279. 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) {
  1280. assert(nrc == 1);
  1281. UNUSED(nrc);
  1282. UNUSED(bx);
  1283. UNUSED(by);
  1284. UNUSED(bs);
  1285. ggml_float sumf = 0.0;
  1286. #if defined(GGML_SIMD)
  1287. const int np = (n & ~(GGML_F16_STEP - 1));
  1288. GGML_F16_VEC sum[GGML_F16_ARR] = { GGML_F16_VEC_ZERO };
  1289. GGML_F16_VEC ax[GGML_F16_ARR];
  1290. GGML_F16_VEC ay[GGML_F16_ARR];
  1291. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1292. for (int j = 0; j < GGML_F16_ARR; j++) {
  1293. ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j);
  1294. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1295. sum[j] = GGML_F16_VEC_FMA(sum[j], ax[j], ay[j]);
  1296. }
  1297. }
  1298. // reduce sum0..sum3 to sum0
  1299. GGML_F16_VEC_REDUCE(sumf, sum);
  1300. // leftovers
  1301. for (int i = np; i < n; ++i) {
  1302. sumf += (ggml_float)(GGML_FP16_TO_FP32(x[i])*GGML_FP16_TO_FP32(y[i]));
  1303. }
  1304. #else
  1305. for (int i = 0; i < n; ++i) {
  1306. sumf += (ggml_float)(GGML_FP16_TO_FP32(x[i])*GGML_FP16_TO_FP32(y[i]));
  1307. }
  1308. #endif
  1309. *s = sumf;
  1310. }
  1311. // compute GGML_VEC_DOT_UNROLL dot products at once
  1312. // xs - x row stride in bytes
  1313. 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) {
  1314. ggml_float sumf[GGML_VEC_DOT_UNROLL] = { 0.0 };
  1315. ggml_fp16_t * restrict x[GGML_VEC_DOT_UNROLL];
  1316. for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
  1317. x[i] = (ggml_fp16_t *) ((char *) xv + i*xs);
  1318. }
  1319. #if defined(GGML_SIMD)
  1320. const int np = (n & ~(GGML_F16_STEP - 1));
  1321. GGML_F16_VEC sum[GGML_VEC_DOT_UNROLL][GGML_F16_ARR] = { { GGML_F16_VEC_ZERO } };
  1322. GGML_F16_VEC ax[GGML_F16_ARR];
  1323. GGML_F16_VEC ay[GGML_F16_ARR];
  1324. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1325. for (int j = 0; j < GGML_F16_ARR; j++) {
  1326. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1327. for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
  1328. ax[j] = GGML_F16_VEC_LOAD(x[k] + i + j*GGML_F16_EPR, j);
  1329. sum[k][j] = GGML_F16_VEC_FMA(sum[k][j], ax[j], ay[j]);
  1330. }
  1331. }
  1332. }
  1333. // reduce sum0..sum3 to sum0
  1334. for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
  1335. GGML_F16_VEC_REDUCE(sumf[k], sum[k]);
  1336. }
  1337. // leftovers
  1338. for (int i = np; i < n; ++i) {
  1339. for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
  1340. sumf[j] += (ggml_float)(GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]));
  1341. }
  1342. }
  1343. #else
  1344. for (int i = 0; i < n; ++i) {
  1345. for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
  1346. sumf[j] += (ggml_float)(GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]));
  1347. }
  1348. }
  1349. #endif
  1350. for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
  1351. s[i] = sumf[i];
  1352. }
  1353. }
  1354. inline static void ggml_vec_mad_f32(const int n, float * restrict y, const float * restrict x, const float v) {
  1355. #if defined(GGML_SIMD)
  1356. const int np = (n & ~(GGML_F32_STEP - 1));
  1357. GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
  1358. GGML_F32_VEC ax[GGML_F32_ARR];
  1359. GGML_F32_VEC ay[GGML_F32_ARR];
  1360. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1361. for (int j = 0; j < GGML_F32_ARR; j++) {
  1362. ax[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
  1363. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1364. ay[j] = GGML_F32_VEC_FMA(ay[j], ax[j], vx);
  1365. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  1366. }
  1367. }
  1368. // leftovers
  1369. for (int i = np; i < n; ++i) {
  1370. y[i] += x[i]*v;
  1371. }
  1372. #else
  1373. // scalar
  1374. for (int i = 0; i < n; ++i) {
  1375. y[i] += x[i]*v;
  1376. }
  1377. #endif
  1378. }
  1379. inline static void ggml_vec_mad_f16(const int n, ggml_fp16_t * restrict y, const ggml_fp16_t * restrict x, const float v) {
  1380. #if defined(GGML_SIMD)
  1381. const int np = (n & ~(GGML_F16_STEP - 1));
  1382. GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
  1383. GGML_F16_VEC ax[GGML_F16_ARR];
  1384. GGML_F16_VEC ay[GGML_F16_ARR];
  1385. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1386. for (int j = 0; j < GGML_F16_ARR; j++) {
  1387. ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j);
  1388. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1389. ay[j] = GGML_F16_VEC_FMA(ay[j], ax[j], vx);
  1390. GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
  1391. }
  1392. }
  1393. // leftovers
  1394. for (int i = np; i < n; ++i) {
  1395. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i]) + GGML_FP16_TO_FP32(x[i])*v);
  1396. }
  1397. #else
  1398. // scalar
  1399. for (int i = 0; i < n; ++i) {
  1400. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i]) + GGML_FP16_TO_FP32(x[i])*v);
  1401. }
  1402. #endif
  1403. }
  1404. // xs and vs are byte strides of x and v
  1405. 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) {
  1406. const float * restrict x[GGML_VEC_MAD_UNROLL];
  1407. const float * restrict v[GGML_VEC_MAD_UNROLL];
  1408. for (int i = 0; i < GGML_VEC_MAD_UNROLL; ++i) {
  1409. x[i] = (const float *) ((const char *) xv + i*xs);
  1410. v[i] = (const float *) ((const char *) vv + i*vs);
  1411. }
  1412. #if defined(GGML_SIMD)
  1413. const int np = (n & ~(GGML_F32_STEP - 1));
  1414. GGML_F32_VEC vx[GGML_VEC_MAD_UNROLL];
  1415. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1416. vx[k] = GGML_F32_VEC_SET1(v[k][0]);
  1417. }
  1418. GGML_F32_VEC ax[GGML_VEC_MAD_UNROLL][GGML_F32_ARR];
  1419. GGML_F32_VEC ay[GGML_F32_ARR];
  1420. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1421. for (int j = 0; j < GGML_F32_ARR; j++) {
  1422. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1423. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1424. ax[k][j] = GGML_F32_VEC_LOAD(x[k] + i + j*GGML_F32_EPR);
  1425. ay[j] = GGML_F32_VEC_FMA(ay[j], ax[k][j], vx[k]);
  1426. }
  1427. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  1428. }
  1429. }
  1430. // leftovers
  1431. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1432. for (int i = np; i < n; ++i) {
  1433. y[i] += x[k][i]*v[k][0];
  1434. }
  1435. }
  1436. #else
  1437. // scalar
  1438. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1439. for (int i = 0; i < n; ++i) {
  1440. y[i] += x[k][i]*v[k][0];
  1441. }
  1442. }
  1443. #endif
  1444. }
  1445. //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; }
  1446. inline static void ggml_vec_scale_f32(const int n, float * y, const float v) {
  1447. #if defined(GGML_USE_ACCELERATE)
  1448. vDSP_vsmul(y, 1, &v, y, 1, n);
  1449. #elif defined(GGML_SIMD)
  1450. const int np = (n & ~(GGML_F32_STEP - 1));
  1451. GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
  1452. GGML_F32_VEC ay[GGML_F32_ARR];
  1453. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1454. for (int j = 0; j < GGML_F32_ARR; j++) {
  1455. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1456. ay[j] = GGML_F32_VEC_MUL(ay[j], vx);
  1457. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  1458. }
  1459. }
  1460. // leftovers
  1461. for (int i = np; i < n; ++i) {
  1462. y[i] *= v;
  1463. }
  1464. #else
  1465. // scalar
  1466. for (int i = 0; i < n; ++i) {
  1467. y[i] *= v;
  1468. }
  1469. #endif
  1470. }
  1471. inline static void ggml_vec_scale_f16(const int n, ggml_fp16_t * y, const float v) {
  1472. #if defined(GGML_SIMD)
  1473. const int np = (n & ~(GGML_F16_STEP - 1));
  1474. GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
  1475. GGML_F16_VEC ay[GGML_F16_ARR];
  1476. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1477. for (int j = 0; j < GGML_F16_ARR; j++) {
  1478. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1479. ay[j] = GGML_F16_VEC_MUL(ay[j], vx);
  1480. GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
  1481. }
  1482. }
  1483. // leftovers
  1484. for (int i = np; i < n; ++i) {
  1485. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i])*v);
  1486. }
  1487. #else
  1488. // scalar
  1489. for (int i = 0; i < n; ++i) {
  1490. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i])*v);
  1491. }
  1492. #endif
  1493. }
  1494. 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); }
  1495. 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]; }
  1496. 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]); }
  1497. 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]); }
  1498. 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]); }
  1499. 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]); }
  1500. 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]); }
  1501. 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); }
  1502. 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; }
  1503. 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]); }
  1504. 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]); }
  1505. 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; }
  1506. 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); }
  1507. 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])); }
  1508. // TODO: optimize performance
  1509. 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)); }
  1510. 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)); }
  1511. 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]); }
  1512. static const float GELU_COEF_A = 0.044715f;
  1513. static const float GELU_QUICK_COEF = -1.702f;
  1514. static const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  1515. inline static float ggml_gelu_f32(float x) {
  1516. return 0.5f*x*(1.0f + tanhf(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  1517. }
  1518. inline static void ggml_vec_gelu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  1519. const uint16_t * i16 = (const uint16_t *) x;
  1520. for (int i = 0; i < n; ++i) {
  1521. y[i] = ggml_table_gelu_f16[i16[i]];
  1522. }
  1523. }
  1524. #ifdef GGML_GELU_FP16
  1525. inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
  1526. uint16_t t;
  1527. for (int i = 0; i < n; ++i) {
  1528. if (x[i] <= -10.0f) {
  1529. y[i] = 0.0f;
  1530. } else if (x[i] >= 10.0f) {
  1531. y[i] = x[i];
  1532. } else {
  1533. ggml_fp16_t fp16 = GGML_FP32_TO_FP16(x[i]);
  1534. memcpy(&t, &fp16, sizeof(uint16_t));
  1535. y[i] = GGML_FP16_TO_FP32(ggml_table_gelu_f16[t]);
  1536. }
  1537. }
  1538. }
  1539. #else
  1540. inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
  1541. for (int i = 0; i < n; ++i) {
  1542. y[i] = ggml_gelu_f32(x[i]);
  1543. }
  1544. }
  1545. #endif
  1546. inline static float ggml_gelu_quick_f32(float x) {
  1547. return x*(1.0f/(1.0f+expf(GELU_QUICK_COEF*x)));
  1548. }
  1549. //inline static void ggml_vec_gelu_quick_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  1550. // const uint16_t * i16 = (const uint16_t *) x;
  1551. // for (int i = 0; i < n; ++i) {
  1552. // y[i] = ggml_table_gelu_quick_f16[i16[i]];
  1553. // }
  1554. //}
  1555. #ifdef GGML_GELU_QUICK_FP16
  1556. inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) {
  1557. uint16_t t;
  1558. for (int i = 0; i < n; ++i) {
  1559. ggml_fp16_t fp16 = GGML_FP32_TO_FP16(x[i]);
  1560. memcpy(&t, &fp16, sizeof(uint16_t));
  1561. y[i] = GGML_FP16_TO_FP32(ggml_table_gelu_quick_f16[t]);
  1562. }
  1563. }
  1564. #else
  1565. inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) {
  1566. for (int i = 0; i < n; ++i) {
  1567. y[i] = ggml_gelu_quick_f32(x[i]);
  1568. }
  1569. }
  1570. #endif
  1571. // Sigmoid Linear Unit (SiLU) function
  1572. inline static float ggml_silu_f32(float x) {
  1573. return x/(1.0f + expf(-x));
  1574. }
  1575. #if __FINITE_MATH_ONLY__
  1576. #error "some routines in ggml.c require non-finite math arithmetics -- pass -fno-finite-math-only to the compiler to fix"
  1577. #error "ref: https://github.com/ggerganov/llama.cpp/pull/7154#issuecomment-2143844461"
  1578. #endif
  1579. #if defined(__ARM_NEON) && defined(__aarch64__)
  1580. // adapted from arm limited optimized routine
  1581. // the maximum error is 1.45358 plus 0.5 ulps
  1582. // numbers above 88.38 will flush to infinity
  1583. // numbers beneath -103.97 will flush to zero
  1584. inline static float32x4_t ggml_v_expf(float32x4_t x) {
  1585. const float32x4_t r = vdupq_n_f32(0x1.8p23f);
  1586. const float32x4_t z = vfmaq_f32(r, x, vdupq_n_f32(0x1.715476p+0f));
  1587. const float32x4_t n = vsubq_f32(z, r);
  1588. const float32x4_t b = vfmsq_f32(vfmsq_f32(x, n, vdupq_n_f32(0x1.62e4p-1f)), n,
  1589. vdupq_n_f32(0x1.7f7d1cp-20f));
  1590. const uint32x4_t e = vshlq_n_u32(vreinterpretq_u32_f32(z), 23);
  1591. const float32x4_t k = vreinterpretq_f32_u32(vaddq_u32(e, vreinterpretq_u32_f32(vdupq_n_f32(1))));
  1592. const uint32x4_t c = vcagtq_f32(n, vdupq_n_f32(126));
  1593. const float32x4_t u = vmulq_f32(b, b);
  1594. const float32x4_t j = vfmaq_f32(
  1595. vmulq_f32(vdupq_n_f32(0x1.ffffecp-1f), b),
  1596. vfmaq_f32(vfmaq_f32(vdupq_n_f32(0x1.fffdb6p-2f), vdupq_n_f32(0x1.555e66p-3f), b),
  1597. vfmaq_f32(vdupq_n_f32(0x1.573e2ep-5f), vdupq_n_f32(0x1.0e4020p-7f), b), u), u);
  1598. if (!vpaddd_u64(vreinterpretq_u64_u32(c)))
  1599. return vfmaq_f32(k, j, k);
  1600. const uint32x4_t d = vandq_u32(vclezq_f32(n), vdupq_n_u32(0x82000000));
  1601. const float32x4_t s1 = vreinterpretq_f32_u32(vaddq_u32(d, vdupq_n_u32(0x7f000000)));
  1602. const float32x4_t s2 = vreinterpretq_f32_u32(vsubq_u32(e, d));
  1603. return vbslq_f32(vcagtq_f32(n, vdupq_n_f32(192)), vmulq_f32(s1, s1),
  1604. vbslq_f32(c, vmulq_f32(vfmaq_f32(s2, s2, j), s1), vfmaq_f32(k, k, j)));
  1605. }
  1606. // computes silu x/(1+exp(-x)) in single precision vector
  1607. inline static float32x4_t ggml_v_silu(float32x4_t x) {
  1608. const float32x4_t one = vdupq_n_f32(1.0f);
  1609. const float32x4_t zero = vdupq_n_f32(0.0f);
  1610. const float32x4_t neg_x = vsubq_f32(zero, x);
  1611. const float32x4_t exp_neg_x = ggml_v_expf(neg_x);
  1612. const float32x4_t one_plus_exp_neg_x = vaddq_f32(one, exp_neg_x);
  1613. return vdivq_f32(x, one_plus_exp_neg_x);
  1614. }
  1615. #elif defined(__AVX512F__) && defined(__AVX512DQ__)
  1616. // adapted from arm limited optimized routine
  1617. // the maximum error is 1.45358 plus 0.5 ulps
  1618. // numbers above 88.38 will flush to infinity
  1619. // numbers beneath -103.97 will flush to zero
  1620. inline static __m512 ggml_v_expf(__m512 x) {
  1621. const __m512 r = _mm512_set1_ps(0x1.8p23f);
  1622. const __m512 z = _mm512_fmadd_ps(x, _mm512_set1_ps(0x1.715476p+0f), r);
  1623. const __m512 n = _mm512_sub_ps(z, r);
  1624. const __m512 b =
  1625. _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.7f7d1cp-20f),
  1626. _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.62e4p-1f), x));
  1627. const __mmask16 d =
  1628. _mm512_cmp_ps_mask(_mm512_abs_ps(n), _mm512_set1_ps(192), _CMP_GT_OQ);
  1629. const __m512 u = _mm512_mul_ps(b, b);
  1630. const __m512 j = _mm512_fmadd_ps(
  1631. _mm512_fmadd_ps(_mm512_fmadd_ps(_mm512_set1_ps(0x1.0e4020p-7f), b,
  1632. _mm512_set1_ps(0x1.573e2ep-5f)),
  1633. u,
  1634. _mm512_fmadd_ps(_mm512_set1_ps(0x1.555e66p-3f), b,
  1635. _mm512_set1_ps(0x1.fffdb6p-2f))),
  1636. u,
  1637. _mm512_fmadd_ps(_mm512_set1_ps(0x1.ffffecp-1f), b, _mm512_set1_ps(1.0F)));
  1638. const __m512 res = _mm512_scalef_ps(j, n);
  1639. if (_mm512_kortestz(d, d))
  1640. return res;
  1641. const __m512 zero = _mm512_setzero_ps();
  1642. const __m512 alt = _mm512_mask_blend_ps(
  1643. _mm512_cmp_ps_mask(n, zero, _CMP_LE_OQ), _mm512_set1_ps(INFINITY), zero);
  1644. return _mm512_mask_blend_ps(d, res, alt);
  1645. }
  1646. // computes silu x/(1+exp(-x)) in single precision vector
  1647. inline static __m512 ggml_v_silu(__m512 x) {
  1648. const __m512 one = _mm512_set1_ps(1);
  1649. const __m512 zero = _mm512_setzero_ps();
  1650. const __m512 neg_x = _mm512_sub_ps(zero, x);
  1651. const __m512 exp_neg_x = ggml_v_expf(neg_x);
  1652. const __m512 one_plus_exp_neg_x = _mm512_add_ps(one, exp_neg_x);
  1653. return _mm512_div_ps(x, one_plus_exp_neg_x);
  1654. }
  1655. #elif defined(__AVX2__) && defined(__FMA__)
  1656. // adapted from arm limited optimized routine
  1657. // the maximum error is 1.45358 plus 0.5 ulps
  1658. // numbers above 88.38 will flush to infinity
  1659. // numbers beneath -103.97 will flush to zero
  1660. inline static __m256 ggml_v_expf(__m256 x) {
  1661. const __m256 r = _mm256_set1_ps(0x1.8p23f);
  1662. const __m256 z = _mm256_fmadd_ps(x, _mm256_set1_ps(0x1.715476p+0f), r);
  1663. const __m256 n = _mm256_sub_ps(z, r);
  1664. const __m256 b = _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.7f7d1cp-20f),
  1665. _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.62e4p-1f), x));
  1666. const __m256i e = _mm256_slli_epi32(_mm256_castps_si256(z), 23);
  1667. const __m256 k = _mm256_castsi256_ps(
  1668. _mm256_add_epi32(e, _mm256_castps_si256(_mm256_set1_ps(1))));
  1669. const __m256i c = _mm256_castps_si256(
  1670. _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n),
  1671. _mm256_set1_ps(126), _CMP_GT_OQ));
  1672. const __m256 u = _mm256_mul_ps(b, b);
  1673. const __m256 j = _mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_set1_ps(0x1.0e4020p-7f), b,
  1674. _mm256_set1_ps(0x1.573e2ep-5f)), u,
  1675. _mm256_fmadd_ps(_mm256_set1_ps(0x1.555e66p-3f), b,
  1676. _mm256_set1_ps(0x1.fffdb6p-2f))),
  1677. u, _mm256_mul_ps(_mm256_set1_ps(0x1.ffffecp-1f), b));
  1678. if (!_mm256_movemask_ps(_mm256_castsi256_ps(c)))
  1679. return _mm256_fmadd_ps(j, k, k);
  1680. const __m256i g = _mm256_and_si256(
  1681. _mm256_castps_si256(_mm256_cmp_ps(n, _mm256_setzero_ps(), _CMP_LE_OQ)),
  1682. _mm256_set1_epi32(0x82000000u));
  1683. const __m256 s1 =
  1684. _mm256_castsi256_ps(_mm256_add_epi32(g, _mm256_set1_epi32(0x7f000000u)));
  1685. const __m256 s2 = _mm256_castsi256_ps(_mm256_sub_epi32(e, g));
  1686. const __m256i d = _mm256_castps_si256(
  1687. _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n),
  1688. _mm256_set1_ps(192), _CMP_GT_OQ));
  1689. return _mm256_or_ps(
  1690. _mm256_and_ps(_mm256_castsi256_ps(d), _mm256_mul_ps(s1, s1)),
  1691. _mm256_andnot_ps(
  1692. _mm256_castsi256_ps(d),
  1693. _mm256_or_ps(
  1694. _mm256_and_ps(_mm256_castsi256_ps(c),
  1695. _mm256_mul_ps(_mm256_fmadd_ps(s2, j, s2), s1)),
  1696. _mm256_andnot_ps(_mm256_castsi256_ps(c), _mm256_fmadd_ps(k, j, k)))));
  1697. }
  1698. // computes silu x/(1+exp(-x)) in single precision vector
  1699. inline static __m256 ggml_v_silu(__m256 x) {
  1700. const __m256 one = _mm256_set1_ps(1);
  1701. const __m256 zero = _mm256_setzero_ps();
  1702. const __m256 neg_x = _mm256_sub_ps(zero, x);
  1703. const __m256 exp_neg_x = ggml_v_expf(neg_x);
  1704. const __m256 one_plus_exp_neg_x = _mm256_add_ps(one, exp_neg_x);
  1705. return _mm256_div_ps(x, one_plus_exp_neg_x);
  1706. }
  1707. #elif defined(__SSE2__) // __AVX2__ / __ARM_NEON
  1708. #if defined(__FMA__)
  1709. #define MADD128(x, y, z) _mm_fmadd_ps(x, y, z)
  1710. #define NMADD128(x, y, z) _mm_fnmadd_ps(x, y, z)
  1711. #else
  1712. #define MADD128(x, y, z) _mm_add_ps(_mm_mul_ps(x, y), z)
  1713. #define NMADD128(x, y, z) _mm_sub_ps(z, _mm_mul_ps(x, y))
  1714. #endif
  1715. // adapted from arm limited optimized routine
  1716. // the maximum error is 1.45358 plus 0.5 ulps
  1717. // numbers above 88.38 will flush to infinity
  1718. // numbers beneath -103.97 will flush to zero
  1719. inline static __m128 ggml_v_expf(__m128 x) {
  1720. const __m128 r = _mm_set1_ps(0x1.8p23f);
  1721. const __m128 z = MADD128(x, _mm_set1_ps(0x1.715476p+0f), r);
  1722. const __m128 n = _mm_sub_ps(z, r);
  1723. const __m128 b =
  1724. NMADD128(n, _mm_set1_ps(0x1.7f7d1cp-20f), NMADD128(n, _mm_set1_ps(0x1.62e4p-1f), x));
  1725. const __m128i e = _mm_slli_epi32(_mm_castps_si128(z), 23);
  1726. const __m128 k = _mm_castsi128_ps(_mm_add_epi32(e, _mm_castps_si128(_mm_set1_ps(1))));
  1727. const __m128i c =
  1728. _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(126)));
  1729. const __m128 u = _mm_mul_ps(b, b);
  1730. const __m128 j =
  1731. MADD128(MADD128(MADD128(_mm_set1_ps(0x1.0e4020p-7f), b, _mm_set1_ps(0x1.573e2ep-5f)), u,
  1732. MADD128(_mm_set1_ps(0x1.555e66p-3f), b, _mm_set1_ps(0x1.fffdb6p-2f))),
  1733. u, _mm_mul_ps(_mm_set1_ps(0x1.ffffecp-1f), b));
  1734. if (!_mm_movemask_epi8(c))
  1735. return MADD128(j, k, k);
  1736. const __m128i g = _mm_and_si128(_mm_castps_si128(_mm_cmple_ps(n, _mm_setzero_ps())),
  1737. _mm_set1_epi32(0x82000000u));
  1738. const __m128 s1 = _mm_castsi128_ps(_mm_add_epi32(g, _mm_set1_epi32(0x7f000000u)));
  1739. const __m128 s2 = _mm_castsi128_ps(_mm_sub_epi32(e, g));
  1740. const __m128i d =
  1741. _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(192)));
  1742. return _mm_or_ps(
  1743. _mm_and_ps(_mm_castsi128_ps(d), _mm_mul_ps(s1, s1)),
  1744. _mm_andnot_ps(_mm_castsi128_ps(d),
  1745. _mm_or_ps(_mm_and_ps(_mm_castsi128_ps(c), _mm_mul_ps(MADD128(s2, j, s2), s1)),
  1746. _mm_andnot_ps(_mm_castsi128_ps(c), MADD128(k, j, k)))));
  1747. }
  1748. // computes silu x/(1+exp(-x)) in single precision vector
  1749. inline static __m128 ggml_v_silu(__m128 x) {
  1750. const __m128 one = _mm_set1_ps(1);
  1751. const __m128 zero = _mm_setzero_ps();
  1752. const __m128 neg_x = _mm_sub_ps(zero, x);
  1753. const __m128 exp_neg_x = ggml_v_expf(neg_x);
  1754. const __m128 one_plus_exp_neg_x = _mm_add_ps(one, exp_neg_x);
  1755. return _mm_div_ps(x, one_plus_exp_neg_x);
  1756. }
  1757. #endif // __ARM_NEON / __AVX2__ / __SSE2__
  1758. static void ggml_vec_silu_f32(const int n, float * y, const float * x) {
  1759. int i = 0;
  1760. #if defined(__AVX512F__) && defined(__AVX512DQ__)
  1761. for (; i + 15 < n; i += 16) {
  1762. _mm512_storeu_ps(y + i, ggml_v_silu(_mm512_loadu_ps(x + i)));
  1763. }
  1764. #elif defined(__AVX2__) && defined(__FMA__)
  1765. for (; i + 7 < n; i += 8) {
  1766. _mm256_storeu_ps(y + i, ggml_v_silu(_mm256_loadu_ps(x + i)));
  1767. }
  1768. #elif defined(__SSE2__)
  1769. for (; i + 3 < n; i += 4) {
  1770. _mm_storeu_ps(y + i, ggml_v_silu(_mm_loadu_ps(x + i)));
  1771. }
  1772. #elif defined(__ARM_NEON) && defined(__aarch64__)
  1773. for (; i + 3 < n; i += 4) {
  1774. vst1q_f32(y + i, ggml_v_silu(vld1q_f32(x + i)));
  1775. }
  1776. #endif
  1777. for (; i < n; ++i) {
  1778. y[i] = ggml_silu_f32(x[i]);
  1779. }
  1780. }
  1781. static ggml_float ggml_vec_soft_max_f32(const int n, float * y, const float * x, float max) {
  1782. int i = 0;
  1783. ggml_float sum = 0;
  1784. #if defined(__AVX512F__) && defined(__AVX512DQ__)
  1785. for (; i + 15 < n; i += 16) {
  1786. __m512 val = ggml_v_expf(_mm512_sub_ps(_mm512_loadu_ps(x + i),
  1787. _mm512_set1_ps(max)));
  1788. _mm512_storeu_ps(y + i, val);
  1789. sum += (ggml_float)_mm512_reduce_add_ps(val);
  1790. }
  1791. #elif defined(__AVX2__) && defined(__FMA__)
  1792. for (; i + 7 < n; i += 8) {
  1793. __m256 val = ggml_v_expf(_mm256_sub_ps(_mm256_loadu_ps(x + i),
  1794. _mm256_set1_ps(max)));
  1795. _mm256_storeu_ps(y + i, val);
  1796. __m128 val2 = _mm_add_ps(_mm256_extractf128_ps(val, 1),
  1797. _mm256_castps256_ps128(val));
  1798. val2 = _mm_add_ps(val2, _mm_movehl_ps(val2, val2));
  1799. val2 = _mm_add_ss(val2, _mm_movehdup_ps(val2));
  1800. sum += (ggml_float)_mm_cvtss_f32(val2);
  1801. }
  1802. #elif defined(__SSE2__)
  1803. for (; i + 3 < n; i += 4) {
  1804. __m128 val = ggml_v_expf(_mm_sub_ps(_mm_loadu_ps(x + i),
  1805. _mm_set1_ps(max)));
  1806. _mm_storeu_ps(y + i, val);
  1807. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__)
  1808. val = _mm_add_ps(val, _mm_movehl_ps(val, val));
  1809. val = _mm_add_ss(val, _mm_movehdup_ps(val));
  1810. #else
  1811. __m128 tmp = _mm_shuffle_ps(val, val, _MM_SHUFFLE(2, 3, 0, 1));
  1812. val = _mm_add_ps(val, tmp);
  1813. tmp = _mm_movehl_ps(tmp, val);
  1814. val = _mm_add_ss(val, tmp);
  1815. #endif
  1816. sum += (ggml_float)_mm_cvtss_f32(val);
  1817. }
  1818. #elif defined(__ARM_NEON) && defined(__aarch64__)
  1819. for (; i + 3 < n; i += 4) {
  1820. float32x4_t val = ggml_v_expf(vsubq_f32(vld1q_f32(x + i),
  1821. vdupq_n_f32(max)));
  1822. vst1q_f32(y + i, val);
  1823. sum += (ggml_float)vaddvq_f32(val);
  1824. }
  1825. #endif
  1826. for (; i < n; ++i) {
  1827. float val = expf(x[i] - max);
  1828. sum += (ggml_float)val;
  1829. y[i] = val;
  1830. }
  1831. return sum;
  1832. }
  1833. static ggml_float ggml_vec_log_soft_max_f32(const int n, float * y, const float * x, float max) {
  1834. // 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)
  1835. int i = 0;
  1836. ggml_float sum = 0;
  1837. for (; i < n; ++i) {
  1838. float val = x[i] - max;
  1839. y[i] = val;
  1840. sum += (ggml_float)expf(val);
  1841. }
  1842. return sum = (ggml_float)logf(sum);
  1843. }
  1844. inline static float ggml_silu_backward_f32(float x, float dy) {
  1845. const float s = 1.0f/(1.0f + expf(-x));
  1846. return dy*s*(1.0f + x*(1.0f - s));
  1847. }
  1848. inline static void ggml_vec_silu_backward_f32(const int n, float * dx, const float * x, const float * dy) {
  1849. for (int i = 0; i < n; ++i) {
  1850. dx[i] = ggml_silu_backward_f32(x[i], dy[i]);
  1851. }
  1852. }
  1853. inline static void ggml_vec_sum_f32(const int n, float * s, const float * x) {
  1854. #ifndef GGML_USE_ACCELERATE
  1855. ggml_float sum = 0.0;
  1856. for (int i = 0; i < n; ++i) {
  1857. sum += (ggml_float)x[i];
  1858. }
  1859. *s = sum;
  1860. #else
  1861. vDSP_sve(x, 1, s, n);
  1862. #endif
  1863. }
  1864. inline static void ggml_vec_sum_f32_ggf(const int n, ggml_float * s, const float * x) {
  1865. ggml_float sum = 0.0;
  1866. for (int i = 0; i < n; ++i) {
  1867. sum += (ggml_float)x[i];
  1868. }
  1869. *s = sum;
  1870. }
  1871. inline static void ggml_vec_sum_f16_ggf(const int n, float * s, const ggml_fp16_t * x) {
  1872. float sum = 0.0f;
  1873. for (int i = 0; i < n; ++i) {
  1874. sum += GGML_FP16_TO_FP32(x[i]);
  1875. }
  1876. *s = sum;
  1877. }
  1878. inline static void ggml_vec_sum_bf16_ggf(const int n, float * s, const ggml_bf16_t * x) {
  1879. float sum = 0.0f;
  1880. for (int i = 0; i < n; ++i) {
  1881. sum += GGML_BF16_TO_FP32(x[i]);
  1882. }
  1883. *s = sum;
  1884. }
  1885. inline static void ggml_vec_max_f32(const int n, float * s, const float * x) {
  1886. #ifndef GGML_USE_ACCELERATE
  1887. float max = -INFINITY;
  1888. for (int i = 0; i < n; ++i) {
  1889. max = MAX(max, x[i]);
  1890. }
  1891. *s = max;
  1892. #else
  1893. vDSP_maxv(x, 1, s, n);
  1894. #endif
  1895. }
  1896. inline static void ggml_vec_norm_inv_f32(const int n, float * s, const float * x) {
  1897. ggml_vec_norm_f32(n, s, x);
  1898. *s = 1.f/(*s);
  1899. }
  1900. inline static void ggml_vec_argmax_f32(const int n, int * s, const float * x) {
  1901. float max = -INFINITY;
  1902. int idx = 0;
  1903. for (int i = 0; i < n; ++i) {
  1904. max = MAX(max, x[i]);
  1905. if (max == x[i]) { idx = i; }
  1906. }
  1907. *s = idx;
  1908. }
  1909. // Helpers for polling loops
  1910. #if defined(__aarch64__) && ( defined(__clang__) || defined(__GNUC__) )
  1911. static inline void ggml_thread_cpu_relax(void) {
  1912. __asm__ volatile("yield" ::: "memory");
  1913. }
  1914. #elif defined(__x86_64__)
  1915. static inline void ggml_thread_cpu_relax(void) {
  1916. _mm_pause();
  1917. }
  1918. #else
  1919. static inline void ggml_thread_cpu_relax(void) {;}
  1920. #endif
  1921. //
  1922. // NUMA support
  1923. //
  1924. #define GGML_NUMA_MAX_NODES 8
  1925. #define GGML_NUMA_MAX_CPUS 512
  1926. struct ggml_numa_node {
  1927. uint32_t cpus[GGML_NUMA_MAX_CPUS]; // hardware threads on this node
  1928. uint32_t n_cpus;
  1929. };
  1930. struct ggml_numa_nodes {
  1931. enum ggml_numa_strategy numa_strategy;
  1932. struct ggml_numa_node nodes[GGML_NUMA_MAX_NODES];
  1933. uint32_t n_nodes;
  1934. uint32_t total_cpus; // hardware threads on system
  1935. uint32_t current_node; // node on which main process is execting
  1936. #if defined(__gnu_linux__)
  1937. cpu_set_t cpuset; // cpuset from numactl
  1938. #else
  1939. uint32_t cpuset; // no NUMA support outside of Linux at this time. Use a portable datatype
  1940. #endif
  1941. };
  1942. //
  1943. // ggml state
  1944. //
  1945. struct ggml_state {
  1946. struct ggml_numa_nodes numa;
  1947. };
  1948. static struct ggml_state g_state = {0};
  1949. void ggml_barrier(struct ggml_threadpool * tp) {
  1950. int n_threads = atomic_load_explicit(&tp->n_threads_cur, memory_order_relaxed);
  1951. if (n_threads == 1) {
  1952. return;
  1953. }
  1954. #ifdef GGML_USE_OPENMP
  1955. #pragma omp barrier
  1956. #else
  1957. int n_passed = atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed);
  1958. // enter barrier (full seq-cst fence)
  1959. int n_barrier = atomic_fetch_add_explicit(&tp->n_barrier, 1, memory_order_seq_cst);
  1960. if (n_barrier == (n_threads - 1)) {
  1961. // last thread
  1962. atomic_store_explicit(&tp->n_barrier, 0, memory_order_relaxed);
  1963. // exit barrier (fill seq-cst fence)
  1964. atomic_fetch_add_explicit(&tp->n_barrier_passed, 1, memory_order_seq_cst);
  1965. return;
  1966. }
  1967. // wait for other threads
  1968. while (atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed) == n_passed) {
  1969. ggml_thread_cpu_relax();
  1970. }
  1971. // exit barrier (full seq-cst fence)
  1972. // TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
  1973. #ifdef GGML_TSAN_ENABLED
  1974. atomic_fetch_add_explicit(&tp->n_barrier_passed, 0, memory_order_seq_cst);
  1975. #else
  1976. atomic_thread_fence(memory_order_seq_cst);
  1977. #endif
  1978. #endif
  1979. }
  1980. #if defined(__gnu_linux__)
  1981. static cpu_set_t ggml_get_numa_affinity(void) {
  1982. cpu_set_t cpuset;
  1983. pthread_t thread;
  1984. thread = pthread_self();
  1985. CPU_ZERO(&cpuset);
  1986. pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
  1987. return cpuset;
  1988. }
  1989. #else
  1990. static uint32_t ggml_get_numa_affinity(void) {
  1991. return 0; // no NUMA support
  1992. }
  1993. #endif
  1994. void ggml_numa_init(enum ggml_numa_strategy numa_flag) {
  1995. if (g_state.numa.n_nodes > 0) {
  1996. fprintf(stderr, "ggml_numa_init: NUMA already initialized\n");
  1997. return;
  1998. }
  1999. #if defined(__gnu_linux__)
  2000. struct stat st;
  2001. char path[256];
  2002. int rv;
  2003. // set numa scheme
  2004. g_state.numa.numa_strategy = numa_flag;
  2005. GGML_PRINT_DEBUG("numa strategy %u\n",g_state.numa.numa_strategy);
  2006. g_state.numa.cpuset = ggml_get_numa_affinity();
  2007. // enumerate nodes
  2008. while (g_state.numa.n_nodes < GGML_NUMA_MAX_NODES) {
  2009. rv = snprintf(path, sizeof(path), "/sys/devices/system/node/node%u", g_state.numa.n_nodes);
  2010. GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
  2011. if (stat(path, &st) != 0) { break; }
  2012. ++g_state.numa.n_nodes;
  2013. }
  2014. // enumerate CPUs
  2015. while (g_state.numa.total_cpus < GGML_NUMA_MAX_CPUS) {
  2016. rv = snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u", g_state.numa.total_cpus);
  2017. GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
  2018. if (stat(path, &st) != 0) { break; }
  2019. ++g_state.numa.total_cpus;
  2020. }
  2021. GGML_PRINT_DEBUG("found %u numa nodes, %u CPUs\n", g_state.numa.n_nodes, g_state.numa.total_cpus);
  2022. // figure out which node we're on
  2023. uint current_cpu;
  2024. int getcpu_ret = 0;
  2025. #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 33) || defined(__COSMOPOLITAN__)
  2026. getcpu_ret = getcpu(&current_cpu, &g_state.numa.current_node);
  2027. #else
  2028. // old glibc doesn't have a wrapper for this call. Fall back on direct syscall
  2029. # if !defined(SYS_getcpu) && defined(SYS_get_cpu)
  2030. # define SYS_getcpu SYS_get_cpu // some older glibc versions use this name
  2031. # endif
  2032. getcpu_ret = syscall(SYS_getcpu, &current_cpu, &g_state.numa.current_node);
  2033. #endif
  2034. if (g_state.numa.n_nodes < 1 || g_state.numa.total_cpus < 1 || getcpu_ret != 0) {
  2035. g_state.numa.n_nodes = 0;
  2036. return;
  2037. }
  2038. GGML_PRINT_DEBUG("found our process on numa node %u, CPU %u\n", g_state.numa.current_node, current_cpu);
  2039. for (uint32_t n = 0; n < g_state.numa.n_nodes; ++n) {
  2040. struct ggml_numa_node * node = &g_state.numa.nodes[n];
  2041. GGML_PRINT_DEBUG("CPUs on node %u:", n);
  2042. node->n_cpus = 0;
  2043. for (uint32_t c = 0; c < g_state.numa.total_cpus; ++c) {
  2044. rv = snprintf(path, sizeof(path), "/sys/devices/system/node/node%u/cpu%u", n, c);
  2045. GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
  2046. if (stat(path, &st) == 0) {
  2047. node->cpus[node->n_cpus++] = c;
  2048. GGML_PRINT_DEBUG(" %u", c);
  2049. }
  2050. }
  2051. GGML_PRINT_DEBUG("\n");
  2052. }
  2053. if (ggml_is_numa()) {
  2054. FILE *fptr = fopen("/proc/sys/kernel/numa_balancing", "r");
  2055. if (fptr != NULL) {
  2056. char buf[42];
  2057. if (fgets(buf, sizeof(buf), fptr) && strncmp(buf, "0\n", sizeof(buf)) != 0) {
  2058. GGML_LOG_WARN("/proc/sys/kernel/numa_balancing is enabled, this has been observed to impair performance\n");
  2059. }
  2060. fclose(fptr);
  2061. }
  2062. }
  2063. #else
  2064. UNUSED(numa_flag);
  2065. // TODO
  2066. #endif
  2067. }
  2068. bool ggml_is_numa(void) {
  2069. return g_state.numa.n_nodes > 1;
  2070. }
  2071. #if defined(__ARM_ARCH)
  2072. #if defined(__linux__) && defined(__aarch64__)
  2073. #include <sys/auxv.h>
  2074. #elif defined(__APPLE__)
  2075. #include <sys/sysctl.h>
  2076. #endif
  2077. #if !defined(HWCAP2_I8MM)
  2078. #define HWCAP2_I8MM (1 << 13)
  2079. #endif
  2080. static void ggml_init_arm_arch_features(void) {
  2081. #if defined(__linux__) && defined(__aarch64__)
  2082. uint32_t hwcap = getauxval(AT_HWCAP);
  2083. uint32_t hwcap2 = getauxval(AT_HWCAP2);
  2084. ggml_arm_arch_features.has_neon = !!(hwcap & HWCAP_ASIMD);
  2085. ggml_arm_arch_features.has_dotprod = !!(hwcap & HWCAP_ASIMDDP);
  2086. ggml_arm_arch_features.has_i8mm = !!(hwcap2 & HWCAP2_I8MM);
  2087. ggml_arm_arch_features.has_sve = !!(hwcap & HWCAP_SVE);
  2088. #if defined(__ARM_FEATURE_SVE)
  2089. ggml_arm_arch_features.sve_cnt = PR_SVE_VL_LEN_MASK & prctl(PR_SVE_GET_VL);
  2090. #endif
  2091. #elif defined(__APPLE__)
  2092. int oldp = 0;
  2093. size_t size = sizeof(oldp);
  2094. if (sysctlbyname("hw.optional.AdvSIMD", &oldp, &size, NULL, 0) != 0) {
  2095. oldp = 0;
  2096. }
  2097. ggml_arm_arch_features.has_neon = oldp;
  2098. if (sysctlbyname("hw.optional.arm.FEAT_DotProd", &oldp, &size, NULL, 0) != 0) {
  2099. oldp = 0;
  2100. }
  2101. ggml_arm_arch_features.has_dotprod = oldp;
  2102. if (sysctlbyname("hw.optional.arm.FEAT_I8MM", &oldp, &size, NULL, 0) != 0) {
  2103. oldp = 0;
  2104. }
  2105. ggml_arm_arch_features.has_i8mm = oldp;
  2106. ggml_arm_arch_features.has_sve = 0;
  2107. ggml_arm_arch_features.sve_cnt = 0;
  2108. #else
  2109. // Run-time CPU feature detection not implemented for this platform, fallback to compile time
  2110. #if defined(__ARM_NEON)
  2111. ggml_arm_arch_features.has_neon = 1;
  2112. #else
  2113. ggml_arm_arch_features.has_neon = 0;
  2114. #endif
  2115. #if defined(__ARM_FEATURE_MATMUL_INT8)
  2116. ggml_arm_arch_features.has_i8mm = 1;
  2117. #else
  2118. ggml_arm_arch_features.has_i8mm = 0;
  2119. #endif
  2120. #if defined(__ARM_FEATURE_SVE)
  2121. ggml_arm_arch_features.has_sve = 1;
  2122. ggml_arm_arch_features.sve_cnt = 16;
  2123. #else
  2124. ggml_arm_arch_features.has_sve = 0;
  2125. ggml_arm_arch_features.sve_cnt = 0;
  2126. #endif
  2127. #endif
  2128. }
  2129. #endif
  2130. struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value) {
  2131. GGML_ASSERT(!ggml_get_no_alloc(ctx));
  2132. struct ggml_tensor * result = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 1);
  2133. ggml_set_i32(result, value);
  2134. return result;
  2135. }
  2136. struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value) {
  2137. GGML_ASSERT(!ggml_get_no_alloc(ctx));
  2138. struct ggml_tensor * result = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
  2139. ggml_set_f32(result, value);
  2140. return result;
  2141. }
  2142. struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value) {
  2143. const int n = ggml_nrows(tensor);
  2144. const int nc = tensor->ne[0];
  2145. const size_t n1 = tensor->nb[1];
  2146. char * const data = tensor->data;
  2147. switch (tensor->type) {
  2148. case GGML_TYPE_I8:
  2149. {
  2150. assert(tensor->nb[0] == sizeof(int8_t));
  2151. for (int i = 0; i < n; i++) {
  2152. ggml_vec_set_i8(nc, (int8_t *)(data + i*n1), value);
  2153. }
  2154. } break;
  2155. case GGML_TYPE_I16:
  2156. {
  2157. assert(tensor->nb[0] == sizeof(int16_t));
  2158. for (int i = 0; i < n; i++) {
  2159. ggml_vec_set_i16(nc, (int16_t *)(data + i*n1), value);
  2160. }
  2161. } break;
  2162. case GGML_TYPE_I32:
  2163. {
  2164. assert(tensor->nb[0] == sizeof(int32_t));
  2165. for (int i = 0; i < n; i++) {
  2166. ggml_vec_set_i32(nc, (int32_t *)(data + i*n1), value);
  2167. }
  2168. } break;
  2169. case GGML_TYPE_F16:
  2170. {
  2171. assert(tensor->nb[0] == sizeof(ggml_fp16_t));
  2172. for (int i = 0; i < n; i++) {
  2173. ggml_vec_set_f16(nc, (ggml_fp16_t *)(data + i*n1), GGML_FP32_TO_FP16(value));
  2174. }
  2175. } break;
  2176. case GGML_TYPE_BF16:
  2177. {
  2178. assert(tensor->nb[0] == sizeof(ggml_fp16_t));
  2179. for (int i = 0; i < n; i++) {
  2180. ggml_vec_set_bf16(nc, (ggml_bf16_t *)(data + i*n1), GGML_FP32_TO_BF16(value));
  2181. }
  2182. } break;
  2183. case GGML_TYPE_F32:
  2184. {
  2185. assert(tensor->nb[0] == sizeof(float));
  2186. for (int i = 0; i < n; i++) {
  2187. ggml_vec_set_f32(nc, (float *)(data + i*n1), value);
  2188. }
  2189. } break;
  2190. default:
  2191. {
  2192. GGML_ABORT("fatal error");
  2193. }
  2194. }
  2195. return tensor;
  2196. }
  2197. struct ggml_tensor * ggml_set_f32(struct ggml_tensor * tensor, float value) {
  2198. const int n = ggml_nrows(tensor);
  2199. const int nc = tensor->ne[0];
  2200. const size_t n1 = tensor->nb[1];
  2201. char * const data = tensor->data;
  2202. switch (tensor->type) {
  2203. case GGML_TYPE_I8:
  2204. {
  2205. assert(tensor->nb[0] == sizeof(int8_t));
  2206. for (int i = 0; i < n; i++) {
  2207. ggml_vec_set_i8(nc, (int8_t *)(data + i*n1), value);
  2208. }
  2209. } break;
  2210. case GGML_TYPE_I16:
  2211. {
  2212. assert(tensor->nb[0] == sizeof(int16_t));
  2213. for (int i = 0; i < n; i++) {
  2214. ggml_vec_set_i16(nc, (int16_t *)(data + i*n1), value);
  2215. }
  2216. } break;
  2217. case GGML_TYPE_I32:
  2218. {
  2219. assert(tensor->nb[0] == sizeof(int32_t));
  2220. for (int i = 0; i < n; i++) {
  2221. ggml_vec_set_i32(nc, (int32_t *)(data + i*n1), value);
  2222. }
  2223. } break;
  2224. case GGML_TYPE_F16:
  2225. {
  2226. assert(tensor->nb[0] == sizeof(ggml_fp16_t));
  2227. for (int i = 0; i < n; i++) {
  2228. ggml_vec_set_f16(nc, (ggml_fp16_t *)(data + i*n1), GGML_FP32_TO_FP16(value));
  2229. }
  2230. } break;
  2231. case GGML_TYPE_BF16:
  2232. {
  2233. assert(tensor->nb[0] == sizeof(ggml_bf16_t));
  2234. for (int i = 0; i < n; i++) {
  2235. ggml_vec_set_bf16(nc, (ggml_bf16_t *)(data + i*n1), GGML_FP32_TO_BF16(value));
  2236. }
  2237. } break;
  2238. case GGML_TYPE_F32:
  2239. {
  2240. assert(tensor->nb[0] == sizeof(float));
  2241. for (int i = 0; i < n; i++) {
  2242. ggml_vec_set_f32(nc, (float *)(data + i*n1), value);
  2243. }
  2244. } break;
  2245. default:
  2246. {
  2247. GGML_ABORT("fatal error");
  2248. }
  2249. }
  2250. return tensor;
  2251. }
  2252. int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i) {
  2253. if (!ggml_is_contiguous(tensor)) {
  2254. int64_t id[4] = { 0, 0, 0, 0 };
  2255. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2256. return ggml_get_i32_nd(tensor, id[0], id[1], id[2], id[3]);
  2257. }
  2258. switch (tensor->type) {
  2259. case GGML_TYPE_I8:
  2260. {
  2261. GGML_ASSERT(tensor->nb[0] == sizeof(int8_t));
  2262. return ((int8_t *)(tensor->data))[i];
  2263. }
  2264. case GGML_TYPE_I16:
  2265. {
  2266. GGML_ASSERT(tensor->nb[0] == sizeof(int16_t));
  2267. return ((int16_t *)(tensor->data))[i];
  2268. }
  2269. case GGML_TYPE_I32:
  2270. {
  2271. GGML_ASSERT(tensor->nb[0] == sizeof(int32_t));
  2272. return ((int32_t *)(tensor->data))[i];
  2273. }
  2274. case GGML_TYPE_F16:
  2275. {
  2276. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t));
  2277. return GGML_FP16_TO_FP32(((ggml_fp16_t *)(tensor->data))[i]);
  2278. }
  2279. case GGML_TYPE_BF16:
  2280. {
  2281. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_bf16_t));
  2282. return GGML_BF16_TO_FP32(((ggml_bf16_t *)(tensor->data))[i]);
  2283. }
  2284. case GGML_TYPE_F32:
  2285. {
  2286. GGML_ASSERT(tensor->nb[0] == sizeof(float));
  2287. return ((float *)(tensor->data))[i];
  2288. }
  2289. default:
  2290. {
  2291. GGML_ABORT("fatal error");
  2292. }
  2293. }
  2294. }
  2295. void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value) {
  2296. if (!ggml_is_contiguous(tensor)) {
  2297. int64_t id[4] = { 0, 0, 0, 0 };
  2298. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2299. ggml_set_i32_nd(tensor, id[0], id[1], id[2], id[3], value);
  2300. return;
  2301. }
  2302. switch (tensor->type) {
  2303. case GGML_TYPE_I8:
  2304. {
  2305. GGML_ASSERT(tensor->nb[0] == sizeof(int8_t));
  2306. ((int8_t *)(tensor->data))[i] = value;
  2307. } break;
  2308. case GGML_TYPE_I16:
  2309. {
  2310. GGML_ASSERT(tensor->nb[0] == sizeof(int16_t));
  2311. ((int16_t *)(tensor->data))[i] = value;
  2312. } break;
  2313. case GGML_TYPE_I32:
  2314. {
  2315. GGML_ASSERT(tensor->nb[0] == sizeof(int32_t));
  2316. ((int32_t *)(tensor->data))[i] = value;
  2317. } break;
  2318. case GGML_TYPE_F16:
  2319. {
  2320. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t));
  2321. ((ggml_fp16_t *)(tensor->data))[i] = GGML_FP32_TO_FP16(value);
  2322. } break;
  2323. case GGML_TYPE_BF16:
  2324. {
  2325. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_bf16_t));
  2326. ((ggml_bf16_t *)(tensor->data))[i] = GGML_FP32_TO_BF16(value);
  2327. } break;
  2328. case GGML_TYPE_F32:
  2329. {
  2330. GGML_ASSERT(tensor->nb[0] == sizeof(float));
  2331. ((float *)(tensor->data))[i] = value;
  2332. } break;
  2333. default:
  2334. {
  2335. GGML_ABORT("fatal error");
  2336. }
  2337. }
  2338. }
  2339. int32_t ggml_get_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  2340. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2341. switch (tensor->type) {
  2342. case GGML_TYPE_I8:
  2343. return ((int8_t *) data)[0];
  2344. case GGML_TYPE_I16:
  2345. return ((int16_t *) data)[0];
  2346. case GGML_TYPE_I32:
  2347. return ((int32_t *) data)[0];
  2348. case GGML_TYPE_F16:
  2349. return GGML_FP16_TO_FP32(((ggml_fp16_t *) data)[0]);
  2350. case GGML_TYPE_BF16:
  2351. return GGML_BF16_TO_FP32(((ggml_bf16_t *) data)[0]);
  2352. case GGML_TYPE_F32:
  2353. return ((float *) data)[0];
  2354. default:
  2355. GGML_ABORT("fatal error");
  2356. }
  2357. }
  2358. void ggml_set_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, int32_t value) {
  2359. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2360. switch (tensor->type) {
  2361. case GGML_TYPE_I8:
  2362. {
  2363. ((int8_t *)(data))[0] = value;
  2364. } break;
  2365. case GGML_TYPE_I16:
  2366. {
  2367. ((int16_t *)(data))[0] = value;
  2368. } break;
  2369. case GGML_TYPE_I32:
  2370. {
  2371. ((int32_t *)(data))[0] = value;
  2372. } break;
  2373. case GGML_TYPE_F16:
  2374. {
  2375. ((ggml_fp16_t *)(data))[0] = GGML_FP32_TO_FP16(value);
  2376. } break;
  2377. case GGML_TYPE_BF16:
  2378. {
  2379. ((ggml_bf16_t *)(data))[0] = GGML_FP32_TO_BF16(value);
  2380. } break;
  2381. case GGML_TYPE_F32:
  2382. {
  2383. ((float *)(data))[0] = value;
  2384. } break;
  2385. default:
  2386. {
  2387. GGML_ABORT("fatal error");
  2388. }
  2389. }
  2390. }
  2391. float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i) {
  2392. if (!ggml_is_contiguous(tensor)) {
  2393. int64_t id[4] = { 0, 0, 0, 0 };
  2394. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2395. return ggml_get_f32_nd(tensor, id[0], id[1], id[2], id[3]);
  2396. }
  2397. switch (tensor->type) {
  2398. case GGML_TYPE_I8:
  2399. {
  2400. return ((int8_t *)(tensor->data))[i];
  2401. }
  2402. case GGML_TYPE_I16:
  2403. {
  2404. return ((int16_t *)(tensor->data))[i];
  2405. }
  2406. case GGML_TYPE_I32:
  2407. {
  2408. return ((int32_t *)(tensor->data))[i];
  2409. }
  2410. case GGML_TYPE_F16:
  2411. {
  2412. return GGML_FP16_TO_FP32(((ggml_fp16_t *)(tensor->data))[i]);
  2413. }
  2414. case GGML_TYPE_BF16:
  2415. {
  2416. return GGML_BF16_TO_FP32(((ggml_bf16_t *)(tensor->data))[i]);
  2417. }
  2418. case GGML_TYPE_F32:
  2419. {
  2420. return ((float *)(tensor->data))[i];
  2421. }
  2422. default:
  2423. {
  2424. GGML_ABORT("fatal error");
  2425. }
  2426. }
  2427. }
  2428. void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value) {
  2429. if (!ggml_is_contiguous(tensor)) {
  2430. int64_t id[4] = { 0, 0, 0, 0 };
  2431. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2432. ggml_set_f32_nd(tensor, id[0], id[1], id[2], id[3], value);
  2433. return;
  2434. }
  2435. switch (tensor->type) {
  2436. case GGML_TYPE_I8:
  2437. {
  2438. ((int8_t *)(tensor->data))[i] = value;
  2439. } break;
  2440. case GGML_TYPE_I16:
  2441. {
  2442. ((int16_t *)(tensor->data))[i] = value;
  2443. } break;
  2444. case GGML_TYPE_I32:
  2445. {
  2446. ((int32_t *)(tensor->data))[i] = value;
  2447. } break;
  2448. case GGML_TYPE_F16:
  2449. {
  2450. ((ggml_fp16_t *)(tensor->data))[i] = GGML_FP32_TO_FP16(value);
  2451. } break;
  2452. case GGML_TYPE_BF16:
  2453. {
  2454. ((ggml_bf16_t *)(tensor->data))[i] = GGML_FP32_TO_BF16(value);
  2455. } break;
  2456. case GGML_TYPE_F32:
  2457. {
  2458. ((float *)(tensor->data))[i] = value;
  2459. } break;
  2460. default:
  2461. {
  2462. GGML_ABORT("fatal error");
  2463. }
  2464. }
  2465. }
  2466. float ggml_get_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  2467. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2468. switch (tensor->type) {
  2469. case GGML_TYPE_I8:
  2470. return ((int8_t *) data)[0];
  2471. case GGML_TYPE_I16:
  2472. return ((int16_t *) data)[0];
  2473. case GGML_TYPE_I32:
  2474. return ((int32_t *) data)[0];
  2475. case GGML_TYPE_F16:
  2476. return GGML_FP16_TO_FP32(((ggml_fp16_t *) data)[0]);
  2477. case GGML_TYPE_BF16:
  2478. return GGML_BF16_TO_FP32(((ggml_bf16_t *) data)[0]);
  2479. case GGML_TYPE_F32:
  2480. return ((float *) data)[0];
  2481. default:
  2482. GGML_ABORT("fatal error");
  2483. }
  2484. }
  2485. void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, float value) {
  2486. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2487. switch (tensor->type) {
  2488. case GGML_TYPE_I8:
  2489. {
  2490. ((int8_t *)(data))[0] = value;
  2491. } break;
  2492. case GGML_TYPE_I16:
  2493. {
  2494. ((int16_t *)(data))[0] = value;
  2495. } break;
  2496. case GGML_TYPE_I32:
  2497. {
  2498. ((int32_t *)(data))[0] = value;
  2499. } break;
  2500. case GGML_TYPE_F16:
  2501. {
  2502. ((ggml_fp16_t *)(data))[0] = GGML_FP32_TO_FP16(value);
  2503. } break;
  2504. case GGML_TYPE_BF16:
  2505. {
  2506. ((ggml_bf16_t *)(data))[0] = GGML_FP32_TO_BF16(value);
  2507. } break;
  2508. case GGML_TYPE_F32:
  2509. {
  2510. ((float *)(data))[0] = value;
  2511. } break;
  2512. default:
  2513. {
  2514. GGML_ABORT("fatal error");
  2515. }
  2516. }
  2517. }
  2518. ////////////////////////////////////////////////////////////////////////////////
  2519. // ggml_compute_forward_dup
  2520. static void ggml_compute_forward_dup_same_cont(
  2521. const struct ggml_compute_params * params,
  2522. struct ggml_tensor * dst) {
  2523. const struct ggml_tensor * src0 = dst->src[0];
  2524. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  2525. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  2526. GGML_ASSERT(src0->type == dst->type);
  2527. const size_t nb0 = ggml_type_size(src0->type);
  2528. const int ith = params->ith; // thread index
  2529. const int nth = params->nth; // number of threads
  2530. // parallelize by elements
  2531. const int ne = ggml_nelements(dst);
  2532. const int dr = (ne + nth - 1) / nth;
  2533. const int ie0 = dr * ith;
  2534. const int ie1 = MIN(ie0 + dr, ne);
  2535. if (ie0 < ie1) {
  2536. memcpy(
  2537. ((char *) dst->data + ie0*nb0),
  2538. ((char *) src0->data + ie0*nb0),
  2539. (ie1 - ie0) * nb0);
  2540. }
  2541. }
  2542. static void ggml_compute_forward_dup_f16(
  2543. const struct ggml_compute_params * params,
  2544. struct ggml_tensor * dst) {
  2545. const struct ggml_tensor * src0 = dst->src[0];
  2546. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  2547. GGML_TENSOR_UNARY_OP_LOCALS
  2548. const int ith = params->ith; // thread index
  2549. const int nth = params->nth; // number of threads
  2550. // parallelize by rows
  2551. const int nr = ne01;
  2552. // number of rows per thread
  2553. const int dr = (nr + nth - 1) / nth;
  2554. // row range for this thread
  2555. const int ir0 = dr * ith;
  2556. const int ir1 = MIN(ir0 + dr, nr);
  2557. if (src0->type == dst->type &&
  2558. ne00 == ne0 &&
  2559. nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
  2560. // copy by rows
  2561. const size_t rs = ne00*nb00;
  2562. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2563. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2564. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2565. memcpy(
  2566. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  2567. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  2568. rs);
  2569. }
  2570. }
  2571. }
  2572. return;
  2573. }
  2574. // TODO: add more special-case implementations for tensor shapes/strides that can benefit from memcpy
  2575. if (ggml_is_contiguous(dst)) {
  2576. if (nb00 == sizeof(ggml_fp16_t)) {
  2577. if (dst->type == GGML_TYPE_F16) {
  2578. size_t id = 0;
  2579. const size_t rs = ne00 * nb00;
  2580. char * dst_ptr = (char *) dst->data;
  2581. for (int i03 = 0; i03 < ne03; i03++) {
  2582. for (int i02 = 0; i02 < ne02; i02++) {
  2583. id += rs * ir0;
  2584. for (int i01 = ir0; i01 < ir1; i01++) {
  2585. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  2586. memcpy(dst_ptr + id, src0_ptr, rs);
  2587. id += rs;
  2588. }
  2589. id += rs * (ne01 - ir1);
  2590. }
  2591. }
  2592. } else if (dst->type == GGML_TYPE_F32) {
  2593. size_t id = 0;
  2594. float * dst_ptr = (float *) dst->data;
  2595. for (int i03 = 0; i03 < ne03; i03++) {
  2596. for (int i02 = 0; i02 < ne02; i02++) {
  2597. id += ne00 * ir0;
  2598. for (int i01 = ir0; i01 < ir1; i01++) {
  2599. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2600. for (int i00 = 0; i00 < ne00; i00++) {
  2601. dst_ptr[id] = GGML_FP16_TO_FP32(src0_ptr[i00]);
  2602. id++;
  2603. }
  2604. }
  2605. id += ne00 * (ne01 - ir1);
  2606. }
  2607. }
  2608. } else if (ggml_get_type_traits_cpu(dst->type)->from_float) {
  2609. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float;
  2610. float * src0_f32 = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
  2611. size_t id = 0;
  2612. size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
  2613. char * dst_ptr = (char *) dst->data;
  2614. for (int i03 = 0; i03 < ne03; i03++) {
  2615. for (int i02 = 0; i02 < ne02; i02++) {
  2616. id += rs * ir0;
  2617. for (int i01 = ir0; i01 < ir1; i01++) {
  2618. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2619. for (int i00 = 0; i00 < ne00; i00++) {
  2620. src0_f32[i00] = GGML_FP16_TO_FP32(src0_ptr[i00]);
  2621. }
  2622. quantize_row_q(src0_f32, dst_ptr + id, ne00);
  2623. id += rs;
  2624. }
  2625. id += rs * (ne01 - ir1);
  2626. }
  2627. }
  2628. } else {
  2629. GGML_ABORT("fatal error"); // TODO: implement
  2630. }
  2631. } else {
  2632. //printf("%s: this is not optimal - fix me\n", __func__);
  2633. if (dst->type == GGML_TYPE_F32) {
  2634. size_t id = 0;
  2635. float * dst_ptr = (float *) dst->data;
  2636. for (int i03 = 0; i03 < ne03; i03++) {
  2637. for (int i02 = 0; i02 < ne02; i02++) {
  2638. id += ne00 * ir0;
  2639. for (int i01 = ir0; i01 < ir1; i01++) {
  2640. for (int i00 = 0; i00 < ne00; i00++) {
  2641. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2642. dst_ptr[id] = GGML_FP16_TO_FP32(*src0_ptr);
  2643. id++;
  2644. }
  2645. }
  2646. id += ne00 * (ne01 - ir1);
  2647. }
  2648. }
  2649. } else if (dst->type == GGML_TYPE_F16) {
  2650. size_t id = 0;
  2651. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  2652. for (int i03 = 0; i03 < ne03; i03++) {
  2653. for (int i02 = 0; i02 < ne02; i02++) {
  2654. id += ne00 * ir0;
  2655. for (int i01 = ir0; i01 < ir1; i01++) {
  2656. for (int i00 = 0; i00 < ne00; i00++) {
  2657. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2658. dst_ptr[id] = *src0_ptr;
  2659. id++;
  2660. }
  2661. }
  2662. id += ne00 * (ne01 - ir1);
  2663. }
  2664. }
  2665. } else {
  2666. GGML_ABORT("fatal error"); // TODO: implement
  2667. }
  2668. }
  2669. return;
  2670. }
  2671. // dst counters
  2672. int64_t i10 = 0;
  2673. int64_t i11 = 0;
  2674. int64_t i12 = 0;
  2675. int64_t i13 = 0;
  2676. if (dst->type == GGML_TYPE_F16) {
  2677. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2678. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2679. i10 += ne00 * ir0;
  2680. while (i10 >= ne0) {
  2681. i10 -= ne0;
  2682. if (++i11 == ne1) {
  2683. i11 = 0;
  2684. if (++i12 == ne2) {
  2685. i12 = 0;
  2686. if (++i13 == ne3) {
  2687. i13 = 0;
  2688. }
  2689. }
  2690. }
  2691. }
  2692. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2693. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2694. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2695. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2696. memcpy(dst_ptr, src0_ptr, sizeof(ggml_fp16_t));
  2697. if (++i10 == ne00) {
  2698. i10 = 0;
  2699. if (++i11 == ne01) {
  2700. i11 = 0;
  2701. if (++i12 == ne02) {
  2702. i12 = 0;
  2703. if (++i13 == ne03) {
  2704. i13 = 0;
  2705. }
  2706. }
  2707. }
  2708. }
  2709. }
  2710. }
  2711. i10 += ne00 * (ne01 - ir1);
  2712. while (i10 >= ne0) {
  2713. i10 -= ne0;
  2714. if (++i11 == ne1) {
  2715. i11 = 0;
  2716. if (++i12 == ne2) {
  2717. i12 = 0;
  2718. if (++i13 == ne3) {
  2719. i13 = 0;
  2720. }
  2721. }
  2722. }
  2723. }
  2724. }
  2725. }
  2726. } else if (dst->type == GGML_TYPE_F32) {
  2727. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2728. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2729. i10 += ne00 * ir0;
  2730. while (i10 >= ne0) {
  2731. i10 -= ne0;
  2732. if (++i11 == ne1) {
  2733. i11 = 0;
  2734. if (++i12 == ne2) {
  2735. i12 = 0;
  2736. if (++i13 == ne3) {
  2737. i13 = 0;
  2738. }
  2739. }
  2740. }
  2741. }
  2742. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2743. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2744. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2745. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2746. *(float *) dst_ptr = GGML_FP16_TO_FP32(*(const ggml_fp16_t *) src0_ptr);
  2747. if (++i10 == ne0) {
  2748. i10 = 0;
  2749. if (++i11 == ne1) {
  2750. i11 = 0;
  2751. if (++i12 == ne2) {
  2752. i12 = 0;
  2753. if (++i13 == ne3) {
  2754. i13 = 0;
  2755. }
  2756. }
  2757. }
  2758. }
  2759. }
  2760. }
  2761. i10 += ne00 * (ne01 - ir1);
  2762. while (i10 >= ne0) {
  2763. i10 -= ne0;
  2764. if (++i11 == ne1) {
  2765. i11 = 0;
  2766. if (++i12 == ne2) {
  2767. i12 = 0;
  2768. if (++i13 == ne3) {
  2769. i13 = 0;
  2770. }
  2771. }
  2772. }
  2773. }
  2774. }
  2775. }
  2776. } else {
  2777. GGML_ABORT("fatal error"); // TODO: implement
  2778. }
  2779. }
  2780. static void ggml_compute_forward_dup_bf16(
  2781. const struct ggml_compute_params * params,
  2782. struct ggml_tensor * dst) {
  2783. const struct ggml_tensor * src0 = dst->src[0];
  2784. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  2785. GGML_TENSOR_UNARY_OP_LOCALS
  2786. const int ith = params->ith; // thread index
  2787. const int nth = params->nth; // number of threads
  2788. // parallelize by rows
  2789. const int nr = ne01;
  2790. // number of rows per thread
  2791. const int dr = (nr + nth - 1) / nth;
  2792. // row range for this thread
  2793. const int ir0 = dr * ith;
  2794. const int ir1 = MIN(ir0 + dr, nr);
  2795. if (src0->type == dst->type &&
  2796. ne00 == ne0 &&
  2797. nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
  2798. // copy by rows
  2799. const size_t rs = ne00*nb00;
  2800. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2801. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2802. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2803. memcpy(
  2804. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  2805. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  2806. rs);
  2807. }
  2808. }
  2809. }
  2810. return;
  2811. }
  2812. // TODO: add more special-case implementations for tensor shapes/strides that can benefit from memcpy
  2813. if (ggml_is_contiguous(dst)) {
  2814. if (nb00 == sizeof(ggml_bf16_t)) {
  2815. if (dst->type == GGML_TYPE_BF16) {
  2816. size_t id = 0;
  2817. const size_t rs = ne00 * nb00;
  2818. char * dst_ptr = (char *) dst->data;
  2819. for (int i03 = 0; i03 < ne03; i03++) {
  2820. for (int i02 = 0; i02 < ne02; i02++) {
  2821. id += rs * ir0;
  2822. for (int i01 = ir0; i01 < ir1; i01++) {
  2823. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  2824. memcpy(dst_ptr + id, src0_ptr, rs);
  2825. id += rs;
  2826. }
  2827. id += rs * (ne01 - ir1);
  2828. }
  2829. }
  2830. } else if (dst->type == GGML_TYPE_F16) {
  2831. size_t id = 0;
  2832. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  2833. for (int i03 = 0; i03 < ne03; i03++) {
  2834. for (int i02 = 0; i02 < ne02; i02++) {
  2835. id += ne00 * ir0;
  2836. for (int i01 = ir0; i01 < ir1; i01++) {
  2837. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2838. for (int i00 = 0; i00 < ne00; i00++) {
  2839. dst_ptr[id] = GGML_FP32_TO_FP16(GGML_BF16_TO_FP32(src0_ptr[i00]));
  2840. id++;
  2841. }
  2842. }
  2843. id += ne00 * (ne01 - ir1);
  2844. }
  2845. }
  2846. } else if (dst->type == GGML_TYPE_F32) {
  2847. size_t id = 0;
  2848. float * dst_ptr = (float *) dst->data;
  2849. for (int i03 = 0; i03 < ne03; i03++) {
  2850. for (int i02 = 0; i02 < ne02; i02++) {
  2851. id += ne00 * ir0;
  2852. for (int i01 = ir0; i01 < ir1; i01++) {
  2853. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2854. for (int i00 = 0; i00 < ne00; i00++) {
  2855. dst_ptr[id] = GGML_BF16_TO_FP32(src0_ptr[i00]);
  2856. id++;
  2857. }
  2858. }
  2859. id += ne00 * (ne01 - ir1);
  2860. }
  2861. }
  2862. } else if (ggml_get_type_traits_cpu(dst->type)->from_float) {
  2863. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float;
  2864. float * src0_f32 = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
  2865. size_t id = 0;
  2866. size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
  2867. char * dst_ptr = (char *) dst->data;
  2868. for (int i03 = 0; i03 < ne03; i03++) {
  2869. for (int i02 = 0; i02 < ne02; i02++) {
  2870. id += rs * ir0;
  2871. for (int i01 = ir0; i01 < ir1; i01++) {
  2872. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2873. for (int i00 = 0; i00 < ne00; i00++) {
  2874. src0_f32[i00] = GGML_BF16_TO_FP32(src0_ptr[i00]);
  2875. }
  2876. quantize_row_q(src0_f32, dst_ptr + id, ne00);
  2877. id += rs;
  2878. }
  2879. id += rs * (ne01 - ir1);
  2880. }
  2881. }
  2882. } else {
  2883. GGML_ABORT("fatal error"); // TODO: implement
  2884. }
  2885. } else {
  2886. //printf("%s: this is not optimal - fix me\n", __func__);
  2887. if (dst->type == GGML_TYPE_F32) {
  2888. size_t id = 0;
  2889. float * dst_ptr = (float *) dst->data;
  2890. for (int i03 = 0; i03 < ne03; i03++) {
  2891. for (int i02 = 0; i02 < ne02; i02++) {
  2892. id += ne00 * ir0;
  2893. for (int i01 = ir0; i01 < ir1; i01++) {
  2894. for (int i00 = 0; i00 < ne00; i00++) {
  2895. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2896. dst_ptr[id] = GGML_BF16_TO_FP32(*src0_ptr);
  2897. id++;
  2898. }
  2899. }
  2900. id += ne00 * (ne01 - ir1);
  2901. }
  2902. }
  2903. } else if (dst->type == GGML_TYPE_BF16) {
  2904. size_t id = 0;
  2905. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) dst->data;
  2906. for (int i03 = 0; i03 < ne03; i03++) {
  2907. for (int i02 = 0; i02 < ne02; i02++) {
  2908. id += ne00 * ir0;
  2909. for (int i01 = ir0; i01 < ir1; i01++) {
  2910. for (int i00 = 0; i00 < ne00; i00++) {
  2911. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2912. dst_ptr[id] = *src0_ptr;
  2913. id++;
  2914. }
  2915. }
  2916. id += ne00 * (ne01 - ir1);
  2917. }
  2918. }
  2919. } else if (dst->type == GGML_TYPE_F16) {
  2920. size_t id = 0;
  2921. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  2922. for (int i03 = 0; i03 < ne03; i03++) {
  2923. for (int i02 = 0; i02 < ne02; i02++) {
  2924. id += ne00 * ir0;
  2925. for (int i01 = ir0; i01 < ir1; i01++) {
  2926. for (int i00 = 0; i00 < ne00; i00++) {
  2927. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2928. dst_ptr[id] = GGML_FP32_TO_FP16(GGML_BF16_TO_FP32(*src0_ptr));
  2929. id++;
  2930. }
  2931. }
  2932. id += ne00 * (ne01 - ir1);
  2933. }
  2934. }
  2935. } else {
  2936. GGML_ABORT("fatal error"); // TODO: implement
  2937. }
  2938. }
  2939. return;
  2940. }
  2941. // dst counters
  2942. int64_t i10 = 0;
  2943. int64_t i11 = 0;
  2944. int64_t i12 = 0;
  2945. int64_t i13 = 0;
  2946. if (dst->type == GGML_TYPE_BF16) {
  2947. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2948. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2949. i10 += ne00 * ir0;
  2950. while (i10 >= ne0) {
  2951. i10 -= ne0;
  2952. if (++i11 == ne1) {
  2953. i11 = 0;
  2954. if (++i12 == ne2) {
  2955. i12 = 0;
  2956. if (++i13 == ne3) {
  2957. i13 = 0;
  2958. }
  2959. }
  2960. }
  2961. }
  2962. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2963. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2964. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2965. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2966. memcpy(dst_ptr, src0_ptr, sizeof(ggml_bf16_t));
  2967. if (++i10 == ne00) {
  2968. i10 = 0;
  2969. if (++i11 == ne01) {
  2970. i11 = 0;
  2971. if (++i12 == ne02) {
  2972. i12 = 0;
  2973. if (++i13 == ne03) {
  2974. i13 = 0;
  2975. }
  2976. }
  2977. }
  2978. }
  2979. }
  2980. }
  2981. i10 += ne00 * (ne01 - ir1);
  2982. while (i10 >= ne0) {
  2983. i10 -= ne0;
  2984. if (++i11 == ne1) {
  2985. i11 = 0;
  2986. if (++i12 == ne2) {
  2987. i12 = 0;
  2988. if (++i13 == ne3) {
  2989. i13 = 0;
  2990. }
  2991. }
  2992. }
  2993. }
  2994. }
  2995. }
  2996. } else if (dst->type == GGML_TYPE_F16) {
  2997. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2998. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2999. i10 += ne00 * ir0;
  3000. while (i10 >= ne0) {
  3001. i10 -= ne0;
  3002. if (++i11 == ne1) {
  3003. i11 = 0;
  3004. if (++i12 == ne2) {
  3005. i12 = 0;
  3006. if (++i13 == ne3) {
  3007. i13 = 0;
  3008. }
  3009. }
  3010. }
  3011. }
  3012. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3013. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3014. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3015. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3016. *(ggml_fp16_t *) dst_ptr = GGML_FP32_TO_FP16(GGML_BF16_TO_FP32(*(const ggml_bf16_t *) src0_ptr));
  3017. if (++i10 == ne0) {
  3018. i10 = 0;
  3019. if (++i11 == ne1) {
  3020. i11 = 0;
  3021. if (++i12 == ne2) {
  3022. i12 = 0;
  3023. if (++i13 == ne3) {
  3024. i13 = 0;
  3025. }
  3026. }
  3027. }
  3028. }
  3029. }
  3030. }
  3031. i10 += ne00 * (ne01 - ir1);
  3032. while (i10 >= ne0) {
  3033. i10 -= ne0;
  3034. if (++i11 == ne1) {
  3035. i11 = 0;
  3036. if (++i12 == ne2) {
  3037. i12 = 0;
  3038. if (++i13 == ne3) {
  3039. i13 = 0;
  3040. }
  3041. }
  3042. }
  3043. }
  3044. }
  3045. }
  3046. } else if (dst->type == GGML_TYPE_F32) {
  3047. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3048. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3049. i10 += ne00 * ir0;
  3050. while (i10 >= ne0) {
  3051. i10 -= ne0;
  3052. if (++i11 == ne1) {
  3053. i11 = 0;
  3054. if (++i12 == ne2) {
  3055. i12 = 0;
  3056. if (++i13 == ne3) {
  3057. i13 = 0;
  3058. }
  3059. }
  3060. }
  3061. }
  3062. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3063. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3064. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3065. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3066. *(float *) dst_ptr = GGML_BF16_TO_FP32(*(const ggml_bf16_t *) src0_ptr);
  3067. if (++i10 == ne0) {
  3068. i10 = 0;
  3069. if (++i11 == ne1) {
  3070. i11 = 0;
  3071. if (++i12 == ne2) {
  3072. i12 = 0;
  3073. if (++i13 == ne3) {
  3074. i13 = 0;
  3075. }
  3076. }
  3077. }
  3078. }
  3079. }
  3080. }
  3081. i10 += ne00 * (ne01 - ir1);
  3082. while (i10 >= ne0) {
  3083. i10 -= ne0;
  3084. if (++i11 == ne1) {
  3085. i11 = 0;
  3086. if (++i12 == ne2) {
  3087. i12 = 0;
  3088. if (++i13 == ne3) {
  3089. i13 = 0;
  3090. }
  3091. }
  3092. }
  3093. }
  3094. }
  3095. }
  3096. } else {
  3097. GGML_ABORT("fatal error"); // TODO: implement
  3098. }
  3099. }
  3100. static void ggml_compute_forward_dup_f32(
  3101. const struct ggml_compute_params * params,
  3102. struct ggml_tensor * dst) {
  3103. const struct ggml_tensor * src0 = dst->src[0];
  3104. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  3105. GGML_TENSOR_UNARY_OP_LOCALS
  3106. const int ith = params->ith; // thread index
  3107. const int nth = params->nth; // number of threads
  3108. // parallelize by rows
  3109. const int nr = ne01;
  3110. // number of rows per thread
  3111. const int dr = (nr + nth - 1) / nth;
  3112. // row range for this thread
  3113. const int ir0 = dr * ith;
  3114. const int ir1 = MIN(ir0 + dr, nr);
  3115. if (src0->type == dst->type &&
  3116. ne00 == ne0 &&
  3117. nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
  3118. // copy by rows
  3119. const size_t rs = ne00*nb00;
  3120. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3121. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3122. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3123. memcpy(
  3124. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  3125. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  3126. rs);
  3127. }
  3128. }
  3129. }
  3130. return;
  3131. }
  3132. if (ggml_is_contiguous(dst)) {
  3133. // TODO: simplify
  3134. if (nb00 == sizeof(float)) {
  3135. if (dst->type == GGML_TYPE_F32) {
  3136. size_t id = 0;
  3137. const size_t rs = ne00 * nb00;
  3138. char * dst_ptr = (char *) dst->data;
  3139. for (int i03 = 0; i03 < ne03; i03++) {
  3140. for (int i02 = 0; i02 < ne02; i02++) {
  3141. id += rs * ir0;
  3142. for (int i01 = ir0; i01 < ir1; i01++) {
  3143. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  3144. memcpy(dst_ptr + id, src0_ptr, rs);
  3145. id += rs;
  3146. }
  3147. id += rs * (ne01 - ir1);
  3148. }
  3149. }
  3150. } else if (ggml_get_type_traits_cpu(dst->type)->from_float) {
  3151. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float;
  3152. size_t id = 0;
  3153. size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
  3154. char * dst_ptr = (char *) dst->data;
  3155. for (int i03 = 0; i03 < ne03; i03++) {
  3156. for (int i02 = 0; i02 < ne02; i02++) {
  3157. id += rs * ir0;
  3158. for (int i01 = ir0; i01 < ir1; i01++) {
  3159. const float * src0_ptr = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  3160. quantize_row_q(src0_ptr, dst_ptr + id, ne00);
  3161. id += rs;
  3162. }
  3163. id += rs * (ne01 - ir1);
  3164. }
  3165. }
  3166. } else {
  3167. GGML_ABORT("fatal error"); // TODO: implement
  3168. }
  3169. } else {
  3170. //printf("%s: this is not optimal - fix me\n", __func__);
  3171. if (dst->type == GGML_TYPE_F32) {
  3172. size_t id = 0;
  3173. float * dst_ptr = (float *) dst->data;
  3174. for (int i03 = 0; i03 < ne03; i03++) {
  3175. for (int i02 = 0; i02 < ne02; i02++) {
  3176. id += ne00 * ir0;
  3177. for (int i01 = ir0; i01 < ir1; i01++) {
  3178. for (int i00 = 0; i00 < ne00; i00++) {
  3179. const float * src0_ptr = (float *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3180. dst_ptr[id] = *src0_ptr;
  3181. id++;
  3182. }
  3183. }
  3184. id += ne00 * (ne01 - ir1);
  3185. }
  3186. }
  3187. } else if (dst->type == GGML_TYPE_F16) {
  3188. size_t id = 0;
  3189. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  3190. for (int i03 = 0; i03 < ne03; i03++) {
  3191. for (int i02 = 0; i02 < ne02; i02++) {
  3192. id += ne00 * ir0;
  3193. for (int i01 = ir0; i01 < ir1; i01++) {
  3194. for (int i00 = 0; i00 < ne00; i00++) {
  3195. const float * src0_ptr = (float *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3196. dst_ptr[id] = GGML_FP32_TO_FP16(*src0_ptr);
  3197. id++;
  3198. }
  3199. }
  3200. id += ne00 * (ne01 - ir1);
  3201. }
  3202. }
  3203. } else if (dst->type == GGML_TYPE_BF16) {
  3204. size_t id = 0;
  3205. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) dst->data;
  3206. for (int i03 = 0; i03 < ne03; i03++) {
  3207. for (int i02 = 0; i02 < ne02; i02++) {
  3208. id += ne00 * ir0;
  3209. for (int i01 = ir0; i01 < ir1; i01++) {
  3210. for (int i00 = 0; i00 < ne00; i00++) {
  3211. const float * src0_ptr = (float *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3212. dst_ptr[id] = GGML_FP32_TO_BF16(*src0_ptr);
  3213. id++;
  3214. }
  3215. }
  3216. id += ne00 * (ne01 - ir1);
  3217. }
  3218. }
  3219. } else {
  3220. GGML_ABORT("fatal error"); // TODO: implement
  3221. }
  3222. }
  3223. return;
  3224. }
  3225. // dst counters
  3226. int64_t i10 = 0;
  3227. int64_t i11 = 0;
  3228. int64_t i12 = 0;
  3229. int64_t i13 = 0;
  3230. if (dst->type == GGML_TYPE_F32) {
  3231. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3232. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3233. i10 += ne00 * ir0;
  3234. while (i10 >= ne0) {
  3235. i10 -= ne0;
  3236. if (++i11 == ne1) {
  3237. i11 = 0;
  3238. if (++i12 == ne2) {
  3239. i12 = 0;
  3240. if (++i13 == ne3) {
  3241. i13 = 0;
  3242. }
  3243. }
  3244. }
  3245. }
  3246. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3247. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3248. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3249. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3250. memcpy(dst_ptr, src0_ptr, sizeof(float));
  3251. if (++i10 == ne0) {
  3252. i10 = 0;
  3253. if (++i11 == ne1) {
  3254. i11 = 0;
  3255. if (++i12 == ne2) {
  3256. i12 = 0;
  3257. if (++i13 == ne3) {
  3258. i13 = 0;
  3259. }
  3260. }
  3261. }
  3262. }
  3263. }
  3264. }
  3265. i10 += ne00 * (ne01 - ir1);
  3266. while (i10 >= ne0) {
  3267. i10 -= ne0;
  3268. if (++i11 == ne1) {
  3269. i11 = 0;
  3270. if (++i12 == ne2) {
  3271. i12 = 0;
  3272. if (++i13 == ne3) {
  3273. i13 = 0;
  3274. }
  3275. }
  3276. }
  3277. }
  3278. }
  3279. }
  3280. } else if (dst->type == GGML_TYPE_F16) {
  3281. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3282. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3283. i10 += ne00 * ir0;
  3284. while (i10 >= ne0) {
  3285. i10 -= ne0;
  3286. if (++i11 == ne1) {
  3287. i11 = 0;
  3288. if (++i12 == ne2) {
  3289. i12 = 0;
  3290. if (++i13 == ne3) {
  3291. i13 = 0;
  3292. }
  3293. }
  3294. }
  3295. }
  3296. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3297. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3298. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3299. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3300. *(ggml_fp16_t *) dst_ptr = GGML_FP32_TO_FP16(*(const float *) src0_ptr);
  3301. if (++i10 == ne0) {
  3302. i10 = 0;
  3303. if (++i11 == ne1) {
  3304. i11 = 0;
  3305. if (++i12 == ne2) {
  3306. i12 = 0;
  3307. if (++i13 == ne3) {
  3308. i13 = 0;
  3309. }
  3310. }
  3311. }
  3312. }
  3313. }
  3314. }
  3315. i10 += ne00 * (ne01 - ir1);
  3316. while (i10 >= ne0) {
  3317. i10 -= ne0;
  3318. if (++i11 == ne1) {
  3319. i11 = 0;
  3320. if (++i12 == ne2) {
  3321. i12 = 0;
  3322. if (++i13 == ne3) {
  3323. i13 = 0;
  3324. }
  3325. }
  3326. }
  3327. }
  3328. }
  3329. }
  3330. } else if (dst->type == GGML_TYPE_BF16) {
  3331. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3332. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3333. i10 += ne00 * ir0;
  3334. while (i10 >= ne0) {
  3335. i10 -= ne0;
  3336. if (++i11 == ne1) {
  3337. i11 = 0;
  3338. if (++i12 == ne2) {
  3339. i12 = 0;
  3340. if (++i13 == ne3) {
  3341. i13 = 0;
  3342. }
  3343. }
  3344. }
  3345. }
  3346. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3347. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3348. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3349. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3350. *(ggml_bf16_t *) dst_ptr = GGML_FP32_TO_BF16(*(const float *) src0_ptr);
  3351. if (++i10 == ne0) {
  3352. i10 = 0;
  3353. if (++i11 == ne1) {
  3354. i11 = 0;
  3355. if (++i12 == ne2) {
  3356. i12 = 0;
  3357. if (++i13 == ne3) {
  3358. i13 = 0;
  3359. }
  3360. }
  3361. }
  3362. }
  3363. }
  3364. }
  3365. i10 += ne00 * (ne01 - ir1);
  3366. while (i10 >= ne0) {
  3367. i10 -= ne0;
  3368. if (++i11 == ne1) {
  3369. i11 = 0;
  3370. if (++i12 == ne2) {
  3371. i12 = 0;
  3372. if (++i13 == ne3) {
  3373. i13 = 0;
  3374. }
  3375. }
  3376. }
  3377. }
  3378. }
  3379. }
  3380. } else {
  3381. GGML_ABORT("fatal error"); // TODO: implement
  3382. }
  3383. }
  3384. // A simplified version of ggml_compute_forward_dup that doesn't do float upcasting, and just plain old memcpy.
  3385. static void ggml_compute_forward_dup_bytes(
  3386. const struct ggml_compute_params * params,
  3387. struct ggml_tensor * dst) {
  3388. const struct ggml_tensor * src0 = dst->src[0];
  3389. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  3390. GGML_ASSERT(src0->type == dst->type);
  3391. GGML_TENSOR_UNARY_OP_LOCALS;
  3392. if (ggml_is_contiguous(src0) && ggml_is_contiguous(dst)) {
  3393. ggml_compute_forward_dup_same_cont(params, dst);
  3394. return;
  3395. }
  3396. const size_t type_size = ggml_type_size(src0->type);
  3397. const int ith = params->ith; // thread index
  3398. const int nth = params->nth; // number of threads
  3399. // parallelize by rows
  3400. const int nr = ne01;
  3401. // number of rows per thread
  3402. const int dr = (nr + nth - 1) / nth;
  3403. // row range for this thread
  3404. const int ir0 = dr * ith;
  3405. const int ir1 = MIN(ir0 + dr, nr);
  3406. if (src0->type == dst->type &&
  3407. ne00 == ne0 &&
  3408. nb00 == type_size && nb0 == type_size) {
  3409. // copy by rows
  3410. const size_t rs = ne00 * type_size;
  3411. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3412. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3413. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3414. memcpy(
  3415. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  3416. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  3417. rs);
  3418. }
  3419. }
  3420. }
  3421. return;
  3422. }
  3423. if (ggml_is_contiguous(dst)) {
  3424. size_t id = 0;
  3425. char * dst_ptr = (char *) dst->data;
  3426. const size_t rs = ne00 * type_size;
  3427. if (nb00 == type_size) {
  3428. // src0 is contigous on first dimension, copy by rows
  3429. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3430. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3431. id += rs * ir0;
  3432. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3433. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  3434. memcpy(dst_ptr + id, src0_ptr, rs);
  3435. id += rs;
  3436. }
  3437. id += rs * (ne01 - ir1);
  3438. }
  3439. }
  3440. } else {
  3441. //printf("%s: this is not optimal - fix me\n", __func__);
  3442. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3443. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3444. id += rs * ir0;
  3445. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3446. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3447. const char * src0_ptr = (char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03;
  3448. memcpy(dst_ptr + id, src0_ptr, type_size);
  3449. id += type_size;
  3450. }
  3451. }
  3452. id += rs * (ne01 - ir1);
  3453. }
  3454. }
  3455. }
  3456. return;
  3457. }
  3458. // dst counters
  3459. int64_t i10 = 0;
  3460. int64_t i11 = 0;
  3461. int64_t i12 = 0;
  3462. int64_t i13 = 0;
  3463. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3464. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3465. i10 += ne00 * ir0;
  3466. while (i10 >= ne0) {
  3467. i10 -= ne0;
  3468. if (++i11 == ne1) {
  3469. i11 = 0;
  3470. if (++i12 == ne2) {
  3471. i12 = 0;
  3472. if (++i13 == ne3) {
  3473. i13 = 0;
  3474. }
  3475. }
  3476. }
  3477. }
  3478. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3479. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3480. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3481. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3482. memcpy(dst_ptr, src0_ptr, type_size);
  3483. if (++i10 == ne0) {
  3484. i10 = 0;
  3485. if (++i11 == ne1) {
  3486. i11 = 0;
  3487. if (++i12 == ne2) {
  3488. i12 = 0;
  3489. if (++i13 == ne3) {
  3490. i13 = 0;
  3491. }
  3492. }
  3493. }
  3494. }
  3495. }
  3496. }
  3497. i10 += ne00 * (ne01 - ir1);
  3498. while (i10 >= ne0) {
  3499. i10 -= ne0;
  3500. if (++i11 == ne1) {
  3501. i11 = 0;
  3502. if (++i12 == ne2) {
  3503. i12 = 0;
  3504. if (++i13 == ne3) {
  3505. i13 = 0;
  3506. }
  3507. }
  3508. }
  3509. }
  3510. }
  3511. }
  3512. }
  3513. static void ggml_compute_forward_dup(
  3514. const struct ggml_compute_params * params,
  3515. struct ggml_tensor * dst) {
  3516. const struct ggml_tensor * src0 = dst->src[0];
  3517. if (src0->type == dst->type) {
  3518. ggml_compute_forward_dup_bytes(params, dst);
  3519. return;
  3520. }
  3521. switch (src0->type) {
  3522. case GGML_TYPE_F16:
  3523. {
  3524. ggml_compute_forward_dup_f16(params, dst);
  3525. } break;
  3526. case GGML_TYPE_BF16:
  3527. {
  3528. ggml_compute_forward_dup_bf16(params, dst);
  3529. } break;
  3530. case GGML_TYPE_F32:
  3531. {
  3532. ggml_compute_forward_dup_f32(params, dst);
  3533. } break;
  3534. default:
  3535. {
  3536. GGML_ABORT("fatal error");
  3537. }
  3538. }
  3539. }
  3540. // ggml_compute_forward_add
  3541. static void ggml_compute_forward_add_f32(
  3542. const struct ggml_compute_params * params,
  3543. struct ggml_tensor * dst) {
  3544. const struct ggml_tensor * src0 = dst->src[0];
  3545. const struct ggml_tensor * src1 = dst->src[1];
  3546. GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  3547. const int ith = params->ith;
  3548. const int nth = params->nth;
  3549. const int nr = ggml_nrows(src0);
  3550. GGML_TENSOR_BINARY_OP_LOCALS
  3551. GGML_ASSERT( nb0 == sizeof(float));
  3552. GGML_ASSERT(nb00 == sizeof(float));
  3553. // rows per thread
  3554. const int dr = (nr + nth - 1)/nth;
  3555. // row range for this thread
  3556. const int ir0 = dr*ith;
  3557. const int ir1 = MIN(ir0 + dr, nr);
  3558. if (nb10 == sizeof(float)) {
  3559. for (int ir = ir0; ir < ir1; ++ir) {
  3560. // src1 is broadcastable across src0 and dst in i1, i2, i3
  3561. const int64_t i03 = ir/(ne02*ne01);
  3562. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  3563. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  3564. const int64_t i13 = i03 % ne13;
  3565. const int64_t i12 = i02 % ne12;
  3566. const int64_t i11 = i01 % ne11;
  3567. const int64_t nr0 = ne00 / ne10;
  3568. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  3569. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  3570. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  3571. for (int64_t r = 0; r < nr0; ++r) {
  3572. #ifdef GGML_USE_ACCELERATE
  3573. vDSP_vadd(src0_ptr + r*ne10, 1, src1_ptr, 1, dst_ptr + r*ne10, 1, ne10);
  3574. #else
  3575. ggml_vec_add_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  3576. #endif
  3577. }
  3578. }
  3579. } else {
  3580. // src1 is not contiguous
  3581. for (int ir = ir0; ir < ir1; ++ir) {
  3582. // src1 is broadcastable across src0 and dst in i1, i2, i3
  3583. const int64_t i03 = ir/(ne02*ne01);
  3584. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  3585. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  3586. const int64_t i13 = i03 % ne13;
  3587. const int64_t i12 = i02 % ne12;
  3588. const int64_t i11 = i01 % ne11;
  3589. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  3590. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  3591. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  3592. const int64_t i10 = i0 % ne10;
  3593. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  3594. dst_ptr[i0] = src0_ptr[i0] + *src1_ptr;
  3595. }
  3596. }
  3597. }
  3598. }
  3599. static void ggml_compute_forward_add_f16_f32(
  3600. const struct ggml_compute_params * params,
  3601. struct ggml_tensor * dst) {
  3602. const struct ggml_tensor * src0 = dst->src[0];
  3603. const struct ggml_tensor * src1 = dst->src[1];
  3604. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3605. const int ith = params->ith;
  3606. const int nth = params->nth;
  3607. const int nr = ggml_nrows(src0);
  3608. GGML_TENSOR_BINARY_OP_LOCALS
  3609. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3610. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3611. if (dst->type == GGML_TYPE_F32) {
  3612. GGML_ASSERT( nb0 == sizeof(float));
  3613. }
  3614. else {
  3615. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3616. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3617. }
  3618. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3619. // rows per thread
  3620. const int dr = (nr + nth - 1)/nth;
  3621. // row range for this thread
  3622. const int ir0 = dr*ith;
  3623. const int ir1 = MIN(ir0 + dr, nr);
  3624. if (nb10 == sizeof(float)) {
  3625. if (dst->type == GGML_TYPE_F16) {
  3626. for (int ir = ir0; ir < ir1; ++ir) {
  3627. // src0, src1 and dst are same shape => same indices
  3628. const int i3 = ir/(ne2*ne1);
  3629. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3630. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3631. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3632. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3633. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3634. for (int i = 0; i < ne0; i++) {
  3635. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + src1_ptr[i]);
  3636. }
  3637. }
  3638. } else {
  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. float * dst_ptr = (float *) ((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_FP16_TO_FP32(src0_ptr[i]) + src1_ptr[i];
  3649. }
  3650. }
  3651. }
  3652. }
  3653. else {
  3654. // src1 is not contiguous
  3655. GGML_ABORT("fatal error");
  3656. }
  3657. }
  3658. static void ggml_compute_forward_add_bf16_f32(
  3659. const struct ggml_compute_params * params,
  3660. struct ggml_tensor * dst) {
  3661. const struct ggml_tensor * src0 = dst->src[0];
  3662. const struct ggml_tensor * src1 = dst->src[1];
  3663. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3664. const int ith = params->ith;
  3665. const int nth = params->nth;
  3666. const int nr = ggml_nrows(src0);
  3667. GGML_TENSOR_BINARY_OP_LOCALS
  3668. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  3669. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3670. if (dst->type == GGML_TYPE_F32) {
  3671. GGML_ASSERT( nb0 == sizeof(float));
  3672. }
  3673. else {
  3674. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  3675. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  3676. }
  3677. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  3678. // rows per thread
  3679. const int dr = (nr + nth - 1)/nth;
  3680. // row range for this thread
  3681. const int ir0 = dr*ith;
  3682. const int ir1 = MIN(ir0 + dr, nr);
  3683. if (nb10 == sizeof(float)) {
  3684. if (dst->type == GGML_TYPE_BF16) {
  3685. for (int ir = ir0; ir < ir1; ++ir) {
  3686. // src0, src1 and dst are same shape => same indices
  3687. const int i3 = ir/(ne2*ne1);
  3688. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3689. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3690. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3691. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3692. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3693. for (int i = 0; i < ne0; i++) {
  3694. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + src1_ptr[i]);
  3695. }
  3696. }
  3697. } else {
  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. float * dst_ptr = (float *) ((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_BF16_TO_FP32(src0_ptr[i]) + src1_ptr[i];
  3708. }
  3709. }
  3710. }
  3711. }
  3712. else {
  3713. // src1 is not contiguous
  3714. GGML_ABORT("fatal error");
  3715. }
  3716. }
  3717. static void ggml_compute_forward_add_f16_f16(
  3718. const struct ggml_compute_params * params,
  3719. struct ggml_tensor * dst) {
  3720. const struct ggml_tensor * src0 = dst->src[0];
  3721. const struct ggml_tensor * src1 = dst->src[1];
  3722. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3723. const int ith = params->ith;
  3724. const int nth = params->nth;
  3725. const int nr = ggml_nrows(src0);
  3726. GGML_TENSOR_BINARY_OP_LOCALS
  3727. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3728. GGML_ASSERT(src1->type == GGML_TYPE_F16);
  3729. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3730. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3731. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3732. // rows per thread
  3733. const int dr = (nr + nth - 1)/nth;
  3734. // row range for this thread
  3735. const int ir0 = dr*ith;
  3736. const int ir1 = MIN(ir0 + dr, nr);
  3737. if (nb10 == sizeof(ggml_fp16_t)) {
  3738. for (int ir = ir0; ir < ir1; ++ir) {
  3739. // src0, src1 and dst are same shape => same indices
  3740. const int i3 = ir/(ne2*ne1);
  3741. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3742. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3743. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3744. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3745. ggml_fp16_t * src1_ptr = (ggml_fp16_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3746. for (int i = 0; i < ne0; i++) {
  3747. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + GGML_FP16_TO_FP32(src1_ptr[i]));
  3748. }
  3749. }
  3750. }
  3751. else {
  3752. // src1 is not contiguous
  3753. GGML_ABORT("fatal error");
  3754. }
  3755. }
  3756. static void ggml_compute_forward_add_bf16_bf16(
  3757. const struct ggml_compute_params * params,
  3758. struct ggml_tensor * dst) {
  3759. const struct ggml_tensor * src0 = dst->src[0];
  3760. const struct ggml_tensor * src1 = dst->src[1];
  3761. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3762. const int ith = params->ith;
  3763. const int nth = params->nth;
  3764. const int nr = ggml_nrows(src0);
  3765. GGML_TENSOR_BINARY_OP_LOCALS
  3766. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  3767. GGML_ASSERT(src1->type == GGML_TYPE_BF16);
  3768. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  3769. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  3770. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  3771. // rows per thread
  3772. const int dr = (nr + nth - 1)/nth;
  3773. // row range for this thread
  3774. const int ir0 = dr*ith;
  3775. const int ir1 = MIN(ir0 + dr, nr);
  3776. if (nb10 == sizeof(ggml_bf16_t)) {
  3777. for (int ir = ir0; ir < ir1; ++ir) {
  3778. // src0, src1 and dst are same shape => same indices
  3779. const int i3 = ir/(ne2*ne1);
  3780. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3781. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3782. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3783. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3784. ggml_bf16_t * src1_ptr = (ggml_bf16_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3785. for (int i = 0; i < ne0; i++) {
  3786. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + GGML_BF16_TO_FP32(src1_ptr[i]));
  3787. }
  3788. }
  3789. }
  3790. else {
  3791. // src1 is not contiguous
  3792. GGML_ABORT("fatal error");
  3793. }
  3794. }
  3795. static void ggml_compute_forward_add_q_f32(
  3796. const struct ggml_compute_params * params,
  3797. struct ggml_tensor * dst) {
  3798. const struct ggml_tensor * src0 = dst->src[0];
  3799. const struct ggml_tensor * src1 = dst->src[1];
  3800. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3801. const int nr = ggml_nrows(src0);
  3802. GGML_TENSOR_BINARY_OP_LOCALS
  3803. const int ith = params->ith;
  3804. const int nth = params->nth;
  3805. const enum ggml_type type = src0->type;
  3806. const enum ggml_type dtype = dst->type;
  3807. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  3808. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dtype)->from_float;
  3809. // we don't support permuted src0 or src1
  3810. GGML_ASSERT(nb00 == ggml_type_size(type));
  3811. GGML_ASSERT(nb10 == sizeof(float));
  3812. // dst cannot be transposed or permuted
  3813. GGML_ASSERT(nb0 <= nb1);
  3814. GGML_ASSERT(nb1 <= nb2);
  3815. GGML_ASSERT(nb2 <= nb3);
  3816. GGML_ASSERT(ggml_is_quantized(src0->type));
  3817. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3818. // rows per thread
  3819. const int dr = (nr + nth - 1)/nth;
  3820. // row range for this thread
  3821. const int ir0 = dr*ith;
  3822. const int ir1 = MIN(ir0 + dr, nr);
  3823. float * wdata = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
  3824. for (int ir = ir0; ir < ir1; ++ir) {
  3825. // src0 indices
  3826. const int i03 = ir/(ne02*ne01);
  3827. const int i02 = (ir - i03*ne02*ne01)/ne01;
  3828. const int i01 = (ir - i03*ne02*ne01 - i02*ne01);
  3829. // src1 and dst are same shape as src0 => same indices
  3830. const int i13 = i03;
  3831. const int i12 = i02;
  3832. const int i11 = i01;
  3833. const int i3 = i03;
  3834. const int i2 = i02;
  3835. const int i1 = i01;
  3836. void * src0_row = (void *) ((char *) src0->data + (i01*nb01 + i02*nb02 + i03*nb03));
  3837. float * src1_row = (float *)((char *) src1->data + (i11*nb11 + i12*nb12 + i13*nb13));
  3838. void * dst_row = (void *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  3839. assert(ne00 % 32 == 0);
  3840. // unquantize row from src0 to temp buffer
  3841. dequantize_row_q(src0_row, wdata, ne00);
  3842. // add src1
  3843. ggml_vec_acc_f32(ne00, wdata, src1_row);
  3844. // quantize row to dst
  3845. if (quantize_row_q != NULL) {
  3846. quantize_row_q(wdata, dst_row, ne00);
  3847. } else {
  3848. memcpy(dst_row, wdata, ne0*nb0);
  3849. }
  3850. }
  3851. }
  3852. static void ggml_compute_forward_add(
  3853. const struct ggml_compute_params * params,
  3854. struct ggml_tensor * dst) {
  3855. const struct ggml_tensor * src0 = dst->src[0];
  3856. const struct ggml_tensor * src1 = dst->src[1];
  3857. switch (src0->type) {
  3858. case GGML_TYPE_F32:
  3859. {
  3860. if (src1->type == GGML_TYPE_F32) {
  3861. ggml_compute_forward_add_f32(params, dst);
  3862. }
  3863. else {
  3864. GGML_ABORT("fatal error");
  3865. }
  3866. } break;
  3867. case GGML_TYPE_F16:
  3868. {
  3869. if (src1->type == GGML_TYPE_F16) {
  3870. ggml_compute_forward_add_f16_f16(params, dst);
  3871. }
  3872. else if (src1->type == GGML_TYPE_F32) {
  3873. ggml_compute_forward_add_f16_f32(params, dst);
  3874. }
  3875. else {
  3876. GGML_ABORT("fatal error");
  3877. }
  3878. } break;
  3879. case GGML_TYPE_BF16:
  3880. {
  3881. if (src1->type == GGML_TYPE_BF16) {
  3882. ggml_compute_forward_add_bf16_bf16(params, dst);
  3883. }
  3884. else if (src1->type == GGML_TYPE_F32) {
  3885. ggml_compute_forward_add_bf16_f32(params, dst);
  3886. }
  3887. else {
  3888. GGML_ABORT("fatal error");
  3889. }
  3890. } break;
  3891. case GGML_TYPE_Q4_0:
  3892. case GGML_TYPE_Q4_1:
  3893. case GGML_TYPE_Q5_0:
  3894. case GGML_TYPE_Q5_1:
  3895. case GGML_TYPE_Q8_0:
  3896. case GGML_TYPE_Q2_K:
  3897. case GGML_TYPE_Q3_K:
  3898. case GGML_TYPE_Q4_K:
  3899. case GGML_TYPE_Q5_K:
  3900. case GGML_TYPE_Q6_K:
  3901. case GGML_TYPE_TQ1_0:
  3902. case GGML_TYPE_TQ2_0:
  3903. case GGML_TYPE_IQ2_XXS:
  3904. case GGML_TYPE_IQ2_XS:
  3905. case GGML_TYPE_IQ3_XXS:
  3906. case GGML_TYPE_IQ1_S:
  3907. case GGML_TYPE_IQ1_M:
  3908. case GGML_TYPE_IQ4_NL:
  3909. case GGML_TYPE_IQ4_XS:
  3910. case GGML_TYPE_IQ3_S:
  3911. case GGML_TYPE_IQ2_S:
  3912. {
  3913. ggml_compute_forward_add_q_f32(params, dst);
  3914. } break;
  3915. default:
  3916. {
  3917. GGML_ABORT("fatal error");
  3918. }
  3919. }
  3920. }
  3921. // ggml_compute_forward_add1
  3922. static void ggml_compute_forward_add1_f32(
  3923. const struct ggml_compute_params * params,
  3924. struct ggml_tensor * dst) {
  3925. const struct ggml_tensor * src0 = dst->src[0];
  3926. const struct ggml_tensor * src1 = dst->src[1];
  3927. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  3928. GGML_ASSERT(ggml_is_scalar(src1));
  3929. const int ith = params->ith;
  3930. const int nth = params->nth;
  3931. const int nr = ggml_nrows(src0);
  3932. GGML_TENSOR_UNARY_OP_LOCALS
  3933. GGML_ASSERT( nb0 == sizeof(float));
  3934. GGML_ASSERT(nb00 == sizeof(float));
  3935. // rows per thread
  3936. const int dr = (nr + nth - 1)/nth;
  3937. // row range for this thread
  3938. const int ir0 = dr*ith;
  3939. const int ir1 = MIN(ir0 + dr, nr);
  3940. for (int ir = ir0; ir < ir1; ++ir) {
  3941. // src0 and dst are same shape => same indices
  3942. const int i3 = ir/(ne2*ne1);
  3943. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3944. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3945. #ifdef GGML_USE_ACCELERATE
  3946. UNUSED(ggml_vec_add1_f32);
  3947. vDSP_vadd(
  3948. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), 1,
  3949. (float *) ((char *) src1->data), 0,
  3950. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), 1,
  3951. ne0);
  3952. #else
  3953. ggml_vec_add1_f32(ne0,
  3954. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ),
  3955. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01),
  3956. *(float *) src1->data);
  3957. #endif
  3958. }
  3959. }
  3960. static void ggml_compute_forward_add1_f16_f32(
  3961. const struct ggml_compute_params * params,
  3962. struct ggml_tensor * dst) {
  3963. const struct ggml_tensor * src0 = dst->src[0];
  3964. const struct ggml_tensor * src1 = dst->src[1];
  3965. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  3966. GGML_ASSERT(ggml_is_scalar(src1));
  3967. // scalar to add
  3968. const float v = *(float *) src1->data;
  3969. const int ith = params->ith;
  3970. const int nth = params->nth;
  3971. const int nr = ggml_nrows(src0);
  3972. GGML_TENSOR_UNARY_OP_LOCALS
  3973. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3974. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3975. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3976. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3977. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3978. // rows per thread
  3979. const int dr = (nr + nth - 1)/nth;
  3980. // row range for this thread
  3981. const int ir0 = dr*ith;
  3982. const int ir1 = MIN(ir0 + dr, nr);
  3983. for (int ir = ir0; ir < ir1; ++ir) {
  3984. // src0 and dst are same shape => same indices
  3985. const int i3 = ir/(ne2*ne1);
  3986. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3987. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3988. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  3989. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3990. for (int i = 0; i < ne0; i++) {
  3991. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + v);
  3992. }
  3993. }
  3994. }
  3995. static void ggml_compute_forward_add1_f16_f16(
  3996. const struct ggml_compute_params * params,
  3997. struct ggml_tensor * dst) {
  3998. const struct ggml_tensor * src0 = dst->src[0];
  3999. const struct ggml_tensor * src1 = dst->src[1];
  4000. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4001. GGML_ASSERT(ggml_is_scalar(src1));
  4002. // scalar to add
  4003. const float v = GGML_FP16_TO_FP32(*(ggml_fp16_t *) src1->data);
  4004. const int ith = params->ith;
  4005. const int nth = params->nth;
  4006. const int nr = ggml_nrows(src0);
  4007. GGML_TENSOR_UNARY_OP_LOCALS
  4008. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  4009. GGML_ASSERT(src1->type == GGML_TYPE_F16);
  4010. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  4011. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  4012. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  4013. // rows per thread
  4014. const int dr = (nr + nth - 1)/nth;
  4015. // row range for this thread
  4016. const int ir0 = dr*ith;
  4017. const int ir1 = MIN(ir0 + dr, nr);
  4018. for (int ir = ir0; ir < ir1; ++ir) {
  4019. // src0 and dst are same shape => same indices
  4020. const int i3 = ir/(ne2*ne1);
  4021. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4022. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4023. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  4024. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4025. for (int i = 0; i < ne0; i++) {
  4026. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + v);
  4027. }
  4028. }
  4029. }
  4030. static void ggml_compute_forward_add1_q_f32(
  4031. const struct ggml_compute_params * params,
  4032. struct ggml_tensor * dst) {
  4033. const struct ggml_tensor * src0 = dst->src[0];
  4034. const struct ggml_tensor * src1 = dst->src[1];
  4035. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4036. GGML_ASSERT(ggml_is_scalar(src1));
  4037. // scalar to add
  4038. const float v = *(float *) src1->data;
  4039. const int ith = params->ith;
  4040. const int nth = params->nth;
  4041. const int nr = ggml_nrows(src0);
  4042. GGML_TENSOR_UNARY_OP_LOCALS
  4043. const enum ggml_type type = src0->type;
  4044. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  4045. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(type)->from_float;
  4046. // we don't support permuted src0
  4047. GGML_ASSERT(nb00 == ggml_type_size(type));
  4048. // dst cannot be transposed or permuted
  4049. GGML_ASSERT(nb0 <= nb1);
  4050. GGML_ASSERT(nb1 <= nb2);
  4051. GGML_ASSERT(nb2 <= nb3);
  4052. GGML_ASSERT(ggml_is_quantized(src0->type));
  4053. GGML_ASSERT(dst->type == src0->type);
  4054. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4055. // rows per thread
  4056. const int dr = (nr + nth - 1)/nth;
  4057. // row range for this thread
  4058. const int ir0 = dr*ith;
  4059. const int ir1 = MIN(ir0 + dr, nr);
  4060. float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith;
  4061. for (int ir = ir0; ir < ir1; ++ir) {
  4062. // src0 and dst are same shape => same indices
  4063. const int i3 = ir/(ne2*ne1);
  4064. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4065. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4066. void * src0_row = (void *) ((char *) src0->data + (i1*nb01 + i2*nb02 + i3*nb03));
  4067. void * dst_row = (void *) ((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb0 ));
  4068. assert(ne0 % 32 == 0);
  4069. // unquantize row from src0 to temp buffer
  4070. dequantize_row_q(src0_row, wdata, ne0);
  4071. // add src1
  4072. ggml_vec_acc1_f32(ne0, wdata, v);
  4073. // quantize row to dst
  4074. quantize_row_q(wdata, dst_row, ne0);
  4075. }
  4076. }
  4077. static void ggml_compute_forward_add1_bf16_f32(
  4078. const struct ggml_compute_params * params,
  4079. struct ggml_tensor * dst) {
  4080. const struct ggml_tensor * src0 = dst->src[0];
  4081. const struct ggml_tensor * src1 = dst->src[1];
  4082. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4083. GGML_ASSERT(ggml_is_scalar(src1));
  4084. // scalar to add
  4085. const float v = *(float *) src1->data;
  4086. const int ith = params->ith;
  4087. const int nth = params->nth;
  4088. const int nr = ggml_nrows(src0);
  4089. GGML_TENSOR_UNARY_OP_LOCALS
  4090. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  4091. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4092. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  4093. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  4094. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  4095. // rows per thread
  4096. const int dr = (nr + nth - 1)/nth;
  4097. // row range for this thread
  4098. const int ir0 = dr*ith;
  4099. const int ir1 = MIN(ir0 + dr, nr);
  4100. for (int ir = ir0; ir < ir1; ++ir) {
  4101. // src0 and dst are same shape => same indices
  4102. const int i3 = ir/(ne2*ne1);
  4103. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4104. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4105. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  4106. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4107. for (int i = 0; i < ne0; i++) {
  4108. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v);
  4109. }
  4110. }
  4111. }
  4112. static void ggml_compute_forward_add1_bf16_bf16(
  4113. const struct ggml_compute_params * params,
  4114. struct ggml_tensor * dst) {
  4115. const struct ggml_tensor * src0 = dst->src[0];
  4116. const struct ggml_tensor * src1 = dst->src[1];
  4117. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4118. GGML_ASSERT(ggml_is_scalar(src1));
  4119. // scalar to add
  4120. const float v = GGML_BF16_TO_FP32(*(ggml_bf16_t *) src1->data);
  4121. const int ith = params->ith;
  4122. const int nth = params->nth;
  4123. const int nr = ggml_nrows(src0);
  4124. GGML_TENSOR_UNARY_OP_LOCALS
  4125. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  4126. GGML_ASSERT(src1->type == GGML_TYPE_BF16);
  4127. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  4128. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  4129. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  4130. // rows per thread
  4131. const int dr = (nr + nth - 1)/nth;
  4132. // row range for this thread
  4133. const int ir0 = dr*ith;
  4134. const int ir1 = MIN(ir0 + dr, nr);
  4135. for (int ir = ir0; ir < ir1; ++ir) {
  4136. // src0 and dst are same shape => same indices
  4137. const int i3 = ir/(ne2*ne1);
  4138. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4139. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4140. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  4141. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4142. for (int i = 0; i < ne0; i++) {
  4143. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v);
  4144. }
  4145. }
  4146. }
  4147. static void ggml_compute_forward_add1(
  4148. const struct ggml_compute_params * params,
  4149. struct ggml_tensor * dst) {
  4150. const struct ggml_tensor * src0 = dst->src[0];
  4151. const struct ggml_tensor * src1 = dst->src[1];
  4152. switch (src0->type) {
  4153. case GGML_TYPE_F32:
  4154. {
  4155. ggml_compute_forward_add1_f32(params, dst);
  4156. } break;
  4157. case GGML_TYPE_F16:
  4158. {
  4159. if (src1->type == GGML_TYPE_F16) {
  4160. ggml_compute_forward_add1_f16_f16(params, dst);
  4161. }
  4162. else if (src1->type == GGML_TYPE_F32) {
  4163. ggml_compute_forward_add1_f16_f32(params, dst);
  4164. }
  4165. else {
  4166. GGML_ABORT("fatal error");
  4167. }
  4168. } break;
  4169. case GGML_TYPE_BF16:
  4170. {
  4171. if (src1->type == GGML_TYPE_BF16) {
  4172. ggml_compute_forward_add1_bf16_bf16(params, dst);
  4173. }
  4174. else if (src1->type == GGML_TYPE_F32) {
  4175. ggml_compute_forward_add1_bf16_f32(params, dst);
  4176. }
  4177. else {
  4178. GGML_ABORT("fatal error");
  4179. }
  4180. } break;
  4181. case GGML_TYPE_Q4_0:
  4182. case GGML_TYPE_Q4_1:
  4183. case GGML_TYPE_Q5_0:
  4184. case GGML_TYPE_Q5_1:
  4185. case GGML_TYPE_Q8_0:
  4186. case GGML_TYPE_Q8_1:
  4187. case GGML_TYPE_Q2_K:
  4188. case GGML_TYPE_Q3_K:
  4189. case GGML_TYPE_Q4_K:
  4190. case GGML_TYPE_Q5_K:
  4191. case GGML_TYPE_Q6_K:
  4192. case GGML_TYPE_TQ1_0:
  4193. case GGML_TYPE_TQ2_0:
  4194. case GGML_TYPE_IQ2_XXS:
  4195. case GGML_TYPE_IQ2_XS:
  4196. case GGML_TYPE_IQ3_XXS:
  4197. case GGML_TYPE_IQ1_S:
  4198. case GGML_TYPE_IQ1_M:
  4199. case GGML_TYPE_IQ4_NL:
  4200. case GGML_TYPE_IQ4_XS:
  4201. case GGML_TYPE_IQ3_S:
  4202. case GGML_TYPE_IQ2_S:
  4203. {
  4204. ggml_compute_forward_add1_q_f32(params, dst);
  4205. } break;
  4206. default:
  4207. {
  4208. GGML_ABORT("fatal error");
  4209. }
  4210. }
  4211. }
  4212. // ggml_compute_forward_acc
  4213. static void ggml_compute_forward_acc_f32(
  4214. const struct ggml_compute_params * params,
  4215. struct ggml_tensor * dst) {
  4216. const struct ggml_tensor * src0 = dst->src[0];
  4217. const struct ggml_tensor * src1 = dst->src[1];
  4218. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4219. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  4220. // view src0 and dst with these strides and data offset inbytes during acc
  4221. // nb0 is implicitly element_size because src0 and dst are contiguous
  4222. size_t nb1 = ((int32_t *) dst->op_params)[0];
  4223. size_t nb2 = ((int32_t *) dst->op_params)[1];
  4224. size_t nb3 = ((int32_t *) dst->op_params)[2];
  4225. size_t offset = ((int32_t *) dst->op_params)[3];
  4226. bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  4227. if (!inplace) {
  4228. if (params->ith == 0) {
  4229. // memcpy needs to be synchronized across threads to avoid race conditions.
  4230. // => do it in INIT phase
  4231. memcpy(
  4232. ((char *) dst->data),
  4233. ((char *) src0->data),
  4234. ggml_nbytes(dst));
  4235. }
  4236. ggml_barrier(params->threadpool);
  4237. }
  4238. const int ith = params->ith;
  4239. const int nth = params->nth;
  4240. const int nr = ggml_nrows(src1);
  4241. const int nc = src1->ne[0];
  4242. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne)
  4243. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  4244. // src0 and dst as viewed during acc
  4245. const size_t nb0 = ggml_element_size(src0);
  4246. const size_t nb00 = nb0;
  4247. const size_t nb01 = nb1;
  4248. const size_t nb02 = nb2;
  4249. const size_t nb03 = nb3;
  4250. 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));
  4251. 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));
  4252. GGML_ASSERT(nb10 == sizeof(float));
  4253. // rows per thread
  4254. const int dr = (nr + nth - 1)/nth;
  4255. // row range for this thread
  4256. const int ir0 = dr*ith;
  4257. const int ir1 = MIN(ir0 + dr, nr);
  4258. for (int ir = ir0; ir < ir1; ++ir) {
  4259. // src0 and dst are viewed with shape of src1 and offset
  4260. // => same indices
  4261. const int i3 = ir/(ne12*ne11);
  4262. const int i2 = (ir - i3*ne12*ne11)/ne11;
  4263. const int i1 = (ir - i3*ne12*ne11 - i2*ne11);
  4264. #ifdef GGML_USE_ACCELERATE
  4265. vDSP_vadd(
  4266. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset), 1,
  4267. (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11), 1,
  4268. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), 1, nc);
  4269. #else
  4270. ggml_vec_add_f32(nc,
  4271. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset),
  4272. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset),
  4273. (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
  4274. #endif
  4275. }
  4276. }
  4277. static void ggml_compute_forward_acc(
  4278. const struct ggml_compute_params * params,
  4279. struct ggml_tensor * dst) {
  4280. const struct ggml_tensor * src0 = dst->src[0];
  4281. switch (src0->type) {
  4282. case GGML_TYPE_F32:
  4283. {
  4284. ggml_compute_forward_acc_f32(params, dst);
  4285. } break;
  4286. case GGML_TYPE_F16:
  4287. case GGML_TYPE_BF16:
  4288. case GGML_TYPE_Q4_0:
  4289. case GGML_TYPE_Q4_1:
  4290. case GGML_TYPE_Q5_0:
  4291. case GGML_TYPE_Q5_1:
  4292. case GGML_TYPE_Q8_0:
  4293. case GGML_TYPE_Q8_1:
  4294. case GGML_TYPE_Q2_K:
  4295. case GGML_TYPE_Q3_K:
  4296. case GGML_TYPE_Q4_K:
  4297. case GGML_TYPE_Q5_K:
  4298. case GGML_TYPE_Q6_K:
  4299. case GGML_TYPE_TQ1_0:
  4300. case GGML_TYPE_TQ2_0:
  4301. case GGML_TYPE_IQ2_XXS:
  4302. case GGML_TYPE_IQ2_XS:
  4303. case GGML_TYPE_IQ3_XXS:
  4304. case GGML_TYPE_IQ1_S:
  4305. case GGML_TYPE_IQ1_M:
  4306. case GGML_TYPE_IQ4_NL:
  4307. case GGML_TYPE_IQ4_XS:
  4308. case GGML_TYPE_IQ3_S:
  4309. case GGML_TYPE_IQ2_S:
  4310. default:
  4311. {
  4312. GGML_ABORT("fatal error");
  4313. }
  4314. }
  4315. }
  4316. // ggml_compute_forward_sub
  4317. static void ggml_compute_forward_sub_f32(
  4318. const struct ggml_compute_params * params,
  4319. struct ggml_tensor * dst) {
  4320. const struct ggml_tensor * src0 = dst->src[0];
  4321. const struct ggml_tensor * src1 = dst->src[1];
  4322. assert(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  4323. const int ith = params->ith;
  4324. const int nth = params->nth;
  4325. const int nr = ggml_nrows(src0);
  4326. GGML_TENSOR_BINARY_OP_LOCALS
  4327. GGML_ASSERT( nb0 == sizeof(float));
  4328. GGML_ASSERT(nb00 == sizeof(float));
  4329. // rows per thread
  4330. const int dr = (nr + nth - 1)/nth;
  4331. // row range for this thread
  4332. const int ir0 = dr*ith;
  4333. const int ir1 = MIN(ir0 + dr, nr);
  4334. if (nb10 == sizeof(float)) {
  4335. for (int ir = ir0; ir < ir1; ++ir) {
  4336. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4337. const int64_t i03 = ir/(ne02*ne01);
  4338. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4339. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4340. const int64_t i13 = i03 % ne13;
  4341. const int64_t i12 = i02 % ne12;
  4342. const int64_t i11 = i01 % ne11;
  4343. const int64_t nr0 = ne00 / ne10;
  4344. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4345. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4346. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  4347. for (int64_t r = 0; r < nr0; ++r) {
  4348. #ifdef GGML_USE_ACCELERATE
  4349. vDSP_vsub(src1_ptr, 1, src0_ptr + r*ne10, 1, dst_ptr + r*ne10, 1, ne10);
  4350. #else
  4351. ggml_vec_sub_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  4352. #endif
  4353. }
  4354. }
  4355. } else {
  4356. // src1 is not contiguous
  4357. for (int ir = ir0; ir < ir1; ++ir) {
  4358. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4359. const int64_t i03 = ir/(ne02*ne01);
  4360. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4361. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4362. const int64_t i13 = i03 % ne13;
  4363. const int64_t i12 = i02 % ne12;
  4364. const int64_t i11 = i01 % ne11;
  4365. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4366. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4367. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  4368. const int64_t i10 = i0 % ne10;
  4369. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  4370. dst_ptr[i0] = src0_ptr[i0] - *src1_ptr;
  4371. }
  4372. }
  4373. }
  4374. }
  4375. static void ggml_compute_forward_sub(
  4376. const struct ggml_compute_params * params,
  4377. struct ggml_tensor * dst) {
  4378. const struct ggml_tensor * src0 = dst->src[0];
  4379. switch (src0->type) {
  4380. case GGML_TYPE_F32:
  4381. {
  4382. ggml_compute_forward_sub_f32(params, dst);
  4383. } break;
  4384. default:
  4385. {
  4386. GGML_ABORT("fatal error");
  4387. }
  4388. }
  4389. }
  4390. // ggml_compute_forward_mul
  4391. static void ggml_compute_forward_mul_f32(
  4392. const struct ggml_compute_params * params,
  4393. struct ggml_tensor * dst) {
  4394. const struct ggml_tensor * src0 = dst->src[0];
  4395. const struct ggml_tensor * src1 = dst->src[1];
  4396. GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  4397. const int ith = params->ith;
  4398. const int nth = params->nth;
  4399. const int64_t nr = ggml_nrows(src0);
  4400. GGML_TENSOR_BINARY_OP_LOCALS
  4401. GGML_ASSERT( nb0 == sizeof(float));
  4402. GGML_ASSERT(nb00 == sizeof(float));
  4403. if (nb10 == sizeof(float)) {
  4404. for (int64_t ir = ith; ir < nr; ir += nth) {
  4405. // src0 and dst are same shape => same indices
  4406. const int64_t i03 = ir/(ne02*ne01);
  4407. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4408. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4409. const int64_t i13 = i03 % ne13;
  4410. const int64_t i12 = i02 % ne12;
  4411. const int64_t i11 = i01 % ne11;
  4412. const int64_t nr0 = ne00 / ne10;
  4413. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4414. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4415. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  4416. for (int64_t r = 0 ; r < nr0; ++r) {
  4417. #ifdef GGML_USE_ACCELERATE
  4418. UNUSED(ggml_vec_mul_f32);
  4419. vDSP_vmul(src0_ptr + r*ne10, 1, src1_ptr, 1, dst_ptr + r*ne10, 1, ne10);
  4420. #else
  4421. ggml_vec_mul_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  4422. #endif
  4423. }
  4424. }
  4425. } else {
  4426. // src1 is not contiguous
  4427. for (int64_t ir = ith; ir < nr; ir += nth) {
  4428. // src0 and dst are same shape => same indices
  4429. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4430. const int64_t i03 = ir/(ne02*ne01);
  4431. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4432. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4433. const int64_t i13 = i03 % ne13;
  4434. const int64_t i12 = i02 % ne12;
  4435. const int64_t i11 = i01 % ne11;
  4436. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4437. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4438. for (int64_t i0 = 0; i0 < ne00; ++i0) {
  4439. const int64_t i10 = i0 % ne10;
  4440. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  4441. dst_ptr[i0] = src0_ptr[i0] * (*src1_ptr);
  4442. }
  4443. }
  4444. }
  4445. }
  4446. static void ggml_compute_forward_mul(
  4447. const struct ggml_compute_params * params,
  4448. struct ggml_tensor * dst) {
  4449. const struct ggml_tensor * src0 = dst->src[0];
  4450. const struct ggml_tensor * src1 = dst->src[1];
  4451. GGML_ASSERT(src1->type == GGML_TYPE_F32 && "only f32 src1 supported for now");
  4452. switch (src0->type) {
  4453. case GGML_TYPE_F32:
  4454. {
  4455. ggml_compute_forward_mul_f32(params, dst);
  4456. } break;
  4457. default:
  4458. {
  4459. GGML_ABORT("fatal error");
  4460. }
  4461. }
  4462. }
  4463. // ggml_compute_forward_div
  4464. static void ggml_compute_forward_div_f32(
  4465. const struct ggml_compute_params * params,
  4466. struct ggml_tensor * dst) {
  4467. const struct ggml_tensor * src0 = dst->src[0];
  4468. const struct ggml_tensor * src1 = dst->src[1];
  4469. GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  4470. const int ith = params->ith;
  4471. const int nth = params->nth;
  4472. const int64_t nr = ggml_nrows(src0);
  4473. GGML_TENSOR_BINARY_OP_LOCALS
  4474. GGML_ASSERT( nb0 == sizeof(float));
  4475. GGML_ASSERT(nb00 == sizeof(float));
  4476. if (nb10 == sizeof(float)) {
  4477. for (int64_t ir = ith; ir < nr; ir += nth) {
  4478. // src0 and dst are same shape => same indices
  4479. const int64_t i03 = ir/(ne02*ne01);
  4480. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4481. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4482. const int64_t i13 = i03 % ne13;
  4483. const int64_t i12 = i02 % ne12;
  4484. const int64_t i11 = i01 % ne11;
  4485. const int64_t nr0 = ne00 / ne10;
  4486. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4487. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4488. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  4489. for (int64_t r = 0; r < nr0; ++r) {
  4490. #ifdef GGML_USE_ACCELERATE
  4491. UNUSED(ggml_vec_div_f32);
  4492. vDSP_vdiv(src1_ptr, 1, src0_ptr + r*ne10, 1, dst_ptr + r*ne10, 1, ne10);
  4493. #else
  4494. ggml_vec_div_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  4495. #endif
  4496. }
  4497. }
  4498. } else {
  4499. // src1 is not contiguous
  4500. for (int64_t ir = ith; ir < nr; ir += nth) {
  4501. // src0 and dst are same shape => same indices
  4502. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4503. const int64_t i03 = ir/(ne02*ne01);
  4504. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4505. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4506. const int64_t i13 = i03 % ne13;
  4507. const int64_t i12 = i02 % ne12;
  4508. const int64_t i11 = i01 % ne11;
  4509. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4510. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4511. for (int64_t i0 = 0; i0 < ne00; ++i0) {
  4512. const int64_t i10 = i0 % ne10;
  4513. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  4514. dst_ptr[i0] = src0_ptr[i0] / (*src1_ptr);
  4515. }
  4516. }
  4517. }
  4518. }
  4519. static void ggml_compute_forward_div(
  4520. const struct ggml_compute_params * params,
  4521. struct ggml_tensor * dst) {
  4522. const struct ggml_tensor * src0 = dst->src[0];
  4523. switch (src0->type) {
  4524. case GGML_TYPE_F32:
  4525. {
  4526. ggml_compute_forward_div_f32(params, dst);
  4527. } break;
  4528. default:
  4529. {
  4530. GGML_ABORT("fatal error");
  4531. }
  4532. }
  4533. }
  4534. // ggml_compute_forward_sqr
  4535. static void ggml_compute_forward_sqr_f32(
  4536. const struct ggml_compute_params * params,
  4537. struct ggml_tensor * dst) {
  4538. const struct ggml_tensor * src0 = dst->src[0];
  4539. if (params->ith != 0) {
  4540. return;
  4541. }
  4542. assert(ggml_are_same_shape(src0, dst));
  4543. const int n = ggml_nrows(src0);
  4544. const int nc = src0->ne[0];
  4545. assert( dst->nb[0] == sizeof(float));
  4546. assert(src0->nb[0] == sizeof(float));
  4547. for (int i = 0; i < n; i++) {
  4548. ggml_vec_sqr_f32(nc,
  4549. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4550. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4551. }
  4552. }
  4553. static void ggml_compute_forward_sqr(
  4554. const struct ggml_compute_params * params,
  4555. struct ggml_tensor * dst) {
  4556. const struct ggml_tensor * src0 = dst->src[0];
  4557. switch (src0->type) {
  4558. case GGML_TYPE_F32:
  4559. {
  4560. ggml_compute_forward_sqr_f32(params, dst);
  4561. } break;
  4562. default:
  4563. {
  4564. GGML_ABORT("fatal error");
  4565. }
  4566. }
  4567. }
  4568. // ggml_compute_forward_sqrt
  4569. static void ggml_compute_forward_sqrt_f32(
  4570. const struct ggml_compute_params * params,
  4571. struct ggml_tensor * dst) {
  4572. const struct ggml_tensor * src0 = dst->src[0];
  4573. if (params->ith != 0) {
  4574. return;
  4575. }
  4576. assert(ggml_are_same_shape(src0, dst));
  4577. const int n = ggml_nrows(src0);
  4578. const int nc = src0->ne[0];
  4579. assert( dst->nb[0] == sizeof(float));
  4580. assert(src0->nb[0] == sizeof(float));
  4581. for (int i = 0; i < n; i++) {
  4582. ggml_vec_sqrt_f32(nc,
  4583. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4584. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4585. }
  4586. }
  4587. static void ggml_compute_forward_sqrt(
  4588. const struct ggml_compute_params * params,
  4589. struct ggml_tensor * dst) {
  4590. const struct ggml_tensor * src0 = dst->src[0];
  4591. switch (src0->type) {
  4592. case GGML_TYPE_F32:
  4593. {
  4594. ggml_compute_forward_sqrt_f32(params, dst);
  4595. } break;
  4596. default:
  4597. {
  4598. GGML_ABORT("fatal error");
  4599. }
  4600. }
  4601. }
  4602. // ggml_compute_forward_log
  4603. static void ggml_compute_forward_log_f32(
  4604. const struct ggml_compute_params * params,
  4605. struct ggml_tensor * dst) {
  4606. const struct ggml_tensor * src0 = dst->src[0];
  4607. if (params->ith != 0) {
  4608. return;
  4609. }
  4610. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4611. const int n = ggml_nrows(src0);
  4612. const int nc = src0->ne[0];
  4613. GGML_ASSERT( dst->nb[0] == sizeof(float));
  4614. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4615. for (int i = 0; i < n; i++) {
  4616. ggml_vec_log_f32(nc,
  4617. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4618. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4619. }
  4620. }
  4621. static void ggml_compute_forward_log(
  4622. const struct ggml_compute_params * params,
  4623. struct ggml_tensor * dst) {
  4624. const struct ggml_tensor * src0 = dst->src[0];
  4625. switch (src0->type) {
  4626. case GGML_TYPE_F32:
  4627. {
  4628. ggml_compute_forward_log_f32(params, dst);
  4629. } break;
  4630. default:
  4631. {
  4632. GGML_ABORT("fatal error");
  4633. }
  4634. }
  4635. }
  4636. // ggml_compute_forward_sin
  4637. static void ggml_compute_forward_sin_f32(
  4638. const struct ggml_compute_params * params,
  4639. struct ggml_tensor * dst) {
  4640. const struct ggml_tensor * src0 = dst->src[0];
  4641. if (params->ith != 0) {
  4642. return;
  4643. }
  4644. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4645. const int n = ggml_nrows(src0);
  4646. const int nc = src0->ne[0];
  4647. GGML_ASSERT( dst->nb[0] == sizeof(float));
  4648. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4649. for (int i = 0; i < n; i++) {
  4650. ggml_vec_sin_f32(nc,
  4651. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4652. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4653. }
  4654. }
  4655. static void ggml_compute_forward_sin(
  4656. const struct ggml_compute_params * params,
  4657. struct ggml_tensor * dst) {
  4658. const struct ggml_tensor * src0 = dst->src[0];
  4659. switch (src0->type) {
  4660. case GGML_TYPE_F32:
  4661. {
  4662. ggml_compute_forward_sin_f32(params, dst);
  4663. } break;
  4664. default:
  4665. {
  4666. GGML_ABORT("fatal error");
  4667. }
  4668. }
  4669. }
  4670. // ggml_compute_forward_cos
  4671. static void ggml_compute_forward_cos_f32(
  4672. const struct ggml_compute_params * params,
  4673. struct ggml_tensor * dst) {
  4674. const struct ggml_tensor * src0 = dst->src[0];
  4675. if (params->ith != 0) {
  4676. return;
  4677. }
  4678. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4679. const int n = ggml_nrows(src0);
  4680. const int nc = src0->ne[0];
  4681. GGML_ASSERT( dst->nb[0] == sizeof(float));
  4682. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4683. for (int i = 0; i < n; i++) {
  4684. ggml_vec_cos_f32(nc,
  4685. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4686. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4687. }
  4688. }
  4689. static void ggml_compute_forward_cos(
  4690. const struct ggml_compute_params * params,
  4691. struct ggml_tensor * dst) {
  4692. const struct ggml_tensor * src0 = dst->src[0];
  4693. switch (src0->type) {
  4694. case GGML_TYPE_F32:
  4695. {
  4696. ggml_compute_forward_cos_f32(params, dst);
  4697. } break;
  4698. default:
  4699. {
  4700. GGML_ABORT("fatal error");
  4701. }
  4702. }
  4703. }
  4704. // ggml_compute_forward_sum
  4705. static void ggml_compute_forward_sum_f32(
  4706. const struct ggml_compute_params * params,
  4707. struct ggml_tensor * dst) {
  4708. const struct ggml_tensor * src0 = dst->src[0];
  4709. if (params->ith != 0) {
  4710. return;
  4711. }
  4712. assert(ggml_is_scalar(dst));
  4713. assert(src0->nb[0] == sizeof(float));
  4714. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  4715. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb)
  4716. ggml_float sum = 0;
  4717. ggml_float row_sum = 0;
  4718. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4719. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4720. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4721. ggml_vec_sum_f32_ggf(ne00,
  4722. &row_sum,
  4723. (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03));
  4724. sum += row_sum;
  4725. }
  4726. }
  4727. }
  4728. ((float *) dst->data)[0] = sum;
  4729. }
  4730. static void ggml_compute_forward_sum_f16(
  4731. const struct ggml_compute_params * params,
  4732. struct ggml_tensor * dst) {
  4733. const struct ggml_tensor * src0 = dst->src[0];
  4734. if (params->ith != 0) {
  4735. return;
  4736. }
  4737. assert(ggml_is_scalar(dst));
  4738. assert(src0->nb[0] == sizeof(ggml_fp16_t));
  4739. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  4740. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb)
  4741. float sum = 0;
  4742. float row_sum = 0;
  4743. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4744. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4745. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4746. ggml_vec_sum_f16_ggf(ne00,
  4747. &row_sum,
  4748. (ggml_fp16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03));
  4749. sum += row_sum;
  4750. }
  4751. }
  4752. }
  4753. ((ggml_fp16_t *) dst->data)[0] = GGML_FP32_TO_FP16(sum);
  4754. }
  4755. static void ggml_compute_forward_sum_bf16(
  4756. const struct ggml_compute_params * params,
  4757. struct ggml_tensor * dst) {
  4758. const struct ggml_tensor * src0 = dst->src[0];
  4759. if (params->ith != 0) {
  4760. return;
  4761. }
  4762. assert(ggml_is_scalar(dst));
  4763. assert(src0->nb[0] == sizeof(ggml_bf16_t));
  4764. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  4765. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb)
  4766. float sum = 0;
  4767. float row_sum = 0;
  4768. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4769. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4770. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4771. ggml_vec_sum_bf16_ggf(ne00,
  4772. &row_sum,
  4773. (ggml_bf16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03));
  4774. sum += row_sum;
  4775. }
  4776. }
  4777. }
  4778. ((ggml_bf16_t *) dst->data)[0] = GGML_FP32_TO_BF16(sum);
  4779. }
  4780. static void ggml_compute_forward_sum(
  4781. const struct ggml_compute_params * params,
  4782. struct ggml_tensor * dst) {
  4783. const struct ggml_tensor * src0 = dst->src[0];
  4784. switch (src0->type) {
  4785. case GGML_TYPE_F32:
  4786. {
  4787. ggml_compute_forward_sum_f32(params, dst);
  4788. } break;
  4789. case GGML_TYPE_F16:
  4790. {
  4791. ggml_compute_forward_sum_f16(params, dst);
  4792. } break;
  4793. case GGML_TYPE_BF16:
  4794. {
  4795. ggml_compute_forward_sum_bf16(params, dst);
  4796. } break;
  4797. default:
  4798. {
  4799. GGML_ABORT("fatal error");
  4800. }
  4801. }
  4802. }
  4803. // ggml_compute_forward_sum_rows
  4804. static void ggml_compute_forward_sum_rows_f32(
  4805. const struct ggml_compute_params * params,
  4806. struct ggml_tensor * dst) {
  4807. const struct ggml_tensor * src0 = dst->src[0];
  4808. if (params->ith != 0) {
  4809. return;
  4810. }
  4811. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4812. GGML_ASSERT(dst->nb[0] == sizeof(float));
  4813. GGML_TENSOR_UNARY_OP_LOCALS
  4814. GGML_ASSERT(ne0 == 1);
  4815. GGML_ASSERT(ne1 == ne01);
  4816. GGML_ASSERT(ne2 == ne02);
  4817. GGML_ASSERT(ne3 == ne03);
  4818. for (int64_t i3 = 0; i3 < ne03; i3++) {
  4819. for (int64_t i2 = 0; i2 < ne02; i2++) {
  4820. for (int64_t i1 = 0; i1 < ne01; i1++) {
  4821. float * src_row = (float *) ((char *) src0->data + i1*nb01 + i2*nb02 + i3*nb03);
  4822. float * dst_row = (float *) ((char *) dst->data + i1*nb1 + i2*nb2 + i3*nb3);
  4823. float row_sum = 0;
  4824. ggml_vec_sum_f32(ne00, &row_sum, src_row);
  4825. dst_row[0] = row_sum;
  4826. }
  4827. }
  4828. }
  4829. }
  4830. static void ggml_compute_forward_sum_rows(
  4831. const struct ggml_compute_params * params,
  4832. struct ggml_tensor * dst) {
  4833. const struct ggml_tensor * src0 = dst->src[0];
  4834. switch (src0->type) {
  4835. case GGML_TYPE_F32:
  4836. {
  4837. ggml_compute_forward_sum_rows_f32(params, dst);
  4838. } break;
  4839. default:
  4840. {
  4841. GGML_ABORT("fatal error");
  4842. }
  4843. }
  4844. }
  4845. // ggml_compute_forward_mean
  4846. static void ggml_compute_forward_mean_f32(
  4847. const struct ggml_compute_params * params,
  4848. struct ggml_tensor * dst) {
  4849. const struct ggml_tensor * src0 = dst->src[0];
  4850. if (params->ith != 0) {
  4851. return;
  4852. }
  4853. assert(src0->nb[0] == sizeof(float));
  4854. GGML_TENSOR_UNARY_OP_LOCALS
  4855. assert(ne0 == 1);
  4856. assert(ne1 == ne01);
  4857. assert(ne2 == ne02);
  4858. assert(ne3 == ne03);
  4859. UNUSED(ne0);
  4860. UNUSED(ne1);
  4861. UNUSED(ne2);
  4862. UNUSED(ne3);
  4863. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4864. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4865. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4866. ggml_vec_sum_f32(ne00,
  4867. (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  4868. (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03));
  4869. *(float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3) /= (float) ne00;
  4870. }
  4871. }
  4872. }
  4873. }
  4874. static void ggml_compute_forward_mean(
  4875. const struct ggml_compute_params * params,
  4876. struct ggml_tensor * dst) {
  4877. const struct ggml_tensor * src0 = dst->src[0];
  4878. switch (src0->type) {
  4879. case GGML_TYPE_F32:
  4880. {
  4881. ggml_compute_forward_mean_f32(params, dst);
  4882. } break;
  4883. default:
  4884. {
  4885. GGML_ABORT("fatal error");
  4886. }
  4887. }
  4888. }
  4889. // ggml_compute_forward_argmax
  4890. static void ggml_compute_forward_argmax_f32(
  4891. const struct ggml_compute_params * params,
  4892. struct ggml_tensor * dst) {
  4893. const struct ggml_tensor * src0 = dst->src[0];
  4894. if (params->ith != 0) {
  4895. return;
  4896. }
  4897. assert(src0->nb[0] == sizeof(float));
  4898. assert(dst->nb[0] == sizeof(float));
  4899. const int64_t ne00 = src0->ne[0];
  4900. const int64_t ne01 = src0->ne[1];
  4901. const size_t nb01 = src0->nb[1];
  4902. const size_t nb0 = dst->nb[0];
  4903. for (int64_t i1 = 0; i1 < ne01; i1++) {
  4904. float * src = (float *) ((char *) src0->data + i1*nb01);
  4905. int32_t * dst_ = (int32_t *) ((char *) dst->data + i1*nb0);
  4906. int v = 0;
  4907. ggml_vec_argmax_f32(ne00, &v, src);
  4908. dst_[0] = v;
  4909. }
  4910. }
  4911. static void ggml_compute_forward_argmax(
  4912. const struct ggml_compute_params * params,
  4913. struct ggml_tensor * dst) {
  4914. const struct ggml_tensor * src0 = dst->src[0];
  4915. switch (src0->type) {
  4916. case GGML_TYPE_F32:
  4917. {
  4918. ggml_compute_forward_argmax_f32(params, dst);
  4919. } break;
  4920. default:
  4921. {
  4922. GGML_ABORT("fatal error");
  4923. }
  4924. }
  4925. }
  4926. // ggml_compute_forward_count_equal
  4927. static void ggml_compute_forward_count_equal_i32(
  4928. const struct ggml_compute_params * params,
  4929. struct ggml_tensor * dst) {
  4930. const struct ggml_tensor * src0 = dst->src[0];
  4931. const struct ggml_tensor * src1 = dst->src[1];
  4932. GGML_TENSOR_BINARY_OP_LOCALS;
  4933. GGML_ASSERT(src0->type == GGML_TYPE_I32);
  4934. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  4935. GGML_ASSERT(ggml_are_same_shape(src0, src1));
  4936. GGML_ASSERT(ggml_is_scalar(dst));
  4937. GGML_ASSERT(dst->type == GGML_TYPE_I64);
  4938. const int64_t nr = ggml_nrows(src0);
  4939. const int ith = params->ith;
  4940. const int nth = params->nth;
  4941. int64_t * sums = (int64_t *) params->wdata;
  4942. int64_t sum_thread = 0;
  4943. // rows per thread
  4944. const int64_t dr = (nr + nth - 1)/nth;
  4945. // row range for this thread
  4946. const int64_t ir0 = dr*ith;
  4947. const int64_t ir1 = MIN(ir0 + dr, nr);
  4948. for (int64_t ir = ir0; ir < ir1; ++ir) {
  4949. const int64_t i03 = ir / (ne02*ne01);
  4950. const int64_t i02 = (ir - i03*ne03) / ne01;
  4951. const int64_t i01 = ir - i03*ne03 - i02*ne02;
  4952. const char * data0 = (const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01;
  4953. const char * data1 = (const char *) src1->data + i03*nb13 + i02*nb12 + i01*nb11;
  4954. for (int64_t i00 = 0; i00 < ne00; ++i00) {
  4955. const int32_t val0 = *((const int32_t *) (data0 + i00*nb00));
  4956. const int32_t val1 = *((const int32_t *) (data1 + i00*nb10));
  4957. sum_thread += val0 == val1;
  4958. }
  4959. }
  4960. if (ith != 0) {
  4961. sums[ith] = sum_thread;
  4962. }
  4963. ggml_barrier(params->threadpool);
  4964. if (ith != 0) {
  4965. return;
  4966. }
  4967. for (int ith_other = 1; ith_other < nth; ++ith_other) {
  4968. sum_thread += sums[ith_other];
  4969. }
  4970. *((int64_t *) dst->data) = sum_thread;
  4971. }
  4972. static void ggml_compute_forward_count_equal(
  4973. const struct ggml_compute_params * params,
  4974. struct ggml_tensor * dst) {
  4975. const struct ggml_tensor * src0 = dst->src[0];
  4976. switch (src0->type) {
  4977. case GGML_TYPE_I32:
  4978. {
  4979. ggml_compute_forward_count_equal_i32(params, dst);
  4980. } break;
  4981. default:
  4982. {
  4983. GGML_ABORT("fatal error");
  4984. }
  4985. }
  4986. }
  4987. // ggml_compute_forward_repeat
  4988. static void ggml_compute_forward_repeat_f32(
  4989. const struct ggml_compute_params * params,
  4990. struct ggml_tensor * dst) {
  4991. const struct ggml_tensor * src0 = dst->src[0];
  4992. if (params->ith != 0) {
  4993. return;
  4994. }
  4995. GGML_ASSERT(ggml_can_repeat(src0, dst));
  4996. GGML_TENSOR_UNARY_OP_LOCALS
  4997. // guaranteed to be an integer due to the check in ggml_can_repeat
  4998. const int nr0 = (int)(ne0/ne00);
  4999. const int nr1 = (int)(ne1/ne01);
  5000. const int nr2 = (int)(ne2/ne02);
  5001. const int nr3 = (int)(ne3/ne03);
  5002. // TODO: support for transposed / permuted tensors
  5003. GGML_ASSERT(nb0 == sizeof(float));
  5004. GGML_ASSERT(nb00 == sizeof(float));
  5005. // TODO: maybe this is not optimal?
  5006. for (int i3 = 0; i3 < nr3; i3++) {
  5007. for (int k3 = 0; k3 < ne03; k3++) {
  5008. for (int i2 = 0; i2 < nr2; i2++) {
  5009. for (int k2 = 0; k2 < ne02; k2++) {
  5010. for (int i1 = 0; i1 < nr1; i1++) {
  5011. for (int k1 = 0; k1 < ne01; k1++) {
  5012. for (int i0 = 0; i0 < nr0; i0++) {
  5013. ggml_vec_cpy_f32(ne00,
  5014. (float *) ((char *) dst->data + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0),
  5015. (float *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01));
  5016. }
  5017. }
  5018. }
  5019. }
  5020. }
  5021. }
  5022. }
  5023. }
  5024. static void ggml_compute_forward_repeat_f16(
  5025. const struct ggml_compute_params * params,
  5026. struct ggml_tensor * dst) {
  5027. const struct ggml_tensor * src0 = dst->src[0];
  5028. if (params->ith != 0) {
  5029. return;
  5030. }
  5031. GGML_ASSERT(ggml_can_repeat(src0, dst));
  5032. GGML_TENSOR_UNARY_OP_LOCALS
  5033. // guaranteed to be an integer due to the check in ggml_can_repeat
  5034. const int nr0 = (int)(ne0/ne00);
  5035. const int nr1 = (int)(ne1/ne01);
  5036. const int nr2 = (int)(ne2/ne02);
  5037. const int nr3 = (int)(ne3/ne03);
  5038. // TODO: support for transposed / permuted tensors
  5039. GGML_ASSERT(nb0 == sizeof(ggml_fp16_t));
  5040. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  5041. // TODO: maybe this is not optimal?
  5042. for (int i3 = 0; i3 < nr3; i3++) {
  5043. for (int k3 = 0; k3 < ne03; k3++) {
  5044. for (int i2 = 0; i2 < nr2; i2++) {
  5045. for (int k2 = 0; k2 < ne02; k2++) {
  5046. for (int i1 = 0; i1 < nr1; i1++) {
  5047. for (int k1 = 0; k1 < ne01; k1++) {
  5048. for (int i0 = 0; i0 < nr0; i0++) {
  5049. 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);
  5050. ggml_fp16_t * x = (ggml_fp16_t *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01);
  5051. // ggml_vec_cpy_f16(ne00, y, x)
  5052. for (int i = 0; i < ne00; ++i) {
  5053. y[i] = x[i];
  5054. }
  5055. }
  5056. }
  5057. }
  5058. }
  5059. }
  5060. }
  5061. }
  5062. }
  5063. static void ggml_compute_forward_repeat(
  5064. const struct ggml_compute_params * params,
  5065. struct ggml_tensor * dst) {
  5066. const struct ggml_tensor * src0 = dst->src[0];
  5067. switch (src0->type) {
  5068. case GGML_TYPE_F16:
  5069. case GGML_TYPE_BF16:
  5070. case GGML_TYPE_I16:
  5071. {
  5072. ggml_compute_forward_repeat_f16(params, dst);
  5073. } break;
  5074. case GGML_TYPE_F32:
  5075. case GGML_TYPE_I32:
  5076. {
  5077. ggml_compute_forward_repeat_f32(params, dst);
  5078. } break;
  5079. default:
  5080. {
  5081. GGML_ABORT("fatal error");
  5082. }
  5083. }
  5084. }
  5085. // ggml_compute_forward_repeat_back
  5086. static void ggml_compute_forward_repeat_back_f32(
  5087. const struct ggml_compute_params * params,
  5088. struct ggml_tensor * dst) {
  5089. const struct ggml_tensor * src0 = dst->src[0];
  5090. if (params->ith != 0) {
  5091. return;
  5092. }
  5093. GGML_ASSERT(ggml_can_repeat(dst, src0));
  5094. GGML_TENSOR_UNARY_OP_LOCALS
  5095. // guaranteed to be an integer due to the check in ggml_can_repeat
  5096. const int nr0 = (int)(ne00/ne0);
  5097. const int nr1 = (int)(ne01/ne1);
  5098. const int nr2 = (int)(ne02/ne2);
  5099. const int nr3 = (int)(ne03/ne3);
  5100. // TODO: support for transposed / permuted tensors
  5101. GGML_ASSERT(nb0 == sizeof(float));
  5102. GGML_ASSERT(nb00 == sizeof(float));
  5103. if (ggml_is_contiguous(dst)) {
  5104. ggml_vec_set_f32(ne0*ne1*ne2*ne3, dst->data, 0);
  5105. } else {
  5106. for (int k3 = 0; k3 < ne3; k3++) {
  5107. for (int k2 = 0; k2 < ne2; k2++) {
  5108. for (int k1 = 0; k1 < ne1; k1++) {
  5109. ggml_vec_set_f32(ne0,
  5110. (float *) ((char *) dst->data + k1*nb1 + k2*nb2 + k3*nb3),
  5111. 0);
  5112. }
  5113. }
  5114. }
  5115. }
  5116. // TODO: maybe this is not optimal?
  5117. for (int i3 = 0; i3 < nr3; i3++) {
  5118. for (int k3 = 0; k3 < ne3; k3++) {
  5119. for (int i2 = 0; i2 < nr2; i2++) {
  5120. for (int k2 = 0; k2 < ne2; k2++) {
  5121. for (int i1 = 0; i1 < nr1; i1++) {
  5122. for (int k1 = 0; k1 < ne1; k1++) {
  5123. for (int i0 = 0; i0 < nr0; i0++) {
  5124. ggml_vec_acc_f32(ne0,
  5125. (float *) ((char *) dst->data + ( k3)*nb3 + ( k2)*nb2 + ( k1)*nb1),
  5126. (float *) ((char *) src0->data + (i3*ne3 + k3)*nb03 + (i2*ne2 + k2)*nb02 + (i1*ne1 + k1)*nb01 + (i0*ne0)*nb00));
  5127. }
  5128. }
  5129. }
  5130. }
  5131. }
  5132. }
  5133. }
  5134. }
  5135. static void ggml_compute_forward_repeat_back(
  5136. const struct ggml_compute_params * params,
  5137. struct ggml_tensor * dst) {
  5138. const struct ggml_tensor * src0 = dst->src[0];
  5139. switch (src0->type) {
  5140. case GGML_TYPE_F32:
  5141. {
  5142. ggml_compute_forward_repeat_back_f32(params, dst);
  5143. } break;
  5144. default:
  5145. {
  5146. GGML_ABORT("fatal error");
  5147. }
  5148. }
  5149. }
  5150. // ggml_compute_forward_concat
  5151. static void ggml_compute_forward_concat_f32(
  5152. const struct ggml_compute_params * params,
  5153. struct ggml_tensor * dst) {
  5154. const struct ggml_tensor * src0 = dst->src[0];
  5155. const struct ggml_tensor * src1 = dst->src[1];
  5156. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5157. const int ith = params->ith;
  5158. const int nth = params->nth;
  5159. GGML_TENSOR_BINARY_OP_LOCALS
  5160. const int32_t dim = ggml_get_op_params_i32(dst, 0);
  5161. GGML_ASSERT(dim >= 0 && dim < 4);
  5162. int64_t o[4] = {0, 0, 0, 0};
  5163. o[dim] = src0->ne[dim];
  5164. const float * x;
  5165. // TODO: smarter multi-theading
  5166. for (int i3 = 0; i3 < ne3; i3++) {
  5167. for (int i2 = ith; i2 < ne2; i2 += nth) {
  5168. for (int i1 = 0; i1 < ne1; i1++) {
  5169. for (int i0 = 0; i0 < ne0; i0++) {
  5170. if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
  5171. x = (const float *) ((const char *)src0->data + (i0 )*nb00 + (i1 )*nb01 + (i2 )*nb02 + (i3 )*nb03);
  5172. } else {
  5173. x = (const float *) ((const char *)src1->data + (i0 - o[0])*nb10 + (i1 - o[1])*nb11 + (i2 - o[2])*nb12 + (i3 - o[3])*nb13);
  5174. }
  5175. float * y = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3);
  5176. *y = *x;
  5177. }
  5178. }
  5179. }
  5180. }
  5181. }
  5182. static void ggml_compute_forward_concat(
  5183. const struct ggml_compute_params * params,
  5184. struct ggml_tensor * dst) {
  5185. const struct ggml_tensor * src0 = dst->src[0];
  5186. switch (src0->type) {
  5187. case GGML_TYPE_F32:
  5188. case GGML_TYPE_I32:
  5189. {
  5190. ggml_compute_forward_concat_f32(params, dst);
  5191. } break;
  5192. default:
  5193. {
  5194. GGML_ABORT("fatal error");
  5195. }
  5196. }
  5197. }
  5198. // ggml_compute_forward_abs
  5199. static void ggml_compute_forward_abs_f32(
  5200. const struct ggml_compute_params * params,
  5201. struct ggml_tensor * dst) {
  5202. const struct ggml_tensor * src0 = dst->src[0];
  5203. if (params->ith != 0) {
  5204. return;
  5205. }
  5206. assert(ggml_is_contiguous_1(src0));
  5207. assert(ggml_is_contiguous_1(dst));
  5208. assert(ggml_are_same_shape(src0, dst));
  5209. const int n = ggml_nrows(src0);
  5210. const int nc = src0->ne[0];
  5211. for (int i = 0; i < n; i++) {
  5212. ggml_vec_abs_f32(nc,
  5213. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5214. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5215. }
  5216. }
  5217. static void ggml_compute_forward_abs(
  5218. const struct ggml_compute_params * params,
  5219. struct ggml_tensor * dst) {
  5220. const struct ggml_tensor * src0 = dst->src[0];
  5221. switch (src0->type) {
  5222. case GGML_TYPE_F32:
  5223. {
  5224. ggml_compute_forward_abs_f32(params, dst);
  5225. } break;
  5226. default:
  5227. {
  5228. GGML_ABORT("fatal error");
  5229. }
  5230. }
  5231. }
  5232. // ggml_compute_forward_sgn
  5233. static void ggml_compute_forward_sgn_f32(
  5234. const struct ggml_compute_params * params,
  5235. struct ggml_tensor * dst) {
  5236. const struct ggml_tensor * src0 = dst->src[0];
  5237. if (params->ith != 0) {
  5238. return;
  5239. }
  5240. assert(ggml_is_contiguous_1(src0));
  5241. assert(ggml_is_contiguous_1(dst));
  5242. assert(ggml_are_same_shape(src0, dst));
  5243. const int n = ggml_nrows(src0);
  5244. const int nc = src0->ne[0];
  5245. for (int i = 0; i < n; i++) {
  5246. ggml_vec_sgn_f32(nc,
  5247. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5248. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5249. }
  5250. }
  5251. static void ggml_compute_forward_sgn(
  5252. const struct ggml_compute_params * params,
  5253. struct ggml_tensor * dst) {
  5254. const struct ggml_tensor * src0 = dst->src[0];
  5255. switch (src0->type) {
  5256. case GGML_TYPE_F32:
  5257. {
  5258. ggml_compute_forward_sgn_f32(params, dst);
  5259. } break;
  5260. default:
  5261. {
  5262. GGML_ABORT("fatal error");
  5263. }
  5264. }
  5265. }
  5266. // ggml_compute_forward_neg
  5267. static void ggml_compute_forward_neg_f32(
  5268. const struct ggml_compute_params * params,
  5269. struct ggml_tensor * dst) {
  5270. const struct ggml_tensor * src0 = dst->src[0];
  5271. if (params->ith != 0) {
  5272. return;
  5273. }
  5274. assert(ggml_is_contiguous_1(src0));
  5275. assert(ggml_is_contiguous_1(dst));
  5276. assert(ggml_are_same_shape(src0, dst));
  5277. const int n = ggml_nrows(src0);
  5278. const int nc = src0->ne[0];
  5279. for (int i = 0; i < n; i++) {
  5280. ggml_vec_neg_f32(nc,
  5281. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5282. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5283. }
  5284. }
  5285. static void ggml_compute_forward_neg(
  5286. const struct ggml_compute_params * params,
  5287. struct ggml_tensor * dst) {
  5288. const struct ggml_tensor * src0 = dst->src[0];
  5289. switch (src0->type) {
  5290. case GGML_TYPE_F32:
  5291. {
  5292. ggml_compute_forward_neg_f32(params, dst);
  5293. } break;
  5294. default:
  5295. {
  5296. GGML_ABORT("fatal error");
  5297. }
  5298. }
  5299. }
  5300. // ggml_compute_forward_step
  5301. static void ggml_compute_forward_step_f32(
  5302. const struct ggml_compute_params * params,
  5303. struct ggml_tensor * dst) {
  5304. const struct ggml_tensor * src0 = dst->src[0];
  5305. if (params->ith != 0) {
  5306. return;
  5307. }
  5308. assert(ggml_is_contiguous_1(src0));
  5309. assert(ggml_is_contiguous_1(dst));
  5310. assert(ggml_are_same_shape(src0, dst));
  5311. const int n = ggml_nrows(src0);
  5312. const int nc = src0->ne[0];
  5313. for (int i = 0; i < n; i++) {
  5314. ggml_vec_step_f32(nc,
  5315. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5316. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5317. }
  5318. }
  5319. static void ggml_compute_forward_step(
  5320. const struct ggml_compute_params * params,
  5321. struct ggml_tensor * dst) {
  5322. const struct ggml_tensor * src0 = dst->src[0];
  5323. switch (src0->type) {
  5324. case GGML_TYPE_F32:
  5325. {
  5326. ggml_compute_forward_step_f32(params, dst);
  5327. } break;
  5328. default:
  5329. {
  5330. GGML_ABORT("fatal error");
  5331. }
  5332. }
  5333. }
  5334. // ggml_compute_forward_tanh
  5335. static void ggml_compute_forward_tanh_f32(
  5336. const struct ggml_compute_params * params,
  5337. struct ggml_tensor * dst) {
  5338. const struct ggml_tensor * src0 = dst->src[0];
  5339. if (params->ith != 0) {
  5340. return;
  5341. }
  5342. assert(ggml_is_contiguous_1(src0));
  5343. assert(ggml_is_contiguous_1(dst));
  5344. assert(ggml_are_same_shape(src0, dst));
  5345. const int n = ggml_nrows(src0);
  5346. const int nc = src0->ne[0];
  5347. for (int i = 0; i < n; i++) {
  5348. ggml_vec_tanh_f32(nc,
  5349. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5350. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5351. }
  5352. }
  5353. static void ggml_compute_forward_tanh(
  5354. const struct ggml_compute_params * params,
  5355. struct ggml_tensor * dst) {
  5356. const struct ggml_tensor * src0 = dst->src[0];
  5357. switch (src0->type) {
  5358. case GGML_TYPE_F32:
  5359. {
  5360. ggml_compute_forward_tanh_f32(params, dst);
  5361. } break;
  5362. default:
  5363. {
  5364. GGML_ABORT("fatal error");
  5365. }
  5366. }
  5367. }
  5368. // ggml_compute_forward_elu
  5369. static void ggml_compute_forward_elu_f32(
  5370. const struct ggml_compute_params * params,
  5371. struct ggml_tensor * dst) {
  5372. const struct ggml_tensor * src0 = dst->src[0];
  5373. if (params->ith != 0) {
  5374. return;
  5375. }
  5376. assert(ggml_is_contiguous_1(src0));
  5377. assert(ggml_is_contiguous_1(dst));
  5378. assert(ggml_are_same_shape(src0, dst));
  5379. const int n = ggml_nrows(src0);
  5380. const int nc = src0->ne[0];
  5381. for (int i = 0; i < n; i++) {
  5382. ggml_vec_elu_f32(nc,
  5383. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5384. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5385. }
  5386. }
  5387. static void ggml_compute_forward_elu(
  5388. const struct ggml_compute_params * params,
  5389. struct ggml_tensor * dst) {
  5390. const struct ggml_tensor * src0 = dst->src[0];
  5391. switch (src0->type) {
  5392. case GGML_TYPE_F32:
  5393. {
  5394. ggml_compute_forward_elu_f32(params, dst);
  5395. } break;
  5396. default:
  5397. {
  5398. GGML_ABORT("fatal error");
  5399. }
  5400. }
  5401. }
  5402. // ggml_compute_forward_relu
  5403. static void ggml_compute_forward_relu_f32(
  5404. const struct ggml_compute_params * params,
  5405. struct ggml_tensor * dst) {
  5406. const struct ggml_tensor * src0 = dst->src[0];
  5407. if (params->ith != 0) {
  5408. return;
  5409. }
  5410. assert(ggml_is_contiguous_1(src0));
  5411. assert(ggml_is_contiguous_1(dst));
  5412. assert(ggml_are_same_shape(src0, dst));
  5413. const int n = ggml_nrows(src0);
  5414. const int nc = src0->ne[0];
  5415. for (int i = 0; i < n; i++) {
  5416. ggml_vec_relu_f32(nc,
  5417. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5418. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5419. }
  5420. }
  5421. static void ggml_compute_forward_relu(
  5422. const struct ggml_compute_params * params,
  5423. struct ggml_tensor * dst) {
  5424. const struct ggml_tensor * src0 = dst->src[0];
  5425. switch (src0->type) {
  5426. case GGML_TYPE_F32:
  5427. {
  5428. ggml_compute_forward_relu_f32(params, dst);
  5429. } break;
  5430. default:
  5431. {
  5432. GGML_ABORT("fatal error");
  5433. }
  5434. }
  5435. }
  5436. // ggml_compute_forward_sigmoid
  5437. static void ggml_compute_forward_sigmoid_f32(
  5438. const struct ggml_compute_params * params,
  5439. struct ggml_tensor * dst) {
  5440. const struct ggml_tensor * src0 = dst->src[0];
  5441. if (params->ith != 0) {
  5442. return;
  5443. }
  5444. assert(ggml_is_contiguous_1(src0));
  5445. assert(ggml_is_contiguous_1(dst));
  5446. assert(ggml_are_same_shape(src0, dst));
  5447. const int n = ggml_nrows(src0);
  5448. const int nc = src0->ne[0];
  5449. for (int i = 0; i < n; i++) {
  5450. ggml_vec_sigmoid_f32(nc,
  5451. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5452. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5453. }
  5454. }
  5455. static void ggml_compute_forward_sigmoid(
  5456. const struct ggml_compute_params * params,
  5457. struct ggml_tensor * dst) {
  5458. const struct ggml_tensor * src0 = dst->src[0];
  5459. switch (src0->type) {
  5460. case GGML_TYPE_F32:
  5461. {
  5462. ggml_compute_forward_sigmoid_f32(params, dst);
  5463. } break;
  5464. default:
  5465. {
  5466. GGML_ABORT("fatal error");
  5467. }
  5468. }
  5469. }
  5470. // ggml_compute_forward_gelu
  5471. static void ggml_compute_forward_gelu_f32(
  5472. const struct ggml_compute_params * params,
  5473. struct ggml_tensor * dst) {
  5474. const struct ggml_tensor * src0 = dst->src[0];
  5475. assert(ggml_is_contiguous_1(src0));
  5476. assert(ggml_is_contiguous_1(dst));
  5477. assert(ggml_are_same_shape(src0, dst));
  5478. const int ith = params->ith;
  5479. const int nth = params->nth;
  5480. const int nc = src0->ne[0];
  5481. const int nr = ggml_nrows(src0);
  5482. // rows per thread
  5483. const int dr = (nr + nth - 1)/nth;
  5484. // row range for this thread
  5485. const int ir0 = dr*ith;
  5486. const int ir1 = MIN(ir0 + dr, nr);
  5487. for (int i1 = ir0; i1 < ir1; i1++) {
  5488. ggml_vec_gelu_f32(nc,
  5489. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5490. (float *) ((char *) src0->data + i1*(src0->nb[1])));
  5491. #ifndef NDEBUG
  5492. for (int k = 0; k < nc; k++) {
  5493. const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k];
  5494. UNUSED(x);
  5495. assert(!isnan(x));
  5496. assert(!isinf(x));
  5497. }
  5498. #endif
  5499. }
  5500. }
  5501. static void ggml_compute_forward_gelu(
  5502. const struct ggml_compute_params * params,
  5503. struct ggml_tensor * dst) {
  5504. const struct ggml_tensor * src0 = dst->src[0];
  5505. switch (src0->type) {
  5506. case GGML_TYPE_F32:
  5507. {
  5508. ggml_compute_forward_gelu_f32(params, dst);
  5509. } break;
  5510. default:
  5511. {
  5512. GGML_ABORT("fatal error");
  5513. }
  5514. }
  5515. }
  5516. // ggml_compute_forward_gelu_quick
  5517. static void ggml_compute_forward_gelu_quick_f32(
  5518. const struct ggml_compute_params * params,
  5519. struct ggml_tensor * dst) {
  5520. const struct ggml_tensor * src0 = dst->src[0];
  5521. assert(ggml_is_contiguous_1(src0));
  5522. assert(ggml_is_contiguous_1(dst));
  5523. assert(ggml_are_same_shape(src0, dst));
  5524. const int ith = params->ith;
  5525. const int nth = params->nth;
  5526. const int nc = src0->ne[0];
  5527. const int nr = ggml_nrows(src0);
  5528. // rows per thread
  5529. const int dr = (nr + nth - 1)/nth;
  5530. // row range for this thread
  5531. const int ir0 = dr*ith;
  5532. const int ir1 = MIN(ir0 + dr, nr);
  5533. for (int i1 = ir0; i1 < ir1; i1++) {
  5534. ggml_vec_gelu_quick_f32(nc,
  5535. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5536. (float *) ((char *) src0->data + i1*(src0->nb[1])));
  5537. #ifndef NDEBUG
  5538. for (int k = 0; k < nc; k++) {
  5539. const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k];
  5540. UNUSED(x);
  5541. assert(!isnan(x));
  5542. assert(!isinf(x));
  5543. }
  5544. #endif
  5545. }
  5546. }
  5547. static void ggml_compute_forward_gelu_quick(
  5548. const struct ggml_compute_params * params,
  5549. struct ggml_tensor * dst) {
  5550. const struct ggml_tensor * src0 = dst->src[0];
  5551. switch (src0->type) {
  5552. case GGML_TYPE_F32:
  5553. {
  5554. ggml_compute_forward_gelu_quick_f32(params, dst);
  5555. } break;
  5556. default:
  5557. {
  5558. GGML_ABORT("fatal error");
  5559. }
  5560. }
  5561. }
  5562. // ggml_compute_forward_silu
  5563. static void ggml_compute_forward_silu_f32(
  5564. const struct ggml_compute_params * params,
  5565. struct ggml_tensor * dst) {
  5566. const struct ggml_tensor * src0 = dst->src[0];
  5567. assert(ggml_is_contiguous_1(src0));
  5568. assert(ggml_is_contiguous_1(dst));
  5569. assert(ggml_are_same_shape(src0, dst));
  5570. const int ith = params->ith;
  5571. const int nth = params->nth;
  5572. const int nc = src0->ne[0];
  5573. const int nr = ggml_nrows(src0);
  5574. // rows per thread
  5575. const int dr = (nr + nth - 1)/nth;
  5576. // row range for this thread
  5577. const int ir0 = dr*ith;
  5578. const int ir1 = MIN(ir0 + dr, nr);
  5579. for (int i1 = ir0; i1 < ir1; i1++) {
  5580. ggml_vec_silu_f32(nc,
  5581. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5582. (float *) ((char *) src0->data + i1*(src0->nb[1])));
  5583. #ifndef NDEBUG
  5584. for (int k = 0; k < nc; k++) {
  5585. const float x = ((float *) ((char *) dst->data + i1*(dst->nb[1])))[k];
  5586. UNUSED(x);
  5587. assert(!isnan(x));
  5588. assert(!isinf(x));
  5589. }
  5590. #endif
  5591. }
  5592. }
  5593. static void ggml_compute_forward_silu(
  5594. const struct ggml_compute_params * params,
  5595. struct ggml_tensor * dst) {
  5596. const struct ggml_tensor * src0 = dst->src[0];
  5597. switch (src0->type) {
  5598. case GGML_TYPE_F32:
  5599. {
  5600. ggml_compute_forward_silu_f32(params, dst);
  5601. } break;
  5602. default:
  5603. {
  5604. GGML_ABORT("fatal error");
  5605. }
  5606. }
  5607. }
  5608. // ggml_compute_forward_leaky_relu
  5609. static void ggml_compute_forward_leaky_relu_f32(
  5610. const struct ggml_compute_params * params,
  5611. struct ggml_tensor * dst) {
  5612. const struct ggml_tensor * src0 = dst->src[0];
  5613. if (params->ith != 0) {
  5614. return;
  5615. }
  5616. assert(ggml_is_contiguous_1(src0));
  5617. assert(ggml_is_contiguous_1(dst));
  5618. assert(ggml_are_same_shape(src0, dst));
  5619. const int n = ggml_nrows(src0);
  5620. const int nc = src0->ne[0];
  5621. float negative_slope;
  5622. memcpy(&negative_slope, dst->op_params, sizeof(float));
  5623. assert(dst->nb[0] == sizeof(float));
  5624. assert(src0->nb[0] == sizeof(float));
  5625. for (int i = 0; i < n; i++) {
  5626. ggml_vec_leaky_relu_f32(nc,
  5627. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5628. (float *) ((char *) src0->data + i*(src0->nb[1])), negative_slope);
  5629. }
  5630. }
  5631. static void ggml_compute_forward_leaky_relu(
  5632. const struct ggml_compute_params * params,
  5633. struct ggml_tensor * dst) {
  5634. const struct ggml_tensor * src0 = dst->src[0];
  5635. switch (src0->type) {
  5636. case GGML_TYPE_F32:
  5637. {
  5638. ggml_compute_forward_leaky_relu_f32(params, dst);
  5639. } break;
  5640. default:
  5641. {
  5642. GGML_ABORT("fatal error");
  5643. }
  5644. }
  5645. }
  5646. // ggml_compute_forward_silu_back
  5647. static void ggml_compute_forward_silu_back_f32(
  5648. const struct ggml_compute_params * params,
  5649. struct ggml_tensor * dst) {
  5650. const struct ggml_tensor * src0 = dst->src[0];
  5651. const struct ggml_tensor * grad = dst->src[1];
  5652. assert(ggml_is_contiguous_1(grad));
  5653. assert(ggml_is_contiguous_1(src0));
  5654. assert(ggml_is_contiguous_1(dst));
  5655. assert(ggml_are_same_shape(src0, dst));
  5656. assert(ggml_are_same_shape(src0, grad));
  5657. const int ith = params->ith;
  5658. const int nth = params->nth;
  5659. const int nc = src0->ne[0];
  5660. const int nr = ggml_nrows(src0);
  5661. // rows per thread
  5662. const int dr = (nr + nth - 1)/nth;
  5663. // row range for this thread
  5664. const int ir0 = dr*ith;
  5665. const int ir1 = MIN(ir0 + dr, nr);
  5666. for (int i1 = ir0; i1 < ir1; i1++) {
  5667. ggml_vec_silu_backward_f32(nc,
  5668. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5669. (float *) ((char *) src0->data + i1*(src0->nb[1])),
  5670. (float *) ((char *) grad->data + i1*(grad->nb[1])));
  5671. #ifndef NDEBUG
  5672. for (int k = 0; k < nc; k++) {
  5673. const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k];
  5674. UNUSED(x);
  5675. assert(!isnan(x));
  5676. assert(!isinf(x));
  5677. }
  5678. #endif
  5679. }
  5680. }
  5681. static void ggml_compute_forward_silu_back(
  5682. const struct ggml_compute_params * params,
  5683. struct ggml_tensor * dst) {
  5684. const struct ggml_tensor * src0 = dst->src[0];
  5685. switch (src0->type) {
  5686. case GGML_TYPE_F32:
  5687. {
  5688. ggml_compute_forward_silu_back_f32(params, dst);
  5689. } break;
  5690. default:
  5691. {
  5692. GGML_ABORT("fatal error");
  5693. }
  5694. }
  5695. }
  5696. static void ggml_compute_forward_hardswish_f32(
  5697. const struct ggml_compute_params * params,
  5698. struct ggml_tensor * dst) {
  5699. const struct ggml_tensor * src0 = dst->src[0];
  5700. if (params->ith != 0) {
  5701. return;
  5702. }
  5703. assert(ggml_is_contiguous_1(src0));
  5704. assert(ggml_is_contiguous_1(dst));
  5705. assert(ggml_are_same_shape(src0, dst));
  5706. const int n = ggml_nrows(src0);
  5707. const int nc = src0->ne[0];
  5708. for (int i = 0; i < n; i++) {
  5709. ggml_vec_hardswish_f32(nc,
  5710. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5711. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5712. }
  5713. }
  5714. static void ggml_compute_forward_hardswish(
  5715. const struct ggml_compute_params * params,
  5716. struct ggml_tensor * dst) {
  5717. const struct ggml_tensor * src0 = dst->src[0];
  5718. switch (src0->type) {
  5719. case GGML_TYPE_F32:
  5720. {
  5721. ggml_compute_forward_hardswish_f32(params, dst);
  5722. } break;
  5723. default:
  5724. {
  5725. GGML_ABORT("fatal error");
  5726. }
  5727. }
  5728. }
  5729. static void ggml_compute_forward_hardsigmoid_f32(
  5730. const struct ggml_compute_params * params,
  5731. struct ggml_tensor * dst) {
  5732. const struct ggml_tensor * src0 = dst->src[0];
  5733. if (params->ith != 0) {
  5734. return;
  5735. }
  5736. assert(ggml_is_contiguous_1(src0));
  5737. assert(ggml_is_contiguous_1(dst));
  5738. assert(ggml_are_same_shape(src0, dst));
  5739. const int n = ggml_nrows(src0);
  5740. const int nc = src0->ne[0];
  5741. for (int i = 0; i < n; i++) {
  5742. ggml_vec_hardsigmoid_f32(nc,
  5743. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5744. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5745. }
  5746. }
  5747. static void ggml_compute_forward_hardsigmoid(
  5748. const struct ggml_compute_params * params,
  5749. struct ggml_tensor * dst) {
  5750. const struct ggml_tensor * src0 = dst->src[0];
  5751. switch (src0->type) {
  5752. case GGML_TYPE_F32:
  5753. {
  5754. ggml_compute_forward_hardsigmoid_f32(params, dst);
  5755. } break;
  5756. default:
  5757. {
  5758. GGML_ABORT("fatal error");
  5759. }
  5760. }
  5761. }
  5762. static void ggml_compute_forward_exp_f32(
  5763. const struct ggml_compute_params * params,
  5764. struct ggml_tensor * dst) {
  5765. const struct ggml_tensor * src0 = dst->src[0];
  5766. if (params->ith != 0) {
  5767. return;
  5768. }
  5769. assert(ggml_is_contiguous_1(src0));
  5770. assert(ggml_is_contiguous_1(dst));
  5771. assert(ggml_are_same_shape(src0, dst));
  5772. const int n = ggml_nrows(src0);
  5773. const int nc = src0->ne[0];
  5774. for (int i = 0; i < n; i++) {
  5775. ggml_vec_exp_f32(nc,
  5776. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5777. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5778. }
  5779. }
  5780. static void ggml_compute_forward_exp(
  5781. const struct ggml_compute_params * params,
  5782. struct ggml_tensor * dst) {
  5783. const struct ggml_tensor * src0 = dst->src[0];
  5784. switch (src0->type) {
  5785. case GGML_TYPE_F32:
  5786. {
  5787. ggml_compute_forward_exp_f32(params, dst);
  5788. } break;
  5789. default:
  5790. {
  5791. GGML_ABORT("fatal error");
  5792. }
  5793. }
  5794. }
  5795. // ggml_compute_forward_norm
  5796. static void ggml_compute_forward_norm_f32(
  5797. const struct ggml_compute_params * params,
  5798. struct ggml_tensor * dst) {
  5799. const struct ggml_tensor * src0 = dst->src[0];
  5800. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  5801. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5802. const int ith = params->ith;
  5803. const int nth = params->nth;
  5804. GGML_TENSOR_UNARY_OP_LOCALS
  5805. float eps;
  5806. memcpy(&eps, dst->op_params, sizeof(float));
  5807. GGML_ASSERT(eps > 0.0f);
  5808. // TODO: optimize
  5809. for (int64_t i03 = 0; i03 < ne03; i03++) {
  5810. for (int64_t i02 = 0; i02 < ne02; i02++) {
  5811. for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
  5812. const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  5813. ggml_float sum = 0.0;
  5814. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5815. sum += (ggml_float)x[i00];
  5816. }
  5817. float mean = sum/ne00;
  5818. float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
  5819. ggml_float sum2 = 0.0;
  5820. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5821. float v = x[i00] - mean;
  5822. y[i00] = v;
  5823. sum2 += (ggml_float)(v*v);
  5824. }
  5825. float variance = sum2/ne00;
  5826. const float scale = 1.0f/sqrtf(variance + eps);
  5827. ggml_vec_scale_f32(ne00, y, scale);
  5828. }
  5829. }
  5830. }
  5831. }
  5832. static void ggml_compute_forward_norm(
  5833. const struct ggml_compute_params * params,
  5834. struct ggml_tensor * dst) {
  5835. const struct ggml_tensor * src0 = dst->src[0];
  5836. switch (src0->type) {
  5837. case GGML_TYPE_F32:
  5838. {
  5839. ggml_compute_forward_norm_f32(params, dst);
  5840. } break;
  5841. default:
  5842. {
  5843. GGML_ABORT("fatal error");
  5844. }
  5845. }
  5846. }
  5847. // ggml_compute_forward_group_rms_norm
  5848. static void ggml_compute_forward_rms_norm_f32(
  5849. const struct ggml_compute_params * params,
  5850. struct ggml_tensor * dst) {
  5851. const struct ggml_tensor * src0 = dst->src[0];
  5852. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  5853. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5854. const int ith = params->ith;
  5855. const int nth = params->nth;
  5856. GGML_TENSOR_UNARY_OP_LOCALS
  5857. float eps;
  5858. memcpy(&eps, dst->op_params, sizeof(float));
  5859. GGML_ASSERT(eps > 0.0f);
  5860. // TODO: optimize
  5861. for (int64_t i03 = 0; i03 < ne03; i03++) {
  5862. for (int64_t i02 = 0; i02 < ne02; i02++) {
  5863. for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
  5864. const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  5865. ggml_float sum = 0.0;
  5866. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5867. sum += (ggml_float)(x[i00] * x[i00]);
  5868. }
  5869. const float mean = sum/ne00;
  5870. float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
  5871. memcpy(y, x, ne00 * sizeof(float));
  5872. // for (int i00 = 0; i00 < ne00; i00++) {
  5873. // y[i00] = x[i00];
  5874. // }
  5875. const float scale = 1.0f/sqrtf(mean + eps);
  5876. ggml_vec_scale_f32(ne00, y, scale);
  5877. }
  5878. }
  5879. }
  5880. }
  5881. static void ggml_compute_forward_rms_norm(
  5882. const struct ggml_compute_params * params,
  5883. struct ggml_tensor * dst) {
  5884. const struct ggml_tensor * src0 = dst->src[0];
  5885. switch (src0->type) {
  5886. case GGML_TYPE_F32:
  5887. {
  5888. ggml_compute_forward_rms_norm_f32(params, dst);
  5889. } break;
  5890. default:
  5891. {
  5892. GGML_ABORT("fatal error");
  5893. }
  5894. }
  5895. }
  5896. static void ggml_compute_forward_rms_norm_back_f32(
  5897. const struct ggml_compute_params * params,
  5898. struct ggml_tensor * dst) {
  5899. const struct ggml_tensor * src0 = dst->src[0];
  5900. const struct ggml_tensor * src1 = dst->src[1];
  5901. GGML_ASSERT(ggml_are_same_shape(src0, dst) && ggml_are_same_shape(src0, src1));
  5902. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5903. const int ith = params->ith;
  5904. const int nth = params->nth;
  5905. GGML_TENSOR_BINARY_OP_LOCALS
  5906. float eps;
  5907. memcpy(&eps, dst->op_params, sizeof(float));
  5908. // TODO: optimize
  5909. for (int64_t i03 = 0; i03 < ne03; i03++) {
  5910. for (int64_t i02 = 0; i02 < ne02; i02++) {
  5911. for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
  5912. // src1 is same shape as src0 => same indices
  5913. const int64_t i11 = i01;
  5914. const int64_t i12 = i02;
  5915. const int64_t i13 = i03;
  5916. const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  5917. const float * dz = (float *) ((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13);
  5918. ggml_float sum_xx = 0.0;
  5919. ggml_float sum_xdz = 0.0;
  5920. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5921. sum_xx += (ggml_float)(x[i00] * x[i00]);
  5922. sum_xdz += (ggml_float)(x[i00] * dz[i00]);
  5923. }
  5924. //const float mean = (float)(sum_xx)/ne00;
  5925. const float mean_eps = (float)(sum_xx)/ne00 + eps;
  5926. const float sum_eps = (float)(sum_xx) + eps*ne00;
  5927. //const float mean_xdz = (float)(sum_xdz)/ne00;
  5928. // we could cache rms from forward pass to improve performance.
  5929. // to do this implement ggml_rms and compose ggml_rms_norm using ggml_rms.
  5930. //const float rms = sqrtf(mean_eps);
  5931. const float rrms = 1.0f / sqrtf(mean_eps);
  5932. //const float scale = -rrms/(ne00 * mean_eps); // -1/(n*rms**3)
  5933. {
  5934. // z = rms_norm(x)
  5935. //
  5936. // rms_norm(src0) =
  5937. // scale(
  5938. // src0,
  5939. // div(
  5940. // 1,
  5941. // sqrt(
  5942. // add(
  5943. // scale(
  5944. // sum(
  5945. // sqr(
  5946. // src0)),
  5947. // (1.0/N)),
  5948. // eps))));
  5949. // postorder:
  5950. // ## op args grad
  5951. // 00 param src0 grad[#00]
  5952. // 01 const 1
  5953. // 02 sqr (#00) grad[#02]
  5954. // 03 sum (#02) grad[#03]
  5955. // 04 const 1/N
  5956. // 05 scale (#03, #04) grad[#05]
  5957. // 06 const eps
  5958. // 07 add (#05, #06) grad[#07]
  5959. // 08 sqrt (#07) grad[#08]
  5960. // 09 div (#01,#08) grad[#09]
  5961. // 10 scale (#00,#09) grad[#10]
  5962. //
  5963. // backward pass, given grad[#10]
  5964. // #10: scale
  5965. // grad[#00] += scale(grad[#10],#09)
  5966. // grad[#09] += sum(mul(grad[#10],#00))
  5967. // #09: div
  5968. // grad[#08] += neg(mul(grad[#09], div(#09,#08)))
  5969. // #08: sqrt
  5970. // grad[#07] += mul(grad[#08], div(0.5, #08))
  5971. // #07: add
  5972. // grad[#05] += grad[#07]
  5973. // #05: scale
  5974. // grad[#03] += scale(grad[#05],#04)
  5975. // #03: sum
  5976. // grad[#02] += repeat(grad[#03], #02)
  5977. // #02:
  5978. // grad[#00] += scale(mul(#00, grad[#02]), 2.0)
  5979. //
  5980. // substitute and simplify:
  5981. // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0)
  5982. // grad[#02] = repeat(grad[#03], #02)
  5983. // grad[#02] = repeat(scale(grad[#05],#04), #02)
  5984. // grad[#02] = repeat(scale(grad[#07],#04), #02)
  5985. // grad[#02] = repeat(scale(mul(grad[#08], div(0.5, #08)),#04), #02)
  5986. // grad[#02] = repeat(scale(mul(neg(mul(grad[#09], div(#09,#08))), div(0.5, #08)),#04), #02)
  5987. // grad[#02] = repeat(scale(mul(neg(mul(sum(mul(grad[#10],#00)), div(#09,#08))), div(0.5, #08)),#04), #02)
  5988. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(#09,#08) * div(0.5, #08) * (1/N)), #02)
  5989. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(div(#01,#08),#08) * div(0.5, #08) * (1/N)), #02)
  5990. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#08*#08) * div(0.5, #08) * (1/N)), #02)
  5991. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N)), #02)
  5992. // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0)
  5993. // 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)
  5994. // 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)
  5995. // grad[#00] = scale(grad(#10), #09) + scale(#00, -(sum(mul(grad[#10],#00)) * div(1,#07) * div(1,#08) * (1/N)))
  5996. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N))
  5997. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N))
  5998. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,mean_eps*rms) * (-1/N))
  5999. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*mean_eps))
  6000. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*(sum_xx/N+eps)))
  6001. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*sum_xx+rms*N*eps))
  6002. // grad[#00] = scale(dz, rrms) + scale(x, sum(mul(dz,x)) * div(-1,rms*N*mean_eps))
  6003. // grad[#00] = scale(dz, rrms) + scale(x, sum_xdz * div(-1,rms*N*mean_eps))
  6004. // a = b*c + d*e
  6005. // a = b*c*f/f + d*e*f/f
  6006. // a = (b*c*f + d*e*f)*(1/f)
  6007. // a = (b*c*(1/c) + d*e*(1/c))*(1/(1/c))
  6008. // a = (b + d*e/c)*c
  6009. // b = dz, c = rrms, d = x, e = sum_xdz * div(-1,rms*N*mean_eps)
  6010. // a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)/rrms)*rrms
  6011. // a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)*rms)*rrms
  6012. // a = (dz + x*sum_xdz * div(-rms,rms*N*mean_eps))*rrms
  6013. // a = (dz + x*sum_xdz * div(-1,N*mean_eps))*rrms
  6014. // a = (dz + x*div(-sum_xdz,N*mean_eps))*rrms
  6015. // a = (dz + x*div(-mean_xdz,mean_eps))*rrms
  6016. // grad[#00] = scale(dz + scale(x, div(-mean_xdz,mean_eps)),rrms)
  6017. // grad[#00] = scale(dz + scale(x, -mean_xdz/mean_eps),rrms)
  6018. // dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms)
  6019. }
  6020. // dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms)
  6021. // post-order:
  6022. // dx := x
  6023. // dx := scale(dx,-mean_xdz/mean_eps)
  6024. // dx := add(dx, dz)
  6025. // dx := scale(dx, rrms)
  6026. float * dx = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
  6027. ggml_vec_cpy_f32 (ne00, dx, x);
  6028. // ggml_vec_scale_f32(ne00, dx, -mean_xdz/mean_eps);
  6029. ggml_vec_scale_f32(ne00, dx, (float)(-sum_xdz)/sum_eps);
  6030. ggml_vec_acc_f32 (ne00, dx, dz);
  6031. ggml_vec_scale_f32(ne00, dx, rrms);
  6032. }
  6033. }
  6034. }
  6035. }
  6036. static void ggml_compute_forward_rms_norm_back(
  6037. const struct ggml_compute_params * params,
  6038. struct ggml_tensor * dst) {
  6039. const struct ggml_tensor * src0 = dst->src[0];
  6040. switch (src0->type) {
  6041. case GGML_TYPE_F32:
  6042. {
  6043. ggml_compute_forward_rms_norm_back_f32(params, dst);
  6044. } break;
  6045. default:
  6046. {
  6047. GGML_ABORT("fatal error");
  6048. }
  6049. }
  6050. }
  6051. // ggml_compute_forward_group_norm
  6052. static void ggml_compute_forward_group_norm_f32(
  6053. const struct ggml_compute_params * params,
  6054. struct ggml_tensor * dst) {
  6055. const struct ggml_tensor * src0 = dst->src[0];
  6056. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6057. GGML_ASSERT(src0->nb[0] == sizeof(float));
  6058. const int ith = params->ith;
  6059. const int nth = params->nth;
  6060. GGML_TENSOR_UNARY_OP_LOCALS
  6061. // TODO: optimize
  6062. float eps;
  6063. memcpy(&eps, dst->op_params + 1, sizeof(float));
  6064. int n_channels = src0->ne[2];
  6065. int n_groups = dst->op_params[0];
  6066. int n_channels_per_group = (n_channels + n_groups - 1) / n_groups;
  6067. for (int i = ith; i < n_groups; i += nth) {
  6068. int start = i * n_channels_per_group;
  6069. int end = start + n_channels_per_group;
  6070. if (end > n_channels) {
  6071. end = n_channels;
  6072. }
  6073. int step = end - start;
  6074. for (int64_t i03 = 0; i03 < ne03; i03++) {
  6075. ggml_float sum = 0.0;
  6076. for (int64_t i02 = start; i02 < end; i02++) {
  6077. for (int64_t i01 = 0; i01 < ne01; i01++) {
  6078. const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03);
  6079. ggml_float sumr = 0.0;
  6080. for (int64_t i00 = 0; i00 < ne00; i00++) {
  6081. sumr += (ggml_float)x[i00];
  6082. }
  6083. sum += sumr;
  6084. }
  6085. }
  6086. const float mean = sum / (ne00 * ne01 * step);
  6087. ggml_float sum2 = 0.0;
  6088. for (int64_t i02 = start; i02 < end; i02++) {
  6089. for (int64_t i01 = 0; i01 < ne01; i01++) {
  6090. const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03);
  6091. float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3);
  6092. ggml_float sumr = 0.0;
  6093. for (int64_t i00 = 0; i00 < ne00; i00++) {
  6094. float v = x[i00] - mean;
  6095. y[i00] = v;
  6096. sumr += (ggml_float)(v * v);
  6097. }
  6098. sum2 += sumr;
  6099. }
  6100. }
  6101. const float variance = sum2 / (ne00 * ne01 * step);
  6102. const float scale = 1.0f / sqrtf(variance + eps);
  6103. for (int64_t i02 = start; i02 < end; i02++) {
  6104. for (int64_t i01 = 0; i01 < ne01; i01++) {
  6105. float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3);
  6106. ggml_vec_scale_f32(ne00, y, scale);
  6107. }
  6108. }
  6109. }
  6110. }
  6111. }
  6112. static void ggml_compute_forward_group_norm(
  6113. const struct ggml_compute_params * params,
  6114. struct ggml_tensor * dst) {
  6115. const struct ggml_tensor * src0 = dst->src[0];
  6116. switch (src0->type) {
  6117. case GGML_TYPE_F32:
  6118. {
  6119. ggml_compute_forward_group_norm_f32(params, dst);
  6120. } break;
  6121. default:
  6122. {
  6123. GGML_ABORT("fatal error");
  6124. }
  6125. }
  6126. }
  6127. // ggml_compute_forward_mul_mat
  6128. static void ggml_compute_forward_mul_mat_one_chunk(
  6129. const struct ggml_compute_params * params,
  6130. struct ggml_tensor * dst,
  6131. const enum ggml_type type,
  6132. const int64_t num_rows_per_vec_dot,
  6133. const int64_t ir0_start,
  6134. const int64_t ir0_end,
  6135. const int64_t ir1_start,
  6136. const int64_t ir1_end) {
  6137. const struct ggml_tensor * src0 = dst->src[0];
  6138. const struct ggml_tensor * src1 = dst->src[1];
  6139. GGML_TENSOR_BINARY_OP_LOCALS
  6140. const bool src1_cont = ggml_is_contiguous(src1);
  6141. ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot;
  6142. enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type;
  6143. // broadcast factors
  6144. const int64_t r2 = ne12 / ne02;
  6145. const int64_t r3 = ne13 / ne03;
  6146. //printf("ir0_start = %6lld, ir0_end = %6lld, ir1_start = %6lld, ir1_end = %6lld\n", ir0_start, ir0_end, ir1_start, ir1_end);
  6147. // threads with no work simply yield (not sure if it helps)
  6148. if (ir0_start >= ir0_end || ir1_start >= ir1_end) {
  6149. return;
  6150. }
  6151. const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
  6152. const size_t row_size = ggml_row_size(vec_dot_type, ne10);
  6153. assert(ne12 % ne02 == 0);
  6154. assert(ne13 % ne03 == 0);
  6155. // block-tiling attempt
  6156. const int64_t blck_0 = 16;
  6157. const int64_t blck_1 = 16;
  6158. const size_t src1_col_stride = src1_cont || src1->type != vec_dot_type ? row_size : nb11;
  6159. // attempt to reduce false-sharing (does not seem to make a difference)
  6160. // 16 * 2, accounting for mmla kernels
  6161. float tmp[32];
  6162. for (int64_t iir1 = ir1_start; iir1 < ir1_end; iir1 += blck_1) {
  6163. for (int64_t iir0 = ir0_start; iir0 < ir0_end; iir0 += blck_0) {
  6164. for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir1_end; ir1 += num_rows_per_vec_dot) {
  6165. const int64_t i13 = (ir1 / (ne12 * ne1));
  6166. const int64_t i12 = (ir1 - i13 * ne12 * ne1) / ne1;
  6167. const int64_t i11 = (ir1 - i13 * ne12 * ne1 - i12 * ne1);
  6168. // broadcast src0 into src1
  6169. const int64_t i03 = i13 / r3;
  6170. const int64_t i02 = i12 / r2;
  6171. const int64_t i1 = i11;
  6172. const int64_t i2 = i12;
  6173. const int64_t i3 = i13;
  6174. const char * src0_row = (const char*)src0->data + (0 + i02 * nb02 + i03 * nb03);
  6175. // desc: when src1 is not a contiguous memory block we have to calculate the offset using the strides
  6176. // if it is, then we have either copied the data to params->wdata and made it contiguous or we are using
  6177. // the original src1 data pointer, so we should index using the indices directly
  6178. // TODO: this is a bit of a hack, we should probably have a better way to handle this
  6179. const char * src1_col = (const char*)wdata +
  6180. (src1_cont || src1->type != vec_dot_type
  6181. ? (i11 + i12 * ne11 + i13 * ne12 * ne11) * row_size
  6182. : (i11 * nb11 + i12 * nb12 + i13 * nb13));
  6183. float * dst_col = (float*)((char*)dst->data + (i1 * nb1 + i2 * nb2 + i3 * nb3));
  6184. //for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ++ir0) {
  6185. // vec_dot(ne00, &dst_col[ir0], src0_row + ir0*nb01, src1_col);
  6186. //}
  6187. for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ir0 += num_rows_per_vec_dot) {
  6188. 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);
  6189. }
  6190. for (int cn = 0; cn < num_rows_per_vec_dot; ++cn) {
  6191. memcpy(&dst_col[iir0 + cn * nb1 / nb0], tmp + (cn * 16), (MIN(iir0 + blck_0, ir0_end) - iir0) * sizeof(float));
  6192. }
  6193. }
  6194. }
  6195. }
  6196. }
  6197. static void ggml_compute_forward_mul_mat(
  6198. const struct ggml_compute_params * params,
  6199. struct ggml_tensor * dst) {
  6200. const struct ggml_tensor * src0 = dst->src[0];
  6201. const struct ggml_tensor * src1 = dst->src[1];
  6202. GGML_TENSOR_BINARY_OP_LOCALS
  6203. const int ith = params->ith;
  6204. const int nth = params->nth;
  6205. enum ggml_type const vec_dot_type = type_traits_cpu[src0->type].vec_dot_type;
  6206. ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float;
  6207. int64_t const vec_dot_num_rows = type_traits_cpu[src0->type].nrows;
  6208. GGML_ASSERT(ne0 == ne01);
  6209. GGML_ASSERT(ne1 == ne11);
  6210. GGML_ASSERT(ne2 == ne12);
  6211. GGML_ASSERT(ne3 == ne13);
  6212. // we don't support permuted src0 or src1
  6213. GGML_ASSERT(nb00 == ggml_type_size(src0->type));
  6214. GGML_ASSERT(nb10 == ggml_type_size(src1->type));
  6215. // dst cannot be transposed or permuted
  6216. GGML_ASSERT(nb0 == sizeof(float));
  6217. GGML_ASSERT(nb0 <= nb1);
  6218. GGML_ASSERT(nb1 <= nb2);
  6219. GGML_ASSERT(nb2 <= nb3);
  6220. // nb01 >= nb00 - src0 is not transposed
  6221. // compute by src0 rows
  6222. // TODO: extract to "extra_op"
  6223. #if GGML_USE_LLAMAFILE
  6224. // broadcast factors
  6225. const int64_t r2 = ne12 / ne02;
  6226. const int64_t r3 = ne13 / ne03;
  6227. const bool src1_cont = ggml_is_contiguous(src1);
  6228. if (src1_cont) {
  6229. for (int64_t i13 = 0; i13 < ne13; i13++)
  6230. for (int64_t i12 = 0; i12 < ne12; i12++)
  6231. if (!llamafile_sgemm(ne01, ne11, ne00/ggml_blck_size(src0->type),
  6232. (const char *)src0->data + i12/r2*nb02 + i13/r3*nb03,
  6233. nb01/ggml_type_size(src0->type),
  6234. (const char *)src1->data + i12*nb12 + i13*nb13,
  6235. nb11/ggml_type_size(src1->type),
  6236. (char *)dst->data + i12*nb2 + i13*nb3,
  6237. nb1/ggml_type_size(dst->type),
  6238. ith, nth,
  6239. src0->type,
  6240. src1->type,
  6241. dst->type))
  6242. goto UseGgmlGemm1;
  6243. return;
  6244. }
  6245. UseGgmlGemm1:;
  6246. #endif
  6247. if (src1->type != vec_dot_type) {
  6248. char * wdata = params->wdata;
  6249. const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
  6250. const size_t nbw2 = nbw1*ne11;
  6251. const size_t nbw3 = nbw2*ne12;
  6252. assert(params->wsize >= ne13*nbw3);
  6253. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6254. for (int64_t i13 = 0; i13 < ne13; ++i13) {
  6255. for (int64_t i12 = 0; i12 < ne12; ++i12) {
  6256. for (int64_t i11 = ith; i11 < ne11; i11 += nth) {
  6257. from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11),
  6258. (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1),
  6259. ne10);
  6260. }
  6261. }
  6262. }
  6263. }
  6264. if (ith == 0) {
  6265. // Every thread starts at ith, so the first unprocessed chunk is nth. This save a bit of coordination right at the start.
  6266. atomic_store_explicit(&params->threadpool->current_chunk, nth, memory_order_relaxed);
  6267. }
  6268. ggml_barrier(params->threadpool);
  6269. #if GGML_USE_LLAMAFILE
  6270. if (src1->type != vec_dot_type) {
  6271. const void* wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
  6272. const size_t row_size = ggml_row_size(vec_dot_type, ne10);
  6273. for (int64_t i13 = 0; i13 < ne13; i13++)
  6274. for (int64_t i12 = 0; i12 < ne12; i12++)
  6275. if (!llamafile_sgemm(ne01, ne11, ne00/ggml_blck_size(src0->type),
  6276. (const char *)src0->data + i12/r2*nb02 + i13/r3*nb03,
  6277. nb01/ggml_type_size(src0->type),
  6278. (const char *)wdata + (i12*ne11 + i13*ne12*ne11)*row_size,
  6279. row_size/ggml_type_size(vec_dot_type),
  6280. (char *)dst->data + i12*nb2 + i13*nb3,
  6281. nb1/ggml_type_size(dst->type),
  6282. ith, nth,
  6283. src0->type,
  6284. vec_dot_type,
  6285. dst->type))
  6286. goto UseGgmlGemm2;
  6287. return;
  6288. }
  6289. UseGgmlGemm2:;
  6290. #endif
  6291. // 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)
  6292. const int64_t nr0 = ne0;
  6293. // This is the size of the rest of the dimensions of the result
  6294. const int64_t nr1 = ne1 * ne2 * ne3;
  6295. // Now select a reasonable chunk size.
  6296. int chunk_size = 16;
  6297. // We need to step up the size if it's small
  6298. if (nr0 == 1 || nr1 == 1) {
  6299. chunk_size = 64;
  6300. }
  6301. // distribute the work across the inner or outer loop based on which one is larger
  6302. // The number of chunks in the 0/1 dim.
  6303. // CEIL(nr0/chunk_size)
  6304. int64_t nchunk0 = (nr0 + chunk_size - 1) / chunk_size;
  6305. int64_t nchunk1 = (nr1 + chunk_size - 1) / chunk_size;
  6306. // If the chunking is poor for the number of threads on this setup, scrap the whole plan. Re-chunk it by thread.
  6307. // Also, chunking by thread was measured to have perform better on NUMA systems. See https://github.com/ggerganov/llama.cpp/pull/6915
  6308. // In theory, chunking should be just as useful on NUMA and non NUMA systems, but testing disagreed with that.
  6309. if (nchunk0 * nchunk1 < nth * 4 || ggml_is_numa()) {
  6310. // distribute the thread work across the inner or outer loop based on which one is larger
  6311. nchunk0 = nr0 > nr1 ? nth : 1; // parallelize by src0 rows
  6312. nchunk1 = nr0 > nr1 ? 1 : nth; // parallelize by src1 rows
  6313. }
  6314. // The number of elements in each chunk
  6315. const int64_t dr0 = (nr0 + nchunk0 - 1) / nchunk0;
  6316. const int64_t dr1 = (nr1 + nchunk1 - 1) / nchunk1;
  6317. // The first chunk comes from our thread_id, the rest will get auto-assigned.
  6318. int current_chunk = ith;
  6319. while (current_chunk < nchunk0 * nchunk1) {
  6320. const int64_t ith0 = current_chunk % nchunk0;
  6321. const int64_t ith1 = current_chunk / nchunk0;
  6322. const int64_t ir0_start = dr0 * ith0;
  6323. const int64_t ir0_end = MIN(ir0_start + dr0, nr0);
  6324. const int64_t ir1_start = dr1 * ith1;
  6325. const int64_t ir1_end = MIN(ir1_start + dr1, nr1);
  6326. // dot kernels can handle 1 row and col at a time, but mmla kernels can process 2 rows and cols
  6327. int64_t num_rows_per_vec_dot = vec_dot_num_rows;
  6328. // these checks are needed to avoid crossing dim1 boundaries
  6329. // can be optimized, but the logic would become more complicated, so keeping it like this for simplicity
  6330. if ((nr0 % 2 != 0) || (ne11 % 2 != 0) || ((ir0_end - ir0_start) % 2 != 0) || ((ir1_end - ir1_start) % 2 != 0)) {
  6331. num_rows_per_vec_dot = 1;
  6332. }
  6333. ggml_compute_forward_mul_mat_one_chunk(params, dst, src0->type, num_rows_per_vec_dot, ir0_start, ir0_end, ir1_start, ir1_end);
  6334. if (nth >= nchunk0 * nchunk1) {
  6335. break;
  6336. }
  6337. current_chunk = atomic_fetch_add_explicit(&params->threadpool->current_chunk, 1, memory_order_relaxed);
  6338. }
  6339. }
  6340. // ggml_compute_forward_mul_mat_id
  6341. static void ggml_compute_forward_mul_mat_id(
  6342. const struct ggml_compute_params * params,
  6343. struct ggml_tensor * dst) {
  6344. const struct ggml_tensor * src0 = dst->src[0];
  6345. const struct ggml_tensor * src1 = dst->src[1];
  6346. const struct ggml_tensor * ids = dst->src[2];
  6347. GGML_TENSOR_BINARY_OP_LOCALS
  6348. const int ith = params->ith;
  6349. const int nth = params->nth;
  6350. const enum ggml_type type = src0->type;
  6351. const bool src1_cont = ggml_is_contiguous(src1);
  6352. ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot;
  6353. enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type;
  6354. ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float;
  6355. // we don't support permuted src0 or src1
  6356. GGML_ASSERT(nb00 == ggml_type_size(type));
  6357. GGML_ASSERT(nb10 == ggml_type_size(src1->type));
  6358. // dst cannot be transposed or permuted
  6359. GGML_ASSERT(nb0 == sizeof(float));
  6360. GGML_ASSERT(nb0 <= nb1);
  6361. GGML_ASSERT(nb1 <= nb2);
  6362. GGML_ASSERT(nb2 <= nb3);
  6363. // row groups
  6364. const int n_ids = ids->ne[0]; // n_expert_used
  6365. const int n_as = ne02; // n_expert
  6366. char * wdata_src1_end = (src1->type == vec_dot_type) ?
  6367. (char *) params->wdata :
  6368. (char *) params->wdata + GGML_PAD(ggml_row_size(vec_dot_type, ggml_nelements(src1)), sizeof(int64_t));
  6369. struct mmid_row_mapping {
  6370. int32_t i1;
  6371. int32_t i2;
  6372. };
  6373. int64_t * matrix_row_counts = (int64_t *) (wdata_src1_end); // [n_as]
  6374. struct mmid_row_mapping * matrix_rows = (struct mmid_row_mapping *)(matrix_row_counts + n_as); // [n_as][ne11]
  6375. if (src1->type != vec_dot_type) {
  6376. char * wdata = params->wdata;
  6377. const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
  6378. const size_t nbw2 = nbw1*ne11;
  6379. const size_t nbw3 = nbw2*ne12;
  6380. assert(params->wsize >= ne13*nbw3);
  6381. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6382. for (int64_t i13 = 0; i13 < ne13; ++i13) {
  6383. for (int64_t i12 = 0; i12 < ne12; ++i12) {
  6384. for (int64_t i11 = ith; i11 < ne11; i11 += nth) {
  6385. from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11),
  6386. (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1),
  6387. ne10);
  6388. }
  6389. }
  6390. }
  6391. }
  6392. #define MMID_MATRIX_ROW(row_id, i1) matrix_rows[(row_id)*ne12 + (i1)]
  6393. if (ith == 0) {
  6394. // initialize matrix_row_counts
  6395. memset(matrix_row_counts, 0, n_as*sizeof(int64_t));
  6396. // group rows by src0 matrix
  6397. for (int64_t iid1 = 0; iid1 < ids->ne[1]; ++iid1) {
  6398. for (int id = 0; id < n_ids; ++id) {
  6399. const int32_t i02 = *(const int32_t *) ((const char *) ids->data + iid1*ids->nb[1] + id*ids->nb[0]);
  6400. assert(i02 >= 0 && i02 < n_as);
  6401. MMID_MATRIX_ROW(i02, matrix_row_counts[i02]) = (struct mmid_row_mapping) {id, iid1};
  6402. matrix_row_counts[i02] += 1;
  6403. }
  6404. }
  6405. }
  6406. ggml_barrier(params->threadpool);
  6407. // compute each matrix multiplication in sequence
  6408. for (int cur_a = 0; cur_a < n_as; ++cur_a) {
  6409. const int64_t cne1 = matrix_row_counts[cur_a];
  6410. if (cne1 == 0) {
  6411. continue;
  6412. }
  6413. const char * src0_cur = (const char *) src0->data + cur_a*nb02;
  6414. const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
  6415. const size_t row_size = ggml_row_size(vec_dot_type, ne10);
  6416. const int64_t nr0 = ne01; // src0 rows
  6417. const int64_t nr1 = cne1; // src1 rows
  6418. // distribute the thread work across the inner or outer loop based on which one is larger
  6419. const int64_t nth0 = nr0 > nr1 ? nth : 1; // parallelize by src0 rows
  6420. const int64_t nth1 = nr0 > nr1 ? 1 : nth; // parallelize by src1 rows
  6421. const int64_t ith0 = ith % nth0;
  6422. const int64_t ith1 = ith / nth0;
  6423. const int64_t dr0 = (nr0 + nth0 - 1)/nth0;
  6424. const int64_t dr1 = (nr1 + nth1 - 1)/nth1;
  6425. const int64_t ir010 = dr0*ith0;
  6426. const int64_t ir011 = MIN(ir010 + dr0, nr0);
  6427. const int64_t ir110 = dr1*ith1;
  6428. const int64_t ir111 = MIN(ir110 + dr1, nr1);
  6429. // threads with no work simply yield (not sure if it helps)
  6430. //if (ir010 >= ir011 || ir110 >= ir111) {
  6431. // sched_yield();
  6432. // continue;
  6433. //}
  6434. // block-tiling attempt
  6435. const int64_t blck_0 = 16;
  6436. const int64_t blck_1 = 16;
  6437. // attempt to reduce false-sharing (does not seem to make a difference)
  6438. float tmp[16];
  6439. for (int64_t iir1 = ir110; iir1 < ir111; iir1 += blck_1) {
  6440. for (int64_t iir0 = ir010; iir0 < ir011; iir0 += blck_0) {
  6441. for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir111; ++ir1) {
  6442. const int64_t _i12 = ir1; // logical row index for this expert
  6443. struct mmid_row_mapping row_mapping = MMID_MATRIX_ROW(cur_a, _i12);
  6444. const int id = row_mapping.i1; // selected expert index
  6445. const int64_t i11 = id % ne11;
  6446. const int64_t i12 = row_mapping.i2; // row index in src1
  6447. const int64_t i1 = id; // selected expert index
  6448. const int64_t i2 = i12; // row
  6449. // desc: when src1 is not a contiguous memory block we have to calculate the offset using the strides
  6450. // if it is, then we have either copied the data to params->wdata and made it contiguous or we are using
  6451. // the original src1 data pointer, so we should index using the indices directly
  6452. // TODO: this is a bit of a hack, we should probably have a better way to handle this
  6453. const char * src1_col = (const char *) wdata +
  6454. (src1_cont || src1->type != vec_dot_type
  6455. ? (i11 + i12*ne11)*row_size
  6456. : (i11*nb11 + i12*nb12));
  6457. float * dst_col = (float *) ((char *) dst->data + (i1*nb1 + i2*nb2));
  6458. //for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir011; ++ir0) {
  6459. // vec_dot(ne00, &dst_col[ir0], src0_row + ir0*nb01, src1_col);
  6460. //}
  6461. for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir011; ++ir0) {
  6462. vec_dot(ne00, &tmp[ir0 - iir0], 0, src0_cur + ir0*nb01, 0, src1_col, 0, 1);
  6463. }
  6464. memcpy(&dst_col[iir0], tmp, (MIN(iir0 + blck_0, ir011) - iir0)*sizeof(float));
  6465. }
  6466. }
  6467. }
  6468. }
  6469. #undef MMID_MATRIX_ROW
  6470. }
  6471. // ggml_compute_forward_out_prod
  6472. static void ggml_compute_forward_out_prod_f32(
  6473. const struct ggml_compute_params * params,
  6474. struct ggml_tensor * dst) {
  6475. const struct ggml_tensor * src0 = dst->src[0];
  6476. const struct ggml_tensor * src1 = dst->src[1];
  6477. GGML_TENSOR_BINARY_OP_LOCALS
  6478. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6479. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6480. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6481. const int ith = params->ith;
  6482. const int nth = params->nth;
  6483. GGML_ASSERT(ne0 == ne00);
  6484. GGML_ASSERT(ne1 == ne10);
  6485. GGML_ASSERT(ne2 == ne02);
  6486. GGML_ASSERT(ne02 == ne12);
  6487. GGML_ASSERT(ne3 == ne13);
  6488. GGML_ASSERT(ne03 == ne13);
  6489. // we don't support permuted src0 or src1
  6490. GGML_ASSERT(nb00 == sizeof(float));
  6491. // dst cannot be transposed or permuted
  6492. GGML_ASSERT(nb0 == sizeof(float));
  6493. // GGML_ASSERT(nb0 <= nb1);
  6494. // GGML_ASSERT(nb1 <= nb2);
  6495. // GGML_ASSERT(nb2 <= nb3);
  6496. // nb01 >= nb00 - src0 is not transposed
  6497. // compute by src0 rows
  6498. if (ith == 0) {
  6499. ggml_vec_set_f32(ne0*ne1*ne2*ne3, dst->data, 0);
  6500. }
  6501. ggml_barrier(params->threadpool);
  6502. // dst[:,:,:,:] = 0
  6503. // for i2,i3:
  6504. // for i1:
  6505. // for i01:
  6506. // for i0:
  6507. // dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3]
  6508. // parallelize by last three dimensions
  6509. // total rows in dst
  6510. const int64_t nr = ne1*ne2*ne3;
  6511. // rows per thread
  6512. const int64_t dr = (nr + nth - 1)/nth;
  6513. // row range for this thread
  6514. const int64_t ir0 = dr*ith;
  6515. const int64_t ir1 = MIN(ir0 + dr, nr);
  6516. // block-tiling attempt
  6517. const int64_t blck_0 = MAX(GGML_VEC_MAD_UNROLL, 32);
  6518. const int64_t blck_1 = 16;
  6519. for (int64_t bir = ir0; bir < ir1; bir += blck_1) {
  6520. const int64_t bir1 = MIN(bir + blck_1, ir1);
  6521. for (int64_t bi01 = 0; bi01 < ne01; bi01 += blck_0) {
  6522. const int64_t bne01 = MIN(bi01 + blck_0, ne01);
  6523. for (int64_t ir = bir; ir < bir1; ++ir) {
  6524. // dst indices
  6525. const int64_t i3 = ir/(ne2*ne1);
  6526. const int64_t i2 = (ir - i3*ne2*ne1)/ne1;
  6527. const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1);
  6528. const int64_t i02 = i2;
  6529. const int64_t i03 = i3;
  6530. //const int64_t i10 = i1;
  6531. const int64_t i12 = i2;
  6532. const int64_t i13 = i3;
  6533. #if GGML_VEC_MAD_UNROLL > 2
  6534. const int64_t bne01_unroll = bne01 - (bne01 % GGML_VEC_MAD_UNROLL);
  6535. for (int64_t i01 = bi01; i01 < bne01_unroll; i01 += GGML_VEC_MAD_UNROLL) {
  6536. const int64_t i11 = i01;
  6537. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6538. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6539. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6540. ggml_vec_mad_f32_unroll(ne0, nb01, nb11, d, s0, s1);
  6541. }
  6542. for (int64_t i01 = bne01_unroll; i01 < bne01; ++i01) {
  6543. const int64_t i11 = i01;
  6544. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6545. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6546. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6547. ggml_vec_mad_f32(ne0, d, s0, *s1);
  6548. }
  6549. #else
  6550. for (int64_t i01 = bi01; i01 < bne01; ++i01) {
  6551. const int64_t i11 = i01;
  6552. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6553. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6554. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6555. ggml_vec_mad_f32(ne0, d, s0, *s1);
  6556. }
  6557. #endif
  6558. }
  6559. }
  6560. }
  6561. }
  6562. static void ggml_compute_forward_out_prod_q_f32(
  6563. const struct ggml_compute_params * params,
  6564. struct ggml_tensor * dst) {
  6565. const struct ggml_tensor * src0 = dst->src[0];
  6566. const struct ggml_tensor * src1 = dst->src[1];
  6567. GGML_TENSOR_BINARY_OP_LOCALS;
  6568. const int ith = params->ith;
  6569. const int nth = params->nth;
  6570. const enum ggml_type type = src0->type;
  6571. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  6572. GGML_ASSERT(ne02 == ne12);
  6573. GGML_ASSERT(ne03 == ne13);
  6574. GGML_ASSERT(ne2 == ne12);
  6575. GGML_ASSERT(ne3 == ne13);
  6576. // we don't support permuted src0 dim0
  6577. GGML_ASSERT(nb00 == ggml_type_size(type));
  6578. // dst dim0 cannot be transposed or permuted
  6579. GGML_ASSERT(nb0 == sizeof(float));
  6580. // GGML_ASSERT(nb0 <= nb1);
  6581. // GGML_ASSERT(nb1 <= nb2);
  6582. // GGML_ASSERT(nb2 <= nb3);
  6583. GGML_ASSERT(ne0 == ne00);
  6584. GGML_ASSERT(ne1 == ne10);
  6585. GGML_ASSERT(ne2 == ne02);
  6586. GGML_ASSERT(ne3 == ne03);
  6587. // nb01 >= nb00 - src0 is not transposed
  6588. // compute by src0 rows
  6589. if (ith == 0) {
  6590. ggml_vec_set_f32(ne0*ne1*ne2*ne3, dst->data, 0);
  6591. }
  6592. ggml_barrier(params->threadpool);
  6593. // parallelize by last three dimensions
  6594. // total rows in dst
  6595. const int64_t nr = ne1*ne2*ne3;
  6596. // rows per thread
  6597. const int64_t dr = (nr + nth - 1)/nth;
  6598. // row range for this thread
  6599. const int64_t ir0 = dr*ith;
  6600. const int64_t ir1 = MIN(ir0 + dr, nr);
  6601. // dst[:,:,:,:] = 0
  6602. // for i2,i3:
  6603. // for i1:
  6604. // for i01:
  6605. // for i0:
  6606. // dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3]
  6607. float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith;
  6608. for (int64_t ir = ir0; ir < ir1; ++ir) {
  6609. // dst indices
  6610. const int64_t i3 = ir/(ne2*ne1);
  6611. const int64_t i2 = (ir - i3*ne2*ne1)/ne1;
  6612. const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1);
  6613. const int64_t i02 = i2;
  6614. const int64_t i03 = i3;
  6615. //const int64_t i10 = i1;
  6616. const int64_t i12 = i2;
  6617. const int64_t i13 = i3;
  6618. for (int64_t i01 = 0; i01 < ne01; ++i01) {
  6619. const int64_t i11 = i01;
  6620. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6621. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6622. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6623. dequantize_row_q(s0, wdata, ne0);
  6624. ggml_vec_mad_f32(ne0, d, wdata, *s1);
  6625. }
  6626. }
  6627. }
  6628. static void ggml_compute_forward_out_prod(
  6629. const struct ggml_compute_params * params,
  6630. struct ggml_tensor * dst) {
  6631. const struct ggml_tensor * src0 = dst->src[0];
  6632. switch (src0->type) {
  6633. case GGML_TYPE_Q4_0:
  6634. case GGML_TYPE_Q4_1:
  6635. case GGML_TYPE_Q5_0:
  6636. case GGML_TYPE_Q5_1:
  6637. case GGML_TYPE_Q8_0:
  6638. case GGML_TYPE_Q2_K:
  6639. case GGML_TYPE_Q3_K:
  6640. case GGML_TYPE_Q4_K:
  6641. case GGML_TYPE_Q5_K:
  6642. case GGML_TYPE_Q6_K:
  6643. case GGML_TYPE_TQ1_0:
  6644. case GGML_TYPE_TQ2_0:
  6645. case GGML_TYPE_IQ2_XXS:
  6646. case GGML_TYPE_IQ2_XS:
  6647. case GGML_TYPE_IQ3_XXS:
  6648. case GGML_TYPE_IQ1_S:
  6649. case GGML_TYPE_IQ1_M:
  6650. case GGML_TYPE_IQ4_NL:
  6651. case GGML_TYPE_IQ4_XS:
  6652. case GGML_TYPE_IQ3_S:
  6653. case GGML_TYPE_IQ2_S:
  6654. {
  6655. ggml_compute_forward_out_prod_q_f32(params, dst);
  6656. } break;
  6657. case GGML_TYPE_F16:
  6658. {
  6659. GGML_ABORT("fatal error"); // todo
  6660. // ggml_compute_forward_out_prod_f16_f32(params, dst);
  6661. }
  6662. case GGML_TYPE_F32:
  6663. {
  6664. ggml_compute_forward_out_prod_f32(params, dst);
  6665. } break;
  6666. default:
  6667. {
  6668. GGML_ABORT("fatal error");
  6669. }
  6670. }
  6671. }
  6672. // ggml_compute_forward_scale
  6673. static void ggml_compute_forward_scale_f32(
  6674. const struct ggml_compute_params * params,
  6675. struct ggml_tensor * dst) {
  6676. const struct ggml_tensor * src0 = dst->src[0];
  6677. GGML_ASSERT(ggml_is_contiguous(src0));
  6678. GGML_ASSERT(ggml_is_contiguous(dst));
  6679. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6680. // scale factor
  6681. float v;
  6682. memcpy(&v, dst->op_params, sizeof(float));
  6683. const int ith = params->ith;
  6684. const int nth = params->nth;
  6685. const int nc = src0->ne[0];
  6686. const int nr = ggml_nrows(src0);
  6687. // rows per thread
  6688. const int dr = (nr + nth - 1)/nth;
  6689. // row range for this thread
  6690. const int ir0 = dr*ith;
  6691. const int ir1 = MIN(ir0 + dr, nr);
  6692. const size_t nb01 = src0->nb[1];
  6693. const size_t nb1 = dst->nb[1];
  6694. for (int i1 = ir0; i1 < ir1; i1++) {
  6695. if (dst->data != src0->data) {
  6696. // src0 is same shape as dst => same indices
  6697. memcpy((char *)dst->data + i1*nb1, (char *)src0->data + i1*nb01, nc * sizeof(float));
  6698. }
  6699. ggml_vec_scale_f32(nc, (float *) ((char *) dst->data + i1*nb1), v);
  6700. }
  6701. }
  6702. static void ggml_compute_forward_scale(
  6703. const struct ggml_compute_params * params,
  6704. struct ggml_tensor * dst) {
  6705. const struct ggml_tensor * src0 = dst->src[0];
  6706. switch (src0->type) {
  6707. case GGML_TYPE_F32:
  6708. {
  6709. ggml_compute_forward_scale_f32(params, dst);
  6710. } break;
  6711. default:
  6712. {
  6713. GGML_ABORT("fatal error");
  6714. }
  6715. }
  6716. }
  6717. // ggml_compute_forward_set
  6718. static void ggml_compute_forward_set_f32(
  6719. const struct ggml_compute_params * params,
  6720. struct ggml_tensor * dst) {
  6721. const struct ggml_tensor * src0 = dst->src[0];
  6722. const struct ggml_tensor * src1 = dst->src[1];
  6723. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6724. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  6725. // view src0 and dst with these strides and data offset inbytes during set
  6726. // nb0 is implicitly element_size because src0 and dst are contiguous
  6727. size_t nb1 = ((int32_t *) dst->op_params)[0];
  6728. size_t nb2 = ((int32_t *) dst->op_params)[1];
  6729. size_t nb3 = ((int32_t *) dst->op_params)[2];
  6730. size_t offset = ((int32_t *) dst->op_params)[3];
  6731. bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  6732. if (!inplace) {
  6733. if (params->ith == 0) {
  6734. // memcpy needs to be synchronized across threads to avoid race conditions.
  6735. // => do it in INIT phase
  6736. memcpy(
  6737. ((char *) dst->data),
  6738. ((char *) src0->data),
  6739. ggml_nbytes(dst));
  6740. }
  6741. ggml_barrier(params->threadpool);
  6742. }
  6743. const int ith = params->ith;
  6744. const int nth = params->nth;
  6745. const int nr = ggml_nrows(src1);
  6746. const int nc = src1->ne[0];
  6747. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne)
  6748. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  6749. // src0 and dst as viewed during set
  6750. const size_t nb0 = ggml_element_size(src0);
  6751. const int im0 = (ne10 == 0 ? 0 : ne10-1);
  6752. const int im1 = (ne11 == 0 ? 0 : ne11-1);
  6753. const int im2 = (ne12 == 0 ? 0 : ne12-1);
  6754. const int im3 = (ne13 == 0 ? 0 : ne13-1);
  6755. GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst));
  6756. GGML_ASSERT(nb10 == sizeof(float));
  6757. // rows per thread
  6758. const int dr = (nr + nth - 1)/nth;
  6759. // row range for this thread
  6760. const int ir0 = dr*ith;
  6761. const int ir1 = MIN(ir0 + dr, nr);
  6762. for (int ir = ir0; ir < ir1; ++ir) {
  6763. // src0 and dst are viewed with shape of src1 and offset
  6764. // => same indices
  6765. const int i3 = ir/(ne12*ne11);
  6766. const int i2 = (ir - i3*ne12*ne11)/ne11;
  6767. const int i1 = (ir - i3*ne12*ne11 - i2*ne11);
  6768. ggml_vec_cpy_f32(nc,
  6769. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset),
  6770. (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
  6771. }
  6772. }
  6773. static void ggml_compute_forward_set_i32(
  6774. const struct ggml_compute_params * params,
  6775. struct ggml_tensor * dst) {
  6776. const struct ggml_tensor * src0 = dst->src[0];
  6777. const struct ggml_tensor * src1 = dst->src[1];
  6778. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6779. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  6780. // view src0 and dst with these strides and data offset inbytes during set
  6781. // nb0 is implicitly element_size because src0 and dst are contiguous
  6782. size_t nb1 = ((int32_t *) dst->op_params)[0];
  6783. size_t nb2 = ((int32_t *) dst->op_params)[1];
  6784. size_t nb3 = ((int32_t *) dst->op_params)[2];
  6785. size_t offset = ((int32_t *) dst->op_params)[3];
  6786. bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  6787. if (!inplace) {
  6788. if (params->ith == 0) {
  6789. // memcpy needs to be synchronized across threads to avoid race conditions.
  6790. // => do it in INIT phase
  6791. memcpy(
  6792. ((char *) dst->data),
  6793. ((char *) src0->data),
  6794. ggml_nbytes(dst));
  6795. }
  6796. ggml_barrier(params->threadpool);
  6797. }
  6798. const int ith = params->ith;
  6799. const int nth = params->nth;
  6800. const int nr = ggml_nrows(src1);
  6801. const int nc = src1->ne[0];
  6802. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne)
  6803. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  6804. // src0 and dst as viewed during set
  6805. const size_t nb0 = ggml_element_size(src0);
  6806. const int im0 = (ne10 == 0 ? 0 : ne10-1);
  6807. const int im1 = (ne11 == 0 ? 0 : ne11-1);
  6808. const int im2 = (ne12 == 0 ? 0 : ne12-1);
  6809. const int im3 = (ne13 == 0 ? 0 : ne13-1);
  6810. GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst));
  6811. GGML_ASSERT(nb10 == sizeof(int32_t));
  6812. // rows per thread
  6813. const int dr = (nr + nth - 1)/nth;
  6814. // row range for this thread
  6815. const int ir0 = dr*ith;
  6816. const int ir1 = MIN(ir0 + dr, nr);
  6817. for (int ir = ir0; ir < ir1; ++ir) {
  6818. // src0 and dst are viewed with shape of src1 and offset
  6819. // => same indices
  6820. const int i3 = ir/(ne12*ne11);
  6821. const int i2 = (ir - i3*ne12*ne11)/ne11;
  6822. const int i1 = (ir - i3*ne12*ne11 - i2*ne11);
  6823. ggml_vec_cpy_i32(nc,
  6824. (int32_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset),
  6825. (int32_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
  6826. }
  6827. }
  6828. static void ggml_compute_forward_set(
  6829. const struct ggml_compute_params * params,
  6830. struct ggml_tensor * dst) {
  6831. const struct ggml_tensor * src0 = dst->src[0];
  6832. switch (src0->type) {
  6833. case GGML_TYPE_F32:
  6834. {
  6835. ggml_compute_forward_set_f32(params, dst);
  6836. } break;
  6837. case GGML_TYPE_I32:
  6838. {
  6839. ggml_compute_forward_set_i32(params, dst);
  6840. } break;
  6841. case GGML_TYPE_F16:
  6842. case GGML_TYPE_BF16:
  6843. case GGML_TYPE_Q4_0:
  6844. case GGML_TYPE_Q4_1:
  6845. case GGML_TYPE_Q5_0:
  6846. case GGML_TYPE_Q5_1:
  6847. case GGML_TYPE_Q8_0:
  6848. case GGML_TYPE_Q8_1:
  6849. case GGML_TYPE_Q2_K:
  6850. case GGML_TYPE_Q3_K:
  6851. case GGML_TYPE_Q4_K:
  6852. case GGML_TYPE_Q5_K:
  6853. case GGML_TYPE_Q6_K:
  6854. case GGML_TYPE_TQ1_0:
  6855. case GGML_TYPE_TQ2_0:
  6856. case GGML_TYPE_IQ2_XXS:
  6857. case GGML_TYPE_IQ2_XS:
  6858. case GGML_TYPE_IQ3_XXS:
  6859. case GGML_TYPE_IQ1_S:
  6860. case GGML_TYPE_IQ1_M:
  6861. case GGML_TYPE_IQ4_NL:
  6862. case GGML_TYPE_IQ4_XS:
  6863. case GGML_TYPE_IQ3_S:
  6864. case GGML_TYPE_IQ2_S:
  6865. default:
  6866. {
  6867. GGML_ABORT("fatal error");
  6868. }
  6869. }
  6870. }
  6871. // ggml_compute_forward_cpy
  6872. static void ggml_compute_forward_cpy(
  6873. const struct ggml_compute_params * params,
  6874. struct ggml_tensor * dst) {
  6875. ggml_compute_forward_dup(params, dst);
  6876. }
  6877. // ggml_compute_forward_cont
  6878. static void ggml_compute_forward_cont(
  6879. const struct ggml_compute_params * params,
  6880. struct ggml_tensor * dst) {
  6881. ggml_compute_forward_dup(params, dst);
  6882. }
  6883. // ggml_compute_forward_reshape
  6884. static void ggml_compute_forward_reshape(
  6885. const struct ggml_compute_params * params,
  6886. struct ggml_tensor * dst) {
  6887. // NOP
  6888. UNUSED(params);
  6889. UNUSED(dst);
  6890. }
  6891. // ggml_compute_forward_view
  6892. static void ggml_compute_forward_view(
  6893. const struct ggml_compute_params * params,
  6894. const struct ggml_tensor * dst) {
  6895. // NOP
  6896. UNUSED(params);
  6897. UNUSED(dst);
  6898. }
  6899. // ggml_compute_forward_permute
  6900. static void ggml_compute_forward_permute(
  6901. const struct ggml_compute_params * params,
  6902. const struct ggml_tensor * dst) {
  6903. // NOP
  6904. UNUSED(params);
  6905. UNUSED(dst);
  6906. }
  6907. // ggml_compute_forward_transpose
  6908. static void ggml_compute_forward_transpose(
  6909. const struct ggml_compute_params * params,
  6910. const struct ggml_tensor * dst) {
  6911. // NOP
  6912. UNUSED(params);
  6913. UNUSED(dst);
  6914. }
  6915. // ggml_compute_forward_get_rows
  6916. static void ggml_compute_forward_get_rows_q(
  6917. const struct ggml_compute_params * params,
  6918. struct ggml_tensor * dst) {
  6919. const struct ggml_tensor * src0 = dst->src[0];
  6920. const struct ggml_tensor * src1 = dst->src[1];
  6921. GGML_TENSOR_BINARY_OP_LOCALS
  6922. const int64_t nc = ne00;
  6923. const int64_t nr = ggml_nelements(src1);
  6924. const enum ggml_type type = src0->type;
  6925. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  6926. assert(ne0 == nc);
  6927. assert(ne02 == ne11);
  6928. assert(nb00 == ggml_type_size(type));
  6929. assert(ggml_nrows(dst) == nr);
  6930. const int ith = params->ith;
  6931. const int nth = params->nth;
  6932. // rows per thread
  6933. const int dr = (nr + nth - 1)/nth;
  6934. // row range for this thread
  6935. const int ir0 = dr*ith;
  6936. const int ir1 = MIN(ir0 + dr, nr);
  6937. for (int64_t i = ir0; i < ir1; ++i) {
  6938. const int64_t i12 = i/(ne11*ne10);
  6939. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  6940. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  6941. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  6942. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  6943. dequantize_row_q(
  6944. (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03),
  6945. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc);
  6946. }
  6947. }
  6948. static void ggml_compute_forward_get_rows_f16(
  6949. const struct ggml_compute_params * params,
  6950. struct ggml_tensor * dst) {
  6951. const struct ggml_tensor * src0 = dst->src[0];
  6952. const struct ggml_tensor * src1 = dst->src[1];
  6953. GGML_TENSOR_BINARY_OP_LOCALS
  6954. const int64_t nc = ne00;
  6955. const int64_t nr = ggml_nelements(src1);
  6956. assert(ne0 == nc);
  6957. assert(ne02 == ne11);
  6958. assert(nb00 == sizeof(ggml_fp16_t));
  6959. assert(ggml_nrows(dst) == nr);
  6960. const int ith = params->ith;
  6961. const int nth = params->nth;
  6962. // rows per thread
  6963. const int dr = (nr + nth - 1)/nth;
  6964. // row range for this thread
  6965. const int ir0 = dr*ith;
  6966. const int ir1 = MIN(ir0 + dr, nr);
  6967. for (int64_t i = ir0; i < ir1; ++i) {
  6968. const int64_t i12 = i/(ne11*ne10);
  6969. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  6970. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  6971. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  6972. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  6973. ggml_fp16_to_fp32_row(
  6974. (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03),
  6975. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc);
  6976. }
  6977. }
  6978. static void ggml_compute_forward_get_rows_bf16(
  6979. const struct ggml_compute_params * params,
  6980. struct ggml_tensor * dst) {
  6981. const struct ggml_tensor * src0 = dst->src[0];
  6982. const struct ggml_tensor * src1 = dst->src[1];
  6983. GGML_TENSOR_BINARY_OP_LOCALS
  6984. const int64_t nc = ne00;
  6985. const int64_t nr = ggml_nelements(src1);
  6986. assert(ne0 == nc);
  6987. assert(ne02 == ne11);
  6988. assert(nb00 == sizeof(ggml_bf16_t));
  6989. assert(ggml_nrows(dst) == nr);
  6990. const int ith = params->ith;
  6991. const int nth = params->nth;
  6992. // rows per thread
  6993. const int dr = (nr + nth - 1)/nth;
  6994. // row range for this thread
  6995. const int ir0 = dr*ith;
  6996. const int ir1 = MIN(ir0 + dr, nr);
  6997. for (int64_t i = ir0; i < ir1; ++i) {
  6998. const int64_t i12 = i/(ne11*ne10);
  6999. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  7000. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  7001. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  7002. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  7003. ggml_bf16_to_fp32_row(
  7004. (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03),
  7005. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc);
  7006. }
  7007. }
  7008. static void ggml_compute_forward_get_rows_f32(
  7009. const struct ggml_compute_params * params,
  7010. struct ggml_tensor * dst) {
  7011. const struct ggml_tensor * src0 = dst->src[0];
  7012. const struct ggml_tensor * src1 = dst->src[1];
  7013. GGML_TENSOR_BINARY_OP_LOCALS
  7014. const int64_t nc = ne00;
  7015. const int64_t nr = ggml_nelements(src1);
  7016. assert(ne0 == nc);
  7017. assert(ne02 == ne11);
  7018. assert(nb00 == sizeof(float));
  7019. assert(ggml_nrows(dst) == nr);
  7020. const int ith = params->ith;
  7021. const int nth = params->nth;
  7022. // rows per thread
  7023. const int dr = (nr + nth - 1)/nth;
  7024. // row range for this thread
  7025. const int ir0 = dr*ith;
  7026. const int ir1 = MIN(ir0 + dr, nr);
  7027. for (int64_t i = ir0; i < ir1; ++i) {
  7028. const int64_t i12 = i/(ne11*ne10);
  7029. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  7030. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  7031. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  7032. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  7033. ggml_vec_cpy_f32(nc,
  7034. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3),
  7035. (float *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03));
  7036. }
  7037. }
  7038. static void ggml_compute_forward_get_rows(
  7039. const struct ggml_compute_params * params,
  7040. struct ggml_tensor * dst) {
  7041. const struct ggml_tensor * src0 = dst->src[0];
  7042. switch (src0->type) {
  7043. case GGML_TYPE_Q4_0:
  7044. case GGML_TYPE_Q4_1:
  7045. case GGML_TYPE_Q5_0:
  7046. case GGML_TYPE_Q5_1:
  7047. case GGML_TYPE_Q8_0:
  7048. case GGML_TYPE_Q8_1:
  7049. case GGML_TYPE_Q2_K:
  7050. case GGML_TYPE_Q3_K:
  7051. case GGML_TYPE_Q4_K:
  7052. case GGML_TYPE_Q5_K:
  7053. case GGML_TYPE_Q6_K:
  7054. case GGML_TYPE_TQ1_0:
  7055. case GGML_TYPE_TQ2_0:
  7056. case GGML_TYPE_IQ2_XXS:
  7057. case GGML_TYPE_IQ2_XS:
  7058. case GGML_TYPE_IQ3_XXS:
  7059. case GGML_TYPE_IQ1_S:
  7060. case GGML_TYPE_IQ1_M:
  7061. case GGML_TYPE_IQ4_NL:
  7062. case GGML_TYPE_IQ4_XS:
  7063. case GGML_TYPE_IQ3_S:
  7064. case GGML_TYPE_IQ2_S:
  7065. {
  7066. ggml_compute_forward_get_rows_q(params, dst);
  7067. } break;
  7068. case GGML_TYPE_F16:
  7069. {
  7070. ggml_compute_forward_get_rows_f16(params, dst);
  7071. } break;
  7072. case GGML_TYPE_BF16:
  7073. {
  7074. ggml_compute_forward_get_rows_bf16(params, dst);
  7075. } break;
  7076. case GGML_TYPE_F32:
  7077. case GGML_TYPE_I32:
  7078. {
  7079. ggml_compute_forward_get_rows_f32(params, dst);
  7080. } break;
  7081. default:
  7082. {
  7083. GGML_ABORT("fatal error");
  7084. }
  7085. }
  7086. //static bool first = true;
  7087. //printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]);
  7088. //if (first) {
  7089. // first = false;
  7090. //} else {
  7091. // for (int k = 0; k < dst->ne[1]; ++k) {
  7092. // for (int j = 0; j < dst->ne[0]/16; ++j) {
  7093. // for (int i = 0; i < 16; ++i) {
  7094. // printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]);
  7095. // }
  7096. // printf("\n");
  7097. // }
  7098. // printf("\n");
  7099. // }
  7100. // printf("\n");
  7101. // exit(0);
  7102. //}
  7103. }
  7104. // ggml_compute_forward_get_rows_back
  7105. static void ggml_compute_forward_get_rows_back_f32_f16(
  7106. const struct ggml_compute_params * params,
  7107. struct ggml_tensor * dst) {
  7108. const struct ggml_tensor * src0 = dst->src[0];
  7109. const struct ggml_tensor * src1 = dst->src[1];
  7110. if (params->ith != 0) {
  7111. return;
  7112. }
  7113. GGML_ASSERT(ggml_is_contiguous(dst));
  7114. // ggml_compute_forward_dup_same_cont(params, opt0, dst);
  7115. memset(dst->data, 0, ggml_nbytes(dst));
  7116. const int nc = src0->ne[0];
  7117. const int nr = ggml_nelements(src1);
  7118. GGML_ASSERT( dst->ne[0] == nc);
  7119. GGML_ASSERT(src0->nb[0] == sizeof(ggml_fp16_t));
  7120. for (int i = 0; i < nr; ++i) {
  7121. const int r = ((int32_t *) src1->data)[i];
  7122. for (int j = 0; j < nc; ++j) {
  7123. ggml_fp16_t v = ((ggml_fp16_t *) ((char *) src0->data + i*src0->nb[1]))[j];
  7124. ((float *) ((char *) dst->data + r*dst->nb[1]))[j] += GGML_FP16_TO_FP32(v);
  7125. }
  7126. }
  7127. }
  7128. static void ggml_compute_forward_get_rows_back_f32(
  7129. const struct ggml_compute_params * params,
  7130. struct ggml_tensor * dst) {
  7131. const struct ggml_tensor * src0 = dst->src[0];
  7132. const struct ggml_tensor * src1 = dst->src[1];
  7133. if (params->ith != 0) {
  7134. return;
  7135. }
  7136. GGML_ASSERT(ggml_is_contiguous(dst));
  7137. // ggml_compute_forward_dup_same_cont(params, opt0, dst);
  7138. memset(dst->data, 0, ggml_nbytes(dst));
  7139. const int nc = src0->ne[0];
  7140. const int nr = ggml_nelements(src1);
  7141. GGML_ASSERT( dst->ne[0] == nc);
  7142. GGML_ASSERT(src0->nb[0] == sizeof(float));
  7143. for (int i = 0; i < nr; ++i) {
  7144. const int r = ((int32_t *) src1->data)[i];
  7145. ggml_vec_add_f32(nc,
  7146. (float *) ((char *) dst->data + r*dst->nb[1]),
  7147. (float *) ((char *) dst->data + r*dst->nb[1]),
  7148. (float *) ((char *) src0->data + i*src0->nb[1]));
  7149. }
  7150. }
  7151. static void ggml_compute_forward_get_rows_back(
  7152. const struct ggml_compute_params * params,
  7153. struct ggml_tensor * dst) {
  7154. const struct ggml_tensor * src0 = dst->src[0];
  7155. switch (src0->type) {
  7156. case GGML_TYPE_F16:
  7157. {
  7158. ggml_compute_forward_get_rows_back_f32_f16(params, dst);
  7159. } break;
  7160. case GGML_TYPE_F32:
  7161. {
  7162. ggml_compute_forward_get_rows_back_f32(params, dst);
  7163. } break;
  7164. default:
  7165. {
  7166. GGML_ABORT("fatal error");
  7167. }
  7168. }
  7169. //static bool first = true;
  7170. //printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]);
  7171. //if (first) {
  7172. // first = false;
  7173. //} else {
  7174. // for (int k = 0; k < dst->ne[1]; ++k) {
  7175. // for (int j = 0; j < dst->ne[0]/16; ++j) {
  7176. // for (int i = 0; i < 16; ++i) {
  7177. // printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]);
  7178. // }
  7179. // printf("\n");
  7180. // }
  7181. // printf("\n");
  7182. // }
  7183. // printf("\n");
  7184. // exit(0);
  7185. //}
  7186. }
  7187. // ggml_compute_forward_diag
  7188. static void ggml_compute_forward_diag_f32(
  7189. const struct ggml_compute_params * params,
  7190. struct ggml_tensor * dst) {
  7191. const struct ggml_tensor * src0 = dst->src[0];
  7192. if (params->ith != 0) {
  7193. return;
  7194. }
  7195. // TODO: handle transposed/permuted matrices
  7196. GGML_TENSOR_UNARY_OP_LOCALS
  7197. GGML_ASSERT(ne00 == ne0);
  7198. GGML_ASSERT(ne00 == ne1);
  7199. GGML_ASSERT(ne01 == 1);
  7200. GGML_ASSERT(ne02 == ne2);
  7201. GGML_ASSERT(ne03 == ne3);
  7202. GGML_ASSERT(nb00 == sizeof(float));
  7203. GGML_ASSERT(nb0 == sizeof(float));
  7204. for (int i3 = 0; i3 < ne3; i3++) {
  7205. for (int i2 = 0; i2 < ne2; i2++) {
  7206. for (int i1 = 0; i1 < ne1; i1++) {
  7207. float * d = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  7208. float * s = (float *)((char *) src0->data + i3*nb03 + i2*nb02);
  7209. for (int i0 = 0; i0 < i1; i0++) {
  7210. d[i0] = 0;
  7211. }
  7212. d[i1] = s[i1];
  7213. for (int i0 = i1+1; i0 < ne0; i0++) {
  7214. d[i0] = 0;
  7215. }
  7216. }
  7217. }
  7218. }
  7219. }
  7220. static void ggml_compute_forward_diag(
  7221. const struct ggml_compute_params * params,
  7222. struct ggml_tensor * dst) {
  7223. const struct ggml_tensor * src0 = dst->src[0];
  7224. switch (src0->type) {
  7225. case GGML_TYPE_F32:
  7226. {
  7227. ggml_compute_forward_diag_f32(params, dst);
  7228. } break;
  7229. default:
  7230. {
  7231. GGML_ABORT("fatal error");
  7232. }
  7233. }
  7234. }
  7235. // ggml_compute_forward_diag_mask_inf
  7236. static void ggml_compute_forward_diag_mask_f32(
  7237. const struct ggml_compute_params * params,
  7238. struct ggml_tensor * dst,
  7239. const float value) {
  7240. const struct ggml_tensor * src0 = dst->src[0];
  7241. const int ith = params->ith;
  7242. const int nth = params->nth;
  7243. const int n_past = ((int32_t *) dst->op_params)[0];
  7244. const bool inplace = src0->data == dst->data;
  7245. GGML_ASSERT(n_past >= 0);
  7246. if (!inplace) {
  7247. if (ith == 0) {
  7248. // memcpy needs to be synchronized across threads to avoid race conditions.
  7249. // => do it in INIT phase
  7250. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  7251. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  7252. memcpy(
  7253. ((char *) dst->data),
  7254. ((char *) src0->data),
  7255. ggml_nbytes(dst));
  7256. }
  7257. ggml_barrier(params->threadpool);
  7258. }
  7259. // TODO: handle transposed/permuted matrices
  7260. const int n = ggml_nrows(src0);
  7261. const int nc = src0->ne[0];
  7262. const int nr = src0->ne[1];
  7263. const int nz = n/nr;
  7264. GGML_ASSERT( dst->nb[0] == sizeof(float));
  7265. GGML_ASSERT(src0->nb[0] == sizeof(float));
  7266. for (int k = 0; k < nz; k++) {
  7267. for (int j = ith; j < nr; j += nth) {
  7268. for (int i = n_past; i < nc; i++) {
  7269. if (i > n_past + j) {
  7270. *(float *)((char *) dst->data + k*dst->nb[2] + j*dst->nb[1] + i*dst->nb[0]) = value;
  7271. }
  7272. }
  7273. }
  7274. }
  7275. }
  7276. static void ggml_compute_forward_diag_mask_inf(
  7277. const struct ggml_compute_params * params,
  7278. struct ggml_tensor * dst) {
  7279. const struct ggml_tensor * src0 = dst->src[0];
  7280. switch (src0->type) {
  7281. case GGML_TYPE_F32:
  7282. {
  7283. ggml_compute_forward_diag_mask_f32(params, dst, -INFINITY);
  7284. } break;
  7285. default:
  7286. {
  7287. GGML_ABORT("fatal error");
  7288. }
  7289. }
  7290. }
  7291. static void ggml_compute_forward_diag_mask_zero(
  7292. const struct ggml_compute_params * params,
  7293. struct ggml_tensor * dst) {
  7294. const struct ggml_tensor * src0 = dst->src[0];
  7295. switch (src0->type) {
  7296. case GGML_TYPE_F32:
  7297. {
  7298. ggml_compute_forward_diag_mask_f32(params, dst, 0);
  7299. } break;
  7300. default:
  7301. {
  7302. GGML_ABORT("fatal error");
  7303. }
  7304. }
  7305. }
  7306. // ggml_compute_forward_soft_max
  7307. static void ggml_compute_forward_soft_max_f32(
  7308. const struct ggml_compute_params * params,
  7309. struct ggml_tensor * dst) {
  7310. const struct ggml_tensor * src0 = dst->src[0];
  7311. const struct ggml_tensor * src1 = dst->src[1];
  7312. assert(ggml_is_contiguous(dst));
  7313. assert(ggml_are_same_shape(src0, dst));
  7314. float scale = 1.0f;
  7315. float max_bias = 0.0f;
  7316. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  7317. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  7318. // TODO: handle transposed/permuted matrices
  7319. const int ith = params->ith;
  7320. const int nth = params->nth;
  7321. GGML_TENSOR_UNARY_OP_LOCALS
  7322. //const int64_t ne11 = src1 ? src1->ne[1] : 1;
  7323. // TODO: is this supposed to be ceil instead of floor?
  7324. // https://huggingface.co/mosaicml/mpt-7b/blob/main/attention.py#L370
  7325. const uint32_t n_head = ne02;
  7326. const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head));
  7327. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  7328. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  7329. const int nc = src0->ne[0];
  7330. const int nr = ggml_nrows(src0);
  7331. // rows per thread
  7332. const int dr = (nr + nth - 1)/nth;
  7333. // row range for this thread
  7334. const int ir0 = dr*ith;
  7335. const int ir1 = MIN(ir0 + dr, nr);
  7336. float * wp = (float *) params->wdata + (nc + CACHE_LINE_SIZE_F32) * ith;
  7337. const bool use_f16 = (src1 && src1->type == GGML_TYPE_F16);
  7338. for (int i1 = ir0; i1 < ir1; i1++) {
  7339. // ALiBi
  7340. const uint32_t h = (i1/ne01)%ne02; // head
  7341. 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;
  7342. float * sp = (float *)((char *) src0->data + i1*src0->nb[1]);
  7343. float * dp = (float *)((char *) dst->data + i1*dst->nb[1]);
  7344. // broadcast the mask across rows
  7345. ggml_fp16_t * mp_f16 = src1 ? (ggml_fp16_t *)((char *) src1->data) + (i1%ne01)*ne00 : NULL;
  7346. float * mp_f32 = src1 ? (float *)((char *) src1->data) + (i1%ne01)*ne00 : NULL;
  7347. ggml_vec_cpy_f32 (nc, wp, sp);
  7348. ggml_vec_scale_f32(nc, wp, scale);
  7349. if (mp_f32) {
  7350. if (use_f16) {
  7351. for (int i = 0; i < nc; ++i) {
  7352. wp[i] += slope*GGML_FP16_TO_FP32(mp_f16[i]);
  7353. }
  7354. } else {
  7355. for (int i = 0; i < nc; ++i) {
  7356. wp[i] += slope*mp_f32[i];
  7357. }
  7358. }
  7359. }
  7360. #ifndef NDEBUG
  7361. for (int i = 0; i < nc; ++i) {
  7362. //printf("p[%d] = %f\n", i, p[i]);
  7363. assert(!isnan(wp[i]));
  7364. }
  7365. #endif
  7366. float max = -INFINITY;
  7367. ggml_vec_max_f32(nc, &max, wp);
  7368. ggml_float sum = ggml_vec_soft_max_f32(nc, dp, wp, max);
  7369. assert(sum > 0.0);
  7370. sum = 1.0/sum;
  7371. ggml_vec_scale_f32(nc, dp, sum);
  7372. #ifndef NDEBUG
  7373. for (int i = 0; i < nc; ++i) {
  7374. assert(!isnan(dp[i]));
  7375. assert(!isinf(dp[i]));
  7376. }
  7377. #endif
  7378. }
  7379. }
  7380. static void ggml_compute_forward_soft_max(
  7381. const struct ggml_compute_params * params,
  7382. struct ggml_tensor * dst) {
  7383. const struct ggml_tensor * src0 = dst->src[0];
  7384. switch (src0->type) {
  7385. case GGML_TYPE_F32:
  7386. {
  7387. ggml_compute_forward_soft_max_f32(params, dst);
  7388. } break;
  7389. default:
  7390. {
  7391. GGML_ABORT("fatal error");
  7392. }
  7393. }
  7394. }
  7395. // ggml_compute_forward_soft_max_back
  7396. static void ggml_compute_forward_soft_max_back_f32(
  7397. const struct ggml_compute_params * params,
  7398. struct ggml_tensor * dst) {
  7399. const struct ggml_tensor * src0 = dst->src[0];
  7400. const struct ggml_tensor * src1 = dst->src[1];
  7401. GGML_ASSERT(ggml_is_contiguous(src0));
  7402. GGML_ASSERT(ggml_is_contiguous(src1));
  7403. GGML_ASSERT(ggml_is_contiguous(dst));
  7404. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  7405. GGML_ASSERT(ggml_are_same_shape(src1, dst));
  7406. // TODO: handle transposed/permuted matrices
  7407. const int ith = params->ith;
  7408. const int nth = params->nth;
  7409. const int nc = src0->ne[0];
  7410. const int nr = ggml_nrows(src0);
  7411. // rows per thread
  7412. const int dr = (nr + nth - 1)/nth;
  7413. // row range for this thread
  7414. const int ir0 = dr*ith;
  7415. const int ir1 = MIN(ir0 + dr, nr);
  7416. for (int i1 = ir0; i1 < ir1; i1++) {
  7417. float *dy = (float *)((char *) src0->data + i1*src0->nb[1]);
  7418. float *y = (float *)((char *) src1->data + i1*src1->nb[1]);
  7419. float *dx = (float *)((char *) dst->data + i1*dst->nb[1]);
  7420. #ifndef NDEBUG
  7421. for (int i = 0; i < nc; ++i) {
  7422. //printf("p[%d] = %f\n", i, p[i]);
  7423. assert(!isnan(dy[i]));
  7424. assert(!isnan(y[i]));
  7425. }
  7426. #endif
  7427. // Jii = yi - yi*yi
  7428. // Jij = -yi*yj
  7429. // J = diag(y)-y.T*y
  7430. // dx = J * dy
  7431. // dxk = sum_i(Jki * dyi)
  7432. // dxk = sum_i(-yk*yi * dyi) - (-yk*yk)*dyk + (yk - yk*yk)*dyk
  7433. // dxk = sum_i(-yk*yi * dyi) + yk*yk*dyk + yk*dyk - yk*yk*dyk
  7434. // dxk = sum_i(-yk*yi * dyi) + yk*dyk
  7435. // dxk = -yk * sum_i(yi * dyi) + yk*dyk
  7436. // dxk = -yk * dot(y, dy) + yk*dyk
  7437. // dxk = yk * (- dot(y, dy) + dyk)
  7438. // dxk = yk * (dyk - dot(y, dy))
  7439. //
  7440. // post-order:
  7441. // dot_y_dy := dot(y, dy)
  7442. // dx := dy
  7443. // dx := dx - dot_y_dy
  7444. // dx := dx * y
  7445. // linear runtime, no additional memory
  7446. float dot_y_dy = 0;
  7447. ggml_vec_dot_f32 (nc, &dot_y_dy, 0, y, 0, dy, 0, 1);
  7448. ggml_vec_cpy_f32 (nc, dx, dy);
  7449. ggml_vec_acc1_f32(nc, dx, -dot_y_dy);
  7450. ggml_vec_mul_f32 (nc, dx, dx, y);
  7451. #ifndef NDEBUG
  7452. for (int i = 0; i < nc; ++i) {
  7453. assert(!isnan(dx[i]));
  7454. assert(!isinf(dx[i]));
  7455. }
  7456. #endif
  7457. }
  7458. }
  7459. static void ggml_compute_forward_soft_max_back(
  7460. const struct ggml_compute_params * params,
  7461. struct ggml_tensor * dst) {
  7462. const struct ggml_tensor * src0 = dst->src[0];
  7463. switch (src0->type) {
  7464. case GGML_TYPE_F32:
  7465. {
  7466. ggml_compute_forward_soft_max_back_f32(params, dst);
  7467. } break;
  7468. default:
  7469. {
  7470. GGML_ABORT("fatal error");
  7471. }
  7472. }
  7473. }
  7474. // ggml_compute_forward_clamp
  7475. static void ggml_compute_forward_clamp_f32(
  7476. const struct ggml_compute_params * params,
  7477. struct ggml_tensor * dst) {
  7478. const struct ggml_tensor * src0 = dst->src[0];
  7479. if (params->ith != 0) {
  7480. return;
  7481. }
  7482. float min;
  7483. float max;
  7484. memcpy(&min, (float *) dst->op_params + 0, sizeof(float));
  7485. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  7486. const int ith = params->ith;
  7487. const int nth = params->nth;
  7488. const int n = ggml_nrows(src0);
  7489. const int nc = src0->ne[0];
  7490. const size_t nb00 = src0->nb[0];
  7491. const size_t nb01 = src0->nb[1];
  7492. const size_t nb0 = dst->nb[0];
  7493. const size_t nb1 = dst->nb[1];
  7494. GGML_ASSERT( nb0 == sizeof(float));
  7495. GGML_ASSERT(nb00 == sizeof(float));
  7496. for (int j = ith; j < n; j += nth) {
  7497. float * dst_ptr = (float *) ((char *) dst->data + j*nb1);
  7498. float * src0_ptr = (float *) ((char *) src0->data + j*nb01);
  7499. for (int i = 0; i < nc; i++) {
  7500. dst_ptr[i] = MAX(MIN(src0_ptr[i], max), min);
  7501. }
  7502. }
  7503. }
  7504. static void ggml_compute_forward_clamp(
  7505. const struct ggml_compute_params * params,
  7506. struct ggml_tensor * dst) {
  7507. const struct ggml_tensor * src0 = dst->src[0];
  7508. switch (src0->type) {
  7509. case GGML_TYPE_F32:
  7510. {
  7511. ggml_compute_forward_clamp_f32(params, dst);
  7512. } break;
  7513. case GGML_TYPE_F16:
  7514. case GGML_TYPE_BF16:
  7515. case GGML_TYPE_Q4_0:
  7516. case GGML_TYPE_Q4_1:
  7517. case GGML_TYPE_Q5_0:
  7518. case GGML_TYPE_Q5_1:
  7519. case GGML_TYPE_Q8_0:
  7520. case GGML_TYPE_Q8_1:
  7521. case GGML_TYPE_Q2_K:
  7522. case GGML_TYPE_Q3_K:
  7523. case GGML_TYPE_Q4_K:
  7524. case GGML_TYPE_Q5_K:
  7525. case GGML_TYPE_Q6_K:
  7526. case GGML_TYPE_TQ1_0:
  7527. case GGML_TYPE_TQ2_0:
  7528. case GGML_TYPE_IQ2_XXS:
  7529. case GGML_TYPE_IQ2_XS:
  7530. case GGML_TYPE_IQ3_XXS:
  7531. case GGML_TYPE_IQ1_S:
  7532. case GGML_TYPE_IQ1_M:
  7533. case GGML_TYPE_IQ4_NL:
  7534. case GGML_TYPE_IQ4_XS:
  7535. case GGML_TYPE_IQ3_S:
  7536. case GGML_TYPE_IQ2_S:
  7537. case GGML_TYPE_Q8_K:
  7538. case GGML_TYPE_I8:
  7539. case GGML_TYPE_I16:
  7540. case GGML_TYPE_I32:
  7541. case GGML_TYPE_I64:
  7542. case GGML_TYPE_F64:
  7543. case GGML_TYPE_COUNT:
  7544. {
  7545. GGML_ABORT("fatal error");
  7546. }
  7547. }
  7548. }
  7549. // ggml_compute_forward_rope
  7550. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  7551. const float y = (i0 / 2 - low) / MAX(0.001f, high - low);
  7552. return 1 - MIN(1, MAX(0, y));
  7553. }
  7554. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  7555. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  7556. static void rope_yarn(
  7557. float theta_extrap, float freq_scale, float corr_dims[2], int64_t i0, float ext_factor, float mscale,
  7558. float * cos_theta, float * sin_theta) {
  7559. // Get n-d rotational scaling corrected for extrapolation
  7560. float theta_interp = freq_scale * theta_extrap;
  7561. float theta = theta_interp;
  7562. if (ext_factor != 0.0f) {
  7563. float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor;
  7564. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  7565. // Get n-d magnitude scaling corrected for interpolation
  7566. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  7567. }
  7568. *cos_theta = cosf(theta) * mscale;
  7569. *sin_theta = sinf(theta) * mscale;
  7570. }
  7571. static void ggml_rope_cache_init(
  7572. float theta_base, float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale,
  7573. float * cache, float sin_sign, float theta_scale) {
  7574. // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py
  7575. float theta = theta_base;
  7576. for (int64_t i0 = 0; i0 < ne0; i0 += 2) {
  7577. const float ff = freq_factors ? freq_factors[i0/2] : 1.0f;
  7578. rope_yarn(
  7579. theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1]
  7580. );
  7581. cache[i0 + 1] *= sin_sign;
  7582. theta *= theta_scale;
  7583. }
  7584. }
  7585. static void ggml_mrope_cache_init(
  7586. float theta_base_t, float theta_base_h, float theta_base_w, float theta_base_e, int sections[4], bool indep_sects,
  7587. float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale,
  7588. float * cache, float sin_sign, float theta_scale) {
  7589. // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py
  7590. float theta_t = theta_base_t;
  7591. float theta_h = theta_base_h;
  7592. float theta_w = theta_base_w;
  7593. float theta_e = theta_base_e; // extra position id for vision encoder
  7594. int sect_dims = sections[0] + sections[1] + sections[2] + sections[3];
  7595. int sec_w = sections[1] + sections[0];
  7596. int sec_e = sections[2] + sec_w;
  7597. GGML_ASSERT(sect_dims <= ne0);
  7598. for (int64_t i0 = 0; i0 < ne0; i0 += 2) {
  7599. const float ff = freq_factors ? freq_factors[i0/2] : 1.0f;
  7600. int sector = (i0 / 2) % sect_dims;
  7601. if (indep_sects) {
  7602. // compute theta independently for each dim sections
  7603. // (i.e. reset corresponding theta when `i0` go from one section to another)
  7604. if (sector == 0) {
  7605. theta_t = theta_base_t;
  7606. }
  7607. else if (sector == sections[0]) {
  7608. theta_h = theta_base_h;;
  7609. }
  7610. else if (sector == sec_w) {
  7611. theta_w = theta_base_w;
  7612. }
  7613. else if (sector == sec_e) {
  7614. theta_e = theta_base_e;
  7615. }
  7616. }
  7617. float theta = theta_t;
  7618. if (sector >= sections[0] && sector < sec_w) {
  7619. theta = theta_h;
  7620. }
  7621. else if (sector >= sec_w && sector < sec_w + sections[2]) {
  7622. theta = theta_w;
  7623. }
  7624. else if (sector >= sec_w + sections[2]) {
  7625. theta = theta_e;
  7626. }
  7627. rope_yarn(
  7628. theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1]
  7629. );
  7630. cache[i0 + 1] *= sin_sign;
  7631. theta_t *= theta_scale;
  7632. theta_w *= theta_scale;
  7633. theta_h *= theta_scale;
  7634. theta_e *= theta_scale;
  7635. }
  7636. }
  7637. static void ggml_compute_forward_rope_f32(
  7638. const struct ggml_compute_params * params,
  7639. struct ggml_tensor * dst,
  7640. const bool forward) {
  7641. const struct ggml_tensor * src0 = dst->src[0];
  7642. const struct ggml_tensor * src1 = dst->src[1];
  7643. const struct ggml_tensor * src2 = dst->src[2];
  7644. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7645. int sections[4];
  7646. //const int n_past = ((int32_t *) dst->op_params)[0];
  7647. const int n_dims = ((int32_t *) dst->op_params)[1];
  7648. const int mode = ((int32_t *) dst->op_params)[2];
  7649. //const int n_ctx = ((int32_t *) dst->op_params)[3];
  7650. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  7651. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7652. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7653. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7654. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7655. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7656. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7657. memcpy(&sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  7658. GGML_TENSOR_UNARY_OP_LOCALS
  7659. //printf("ne0: %d, ne1: %d, ne2: %d, ne3: %d\n", ne0, ne1, ne2, ne3);
  7660. //printf("n_past = %d, ne2 = %d\n", n_past, ne2);
  7661. GGML_ASSERT(nb00 == sizeof(float));
  7662. const int ith = params->ith;
  7663. const int nth = params->nth;
  7664. const int nr = ggml_nrows(dst);
  7665. GGML_ASSERT(n_dims <= ne0);
  7666. GGML_ASSERT(n_dims % 2 == 0);
  7667. // rows per thread
  7668. const int dr = (nr + nth - 1)/nth;
  7669. // row range for this thread
  7670. const int ir0 = dr*ith;
  7671. const int ir1 = MIN(ir0 + dr, nr);
  7672. // row index used to determine which thread to use
  7673. int ir = 0;
  7674. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  7675. float corr_dims[2];
  7676. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  7677. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  7678. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE; // ggml_rope_multi, multimodal rotary position embedding
  7679. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  7680. if (is_mrope) {
  7681. GGML_ASSERT(sections[0] > 0 || sections[1] > 0 || sections[2] > 0);
  7682. }
  7683. if (is_vision) {
  7684. GGML_ASSERT(n_dims == ne0/2);
  7685. }
  7686. const float * freq_factors = NULL;
  7687. if (src2 != NULL) {
  7688. GGML_ASSERT(src2->type == GGML_TYPE_F32);
  7689. GGML_ASSERT(src2->ne[0] >= n_dims / 2);
  7690. freq_factors = (const float *) src2->data;
  7691. }
  7692. // backward process uses inverse rotation by cos and sin.
  7693. // cos and sin build a rotation matrix, where the inverse is the transpose.
  7694. // this essentially just switches the sign of sin.
  7695. const float sin_sign = forward ? 1.0f : -1.0f;
  7696. const int32_t * pos = (const int32_t *) src1->data;
  7697. for (int64_t i3 = 0; i3 < ne3; i3++) { // batch
  7698. for (int64_t i2 = 0; i2 < ne2; i2++) { // seq-len
  7699. float * cache = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32)*ith;
  7700. if (!is_mrope) {
  7701. const int64_t p = pos[i2];
  7702. ggml_rope_cache_init(p, freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7703. }
  7704. else {
  7705. const int64_t p_t = pos[i2];
  7706. const int64_t p_h = pos[i2 + ne2];
  7707. const int64_t p_w = pos[i2 + ne2 * 2];
  7708. const int64_t p_e = pos[i2 + ne2 * 3];
  7709. ggml_mrope_cache_init(
  7710. p_t, p_h, p_w, p_e, sections, is_vision,
  7711. freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7712. }
  7713. for (int64_t i1 = 0; i1 < ne1; i1++) { // attn-heads
  7714. if (ir++ < ir0) continue;
  7715. if (ir > ir1) break;
  7716. if (is_neox || is_mrope) {
  7717. if (is_vision){
  7718. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7719. const int64_t ic = i0/2;
  7720. const float cos_theta = cache[i0 + 0];
  7721. const float sin_theta = cache[i0 + 1];
  7722. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7723. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7724. const float x0 = src[0];
  7725. const float x1 = src[n_dims];
  7726. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7727. dst_data[n_dims] = x0*sin_theta + x1*cos_theta;
  7728. }
  7729. } else {
  7730. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7731. const int64_t ic = i0/2;
  7732. const float cos_theta = cache[i0 + 0];
  7733. const float sin_theta = cache[i0 + 1];
  7734. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7735. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7736. const float x0 = src[0];
  7737. const float x1 = src[n_dims/2];
  7738. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7739. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  7740. }
  7741. }
  7742. } else {
  7743. for (int64_t i0 = 0; i0 < n_dims; 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 + i0*nb00);
  7747. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7748. const float x0 = src[0];
  7749. const float x1 = src[1];
  7750. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7751. dst_data[1] = x0*sin_theta + x1*cos_theta;
  7752. }
  7753. }
  7754. if (is_vision) {
  7755. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7756. const int64_t ic = i0/2;
  7757. const float cos_theta = cache[i0 + 0];
  7758. const float sin_theta = cache[i0 + 1];
  7759. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7760. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7761. const float x0 = src[0];
  7762. const float x1 = src[n_dims];
  7763. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7764. dst_data[n_dims] = x0*sin_theta + x1*cos_theta;
  7765. }
  7766. } else {
  7767. // fill the remain channels with data from src tensor
  7768. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7769. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7770. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7771. dst_data[0] = src[0];
  7772. dst_data[1] = src[1];
  7773. }
  7774. }
  7775. }
  7776. }
  7777. }
  7778. }
  7779. // TODO: deduplicate f16/f32 code
  7780. static void ggml_compute_forward_rope_f16(
  7781. const struct ggml_compute_params * params,
  7782. struct ggml_tensor * dst,
  7783. const bool forward) {
  7784. const struct ggml_tensor * src0 = dst->src[0];
  7785. const struct ggml_tensor * src1 = dst->src[1];
  7786. const struct ggml_tensor * src2 = dst->src[2];
  7787. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7788. int sections[4];
  7789. //const int n_past = ((int32_t *) dst->op_params)[0];
  7790. const int n_dims = ((int32_t *) dst->op_params)[1];
  7791. const int mode = ((int32_t *) dst->op_params)[2];
  7792. //const int n_ctx = ((int32_t *) dst->op_params)[3];
  7793. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  7794. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7795. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7796. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7797. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7798. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7799. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7800. memcpy(&sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  7801. GGML_TENSOR_UNARY_OP_LOCALS
  7802. //printf("ne0: %d, ne1: %d, ne2: %d, ne3: %d\n", ne0, ne1, ne2, ne3);
  7803. //printf("n_past = %d, ne2 = %d\n", n_past, ne2);
  7804. GGML_ASSERT(nb0 == sizeof(ggml_fp16_t));
  7805. const int ith = params->ith;
  7806. const int nth = params->nth;
  7807. const int nr = ggml_nrows(dst);
  7808. GGML_ASSERT(n_dims <= ne0);
  7809. GGML_ASSERT(n_dims % 2 == 0);
  7810. // rows per thread
  7811. const int dr = (nr + nth - 1)/nth;
  7812. // row range for this thread
  7813. const int ir0 = dr*ith;
  7814. const int ir1 = MIN(ir0 + dr, nr);
  7815. // row index used to determine which thread to use
  7816. int ir = 0;
  7817. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  7818. float corr_dims[2];
  7819. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  7820. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  7821. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  7822. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  7823. if (is_mrope) {
  7824. GGML_ASSERT(sections[0] > 0 || sections[1] > 0 || sections[2] > 0);
  7825. }
  7826. if (is_vision) {
  7827. GGML_ASSERT(n_dims == ne0/2);
  7828. }
  7829. const float * freq_factors = NULL;
  7830. if (src2 != NULL) {
  7831. GGML_ASSERT(src2->type == GGML_TYPE_F32);
  7832. GGML_ASSERT(src2->ne[0] >= n_dims / 2);
  7833. freq_factors = (const float *) src2->data;
  7834. }
  7835. // backward process uses inverse rotation by cos and sin.
  7836. // cos and sin build a rotation matrix, where the inverse is the transpose.
  7837. // this essentially just switches the sign of sin.
  7838. const float sin_sign = forward ? 1.0f : -1.0f;
  7839. const int32_t * pos = (const int32_t *) src1->data;
  7840. for (int64_t i3 = 0; i3 < ne3; i3++) {
  7841. for (int64_t i2 = 0; i2 < ne2; i2++) {
  7842. float * cache = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32)*ith;
  7843. if (!is_mrope) {
  7844. const int64_t p = pos[i2];
  7845. ggml_rope_cache_init(p, freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7846. }
  7847. else {
  7848. const int64_t p_t = pos[i2];
  7849. const int64_t p_h = pos[i2 + ne2];
  7850. const int64_t p_w = pos[i2 + ne2 * 2];
  7851. const int64_t p_e = pos[i2 + ne2 * 3];
  7852. ggml_mrope_cache_init(
  7853. p_t, p_h, p_w, p_e, sections, is_vision,
  7854. freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7855. }
  7856. for (int64_t i1 = 0; i1 < ne1; i1++) {
  7857. if (ir++ < ir0) continue;
  7858. if (ir > ir1) break;
  7859. if (is_neox || is_mrope) {
  7860. if (is_vision) {
  7861. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7862. const int64_t ic = i0/2;
  7863. const float cos_theta = cache[i0 + 0];
  7864. const float sin_theta = cache[i0 + 1];
  7865. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7866. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7867. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7868. const float x1 = GGML_FP16_TO_FP32(src[n_dims]);
  7869. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7870. dst_data[n_dims] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7871. }
  7872. } else {
  7873. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7874. const int64_t ic = i0/2;
  7875. const float cos_theta = cache[i0 + 0];
  7876. const float sin_theta = cache[i0 + 1];
  7877. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7878. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7879. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7880. const float x1 = GGML_FP16_TO_FP32(src[n_dims/2]);
  7881. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7882. dst_data[n_dims/2] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7883. }
  7884. }
  7885. } else {
  7886. for (int64_t i0 = 0; i0 < n_dims; 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 + i0*nb00);
  7890. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7891. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7892. const float x1 = GGML_FP16_TO_FP32(src[1]);
  7893. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7894. dst_data[1] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7895. }
  7896. }
  7897. if (is_vision) {
  7898. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7899. const int64_t ic = i0/2;
  7900. const float cos_theta = cache[i0 + 0];
  7901. const float sin_theta = cache[i0 + 1];
  7902. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7903. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7904. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7905. const float x1 = GGML_FP16_TO_FP32(src[n_dims]);
  7906. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7907. dst_data[n_dims] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7908. }
  7909. } else {
  7910. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7911. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7912. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7913. dst_data[0] = src[0];
  7914. dst_data[1] = src[1];
  7915. }
  7916. }
  7917. }
  7918. }
  7919. }
  7920. }
  7921. static void ggml_compute_forward_rope(
  7922. const struct ggml_compute_params * params,
  7923. struct ggml_tensor * dst) {
  7924. const struct ggml_tensor * src0 = dst->src[0];
  7925. switch (src0->type) {
  7926. case GGML_TYPE_F16:
  7927. {
  7928. ggml_compute_forward_rope_f16(params, dst, true);
  7929. } break;
  7930. case GGML_TYPE_F32:
  7931. {
  7932. ggml_compute_forward_rope_f32(params, dst, true);
  7933. } break;
  7934. default:
  7935. {
  7936. GGML_ABORT("fatal error");
  7937. }
  7938. }
  7939. }
  7940. // ggml_compute_forward_rope_back
  7941. static void ggml_compute_forward_rope_back(
  7942. const struct ggml_compute_params * params,
  7943. struct ggml_tensor * dst) {
  7944. const struct ggml_tensor * src0 = dst->src[0];
  7945. switch (src0->type) {
  7946. case GGML_TYPE_F16:
  7947. {
  7948. ggml_compute_forward_rope_f16(params, dst, false);
  7949. } break;
  7950. case GGML_TYPE_F32:
  7951. {
  7952. ggml_compute_forward_rope_f32(params, dst, false);
  7953. } break;
  7954. default:
  7955. {
  7956. GGML_ABORT("fatal error");
  7957. }
  7958. }
  7959. }
  7960. // ggml_compute_forward_conv_transpose_1d
  7961. static void ggml_compute_forward_conv_transpose_1d_f16_f32(
  7962. const struct ggml_compute_params * params,
  7963. struct ggml_tensor * dst) {
  7964. const struct ggml_tensor * src0 = dst->src[0];
  7965. const struct ggml_tensor * src1 = dst->src[1];
  7966. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7967. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7968. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7969. GGML_TENSOR_BINARY_OP_LOCALS
  7970. const int ith = params->ith;
  7971. const int nth = params->nth;
  7972. const int nk = ne00*ne01*ne02;
  7973. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  7974. GGML_ASSERT(nb10 == sizeof(float));
  7975. if (ith == 0) {
  7976. memset(params->wdata, 0, params->wsize);
  7977. // permute kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout)
  7978. {
  7979. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  7980. for (int64_t i02 = 0; i02 < ne02; i02++) {
  7981. for (int64_t i01 = 0; i01 < ne01; i01++) {
  7982. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i02*nb02 + i01*nb01);
  7983. ggml_fp16_t * dst_data = wdata + i01*ne00*ne02;
  7984. for (int64_t i00 = 0; i00 < ne00; i00++) {
  7985. dst_data[i00*ne02 + i02] = src[i00];
  7986. }
  7987. }
  7988. }
  7989. }
  7990. // permute source data (src1) from (L x Cin) to (Cin x L)
  7991. {
  7992. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + nk;
  7993. ggml_fp16_t * dst_data = wdata;
  7994. for (int64_t i11 = 0; i11 < ne11; i11++) {
  7995. const float * const src = (float *)((char *) src1->data + i11*nb11);
  7996. for (int64_t i10 = 0; i10 < ne10; i10++) {
  7997. dst_data[i10*ne11 + i11] = GGML_FP32_TO_FP16(src[i10]);
  7998. }
  7999. }
  8000. }
  8001. // need to zero dst since we are accumulating into it
  8002. memset(dst->data, 0, ggml_nbytes(dst));
  8003. }
  8004. ggml_barrier(params->threadpool);
  8005. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  8006. // total rows in dst
  8007. const int nr = ne1;
  8008. // rows per thread
  8009. const int dr = (nr + nth - 1)/nth;
  8010. // row range for this thread
  8011. const int ir0 = dr*ith;
  8012. const int ir1 = MIN(ir0 + dr, nr);
  8013. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  8014. ggml_fp16_t * const wdata_src = wdata + nk;
  8015. for (int i1 = ir0; i1 < ir1; i1++) {
  8016. float * dst_data = (float *)((char *) dst->data + i1*nb1);
  8017. ggml_fp16_t * wdata_kernel = wdata + i1*ne02*ne00;
  8018. for (int i10 = 0; i10 < ne10; i10++) {
  8019. const int i1n = i10*ne11;
  8020. for (int i00 = 0; i00 < ne00; i00++) {
  8021. float v = 0;
  8022. ggml_vec_dot_f16(ne02, &v, 0,
  8023. (ggml_fp16_t *) wdata_src + i1n, 0,
  8024. (ggml_fp16_t *) wdata_kernel + i00*ne02, 0, 1);
  8025. dst_data[i10*s0 + i00] += v;
  8026. }
  8027. }
  8028. }
  8029. }
  8030. static void ggml_compute_forward_conv_transpose_1d_f32(
  8031. const struct ggml_compute_params * params,
  8032. struct ggml_tensor * dst) {
  8033. const struct ggml_tensor * src0 = dst->src[0];
  8034. const struct ggml_tensor * src1 = dst->src[1];
  8035. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8036. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8037. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8038. GGML_TENSOR_BINARY_OP_LOCALS
  8039. const int ith = params->ith;
  8040. const int nth = params->nth;
  8041. const int nk = ne00*ne01*ne02;
  8042. GGML_ASSERT(nb00 == sizeof(float));
  8043. GGML_ASSERT(nb10 == sizeof(float));
  8044. if (ith == 0) {
  8045. memset(params->wdata, 0, params->wsize);
  8046. // prepare kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout)
  8047. {
  8048. float * const wdata = (float *) params->wdata + 0;
  8049. for (int64_t i02 = 0; i02 < ne02; i02++) {
  8050. for (int64_t i01 = 0; i01 < ne01; i01++) {
  8051. const float * const src = (float *)((char *) src0->data + i02*nb02 + i01*nb01);
  8052. float * dst_data = wdata + i01*ne00*ne02;
  8053. for (int64_t i00 = 0; i00 < ne00; i00++) {
  8054. dst_data[i00*ne02 + i02] = src[i00];
  8055. }
  8056. }
  8057. }
  8058. }
  8059. // prepare source data (src1)
  8060. {
  8061. float * const wdata = (float *) params->wdata + nk;
  8062. float * dst_data = wdata;
  8063. for (int64_t i11 = 0; i11 < ne11; i11++) {
  8064. const float * const src = (float *)((char *) src1->data + i11*nb11);
  8065. for (int64_t i10 = 0; i10 < ne10; i10++) {
  8066. dst_data[i10*ne11 + i11] = src[i10];
  8067. }
  8068. }
  8069. }
  8070. // need to zero dst since we are accumulating into it
  8071. memset(dst->data, 0, ggml_nbytes(dst));
  8072. }
  8073. ggml_barrier(params->threadpool);
  8074. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  8075. // total rows in dst
  8076. const int nr = ne1;
  8077. // rows per thread
  8078. const int dr = (nr + nth - 1)/nth;
  8079. // row range for this thread
  8080. const int ir0 = dr*ith;
  8081. const int ir1 = MIN(ir0 + dr, nr);
  8082. float * const wdata = (float *) params->wdata + 0;
  8083. float * const wdata_src = wdata + nk;
  8084. for (int i1 = ir0; i1 < ir1; i1++) {
  8085. float * dst_data = (float *)((char *) dst->data + i1*nb1);
  8086. float * wdata_kernel = wdata + i1*ne02*ne00;
  8087. for (int i10 = 0; i10 < ne10; i10++) {
  8088. const int i1n = i10*ne11;
  8089. for (int i00 = 0; i00 < ne00; i00++) {
  8090. float v = 0;
  8091. ggml_vec_dot_f32(ne02, &v, 0,
  8092. wdata_src + i1n, 0,
  8093. wdata_kernel + i00*ne02, 0, 1);
  8094. dst_data[i10*s0 + i00] += v;
  8095. }
  8096. }
  8097. }
  8098. }
  8099. static void ggml_compute_forward_conv_transpose_1d(
  8100. const struct ggml_compute_params * params,
  8101. struct ggml_tensor * dst) {
  8102. const struct ggml_tensor * src0 = dst->src[0];
  8103. switch (src0->type) {
  8104. case GGML_TYPE_F16:
  8105. {
  8106. ggml_compute_forward_conv_transpose_1d_f16_f32(params, dst);
  8107. } break;
  8108. case GGML_TYPE_F32:
  8109. {
  8110. ggml_compute_forward_conv_transpose_1d_f32(params, dst);
  8111. } break;
  8112. default:
  8113. {
  8114. GGML_ABORT("fatal error");
  8115. }
  8116. }
  8117. }
  8118. // ggml_compute_forward_im2col_f32
  8119. // src0: kernel [OC, IC, KH, KW]
  8120. // src1: image [N, IC, IH, IW]
  8121. // dst: result [N, OH, OW, IC*KH*KW]
  8122. static void ggml_compute_forward_im2col_f32(
  8123. const struct ggml_compute_params * params,
  8124. struct ggml_tensor * dst) {
  8125. const struct ggml_tensor * src0 = dst->src[0];
  8126. const struct ggml_tensor * src1 = dst->src[1];
  8127. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8128. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8129. GGML_TENSOR_BINARY_OP_LOCALS;
  8130. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8131. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8132. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  8133. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  8134. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  8135. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  8136. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  8137. const int ith = params->ith;
  8138. const int nth = params->nth;
  8139. const int64_t N = is_2D ? ne13 : ne12;
  8140. const int64_t IC = is_2D ? ne12 : ne11;
  8141. const int64_t IH = is_2D ? ne11 : 1;
  8142. const int64_t IW = ne10;
  8143. const int64_t KH = is_2D ? ne01 : 1;
  8144. const int64_t KW = ne00;
  8145. const int64_t OH = is_2D ? ne2 : 1;
  8146. const int64_t OW = ne1;
  8147. int ofs0 = is_2D ? nb13 : nb12;
  8148. int ofs1 = is_2D ? nb12 : nb11;
  8149. GGML_ASSERT(nb10 == sizeof(float));
  8150. // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
  8151. {
  8152. float * const wdata = (float *) dst->data;
  8153. for (int64_t in = 0; in < N; in++) {
  8154. for (int64_t ioh = 0; ioh < OH; ioh++) { // 1
  8155. for (int64_t iow = 0; iow < OW; iow++) {
  8156. for (int64_t iic = ith; iic < IC; iic += nth) {
  8157. // micro kernel
  8158. float * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW]
  8159. const float * const src_data = (float *)((char *) src1->data + in*ofs0 + iic*ofs1); // [IH, IW]
  8160. for (int64_t ikh = 0; ikh < KH; ikh++) { // 1
  8161. for (int64_t ikw = 0; ikw < KW; ikw++) {
  8162. const int64_t iiw = iow*s0 + ikw*d0 - p0;
  8163. const int64_t iih = ioh*s1 + ikh*d1 - p1;
  8164. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  8165. dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0;
  8166. } else {
  8167. dst_data[iic*(KH*KW) + ikh*KW + ikw] = (src_data[iih*IW + iiw]);
  8168. }
  8169. }
  8170. }
  8171. }
  8172. }
  8173. }
  8174. }
  8175. }
  8176. }
  8177. // ggml_compute_forward_im2col_f16
  8178. // src0: kernel [OC, IC, KH, KW]
  8179. // src1: image [N, IC, IH, IW]
  8180. // dst: result [N, OH, OW, IC*KH*KW]
  8181. static void ggml_compute_forward_im2col_f16(
  8182. const struct ggml_compute_params * params,
  8183. struct ggml_tensor * dst) {
  8184. const struct ggml_tensor * src0 = dst->src[0];
  8185. const struct ggml_tensor * src1 = dst->src[1];
  8186. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8187. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8188. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  8189. GGML_TENSOR_BINARY_OP_LOCALS;
  8190. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8191. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8192. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  8193. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  8194. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  8195. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  8196. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  8197. const int ith = params->ith;
  8198. const int nth = params->nth;
  8199. const int64_t N = is_2D ? ne13 : ne12;
  8200. const int64_t IC = is_2D ? ne12 : ne11;
  8201. const int64_t IH = is_2D ? ne11 : 1;
  8202. const int64_t IW = ne10;
  8203. const int64_t KH = is_2D ? ne01 : 1;
  8204. const int64_t KW = ne00;
  8205. const int64_t OH = is_2D ? ne2 : 1;
  8206. const int64_t OW = ne1;
  8207. int ofs0 = is_2D ? nb13 : nb12;
  8208. int ofs1 = is_2D ? nb12 : nb11;
  8209. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  8210. GGML_ASSERT(nb10 == sizeof(float));
  8211. // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
  8212. {
  8213. ggml_fp16_t * const wdata = (ggml_fp16_t *) dst->data;
  8214. for (int64_t in = 0; in < N; in++) {
  8215. for (int64_t ioh = 0; ioh < OH; ioh++) { // 1
  8216. for (int64_t iow = 0; iow < OW; iow++) {
  8217. for (int64_t iic = ith; iic < IC; iic += nth) {
  8218. // micro kernel
  8219. ggml_fp16_t * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW]
  8220. const float * const src_data = (float *)((char *) src1->data + in*ofs0 + iic*ofs1); // [IH, IW]
  8221. for (int64_t ikh = 0; ikh < KH; ikh++) { // 1
  8222. for (int64_t ikw = 0; ikw < KW; ikw++) {
  8223. const int64_t iiw = iow*s0 + ikw*d0 - p0;
  8224. const int64_t iih = ioh*s1 + ikh*d1 - p1;
  8225. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  8226. dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0;
  8227. } else {
  8228. dst_data[iic*(KH*KW) + ikh*KW + ikw] = GGML_FP32_TO_FP16(src_data[iih*IW + iiw]);
  8229. }
  8230. }
  8231. }
  8232. }
  8233. }
  8234. }
  8235. }
  8236. }
  8237. }
  8238. static void ggml_compute_forward_im2col(
  8239. const struct ggml_compute_params * params,
  8240. struct ggml_tensor * dst) {
  8241. switch (dst->type) {
  8242. case GGML_TYPE_F16:
  8243. {
  8244. ggml_compute_forward_im2col_f16(params, dst);
  8245. } break;
  8246. case GGML_TYPE_F32:
  8247. {
  8248. ggml_compute_forward_im2col_f32(params, dst);
  8249. } break;
  8250. default:
  8251. {
  8252. GGML_ABORT("fatal error");
  8253. }
  8254. }
  8255. }
  8256. // ggml_compute_forward_im2col_back_f32
  8257. static void ggml_compute_forward_im2col_back_f32(
  8258. const struct ggml_compute_params * params,
  8259. struct ggml_tensor * dst) {
  8260. const struct ggml_tensor * src0 = dst->src[0];
  8261. const struct ggml_tensor * src1 = dst->src[1];
  8262. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8263. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8264. GGML_TENSOR_BINARY_OP_LOCALS;
  8265. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8266. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8267. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  8268. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  8269. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  8270. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  8271. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  8272. const int ith = params->ith;
  8273. const int nth = params->nth;
  8274. const int64_t N = is_2D ? ne3 : ne2;
  8275. const int64_t IC = is_2D ? ne2 : ne1;
  8276. const int64_t IH = is_2D ? ne1 : 1;
  8277. const int64_t IW = ne0;
  8278. const int64_t KH = is_2D ? ne01 : 1;
  8279. const int64_t KW = ne00;
  8280. const int64_t OH = is_2D ? ne12 : 1;
  8281. const int64_t OW = ne11;
  8282. int ofs0 = is_2D ? nb3 : nb2;
  8283. int ofs1 = is_2D ? nb2 : nb1;
  8284. GGML_ASSERT(nb0 == sizeof(float));
  8285. // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
  8286. {
  8287. float * const wdata = (float *) dst->data;
  8288. for (int64_t in = 0; in < N; in++) {
  8289. for (int64_t iic = ith; iic < IC; iic += nth) {
  8290. for (int64_t iih = 0; iih < IH; iih++) {
  8291. for (int64_t iiw = 0; iiw < IW; iiw++) {
  8292. // micro kernel
  8293. float grad = 0.0f;
  8294. for (int64_t ikh = 0; ikh < KH; ikh++) {
  8295. for (int64_t ikw = 0; ikw < KW; ikw++) {
  8296. // For s0 > 1 some values were skipped over in the forward pass.
  8297. // These values have tmpw % s0 != 0 and need to be skipped in the backwards pass as well.
  8298. const int64_t tmpw = (iiw + p0 - ikw*d0);
  8299. if (tmpw % s0 != 0) {
  8300. continue;
  8301. }
  8302. const int64_t iow = tmpw / s0;
  8303. // Equivalent logic as above except for s1.
  8304. int64_t ioh;
  8305. if (is_2D) {
  8306. const int64_t tmph = iih + p1 - ikh*d1;
  8307. if (tmph % s1 != 0) {
  8308. continue;
  8309. }
  8310. ioh = tmph / s1;
  8311. } else {
  8312. ioh = 0;
  8313. }
  8314. if (iow < 0 || iow >= OW || ioh < 0 || ioh >= OH) {
  8315. continue;
  8316. }
  8317. const float * const src_data = (const float *) src1->data
  8318. + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW]
  8319. grad += src_data[iic*(KH*KW) + ikh*KW + ikw];
  8320. }
  8321. }
  8322. float * dst_data = (float *)((char *) wdata + (in*ofs0 + iic*ofs1)); // [IH, IW]
  8323. dst_data[iih*IW + iiw] = grad;
  8324. }
  8325. }
  8326. }
  8327. }
  8328. }
  8329. }
  8330. // ggml_compute_forward_conv_transpose_2d
  8331. static void ggml_compute_forward_conv_transpose_2d(
  8332. const struct ggml_compute_params * params,
  8333. struct ggml_tensor * dst) {
  8334. const struct ggml_tensor * src0 = dst->src[0];
  8335. const struct ggml_tensor * src1 = dst->src[1];
  8336. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8337. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8338. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8339. GGML_TENSOR_BINARY_OP_LOCALS
  8340. const int ith = params->ith;
  8341. const int nth = params->nth;
  8342. const int nk = ne00*ne01*ne02*ne03;
  8343. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  8344. GGML_ASSERT(nb10 == sizeof(float));
  8345. if (ith == 0) {
  8346. memset(params->wdata, 0, params->wsize);
  8347. // permute kernel data (src0) from (Kw x Kh x Cout x Cin) to (Cin x Kw x Kh x Cout)
  8348. {
  8349. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  8350. for (int64_t i03 = 0; i03 < ne03; i03++) {
  8351. for (int64_t i02 = 0; i02 < ne02; i02++) {
  8352. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i03*nb03 + i02*nb02);
  8353. ggml_fp16_t * dst_data = wdata + i02*ne01*ne00*ne03;
  8354. for (int64_t i01 = 0; i01 < ne01; i01++) {
  8355. for (int64_t i00 = 0; i00 < ne00; i00++) {
  8356. dst_data[i01*ne00*ne03 + i00*ne03 + i03] = src[i01 * ne00 + i00];
  8357. }
  8358. }
  8359. }
  8360. }
  8361. }
  8362. // permute source data (src1) from (Sw x Sh x Cin) to (Cin x Sw x Sh)
  8363. {
  8364. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + nk;
  8365. for (int i12 = 0; i12 < ne12; i12++) {
  8366. for (int i11 = 0; i11 < ne11; i11++) {
  8367. const float * const src = (float *)((char *) src1->data + i12*nb12 + i11*nb11);
  8368. ggml_fp16_t * dst_data = wdata + i11*ne10*ne12;
  8369. for (int i10 = 0; i10 < ne10; i10++) {
  8370. dst_data[i10*ne12 + i12] = GGML_FP32_TO_FP16(src[i10]);
  8371. }
  8372. }
  8373. }
  8374. }
  8375. memset(dst->data, 0, ggml_nbytes(dst));
  8376. }
  8377. ggml_barrier(params->threadpool);
  8378. const int32_t stride = ggml_get_op_params_i32(dst, 0);
  8379. // total patches in dst
  8380. const int np = ne2;
  8381. // patches per thread
  8382. const int dp = (np + nth - 1)/nth;
  8383. // patch range for this thread
  8384. const int ip0 = dp*ith;
  8385. const int ip1 = MIN(ip0 + dp, np);
  8386. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  8387. ggml_fp16_t * const wdata_src = wdata + nk;
  8388. for (int i2 = ip0; i2 < ip1; i2++) { // Cout
  8389. float * dst_data = (float *)((char *) dst->data + i2*nb2);
  8390. ggml_fp16_t * wdata_kernel = wdata + i2*ne01*ne00*ne03;
  8391. for (int i11 = 0; i11 < ne11; i11++) {
  8392. for (int i10 = 0; i10 < ne10; i10++) {
  8393. const int i1n = i11*ne10*ne12 + i10*ne12;
  8394. for (int i01 = 0; i01 < ne01; i01++) {
  8395. for (int i00 = 0; i00 < ne00; i00++) {
  8396. float v = 0;
  8397. ggml_vec_dot_f16(ne03, &v, 0,
  8398. wdata_src + i1n, 0,
  8399. wdata_kernel + i01*ne00*ne03 + i00*ne03, 0, 1);
  8400. dst_data[(i11*stride + i01)*ne0 + i10*stride + i00] += v;
  8401. }
  8402. }
  8403. }
  8404. }
  8405. }
  8406. }
  8407. // ggml_compute_forward_pool_1d_sk_p0
  8408. static void ggml_compute_forward_pool_1d_sk_p0(
  8409. const struct ggml_compute_params * params,
  8410. const enum ggml_op_pool op,
  8411. const int k,
  8412. struct ggml_tensor * dst) {
  8413. const struct ggml_tensor * src = dst->src[0];
  8414. assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16);
  8415. if (params->ith != 0) {
  8416. return;
  8417. }
  8418. const char * cdata = (const char *)src->data;
  8419. const char * const data_end = cdata + ggml_nbytes(src);
  8420. float * drow = (float *)dst->data;
  8421. const int64_t rs = dst->ne[0];
  8422. while (cdata < data_end) {
  8423. const void * srow = (const void *)cdata;
  8424. int j = 0;
  8425. for (int64_t i = 0; i < rs; ++i) {
  8426. switch (op) {
  8427. case GGML_OP_POOL_AVG: drow[i] = 0; break;
  8428. case GGML_OP_POOL_MAX: drow[i] = -FLT_MAX; break;
  8429. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8430. }
  8431. for (int ki = 0; ki < k; ++ki) {
  8432. const float srow_j = (src->type == GGML_TYPE_F32) ? ((const float*)srow)[j] : GGML_FP16_TO_FP32(((const ggml_fp16_t*)srow)[j]);
  8433. switch (op) {
  8434. case GGML_OP_POOL_AVG: drow[i] += srow_j; break;
  8435. case GGML_OP_POOL_MAX: if (srow_j > drow[i]) drow[i] = srow_j; break;
  8436. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8437. }
  8438. ++j;
  8439. }
  8440. switch (op) {
  8441. case GGML_OP_POOL_AVG: drow[i] /= k; break;
  8442. case GGML_OP_POOL_MAX: break;
  8443. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8444. }
  8445. }
  8446. cdata += src->nb[1];
  8447. drow += rs;
  8448. }
  8449. }
  8450. // ggml_compute_forward_pool_1d
  8451. static void ggml_compute_forward_pool_1d(
  8452. const struct ggml_compute_params * params,
  8453. struct ggml_tensor * dst) {
  8454. const int32_t * opts = (const int32_t *)dst->op_params;
  8455. enum ggml_op_pool op = opts[0];
  8456. const int k0 = opts[1];
  8457. const int s0 = opts[2];
  8458. const int p0 = opts[3];
  8459. GGML_ASSERT(p0 == 0); // padding not supported
  8460. GGML_ASSERT(k0 == s0); // only s = k supported
  8461. ggml_compute_forward_pool_1d_sk_p0(params, op, k0, dst);
  8462. }
  8463. // ggml_compute_forward_pool_2d
  8464. static void ggml_compute_forward_pool_2d(
  8465. const struct ggml_compute_params * params,
  8466. struct ggml_tensor * dst) {
  8467. const struct ggml_tensor * src = dst->src[0];
  8468. assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16);
  8469. if (params->ith != 0) {
  8470. return;
  8471. }
  8472. const int32_t * opts = (const int32_t *)dst->op_params;
  8473. enum ggml_op_pool op = opts[0];
  8474. const int k0 = opts[1];
  8475. const int k1 = opts[2];
  8476. const int s0 = opts[3];
  8477. const int s1 = opts[4];
  8478. const int p0 = opts[5];
  8479. const int p1 = opts[6];
  8480. const char * cdata = (const char*)src->data;
  8481. const char * const data_end = cdata + ggml_nbytes(src);
  8482. const int64_t px = dst->ne[0];
  8483. const int64_t py = dst->ne[1];
  8484. const int64_t pa = px * py;
  8485. float * dplane = (float *)dst->data;
  8486. const int ka = k0 * k1;
  8487. const int offset0 = -p0;
  8488. const int offset1 = -p1;
  8489. while (cdata < data_end) {
  8490. for (int oy = 0; oy < py; ++oy) {
  8491. float * const drow = dplane + oy * px;
  8492. for (int ox = 0; ox < px; ++ox) {
  8493. float * const out = drow + ox;
  8494. switch (op) {
  8495. case GGML_OP_POOL_AVG: *out = 0; break;
  8496. case GGML_OP_POOL_MAX: *out = -FLT_MAX; break;
  8497. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8498. }
  8499. const int ix = offset0 + ox * s0;
  8500. const int iy = offset1 + oy * s1;
  8501. for (int ky = 0; ky < k1; ++ky) {
  8502. if (iy + ky < 0 || iy + ky >= src->ne[1]) continue;
  8503. const void * srow = (const void *)(cdata + src->nb[1] * (iy + ky));
  8504. for (int kx = 0; kx < k0; ++kx) {
  8505. int j = ix + kx;
  8506. if (j < 0 || j >= src->ne[0]) continue;
  8507. const float srow_j = (src->type == GGML_TYPE_F32) ? ((const float*)srow)[j] : GGML_FP16_TO_FP32(((const ggml_fp16_t*)srow)[j]);
  8508. switch (op) {
  8509. case GGML_OP_POOL_AVG: *out += srow_j; break;
  8510. case GGML_OP_POOL_MAX: if (srow_j > *out) *out = srow_j; break;
  8511. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8512. }
  8513. }
  8514. }
  8515. switch (op) {
  8516. case GGML_OP_POOL_AVG: *out /= ka; break;
  8517. case GGML_OP_POOL_MAX: break;
  8518. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8519. }
  8520. }
  8521. }
  8522. cdata += src->nb[2];
  8523. dplane += pa;
  8524. }
  8525. }
  8526. // ggml_compute_forward_pool_2d_back
  8527. static void ggml_compute_forward_pool_2d_back(
  8528. const struct ggml_compute_params * params,
  8529. struct ggml_tensor * dst) {
  8530. const struct ggml_tensor * src = dst->src[0];
  8531. const struct ggml_tensor * dstf = dst->src[1]; // forward tensor of dst
  8532. assert(dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  8533. if (params->ith != 0) {
  8534. return;
  8535. }
  8536. const int32_t * opts = (const int32_t *)dst->op_params;
  8537. enum ggml_op_pool op = opts[0];
  8538. const int k0 = opts[1];
  8539. const int k1 = opts[2];
  8540. const int s0 = opts[3];
  8541. const int s1 = opts[4];
  8542. const int p0 = opts[5];
  8543. const int p1 = opts[6];
  8544. char * cdata = (char *) dst->data;
  8545. const char * cdataf = (const char *) dstf->data;
  8546. const char * const data_end = cdata + ggml_nbytes(dst);
  8547. GGML_ASSERT(params->ith == 0);
  8548. memset(cdata, 0, ggml_nbytes(dst));
  8549. const int64_t px = src->ne[0];
  8550. const int64_t py = src->ne[1];
  8551. const int64_t pa = px * py;
  8552. const float * splane = (const float *) src->data;
  8553. const int ka = k0 * k1;
  8554. const int offset0 = -p0;
  8555. const int offset1 = -p1;
  8556. while (cdata < data_end) {
  8557. for (int oy = 0; oy < py; ++oy) {
  8558. const float * const srow = splane + oy * px;
  8559. for (int ox = 0; ox < px; ++ox) {
  8560. const float grad0 = srow[ox];
  8561. const int ix = offset0 + ox * s0;
  8562. const int iy = offset1 + oy * s1;
  8563. if (op == GGML_OP_POOL_MAX) {
  8564. float maxval = -FLT_MAX;
  8565. int kxmax = -1;
  8566. int kymax = -1;
  8567. for (int ky = 0; ky < k1; ++ky) {
  8568. if (iy + ky < 0 || iy + ky >= dst->ne[1]) {
  8569. continue;
  8570. }
  8571. const void * drowf = (const void *)(cdataf + dst->nb[1] * (iy + ky));
  8572. for (int kx = 0; kx < k0; ++kx) {
  8573. int j = ix + kx;
  8574. if (j < 0 || j >= dst->ne[0]) {
  8575. continue;
  8576. }
  8577. const float val = dst->type == GGML_TYPE_F32 ?
  8578. ((const float *) drowf)[j] : GGML_FP16_TO_FP32(((const ggml_fp16_t *) drowf)[j]);
  8579. if (val <= maxval) {
  8580. continue;
  8581. }
  8582. maxval = val;
  8583. kxmax = kx;
  8584. kymax = ky;
  8585. }
  8586. }
  8587. if (kxmax == -1 || kymax == -1) {
  8588. continue;
  8589. }
  8590. void * drow = (void *)(cdata + dst->nb[1] * (iy + kymax));
  8591. const int j = ix + kxmax;
  8592. if (dst->type == GGML_TYPE_F32) {
  8593. ((float *) drow)[j] += grad0;
  8594. } else {
  8595. ((ggml_fp16_t *) drow)[j] = GGML_FP32_TO_FP16(grad0 + GGML_FP16_TO_FP32(((const ggml_fp16_t *) drow)[j]));
  8596. }
  8597. } else if (op == GGML_OP_POOL_AVG) {
  8598. const float grad = grad0 / ka;
  8599. for (int ky = 0; ky < k1; ++ky) {
  8600. if (iy + ky < 0 || iy + ky >= dst->ne[1]) {
  8601. continue;
  8602. }
  8603. void * drow = (void *)(cdata + dst->nb[1] * (iy + ky));
  8604. for (int kx = 0; kx < k0; ++kx) {
  8605. int j = ix + kx;
  8606. if (j < 0 || j >= dst->ne[0]) {
  8607. continue;
  8608. }
  8609. if (dst->type == GGML_TYPE_F32) {
  8610. ((float *) drow)[j] += grad;
  8611. } else {
  8612. ((ggml_fp16_t *) drow)[j] += GGML_FP32_TO_FP16(grad);
  8613. }
  8614. }
  8615. }
  8616. } else {
  8617. GGML_ASSERT(false);
  8618. }
  8619. }
  8620. }
  8621. cdata += dst->nb[2];
  8622. cdataf += dst->nb[2];
  8623. splane += pa;
  8624. }
  8625. }
  8626. // ggml_compute_forward_upscale
  8627. static void ggml_compute_forward_upscale_f32(
  8628. const struct ggml_compute_params * params,
  8629. struct ggml_tensor * dst) {
  8630. const struct ggml_tensor * src0 = dst->src[0];
  8631. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8632. const int ith = params->ith;
  8633. const int nth = params->nth;
  8634. GGML_TENSOR_UNARY_OP_LOCALS
  8635. const float sf0 = (float)ne0/src0->ne[0];
  8636. const float sf1 = (float)ne1/src0->ne[1];
  8637. const float sf2 = (float)ne2/src0->ne[2];
  8638. const float sf3 = (float)ne3/src0->ne[3];
  8639. // TODO: optimize
  8640. for (int64_t i3 = 0; i3 < ne3; i3++) {
  8641. const int64_t i03 = i3 / sf3;
  8642. for (int64_t i2 = ith; i2 < ne2; i2 += nth) {
  8643. const int64_t i02 = i2 / sf2;
  8644. for (int64_t i1 = 0; i1 < ne1; i1++) {
  8645. const int64_t i01 = i1 / sf1;
  8646. for (int64_t i0 = 0; i0 < ne0; i0++) {
  8647. const int64_t i00 = i0 / sf0;
  8648. const float * x = (float *)((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  8649. float * y = (float *)((char *) dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3);
  8650. *y = *x;
  8651. }
  8652. }
  8653. }
  8654. }
  8655. }
  8656. static void ggml_compute_forward_upscale(
  8657. const struct ggml_compute_params * params,
  8658. struct ggml_tensor * dst) {
  8659. const struct ggml_tensor * src0 = dst->src[0];
  8660. switch (src0->type) {
  8661. case GGML_TYPE_F32:
  8662. {
  8663. ggml_compute_forward_upscale_f32(params, dst);
  8664. } break;
  8665. default:
  8666. {
  8667. GGML_ABORT("fatal error");
  8668. }
  8669. }
  8670. }
  8671. // ggml_compute_forward_pad
  8672. static void ggml_compute_forward_pad_f32(
  8673. const struct ggml_compute_params * params,
  8674. struct ggml_tensor * dst) {
  8675. const struct ggml_tensor * src0 = dst->src[0];
  8676. GGML_ASSERT(src0->nb[0] == sizeof(float));
  8677. GGML_ASSERT( dst->nb[0] == sizeof(float));
  8678. const int ith = params->ith;
  8679. const int nth = params->nth;
  8680. GGML_TENSOR_UNARY_OP_LOCALS
  8681. float * dst_ptr = (float *) dst->data;
  8682. // TODO: optimize
  8683. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  8684. for (int64_t i1 = ith; i1 < ne1; i1 += nth) {
  8685. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  8686. for (int64_t i3 = 0; i3 < ne3; ++i3) {
  8687. const int64_t dst_idx = i3*(ne0*ne1*ne2) + i2*(ne0*ne1) + i1*ne0 + i0;
  8688. const float * src_ptr = (const float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  8689. if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
  8690. dst_ptr[dst_idx] = *src_ptr;
  8691. } else {
  8692. dst_ptr[dst_idx] = 0;
  8693. }
  8694. }
  8695. }
  8696. }
  8697. }
  8698. }
  8699. static void ggml_compute_forward_pad(
  8700. const struct ggml_compute_params * params,
  8701. struct ggml_tensor * dst) {
  8702. const struct ggml_tensor * src0 = dst->src[0];
  8703. switch (src0->type) {
  8704. case GGML_TYPE_F32:
  8705. {
  8706. ggml_compute_forward_pad_f32(params, dst);
  8707. } break;
  8708. default:
  8709. {
  8710. GGML_ABORT("fatal error");
  8711. }
  8712. }
  8713. }
  8714. // ggml_compute_forward_pad_reflect_1d
  8715. static void ggml_compute_forward_pad_reflect_1d(
  8716. const struct ggml_compute_params * params,
  8717. struct ggml_tensor * dst) {
  8718. const struct ggml_tensor * src0 = dst->src[0];
  8719. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8720. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8721. const int ith = params->ith;
  8722. const int nth = params->nth;
  8723. const int32_t * opts = (const int32_t *) dst->op_params;
  8724. const int p0 = opts[0];
  8725. const int p1 = opts[1];
  8726. GGML_TENSOR_UNARY_OP_LOCALS
  8727. for (int64_t i3 = 0; i3 < ne3; i3++) {
  8728. for (int64_t i2 = 0; i2 < ne2; i2++) {
  8729. for (int64_t i1 = ith; i1 < ne1; i1 += nth) {
  8730. float * left = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + p0*nb0);
  8731. float * right = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + (ne0-p1-1)*nb0);
  8732. ggml_vec_cpy_f32(ne00, left, (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01));
  8733. for (int i0 = 1; i0 <= p0; i0++) { left[-i0] = left[i0]; }
  8734. for (int i0 = 1; i0 <= p1; i0++) { right[i0] = right[-i0]; }
  8735. }
  8736. }
  8737. }
  8738. }
  8739. static void ggml_compute_forward_unpad_f32(
  8740. const struct ggml_compute_params *params,
  8741. struct ggml_tensor *dst) {
  8742. const struct ggml_tensor * src0 = dst->src[0];
  8743. GGML_ASSERT(src0->nb[0] == sizeof(float));
  8744. GGML_ASSERT( dst->nb[0] == sizeof(float));
  8745. const int ith = params->ith;
  8746. const int nth = params->nth;
  8747. GGML_TENSOR_UNARY_OP_LOCALS
  8748. float * dst_ptr = (float *) dst->data;
  8749. // TODO: optimize
  8750. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  8751. for (int64_t i1 = ith; i1 < ne1; i1 += nth) {
  8752. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  8753. for (int64_t i3 = 0; i3 < ne3; ++i3) {
  8754. const int64_t dst_idx = i3*(ne0*ne1*ne2) + i2*(ne0*ne1) + i1*ne0 + i0;
  8755. const float * src_ptr = (const float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  8756. if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
  8757. dst_ptr[dst_idx] = *src_ptr;
  8758. }
  8759. }
  8760. }
  8761. }
  8762. }
  8763. }
  8764. static void ggml_compute_forward_unpad(
  8765. const struct ggml_compute_params * params,
  8766. struct ggml_tensor * dst) {
  8767. const struct ggml_tensor * src0 = dst->src[0];
  8768. switch (src0->type) {
  8769. case GGML_TYPE_F32:
  8770. {
  8771. ggml_compute_forward_unpad_f32(params, dst);
  8772. } break;
  8773. default:
  8774. {
  8775. GGML_ABORT("fatal error");
  8776. }
  8777. }
  8778. }
  8779. // ggml_compute_forward_arange
  8780. static void ggml_compute_forward_arange_f32(
  8781. const struct ggml_compute_params * params,
  8782. struct ggml_tensor * dst) {
  8783. GGML_ASSERT(dst->nb[0] == sizeof(float));
  8784. const int ith = params->ith;
  8785. const int nth = params->nth;
  8786. const float start = ggml_get_op_params_f32(dst, 0);
  8787. const float stop = ggml_get_op_params_f32(dst, 1);
  8788. const float step = ggml_get_op_params_f32(dst, 2);
  8789. const int64_t steps = (int64_t) ceilf((stop - start) / step);
  8790. GGML_ASSERT(ggml_nelements(dst) == steps);
  8791. for (int64_t i = ith; i < steps; i+= nth) {
  8792. float value = start + step * i;
  8793. ((float *)dst->data)[i] = value;
  8794. }
  8795. }
  8796. static void ggml_compute_forward_arange(
  8797. const struct ggml_compute_params * params,
  8798. struct ggml_tensor * dst) {
  8799. switch (dst->type) {
  8800. case GGML_TYPE_F32:
  8801. {
  8802. ggml_compute_forward_arange_f32(params, dst);
  8803. } break;
  8804. default:
  8805. {
  8806. GGML_ABORT("fatal error");
  8807. }
  8808. }
  8809. }
  8810. static void ggml_compute_forward_timestep_embedding_f32(
  8811. const struct ggml_compute_params * params,
  8812. struct ggml_tensor * dst) {
  8813. const struct ggml_tensor * src0 = dst->src[0];
  8814. GGML_ASSERT(src0->nb[0] == sizeof(float));
  8815. const int ith = params->ith;
  8816. const int nth = params->nth;
  8817. GGML_TENSOR_UNARY_OP_LOCALS
  8818. const int dim = ggml_get_op_params_i32(dst, 0);
  8819. const int max_period = ggml_get_op_params_i32(dst, 1);
  8820. int half = dim / 2;
  8821. for (int64_t i = 0; i < ne00; i++) {
  8822. float * embed_data = (float *)((char *) dst->data + i*nb1);
  8823. for (int64_t j = ith; j < half; j += nth) {
  8824. float timestep = ((float *)src0->data)[i];
  8825. float freq = (float)expf(-logf(max_period) * j / half);
  8826. float arg = timestep * freq;
  8827. embed_data[j] = cosf(arg);
  8828. embed_data[j + half] = sinf(arg);
  8829. }
  8830. if (dim % 2 != 0 && ith == 0) {
  8831. embed_data[dim] = 0.f;
  8832. }
  8833. }
  8834. }
  8835. static void ggml_compute_forward_timestep_embedding(
  8836. const struct ggml_compute_params * params,
  8837. struct ggml_tensor * dst) {
  8838. const struct ggml_tensor * src0 = dst->src[0];
  8839. switch (src0->type) {
  8840. case GGML_TYPE_F32:
  8841. {
  8842. ggml_compute_forward_timestep_embedding_f32(params, dst);
  8843. } break;
  8844. default:
  8845. {
  8846. GGML_ABORT("fatal error");
  8847. }
  8848. }
  8849. }
  8850. // ggml_compute_forward_argsort
  8851. static void ggml_compute_forward_argsort_f32(
  8852. const struct ggml_compute_params * params,
  8853. struct ggml_tensor * dst) {
  8854. const struct ggml_tensor * src0 = dst->src[0];
  8855. GGML_TENSOR_UNARY_OP_LOCALS
  8856. GGML_ASSERT(nb0 == sizeof(float));
  8857. const int ith = params->ith;
  8858. const int nth = params->nth;
  8859. const int64_t nr = ggml_nrows(src0);
  8860. enum ggml_sort_order order = (enum ggml_sort_order) ggml_get_op_params_i32(dst, 0);
  8861. for (int64_t i = ith; i < nr; i += nth) {
  8862. int32_t * dst_data = (int32_t *)((char *) dst->data + i*nb1);
  8863. const float * src_data = (float *)((char *) src0->data + i*nb01);
  8864. for (int64_t j = 0; j < ne0; j++) {
  8865. dst_data[j] = j;
  8866. }
  8867. // C doesn't have a functional sort, so we do a bubble sort instead
  8868. for (int64_t j = 0; j < ne0; j++) {
  8869. for (int64_t k = j + 1; k < ne0; k++) {
  8870. if ((order == GGML_SORT_ORDER_ASC && src_data[dst_data[j]] > src_data[dst_data[k]]) ||
  8871. (order == GGML_SORT_ORDER_DESC && src_data[dst_data[j]] < src_data[dst_data[k]])) {
  8872. int32_t tmp = dst_data[j];
  8873. dst_data[j] = dst_data[k];
  8874. dst_data[k] = tmp;
  8875. }
  8876. }
  8877. }
  8878. }
  8879. }
  8880. static void ggml_compute_forward_argsort(
  8881. const struct ggml_compute_params * params,
  8882. struct ggml_tensor * dst) {
  8883. const struct ggml_tensor * src0 = dst->src[0];
  8884. switch (src0->type) {
  8885. case GGML_TYPE_F32:
  8886. {
  8887. ggml_compute_forward_argsort_f32(params, dst);
  8888. } break;
  8889. default:
  8890. {
  8891. GGML_ABORT("fatal error");
  8892. }
  8893. }
  8894. }
  8895. // ggml_compute_forward_flash_attn_ext
  8896. static void ggml_compute_forward_flash_attn_ext_f16(
  8897. const struct ggml_compute_params * params,
  8898. const struct ggml_tensor * q,
  8899. const struct ggml_tensor * k,
  8900. const struct ggml_tensor * v,
  8901. const struct ggml_tensor * mask,
  8902. struct ggml_tensor * dst) {
  8903. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  8904. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  8905. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  8906. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  8907. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  8908. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  8909. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  8910. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  8911. const int ith = params->ith;
  8912. const int nth = params->nth;
  8913. const int64_t D = neq0;
  8914. const int64_t N = neq1;
  8915. GGML_ASSERT(ne0 == D);
  8916. GGML_ASSERT(ne2 == N);
  8917. // input tensor rows must be contiguous
  8918. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  8919. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  8920. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  8921. GGML_ASSERT(neq0 == D);
  8922. GGML_ASSERT(nek0 == D);
  8923. GGML_ASSERT(nev0 == D);
  8924. GGML_ASSERT(neq1 == N);
  8925. GGML_ASSERT(nev0 == D);
  8926. // dst cannot be transposed or permuted
  8927. GGML_ASSERT(nb0 == sizeof(float));
  8928. GGML_ASSERT(nb0 <= nb1);
  8929. GGML_ASSERT(nb1 <= nb2);
  8930. GGML_ASSERT(nb2 <= nb3);
  8931. // broadcast factors
  8932. const int64_t rk2 = neq2/nek2;
  8933. const int64_t rk3 = neq3/nek3;
  8934. const int64_t rv2 = neq2/nev2;
  8935. const int64_t rv3 = neq3/nev3;
  8936. // parallelize by q rows using ggml_vec_dot_f32
  8937. // total rows in q
  8938. const int nr = neq1*neq2*neq3;
  8939. // rows per thread
  8940. const int dr = (nr + nth - 1)/nth;
  8941. // row range for this thread
  8942. const int ir0 = dr*ith;
  8943. const int ir1 = MIN(ir0 + dr, nr);
  8944. float scale = 1.0f;
  8945. float max_bias = 0.0f;
  8946. float logit_softcap = 0.0f;
  8947. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  8948. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  8949. memcpy(&logit_softcap, (float *) dst->op_params + 2, sizeof(float));
  8950. if (logit_softcap != 0) {
  8951. scale /= logit_softcap;
  8952. }
  8953. const uint32_t n_head = neq2;
  8954. const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head));
  8955. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  8956. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  8957. enum ggml_type const k_vec_dot_type = type_traits_cpu[k->type].vec_dot_type;
  8958. ggml_from_float_t const q_to_vec_dot = type_traits_cpu[k_vec_dot_type].from_float;
  8959. ggml_vec_dot_t const kq_vec_dot = type_traits_cpu[k->type].vec_dot;
  8960. ggml_to_float_t const v_to_float = ggml_get_type_traits(v->type)->to_float;
  8961. GGML_ASSERT(q_to_vec_dot && "fattn: unsupported K-type");
  8962. GGML_ASSERT(v_to_float && "fattn: unsupported V-type");
  8963. // loop over n_batch and n_head
  8964. for (int ir = ir0; ir < ir1; ++ir) {
  8965. // q indices
  8966. const int iq3 = ir/(neq2*neq1);
  8967. const int iq2 = (ir - iq3*neq2*neq1)/neq1;
  8968. const int iq1 = (ir - iq3*neq2*neq1 - iq2*neq1);
  8969. const uint32_t h = iq2; // head index
  8970. 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;
  8971. float S = 0.0f; // sum
  8972. float M = -INFINITY; // maximum KQ value
  8973. float * VKQ32 = (float *) params->wdata + ith*(3*D + CACHE_LINE_SIZE_F32); // FP32 VKQ accumulator
  8974. float * V32 = (VKQ32 + 1*D); // (temporary) FP32 V buffer
  8975. ggml_fp16_t * VKQ16 = (ggml_fp16_t *) (VKQ32 + 1*D); // (temporary) FP16 VKQ accumulator
  8976. ggml_fp16_t * Q_q = (ggml_fp16_t *) (VKQ32 + 2*D); // (temporary) buffer for Q converted to quantized/FP16
  8977. if (v->type == GGML_TYPE_F16) {
  8978. memset(VKQ16, 0, D*sizeof(ggml_fp16_t));
  8979. } else {
  8980. memset(VKQ32, 0, D*sizeof(float));
  8981. }
  8982. const ggml_fp16_t * mp = mask ? (ggml_fp16_t *)((char *) mask->data + iq1*mask->nb[1]) : NULL;
  8983. // k indices
  8984. const int ik3 = iq3 / rk3;
  8985. const int ik2 = iq2 / rk2;
  8986. // v indices
  8987. const int iv3 = iq3 / rv3;
  8988. const int iv2 = iq2 / rv2;
  8989. const float * pq = (const float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3));
  8990. q_to_vec_dot(pq, Q_q, D);
  8991. // online softmax / attention
  8992. // loop over n_kv and n_head_kv
  8993. // ref: https://arxiv.org/pdf/2112.05682.pdf
  8994. for (int64_t ic = 0; ic < nek1; ++ic) {
  8995. const float mv = mp ? slope*GGML_FP16_TO_FP32(mp[ic]) : 0.0f;
  8996. if (mv == -INFINITY) {
  8997. continue;
  8998. }
  8999. float s; // KQ value
  9000. const char * k_data = (const char *) k->data + ( ic*nbk1 + ik2*nbk2 + ik3*nbk3);
  9001. kq_vec_dot(D, &s, 0, k_data, 0, Q_q, 0, 1);
  9002. s = s*scale; // scale KQ value
  9003. if (logit_softcap != 0.0f) {
  9004. s = logit_softcap*tanhf(s);
  9005. }
  9006. s += mv; // apply mask
  9007. const float Mold = M;
  9008. float ms = 1.0f; // upon new higher max val, scale VKQ and KQ sum with this value
  9009. float vs = 1.0f; // post-softmax KQ value, expf(s - M)
  9010. const char * v_data = ((const char *) v->data + (ic*nbv1 + iv2*nbv2 + iv3*nbv3));
  9011. if (v->type == GGML_TYPE_F16) {
  9012. if (s > M) {
  9013. // s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f
  9014. M = s;
  9015. ms = expf(Mold - M);
  9016. // V = V*expf(Mold - M)
  9017. ggml_vec_scale_f16(D, VKQ16, ms);
  9018. } else {
  9019. // no new maximum, ms == 1.0f, vs != 1.0f
  9020. vs = expf(s - M);
  9021. }
  9022. // V += v*expf(s - M)
  9023. ggml_vec_mad_f16(D, VKQ16, (const ggml_fp16_t *) v_data, vs);
  9024. } else {
  9025. if (s > M) {
  9026. // s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f
  9027. M = s;
  9028. ms = expf(Mold - M);
  9029. // V = V*expf(Mold - M)
  9030. ggml_vec_scale_f32(D, VKQ32, ms);
  9031. } else {
  9032. // no new maximum, ms == 1.0f, vs != 1.0f
  9033. vs = expf(s - M);
  9034. }
  9035. v_to_float(v_data, V32, D);
  9036. // V += v*expf(s - M)
  9037. ggml_vec_mad_f32(D, VKQ32, V32, vs);
  9038. }
  9039. S = S*ms + vs; // scale and increment sum with partial sum
  9040. }
  9041. if (v->type == GGML_TYPE_F16) {
  9042. for (int64_t d = 0; d < D; ++d) {
  9043. VKQ32[d] = GGML_FP16_TO_FP32(VKQ16[d]);
  9044. }
  9045. }
  9046. // V /= S
  9047. const float S_inv = 1.0f/S;
  9048. ggml_vec_scale_f32(D, VKQ32, S_inv);
  9049. // dst indices
  9050. const int i1 = iq1;
  9051. const int i2 = iq2;
  9052. const int i3 = iq3;
  9053. // original
  9054. //memcpy((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb3), V, nev0*sizeof(float));
  9055. // permute(0, 2, 1, 3)
  9056. memcpy((char *) dst->data + (i3*ne2*ne1 + i2 + i1*ne1)*nb1, VKQ32, nb1);
  9057. }
  9058. }
  9059. static void ggml_compute_forward_flash_attn_ext(
  9060. const struct ggml_compute_params * params,
  9061. const struct ggml_tensor * q,
  9062. const struct ggml_tensor * k,
  9063. const struct ggml_tensor * v,
  9064. const struct ggml_tensor * mask,
  9065. struct ggml_tensor * dst) {
  9066. switch (dst->op_params[3]) {
  9067. case GGML_PREC_DEFAULT:
  9068. case GGML_PREC_F32:
  9069. {
  9070. // uses F32 accumulators
  9071. ggml_compute_forward_flash_attn_ext_f16(params, q, k, v, mask, dst);
  9072. } break;
  9073. default:
  9074. {
  9075. GGML_ABORT("fatal error");
  9076. }
  9077. }
  9078. }
  9079. // ggml_compute_forward_flash_attn_back
  9080. static void ggml_compute_forward_flash_attn_back_f32(
  9081. const struct ggml_compute_params * params,
  9082. const bool masked,
  9083. struct ggml_tensor * dst) {
  9084. const struct ggml_tensor * q = dst->src[0];
  9085. const struct ggml_tensor * k = dst->src[1];
  9086. const struct ggml_tensor * v = dst->src[2];
  9087. const struct ggml_tensor * d = dst->src[3];
  9088. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  9089. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  9090. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  9091. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  9092. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  9093. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  9094. GGML_TENSOR_LOCALS(int64_t, ned, d, ne)
  9095. GGML_TENSOR_LOCALS(size_t, nbd, d, nb)
  9096. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  9097. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  9098. const int ith = params->ith;
  9099. const int nth = params->nth;
  9100. const int64_t D = neq0;
  9101. const int64_t N = neq1;
  9102. const int64_t P = nek1 - N;
  9103. const int64_t M = P + N;
  9104. const int Mup = ggml_up(M, GGML_SOFT_MAX_UNROLL);
  9105. const int mxDM = MAX(D, Mup);
  9106. // GGML_ASSERT(ne0 == D);
  9107. // GGML_ASSERT(ne1 == N);
  9108. GGML_ASSERT(P >= 0);
  9109. GGML_ASSERT(nbq0 == sizeof(float));
  9110. GGML_ASSERT(nbk0 == sizeof(float));
  9111. GGML_ASSERT(nbv0 == sizeof(float));
  9112. GGML_ASSERT(neq0 == D);
  9113. GGML_ASSERT(nek0 == D);
  9114. GGML_ASSERT(nev1 == D);
  9115. GGML_ASSERT(ned0 == D);
  9116. GGML_ASSERT(neq1 == N);
  9117. GGML_ASSERT(nek1 == N + P);
  9118. GGML_ASSERT(nev1 == D);
  9119. GGML_ASSERT(ned1 == N);
  9120. // dst cannot be transposed or permuted
  9121. GGML_ASSERT(nb0 == sizeof(float));
  9122. GGML_ASSERT(nb0 <= nb1);
  9123. GGML_ASSERT(nb1 <= nb2);
  9124. GGML_ASSERT(nb2 <= nb3);
  9125. if (ith == 0) {
  9126. memset(dst->data, 0, nb0*ne0*ne1*ne2*ne3);
  9127. }
  9128. ggml_barrier(params->threadpool);
  9129. const int64_t elem_q = ggml_nelements(q);
  9130. const int64_t elem_k = ggml_nelements(k);
  9131. enum ggml_type result_type = dst->type;
  9132. GGML_ASSERT(ggml_blck_size(result_type) == 1);
  9133. const size_t tsize = ggml_type_size(result_type);
  9134. const size_t offs_q = 0;
  9135. const size_t offs_k = offs_q + GGML_PAD(elem_q * tsize, GGML_MEM_ALIGN);
  9136. const size_t offs_v = offs_k + GGML_PAD(elem_k * tsize, GGML_MEM_ALIGN);
  9137. void * grad_q = (char *) dst->data;
  9138. void * grad_k = (char *) dst->data + offs_k;
  9139. void * grad_v = (char *) dst->data + offs_v;
  9140. const size_t nbgq1 = nb0*neq0;
  9141. const size_t nbgq2 = nb0*neq0*neq1;
  9142. const size_t nbgq3 = nb0*neq0*neq1*neq2;
  9143. const size_t nbgk1 = nb0*nek0;
  9144. const size_t nbgk2 = nb0*nek0*nek1;
  9145. const size_t nbgk3 = nb0*nek0*nek1*neq2;
  9146. const size_t nbgv1 = nb0*nev0;
  9147. const size_t nbgv2 = nb0*nev0*nev1;
  9148. const size_t nbgv3 = nb0*nev0*nev1*neq2;
  9149. // parallelize by k rows using ggml_vec_dot_f32
  9150. // total rows in k
  9151. const int nr = nek2*nek3;
  9152. // rows per thread
  9153. const int dr = (nr + nth - 1)/nth;
  9154. // row range for this thread
  9155. const int ir0 = dr*ith;
  9156. const int ir1 = MIN(ir0 + dr, nr);
  9157. const float scale = 1.0f/sqrtf(D);
  9158. //printf("P=%d N=%d D=%d ir0=%d ir1=%d scale = %f\n", P, N, D, ir0, ir1, scale);
  9159. // how often k2 (and v2) is repeated in q2
  9160. int nrep = neq2/nek2;
  9161. for (int ir = ir0; ir < ir1; ++ir) {
  9162. // q indices
  9163. const int ik3 = ir/(nek2);
  9164. const int ik2 = ir - ik3*nek2;
  9165. const int iq3 = ik3;
  9166. const int id3 = ik3;
  9167. const int iv3 = ik3;
  9168. const int iv2 = ik2;
  9169. for (int irep = 0; irep < nrep; ++irep) {
  9170. const int iq2 = ik2 + irep*nek2;
  9171. const int id2 = iq2;
  9172. // (ik2 + irep*nek2) % nek2 == ik2
  9173. for (int iq1 = 0; iq1 < neq1; ++iq1) {
  9174. const int id1 = iq1;
  9175. // not sure about CACHE_LINE_SIZE_F32..
  9176. // - maybe it must not be multiplied by 2 and excluded from .. in SM 1*(..) offset?
  9177. float * S = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 0*(mxDM+CACHE_LINE_SIZE_F32);
  9178. float * SM = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 1*(mxDM+CACHE_LINE_SIZE_F32);
  9179. for (int i = M; i < Mup; ++i) {
  9180. S[i] = -INFINITY;
  9181. }
  9182. const int64_t masked_begin = masked ? (P + iq1 + 1) : M;
  9183. for (int64_t ic = 0; ic < masked_begin; ++ic) {
  9184. // k indices
  9185. const int ik1 = ic;
  9186. // S indices
  9187. const int i1 = ik1;
  9188. ggml_vec_dot_f32(neq0,
  9189. S + i1, 0,
  9190. (float *) ((char *) k->data + (ik1*nbk1 + ik2*nbk2 + ik3*nbk3)), 0,
  9191. (float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)), 0, 1);
  9192. }
  9193. // scale
  9194. ggml_vec_scale_f32(masked_begin, S, scale);
  9195. for (int64_t i = masked_begin; i < M; i++) {
  9196. S[i] = -INFINITY;
  9197. }
  9198. // softmax
  9199. // exclude known -INF S[..] values from max and loop
  9200. // dont forget to set their SM values to zero
  9201. {
  9202. float max = -INFINITY;
  9203. ggml_vec_max_f32(masked_begin, &max, S);
  9204. ggml_float sum = 0.0;
  9205. {
  9206. #ifdef GGML_SOFT_MAX_ACCELERATE
  9207. max = -max;
  9208. vDSP_vsadd(SM, 1, &max, SM, 1, Mup);
  9209. vvexpf(SM, SM, &Mup);
  9210. ggml_vec_sum_f32(Mup, &sum, SM);
  9211. #else
  9212. sum = ggml_vec_soft_max_f32(Mup, SM, S, max);
  9213. #endif
  9214. }
  9215. assert(sum > 0.0);
  9216. sum = 1.0/sum;
  9217. ggml_vec_scale_f32(masked_begin, SM, sum);
  9218. }
  9219. // step-by-step explanation
  9220. {
  9221. // forward-process shape grads from backward process
  9222. // parallel_for ik2,ik3:
  9223. // for irep:
  9224. // iq2 = ik2 + irep*nek2
  9225. // k[:D,:M,:,:] [D,M,:,:] grad[k][:D,:M,ik2,ik3] += grad[kcur]
  9226. // q[:D,:N,:,:] [D,N,:,:] grad[q][:D,iq1,iq2,iq3] += grad[qcur]
  9227. // v[:M,:D,:,:] [M,D,:,:] grad[v][:M,:D,iv2,iv3] += grad[vcur]
  9228. // for iq1:
  9229. // kcur = k[:D,:M,ik2,ik3] [D,M,1,1] grad[kcur] = grad[S1].T @ qcur
  9230. // qcur = q[:D,iq1,iq2,iq3] [D,1,1,1] grad[qcur] = grad[S1] @ kcur
  9231. // vcur = v[:M,:D,iv2,iv3] [M,D,1,1] grad[vcur] = grad[S5].T @ S4
  9232. // S0 = -Inf [D,1,1,1]
  9233. // ~S1[i] = dot(kcur[:D,i], qcur)
  9234. // S1 = qcur @ kcur.T [M,1,1,1] grad[S1] = grad[S2] * scale
  9235. // S2 = S1 * scale [M,1,1,1] grad[S2] = diag_mask_zero(grad[S3], P)
  9236. // S3 = diag_mask_inf(S2, P) [M,1,1,1] grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4]))
  9237. // S4 = softmax(S3) [M,1,1,1] grad[S4] = grad[S5] @ vcur
  9238. // ~S5[i] = dot(vcur[:,i], S4)
  9239. // S5 = S4 @ vcur.T [D,1,1,1] grad[S5] = d[:D,id1,id2,id3]
  9240. // ~dst[i,iq1,iq2,iq3] = S5[i] ^
  9241. // dst[:D,iq1,iq2,iq3] = S5 | grad[dst[:D,iq1,iq2,iq3]] = d[:D,id1,id2,id3]
  9242. // dst backward-/ grad[dst] = d
  9243. //
  9244. // output gradients with their dependencies:
  9245. //
  9246. // grad[kcur] = grad[S1].T @ qcur
  9247. // grad[S1] = diag_mask_zero(grad[S3], P) * scale
  9248. // grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4]))
  9249. // grad[S4] = grad[S5] @ vcur
  9250. // grad[S4] = d[:D,id1,id2,id3] @ vcur
  9251. // grad[qcur] = grad[S1] @ kcur
  9252. // grad[vcur] = grad[S5].T @ S4
  9253. // grad[vcur] = d[:D,id1,id2,id3].T @ S4
  9254. //
  9255. // in post-order:
  9256. //
  9257. // S1 = qcur @ kcur.T
  9258. // S2 = S1 * scale
  9259. // S3 = diag_mask_inf(S2, P)
  9260. // S4 = softmax(S3)
  9261. // grad[S4] = d[:D,id1,id2,id3] @ vcur
  9262. // grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4]))
  9263. // grad[S1] = diag_mask_zero(grad[S3], P) * scale
  9264. // grad[qcur] = grad[S1] @ kcur
  9265. // grad[kcur] = grad[S1].T @ qcur
  9266. // grad[vcur] = d[:D,id1,id2,id3].T @ S4
  9267. //
  9268. // using less variables (SM=S4):
  9269. //
  9270. // S = diag_mask_inf(qcur @ kcur.T * scale, P)
  9271. // SM = softmax(S)
  9272. // S = d[:D,iq1,iq2,iq3] @ vcur
  9273. // dot_SM_gradSM = dot(SM, S)
  9274. // S = SM * (S - dot(SM, S))
  9275. // S = diag_mask_zero(S, P) * scale
  9276. //
  9277. // grad[q][:D,iq1,iq2,iq3] += S @ kcur
  9278. // grad[k][:D,:M,ik2,ik3] += S.T @ qcur
  9279. // grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM
  9280. }
  9281. // S = gradSM = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3]
  9282. // S = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3]
  9283. // for ic:
  9284. // S[:M] += vcur[:M,ic,iv2,iv3] * d[ic,id1,id2,id3]
  9285. // exclude known future zero S[..] values from operation
  9286. ggml_vec_set_f32(masked_begin, S, 0);
  9287. for (int64_t ic = 0; ic < D; ++ic) {
  9288. ggml_vec_mad_f32(masked_begin,
  9289. S,
  9290. (float *) ((char *) v->data + ( ic*nbv1 + iv2*nbv2 + iv3*nbv3)),
  9291. *(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3)));
  9292. }
  9293. // S = SM * (S - dot(SM, S))
  9294. float dot_SM_gradSM = 0;
  9295. ggml_vec_dot_f32 (masked_begin, &dot_SM_gradSM, 0, SM, 0, S, 0, 1);
  9296. ggml_vec_acc1_f32(M, S, -dot_SM_gradSM);
  9297. ggml_vec_mul_f32 (masked_begin, S, S, SM);
  9298. // S = diag_mask_zero(S, P) * scale
  9299. // already done by above ggml_vec_set_f32
  9300. // exclude known zero S[..] values from operation
  9301. ggml_vec_scale_f32(masked_begin, S, scale);
  9302. // S shape [M,1]
  9303. // SM shape [M,1]
  9304. // kcur shape [D,M]
  9305. // qcur shape [D,1]
  9306. // vcur shape [M,D]
  9307. // grad[q][:D,iq1,iq2,iq3] += S @ kcur
  9308. // grad[q][:D,iq1,iq2,iq3] += shape[M,1] @ shape[D,M]
  9309. // for ic:
  9310. // grad[q][:D,iq1,iq2,iq3] += S[ic] * kcur[:D,ic,ik2,ik3]
  9311. // exclude known zero S[..] values from loop
  9312. for (int64_t ic = 0; ic < masked_begin; ++ic) {
  9313. ggml_vec_mad_f32(D,
  9314. (float *) ((char *) grad_q + (iq1*nbgq1 + iq2*nbgq2 + iq3*nbgq3)),
  9315. (float *) ((char *) k->data + (ic*nbk1 + ik2*nbk2 + ik3*nbk3)),
  9316. S[ic]);
  9317. }
  9318. // grad[k][:D,:M,iq2,iq3] += S.T @ qcur
  9319. // for ic:
  9320. // grad[k][:D,ic,iq2,iq3] += S.T[0,ic] * qcur[:D,0]
  9321. // grad[k][:D,ic,iq2,iq3] += S[ic] * qcur[:D,0]
  9322. // exclude known zero S[..] values from loop
  9323. for (int64_t ic = 0; ic < masked_begin; ++ic) {
  9324. ggml_vec_mad_f32(D,
  9325. (float *) ((char *) grad_k + (ic*nbgk1 + ik2*nbgk2 + ik3*nbgk3)),
  9326. (float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)),
  9327. S[ic]);
  9328. }
  9329. // grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM
  9330. // for ic:
  9331. // grad[v][:M,ic,iv2,iv3] += d[:D,id1,id2,id3].T[0,ic] * SM[:M]
  9332. // grad[v][:M,ic,iv2,iv3] += d[ic,id1,id2,id3] * SM[:M]
  9333. // exclude known zero SM[..] values from mad
  9334. for (int64_t ic = 0; ic < D; ++ic) {
  9335. ggml_vec_mad_f32(masked_begin,
  9336. (float *) ((char *) grad_v + ( ic*nbgv1 + iv2*nbgv2 + iv3*nbgv3)),
  9337. SM,
  9338. *(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3)));
  9339. }
  9340. }
  9341. }
  9342. }
  9343. }
  9344. static void ggml_compute_forward_flash_attn_back(
  9345. const struct ggml_compute_params * params,
  9346. const bool masked,
  9347. struct ggml_tensor * dst) {
  9348. const struct ggml_tensor * q = dst->src[0];
  9349. switch (q->type) {
  9350. case GGML_TYPE_F32:
  9351. {
  9352. ggml_compute_forward_flash_attn_back_f32(params, masked, dst);
  9353. } break;
  9354. default:
  9355. {
  9356. GGML_ABORT("fatal error");
  9357. }
  9358. }
  9359. }
  9360. // ggml_compute_forward_ssm_conv
  9361. static void ggml_compute_forward_ssm_conv_f32(
  9362. const struct ggml_compute_params * params,
  9363. struct ggml_tensor * dst) {
  9364. const struct ggml_tensor * src0 = dst->src[0]; // conv_x
  9365. const struct ggml_tensor * src1 = dst->src[1]; // conv1d.weight
  9366. const int ith = params->ith;
  9367. const int nth = params->nth;
  9368. const int nc = src1->ne[0]; // d_conv
  9369. const int ncs = src0->ne[0]; // d_conv - 1 + n_t
  9370. const int nr = src0->ne[1]; // d_inner
  9371. const int n_t = dst->ne[1]; // tokens per sequence
  9372. const int n_s = dst->ne[2]; // number of sequences in the batch
  9373. GGML_ASSERT( dst->ne[0] == nr);
  9374. GGML_ASSERT(src0->nb[0] == sizeof(float));
  9375. GGML_ASSERT(src1->nb[0] == sizeof(float));
  9376. GGML_ASSERT(src0->nb[1] == src0->ne[0]*sizeof(float));
  9377. // rows per thread
  9378. const int dr = (nr + nth - 1)/nth;
  9379. // row range for this thread
  9380. const int ir0 = dr*ith;
  9381. const int ir1 = MIN(ir0 + dr, nr);
  9382. const int ir = ir1 - ir0;
  9383. for (int i3 = 0; i3 < n_s; ++i3) {
  9384. for (int i2 = 0; i2 < n_t; ++i2) {
  9385. // {d_conv - 1 + n_t, d_inner, n_seqs}
  9386. // sliding window
  9387. 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}
  9388. const float * c = (const float *) ((const char *) src1->data + ir0*(src1->nb[1])); // {d_conv, d_inner}
  9389. float * x = (float *) ((char *) dst->data + ir0*(dst->nb[0]) + i2*(dst->nb[1]) + i3*(dst->nb[2])); // {d_inner, n_t, n_s}
  9390. // TODO: transpose the output for smaller strides for big batches?
  9391. // d_inner
  9392. for (int i1 = 0; i1 < ir; ++i1) {
  9393. // rowwise dot product
  9394. // NOTE: not using ggml_vec_dot_f32, because its sum is in double precision
  9395. float sumf = 0.0f;
  9396. // d_conv
  9397. for (int i0 = 0; i0 < nc; ++i0) {
  9398. sumf += s[i0 + i1*ncs] * c[i0 + i1*nc];
  9399. }
  9400. x[i1] = sumf;
  9401. }
  9402. }
  9403. }
  9404. }
  9405. static void ggml_compute_forward_ssm_conv(
  9406. const struct ggml_compute_params * params,
  9407. struct ggml_tensor * dst) {
  9408. switch (dst->src[0]->type) {
  9409. case GGML_TYPE_F32:
  9410. {
  9411. ggml_compute_forward_ssm_conv_f32(params, dst);
  9412. } break;
  9413. default:
  9414. {
  9415. GGML_ABORT("fatal error");
  9416. }
  9417. }
  9418. }
  9419. // ggml_compute_forward_ssm_scan
  9420. static void ggml_compute_forward_ssm_scan_f32(
  9421. const struct ggml_compute_params * params,
  9422. struct ggml_tensor * dst) {
  9423. const struct ggml_tensor * src0 = dst->src[0]; // s
  9424. const struct ggml_tensor * src1 = dst->src[1]; // x
  9425. const struct ggml_tensor * src2 = dst->src[2]; // dt
  9426. const struct ggml_tensor * src3 = dst->src[3]; // A
  9427. const struct ggml_tensor * src4 = dst->src[4]; // B
  9428. const struct ggml_tensor * src5 = dst->src[5]; // C
  9429. const int ith = params->ith;
  9430. const int nth = params->nth;
  9431. const int64_t nc = src0->ne[0]; // d_state
  9432. const int64_t nr = src0->ne[1]; // d_inner
  9433. const int64_t n_t = src1->ne[1]; // number of tokens per sequence
  9434. const int64_t n_s = src0->ne[2]; // number of sequences in the batch
  9435. GGML_ASSERT(ggml_nelements(src1) + ggml_nelements(src0) == ggml_nelements(dst));
  9436. GGML_ASSERT(src0->nb[0] == sizeof(float));
  9437. GGML_ASSERT(src1->nb[0] == sizeof(float));
  9438. GGML_ASSERT(src2->nb[0] == sizeof(float));
  9439. GGML_ASSERT(src3->nb[0] == sizeof(float));
  9440. GGML_ASSERT(src4->nb[0] == sizeof(float));
  9441. GGML_ASSERT(src5->nb[0] == sizeof(float));
  9442. // required for the dot product between s and C
  9443. GGML_ASSERT(src0->nb[1] == src0->ne[0]*sizeof(float));
  9444. // required for per-sequence offsets for states
  9445. GGML_ASSERT(src0->nb[2] == src0->ne[0]*src0->ne[1]*sizeof(float));
  9446. // required to get correct offset for state destination (i.e. src1->nb[3])
  9447. GGML_ASSERT(src1->nb[3] == src1->ne[0]*src1->ne[1]*src1->ne[2]*sizeof(float));
  9448. // rows per thread
  9449. const int dr = (nr + nth - 1)/nth;
  9450. // row range for this thread
  9451. const int ir0 = dr*ith;
  9452. const int ir1 = MIN(ir0 + dr, nr);
  9453. const int ir = ir1 - ir0;
  9454. for (int i3 = 0; i3 < n_s; ++i3) {
  9455. for (int i2 = 0; i2 < n_t; ++i2) {
  9456. const float * s0 = (const float *) ((const char *) src0->data + ir0*(src0->nb[1]) + i3*(src0->nb[2])); // {d_state, d_inner, n_s}
  9457. 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}
  9458. 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}
  9459. const float * A = (const float *) ((const char *) src3->data + ir0*(src3->nb[1])); // {d_state, d_inner}
  9460. const float * B = (const float *) ((const char *) src4->data + i2*(src4->nb[1]) + i3*(src4->nb[2])); // {d_state, n_t, n_s}
  9461. const float * C = (const float *) ((const char *) src5->data + i2*(src5->nb[1]) + i3*(src5->nb[2])); // {d_state, n_t, n_s}
  9462. float * y = ( float *) (( char *) dst->data + ir0*(src1->nb[0]) + i2*(src1->nb[1]) + i3*(src1->nb[2])); // {d_inner, n_t, n_s}
  9463. float * s = ( float *) (( char *) dst->data + ir0*(src0->nb[1]) + i3*(src0->nb[2]) + src1->nb[3]); // {d_state, d_inner, n_s}
  9464. // use the output as the source for the next token-wise iterations
  9465. if (i2 > 0) { s0 = s; }
  9466. // d_inner
  9467. for (int i1 = 0; i1 < ir; ++i1) {
  9468. // ref: https://github.com/state-spaces/mamba/blob/34076d664838588a3c97727b263478ab9f621a07/mamba_ssm/ops/triton/selective_state_update.py#L78
  9469. float dt_soft_plus = dt[i1] <= 20.0f ? log1pf(expf(dt[i1])) : dt[i1];
  9470. float x_dt = x[i1] * dt_soft_plus;
  9471. float sumf = 0.0f;
  9472. // d_state
  9473. for (int i0 = 0; i0 < nc; ++i0) {
  9474. int i = i0 + i1*nc;
  9475. // state = prev_state * dA + dB * x
  9476. float state = (s0[i] * expf(dt_soft_plus * A[i])) + (B[i0] * x_dt);
  9477. // y = rowwise_dotprod(state, C)
  9478. sumf += state * C[i0];
  9479. s[i] = state;
  9480. }
  9481. y[i1] = sumf;
  9482. }
  9483. }
  9484. }
  9485. }
  9486. static void ggml_compute_forward_ssm_scan(
  9487. const struct ggml_compute_params * params,
  9488. struct ggml_tensor * dst) {
  9489. switch (dst->src[0]->type) {
  9490. case GGML_TYPE_F32:
  9491. {
  9492. ggml_compute_forward_ssm_scan_f32(params, dst);
  9493. } break;
  9494. default:
  9495. {
  9496. GGML_ABORT("fatal error");
  9497. }
  9498. }
  9499. }
  9500. // ggml_compute_forward_win_part
  9501. static void ggml_compute_forward_win_part_f32(
  9502. const struct ggml_compute_params * params,
  9503. struct ggml_tensor * dst) {
  9504. UNUSED(params);
  9505. const struct ggml_tensor * src0 = dst->src[0];
  9506. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  9507. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  9508. const int32_t nep0 = ((const int32_t *)(dst->op_params))[0];
  9509. const int32_t nep1 = ((const int32_t *)(dst->op_params))[1];
  9510. const int32_t w = ((const int32_t *)(dst->op_params))[2];
  9511. assert(ne00 == ne0);
  9512. assert(ne3 == nep0*nep1);
  9513. // TODO: optimize / multi-thread
  9514. for (int py = 0; py < nep1; ++py) {
  9515. for (int px = 0; px < nep0; ++px) {
  9516. const int64_t i3 = py*nep0 + px;
  9517. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  9518. for (int64_t i1 = 0; i1 < ne1; ++i1) {
  9519. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  9520. const int64_t i02 = py*w + i2;
  9521. const int64_t i01 = px*w + i1;
  9522. const int64_t i00 = i0;
  9523. const int64_t i = i3*ne2*ne1*ne0 + i2*ne1*ne0 + i1*ne0 + i0;
  9524. const int64_t j = i02*ne01*ne00 + i01*ne00 + i00;
  9525. if (py*w + i2 >= ne02 || px*w + i1 >= ne01) {
  9526. ((float *) dst->data)[i] = 0.0f;
  9527. } else {
  9528. ((float *) dst->data)[i] = ((float *) src0->data)[j];
  9529. }
  9530. }
  9531. }
  9532. }
  9533. }
  9534. }
  9535. }
  9536. static void ggml_compute_forward_win_part(
  9537. const struct ggml_compute_params * params,
  9538. struct ggml_tensor * dst) {
  9539. const struct ggml_tensor * src0 = dst->src[0];
  9540. switch (src0->type) {
  9541. case GGML_TYPE_F32:
  9542. {
  9543. ggml_compute_forward_win_part_f32(params, dst);
  9544. } break;
  9545. default:
  9546. {
  9547. GGML_ABORT("fatal error");
  9548. }
  9549. }
  9550. }
  9551. // ggml_compute_forward_win_unpart
  9552. static void ggml_compute_forward_win_unpart_f32(
  9553. const struct ggml_compute_params * params,
  9554. struct ggml_tensor * dst) {
  9555. UNUSED(params);
  9556. const struct ggml_tensor * src0 = dst->src[0];
  9557. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  9558. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  9559. const int32_t w = ((const int32_t *)(dst->op_params))[0];
  9560. // padding
  9561. const int px = (w - ne1%w)%w;
  9562. //const int py = (w - ne2%w)%w;
  9563. const int npx = (px + ne1)/w;
  9564. //const int npy = (py + ne2)/w;
  9565. assert(ne0 == ne00);
  9566. // TODO: optimize / multi-thread
  9567. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  9568. for (int64_t i1 = 0; i1 < ne1; ++i1) {
  9569. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  9570. const int ip2 = i2/w;
  9571. const int ip1 = i1/w;
  9572. const int64_t i02 = i2%w;
  9573. const int64_t i01 = i1%w;
  9574. const int64_t i00 = i0;
  9575. const int64_t i = (ip2*npx + ip1)*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00 + i00;
  9576. const int64_t j = i2*ne1*ne0 + i1*ne0 + i0;
  9577. ((float *) dst->data)[j] = ((float *) src0->data)[i];
  9578. }
  9579. }
  9580. }
  9581. }
  9582. static void ggml_compute_forward_win_unpart(
  9583. const struct ggml_compute_params * params,
  9584. struct ggml_tensor * dst) {
  9585. const struct ggml_tensor * src0 = dst->src[0];
  9586. switch (src0->type) {
  9587. case GGML_TYPE_F32:
  9588. {
  9589. ggml_compute_forward_win_unpart_f32(params, dst);
  9590. } break;
  9591. default:
  9592. {
  9593. GGML_ABORT("fatal error");
  9594. }
  9595. }
  9596. }
  9597. //gmml_compute_forward_unary
  9598. static void ggml_compute_forward_unary(
  9599. const struct ggml_compute_params * params,
  9600. struct ggml_tensor * dst) {
  9601. const enum ggml_unary_op op = ggml_get_unary_op(dst);
  9602. switch (op) {
  9603. case GGML_UNARY_OP_ABS:
  9604. {
  9605. ggml_compute_forward_abs(params, dst);
  9606. } break;
  9607. case GGML_UNARY_OP_SGN:
  9608. {
  9609. ggml_compute_forward_sgn(params, dst);
  9610. } break;
  9611. case GGML_UNARY_OP_NEG:
  9612. {
  9613. ggml_compute_forward_neg(params, dst);
  9614. } break;
  9615. case GGML_UNARY_OP_STEP:
  9616. {
  9617. ggml_compute_forward_step(params, dst);
  9618. } break;
  9619. case GGML_UNARY_OP_TANH:
  9620. {
  9621. ggml_compute_forward_tanh(params, dst);
  9622. } break;
  9623. case GGML_UNARY_OP_ELU:
  9624. {
  9625. ggml_compute_forward_elu(params, dst);
  9626. } break;
  9627. case GGML_UNARY_OP_RELU:
  9628. {
  9629. ggml_compute_forward_relu(params, dst);
  9630. } break;
  9631. case GGML_UNARY_OP_SIGMOID:
  9632. {
  9633. ggml_compute_forward_sigmoid(params, dst);
  9634. } break;
  9635. case GGML_UNARY_OP_GELU:
  9636. {
  9637. ggml_compute_forward_gelu(params, dst);
  9638. } break;
  9639. case GGML_UNARY_OP_GELU_QUICK:
  9640. {
  9641. ggml_compute_forward_gelu_quick(params, dst);
  9642. } break;
  9643. case GGML_UNARY_OP_SILU:
  9644. {
  9645. ggml_compute_forward_silu(params, dst);
  9646. } break;
  9647. case GGML_UNARY_OP_HARDSWISH:
  9648. {
  9649. ggml_compute_forward_hardswish(params, dst);
  9650. } break;
  9651. case GGML_UNARY_OP_HARDSIGMOID:
  9652. {
  9653. ggml_compute_forward_hardsigmoid(params, dst);
  9654. } break;
  9655. case GGML_UNARY_OP_EXP:
  9656. {
  9657. ggml_compute_forward_exp(params, dst);
  9658. } break;
  9659. default:
  9660. {
  9661. GGML_ABORT("fatal error");
  9662. }
  9663. }
  9664. }
  9665. // ggml_compute_forward_get_rel_pos
  9666. static void ggml_compute_forward_get_rel_pos_f16(
  9667. const struct ggml_compute_params * params,
  9668. struct ggml_tensor * dst) {
  9669. UNUSED(params);
  9670. const struct ggml_tensor * src0 = dst->src[0];
  9671. // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L292-L322
  9672. GGML_TENSOR_UNARY_OP_LOCALS
  9673. const int64_t w = ne1;
  9674. ggml_fp16_t * src0_data = (ggml_fp16_t *) src0->data;
  9675. ggml_fp16_t * dst_data = (ggml_fp16_t *) dst->data;
  9676. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  9677. for (int64_t i1 = 0; i1 < ne1; ++i1) {
  9678. const int64_t pos = (w - i1 - 1) + i2;
  9679. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  9680. dst_data[i2*ne1*ne0 + i1*ne0 + i0] = src0_data[pos*ne00 + i0];
  9681. }
  9682. }
  9683. }
  9684. }
  9685. static void ggml_compute_forward_get_rel_pos(
  9686. const struct ggml_compute_params * params,
  9687. struct ggml_tensor * dst) {
  9688. const struct ggml_tensor * src0 = dst->src[0];
  9689. switch (src0->type) {
  9690. case GGML_TYPE_F16:
  9691. case GGML_TYPE_BF16:
  9692. {
  9693. ggml_compute_forward_get_rel_pos_f16(params, dst);
  9694. } break;
  9695. default:
  9696. {
  9697. GGML_ABORT("fatal error");
  9698. }
  9699. }
  9700. }
  9701. // ggml_compute_forward_add_rel_pos
  9702. static void ggml_compute_forward_add_rel_pos_f32(
  9703. const struct ggml_compute_params * params,
  9704. struct ggml_tensor * dst) {
  9705. const struct ggml_tensor * src0 = dst->src[0];
  9706. const struct ggml_tensor * src1 = dst->src[1];
  9707. const struct ggml_tensor * src2 = dst->src[2];
  9708. const bool inplace = (bool) ((int32_t *) dst->op_params)[0];
  9709. if (!inplace) {
  9710. if (params->ith == 0) {
  9711. memcpy((char *) dst->data, (char *) src0->data, ggml_nbytes(dst));
  9712. }
  9713. ggml_barrier(params->threadpool);
  9714. }
  9715. // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L357-L359
  9716. float * src1_data = (float *) src1->data;
  9717. float * src2_data = (float *) src2->data;
  9718. float * dst_data = (float *) dst->data;
  9719. const int64_t ne10 = src1->ne[0];
  9720. const int64_t ne11 = src1->ne[1];
  9721. const int64_t ne12 = src1->ne[2];
  9722. const int64_t ne13 = src1->ne[3];
  9723. const int ith = params->ith;
  9724. const int nth = params->nth;
  9725. // total patches in dst
  9726. const int np = ne13;
  9727. // patches per thread
  9728. const int dp = (np + nth - 1)/nth;
  9729. // patch range for this thread
  9730. const int ip0 = dp*ith;
  9731. const int ip1 = MIN(ip0 + dp, np);
  9732. for (int64_t i13 = ip0; i13 < ip1; ++i13) {
  9733. for (int64_t i12 = 0; i12 < ne12; ++i12) {
  9734. for (int64_t i11 = 0; i11 < ne11; ++i11) {
  9735. const int64_t jp1 = i13*ne12*ne11*ne10 + i12*ne11*ne10 + i11*ne10;
  9736. for (int64_t i10 = 0; i10 < ne10; ++i10) {
  9737. const int64_t jp0 = jp1 + i10;
  9738. const float src1_e = src1_data[jp0];
  9739. const float src2_e = src2_data[jp0];
  9740. const int64_t jdh = jp0 * ne10;
  9741. const int64_t jdw = jdh - (ne10 - 1) * i10;
  9742. for (int64_t j = 0; j < ne10; ++j) {
  9743. dst_data[jdh + j ] += src2_e;
  9744. dst_data[jdw + j*ne10] += src1_e;
  9745. }
  9746. }
  9747. }
  9748. }
  9749. }
  9750. }
  9751. static void ggml_compute_forward_add_rel_pos(
  9752. const struct ggml_compute_params * params,
  9753. struct ggml_tensor * dst) {
  9754. const struct ggml_tensor * src0 = dst->src[0];
  9755. switch (src0->type) {
  9756. case GGML_TYPE_F32:
  9757. {
  9758. ggml_compute_forward_add_rel_pos_f32(params, dst);
  9759. } break;
  9760. default:
  9761. {
  9762. GGML_ABORT("fatal error");
  9763. }
  9764. }
  9765. }
  9766. // ggml_compute_forward_rwkv_wkv6
  9767. static void ggml_compute_forward_rwkv_wkv6_f32(
  9768. const struct ggml_compute_params * params,
  9769. struct ggml_tensor * dst) {
  9770. const int64_t T = dst->src[1]->ne[3];
  9771. const int64_t C = dst->ne[0];
  9772. const int64_t HEADS = dst->src[1]->ne[2];
  9773. const int64_t n_seqs = dst->src[5]->ne[1];
  9774. const int64_t head_size = C / HEADS;
  9775. float * dst_data = (float *) dst->data;
  9776. float * state = ((float *) dst->data) + C * T;
  9777. const int ith = params->ith;
  9778. const int nth = params->nth;
  9779. if (ith >= HEADS) {
  9780. return;
  9781. }
  9782. const int h_start = (HEADS * ith) / nth;
  9783. const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ?
  9784. (HEADS * (ith + 1)) / nth : HEADS;
  9785. float * k = (float *) dst->src[0]->data;
  9786. float * v = (float *) dst->src[1]->data;
  9787. float * r = (float *) dst->src[2]->data;
  9788. float * time_faaaa = (float *) dst->src[3]->data;
  9789. float * time_decay = (float *) dst->src[4]->data;
  9790. size_t t_stride = HEADS * head_size; // Same to C
  9791. size_t h_stride = C / HEADS;
  9792. GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS
  9793. size_t h_stride_2d = head_size * head_size;
  9794. if (ith == 0) {
  9795. memset(dst_data, 0, T * C * sizeof(float));
  9796. }
  9797. ggml_barrier(params->threadpool);
  9798. #if defined(__AVX__) && !defined(__AVX512F__)
  9799. #define GGML_F32X GGML_F32x8
  9800. #define GGML_F32X_SET1 GGML_F32x8_SET1
  9801. #define GGML_F32X_LOAD GGML_F32x8_LOAD
  9802. #define GGML_F32X_STORE GGML_F32x8_STORE
  9803. #define GGML_F32X_MUL GGML_F32x8_MUL
  9804. #define GGML_F32X_FMA GGML_F32x8_FMA
  9805. #define WKV_VECTOR_SIZE 8
  9806. #elif defined(__AVX512F__)
  9807. #define GGML_F32X GGML_F32x16
  9808. #define GGML_F32X_SET1 GGML_F32x16_SET1
  9809. #define GGML_F32X_LOAD GGML_F32x16_LOAD
  9810. #define GGML_F32X_STORE GGML_F32x16_STORE
  9811. #define GGML_F32X_MUL GGML_F32x16_MUL
  9812. #define GGML_F32X_FMA GGML_F32x16_FMA
  9813. #define WKV_VECTOR_SIZE 16
  9814. #elif defined(__ARM_NEON) && defined(__aarch64__)
  9815. #define GGML_F32X GGML_F32x4
  9816. #define GGML_F32X_SET1 GGML_F32x4_SET1
  9817. #define GGML_F32X_LOAD GGML_F32x4_LOAD
  9818. #define GGML_F32X_STORE GGML_F32x4_STORE
  9819. #define GGML_F32X_MUL GGML_F32x4_MUL
  9820. #define GGML_F32X_FMA GGML_F32x4_FMA
  9821. #define WKV_VECTOR_SIZE 4
  9822. #endif
  9823. #ifdef WKV_VECTOR_SIZE
  9824. const int64_t vec_count = head_size / WKV_VECTOR_SIZE;
  9825. for (int64_t t = 0; t < T; t++) {
  9826. size_t t_offset = t * t_stride;
  9827. size_t state_offset = head_size * C * (t / (T / n_seqs));
  9828. float * state_cur = state + state_offset;
  9829. float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset;
  9830. for (int64_t h = h_start; h < h_end; h++) {
  9831. size_t h_offset = h * h_stride;
  9832. size_t t_h_offset = t_offset + h_offset;
  9833. size_t h_2d_offset = h * h_stride_2d;
  9834. for (int64_t i = 0; i < head_size; i++) {
  9835. size_t t_h_i_offset = t_h_offset + i;
  9836. size_t h_i_offset = h_offset + i;
  9837. size_t h_2d_i_offset = h_2d_offset + i * h_stride;
  9838. float k_val = k[t_h_i_offset];
  9839. float r_val = r[t_h_i_offset];
  9840. float time_faaaa_val = time_faaaa[h_i_offset];
  9841. float time_decay_val = time_decay[t_h_i_offset];
  9842. // Broadcast scalar values to vectors
  9843. GGML_F32X k_vec = GGML_F32X_SET1(k_val);
  9844. GGML_F32X r_vec = GGML_F32X_SET1(r_val);
  9845. GGML_F32X time_faaaa_vec = GGML_F32X_SET1(time_faaaa_val);
  9846. GGML_F32X time_decay_vec = GGML_F32X_SET1(time_decay_val);
  9847. for (int64_t j = 0; j < vec_count; j++) {
  9848. size_t base_j = j * WKV_VECTOR_SIZE;
  9849. size_t t_h_j_offset = t_h_offset + base_j;
  9850. size_t h_2d_i_j_offset = h_2d_i_offset + base_j;
  9851. // Load x elements at once
  9852. GGML_F32X v_vec = GGML_F32X_LOAD(&v[t_h_j_offset]);
  9853. GGML_F32X prev_state_vec = GGML_F32X_LOAD(&state_prev[h_2d_i_j_offset]);
  9854. GGML_F32X dst_vec = GGML_F32X_LOAD(&dst_data[t_h_j_offset]);
  9855. // Compute kv = v * k
  9856. GGML_F32X kv_vec = GGML_F32X_MUL(v_vec, k_vec);
  9857. // Compute temp = kv * time_faaaa + prev_state
  9858. GGML_F32X temp_vec = GGML_F32X_FMA(prev_state_vec, kv_vec, time_faaaa_vec);
  9859. // Update dst: dst += temp * r
  9860. dst_vec = GGML_F32X_FMA(dst_vec, temp_vec, r_vec);
  9861. GGML_F32X_STORE(&dst_data[t_h_j_offset], dst_vec);
  9862. // Update state: state = prev_state * time_decay + kv
  9863. GGML_F32X new_state_vec = GGML_F32X_FMA(kv_vec, prev_state_vec, time_decay_vec);
  9864. GGML_F32X_STORE(&state_cur[h_2d_i_j_offset], new_state_vec);
  9865. }
  9866. // Handle remaining elements, this will not be used.
  9867. for (int64_t j = vec_count * WKV_VECTOR_SIZE; j < head_size; j++) {
  9868. size_t t_h_j_offset = t_h_offset + j;
  9869. size_t h_2d_i_j_offset = h_2d_i_offset + j;
  9870. float v_val = v[t_h_j_offset];
  9871. float kv_val = v_val * k_val;
  9872. float prev_state_val = state_prev[h_2d_i_j_offset];
  9873. float temp_val = kv_val * time_faaaa_val + prev_state_val;
  9874. dst_data[t_h_j_offset] += temp_val * r_val;
  9875. state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val;
  9876. }
  9877. }
  9878. }
  9879. }
  9880. #else
  9881. // basically fused operations:
  9882. // dst = r @ (time_faaaa * (k @ v) + state),
  9883. // state = time_decay * state + (k @ v),
  9884. // recursive through each token
  9885. for (int64_t t = 0; t < T; t++) {
  9886. size_t t_offset = t * t_stride;
  9887. size_t state_offset = head_size * C * (t / (T / n_seqs));
  9888. float * state_cur = state + state_offset;
  9889. float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset;
  9890. for (int64_t h = h_start; h < h_end; h++) {
  9891. size_t h_offset = h * h_stride;
  9892. size_t t_h_offset = t_offset + h_offset;
  9893. size_t h_2d_offset = h * h_stride_2d;
  9894. for (int64_t i = 0; i < head_size; i++) {
  9895. size_t t_h_i_offset = t_h_offset + i;
  9896. size_t h_i_offset = h_offset + i;
  9897. size_t h_2d_i_offset = h_2d_offset + i * h_stride;
  9898. float k_val = k[t_h_i_offset];
  9899. float r_val = r[t_h_i_offset];
  9900. float time_faaaa_val = time_faaaa[h_i_offset];
  9901. // RWKV v6: different time_decay for each token.
  9902. float time_decay_val = time_decay[t_h_i_offset];
  9903. for (int64_t j = 0; j < head_size; j++) {
  9904. size_t t_h_j_offset = t_h_offset + j;
  9905. size_t h_2d_i_j_offset = h_2d_i_offset + j;
  9906. float v_val = v[t_h_j_offset];
  9907. float kv_val = v_val * k_val;
  9908. float prev_state_val = state_prev[h_2d_i_j_offset];
  9909. float temp_val = kv_val * time_faaaa_val + prev_state_val;
  9910. dst_data[t_h_j_offset] += temp_val * r_val;
  9911. state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val;
  9912. }
  9913. }
  9914. }
  9915. }
  9916. #endif
  9917. }
  9918. static void ggml_compute_forward_rwkv_wkv6(
  9919. const struct ggml_compute_params * params,
  9920. struct ggml_tensor * dst) {
  9921. const struct ggml_tensor * src0 = dst->src[0];
  9922. switch (src0->type) {
  9923. case GGML_TYPE_F32:
  9924. {
  9925. ggml_compute_forward_rwkv_wkv6_f32(params, dst);
  9926. } break;
  9927. default:
  9928. {
  9929. GGML_ABORT("fatal error");
  9930. }
  9931. }
  9932. }
  9933. // ggml_compute_forward_map_unary
  9934. static void ggml_compute_forward_map_unary_f32(
  9935. const struct ggml_compute_params * params,
  9936. struct ggml_tensor * dst,
  9937. const ggml_unary_op_f32_t fun) {
  9938. const struct ggml_tensor * src0 = dst->src[0];
  9939. if (params->ith != 0) {
  9940. return;
  9941. }
  9942. assert(ggml_is_contiguous_1(src0));
  9943. assert(ggml_is_contiguous_1(dst));
  9944. assert(ggml_are_same_shape(src0, dst));
  9945. const int n = ggml_nrows(src0);
  9946. const int nc = src0->ne[0];
  9947. for (int i = 0; i < n; i++) {
  9948. fun(nc,
  9949. (float *) ((char *) dst->data + i*( dst->nb[1])),
  9950. (float *) ((char *) src0->data + i*(src0->nb[1])));
  9951. }
  9952. }
  9953. static void ggml_compute_forward_map_unary(
  9954. const struct ggml_compute_params * params,
  9955. struct ggml_tensor * dst,
  9956. const ggml_unary_op_f32_t fun) {
  9957. const struct ggml_tensor * src0 = dst->src[0];
  9958. switch (src0->type) {
  9959. case GGML_TYPE_F32:
  9960. {
  9961. ggml_compute_forward_map_unary_f32(params, dst, fun);
  9962. } break;
  9963. default:
  9964. {
  9965. GGML_ABORT("fatal error");
  9966. }
  9967. }
  9968. }
  9969. // ggml_compute_forward_map_binary
  9970. static void ggml_compute_forward_map_binary_f32(
  9971. const struct ggml_compute_params * params,
  9972. struct ggml_tensor * dst,
  9973. const ggml_binary_op_f32_t fun) {
  9974. const struct ggml_tensor * src0 = dst->src[0];
  9975. const struct ggml_tensor * src1 = dst->src[1];
  9976. if (params->ith != 0) {
  9977. return;
  9978. }
  9979. assert(ggml_is_contiguous_1(src0));
  9980. assert(ggml_is_contiguous_1(src1));
  9981. assert(ggml_is_contiguous_1(dst));
  9982. assert(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  9983. const int n = ggml_nrows(src0);
  9984. const int nc = src0->ne[0];
  9985. for (int i = 0; i < n; i++) {
  9986. fun(nc,
  9987. (float *) ((char *) dst->data + i*( dst->nb[1])),
  9988. (float *) ((char *) src0->data + i*(src0->nb[1])),
  9989. (float *) ((char *) src1->data + i*(src1->nb[1])));
  9990. }
  9991. }
  9992. static void ggml_compute_forward_map_binary(
  9993. const struct ggml_compute_params * params,
  9994. struct ggml_tensor * dst,
  9995. const ggml_binary_op_f32_t fun) {
  9996. const struct ggml_tensor * src0 = dst->src[0];
  9997. switch (src0->type) {
  9998. case GGML_TYPE_F32:
  9999. {
  10000. ggml_compute_forward_map_binary_f32(params, dst, fun);
  10001. } break;
  10002. default:
  10003. {
  10004. GGML_ABORT("fatal error");
  10005. }
  10006. }
  10007. }
  10008. // ggml_compute_forward_map_custom1
  10009. static void ggml_compute_forward_map_custom1_f32(
  10010. const struct ggml_compute_params * params,
  10011. struct ggml_tensor * dst,
  10012. const ggml_custom1_op_f32_t fun) {
  10013. const struct ggml_tensor * a = dst->src[0];
  10014. if (params->ith != 0) {
  10015. return;
  10016. }
  10017. fun(dst, a);
  10018. }
  10019. // ggml_compute_forward_map_custom2
  10020. static void ggml_compute_forward_map_custom2_f32(
  10021. const struct ggml_compute_params * params,
  10022. struct ggml_tensor * dst,
  10023. const ggml_custom2_op_f32_t fun) {
  10024. const struct ggml_tensor * a = dst->src[0];
  10025. const struct ggml_tensor * b = dst->src[1];
  10026. if (params->ith != 0) {
  10027. return;
  10028. }
  10029. fun(dst, a, b);
  10030. }
  10031. // ggml_compute_forward_map_custom3
  10032. static void ggml_compute_forward_map_custom3_f32(
  10033. const struct ggml_compute_params * params,
  10034. struct ggml_tensor * dst,
  10035. const ggml_custom3_op_f32_t fun) {
  10036. const struct ggml_tensor * a = dst->src[0];
  10037. const struct ggml_tensor * b = dst->src[1];
  10038. const struct ggml_tensor * c = dst->src[1];
  10039. if (params->ith != 0) {
  10040. return;
  10041. }
  10042. fun(dst, a, b, c);
  10043. }
  10044. // ggml_compute_forward_map_custom1
  10045. static void ggml_compute_forward_map_custom1(
  10046. const struct ggml_compute_params * params,
  10047. struct ggml_tensor * dst) {
  10048. const struct ggml_tensor * a = dst->src[0];
  10049. struct ggml_map_custom1_op_params p;
  10050. memcpy(&p, dst->op_params, sizeof(p));
  10051. p.fun(dst, a, params->ith, params->nth, p.userdata);
  10052. }
  10053. // ggml_compute_forward_map_custom2
  10054. static void ggml_compute_forward_map_custom2(
  10055. const struct ggml_compute_params * params,
  10056. struct ggml_tensor * dst) {
  10057. const struct ggml_tensor * a = dst->src[0];
  10058. const struct ggml_tensor * b = dst->src[1];
  10059. struct ggml_map_custom2_op_params p;
  10060. memcpy(&p, dst->op_params, sizeof(p));
  10061. p.fun(dst, a, b, params->ith, params->nth, p.userdata);
  10062. }
  10063. // ggml_compute_forward_map_custom3
  10064. static void ggml_compute_forward_map_custom3(
  10065. const struct ggml_compute_params * params,
  10066. struct ggml_tensor * dst) {
  10067. const struct ggml_tensor * a = dst->src[0];
  10068. const struct ggml_tensor * b = dst->src[1];
  10069. const struct ggml_tensor * c = dst->src[2];
  10070. struct ggml_map_custom3_op_params p;
  10071. memcpy(&p, dst->op_params, sizeof(p));
  10072. p.fun(dst, a, b, c, params->ith, params->nth, p.userdata);
  10073. }
  10074. // ggml_compute_forward_cross_entropy_loss
  10075. static void ggml_compute_forward_cross_entropy_loss_f32(
  10076. const struct ggml_compute_params * params,
  10077. struct ggml_tensor * dst) {
  10078. const struct ggml_tensor * src0 = dst->src[0];
  10079. const struct ggml_tensor * src1 = dst->src[1];
  10080. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10081. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10082. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  10083. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  10084. GGML_ASSERT(ggml_are_same_shape(src0, src1));
  10085. GGML_ASSERT(ggml_is_scalar(dst));
  10086. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10087. // TODO: handle transposed/permuted matrices
  10088. const int64_t nc = src0->ne[0];
  10089. const int64_t nr = ggml_nrows(src0);
  10090. const int ith = params->ith;
  10091. const int nth = params->nth;
  10092. float * sums = (float *) params->wdata;
  10093. float * st = ((float *) params->wdata) + nth + ith*nc;
  10094. float sum_thread = 0.0f;
  10095. GGML_ASSERT(params->wsize >= sizeof(float) * (nth + nth * nc));
  10096. // rows per thread
  10097. const int64_t dr = (nr + nth - 1)/nth;
  10098. // row range for this thread
  10099. const int64_t ir0 = dr*ith;
  10100. const int64_t ir1 = MIN(ir0 + dr, nr);
  10101. for (int64_t i1 = ir0; i1 < ir1; ++i1) {
  10102. const float * s0 = (const float *)((const char *) src0->data + i1*src0->nb[1]);
  10103. const float * s1 = (const float *)((const char *) src1->data + i1*src1->nb[1]);
  10104. #ifndef NDEBUG
  10105. for (int64_t i = 0; i < nc; ++i) {
  10106. //printf("p[%d] = %f\n", i, p[i]);
  10107. assert(!isnan(s0[i]));
  10108. assert(!isnan(s1[i]));
  10109. }
  10110. #endif
  10111. float max = -INFINITY;
  10112. ggml_vec_max_f32(nc, &max, s0);
  10113. const ggml_float sum_softmax = ggml_vec_log_soft_max_f32(nc, st, s0, max);
  10114. assert(sum_softmax >= 0.0);
  10115. ggml_vec_add1_f32(nc, st, st, -sum_softmax);
  10116. ggml_vec_mul_f32(nc, st, st, s1);
  10117. float sum_st = 0.0f;
  10118. ggml_vec_sum_f32(nc, &sum_st, st);
  10119. sum_thread += sum_st;
  10120. #ifndef NDEBUG
  10121. for (int64_t i = 0; i < nc; ++i) {
  10122. assert(!isnan(st[i]));
  10123. assert(!isinf(st[i]));
  10124. }
  10125. #endif
  10126. }
  10127. sums[ith] = sum_thread;
  10128. ggml_barrier(params->threadpool);
  10129. if (ith == 0) {
  10130. float * dp = (float *) dst->data;
  10131. ggml_vec_sum_f32(nth, dp, sums);
  10132. dp[0] *= -1.0f / (float) nr;
  10133. }
  10134. }
  10135. static void ggml_compute_forward_cross_entropy_loss(
  10136. const struct ggml_compute_params * params,
  10137. struct ggml_tensor * dst) {
  10138. const struct ggml_tensor * src0 = dst->src[0];
  10139. switch (src0->type) {
  10140. case GGML_TYPE_F32:
  10141. {
  10142. ggml_compute_forward_cross_entropy_loss_f32(params, dst);
  10143. } break;
  10144. default:
  10145. {
  10146. GGML_ABORT("fatal error");
  10147. }
  10148. }
  10149. }
  10150. // ggml_compute_forward_cross_entropy_loss_back
  10151. static void ggml_compute_forward_cross_entropy_loss_back_f32(
  10152. const struct ggml_compute_params * params,
  10153. struct ggml_tensor * dst) {
  10154. const struct ggml_tensor * src0 = dst->src[0];
  10155. const struct ggml_tensor * src1 = dst->src[1];
  10156. const struct ggml_tensor * opt0 = dst->src[2];
  10157. GGML_ASSERT(ggml_is_contiguous(dst));
  10158. GGML_ASSERT(ggml_is_contiguous(src0));
  10159. GGML_ASSERT(ggml_is_contiguous(src1));
  10160. GGML_ASSERT(ggml_is_contiguous(opt0));
  10161. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  10162. const int64_t ith = params->ith;
  10163. const int64_t nth = params->nth;
  10164. // TODO: handle transposed/permuted matrices
  10165. const int64_t nc = src0->ne[0];
  10166. const int64_t nr = ggml_nrows(src0);
  10167. // rows per thread
  10168. const int64_t dr = (nr + nth - 1)/nth;
  10169. // row range for this thread
  10170. const int64_t ir0 = dr*ith;
  10171. const int64_t ir1 = MIN(ir0 + dr, nr);
  10172. const float d_by_nr = ((const float *) opt0->data)[0] / (float) nr;
  10173. for (int64_t i1 = ir0; i1 < ir1; i1++) {
  10174. float * ds0 = (float *)((char *) dst->data + i1*dst->nb[1]);
  10175. float * s0 = (float *)((char *) src0->data + i1*src0->nb[1]);
  10176. float * s1 = (float *)((char *) src1->data + i1*src1->nb[1]);
  10177. #ifndef NDEBUG
  10178. for (int64_t i = 0; i < nc; ++i) {
  10179. //printf("p[%d] = %f\n", i, p[i]);
  10180. assert(!isnan(s0[i]));
  10181. assert(!isnan(s1[i]));
  10182. }
  10183. #endif
  10184. // soft_max
  10185. float max = -INFINITY;
  10186. ggml_vec_max_f32(nc, &max, s0);
  10187. ggml_float sum = ggml_vec_soft_max_f32(nc, ds0, s0, max);
  10188. assert(sum > 0.0);
  10189. ggml_vec_scale_f32(nc, ds0, 1.0/sum);
  10190. // grad(src0) = (softmax(src0) - src1) * grad(cross_entropy_loss(src0, src1)) / nr
  10191. ggml_vec_sub_f32(nc, ds0, ds0, s1);
  10192. ggml_vec_scale_f32(nc, ds0, d_by_nr);
  10193. #ifndef NDEBUG
  10194. for (int64_t i = 0; i < nc; ++i) {
  10195. assert(!isnan(ds0[i]));
  10196. assert(!isinf(ds0[i]));
  10197. }
  10198. #endif
  10199. }
  10200. }
  10201. static void ggml_compute_forward_cross_entropy_loss_back(
  10202. const struct ggml_compute_params * params,
  10203. struct ggml_tensor * dst) {
  10204. const struct ggml_tensor * src0 = dst->src[0];
  10205. switch (src0->type) {
  10206. case GGML_TYPE_F32:
  10207. {
  10208. ggml_compute_forward_cross_entropy_loss_back_f32(params, dst);
  10209. } break;
  10210. default:
  10211. {
  10212. GGML_ABORT("fatal error");
  10213. }
  10214. }
  10215. }
  10216. static void ggml_compute_forward_opt_step_adamw_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 * src0_grad = dst->src[1];
  10221. const struct ggml_tensor * src0_grad_m = dst->src[2];
  10222. const struct ggml_tensor * src0_grad_v = dst->src[3];
  10223. const struct ggml_tensor * adamw_params = dst->src[4];
  10224. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad));
  10225. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_m));
  10226. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_v));
  10227. GGML_ASSERT(ggml_nelements(adamw_params) == 7);
  10228. const int ith = params->ith;
  10229. const int nth = params->nth;
  10230. const int nr = ggml_nrows(src0);
  10231. GGML_TENSOR_UNARY_OP_LOCALS
  10232. GGML_ASSERT(nb00 == sizeof(float));
  10233. // rows per thread
  10234. const int dr = (nr + nth - 1)/nth;
  10235. // row range for this thread
  10236. const int ir0 = dr*ith;
  10237. const int ir1 = MIN(ir0 + dr, nr);
  10238. const float * adamw_params_ptr = ggml_get_data_f32(adamw_params);
  10239. const float alpha = adamw_params_ptr[0];
  10240. const float beta1 = adamw_params_ptr[1];
  10241. const float beta2 = adamw_params_ptr[2];
  10242. const float eps = adamw_params_ptr[3];
  10243. const float wd = adamw_params_ptr[4];
  10244. const float beta1h = adamw_params_ptr[5];
  10245. const float beta2h = adamw_params_ptr[6];
  10246. for (int ir = ir0; ir < ir1; ++ir) {
  10247. const int64_t i03 = ir/(ne02*ne01);
  10248. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  10249. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  10250. const size_t offset = i03*nb03 + i02*nb02 + i01*nb01;
  10251. float * w = (float *) ((char *) src0->data + offset); // weight
  10252. const float * g = (const float *) ((const char *) src0_grad->data + offset); // grad
  10253. float * m = (float *) ((char *) src0_grad_m->data + offset);
  10254. float * v = (float *) ((char *) src0_grad_v->data + offset);
  10255. for (int i00 = 0; i00 < ne00; ++i00) {
  10256. m[i00] = m[i00]*beta1 + g[i00]*(1.0f - beta1);
  10257. v[i00] = v[i00]*beta2 + g[i00]*g[i00]*(1.0f - beta2);
  10258. const float mh = m[i00]*beta1h;
  10259. const float vh = sqrtf(v[i00]*beta2h) + eps;
  10260. // The weight decay is applied independently of the Adam momenta m and v.
  10261. // This is NOT equivalent to l2 regularization that adds w[i00]*w[i00] to the loss.
  10262. // See: https://arxiv.org/pdf/1711.05101v3.pdf
  10263. w[i00] = w[i00]*(1.0f - alpha*wd) - alpha*mh/vh;
  10264. }
  10265. }
  10266. }
  10267. static void ggml_compute_forward_opt_step_adamw(
  10268. const struct ggml_compute_params * params,
  10269. struct ggml_tensor * dst) {
  10270. const struct ggml_tensor * src0 = dst->src[0];
  10271. switch (src0->type) {
  10272. case GGML_TYPE_F32:
  10273. {
  10274. ggml_compute_forward_opt_step_adamw_f32(params, dst);
  10275. } break;
  10276. default:
  10277. {
  10278. GGML_ABORT("fatal error");
  10279. }
  10280. }
  10281. }
  10282. /////////////////////////////////
  10283. static void ggml_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  10284. GGML_ASSERT(params);
  10285. if (tensor->op == GGML_OP_NONE || ggml_is_empty(tensor)) {
  10286. return;
  10287. }
  10288. // extra_buffer op?
  10289. if (ggml_cpu_extra_compute_forward(params, tensor)) return;
  10290. switch (tensor->op) {
  10291. case GGML_OP_DUP:
  10292. {
  10293. ggml_compute_forward_dup(params, tensor);
  10294. } break;
  10295. case GGML_OP_ADD:
  10296. {
  10297. ggml_compute_forward_add(params, tensor);
  10298. } break;
  10299. case GGML_OP_ADD1:
  10300. {
  10301. ggml_compute_forward_add1(params, tensor);
  10302. } break;
  10303. case GGML_OP_ACC:
  10304. {
  10305. ggml_compute_forward_acc(params, tensor);
  10306. } break;
  10307. case GGML_OP_SUB:
  10308. {
  10309. ggml_compute_forward_sub(params, tensor);
  10310. } break;
  10311. case GGML_OP_MUL:
  10312. {
  10313. ggml_compute_forward_mul(params, tensor);
  10314. } break;
  10315. case GGML_OP_DIV:
  10316. {
  10317. ggml_compute_forward_div(params, tensor);
  10318. } break;
  10319. case GGML_OP_SQR:
  10320. {
  10321. ggml_compute_forward_sqr(params, tensor);
  10322. } break;
  10323. case GGML_OP_SQRT:
  10324. {
  10325. ggml_compute_forward_sqrt(params, tensor);
  10326. } break;
  10327. case GGML_OP_LOG:
  10328. {
  10329. ggml_compute_forward_log(params, tensor);
  10330. } break;
  10331. case GGML_OP_SIN:
  10332. {
  10333. ggml_compute_forward_sin(params, tensor);
  10334. } break;
  10335. case GGML_OP_COS:
  10336. {
  10337. ggml_compute_forward_cos(params, tensor);
  10338. } break;
  10339. case GGML_OP_SUM:
  10340. {
  10341. ggml_compute_forward_sum(params, tensor);
  10342. } break;
  10343. case GGML_OP_SUM_ROWS:
  10344. {
  10345. ggml_compute_forward_sum_rows(params, tensor);
  10346. } break;
  10347. case GGML_OP_MEAN:
  10348. {
  10349. ggml_compute_forward_mean(params, tensor);
  10350. } break;
  10351. case GGML_OP_ARGMAX:
  10352. {
  10353. ggml_compute_forward_argmax(params, tensor);
  10354. } break;
  10355. case GGML_OP_COUNT_EQUAL:
  10356. {
  10357. ggml_compute_forward_count_equal(params, tensor);
  10358. } break;
  10359. case GGML_OP_REPEAT:
  10360. {
  10361. ggml_compute_forward_repeat(params, tensor);
  10362. } break;
  10363. case GGML_OP_REPEAT_BACK:
  10364. {
  10365. ggml_compute_forward_repeat_back(params, tensor);
  10366. } break;
  10367. case GGML_OP_CONCAT:
  10368. {
  10369. ggml_compute_forward_concat(params, tensor);
  10370. } break;
  10371. case GGML_OP_SILU_BACK:
  10372. {
  10373. ggml_compute_forward_silu_back(params, tensor);
  10374. } break;
  10375. case GGML_OP_NORM:
  10376. {
  10377. ggml_compute_forward_norm(params, tensor);
  10378. } break;
  10379. case GGML_OP_RMS_NORM:
  10380. {
  10381. ggml_compute_forward_rms_norm(params, tensor);
  10382. } break;
  10383. case GGML_OP_RMS_NORM_BACK:
  10384. {
  10385. ggml_compute_forward_rms_norm_back(params, tensor);
  10386. } break;
  10387. case GGML_OP_GROUP_NORM:
  10388. {
  10389. ggml_compute_forward_group_norm(params, tensor);
  10390. } break;
  10391. case GGML_OP_MUL_MAT:
  10392. {
  10393. ggml_compute_forward_mul_mat(params, tensor);
  10394. } break;
  10395. case GGML_OP_MUL_MAT_ID:
  10396. {
  10397. ggml_compute_forward_mul_mat_id(params, tensor);
  10398. } break;
  10399. case GGML_OP_OUT_PROD:
  10400. {
  10401. ggml_compute_forward_out_prod(params, tensor);
  10402. } break;
  10403. case GGML_OP_SCALE:
  10404. {
  10405. ggml_compute_forward_scale(params, tensor);
  10406. } break;
  10407. case GGML_OP_SET:
  10408. {
  10409. ggml_compute_forward_set(params, tensor);
  10410. } break;
  10411. case GGML_OP_CPY:
  10412. {
  10413. ggml_compute_forward_cpy(params, tensor);
  10414. } break;
  10415. case GGML_OP_CONT:
  10416. {
  10417. ggml_compute_forward_cont(params, tensor);
  10418. } break;
  10419. case GGML_OP_RESHAPE:
  10420. {
  10421. ggml_compute_forward_reshape(params, tensor);
  10422. } break;
  10423. case GGML_OP_VIEW:
  10424. {
  10425. ggml_compute_forward_view(params, tensor);
  10426. } break;
  10427. case GGML_OP_PERMUTE:
  10428. {
  10429. ggml_compute_forward_permute(params, tensor);
  10430. } break;
  10431. case GGML_OP_TRANSPOSE:
  10432. {
  10433. ggml_compute_forward_transpose(params, tensor);
  10434. } break;
  10435. case GGML_OP_GET_ROWS:
  10436. {
  10437. ggml_compute_forward_get_rows(params, tensor);
  10438. } break;
  10439. case GGML_OP_GET_ROWS_BACK:
  10440. {
  10441. ggml_compute_forward_get_rows_back(params, tensor);
  10442. } break;
  10443. case GGML_OP_DIAG:
  10444. {
  10445. ggml_compute_forward_diag(params, tensor);
  10446. } break;
  10447. case GGML_OP_DIAG_MASK_INF:
  10448. {
  10449. ggml_compute_forward_diag_mask_inf(params, tensor);
  10450. } break;
  10451. case GGML_OP_DIAG_MASK_ZERO:
  10452. {
  10453. ggml_compute_forward_diag_mask_zero(params, tensor);
  10454. } break;
  10455. case GGML_OP_SOFT_MAX:
  10456. {
  10457. ggml_compute_forward_soft_max(params, tensor);
  10458. } break;
  10459. case GGML_OP_SOFT_MAX_BACK:
  10460. {
  10461. ggml_compute_forward_soft_max_back(params, tensor);
  10462. } break;
  10463. case GGML_OP_ROPE:
  10464. {
  10465. ggml_compute_forward_rope(params, tensor);
  10466. } break;
  10467. case GGML_OP_ROPE_BACK:
  10468. {
  10469. ggml_compute_forward_rope_back(params, tensor);
  10470. } break;
  10471. case GGML_OP_CLAMP:
  10472. {
  10473. ggml_compute_forward_clamp(params, tensor);
  10474. } break;
  10475. case GGML_OP_CONV_TRANSPOSE_1D:
  10476. {
  10477. ggml_compute_forward_conv_transpose_1d(params, tensor);
  10478. } break;
  10479. case GGML_OP_IM2COL:
  10480. {
  10481. ggml_compute_forward_im2col(params, tensor);
  10482. } break;
  10483. case GGML_OP_IM2COL_BACK:
  10484. {
  10485. ggml_compute_forward_im2col_back_f32(params, tensor);
  10486. } break;
  10487. case GGML_OP_CONV_TRANSPOSE_2D:
  10488. {
  10489. ggml_compute_forward_conv_transpose_2d(params, tensor);
  10490. } break;
  10491. case GGML_OP_POOL_1D:
  10492. {
  10493. ggml_compute_forward_pool_1d(params, tensor);
  10494. } break;
  10495. case GGML_OP_POOL_2D:
  10496. {
  10497. ggml_compute_forward_pool_2d(params, tensor);
  10498. } break;
  10499. case GGML_OP_POOL_2D_BACK:
  10500. {
  10501. ggml_compute_forward_pool_2d_back(params, tensor);
  10502. } break;
  10503. case GGML_OP_UPSCALE:
  10504. {
  10505. ggml_compute_forward_upscale(params, tensor);
  10506. } break;
  10507. case GGML_OP_PAD:
  10508. {
  10509. ggml_compute_forward_pad(params, tensor);
  10510. } break;
  10511. case GGML_OP_PAD_REFLECT_1D:
  10512. {
  10513. ggml_compute_forward_pad_reflect_1d(params, tensor);
  10514. } break;
  10515. case GGML_OP_UNPAD:
  10516. {
  10517. ggml_compute_forward_unpad(params, tensor);
  10518. } break;
  10519. case GGML_OP_ARANGE:
  10520. {
  10521. ggml_compute_forward_arange(params, tensor);
  10522. } break;
  10523. case GGML_OP_TIMESTEP_EMBEDDING:
  10524. {
  10525. ggml_compute_forward_timestep_embedding(params, tensor);
  10526. } break;
  10527. case GGML_OP_ARGSORT:
  10528. {
  10529. ggml_compute_forward_argsort(params, tensor);
  10530. } break;
  10531. case GGML_OP_LEAKY_RELU:
  10532. {
  10533. ggml_compute_forward_leaky_relu(params, tensor);
  10534. } break;
  10535. case GGML_OP_FLASH_ATTN_EXT:
  10536. {
  10537. ggml_compute_forward_flash_attn_ext(params, tensor->src[0], tensor->src[1], tensor->src[2], tensor->src[3], tensor);
  10538. } break;
  10539. case GGML_OP_FLASH_ATTN_BACK:
  10540. {
  10541. int32_t t = ggml_get_op_params_i32(tensor, 0);
  10542. GGML_ASSERT(t == 0 || t == 1);
  10543. bool masked = t != 0;
  10544. ggml_compute_forward_flash_attn_back(params, masked, tensor);
  10545. } break;
  10546. case GGML_OP_SSM_CONV:
  10547. {
  10548. ggml_compute_forward_ssm_conv(params, tensor);
  10549. } break;
  10550. case GGML_OP_SSM_SCAN:
  10551. {
  10552. ggml_compute_forward_ssm_scan(params, tensor);
  10553. } break;
  10554. case GGML_OP_WIN_PART:
  10555. {
  10556. ggml_compute_forward_win_part(params, tensor);
  10557. } break;
  10558. case GGML_OP_WIN_UNPART:
  10559. {
  10560. ggml_compute_forward_win_unpart(params, tensor);
  10561. } break;
  10562. case GGML_OP_UNARY:
  10563. {
  10564. ggml_compute_forward_unary(params, tensor);
  10565. } break;
  10566. case GGML_OP_GET_REL_POS:
  10567. {
  10568. ggml_compute_forward_get_rel_pos(params, tensor);
  10569. } break;
  10570. case GGML_OP_ADD_REL_POS:
  10571. {
  10572. ggml_compute_forward_add_rel_pos(params, tensor);
  10573. } break;
  10574. case GGML_OP_RWKV_WKV6:
  10575. {
  10576. ggml_compute_forward_rwkv_wkv6(params, tensor);
  10577. } break;
  10578. case GGML_OP_MAP_UNARY:
  10579. {
  10580. ggml_unary_op_f32_t fun;
  10581. memcpy(&fun, tensor->op_params, sizeof(fun));
  10582. ggml_compute_forward_map_unary(params, tensor, fun);
  10583. }
  10584. break;
  10585. case GGML_OP_MAP_BINARY:
  10586. {
  10587. ggml_binary_op_f32_t fun;
  10588. memcpy(&fun, tensor->op_params, sizeof(fun));
  10589. ggml_compute_forward_map_binary(params, tensor, fun);
  10590. }
  10591. break;
  10592. case GGML_OP_MAP_CUSTOM1_F32:
  10593. {
  10594. ggml_custom1_op_f32_t fun;
  10595. memcpy(&fun, tensor->op_params, sizeof(fun));
  10596. ggml_compute_forward_map_custom1_f32(params, tensor, fun);
  10597. }
  10598. break;
  10599. case GGML_OP_MAP_CUSTOM2_F32:
  10600. {
  10601. ggml_custom2_op_f32_t fun;
  10602. memcpy(&fun, tensor->op_params, sizeof(fun));
  10603. ggml_compute_forward_map_custom2_f32(params, tensor, fun);
  10604. }
  10605. break;
  10606. case GGML_OP_MAP_CUSTOM3_F32:
  10607. {
  10608. ggml_custom3_op_f32_t fun;
  10609. memcpy(&fun, tensor->op_params, sizeof(fun));
  10610. ggml_compute_forward_map_custom3_f32(params, tensor, fun);
  10611. }
  10612. break;
  10613. case GGML_OP_MAP_CUSTOM1:
  10614. {
  10615. ggml_compute_forward_map_custom1(params, tensor);
  10616. }
  10617. break;
  10618. case GGML_OP_MAP_CUSTOM2:
  10619. {
  10620. ggml_compute_forward_map_custom2(params, tensor);
  10621. }
  10622. break;
  10623. case GGML_OP_MAP_CUSTOM3:
  10624. {
  10625. ggml_compute_forward_map_custom3(params, tensor);
  10626. }
  10627. break;
  10628. case GGML_OP_CROSS_ENTROPY_LOSS:
  10629. {
  10630. ggml_compute_forward_cross_entropy_loss(params, tensor);
  10631. }
  10632. break;
  10633. case GGML_OP_CROSS_ENTROPY_LOSS_BACK:
  10634. {
  10635. ggml_compute_forward_cross_entropy_loss_back(params, tensor);
  10636. }
  10637. break;
  10638. case GGML_OP_OPT_STEP_ADAMW:
  10639. {
  10640. ggml_compute_forward_opt_step_adamw(params, tensor);
  10641. }
  10642. break;
  10643. case GGML_OP_NONE:
  10644. {
  10645. // nop
  10646. } break;
  10647. case GGML_OP_COUNT:
  10648. {
  10649. GGML_ABORT("fatal error");
  10650. }
  10651. }
  10652. }
  10653. // Android's libc implementation "bionic" does not support setting affinity
  10654. #if defined(__gnu_linux__)
  10655. static void set_numa_thread_affinity(int thread_n) {
  10656. if (!ggml_is_numa()) {
  10657. return;
  10658. }
  10659. int node_num;
  10660. int rv;
  10661. size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus);
  10662. switch(g_state.numa.numa_strategy) {
  10663. case GGML_NUMA_STRATEGY_DISTRIBUTE:
  10664. // run thread on node_num thread_n / (threads per node)
  10665. node_num = thread_n % g_state.numa.n_nodes;
  10666. break;
  10667. case GGML_NUMA_STRATEGY_ISOLATE:
  10668. // run thread on current_node
  10669. node_num = g_state.numa.current_node;
  10670. break;
  10671. case GGML_NUMA_STRATEGY_NUMACTL:
  10672. // use the cpuset that numactl gave us
  10673. rv = pthread_setaffinity_np(pthread_self(), setsize, &g_state.numa.cpuset);
  10674. if (rv) {
  10675. fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n",strerror(rv));
  10676. }
  10677. return;
  10678. default:
  10679. return;
  10680. }
  10681. struct ggml_numa_node * node = &g_state.numa.nodes[node_num];
  10682. cpu_set_t * cpus = CPU_ALLOC(g_state.numa.total_cpus);
  10683. CPU_ZERO_S(setsize, cpus);
  10684. for (size_t i = 0; i < node->n_cpus; ++i) {
  10685. CPU_SET_S(node->cpus[i], setsize, cpus);
  10686. }
  10687. rv = pthread_setaffinity_np(pthread_self(), setsize, cpus);
  10688. if (rv) {
  10689. fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n", strerror(rv));
  10690. }
  10691. CPU_FREE(cpus);
  10692. }
  10693. static void clear_numa_thread_affinity(void) {
  10694. if (!ggml_is_numa()) {
  10695. return;
  10696. }
  10697. size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus);
  10698. cpu_set_t * cpus = CPU_ALLOC(g_state.numa.total_cpus);
  10699. CPU_ZERO_S(setsize, cpus);
  10700. for (unsigned i = 0; i < g_state.numa.total_cpus; ++i) {
  10701. CPU_SET_S(i, setsize, cpus);
  10702. }
  10703. int rv = pthread_setaffinity_np(pthread_self(), setsize, cpus);
  10704. if (rv) {
  10705. fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n", strerror(rv));
  10706. }
  10707. CPU_FREE(cpus);
  10708. }
  10709. #else
  10710. // TODO: Windows etc.
  10711. // (the linux implementation may also work on BSD, someone should test)
  10712. static void set_numa_thread_affinity(int thread_n) { UNUSED(thread_n); }
  10713. static void clear_numa_thread_affinity(void) {}
  10714. #endif
  10715. static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) {
  10716. int n_tasks = 0;
  10717. if (ggml_is_empty(node)) {
  10718. // no need to multi-thread a no-op
  10719. n_tasks = 1;
  10720. return n_tasks;
  10721. }
  10722. switch (node->op) {
  10723. case GGML_OP_CPY:
  10724. case GGML_OP_DUP:
  10725. case GGML_OP_CONT:
  10726. case GGML_OP_ADD:
  10727. case GGML_OP_ADD1:
  10728. case GGML_OP_ACC:
  10729. {
  10730. n_tasks = n_threads;
  10731. } break;
  10732. case GGML_OP_SUB:
  10733. case GGML_OP_SQR:
  10734. case GGML_OP_SQRT:
  10735. case GGML_OP_LOG:
  10736. case GGML_OP_SIN:
  10737. case GGML_OP_COS:
  10738. case GGML_OP_SUM:
  10739. case GGML_OP_SUM_ROWS:
  10740. case GGML_OP_MEAN:
  10741. case GGML_OP_ARGMAX:
  10742. {
  10743. n_tasks = 1;
  10744. } break;
  10745. case GGML_OP_COUNT_EQUAL:
  10746. {
  10747. n_tasks = n_threads;
  10748. } break;
  10749. case GGML_OP_REPEAT:
  10750. case GGML_OP_REPEAT_BACK:
  10751. case GGML_OP_LEAKY_RELU:
  10752. {
  10753. n_tasks = 1;
  10754. } break;
  10755. case GGML_OP_UNARY:
  10756. switch (ggml_get_unary_op(node)) {
  10757. case GGML_UNARY_OP_ABS:
  10758. case GGML_UNARY_OP_SGN:
  10759. case GGML_UNARY_OP_NEG:
  10760. case GGML_UNARY_OP_STEP:
  10761. case GGML_UNARY_OP_TANH:
  10762. case GGML_UNARY_OP_ELU:
  10763. case GGML_UNARY_OP_RELU:
  10764. case GGML_UNARY_OP_SIGMOID:
  10765. case GGML_UNARY_OP_HARDSWISH:
  10766. case GGML_UNARY_OP_HARDSIGMOID:
  10767. case GGML_UNARY_OP_EXP:
  10768. {
  10769. n_tasks = 1;
  10770. } break;
  10771. case GGML_UNARY_OP_GELU:
  10772. case GGML_UNARY_OP_GELU_QUICK:
  10773. case GGML_UNARY_OP_SILU:
  10774. {
  10775. n_tasks = n_threads;
  10776. } break;
  10777. default:
  10778. GGML_ABORT("fatal error");
  10779. }
  10780. break;
  10781. case GGML_OP_SILU_BACK:
  10782. case GGML_OP_MUL:
  10783. case GGML_OP_DIV:
  10784. case GGML_OP_NORM:
  10785. case GGML_OP_RMS_NORM:
  10786. case GGML_OP_RMS_NORM_BACK:
  10787. case GGML_OP_GROUP_NORM:
  10788. case GGML_OP_CONCAT:
  10789. case GGML_OP_MUL_MAT:
  10790. case GGML_OP_MUL_MAT_ID:
  10791. case GGML_OP_OUT_PROD:
  10792. {
  10793. n_tasks = n_threads;
  10794. } break;
  10795. case GGML_OP_GET_ROWS:
  10796. {
  10797. // FIXME: get_rows can use additional threads, but the cost of launching additional threads
  10798. // decreases performance with GPU offloading
  10799. //n_tasks = n_threads;
  10800. n_tasks = 1;
  10801. } break;
  10802. case GGML_OP_SCALE:
  10803. case GGML_OP_SET:
  10804. case GGML_OP_RESHAPE:
  10805. case GGML_OP_VIEW:
  10806. case GGML_OP_PERMUTE:
  10807. case GGML_OP_TRANSPOSE:
  10808. case GGML_OP_GET_ROWS_BACK:
  10809. case GGML_OP_DIAG:
  10810. {
  10811. n_tasks = 1;
  10812. } break;
  10813. case GGML_OP_DIAG_MASK_ZERO:
  10814. case GGML_OP_DIAG_MASK_INF:
  10815. case GGML_OP_SOFT_MAX_BACK:
  10816. case GGML_OP_ROPE:
  10817. case GGML_OP_ROPE_BACK:
  10818. case GGML_OP_ADD_REL_POS:
  10819. {
  10820. n_tasks = n_threads;
  10821. } break;
  10822. case GGML_OP_CLAMP:
  10823. {
  10824. n_tasks = 1; //TODO
  10825. } break;
  10826. case GGML_OP_SOFT_MAX:
  10827. {
  10828. n_tasks = MIN(n_threads, ggml_nrows(node->src[0]));
  10829. } break;
  10830. case GGML_OP_IM2COL:
  10831. case GGML_OP_IM2COL_BACK:
  10832. case GGML_OP_CONV_TRANSPOSE_1D:
  10833. case GGML_OP_CONV_TRANSPOSE_2D:
  10834. {
  10835. n_tasks = n_threads;
  10836. } break;
  10837. case GGML_OP_POOL_1D:
  10838. case GGML_OP_POOL_2D:
  10839. case GGML_OP_POOL_2D_BACK:
  10840. {
  10841. n_tasks = 1;
  10842. } break;
  10843. case GGML_OP_UPSCALE:
  10844. case GGML_OP_PAD:
  10845. case GGML_OP_PAD_REFLECT_1D:
  10846. case GGML_OP_UNPAD:
  10847. case GGML_OP_ARANGE:
  10848. case GGML_OP_TIMESTEP_EMBEDDING:
  10849. case GGML_OP_ARGSORT:
  10850. case GGML_OP_FLASH_ATTN_EXT:
  10851. case GGML_OP_FLASH_ATTN_BACK:
  10852. case GGML_OP_SSM_CONV:
  10853. case GGML_OP_SSM_SCAN:
  10854. {
  10855. n_tasks = n_threads;
  10856. } break;
  10857. case GGML_OP_WIN_PART:
  10858. case GGML_OP_WIN_UNPART:
  10859. case GGML_OP_GET_REL_POS:
  10860. case GGML_OP_RWKV_WKV6:
  10861. case GGML_OP_MAP_UNARY:
  10862. case GGML_OP_MAP_BINARY:
  10863. case GGML_OP_MAP_CUSTOM1_F32:
  10864. case GGML_OP_MAP_CUSTOM2_F32:
  10865. case GGML_OP_MAP_CUSTOM3_F32:
  10866. {
  10867. n_tasks = 1;
  10868. } break;
  10869. case GGML_OP_MAP_CUSTOM1:
  10870. {
  10871. struct ggml_map_custom1_op_params p;
  10872. memcpy(&p, node->op_params, sizeof(p));
  10873. if (p.n_tasks == GGML_N_TASKS_MAX) {
  10874. n_tasks = n_threads;
  10875. } else {
  10876. n_tasks = MIN(p.n_tasks, n_threads);
  10877. }
  10878. } break;
  10879. case GGML_OP_MAP_CUSTOM2:
  10880. {
  10881. struct ggml_map_custom2_op_params p;
  10882. memcpy(&p, node->op_params, sizeof(p));
  10883. if (p.n_tasks == GGML_N_TASKS_MAX) {
  10884. n_tasks = n_threads;
  10885. } else {
  10886. n_tasks = MIN(p.n_tasks, n_threads);
  10887. }
  10888. } break;
  10889. case GGML_OP_MAP_CUSTOM3:
  10890. {
  10891. struct ggml_map_custom3_op_params p;
  10892. memcpy(&p, node->op_params, sizeof(p));
  10893. if (p.n_tasks == GGML_N_TASKS_MAX) {
  10894. n_tasks = n_threads;
  10895. } else {
  10896. n_tasks = MIN(p.n_tasks, n_threads);
  10897. }
  10898. } break;
  10899. case GGML_OP_CROSS_ENTROPY_LOSS:
  10900. case GGML_OP_CROSS_ENTROPY_LOSS_BACK:
  10901. case GGML_OP_OPT_STEP_ADAMW:
  10902. {
  10903. n_tasks = n_threads;
  10904. } break;
  10905. case GGML_OP_NONE:
  10906. {
  10907. n_tasks = 1;
  10908. } break;
  10909. case GGML_OP_COUNT:
  10910. {
  10911. GGML_ABORT("fatal error");
  10912. }
  10913. default:
  10914. {
  10915. fprintf(stderr, "%s: op not implemented: ", __func__);
  10916. if (node->op < GGML_OP_COUNT) {
  10917. fprintf(stderr, "%s\n", ggml_op_name(node->op));
  10918. } else {
  10919. fprintf(stderr, "%d\n", node->op);
  10920. }
  10921. GGML_ABORT("fatal error");
  10922. }
  10923. }
  10924. assert(n_tasks > 0);
  10925. return n_tasks;
  10926. }
  10927. static thread_ret_t ggml_graph_compute_secondary_thread(void* data);
  10928. #if defined(_WIN32)
  10929. #include "windows.h"
  10930. // TODO: support > 64 CPUs
  10931. static bool ggml_thread_apply_affinity(bool * mask) {
  10932. HANDLE h = GetCurrentThread();
  10933. uint64_t bitmask = 0ULL;
  10934. assert(GGML_MAX_N_THREADS >= 64);
  10935. for (int32_t i = 0; i < 8; i++) {
  10936. int32_t idx = i * 8;
  10937. uint8_t val = 0;
  10938. val |= mask[idx + 0] << 0;
  10939. val |= mask[idx + 1] << 1;
  10940. val |= mask[idx + 2] << 2;
  10941. val |= mask[idx + 3] << 3;
  10942. val |= mask[idx + 4] << 4;
  10943. val |= mask[idx + 5] << 5;
  10944. val |= mask[idx + 6] << 6;
  10945. val |= mask[idx + 7] << 7;
  10946. bitmask |= (uint64_t)val << idx;
  10947. }
  10948. for (int32_t i = 64; i < GGML_MAX_N_THREADS; i++) {
  10949. if (mask[i]) {
  10950. fprintf(stderr, "warn: setting thread-affinity for > 64 CPUs isn't supported on windows!\n");
  10951. break;
  10952. }
  10953. }
  10954. DWORD_PTR m = (DWORD_PTR)bitmask;
  10955. m = SetThreadAffinityMask(h, m);
  10956. return m != 0;
  10957. }
  10958. static bool ggml_thread_apply_priority(int32_t prio) {
  10959. // Note that on Windows the Process Priority Class must be updated in order to set Thread priority.
  10960. // This is up to the applications.
  10961. DWORD p = THREAD_PRIORITY_NORMAL;
  10962. switch (prio) {
  10963. case GGML_SCHED_PRIO_NORMAL: p = THREAD_PRIORITY_NORMAL; break;
  10964. case GGML_SCHED_PRIO_MEDIUM: p = THREAD_PRIORITY_ABOVE_NORMAL; break;
  10965. case GGML_SCHED_PRIO_HIGH: p = THREAD_PRIORITY_HIGHEST; break;
  10966. case GGML_SCHED_PRIO_REALTIME: p = THREAD_PRIORITY_TIME_CRITICAL; break;
  10967. }
  10968. if (prio == GGML_SCHED_PRIO_NORMAL) {
  10969. // Keep inherited policy/priority
  10970. return true;
  10971. }
  10972. if (!SetThreadPriority(GetCurrentThread(), p)) {
  10973. fprintf(stderr, "warn: failed to set thread priority %d : (%d)\n", prio, (int) GetLastError());
  10974. return false;
  10975. }
  10976. return true;
  10977. }
  10978. #elif defined(__APPLE__)
  10979. #include <sys/types.h>
  10980. #include <sys/resource.h>
  10981. static bool ggml_thread_apply_affinity(const bool * mask) {
  10982. // Not supported on Apple platforms
  10983. UNUSED(mask);
  10984. return true;
  10985. }
  10986. static bool ggml_thread_apply_priority(int32_t prio) {
  10987. struct sched_param p;
  10988. int32_t policy = SCHED_OTHER;
  10989. switch (prio) {
  10990. case GGML_SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
  10991. case GGML_SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
  10992. case GGML_SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
  10993. case GGML_SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
  10994. }
  10995. if (prio == GGML_SCHED_PRIO_NORMAL) {
  10996. // Keep inherited policy/priority
  10997. return true;
  10998. }
  10999. int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
  11000. if (err != 0) {
  11001. fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
  11002. return false;
  11003. }
  11004. return true;
  11005. }
  11006. #elif defined(__gnu_linux__)
  11007. // TODO: this may not work on BSD, to be verified
  11008. static bool ggml_thread_apply_affinity(const bool * mask) {
  11009. cpu_set_t cpuset;
  11010. int err;
  11011. CPU_ZERO(&cpuset);
  11012. for (uint32_t i = 0; i < GGML_MAX_N_THREADS; i++) {
  11013. if (mask[i]) {
  11014. GGML_PRINT_DEBUG("Thread %lx: adding %d to cpuset\n", pthread_self(), i);
  11015. CPU_SET(i, &cpuset);
  11016. }
  11017. }
  11018. #ifdef __ANDROID__
  11019. err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
  11020. if (err < 0) {
  11021. err = errno;
  11022. }
  11023. #else
  11024. err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
  11025. #endif
  11026. if (err != 0) {
  11027. fprintf(stderr, "warn: failed to set affinity mask 0x%llx : %s (%d)\n", (unsigned long long)mask, strerror(err), err);
  11028. return false;
  11029. }
  11030. return true;
  11031. }
  11032. static bool ggml_thread_apply_priority(int32_t prio) {
  11033. struct sched_param p;
  11034. int32_t policy = SCHED_OTHER;
  11035. switch (prio) {
  11036. case GGML_SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
  11037. case GGML_SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
  11038. case GGML_SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
  11039. case GGML_SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
  11040. }
  11041. if (prio == GGML_SCHED_PRIO_NORMAL) {
  11042. // Keep inherited policy/priority
  11043. return true;
  11044. }
  11045. int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
  11046. if (err != 0) {
  11047. fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
  11048. return false;
  11049. }
  11050. return true;
  11051. }
  11052. #else // unsupported platforms
  11053. static bool ggml_thread_apply_affinity(const bool * mask) {
  11054. UNUSED(mask);
  11055. return true;
  11056. }
  11057. static bool ggml_thread_apply_priority(int32_t prio) {
  11058. UNUSED(prio);
  11059. return true;
  11060. }
  11061. #endif
  11062. static bool ggml_thread_cpumask_is_valid(const bool * mask) {
  11063. for (int i = 0; i < GGML_MAX_N_THREADS; i++) {
  11064. if (mask[i]) { return true; }
  11065. }
  11066. return false;
  11067. }
  11068. static void ggml_thread_cpumask_next(const bool * global_mask, bool * local_mask, bool strict, int32_t* iter) {
  11069. if (!strict) {
  11070. memcpy(local_mask, global_mask, GGML_MAX_N_THREADS);
  11071. return;
  11072. } else {
  11073. memset(local_mask, 0, GGML_MAX_N_THREADS);
  11074. int32_t base_idx = *iter;
  11075. for (int32_t i = 0; i < GGML_MAX_N_THREADS; i++) {
  11076. int32_t idx = base_idx + i;
  11077. if (idx >= GGML_MAX_N_THREADS) {
  11078. // Just a cheaper modulo
  11079. idx -= GGML_MAX_N_THREADS;
  11080. }
  11081. if (global_mask[idx]) {
  11082. local_mask[idx] = 1;
  11083. *iter = idx + 1;
  11084. return;
  11085. }
  11086. }
  11087. }
  11088. }
  11089. void ggml_threadpool_free(struct ggml_threadpool* threadpool) {
  11090. if (!threadpool) return;
  11091. const int n_threads = threadpool->n_threads_max;
  11092. #ifndef GGML_USE_OPENMP
  11093. struct ggml_compute_state* workers = threadpool->workers;
  11094. ggml_mutex_lock(&threadpool->mutex);
  11095. threadpool->stop = true;
  11096. threadpool->pause = false;
  11097. ggml_cond_broadcast(&threadpool->cond);
  11098. ggml_mutex_unlock(&threadpool->mutex);
  11099. for (int j = 1; j < n_threads; j++) {
  11100. int32_t rc = ggml_thread_join(workers[j].thrd, NULL);
  11101. GGML_ASSERT(rc == GGML_EXIT_SUCCESS || rc == GGML_EXIT_ABORTED);
  11102. UNUSED(rc);
  11103. }
  11104. ggml_mutex_destroy(&threadpool->mutex);
  11105. ggml_cond_destroy(&threadpool->cond);
  11106. #endif // GGML_USE_OPENMP
  11107. const size_t workers_size = sizeof(struct ggml_compute_state) * n_threads;
  11108. ggml_aligned_free(threadpool->workers, workers_size);
  11109. ggml_aligned_free(threadpool, sizeof(struct ggml_threadpool));
  11110. }
  11111. #ifndef GGML_USE_OPENMP
  11112. // pause/resume must be called under mutex
  11113. static void ggml_threadpool_pause_locked(struct ggml_threadpool * threadpool) {
  11114. GGML_PRINT_DEBUG("Pausing threadpool\n");
  11115. threadpool->pause = true;
  11116. ggml_cond_broadcast(&threadpool->cond);
  11117. }
  11118. static void ggml_threadpool_resume_locked(struct ggml_threadpool * threadpool) {
  11119. GGML_PRINT_DEBUG("Resuming threadpool\n");
  11120. threadpool->pause = false;
  11121. ggml_cond_broadcast(&threadpool->cond);
  11122. }
  11123. #endif
  11124. void ggml_threadpool_pause(struct ggml_threadpool * threadpool) {
  11125. #ifndef GGML_USE_OPENMP
  11126. ggml_mutex_lock(&threadpool->mutex);
  11127. if (!threadpool->pause) {
  11128. ggml_threadpool_pause_locked(threadpool);
  11129. }
  11130. ggml_mutex_unlock(&threadpool->mutex);
  11131. #else
  11132. UNUSED(threadpool);
  11133. #endif
  11134. }
  11135. void ggml_threadpool_resume(struct ggml_threadpool * threadpool) {
  11136. #ifndef GGML_USE_OPENMP
  11137. ggml_mutex_lock(&threadpool->mutex);
  11138. if (threadpool->pause) {
  11139. ggml_threadpool_resume_locked(threadpool);
  11140. }
  11141. ggml_mutex_unlock(&threadpool->mutex);
  11142. #else
  11143. UNUSED(threadpool);
  11144. #endif
  11145. }
  11146. struct ggml_cplan ggml_graph_plan(
  11147. const struct ggml_cgraph * cgraph,
  11148. int n_threads,
  11149. struct ggml_threadpool * threadpool) {
  11150. if (threadpool == NULL) {
  11151. //GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads);
  11152. }
  11153. if (n_threads <= 0) {
  11154. n_threads = threadpool ? threadpool->n_threads_max : GGML_DEFAULT_N_THREADS;
  11155. }
  11156. size_t work_size = 0;
  11157. struct ggml_cplan cplan;
  11158. memset(&cplan, 0, sizeof(struct ggml_cplan));
  11159. int max_tasks = 1;
  11160. // thread scheduling for the different operations + work buffer size estimation
  11161. for (int i = 0; i < cgraph->n_nodes; i++) {
  11162. struct ggml_tensor * node = cgraph->nodes[i];
  11163. const int n_tasks = ggml_get_n_tasks(node, n_threads);
  11164. max_tasks = MAX(max_tasks, n_tasks);
  11165. size_t cur = 0;
  11166. if (!ggml_cpu_extra_work_size(n_threads, node, &cur)) {
  11167. switch (node->op) {
  11168. case GGML_OP_CPY:
  11169. case GGML_OP_DUP:
  11170. {
  11171. if (ggml_is_quantized(node->type) ||
  11172. // F16 -> BF16 and BF16 -> F16 copies go through intermediate F32
  11173. (node->src[0]->type == GGML_TYPE_F16 && node->src[1] && node->src[1]->type == GGML_TYPE_BF16) ||
  11174. (node->src[0]->type == GGML_TYPE_BF16 && node->src[1] && node->src[1]->type == GGML_TYPE_F16)) {
  11175. cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
  11176. }
  11177. } break;
  11178. case GGML_OP_ADD:
  11179. case GGML_OP_ADD1:
  11180. {
  11181. if (ggml_is_quantized(node->src[0]->type)) {
  11182. cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
  11183. }
  11184. } break;
  11185. case GGML_OP_ACC:
  11186. {
  11187. if (ggml_is_quantized(node->src[0]->type)) {
  11188. cur = ggml_type_size(GGML_TYPE_F32) * node->src[1]->ne[0] * n_tasks;
  11189. }
  11190. } break;
  11191. case GGML_OP_COUNT_EQUAL:
  11192. {
  11193. cur = ggml_type_size(node->type)*n_tasks;
  11194. } break;
  11195. case GGML_OP_MUL_MAT:
  11196. {
  11197. const enum ggml_type vec_dot_type = type_traits_cpu[node->src[0]->type].vec_dot_type;
  11198. if (node->src[1]->type != vec_dot_type) {
  11199. cur = ggml_row_size(vec_dot_type, ggml_nelements(node->src[1]));
  11200. }
  11201. } break;
  11202. case GGML_OP_MUL_MAT_ID:
  11203. {
  11204. cur = 0;
  11205. const struct ggml_tensor * src0 = node->src[0];
  11206. const struct ggml_tensor * src1 = node->src[1];
  11207. const enum ggml_type vec_dot_type = type_traits_cpu[src0->type].vec_dot_type;
  11208. if (src1->type != vec_dot_type) {
  11209. cur += ggml_row_size(vec_dot_type, ggml_nelements(src1));
  11210. }
  11211. const int n_as = src0->ne[2];
  11212. cur += GGML_PAD(cur, sizeof(int64_t)); // align
  11213. cur += n_as * sizeof(int64_t); // matrix_row_counts
  11214. cur += n_as * src1->ne[2] * sizeof(int64_t); // matrix_rows
  11215. } break;
  11216. case GGML_OP_OUT_PROD:
  11217. {
  11218. if (ggml_is_quantized(node->src[0]->type)) {
  11219. cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
  11220. }
  11221. } break;
  11222. case GGML_OP_SOFT_MAX:
  11223. case GGML_OP_ROPE:
  11224. {
  11225. cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
  11226. } break;
  11227. case GGML_OP_CONV_TRANSPOSE_1D:
  11228. {
  11229. GGML_ASSERT(node->src[0]->ne[3] == 1);
  11230. GGML_ASSERT(node->src[1]->ne[2] == 1);
  11231. GGML_ASSERT(node->src[1]->ne[3] == 1);
  11232. const int64_t ne00 = node->src[0]->ne[0]; // K
  11233. const int64_t ne01 = node->src[0]->ne[1]; // Cout
  11234. const int64_t ne02 = node->src[0]->ne[2]; // Cin
  11235. const int64_t ne10 = node->src[1]->ne[0]; // L
  11236. const int64_t ne11 = node->src[1]->ne[1]; // Cin
  11237. if ((node->src[0]->type == GGML_TYPE_F16 ||
  11238. node->src[0]->type == GGML_TYPE_BF16) &&
  11239. node->src[1]->type == GGML_TYPE_F32) {
  11240. cur += sizeof(ggml_fp16_t)*ne00*ne01*ne02;
  11241. cur += sizeof(ggml_fp16_t)*ne10*ne11;
  11242. } else if (node->src[0]->type == GGML_TYPE_F32 &&
  11243. node->src[1]->type == GGML_TYPE_F32) {
  11244. cur += sizeof(float)*ne00*ne01*ne02;
  11245. cur += sizeof(float)*ne10*ne11;
  11246. } else {
  11247. GGML_ABORT("fatal error");
  11248. }
  11249. } break;
  11250. case GGML_OP_CONV_TRANSPOSE_2D:
  11251. {
  11252. const int64_t ne00 = node->src[0]->ne[0]; // W
  11253. const int64_t ne01 = node->src[0]->ne[1]; // H
  11254. const int64_t ne02 = node->src[0]->ne[2]; // Channels Out
  11255. const int64_t ne03 = node->src[0]->ne[3]; // Channels In
  11256. const int64_t ne10 = node->src[1]->ne[0]; // W
  11257. const int64_t ne11 = node->src[1]->ne[1]; // H
  11258. const int64_t ne12 = node->src[1]->ne[2]; // Channels In
  11259. cur += sizeof(ggml_fp16_t)*ne00*ne01*ne02*ne03;
  11260. cur += sizeof(ggml_fp16_t)*ne10*ne11*ne12;
  11261. } break;
  11262. case GGML_OP_FLASH_ATTN_EXT:
  11263. {
  11264. const int64_t ne00 = node->src[0]->ne[0]; // D
  11265. cur = 3*sizeof(float)*ne00*n_tasks; // 3x head size/thread
  11266. } break;
  11267. case GGML_OP_FLASH_ATTN_BACK:
  11268. {
  11269. const int64_t D = node->src[0]->ne[0];
  11270. const int64_t ne11 = ggml_up(node->src[1]->ne[1], GGML_SOFT_MAX_UNROLL);
  11271. const int64_t mxDn = MAX(D, ne11) * 2; // *2 because of S and SM in ggml_compute_forward_flash_attn_back
  11272. if (node->src[1]->type == GGML_TYPE_F32) {
  11273. cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
  11274. cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
  11275. } else if (node->src[1]->type == GGML_TYPE_F16) {
  11276. cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
  11277. cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
  11278. } else if (node->src[1]->type == GGML_TYPE_BF16) {
  11279. cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
  11280. cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
  11281. }
  11282. } break;
  11283. case GGML_OP_CROSS_ENTROPY_LOSS:
  11284. {
  11285. cur = ggml_type_size(node->type)*(n_tasks + node->src[0]->ne[0]*n_tasks);
  11286. } break;
  11287. case GGML_OP_COUNT:
  11288. {
  11289. GGML_ABORT("fatal error");
  11290. }
  11291. default:
  11292. break;
  11293. }
  11294. }
  11295. work_size = MAX(work_size, cur);
  11296. }
  11297. if (work_size > 0) {
  11298. work_size += CACHE_LINE_SIZE*(n_threads);
  11299. }
  11300. cplan.threadpool = threadpool;
  11301. cplan.n_threads = MIN(max_tasks, n_threads);
  11302. cplan.work_size = work_size;
  11303. cplan.work_data = NULL;
  11304. return cplan;
  11305. }
  11306. static thread_ret_t ggml_graph_compute_thread(void * data) {
  11307. struct ggml_compute_state * state = (struct ggml_compute_state *) data;
  11308. struct ggml_threadpool * tp = state->threadpool;
  11309. const struct ggml_cgraph * cgraph = tp->cgraph;
  11310. const struct ggml_cplan * cplan = tp->cplan;
  11311. set_numa_thread_affinity(state->ith);
  11312. struct ggml_compute_params params = {
  11313. /*.ith =*/ state->ith,
  11314. /*.nth =*/ atomic_load_explicit(&tp->n_threads_cur, memory_order_relaxed),
  11315. /*.wsize =*/ cplan->work_size,
  11316. /*.wdata =*/ cplan->work_data,
  11317. /*.threadpool=*/ tp,
  11318. };
  11319. for (int node_n = 0; node_n < cgraph->n_nodes && !tp->abort; node_n++) {
  11320. struct ggml_tensor * node = cgraph->nodes[node_n];
  11321. ggml_compute_forward(&params, node);
  11322. if (state->ith == 0 && cplan->abort_callback &&
  11323. cplan->abort_callback(cplan->abort_callback_data)) {
  11324. tp->abort = true;
  11325. tp->ec = GGML_STATUS_ABORTED;
  11326. }
  11327. ggml_barrier(state->threadpool);
  11328. }
  11329. return 0;
  11330. }
  11331. #ifndef GGML_USE_OPENMP
  11332. // check if thread is active
  11333. static inline bool ggml_graph_compute_thread_active(struct ggml_compute_state * state) {
  11334. struct ggml_threadpool * threadpool = state->threadpool;
  11335. int n_threads = atomic_load_explicit(&threadpool->n_threads_cur, memory_order_relaxed);
  11336. return (state->ith < n_threads);
  11337. }
  11338. // check if thread is ready to proceed (exit from polling or sleeping)
  11339. static inline bool ggml_graph_compute_thread_ready(struct ggml_compute_state * state) {
  11340. struct ggml_threadpool * threadpool = state->threadpool;
  11341. if (state->pending || threadpool->stop || threadpool->pause) { return true; }
  11342. // check for new graph/work
  11343. int new_graph = atomic_load_explicit(&threadpool->n_graph, memory_order_relaxed);
  11344. if (new_graph != state->last_graph) {
  11345. state->pending = ggml_graph_compute_thread_active(state);
  11346. state->last_graph = new_graph;
  11347. }
  11348. return state->pending;
  11349. }
  11350. // sync thread state after polling
  11351. static inline void ggml_graph_compute_thread_sync(struct ggml_compute_state * state) {
  11352. // TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
  11353. #ifdef GGML_TSAN_ENABLED
  11354. atomic_fetch_add_explicit(&state->threadpool->n_graph, 0, memory_order_seq_cst);
  11355. #else
  11356. atomic_thread_fence(memory_order_seq_cst);
  11357. #endif
  11358. UNUSED(state);
  11359. }
  11360. static inline bool ggml_graph_compute_poll_for_work(struct ggml_compute_state * state) {
  11361. struct ggml_threadpool * threadpool = state->threadpool;
  11362. // Skip polling for unused threads
  11363. if (!ggml_graph_compute_thread_active(state)) {
  11364. return state->pending;
  11365. }
  11366. // This seems to make 0 ... 100 a decent range for polling level across modern processors.
  11367. // Perhaps, we can adjust it dynamically based on load and things.
  11368. const uint64_t n_rounds = 1024UL * 128 * threadpool->poll;
  11369. for (uint64_t i=0; !ggml_graph_compute_thread_ready(state) && i < n_rounds; i++) {
  11370. // No new work. Keep polling.
  11371. ggml_thread_cpu_relax();
  11372. }
  11373. return state->pending;
  11374. }
  11375. static inline bool ggml_graph_compute_check_for_work(struct ggml_compute_state * state) {
  11376. struct ggml_threadpool * threadpool = state->threadpool;
  11377. if (ggml_graph_compute_poll_for_work(state)) {
  11378. ggml_graph_compute_thread_sync(state);
  11379. return state->pending;
  11380. }
  11381. ggml_mutex_lock_shared(&threadpool->mutex);
  11382. while (!ggml_graph_compute_thread_ready(state)) {
  11383. // No new work. Wait for the signal.
  11384. GGML_PRINT_DEBUG("thread #%d waiting for work (sleeping)\n", state->ith);
  11385. ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
  11386. }
  11387. ggml_mutex_unlock_shared(&threadpool->mutex);
  11388. return state->pending;
  11389. }
  11390. static thread_ret_t ggml_graph_compute_secondary_thread(void* data) {
  11391. struct ggml_compute_state * state = (struct ggml_compute_state *) data;
  11392. struct ggml_threadpool * threadpool = state->threadpool;
  11393. ggml_thread_apply_priority(threadpool->prio);
  11394. if (ggml_thread_cpumask_is_valid(state->cpumask)) {
  11395. ggml_thread_apply_affinity(state->cpumask);
  11396. }
  11397. while (true) {
  11398. // Check if we need to sleep
  11399. while (threadpool->pause) {
  11400. GGML_PRINT_DEBUG("thread #%d inside pause loop\n", state->ith);
  11401. ggml_mutex_lock_shared(&threadpool->mutex);
  11402. if (threadpool->pause) {
  11403. ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
  11404. }
  11405. GGML_PRINT_DEBUG("thread #%d resuming after wait\n", state->ith);
  11406. ggml_mutex_unlock_shared(&threadpool->mutex);
  11407. }
  11408. // This needs to be checked for after the cond_wait
  11409. if (threadpool->stop) break;
  11410. // Check if there is new work
  11411. // The main thread is the only one that can dispatch new work
  11412. ggml_graph_compute_check_for_work(state);
  11413. if (state->pending) {
  11414. state->pending = false;
  11415. ggml_graph_compute_thread(state);
  11416. }
  11417. }
  11418. return (thread_ret_t) 0;
  11419. }
  11420. // Start processing new graph
  11421. static void ggml_graph_compute_kickoff(struct ggml_threadpool * threadpool, int n_threads)
  11422. {
  11423. // Always take the mutex here because the worker threads are doing hybrid poll/wait
  11424. ggml_mutex_lock(&threadpool->mutex);
  11425. GGML_PRINT_DEBUG("threadpool: n_threads_cur %d n_threads %d\n", threadpool->n_threads_cur, n_threads);
  11426. // Update the number of active threads
  11427. atomic_store_explicit(&threadpool->n_threads_cur, n_threads, memory_order_relaxed);
  11428. // Indicate the graph is ready to be processed
  11429. // We need the full seq-cst fence here because of the polling threads (used in thread_sync)
  11430. atomic_fetch_add_explicit(&threadpool->n_graph, 1, memory_order_seq_cst);
  11431. if (threadpool->pause) {
  11432. // Update main thread prio and affinity to match the threadpool settings
  11433. ggml_thread_apply_priority(threadpool->prio);
  11434. if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
  11435. ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
  11436. }
  11437. // resume does cond broadcast
  11438. ggml_threadpool_resume_locked(threadpool);
  11439. } else {
  11440. ggml_cond_broadcast(&threadpool->cond);
  11441. }
  11442. ggml_mutex_unlock(&threadpool->mutex);
  11443. }
  11444. #endif // GGML_USE_OPENMP
  11445. static struct ggml_threadpool * ggml_threadpool_new_impl(
  11446. struct ggml_threadpool_params * tpp,
  11447. struct ggml_cgraph * cgraph,
  11448. struct ggml_cplan * cplan) {
  11449. struct ggml_threadpool * threadpool =
  11450. ggml_aligned_malloc(sizeof(struct ggml_threadpool));
  11451. {
  11452. threadpool->cgraph = cgraph;
  11453. threadpool->cplan = cplan;
  11454. threadpool->n_graph = 0;
  11455. threadpool->n_barrier = 0;
  11456. threadpool->n_barrier_passed = 0;
  11457. threadpool->current_chunk = 0;
  11458. threadpool->stop = false;
  11459. threadpool->pause = tpp->paused;
  11460. threadpool->abort = false;
  11461. threadpool->workers = NULL;
  11462. threadpool->n_threads_max = tpp->n_threads;
  11463. threadpool->n_threads_cur = tpp->n_threads;
  11464. threadpool->poll = tpp->poll;
  11465. threadpool->prio = tpp->prio;
  11466. threadpool->ec = GGML_STATUS_SUCCESS;
  11467. }
  11468. // Allocate and init workers state
  11469. const size_t workers_size = sizeof(struct ggml_compute_state) * tpp->n_threads;
  11470. struct ggml_compute_state * workers = ggml_aligned_malloc(workers_size);
  11471. memset(workers, 0, workers_size);
  11472. for (int j = 0; j < tpp->n_threads; j++) {
  11473. workers[j].threadpool = threadpool;
  11474. workers[j].ith = j;
  11475. }
  11476. threadpool->workers = workers;
  11477. #ifndef GGML_USE_OPENMP
  11478. ggml_mutex_init(&threadpool->mutex);
  11479. ggml_cond_init(&threadpool->cond);
  11480. // Spin the threads for all workers, and update CPU placements.
  11481. // Place the main thread last (towards the higher numbered CPU cores).
  11482. int32_t cpumask_iter = 0;
  11483. for (int j = 1; j < tpp->n_threads; j++) {
  11484. ggml_thread_cpumask_next(tpp->cpumask, workers[j].cpumask, tpp->strict_cpu, &cpumask_iter);
  11485. int32_t rc = ggml_thread_create(&workers[j].thrd, NULL, ggml_graph_compute_secondary_thread, &workers[j]);
  11486. GGML_ASSERT(rc == 0);
  11487. }
  11488. ggml_thread_cpumask_next(tpp->cpumask, workers[0].cpumask, tpp->strict_cpu, &cpumask_iter);
  11489. if (!threadpool->pause) {
  11490. // Update main thread prio and affinity at the start, otherwise we'll do it in resume
  11491. ggml_thread_apply_priority(threadpool->prio);
  11492. if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
  11493. ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
  11494. }
  11495. }
  11496. #endif // GGML_USE_OPENMP
  11497. return threadpool;
  11498. }
  11499. struct ggml_threadpool * ggml_threadpool_new(struct ggml_threadpool_params * tpp) {
  11500. return ggml_threadpool_new_impl(tpp, NULL, NULL);
  11501. }
  11502. enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan) {
  11503. ggml_cpu_init();
  11504. GGML_ASSERT(cplan);
  11505. GGML_ASSERT(cplan->n_threads > 0);
  11506. GGML_ASSERT(cplan->work_size == 0 || cplan->work_data != NULL);
  11507. int n_threads = cplan->n_threads;
  11508. struct ggml_threadpool * threadpool = cplan->threadpool;
  11509. bool disposable_threadpool = false;
  11510. if (threadpool == NULL) {
  11511. //GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads);
  11512. disposable_threadpool = true;
  11513. struct ggml_threadpool_params ttp = ggml_threadpool_params_default(n_threads);
  11514. threadpool = ggml_threadpool_new_impl(&ttp, cgraph, cplan);
  11515. } else {
  11516. // Reset some of the parameters that need resetting
  11517. // No worker threads should be accessing the parameters below at this stage
  11518. threadpool->cgraph = cgraph;
  11519. threadpool->cplan = cplan;
  11520. threadpool->current_chunk = 0;
  11521. threadpool->abort = false;
  11522. threadpool->ec = GGML_STATUS_SUCCESS;
  11523. }
  11524. #ifdef GGML_USE_OPENMP
  11525. if (n_threads > 1) {
  11526. #pragma omp parallel num_threads(n_threads)
  11527. {
  11528. #pragma omp single
  11529. {
  11530. // update the number of threads from the actual number of threads that we got from OpenMP
  11531. n_threads = omp_get_num_threads();
  11532. atomic_store_explicit(&threadpool->n_threads_cur, n_threads, memory_order_relaxed);
  11533. }
  11534. ggml_graph_compute_thread(&threadpool->workers[omp_get_thread_num()]);
  11535. }
  11536. } else {
  11537. atomic_store_explicit(&threadpool->n_threads_cur, 1, memory_order_relaxed);
  11538. ggml_graph_compute_thread(&threadpool->workers[0]);
  11539. }
  11540. #else
  11541. if (n_threads > threadpool->n_threads_max) {
  11542. GGML_LOG_WARN("cplan requested more threads (%d) than available (%d)\n", n_threads, threadpool->n_threads_max);
  11543. n_threads = threadpool->n_threads_max;
  11544. }
  11545. // Kick all threads to start the new graph
  11546. ggml_graph_compute_kickoff(threadpool, n_threads);
  11547. // This is a work thread too
  11548. ggml_graph_compute_thread(&threadpool->workers[0]);
  11549. #endif
  11550. // don't leave affinity set on the main thread
  11551. clear_numa_thread_affinity();
  11552. enum ggml_status ret = threadpool->ec;
  11553. if (disposable_threadpool) {
  11554. ggml_threadpool_free(threadpool);
  11555. }
  11556. return ret;
  11557. }
  11558. enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads) {
  11559. struct ggml_cplan cplan = ggml_graph_plan(cgraph, n_threads, NULL);
  11560. cplan.work_data = (uint8_t *)ggml_new_buffer(ctx, cplan.work_size);
  11561. return ggml_graph_compute(cgraph, &cplan);
  11562. }
  11563. int ggml_cpu_has_avx(void) {
  11564. #if defined(__AVX__)
  11565. return 1;
  11566. #else
  11567. return 0;
  11568. #endif
  11569. }
  11570. int ggml_cpu_has_avx_vnni(void) {
  11571. #if defined(__AVXVNNI__)
  11572. return 1;
  11573. #else
  11574. return 0;
  11575. #endif
  11576. }
  11577. int ggml_cpu_has_avx2(void) {
  11578. #if defined(__AVX2__)
  11579. return 1;
  11580. #else
  11581. return 0;
  11582. #endif
  11583. }
  11584. int ggml_cpu_has_avx512(void) {
  11585. #if defined(__AVX512F__)
  11586. return 1;
  11587. #else
  11588. return 0;
  11589. #endif
  11590. }
  11591. int ggml_cpu_has_avx512_vbmi(void) {
  11592. #if defined(__AVX512VBMI__)
  11593. return 1;
  11594. #else
  11595. return 0;
  11596. #endif
  11597. }
  11598. int ggml_cpu_has_avx512_vnni(void) {
  11599. #if defined(__AVX512VNNI__)
  11600. return 1;
  11601. #else
  11602. return 0;
  11603. #endif
  11604. }
  11605. int ggml_cpu_has_avx512_bf16(void) {
  11606. #if defined(__AVX512BF16__)
  11607. return 1;
  11608. #else
  11609. return 0;
  11610. #endif
  11611. }
  11612. int ggml_cpu_has_amx_int8(void) {
  11613. #if defined(__AMX_INT8__)
  11614. return 1;
  11615. #else
  11616. return 0;
  11617. #endif
  11618. }
  11619. int ggml_cpu_has_fma(void) {
  11620. #if defined(__FMA__)
  11621. return 1;
  11622. #else
  11623. return 0;
  11624. #endif
  11625. }
  11626. int ggml_cpu_has_arm_fma(void) {
  11627. #if defined(__ARM_FEATURE_FMA)
  11628. return 1;
  11629. #else
  11630. return 0;
  11631. #endif
  11632. }
  11633. int ggml_cpu_has_riscv_v(void) {
  11634. #if defined(__riscv_v_intrinsic)
  11635. return 1;
  11636. #else
  11637. return 0;
  11638. #endif
  11639. }
  11640. int ggml_cpu_has_f16c(void) {
  11641. #if defined(__F16C__)
  11642. return 1;
  11643. #else
  11644. return 0;
  11645. #endif
  11646. }
  11647. int ggml_cpu_has_fp16_va(void) {
  11648. #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
  11649. return 1;
  11650. #else
  11651. return 0;
  11652. #endif
  11653. }
  11654. int ggml_cpu_has_wasm_simd(void) {
  11655. #if defined(__wasm_simd128__)
  11656. return 1;
  11657. #else
  11658. return 0;
  11659. #endif
  11660. }
  11661. int ggml_cpu_has_llamafile(void) {
  11662. #if defined(GGML_USE_LLAMAFILE)
  11663. return 1;
  11664. #else
  11665. return 0;
  11666. #endif
  11667. }
  11668. int ggml_cpu_has_sse3(void) {
  11669. #if defined(__SSE3__)
  11670. return 1;
  11671. #else
  11672. return 0;
  11673. #endif
  11674. }
  11675. int ggml_cpu_has_ssse3(void) {
  11676. #if defined(__SSSE3__)
  11677. return 1;
  11678. #else
  11679. return 0;
  11680. #endif
  11681. }
  11682. int ggml_cpu_has_vsx(void) {
  11683. #if defined(__POWER9_VECTOR__)
  11684. return 1;
  11685. #else
  11686. return 0;
  11687. #endif
  11688. }
  11689. int ggml_cpu_has_neon(void) {
  11690. #if defined(__ARM_ARCH) && defined(__ARM_NEON)
  11691. return ggml_arm_arch_features.has_neon;
  11692. #else
  11693. return 0;
  11694. #endif
  11695. }
  11696. int ggml_cpu_has_dotprod(void) {
  11697. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_DOTPROD)
  11698. return ggml_arm_arch_features.has_dotprod;
  11699. #else
  11700. return 0;
  11701. #endif
  11702. }
  11703. int ggml_cpu_has_sve(void) {
  11704. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SVE)
  11705. return ggml_arm_arch_features.has_sve;
  11706. #else
  11707. return 0;
  11708. #endif
  11709. }
  11710. int ggml_cpu_has_matmul_int8(void) {
  11711. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_MATMUL_INT8)
  11712. return ggml_arm_arch_features.has_i8mm;
  11713. #else
  11714. return 0;
  11715. #endif
  11716. }
  11717. int ggml_cpu_get_sve_cnt(void) {
  11718. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SVE)
  11719. return ggml_arm_arch_features.sve_cnt;
  11720. #else
  11721. return 0;
  11722. #endif
  11723. }
  11724. void ggml_cpu_init(void) {
  11725. // needed to initialize f16 tables
  11726. {
  11727. struct ggml_init_params params = { 0, NULL, false };
  11728. struct ggml_context * ctx = ggml_init(params);
  11729. ggml_free(ctx);
  11730. }
  11731. ggml_critical_section_start();
  11732. static bool is_first_call = true;
  11733. if (is_first_call) {
  11734. // initialize GELU, Quick GELU, SILU and EXP F32 tables
  11735. {
  11736. const uint64_t t_start = ggml_time_us(); UNUSED(t_start);
  11737. for (int i = 0; i < (1 << 16); ++i) {
  11738. union {
  11739. uint16_t u16;
  11740. ggml_fp16_t fp16;
  11741. } u = {i};
  11742. float f = GGML_FP16_TO_FP32(u.fp16);
  11743. ggml_table_gelu_f16[i] = GGML_FP32_TO_FP16(ggml_gelu_f32(f));
  11744. ggml_table_gelu_quick_f16[i] = GGML_FP32_TO_FP16(ggml_gelu_quick_f32(f));
  11745. }
  11746. const uint64_t t_end = ggml_time_us(); UNUSED(t_end);
  11747. GGML_PRINT_DEBUG("%s: GELU, Quick GELU, SILU and EXP tables initialized in %f ms\n", __func__, (t_end - t_start)/1000.0);
  11748. }
  11749. #if defined(__ARM_ARCH)
  11750. ggml_init_arm_arch_features();
  11751. #endif
  11752. is_first_call = false;
  11753. }
  11754. ggml_critical_section_end();
  11755. }